From b3d2ef203ef956cc92dae5033189b3e2d62bc1a1 Mon Sep 17 00:00:00 2001
From: puddly <32534428+puddly@users.noreply.github.com>
Date: Fri, 3 May 2024 16:55:18 -0400
Subject: [PATCH] Implement a packet filter to "subscribe" to all multicast
groups
---
src/ncp-uart-hw/.cproject | 8 +-
src/ncp-uart-hw/app.c | 90 +++++++++++++++++++
.../config/packet-handoff-config.h | 78 ++++++++++++++++
src/ncp-uart-hw/ncp-uart-hw.slcp | 2 +
4 files changed, 176 insertions(+), 2 deletions(-)
create mode 100644 src/ncp-uart-hw/config/packet-handoff-config.h
diff --git a/src/ncp-uart-hw/.cproject b/src/ncp-uart-hw/.cproject
index 55474400..28171922 100644
--- a/src/ncp-uart-hw/.cproject
+++ b/src/ncp-uart-hw/.cproject
@@ -23,7 +23,7 @@
-
+
@@ -131,6 +131,7 @@
+
@@ -370,6 +373,7 @@
+
@@ -395,7 +399,7 @@
-
+
diff --git a/src/ncp-uart-hw/app.c b/src/ncp-uart-hw/app.c
index 9e7f24f5..7391e395 100644
--- a/src/ncp-uart-hw/app.c
+++ b/src/ncp-uart-hw/app.c
@@ -17,6 +17,25 @@
#include PLATFORM_HEADER
#include "ember.h"
+#include "ember-types.h"
+#include "ezsp-enum.h"
+
+#include "stack/include/message.h"
+
+
+enum CUSTOM_EZSP_CMD {
+ XNCP_CMD_GET_PROTOCOL_VERSION_REQ = 0x0000,
+ XNCP_CMD_GET_PROTOCOL_VERSION_RSP = 0x8000,
+ XNCP_CMD_GET_SUPPORTED_FEATURES_REQ = 0x0001,
+ XNCP_CMD_GET_SUPPORTED_FEATURES_RSP = 0x8001,
+};
+
+
+#define XNCP_PROTOCOL_VERSION (0x00000001)
+
+#define XNCP_FEATURE_MEMBER_OF_ALL_GROUPS (0b00000000000000000000000000000001)
+#define XNCP_SUPPORTED_FEATURES (XNCP_FEATURE_MEMBER_OF_ALL_GROUPS)
+
//----------------------
// Implemented Callbacks
@@ -36,3 +55,74 @@ void emberAfRadioNeedsCalibratingCallback(void)
void emberAfMainInitCallback(void)
{
}
+
+/** @brief Packet filter callback
+ *
+ * Filters and/or mutates incoming packets. Currently used only for wildcard multicast
+ * group membership.
+ */
+EmberPacketAction emberAfIncomingPacketFilterCallback(EmberZigbeePacketType packetType,
+ uint8_t* packetData,
+ uint8_t* size_p,
+ void* data)
+{
+ if ((packetType == EMBER_ZIGBEE_PACKET_TYPE_APS_COMMAND) && (*size_p >= 3)) {
+ uint8_t deliveryMode = (packetData[0] & 0b00001100) >> 2;
+
+ // Ensure we automatically "join" every multicast group
+ if (deliveryMode == 0x03) {
+ // Take ownership over the first entry and continuously rewrite it
+ EmberMulticastTableEntry *tableEntry = &(sl_zigbee_get_multicast_table()[0]);
+
+ tableEntry->endpoint = 1;
+ tableEntry->multicastId = (packetData[2] >> 8) | (packetData[1] >> 0);
+ tableEntry->networkIndex = 0;
+ }
+ }
+
+ return EMBER_ACCEPT_PACKET;
+}
+
+
+EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
+ uint8_t *messagePayload,
+ uint8_t *replyPayloadLength,
+ uint8_t *replyPayload) {
+ *replyPayloadLength = 0;
+
+ if (messageLength < 2) {
+ return EMBER_BAD_ARGUMENT;
+ }
+
+ uint16_t command_id = ((messagePayload[0]) << 0) | ((messagePayload[1]) << 8);
+
+ switch (command_id) {
+ case XNCP_CMD_GET_PROTOCOL_VERSION_REQ:
+ *replyPayloadLength += 2;
+ replyPayload[0] = (uint8_t)((XNCP_CMD_GET_PROTOCOL_VERSION_RSP >> 0) & 0xFF);
+ replyPayload[1] = (uint8_t)((XNCP_CMD_GET_PROTOCOL_VERSION_RSP >> 8) & 0xFF);
+
+ *replyPayloadLength += 4;
+ replyPayload[2] = (uint8_t)((XNCP_PROTOCOL_VERSION >> 0) & 0xFF);
+ replyPayload[3] = (uint8_t)((XNCP_PROTOCOL_VERSION >> 8) & 0xFF);
+ replyPayload[4] = (uint8_t)((XNCP_PROTOCOL_VERSION >> 16) & 0xFF);
+ replyPayload[5] = (uint8_t)((XNCP_PROTOCOL_VERSION >> 24) & 0xFF);
+ break;
+
+ case XNCP_CMD_GET_SUPPORTED_FEATURES_REQ:
+ *replyPayloadLength += 2;
+ replyPayload[0] = (uint8_t)((XNCP_CMD_GET_SUPPORTED_FEATURES_RSP >> 0) & 0xFF);
+ replyPayload[1] = (uint8_t)((XNCP_CMD_GET_SUPPORTED_FEATURES_RSP >> 8) & 0xFF);
+
+ *replyPayloadLength += 4;
+ replyPayload[2] = (uint8_t)((XNCP_SUPPORTED_FEATURES >> 0) & 0xFF);
+ replyPayload[3] = (uint8_t)((XNCP_SUPPORTED_FEATURES >> 8) & 0xFF);
+ replyPayload[4] = (uint8_t)((XNCP_SUPPORTED_FEATURES >> 16) & 0xFF);
+ replyPayload[5] = (uint8_t)((XNCP_SUPPORTED_FEATURES >> 24) & 0xFF);
+ break;
+ default:
+ return EMBER_BAD_ARGUMENT;
+ }
+
+ return EMBER_SUCCESS;
+}
diff --git a/src/ncp-uart-hw/config/packet-handoff-config.h b/src/ncp-uart-hw/config/packet-handoff-config.h
new file mode 100644
index 00000000..2d295f6b
--- /dev/null
+++ b/src/ncp-uart-hw/config/packet-handoff-config.h
@@ -0,0 +1,78 @@
+/***************************************************************************//**
+ * @brief Zigbee Packet Handoff component configuration header.
+ *\n*******************************************************************************
+ * # License
+ * Copyright 2020 Silicon Laboratories Inc. www.silabs.com
+ *******************************************************************************
+ *
+ * The licensor of this software is Silicon Laboratories Inc. Your use of this
+ * software is governed by the terms of Silicon Labs Master Software License
+ * Agreement (MSLA) available at
+ * www.silabs.com/about-us/legal/master-software-license-agreement. This
+ * software is distributed to you in Source Code format and is governed by the
+ * sections of the MSLA applicable to Source Code.
+ *
+ ******************************************************************************/
+
+// <<< Use Configuration Wizard in Context Menu >>>
+
+// Zigbee Packet Handoff configuration
+
+// Handoff All Packets
+// Default: TRUE
+// Allow all packets
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_ALL_PACKETS 1
+
+// Handoff Raw Mac
+// Default: FALSE
+// Allow raw mac
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_RAW_MAC 0
+
+// Handoff Mac Commands
+// Default: FALSE
+// Allow mac command
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_MAC_COMMAND 0
+
+// Handoff Network Data
+// Default: FALSE
+// Allow network data
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_NETWORK_DATA 0
+
+// Handoff Network Commands
+// Default: FALSE
+// Allow network command
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_NETWORK_COMMAND 0
+
+// Handoff APS Data
+// Default: FALSE
+// Allow aps data
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_APS_DATA 0
+
+// Handoff APS Commands
+// Default: FALSE
+// Allow aps command
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_APS_COMMAND 0
+
+// Handoff ZDO Commands
+// Default: FALSE
+// Allow zdo
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_ZDO 0
+
+// Handoff ZCL Commands
+// Default: FALSE
+// Allow zcl
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_ZCL 0
+
+// Handoff Beacons
+// Default: FALSE
+// Allow beacon
+#define EMBER_AF_PLUGIN_PACKET_HANDOFF_ALLOW_BEACON 0
+
+// Packet Handoff Buffer Size <128-512>
+// Default: 256
+// Handoff buffer size
+#define PACKET_HANDOFF_BUFFER_SIZE 256
+
+//
+
+// <<< end of configuration section >>>
diff --git a/src/ncp-uart-hw/ncp-uart-hw.slcp b/src/ncp-uart-hw/ncp-uart-hw.slcp
index 72d95dac..237c7b49 100644
--- a/src/ncp-uart-hw/ncp-uart-hw.slcp
+++ b/src/ncp-uart-hw/ncp-uart-hw.slcp
@@ -43,11 +43,13 @@ component:
- {id: zigbee_gp}
- {id: zigbee_mfglib}
- {id: zigbee_ncp_uart_hardware}
+- {id: zigbee_packet_handoff}
- {id: zigbee_pro_stack}
- {id: zigbee_r22_support}
- {id: zigbee_security_link_keys}
- {id: zigbee_source_route}
- {id: zigbee_token_interface}
+- {id: zigbee_xncp}
- {id: zigbee_zll}
define:
- {name: EMBER_CUSTOM_MAC_FILTER_TABLE_SIZE, value: '15'}