From c4cc3dd1ec9c27fc808158cd60505a176c686da4 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Mon, 24 Feb 2025 20:52:13 +1100 Subject: [PATCH] * repeater and room server: login password now using strcmp(), new 'set direct.txdelay ..' config --- examples/simple_repeater/main.cpp | 20 +++++++++++++++++--- examples/simple_room_server/main.cpp | 20 +++++++++++++++++--- src/Mesh.cpp | 7 ++++++- src/Mesh.h | 5 +++++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index e3ec8d34..d7074228 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -138,6 +138,7 @@ struct NodePrefs { // persisted to file float rx_delay_base; float tx_delay_factor; char guest_password[16]; + float direct_tx_delay_factor; }; class MyMesh : public mesh::Mesh { @@ -309,6 +310,10 @@ protected: uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor); return getRNG()->nextInt(0, 6)*t; } + uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override { + uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 6)*t; + } void onAnonDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Identity& sender, uint8_t* data, size_t len) override { if (type == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage) @@ -316,13 +321,13 @@ protected: memcpy(×tamp, data, 4); bool is_admin; - if (memcmp(&data[4], _prefs.password, strlen(_prefs.password)) == 0) { // check for valid password + data[len] = 0; // ensure null terminator + if (strcmp((char *) &data[4], _prefs.password) == 0) { // check for valid password is_admin = true; - } else if (memcmp(&data[4], _prefs.guest_password, strlen(_prefs.guest_password)) == 0) { // check guest password + } else if (strcmp((char *) &data[4], _prefs.guest_password) == 0) { // check guest password is_admin = false; } else { #if MESH_DEBUG - data[len] = 0; // ensure null terminator MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]); #endif return; @@ -675,6 +680,15 @@ public: } else { strcpy(reply, "Error, cannot be negative"); } + } else if (memcmp(config, "direct.txdelay ", 15) == 0) { + float f = atof(&config[15]); + if (f >= 0) { + _prefs.direct_tx_delay_factor = f; + savePrefs(); + strcpy(reply, "OK"); + } else { + strcpy(reply, "Error, cannot be negative"); + } } else if (memcmp(config, "tx ", 3) == 0) { _prefs.tx_power_dbm = atoi(&config[3]); savePrefs(); diff --git a/examples/simple_room_server/main.cpp b/examples/simple_room_server/main.cpp index 0010024a..10853fd8 100644 --- a/examples/simple_room_server/main.cpp +++ b/examples/simple_room_server/main.cpp @@ -145,6 +145,7 @@ struct NodePrefs { // persisted to file float rx_delay_base; float tx_delay_factor; char guest_password[16]; + float direct_tx_delay_factor; }; class MyMesh : public mesh::Mesh { @@ -295,6 +296,10 @@ protected: uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor); return getRNG()->nextInt(0, 6)*t; } + uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override { + uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); + return getRNG()->nextInt(0, 6)*t; + } bool allowPacketForward(const mesh::Packet* packet) override { return !_prefs.disable_fwd; @@ -307,12 +312,12 @@ protected: memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp bool is_admin; - if (memcmp(&data[8], _prefs.password, strlen(_prefs.password)) == 0) { // check for valid admin password + data[len] = 0; // ensure null terminator + if (strcmp((char *) &data[8], _prefs.password) == 0) { // check for valid admin password is_admin = true; } else { is_admin = false; - int len = strlen(_prefs.guest_password); - if (len > 0 && memcmp(&data[8], _prefs.guest_password, len) != 0) { // check the room/public password + if (strcmp((char *) &data[8], _prefs.guest_password) != 0) { // check the room/public password MESH_DEBUG_PRINTLN("Incorrect room password"); return; // no response. Client will timeout } @@ -694,6 +699,15 @@ public: } else { strcpy(reply, "Error, cannot be negative"); } + } else if (memcmp(config, "direct.txdelay ", 15) == 0) { + float f = atof(&config[15]); + if (f >= 0) { + _prefs.direct_tx_delay_factor = f; + savePrefs(); + strcpy(reply, "OK"); + } else { + strcpy(reply, "Error, cannot be negative"); + } } else if (memcmp(config, "tx ", 3) == 0) { _prefs.tx_power_dbm = atoi(&config[3]); savePrefs(); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index be0ba118..8c0759e8 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -19,6 +19,9 @@ uint32_t Mesh::getRetransmitDelay(const mesh::Packet* packet) { return _rng->nextInt(0, 5)*t; } +uint32_t Mesh::getDirectRetransmitDelay(const Packet* packet) { + return 0; // by default, no delay +} int Mesh::searchPeersByHash(const uint8_t* hash) { return 0; // not found @@ -41,7 +44,9 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { // remove our hash from 'path', then re-broadcast pkt->path_len -= PATH_HASH_SIZE; memcpy(pkt->path, &pkt->path[PATH_HASH_SIZE], pkt->path_len); - return ACTION_RETRANSMIT(0); // Routed traffic is HIGHEST priority (and NO per-hop delay) + + uint32_t d = getDirectRetransmitDelay(pkt); + return ACTION_RETRANSMIT_DELAYED(0, d); // Routed traffic is HIGHEST priority } return ACTION_RELEASE; // this node is NOT the next hop (OR this packet has already been forwarded), so discard. } diff --git a/src/Mesh.h b/src/Mesh.h index 4552c85b..41f10863 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -74,6 +74,11 @@ protected: */ virtual uint32_t getRetransmitDelay(const Packet* packet); + /** + * \returns number of milliseconds delay to apply to retransmitting the given packet, for DIRECT mode. + */ + virtual uint32_t getDirectRetransmitDelay(const Packet* packet); + /** * \brief Perform search of local DB of peers/contacts. * \returns Number of peers with matching hash