Skip to content

Commit 6657ab2

Browse files
committed
Use a generic command for manufacturing token overrides
1 parent 343b512 commit 6657ab2

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

src/ncp-uart-hw/app.c

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,23 @@
3030
typedef enum {
3131
XNCP_CMD_GET_SUPPORTED_FEATURES_REQ = 0x0000,
3232
XNCP_CMD_SET_SOURCE_ROUTE_REQ = 0x0001,
33-
XNCP_CMD_GET_BOARD_NAME_REQ = 0x0002,
34-
XNCP_CMD_GET_MANUF_NAME_REQ = 0x0003,
33+
XNCP_CMD_GET_MFG_TOKEN_OVERRIDE_REQ = 0x0002,
3534

3635
XNCP_CMD_GET_SUPPORTED_FEATURES_RSP = XNCP_CMD_GET_SUPPORTED_FEATURES_REQ | 0x8000,
3736
XNCP_CMD_SET_SOURCE_ROUTE_RSP = XNCP_CMD_SET_SOURCE_ROUTE_REQ | 0x8000,
38-
XNCP_CMD_GET_BOARD_NAME_RSP = XNCP_CMD_GET_BOARD_NAME_REQ | 0x8000,
39-
XNCP_CMD_GET_MANUF_NAME_RSP = XNCP_CMD_GET_MANUF_NAME_REQ | 0x8000,
37+
XNCP_CMD_GET_MFG_TOKEN_OVERRIDE_RSP = XNCP_CMD_GET_MFG_TOKEN_OVERRIDE_REQ | 0x8000,
4038

4139
XNCP_CMD_UNKNOWN = 0xFFFF
4240
} XNCP_COMMAND;
4341

4442

4543
#define FEATURE_MEMBER_OF_ALL_GROUPS (0b00000000000000000000000000000001)
4644
#define FEATURE_MANUAL_SOURCE_ROUTE (0b00000000000000000000000000000010)
47-
#define FEATURE_BOARD_MANUF (0b00000000000000000000000000000100)
45+
#define FEATURE_MFG_TOKEN_OVERRIDES (0b00000000000000000000000000000100)
4846
#define SUPPORTED_FEATURES ( \
4947
FEATURE_MEMBER_OF_ALL_GROUPS \
5048
| FEATURE_MANUAL_SOURCE_ROUTE \
51-
| FEATURE_BOARD_MANUF \
49+
| FEATURE_MFG_TOKEN_OVERRIDES \
5250
)
5351

5452

@@ -151,22 +149,28 @@ EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
151149
uint8_t *messagePayload,
152150
uint8_t *replyPayloadLength,
153151
uint8_t *replyPayload) {
154-
*replyPayloadLength = 0;
155-
156-
uint8_t rsp_status = EMBER_ERR_FATAL;
157-
uint16_t req_command_id = XNCP_CMD_UNKNOWN;
152+
uint8_t rsp_status = EMBER_SUCCESS;
158153
uint16_t rsp_command_id = XNCP_CMD_UNKNOWN;
159154

160-
if (messageLength >= 3) {
161-
rsp_status = EMBER_SUCCESS;
162-
req_command_id = BUILD_UINT16(messagePayload[0], messagePayload[1]);
163-
164-
uint8_t req_status = messagePayload[2];
165-
(void)req_status;
155+
if (messageLength < 3) {
156+
rsp_status = EMBER_BAD_ARGUMENT;
166157

167-
messagePayload += 3;
158+
replyPayload[0] = (uint8_t)((rsp_command_id >> 0) & 0xFF);
159+
replyPayload[1] = (uint8_t)((rsp_command_id >> 8) & 0xFF);
160+
replyPayload[2] = rsp_status;
161+
return EMBER_SUCCESS;
168162
}
169163

164+
uint16_t req_command_id = BUILD_UINT16(messagePayload[0], messagePayload[1]);
165+
uint8_t req_status = messagePayload[2];
166+
(void)req_status;
167+
168+
// Strip the packet header to simplify command parsing below
169+
messagePayload += 3;
170+
messageLength -= 3;
171+
172+
// Leave space for the reply packet header
173+
*replyPayloadLength = 0;
170174
*replyPayloadLength += 2; // Space for the response command ID
171175
*replyPayloadLength += 1; // Space for the status
172176

@@ -186,12 +190,12 @@ EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
186190
rsp_command_id = XNCP_CMD_SET_SOURCE_ROUTE_RSP;
187191
rsp_status = EMBER_SUCCESS;
188192

189-
if ((messageLength < 4) || (messageLength % 2 != 0)) {
193+
if (messageLength % 2 != 0) {
190194
rsp_status = EMBER_BAD_ARGUMENT;
191195
break;
192196
}
193197

194-
uint8_t num_relays = (messageLength - 4) / 2;
198+
uint8_t num_relays = messageLength / 2;
195199

196200
if (num_relays > EMBER_MAX_SOURCE_ROUTE_RELAY_COUNT + 1) {
197201
rsp_status = EMBER_BAD_ARGUMENT;
@@ -200,7 +204,7 @@ EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
200204

201205
// If we don't find a better index, pick one at random to replace
202206
uint8_t insertion_index = emberGetPseudoRandomNumber() % XNCP_MANUAL_SOURCE_ROUTE_TABLE_SIZE;
203-
uint16_t node_id = BUILD_UINT16(messagePayload[2], messagePayload[3]);
207+
uint16_t node_id = BUILD_UINT16(messagePayload[0], messagePayload[1]);
204208

205209
for (uint8_t i = 0; i < XNCP_MANUAL_SOURCE_ROUTE_TABLE_SIZE; i++) {
206210
ManualSourceRoute *route = &manual_source_routes[i];
@@ -216,7 +220,7 @@ EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
216220
ManualSourceRoute *route = &manual_source_routes[insertion_index];
217221

218222
for (uint8_t i = 0; i < num_relays; i++) {
219-
uint16_t relay = BUILD_UINT16(messagePayload[4 + 2 * i + 0], messagePayload[4 + 2 * i + 1]);
223+
uint16_t relay = BUILD_UINT16(messagePayload[2 + 2 * i + 0], messagePayload[2 + 2 * i + 1]);
220224
route->relays[i] = relay;
221225
}
222226

@@ -227,23 +231,39 @@ EmberStatus emberAfPluginXncpIncomingCustomFrameCallback(uint8_t messageLength,
227231
break;
228232
}
229233

230-
case XNCP_CMD_GET_BOARD_NAME_REQ: {
231-
rsp_command_id = XNCP_CMD_GET_BOARD_NAME_RSP;
234+
case XNCP_CMD_GET_MFG_TOKEN_OVERRIDE_REQ: {
235+
rsp_command_id = XNCP_CMD_GET_MFG_TOKEN_OVERRIDE_RSP;
232236
rsp_status = EMBER_SUCCESS;
233237

234-
uint8_t name_length = strlen(XNCP_BOARD_NAME);
235-
*replyPayloadLength += name_length;
236-
memcpy(replyPayload, XNCP_BOARD_NAME, name_length);
237-
break;
238-
}
238+
if (messageLength != 1) {
239+
rsp_status = EMBER_BAD_ARGUMENT;
240+
break;
241+
}
239242

240-
case XNCP_CMD_GET_MANUF_NAME_REQ: {
241-
rsp_command_id = XNCP_CMD_GET_MANUF_NAME_RSP;
242-
rsp_status = EMBER_SUCCESS;
243+
uint8_t token_id = messagePayload[0];
244+
char *override_value;
245+
246+
switch (token_id) {
247+
case EZSP_MFG_STRING: {
248+
override_value = XNCP_MFG_MANUF_NAME;
249+
break;
250+
}
251+
252+
case EZSP_MFG_BOARD_NAME: {
253+
override_value = XNCP_MFG_BOARD_NAME;
254+
break;
255+
}
256+
257+
default: {
258+
rsp_status = EMBER_NOT_FOUND;
259+
override_value = "";
260+
break;
261+
}
262+
}
243263

244-
uint8_t name_length = strlen(XNCP_MANUF_NAME);
245-
*replyPayloadLength += name_length;
246-
memcpy(replyPayload, XNCP_MANUF_NAME, name_length);
264+
uint8_t value_length = strlen(override_value);
265+
memcpy(replyPayload + *replyPayloadLength, override_value, value_length);
266+
*replyPayloadLength += value_length;
247267
break;
248268
}
249269

src/ncp-uart-hw/config/xncp_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Some manufacturers do not write a board or manufacturer name to the NCP.
1111
// Rather than writing the manufacturing tokens within the application, you can instead
1212
// supply overrides that will be preferred to the manufacturing token values.
13-
#define XNCP_BOARD_NAME ("")
14-
#define XNCP_MANUF_NAME ("")
13+
#define XNCP_MFG_MANUF_NAME ("")
14+
#define XNCP_MFG_BOARD_NAME ("")
1515

1616
#endif /* CONFIG_XNCP_CONFIG_H_ */

0 commit comments

Comments
 (0)