Skip to content

Commit e3008c6

Browse files
committed
feat: improve error handling with ERR_NSCM and data hints
Add ERR_NSCM (0xC0) error code to distinguish unrecognized commands from packet errors. Update ERR_PCKT description to clarify that the command was recognized but requires additional data. The raw command now provides helpful hints when ERR_PCKT is returned, showing the required data format and example usage for known commands. Error distinction: 0xC0 (ERR_NSCM) = command not recognized by bootloader 0xC1 (ERR_PCKT) = command recognized but data missing/invalid Signed-off-by: Vincent Jardin <vjardin@free.fr>
1 parent 491b847 commit e3008c6

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/radfu.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,59 @@ ra_raw_cmd(ra_device_t *dev, uint8_t cmd, const uint8_t *data, size_t data_len)
27052705
/* Check if response indicates error */
27062706
if (n >= 4 && (resp[3] & 0x80)) {
27072707
printf("\n*** Response indicates ERROR ***\n");
2708+
2709+
/* Provide hints for ERR_PCKT (command recognized but needs data) */
2710+
if (n >= 5 && resp[4] == ERR_PCKT) {
2711+
uint8_t orig_cmd = resp[3] & 0x7F;
2712+
printf("\nHint: Command 0x%02X is recognized but requires data.\n", orig_cmd);
2713+
switch (orig_cmd) {
2714+
case 0x12: /* ERA */
2715+
case 0x13: /* WRI */
2716+
case 0x15: /* REA */
2717+
case 0x18: /* CRC */
2718+
printf(" Required: SAD[4] + EAD[4] (8 bytes, big-endian addresses)\n");
2719+
printf(" Example: radfu raw 0x%02X 00 00 00 00 00 00 00 FF\n", orig_cmd);
2720+
break;
2721+
case 0x34: /* BAU */
2722+
printf(" Required: BRT[4] (4 bytes, big-endian baud rate)\n");
2723+
printf(" Example: radfu raw 0x34 00 01 C2 00 (115200 bps)\n");
2724+
break;
2725+
case 0x3B: /* ARE */
2726+
printf(" Required: NUM[1] (1 byte, area number 0-3)\n");
2727+
printf(" Example: radfu raw 0x3B 00\n");
2728+
break;
2729+
case 0x30: /* AUTH/IDA */
2730+
printf(" Required: ID code (16 bytes) or SDLM+DDLM+CHCT (3 bytes)\n");
2731+
break;
2732+
case 0x4E: /* BND_SET */
2733+
printf(" Required: CFS1[2]+CFS2[2]+DFS[2]+SRS1[2]+SRS2[2] (10 bytes)\n");
2734+
break;
2735+
case 0x50: /* INI */
2736+
printf(" Required: SDLM[1]+DDLM[1] (2 bytes)\n");
2737+
break;
2738+
case 0x51: /* PRM_SET */
2739+
printf(" Required: PMID[1]+PMDT[1] (2 bytes)\n");
2740+
break;
2741+
case 0x52: /* PRM */
2742+
printf(" Required: PMID[1] (1 byte, parameter ID)\n");
2743+
printf(" Example: radfu raw 0x52 01\n");
2744+
break;
2745+
case 0x71: /* DLM_TRANSIT */
2746+
printf(" Required: SDLM[1]+DDLM[1] (2 bytes)\n");
2747+
break;
2748+
case 0x28: /* KEY */
2749+
printf(" Required: KYTY[1]+wrapped_key (1+80 bytes)\n");
2750+
break;
2751+
case 0x29: /* KEY_VFY */
2752+
case 0x2B: /* UKEY_VFY */
2753+
printf(" Required: KYID[1] (1 byte, key index)\n");
2754+
printf(" Example: radfu raw 0x%02X 01\n", orig_cmd);
2755+
break;
2756+
default:
2757+
printf(" Check protocol documentation for required data format.\n");
2758+
break;
2759+
}
2760+
}
27082761
return -1;
27092762
}
27102763

src/rapacker.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ static const struct {
1616
const char *name;
1717
const char *desc;
1818
} error_codes[] = {
19-
{ ERR_UNSU, "ERR_UNSU", "unsupported command" },
20-
{ ERR_PCKT, "ERR_PCKT", "packet error (length/ETX)" },
19+
{ ERR_UNSU, "ERR_UNSU", "unsupported command" },
20+
{ ERR_NSCM, "ERR_NSCM", "unrecognized command" },
21+
{ ERR_PCKT, "ERR_PCKT", "packet error (command recognized, need data)" },
2122
{ ERR_CHKS, "ERR_CHKS", "checksum mismatch" },
2223
{ ERR_FLOW, "ERR_FLOW", "command flow error" },
2324
{ ERR_ADDR, "ERR_ADDR", "invalid address" },

src/rapacker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
/* MCU error codes */
4242
#define ERR_UNSU 0x0C /* Unsupported command */
43-
#define ERR_PCKT 0xC1 /* Packet error (length/ETX) */
43+
#define ERR_NSCM 0xC0 /* Unrecognized command (not supported by bootloader) */
44+
#define ERR_PCKT 0xC1 /* Packet error (command recognized, data missing/invalid) */
4445
#define ERR_CHKS 0xC2 /* Checksum mismatch */
4546
#define ERR_FLOW 0xC3 /* Command flow error */
4647
#define ERR_ADDR 0xD0 /* Invalid address */

tests/test_rapacker.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ test_strdesc(void **state) {
241241

242242
/* Test error descriptions */
243243
assert_string_equal(ra_strdesc(ERR_UNSU), "unsupported command");
244-
assert_string_equal(ra_strdesc(ERR_PCKT), "packet error (length/ETX)");
244+
assert_string_equal(ra_strdesc(ERR_NSCM), "unrecognized command");
245+
assert_string_equal(ra_strdesc(ERR_PCKT), "packet error (command recognized, need data)");
245246
assert_string_equal(ra_strdesc(ERR_CHKS), "checksum mismatch");
246247
assert_string_equal(ra_strdesc(ERR_FLOW), "command flow error");
247248
assert_string_equal(ra_strdesc(ERR_ADDR), "invalid address");

0 commit comments

Comments
 (0)