Skip to content

Commit 107b94c

Browse files
author
realtag
committed
Allow repeater ping and status hash-size suffixes
1 parent f683868 commit 107b94c

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

examples/simple_repeater/MyMesh.cpp

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,32 @@ static const char* findMessageBodyAllowSelf(const char* text, char* sender, size
257257
return text;
258258
}
259259

260-
static bool containsSubstringCaseInsensitive(const char* haystack, const char* needle) {
261-
if (!haystack || !needle) return false;
262-
if (needle[0] == 0) return true;
263-
for (const char* h = haystack; *h; ++h) {
264-
const char* h_it = h;
265-
const char* n_it = needle;
266-
while (*h_it && *n_it &&
267-
tolower(static_cast<unsigned char>(*h_it)) ==
268-
tolower(static_cast<unsigned char>(*n_it))) {
269-
++h_it;
270-
++n_it;
271-
}
272-
if (*n_it == 0) return true;
260+
static uint8_t parseCommandPathHashSize(const char* text, const char* command, bool* found) {
261+
if (found) *found = false;
262+
if (!text || !command || command[0] == 0) return 1;
263+
264+
size_t command_len = strlen(command);
265+
bool matched = false;
266+
for (const char* cursor = text; *cursor; ++cursor) {
267+
size_t i = 0;
268+
while (i < command_len && cursor[i] &&
269+
tolower(static_cast<unsigned char>(cursor[i])) ==
270+
tolower(static_cast<unsigned char>(command[i]))) {
271+
++i;
272+
}
273+
if (i != command_len) continue;
274+
275+
matched = true;
276+
const char* suffix = cursor + command_len;
277+
if (suffix[0] == ':' && (suffix[1] == '1' || suffix[1] == '2' || suffix[1] == '3') &&
278+
!isdigit(static_cast<unsigned char>(suffix[2]))) {
279+
if (found) *found = true;
280+
return (uint8_t)(suffix[1] - '0');
281+
}
273282
}
274-
return false;
283+
284+
if (found) *found = matched;
285+
return 1;
275286
}
276287

277288
void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) {
@@ -1221,11 +1232,13 @@ void MyMesh::sendPingReply(const mesh::GroupChannel& channel) {
12211232

12221233
auto reply = createGroupDatagram(PAYLOAD_TYPE_GRP_TXT, channel, temp, 5 + msg_len);
12231234
if (reply) {
1224-
sendFlood(reply, SERVER_RESPONSE_DELAY + delay_ms);
1235+
uint8_t path_hash_size = pending_ping_path_hash_size;
1236+
if (path_hash_size < 1 || path_hash_size > 3) path_hash_size = 1;
1237+
sendFlood(reply, SERVER_RESPONSE_DELAY + delay_ms, path_hash_size);
12251238
}
12261239
}
12271240

1228-
void MyMesh::sendStatusReply(const mesh::GroupChannel& channel, const char* sender_name) {
1241+
void MyMesh::sendStatusReply(const mesh::GroupChannel& channel, const char* sender_name, uint8_t path_hash_size) {
12291242
if (!public_channel_ready && !test_channel_ready) return;
12301243

12311244
uint8_t busy_pct = calcBusyPercent();
@@ -1248,7 +1261,8 @@ void MyMesh::sendStatusReply(const mesh::GroupChannel& channel, const char* send
12481261

12491262
auto reply = createGroupDatagram(PAYLOAD_TYPE_GRP_TXT, channel, temp, 5 + msg_len);
12501263
if (reply) {
1251-
sendFlood(reply, SERVER_RESPONSE_DELAY + delay_ms);
1264+
if (path_hash_size < 1 || path_hash_size > 3) path_hash_size = 1;
1265+
sendFlood(reply, SERVER_RESPONSE_DELAY + delay_ms, path_hash_size);
12521266
}
12531267
}
12541268

@@ -1461,8 +1475,10 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
14611475

14621476
if (!body) return;
14631477
if (!isPingChannel(channel)) return;
1464-
bool is_ping = containsSubstringCaseInsensitive(body, "!ping");
1465-
bool is_status = containsSubstringCaseInsensitive(body, "!status");
1478+
bool is_ping = false;
1479+
bool is_status = false;
1480+
uint8_t ping_path_hash_size = parseCommandPathHashSize(body, "!ping", &is_ping);
1481+
uint8_t status_path_hash_size = parseCommandPathHashSize(body, "!status", &is_status);
14661482
if (!is_ping && !is_status) return;
14671483

14681484
bool is_public_channel = public_channel_ready && memcmp(channel.hash, public_channel.hash, PATH_HASH_SIZE) == 0;
@@ -1477,7 +1493,7 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
14771493
StrHelper::strncpy(sender_name, "Unknown", sizeof(sender_name));
14781494
}
14791495
if (is_status) {
1480-
sendStatusReply(channel, sender_name);
1496+
sendStatusReply(channel, sender_name, status_path_hash_size);
14811497
return;
14821498
}
14831499

@@ -1488,6 +1504,7 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
14881504
pending_ping_channel_ready = true;
14891505
pending_ping_total = (uint8_t)min(3, (int)max_replies);
14901506
pending_ping_retries = pending_ping_total;
1507+
pending_ping_path_hash_size = ping_path_hash_size;
14911508
pending_ping_include_prefix = true;
14921509
next_ping_send_at = 0;
14931510

@@ -1689,6 +1706,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
16891706
pending_ping_path[0] = 0;
16901707
pending_ping_retries = 0;
16911708
pending_ping_total = PING_REPLY_ATTEMPTS;
1709+
pending_ping_path_hash_size = 1;
16921710
pending_ping_include_prefix = true;
16931711
pending_ping_is_5count = false;
16941712
next_ping_send_at = 0;

examples/simple_repeater/MyMesh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
9898
uint8_t reply_path_hash_size;
9999
uint8_t pending_ping_retries;
100100
uint8_t pending_ping_total;
101+
uint8_t pending_ping_path_hash_size;
101102
bool pending_ping_include_prefix;
102103
bool pending_ping_is_5count;
103104
unsigned long next_ping_send_at;
@@ -182,7 +183,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
182183
void initPublicChannel();
183184
void initTestChannel();
184185
void sendPingReply(const mesh::GroupChannel& channel);
185-
void sendStatusReply(const mesh::GroupChannel& channel, const char* sender_name);
186+
void sendStatusReply(const mesh::GroupChannel& channel, const char* sender_name, uint8_t path_hash_size);
186187
void sendPublicBroadcast(const mesh::GroupChannel& channel);
187188
uint8_t calcBusyPercent();
188189
uint32_t calcBusyDelayMs(uint8_t busy_pct);

0 commit comments

Comments
 (0)