Skip to content

Commit 7755400

Browse files
author
Scott Powell
committed
* Companion: Now using transport codes { 0, 0 } when Share contact zero hop.
* Repeater: onAdvertRecv(), adverts via Share now NOT added to neighbours table
1 parent ef75292 commit 7755400

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

examples/simple_repeater/MyMesh.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,19 @@ void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
448448
}
449449
}
450450

451+
static bool isShare(const mesh::Packet *packet) {
452+
if (packet->hasTransportCodes()) {
453+
return packet->transport_codes[0] == 0 && packet->transport_codes[1] == 0; // codes { 0, 0 } means 'send to nowhere'
454+
}
455+
return false;
456+
}
457+
451458
void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32_t timestamp,
452459
const uint8_t *app_data, size_t app_data_len) {
453460
mesh::Mesh::onAdvertRecv(packet, id, timestamp, app_data, app_data_len); // chain to super impl
454461

455-
// if this a zero hop advert, add it to neighbours
456-
if (packet->path_len == 0) {
462+
// if this a zero hop advert (and not via 'Share'), add it to neighbours
463+
if (packet->path_len == 0 && !isShare(packet)) {
457464
AdvertDataParser parser(app_data, app_data_len);
458465
if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) { // just keep neigbouring Repeaters
459466
putNeighbour(id, timestamp, packet->getSNR());

src/Mesh.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,4 +645,17 @@ void Mesh::sendZeroHop(Packet* packet, uint32_t delay_millis) {
645645
sendPacket(packet, 0, delay_millis);
646646
}
647647

648+
void Mesh::sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis) {
649+
packet->header &= ~PH_ROUTE_MASK;
650+
packet->header |= ROUTE_TYPE_TRANSPORT_DIRECT;
651+
packet->transport_codes[0] = transport_codes[0];
652+
packet->transport_codes[1] = transport_codes[1];
653+
654+
packet->path_len = 0; // path_len of zero means Zero Hop
655+
656+
_tables->hasSeen(packet); // mark this packet as already sent in case it is rebroadcast back to us
657+
658+
sendPacket(packet, 0, delay_millis);
659+
}
660+
648661
}

src/Mesh.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ class Mesh : public Dispatcher {
196196
*/
197197
void sendZeroHop(Packet* packet, uint32_t delay_millis=0);
198198

199+
/**
200+
* \brief send a locally-generated Packet to just neigbor nodes (zero hops), with specific transort codes
201+
* \param transport_codes array of 2 codes to attach to packet
202+
*/
203+
void sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0);
204+
199205
};
200206

201207
}

src/helpers/BaseChatMesh.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,16 @@ void BaseChatMesh::onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id,
6868
}
6969

7070
// save a copy of raw advert packet (to support "Share..." function)
71-
int plen = packet->writeTo(temp_buf);
71+
int plen;
72+
{
73+
uint8_t save = packet->header;
74+
packet->header &= ~PH_ROUTE_MASK;
75+
packet->header |= ROUTE_TYPE_FLOOD; // make sure transport codes are NOT saved
76+
plen = packet->writeTo(temp_buf);
77+
packet->header = save;
78+
}
7279
putBlobByKey(id.pub_key, PUB_KEY_SIZE, temp_buf, plen);
73-
80+
7481
bool is_new = false;
7582
if (from == NULL) {
7683
if (!isAutoAddEnabled()) {
@@ -405,7 +412,9 @@ bool BaseChatMesh::shareContactZeroHop(const ContactInfo& contact) {
405412
if (packet == NULL) return false; // no Packets available
406413

407414
packet->readFrom(temp_buf, plen); // restore Packet from 'blob'
408-
sendZeroHop(packet);
415+
uint16_t codes[2];
416+
codes[0] = codes[1] = 0; // { 0, 0 } means 'send this nowhere'
417+
sendZeroHop(packet, codes);
409418
return true; // success
410419
}
411420

0 commit comments

Comments
 (0)