4646#define CMD_SET_CUSTOM_VAR 41
4747#define CMD_GET_ADVERT_PATH 42
4848#define CMD_GET_TUNING_PARAMS 43
49+ // NOTE: CMD range 44..49 parked, potentially for WiFi operations
50+ #define CMD_SEND_BINARY_REQ 50
51+ #define CMD_FACTORY_RESET 51
4952
5053#define RESP_CODE_OK 0
5154#define RESP_CODE_ERR 1
9396#define PUSH_CODE_TRACE_DATA 0x89
9497#define PUSH_CODE_NEW_ADVERT 0x8A
9598#define PUSH_CODE_TELEMETRY_RESPONSE 0x8B
99+ #define PUSH_CODE_BINARY_RESPONSE 0x8C
96100
97101#define ERR_CODE_UNSUPPORTED_CMD 1
98102#define ERR_CODE_NOT_FOUND 2
@@ -468,6 +472,7 @@ void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data,
468472 i += 6 ; // pub_key_prefix
469473 memcpy (&out_frame[i], &tag, 4 );
470474 i += 4 ; // NEW: include server timestamp
475+ out_frame[i++] = data[7 ]; // NEW (v7): ACL permissions
471476 } else {
472477 out_frame[i++] = PUSH_CODE_LOGIN_FAIL;
473478 out_frame[i++] = 0 ; // reserved
@@ -490,7 +495,7 @@ void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data,
490495 memcpy (&out_frame[i], &data[4 ], len - 4 );
491496 i += (len - 4 );
492497 _serial->writeFrame (out_frame, i);
493- } else if (len > 4 && tag == pending_telemetry) { // check for telemetry response
498+ } else if (len > 4 && tag == pending_telemetry) { // check for matching response tag
494499 pending_telemetry = 0 ;
495500
496501 int i = 0 ;
@@ -501,6 +506,17 @@ void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data,
501506 memcpy (&out_frame[i], &data[4 ], len - 4 );
502507 i += (len - 4 );
503508 _serial->writeFrame (out_frame, i);
509+ } else if (len > 4 && tag == pending_req) { // check for matching response tag
510+ pending_req = 0 ;
511+
512+ int i = 0 ;
513+ out_frame[i++] = PUSH_CODE_BINARY_RESPONSE;
514+ out_frame[i++] = 0 ; // reserved
515+ memcpy (&out_frame[i], &tag, 4 ); // app needs to match this to RESP_CODE_SENT.tag
516+ i += 4 ;
517+ memcpy (&out_frame[i], &data[4 ], len - 4 );
518+ i += (len - 4 );
519+ _serial->writeFrame (out_frame, i);
504520 }
505521}
506522
@@ -566,7 +582,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
566582 _cli_rescue = false ;
567583 offline_queue_len = 0 ;
568584 app_target_ver = 0 ;
569- pending_login = pending_status = pending_telemetry = 0 ;
585+ pending_login = pending_status = pending_telemetry = pending_req = 0 ;
570586 next_ack_idx = 0 ;
571587 sign_data = NULL ;
572588 dirty_contacts_expiry = 0 ;
@@ -1103,7 +1119,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11031119 if (result == MSG_SEND_FAILED) {
11041120 writeErrFrame (ERR_CODE_TABLE_FULL);
11051121 } else {
1106- pending_telemetry = pending_status = 0 ;
1122+ pending_req = pending_telemetry = pending_status = 0 ;
11071123 memcpy (&pending_login, recipient->id .pub_key , 4 ); // match this to onContactResponse()
11081124 out_frame[0 ] = RESP_CODE_SENT;
11091125 out_frame[1 ] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0 ;
@@ -1123,7 +1139,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11231139 if (result == MSG_SEND_FAILED) {
11241140 writeErrFrame (ERR_CODE_TABLE_FULL);
11251141 } else {
1126- pending_telemetry = pending_login = 0 ;
1142+ pending_req = pending_telemetry = pending_login = 0 ;
11271143 // FUTURE: pending_status = tag; // match this in onContactResponse()
11281144 memcpy (&pending_status, recipient->id .pub_key , 4 ); // legacy matching scheme
11291145 out_frame[0 ] = RESP_CODE_SENT;
@@ -1135,7 +1151,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11351151 } else {
11361152 writeErrFrame (ERR_CODE_NOT_FOUND); // contact not found
11371153 }
1138- } else if (cmd_frame[0 ] == CMD_SEND_TELEMETRY_REQ && len >= 4 + PUB_KEY_SIZE) {
1154+ } else if (cmd_frame[0 ] == CMD_SEND_TELEMETRY_REQ && len >= 4 + PUB_KEY_SIZE) { // can deprecate, in favour of CMD_SEND_BINARY_REQ
11391155 uint8_t *pub_key = &cmd_frame[4 ];
11401156 ContactInfo *recipient = lookupContactByPubKey (pub_key, PUB_KEY_SIZE);
11411157 if (recipient) {
@@ -1144,7 +1160,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11441160 if (result == MSG_SEND_FAILED) {
11451161 writeErrFrame (ERR_CODE_TABLE_FULL);
11461162 } else {
1147- pending_status = pending_login = 0 ;
1163+ pending_status = pending_login = pending_req = 0 ;
11481164 pending_telemetry = tag; // match this in onContactResponse()
11491165 out_frame[0 ] = RESP_CODE_SENT;
11501166 out_frame[1 ] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0 ;
@@ -1170,6 +1186,27 @@ void MyMesh::handleCmdFrame(size_t len) {
11701186 memcpy (&out_frame[i], telemetry.getBuffer (), tlen);
11711187 i += tlen;
11721188 _serial->writeFrame (out_frame, i);
1189+ } else if (cmd_frame[0 ] == CMD_SEND_BINARY_REQ && len >= 2 + PUB_KEY_SIZE) {
1190+ uint8_t *pub_key = &cmd_frame[1 ];
1191+ ContactInfo *recipient = lookupContactByPubKey (pub_key, PUB_KEY_SIZE);
1192+ if (recipient) {
1193+ uint8_t *req_data = &cmd_frame[1 + PUB_KEY_SIZE];
1194+ uint32_t tag, est_timeout;
1195+ int result = sendRequest (*recipient, req_data, len - (1 + PUB_KEY_SIZE), tag, est_timeout);
1196+ if (result == MSG_SEND_FAILED) {
1197+ writeErrFrame (ERR_CODE_TABLE_FULL);
1198+ } else {
1199+ pending_status = pending_login = pending_telemetry = 0 ;
1200+ pending_req = tag; // match this in onContactResponse()
1201+ out_frame[0 ] = RESP_CODE_SENT;
1202+ out_frame[1 ] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0 ;
1203+ memcpy (&out_frame[2 ], &tag, 4 );
1204+ memcpy (&out_frame[6 ], &est_timeout, 4 );
1205+ _serial->writeFrame (out_frame, 10 );
1206+ }
1207+ } else {
1208+ writeErrFrame (ERR_CODE_NOT_FOUND); // contact not found
1209+ }
11731210 } else if (cmd_frame[0 ] == CMD_HAS_CONNECTION && len >= 1 + PUB_KEY_SIZE) {
11741211 uint8_t *pub_key = &cmd_frame[1 ];
11751212 if (hasConnectionTo (pub_key)) {
@@ -1325,6 +1362,15 @@ void MyMesh::handleCmdFrame(size_t len) {
13251362 } else {
13261363 writeErrFrame (ERR_CODE_NOT_FOUND);
13271364 }
1365+ } else if (cmd_frame[0 ] == CMD_FACTORY_RESET && memcmp (&cmd_frame[1 ], " reset" , 5 ) == 0 ) {
1366+ bool success = _store->formatFileSystem ();
1367+ if (success) {
1368+ writeOKFrame ();
1369+ delay (1000 );
1370+ board.reboot (); // doesn't return
1371+ } else {
1372+ writeErrFrame (ERR_CODE_FILE_IO_ERROR);
1373+ }
13281374 } else {
13291375 writeErrFrame (ERR_CODE_UNSUPPORTED_CMD);
13301376 MESH_DEBUG_PRINTLN (" ERROR: unknown command: %02X" , cmd_frame[0 ]);
0 commit comments