Skip to content

Commit bbc5f0c

Browse files
authored
Merge pull request #1718 from realtag-github/repeater-v1.13-implement-discover
discover sends a single repeater discovery request and populates the neighbor list; self is excluded
2 parents 2e00298 + 0770618 commit bbc5f0c

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

examples/simple_repeater/MyMesh.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,47 @@ void MyMesh::onControlDataRecv(mesh::Packet* packet) {
742742
sendZeroHop(resp, getRetransmitDelay(resp)*4); // apply random delay (widened x4), as multiple nodes can respond to this
743743
}
744744
}
745+
} else if (type == CTL_TYPE_NODE_DISCOVER_RESP && packet->payload_len >= 6) {
746+
uint8_t node_type = packet->payload[0] & 0x0F;
747+
if (node_type != ADV_TYPE_REPEATER) {
748+
return;
749+
}
750+
if (packet->payload_len < 6 + PUB_KEY_SIZE) {
751+
MESH_DEBUG_PRINTLN("onControlDataRecv: DISCOVER_RESP pubkey too short: %d", (uint32_t)packet->payload_len);
752+
return;
753+
}
754+
755+
if (pending_discover_tag == 0 || millisHasNowPassed(pending_discover_until)) {
756+
pending_discover_tag = 0;
757+
return;
758+
}
759+
uint32_t tag;
760+
memcpy(&tag, &packet->payload[2], 4);
761+
if (tag != pending_discover_tag) {
762+
return;
763+
}
764+
765+
mesh::Identity id(&packet->payload[6]);
766+
if (id.matches(self_id)) {
767+
return;
768+
}
769+
putNeighbour(id, rtc_clock.getCurrentTime(), packet->getSNR());
770+
}
771+
}
772+
773+
void MyMesh::sendNodeDiscoverReq() {
774+
uint8_t data[10];
775+
data[0] = CTL_TYPE_NODE_DISCOVER_REQ; // prefix_only=0
776+
data[1] = (1 << ADV_TYPE_REPEATER);
777+
getRNG()->random(&data[2], 4); // tag
778+
memcpy(&pending_discover_tag, &data[2], 4);
779+
pending_discover_until = futureMillis(60000);
780+
uint32_t since = 0;
781+
memcpy(&data[6], &since, 4);
782+
783+
auto pkt = createControlData(data, sizeof(data));
784+
if (pkt) {
785+
sendZeroHop(pkt);
745786
}
746787
}
747788

@@ -805,6 +846,9 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
805846
_prefs.advert_loc_policy = ADVERT_LOC_PREFS;
806847

807848
_prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
849+
850+
pending_discover_tag = 0;
851+
pending_discover_until = 0;
808852
}
809853

810854
void MyMesh::begin(FILESYSTEM *fs) {
@@ -1172,6 +1216,15 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
11721216
} else {
11731217
strcpy(reply, "Err - ??");
11741218
}
1219+
} else if (memcmp(command, "discover.neighbors", 18) == 0) {
1220+
const char* sub = command + 18;
1221+
while (*sub == ' ') sub++;
1222+
if (*sub != 0) {
1223+
strcpy(reply, "Err - discover.neighbors has no options");
1224+
} else {
1225+
sendNodeDiscoverReq();
1226+
strcpy(reply, "OK - Discover sent");
1227+
}
11751228
} else{
11761229
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
11771230
}

examples/simple_repeater/MyMesh.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
9797
RegionEntry* load_stack[8];
9898
RegionEntry* recv_pkt_region;
9999
RateLimiter discover_limiter, anon_limiter;
100+
uint32_t pending_discover_tag;
101+
unsigned long pending_discover_until;
100102
bool region_load_active;
101103
unsigned long dirty_contacts_expiry;
102104
#if MAX_NEIGHBOURS
@@ -116,6 +118,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
116118
#endif
117119

118120
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
121+
void sendNodeDiscoverReq();
119122
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
120123
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
121124
uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);

0 commit comments

Comments
 (0)