Skip to content

Commit df33321

Browse files
author
Scott Powell
committed
* companion: added CMD_SEND_BINARY_REQ (50)
1 parent 2c9a2ee commit df33321

File tree

4 files changed

+90
-33
lines changed

4 files changed

+90
-33
lines changed

examples/companion_radio/MyMesh.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
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
4951

5052
#define RESP_CODE_OK 0
5153
#define RESP_CODE_ERR 1
@@ -92,7 +94,7 @@
9294
#define PUSH_CODE_LOG_RX_DATA 0x88
9395
#define PUSH_CODE_TRACE_DATA 0x89
9496
#define PUSH_CODE_NEW_ADVERT 0x8A
95-
#define PUSH_CODE_TELEMETRY_RESPONSE 0x8B
97+
#define PUSH_CODE_BINARY_RESPONSE 0x8B // was 'PUSH_CODE_TELEMETRY_RESPONSE'
9698

9799
#define ERR_CODE_UNSUPPORTED_CMD 1
98100
#define ERR_CODE_NOT_FOUND 2
@@ -490,11 +492,11 @@ void MyMesh::onContactResponse(const ContactInfo &contact, const uint8_t *data,
490492
memcpy(&out_frame[i], &data[4], len - 4);
491493
i += (len - 4);
492494
_serial->writeFrame(out_frame, i);
493-
} else if (len > 4 && tag == pending_telemetry) { // check for telemetry response
494-
pending_telemetry = 0;
495+
} else if (len > 4 && tag == pending_req) { // check for matching response tag
496+
pending_req = 0;
495497

496498
int i = 0;
497-
out_frame[i++] = PUSH_CODE_TELEMETRY_RESPONSE;
499+
out_frame[i++] = PUSH_CODE_BINARY_RESPONSE;
498500
out_frame[i++] = 0; // reserved
499501
memcpy(&out_frame[i], contact.id.pub_key, 6);
500502
i += 6; // pub_key_prefix
@@ -566,7 +568,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
566568
_cli_rescue = false;
567569
offline_queue_len = 0;
568570
app_target_ver = 0;
569-
pending_login = pending_status = pending_telemetry = 0;
571+
pending_login = pending_status = pending_req = 0;
570572
next_ack_idx = 0;
571573
sign_data = NULL;
572574
dirty_contacts_expiry = 0;
@@ -1103,7 +1105,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11031105
if (result == MSG_SEND_FAILED) {
11041106
writeErrFrame(ERR_CODE_TABLE_FULL);
11051107
} else {
1106-
pending_telemetry = pending_status = 0;
1108+
pending_req = pending_status = 0;
11071109
memcpy(&pending_login, recipient->id.pub_key, 4); // match this to onContactResponse()
11081110
out_frame[0] = RESP_CODE_SENT;
11091111
out_frame[1] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0;
@@ -1123,7 +1125,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11231125
if (result == MSG_SEND_FAILED) {
11241126
writeErrFrame(ERR_CODE_TABLE_FULL);
11251127
} else {
1126-
pending_telemetry = pending_login = 0;
1128+
pending_req = pending_login = 0;
11271129
// FUTURE: pending_status = tag; // match this in onContactResponse()
11281130
memcpy(&pending_status, recipient->id.pub_key, 4); // legacy matching scheme
11291131
out_frame[0] = RESP_CODE_SENT;
@@ -1135,7 +1137,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11351137
} else {
11361138
writeErrFrame(ERR_CODE_NOT_FOUND); // contact not found
11371139
}
1138-
} else if (cmd_frame[0] == CMD_SEND_TELEMETRY_REQ && len >= 4 + PUB_KEY_SIZE) {
1140+
} else if (cmd_frame[0] == CMD_SEND_TELEMETRY_REQ && len >= 4 + PUB_KEY_SIZE) { // can deprecate, in favour of CMD_SEND_BINARY_REQ
11391141
uint8_t *pub_key = &cmd_frame[4];
11401142
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
11411143
if (recipient) {
@@ -1145,7 +1147,7 @@ void MyMesh::handleCmdFrame(size_t len) {
11451147
writeErrFrame(ERR_CODE_TABLE_FULL);
11461148
} else {
11471149
pending_status = pending_login = 0;
1148-
pending_telemetry = tag; // match this in onContactResponse()
1150+
pending_req = tag; // match this in onContactResponse()
11491151
out_frame[0] = RESP_CODE_SENT;
11501152
out_frame[1] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0;
11511153
memcpy(&out_frame[2], &tag, 4);
@@ -1162,14 +1164,35 @@ void MyMesh::handleCmdFrame(size_t len) {
11621164
sensors.querySensors(0xFF, telemetry);
11631165

11641166
int i = 0;
1165-
out_frame[i++] = PUSH_CODE_TELEMETRY_RESPONSE;
1167+
out_frame[i++] = PUSH_CODE_BINARY_RESPONSE;
11661168
out_frame[i++] = 0; // reserved
11671169
memcpy(&out_frame[i], self_id.pub_key, 6);
11681170
i += 6; // pub_key_prefix
11691171
uint8_t tlen = telemetry.getSize();
11701172
memcpy(&out_frame[i], telemetry.getBuffer(), tlen);
11711173
i += tlen;
11721174
_serial->writeFrame(out_frame, i);
1175+
} else if (cmd_frame[0] == CMD_SEND_BINARY_REQ && len >= 2 + PUB_KEY_SIZE) {
1176+
uint8_t *pub_key = &cmd_frame[1];
1177+
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
1178+
if (recipient) {
1179+
uint8_t *req_data = &cmd_frame[1 + PUB_KEY_SIZE];
1180+
uint32_t tag, est_timeout;
1181+
int result = sendRequest(*recipient, req_data, len - (1 + PUB_KEY_SIZE), tag, est_timeout);
1182+
if (result == MSG_SEND_FAILED) {
1183+
writeErrFrame(ERR_CODE_TABLE_FULL);
1184+
} else {
1185+
pending_status = pending_login = 0;
1186+
pending_req = tag; // match this in onContactResponse()
1187+
out_frame[0] = RESP_CODE_SENT;
1188+
out_frame[1] = (result == MSG_SEND_SENT_FLOOD) ? 1 : 0;
1189+
memcpy(&out_frame[2], &tag, 4);
1190+
memcpy(&out_frame[6], &est_timeout, 4);
1191+
_serial->writeFrame(out_frame, 10);
1192+
}
1193+
} else {
1194+
writeErrFrame(ERR_CODE_NOT_FOUND); // contact not found
1195+
}
11731196
} else if (cmd_frame[0] == CMD_HAS_CONNECTION && len >= 1 + PUB_KEY_SIZE) {
11741197
uint8_t *pub_key = &cmd_frame[1];
11751198
if (hasConnectionTo(pub_key)) {

examples/companion_radio/MyMesh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
160160
NodePrefs _prefs;
161161
uint32_t pending_login;
162162
uint32_t pending_status;
163-
uint32_t pending_telemetry;
163+
uint32_t pending_req; // pending _BINARY_REQ (or legacy _TELEMETRY_REQ)
164164
BaseSerialInterface *_serial;
165165

166166
ContactsIterator _iter;

src/helpers/BaseChatMesh.cpp

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,52 @@ bool BaseChatMesh::importContact(const uint8_t src_buf[], uint8_t len) {
403403
}
404404

405405
int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password, uint32_t& est_timeout) {
406-
int tlen;
407-
uint8_t temp[24];
408-
uint32_t now = getRTCClock()->getCurrentTimeUnique();
409-
memcpy(temp, &now, 4); // mostly an extra blob to help make packet_hash unique
410-
if (recipient.type == ADV_TYPE_ROOM) {
411-
memcpy(&temp[4], &recipient.sync_since, 4);
412-
int len = strlen(password); if (len > 15) len = 15; // max 15 chars currently
413-
memcpy(&temp[8], password, len);
414-
tlen = 8 + len;
415-
} else {
416-
int len = strlen(password); if (len > 15) len = 15; // max 15 chars currently
417-
memcpy(&temp[4], password, len);
418-
tlen = 4 + len;
406+
mesh::Packet* pkt;
407+
{
408+
int tlen;
409+
uint8_t temp[24];
410+
uint32_t now = getRTCClock()->getCurrentTimeUnique();
411+
memcpy(temp, &now, 4); // mostly an extra blob to help make packet_hash unique
412+
if (recipient.type == ADV_TYPE_ROOM) {
413+
memcpy(&temp[4], &recipient.sync_since, 4);
414+
int len = strlen(password); if (len > 15) len = 15; // max 15 chars currently
415+
memcpy(&temp[8], password, len);
416+
tlen = 8 + len;
417+
} else {
418+
int len = strlen(password); if (len > 15) len = 15; // max 15 chars currently
419+
memcpy(&temp[4], password, len);
420+
tlen = 4 + len;
421+
}
422+
423+
pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.shared_secret, temp, tlen);
424+
}
425+
if (pkt) {
426+
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
427+
if (recipient.out_path_len < 0) {
428+
sendFlood(pkt);
429+
est_timeout = calcFloodTimeoutMillisFor(t);
430+
return MSG_SEND_SENT_FLOOD;
431+
} else {
432+
sendDirect(pkt, recipient.out_path, recipient.out_path_len);
433+
est_timeout = calcDirectTimeoutMillisFor(t, recipient.out_path_len);
434+
return MSG_SEND_SENT_DIRECT;
435+
}
419436
}
437+
return MSG_SEND_FAILED;
438+
}
439+
440+
int BaseChatMesh::sendRequest(const ContactInfo& recipient, const uint8_t* req_data, uint8_t data_len, uint32_t& tag, uint32_t& est_timeout) {
441+
if (data_len > MAX_PACKET_PAYLOAD - 16) return MSG_SEND_FAILED;
420442

421-
auto pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.shared_secret, temp, tlen);
443+
mesh::Packet* pkt;
444+
{
445+
uint8_t temp[MAX_PACKET_PAYLOAD];
446+
tag = getRTCClock()->getCurrentTimeUnique();
447+
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
448+
memcpy(&temp[4], req_data, data_len);
449+
450+
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, 4 + data_len);
451+
}
422452
if (pkt) {
423453
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
424454
if (recipient.out_path_len < 0) {
@@ -435,14 +465,17 @@ int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password,
435465
}
436466

437467
int BaseChatMesh::sendRequest(const ContactInfo& recipient, uint8_t req_type, uint32_t& tag, uint32_t& est_timeout) {
438-
uint8_t temp[13];
439-
tag = getRTCClock()->getCurrentTimeUnique();
440-
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
441-
temp[4] = req_type;
442-
memset(&temp[5], 0, 4); // reserved (possibly for 'since' param)
443-
getRNG()->random(&temp[9], 4); // random blob to help make packet-hash unique
444-
445-
auto pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, sizeof(temp));
468+
mesh::Packet* pkt;
469+
{
470+
uint8_t temp[13];
471+
tag = getRTCClock()->getCurrentTimeUnique();
472+
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
473+
temp[4] = req_type;
474+
memset(&temp[5], 0, 4); // reserved (possibly for 'since' param)
475+
getRNG()->random(&temp[9], 4); // random blob to help make packet-hash unique
476+
477+
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, sizeof(temp));
478+
}
446479
if (pkt) {
447480
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
448481
if (recipient.out_path_len < 0) {

src/helpers/BaseChatMesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class BaseChatMesh : public mesh::Mesh {
134134
bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len);
135135
int sendLogin(const ContactInfo& recipient, const char* password, uint32_t& est_timeout);
136136
int sendRequest(const ContactInfo& recipient, uint8_t req_type, uint32_t& tag, uint32_t& est_timeout);
137+
int sendRequest(const ContactInfo& recipient, const uint8_t* req_data, uint8_t data_len, uint32_t& tag, uint32_t& est_timeout);
137138
bool shareContactZeroHop(const ContactInfo& contact);
138139
uint8_t exportContact(const ContactInfo& contact, uint8_t dest_buf[]);
139140
bool importContact(const uint8_t src_buf[], uint8_t len);

0 commit comments

Comments
 (0)