From ab8cd85d8ea4146e663d7591bfc55d439025c1ec Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Fri, 11 Apr 2025 15:12:04 +1000 Subject: [PATCH] * added Packet::getRawLength() helper --- src/Dispatcher.cpp | 4 ++-- src/Mesh.cpp | 2 +- src/Packet.cpp | 4 ++++ src/Packet.h | 5 +++++ src/helpers/BaseChatMesh.cpp | 8 ++++---- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 953ede67..cd7090d1 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -138,7 +138,7 @@ void Dispatcher::checkRecv() { #if MESH_PACKET_LOGGING Serial.print(getLogDateTime()); Serial.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d", - 2 + pkt->path_len + pkt->payload_len, pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len, + pkt->getRawLength(), pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len, (int)pkt->getSNR(), (int)_radio->getLastRSSI(), (int)(score*1000)); static uint8_t packet_hash[MAX_HASH_SIZE]; @@ -153,7 +153,7 @@ void Dispatcher::checkRecv() { Serial.printf("\n"); } #endif - logRx(pkt, 2 + pkt->path_len + pkt->payload_len, score); // hook for custom logging + logRx(pkt, pkt->getRawLength(), score); // hook for custom logging if (pkt->isRouteFlood()) { n_recv_flood++; diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 0a0fd769..fe3b2473 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -15,7 +15,7 @@ bool Mesh::allowPacketForward(const mesh::Packet* packet) { return false; // by default, Transport NOT enabled } uint32_t Mesh::getRetransmitDelay(const mesh::Packet* packet) { - uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * 52 / 50) / 2; + uint32_t t = (_radio->getEstAirtimeFor(packet->getRawLength()) * 52 / 50) / 2; return _rng->nextInt(0, 5)*t; } diff --git a/src/Packet.cpp b/src/Packet.cpp index 48f7699e..386585fc 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -10,6 +10,10 @@ Packet::Packet() { payload_len = 0; } +int Packet::getRawLength() const { + return 2 + path_len + payload_len + (hasTransCodes() ? 4 : 0); +} + void Packet::calculatePacketHash(uint8_t* hash) const { SHA256 sha; uint8_t t = getPayloadType(); diff --git a/src/Packet.h b/src/Packet.h index 08e78899..dfd61da8 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -79,6 +79,11 @@ public: float getSNR() const { return ((float)_snr) / 4.0f; } + /** + * \returns the encoded/wire format length of this packet + */ + int getRawLength() const; + /** * \brief save entire packet as a blob * \param dest (OUT) destination buffer (assumed to be MAX_MTU_SIZE) diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 083a0629..989fcb2c 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -252,7 +252,7 @@ int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp, mesh::Packet* pkt = composeMsgPacket(recipient, timestamp, attempt, text, expected_ack); if (pkt == NULL) return MSG_SEND_FAILED; - uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); + uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); int rc; if (recipient.out_path_len < 0) { @@ -279,7 +279,7 @@ int BaseChatMesh::sendCommandData(const ContactInfo& recipient, uint32_t timest auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 5 + text_len); if (pkt == NULL) return MSG_SEND_FAILED; - uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); + uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); int rc; if (recipient.out_path_len < 0) { sendFlood(pkt); @@ -362,7 +362,7 @@ int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password, auto pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.shared_secret, temp, tlen); if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); + uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len < 0) { sendFlood(pkt); est_timeout = calcFloodTimeoutMillisFor(t); @@ -386,7 +386,7 @@ int BaseChatMesh::sendStatusRequest(const ContactInfo& recipient, uint32_t& est auto pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, sizeof(temp)); if (pkt) { - uint32_t t = _radio->getEstAirtimeFor(pkt->payload_len + pkt->path_len + 2); + uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength()); if (recipient.out_path_len < 0) { sendFlood(pkt); est_timeout = calcFloodTimeoutMillisFor(t);