From 906c675b4bd80258e1bd4bafd17c3e35f7b94820 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 22 Aug 2026 18:13:02 +0200 Subject: [PATCH] =?UTF-8?q?fix(mesh):=20=C5=A1=C3=AD=C5=99ku=20path=20hash?= =?UTF-8?q?e=20bere=20ka=C5=BEd=C3=A1=20TX=20cesta=20z=20prefs=20uzlu,=20n?= =?UTF-8?q?e=20z=20konstanty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 (1M context) --- examples/companion_radio/MyMesh.h | 5 +++++ examples/simple_repeater/MyMesh.h | 5 +++++ examples/simple_room_server/MyMesh.h | 5 +++++ examples/simple_sensor/SensorMesh.h | 5 +++++ src/Mesh.cpp | 16 +++++++++++++++- src/Mesh.h | 26 +++++++++++++++++++++----- 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index c1fcd2b3..4bcbfaf5 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -113,6 +113,11 @@ protected: bool filterRecvFloodPacket(mesh::Packet* packet) override; bool allowPacketForward(const mesh::Packet* packet) override; + // Šířku path hashe volí ODESÍLATEL, takže musí přijít z prefs tohohle uzlu. + // Díky tomuhle overridu ji dostanou i volání send*(), která ji nepředávají + // explicitně (ACKy, PATH-return, zerohop odpovědi sousedovi). + uint8_t getSelfPathHashSize() const override { return _prefs.path_hash_mode + 1; } + void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis); void sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0) override; void sendFloodScoped(const mesh::GroupChannel& channel, mesh::Packet* pkt, uint32_t delay_millis=0) override; diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 901805bc..c3f2b205 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -305,6 +305,11 @@ protected: } bool allowPacketForward(const mesh::Packet* packet) override; + + // Šířku path hashe volí ODESÍLATEL, takže musí přijít z prefs tohohle uzlu. + // Díky tomuhle overridu ji dostanou i volání send*(), která ji nepředávají + // explicitně (ACKy, PATH-return, zerohop odpovědi sousedovi). + uint8_t getSelfPathHashSize() const override { return _prefs.path_hash_mode + 1; } #ifdef BOT_CHANNEL_PSK // CUSTOM (TeTeHacko): both are no-op virtuals in mesh::Mesh, which is the only // reason a repeater is normally deaf to channel traffic. Overriding them does diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 5cf949c6..80034139 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -159,6 +159,11 @@ protected: mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override; bool allowPacketForward(const mesh::Packet* packet) override; + + // Šířku path hashe volí ODESÍLATEL, takže musí přijít z prefs tohohle uzlu. + // Díky tomuhle overridu ji dostanou i volání send*(), která ji nepředávají + // explicitně (ACKy, PATH-return, zerohop odpovědi sousedovi). + uint8_t getSelfPathHashSize() const override { return _prefs.path_hash_mode + 1; } void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; int searchPeersByHash(const uint8_t* hash) override ; void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override; diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index b5e96d5c..f33a1a80 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -116,6 +116,11 @@ protected: // Mesh overrides float getAirtimeBudgetFactor() const override; bool allowPacketForward(const mesh::Packet* packet) override; + + // Šířku path hashe volí ODESÍLATEL, takže musí přijít z prefs tohohle uzlu. + // Díky tomuhle overridu ji dostanou i volání send*(), která ji nepředávají + // explicitně (ACKy, PATH-return, zerohop odpovědi sousedovi). + uint8_t getSelfPathHashSize() const override { return _prefs.path_hash_mode + 1; } int calcRxDelay(float score, uint32_t air_time) const override; uint32_t getRetransmitDelay(const mesh::Packet* packet) override; uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override; diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 1d646b3a..acde5b82 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -650,6 +650,7 @@ void Mesh::sendFlood(Packet* packet, uint32_t delay_millis, uint8_t path_hash_si MESH_DEBUG_PRINTLN("%s Mesh::sendFlood(): TRACE type not suspported", getLogDateTime()); return; } + if (path_hash_size == 0) path_hash_size = getSelfPathHashSize(); if (path_hash_size == 0 || path_hash_size > 3) { MESH_DEBUG_PRINTLN("%s Mesh::sendFlood(): invalid path_hash_size", getLogDateTime()); return; @@ -677,6 +678,7 @@ void Mesh::sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_m MESH_DEBUG_PRINTLN("%s Mesh::sendFlood(): TRACE type not suspported", getLogDateTime()); return; } + if (path_hash_size == 0) path_hash_size = getSelfPathHashSize(); if (path_hash_size == 0 || path_hash_size > 3) { MESH_DEBUG_PRINTLN("%s Mesh::sendFlood(): invalid path_hash_size", getLogDateTime()); return; @@ -701,10 +703,12 @@ void Mesh::sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_m sendPacket(packet, pri, delay_millis); } -void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uint32_t delay_millis) { +void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uint32_t delay_millis, uint8_t path_hash_size) { packet->header &= ~PH_ROUTE_MASK; packet->header |= ROUTE_TYPE_DIRECT; + if (path_hash_size == 0) path_hash_size = getSelfPathHashSize(); + uint8_t pri; if (packet->getPayloadType() == PAYLOAD_TYPE_TRACE) { // TRACE packets are different // for TRACE packets, path is appended to end of PAYLOAD. (path is used for SNR's) @@ -715,6 +719,12 @@ void Mesh::sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uin pri = 5; // maybe make this configurable } else { packet->path_len = Packet::copyPath(packet->path, path, path_len); + // An empty path has no hash-size bits of its own to preserve, and a bare 0 decodes as + // 1 byte. Zero-hop DIRECT is the common case for a neighbour reply, so without this a + // 2-byte node declares 1 byte on every such reply. + if (packet->getPathHashCount() == 0) { + packet->setPathHashSizeAndCount(path_hash_size, 0); + } if (packet->getPayloadType() == PAYLOAD_TYPE_PATH) { pri = 1; // slightly less priority } else { @@ -729,6 +739,8 @@ void Mesh::sendZeroHop(Packet* packet, uint32_t delay_millis, uint8_t path_hash_ packet->header &= ~PH_ROUTE_MASK; packet->header |= ROUTE_TYPE_DIRECT; + if (path_hash_size == 0) path_hash_size = getSelfPathHashSize(); + // Zero Hop = nulový počet hopů. POZOR, dřív tu bylo `packet->path_len = 0`, // což vynulovalo CELÝ bajt — a v jeho horních dvou bitech bydlí šířka hashe // (Packet.h:85). Každý zerohop paket proto hlásil 1 B bez ohledu na @@ -743,6 +755,8 @@ void Mesh::sendZeroHop(Packet* packet, uint32_t delay_millis, uint8_t path_hash_ void Mesh::sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis, uint8_t path_hash_size) { packet->header &= ~PH_ROUTE_MASK; packet->header |= ROUTE_TYPE_TRANSPORT_DIRECT; + + if (path_hash_size == 0) path_hash_size = getSelfPathHashSize(); packet->transport_codes[0] = transport_codes[0]; packet->transport_codes[1] = transport_codes[1]; diff --git a/src/Mesh.h b/src/Mesh.h index 2b2e6635..5e484c5c 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -71,6 +71,17 @@ protected: */ virtual uint8_t getExtraAckTransmitCount() const; + /** + * \brief Width (in bytes) of the path hashes this node writes into packets it originates. + * + * Sender-chosen, so it must come from this node's own prefs. The base class has no prefs + * and returns the protocol minimum; firmwares with a NodePrefs override it. Used whenever + * a send*() caller does not pass an explicit width. + * + * \returns 1, 2 or 3 + */ + virtual uint8_t getSelfPathHashSize() const { return 1; } + /** * \brief Perform search of local DB of peers/contacts. * \returns Number of peers with matching hash @@ -199,18 +210,23 @@ public: /** * \brief send a locally-generated Packet with flood routing */ - void sendFlood(Packet* packet, uint32_t delay_millis=0, uint8_t path_hash_size=1); + void sendFlood(Packet* packet, uint32_t delay_millis=0, uint8_t path_hash_size=0); /** * \brief send a locally-generated Packet with flood routing * \param transport_codes array of 2 codes to attach to packet */ - void sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0, uint8_t path_hash_size=1); + void sendFlood(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0, uint8_t path_hash_size=0); /** * \brief send a locally-generated Packet with Direct routing + * + * `path_len` is the ENCODED path byte (hash-size bits + hop count), so a non-empty path + * carries its own width across unchanged. An EMPTY path has no width to carry, and a bare + * 0 decodes as 1 byte -- which is why zero-hop replies used to declare 1 B on a 2 B node. + * `path_hash_size` (0 = this node's prefs) fills that gap. */ - void sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uint32_t delay_millis=0); + void sendDirect(Packet* packet, const uint8_t* path, uint8_t path_len, uint32_t delay_millis=0, uint8_t path_hash_size=0); /** * \brief send a locally-generated Packet to just neighbor nodes (zero hops) @@ -218,13 +234,13 @@ public: // `path_hash_size` tu je ze stejného důvodu jako u sendFlood: zerohop paket // sice cestu nenese, ale ta šířka je v hlavičce a analyzery z ní odvozují, // na kolika bajtech uzel jede. Bez toho parametru hlásí každý zerohop 1 B. - void sendZeroHop(Packet* packet, uint32_t delay_millis=0, uint8_t path_hash_size=1); + void sendZeroHop(Packet* packet, uint32_t delay_millis=0, uint8_t path_hash_size=0); /** * \brief send a locally-generated Packet to just neighbor nodes (zero hops), with specific transport codes * \param transport_codes array of 2 codes to attach to packet */ - void sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0, uint8_t path_hash_size=1); + void sendZeroHop(Packet* packet, uint16_t* transport_codes, uint32_t delay_millis=0, uint8_t path_hash_size=0); };