Skip to content

Commit 74dea26

Browse files
author
Scott Powell
committed
* proposed change for re-trying reciprocal path transmit
1 parent 6a9dedf commit 74dea26

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

examples/companion_radio/MyMesh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void MyMesh::onContactPathUpdated(const ContactInfo &contact) {
294294
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
295295
}
296296

297-
bool MyMesh::processAck(const uint8_t *data) {
297+
ContactInfo* MyMesh::processAck(const uint8_t *data) {
298298
// see if matches any in a table
299299
for (int i = 0; i < EXPECTED_ACK_TABLE_SIZE; i++) {
300300
if (memcmp(data, &expected_ack_table[i].ack, 4) == 0) { // got an ACK from recipient
@@ -306,7 +306,7 @@ bool MyMesh::processAck(const uint8_t *data) {
306306

307307
// NOTE: the same ACK can be received multiple times!
308308
expected_ack_table[i].ack = 0; // clear expected hash, now that we have received ACK
309-
return true;
309+
return expected_ack_table[i].contact;
310310
}
311311
}
312312
return checkConnectionsAck(data);
@@ -825,6 +825,7 @@ void MyMesh::handleCmdFrame(size_t len) {
825825
if (expected_ack) {
826826
expected_ack_table[next_ack_idx].msg_sent = _ms->getMillis(); // add to circular table
827827
expected_ack_table[next_ack_idx].ack = expected_ack;
828+
expected_ack_table[next_ack_idx].contact = recipient;
828829
next_ack_idx = (next_ack_idx + 1) % EXPECTED_ACK_TABLE_SIZE;
829830
}
830831

examples/companion_radio/MyMesh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
112112
bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
113113
void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
114114
void onContactPathUpdated(const ContactInfo &contact) override;
115-
bool processAck(const uint8_t *data) override;
115+
ContactInfo* processAck(const uint8_t *data) override;
116116
void queueMessage(const ContactInfo &from, uint8_t txt_type, mesh::Packet *pkt, uint32_t sender_timestamp,
117117
const uint8_t *extra, int extra_len, const char *text);
118118

@@ -205,6 +205,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
205205
struct AckTableEntry {
206206
unsigned long msg_sent;
207207
uint32_t ack;
208+
ContactInfo* contact;
208209
};
209210
#define EXPECTED_ACK_TABLE_SIZE 8
210211
AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table

examples/simple_secure_chat/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,18 @@ class MyMesh : public BaseChatMesh, ContactVisitor {
217217
saveContacts();
218218
}
219219

220-
bool processAck(const uint8_t *data) override {
220+
ContactInfo* processAck(const uint8_t *data) override {
221221
if (memcmp(data, &expected_ack_crc, 4) == 0) { // got an ACK from recipient
222222
Serial.printf(" Got ACK! (round trip: %d millis)\n", _ms->getMillis() - last_msg_sent);
223223
// NOTE: the same ACK can be received multiple times!
224224
expected_ack_crc = 0; // reset our expected hash, now that we have received ACK
225-
return true;
225+
return NULL; // TODO: really should return ContactInfo pointer
226226
}
227227

228228
//uint32_t crc;
229229
//memcpy(&crc, data, 4);
230230
//MESH_DEBUG_PRINTLN("unknown ACK received: %08X (expected: %08X)", crc, expected_ack_crc);
231-
return false;
231+
return NULL;
232232
}
233233

234234
void onMessageRecv(const ContactInfo& from, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) override {

src/helpers/BaseChatMesh.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
223223
}
224224
} else if (type == PAYLOAD_TYPE_RESPONSE && len > 0) {
225225
onContactResponse(from, data, len);
226+
if (packet->isRouteFlood() && from.out_path_len >= 0) {
227+
// we have direct path, but other node is still sending flood response, so maybe they didn't receive reciprocal path properly(?)
228+
handleReturnPathRetry(from, packet->path, packet->path_len);
229+
}
226230
}
227231
}
228232

@@ -248,7 +252,7 @@ bool BaseChatMesh::onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_
248252

249253
if (extra_type == PAYLOAD_TYPE_ACK && extra_len >= 4) {
250254
// also got an encoded ACK!
251-
if (processAck(extra)) {
255+
if (processAck(extra) != NULL) {
252256
txt_send_timeout = 0; // matched one we're waiting for, cancel timeout timer
253257
}
254258
} else if (extra_type == PAYLOAD_TYPE_RESPONSE && extra_len > 0) {
@@ -258,12 +262,25 @@ bool BaseChatMesh::onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_
258262
}
259263

260264
void BaseChatMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) {
261-
if (processAck((uint8_t *)&ack_crc)) {
265+
ContactInfo* from;
266+
if ((from = processAck((uint8_t *)&ack_crc)) != NULL) {
262267
txt_send_timeout = 0; // matched one we're waiting for, cancel timeout timer
263268
packet->markDoNotRetransmit(); // ACK was for this node, so don't retransmit
269+
270+
if (packet->isRouteFlood() && from->out_path_len >= 0) {
271+
// we have direct path, but other node is still sending flood, so maybe they didn't receive reciprocal path properly(?)
272+
handleReturnPathRetry(*from, packet->path, packet->path_len);
273+
}
264274
}
265275
}
266276

277+
void BaseChatMesh::handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len) {
278+
// NOTE: simplest impl is just to re-send a reciprocal return path to sender (DIRECTLY)
279+
// override this method in various firmwares, if there's a better strategy
280+
mesh::Packet* rpath = createPathReturn(contact.id, contact.shared_secret, path, path_len, 0, NULL, 0);
281+
if (rpath) sendDirect(rpath, contact.out_path, contact.out_path_len, 3000); // 3 second delay
282+
}
283+
267284
#ifdef MAX_GROUP_CHANNELS
268285
int BaseChatMesh::searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches) {
269286
int n = 0;
@@ -550,7 +567,7 @@ void BaseChatMesh::markConnectionActive(const ContactInfo& contact) {
550567
}
551568
}
552569

553-
bool BaseChatMesh::checkConnectionsAck(const uint8_t* data) {
570+
ContactInfo* BaseChatMesh::checkConnectionsAck(const uint8_t* data) {
554571
for (int i = 0; i < MAX_CONNECTIONS; i++) {
555572
if (connections[i].keep_alive_millis > 0 && memcmp(&connections[i].expected_ack, data, 4) == 0) {
556573
// yes, got an ack for our keep_alive request!
@@ -559,10 +576,12 @@ bool BaseChatMesh::checkConnectionsAck(const uint8_t* data) {
559576

560577
// re-schedule next KEEP_ALIVE, now that we have heard from server
561578
connections[i].next_ping = futureMillis(connections[i].keep_alive_millis);
562-
return true; // yes, a match
579+
580+
auto id = &connections[i].server_id;
581+
return lookupContactByPubKey(id->pub_key, PUB_KEY_SIZE); // yes, a match
563582
}
564583
}
565-
return false; /// no match
584+
return NULL; /// no match
566585
}
567586

568587
void BaseChatMesh::checkConnections() {

src/helpers/BaseChatMesh.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class BaseChatMesh : public mesh::Mesh {
9393
// 'UI' concepts, for sub-classes to implement
9494
virtual bool isAutoAddEnabled() const { return true; }
9595
virtual void onDiscoveredContact(ContactInfo& contact, bool is_new, uint8_t path_len, const uint8_t* path) = 0;
96-
virtual bool processAck(const uint8_t *data) = 0;
96+
virtual ContactInfo* processAck(const uint8_t *data) = 0;
9797
virtual void onContactPathUpdated(const ContactInfo& contact) = 0;
9898
virtual bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len);
9999
virtual void onMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
@@ -105,6 +105,7 @@ class BaseChatMesh : public mesh::Mesh {
105105
virtual void onChannelMessageRecv(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t timestamp, const char *text) = 0;
106106
virtual uint8_t onContactRequest(const ContactInfo& contact, uint32_t sender_timestamp, const uint8_t* data, uint8_t len, uint8_t* reply) = 0;
107107
virtual void onContactResponse(const ContactInfo& contact, const uint8_t* data, uint8_t len) = 0;
108+
virtual void handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len);
108109

109110
// storage concepts, for sub-classes to override/implement
110111
virtual int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) { return 0; } // not implemented
@@ -127,7 +128,7 @@ class BaseChatMesh : public mesh::Mesh {
127128
void stopConnection(const uint8_t* pub_key);
128129
bool hasConnectionTo(const uint8_t* pub_key);
129130
void markConnectionActive(const ContactInfo& contact);
130-
bool checkConnectionsAck(const uint8_t* data);
131+
ContactInfo* checkConnectionsAck(const uint8_t* data);
131132
void checkConnections();
132133

133134
public:

0 commit comments

Comments
 (0)