From c5bacd7de9a3df37e9043e05668720214c716a13 Mon Sep 17 00:00:00 2001 From: jirogit Date: Thu, 26 Mar 2026 00:29:36 -0700 Subject: [PATCH] JP_STRICT: dynamic MAX_TEXT_LEN based on runtime CR value Instead of compile-time LORA_CR define, MAX_TEXT_LEN is now determined at runtime by reading the actual coding rate from the radio hardware. Added getMaxTextLen() to RadioLibWrapper and Dispatcher: - CR4/5: 48 bytes (~16 JP chars, TX ~3808ms) - CR4/6: 32 bytes (~10 JP chars) - CR4/7: 24 bytes (~8 JP chars) - CR4/8: 16 bytes (~5 JP chars, default) getCodingRate() added to CustomSX1262Wrapper to read codingRate from RadioLib PhysicalLayer at runtime. Tested: 48-byte limit with LORA_CR=5, 16-byte limit with LORA_CR=8. --- src/Dispatcher.h | 4 ++++ src/helpers/BaseChatMesh.cpp | 7 +++++++ src/helpers/BaseChatMesh.h | 20 +------------------- src/helpers/radiolib/CustomSX1262Wrapper.h | 4 ++++ src/helpers/radiolib/RadioLibWrappers.h | 16 ++++++++++++++-- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/Dispatcher.h b/src/Dispatcher.h index 2a99d068..7d4e2abf 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -38,6 +38,10 @@ public: virtual float packetScore(float snr, int packet_len) = 0; +#ifdef JP_STRICT + virtual int getMaxTextLen() const { return 1 * 16; } // default: CR4/8 +#endif + /** * \brief starts the raw packet send. (no wait) * \param bytes the raw packet data diff --git a/src/helpers/BaseChatMesh.cpp b/src/helpers/BaseChatMesh.cpp index 7ddc461d..d414c7b7 100644 --- a/src/helpers/BaseChatMesh.cpp +++ b/src/helpers/BaseChatMesh.cpp @@ -395,8 +395,15 @@ void BaseChatMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mes mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack) { int text_len = strlen(text); + +#ifdef JP_STRICT + int max_len = _radio->getMaxTextLen(); + if (text_len > max_len) return NULL; + if (attempt > 3 && text_len > max_len - 2) return NULL; +#else if (text_len > MAX_TEXT_LEN) return NULL; if (attempt > 3 && text_len > MAX_TEXT_LEN-2) return NULL; +#endif uint8_t temp[5+MAX_TEXT_LEN+1]; memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique diff --git a/src/helpers/BaseChatMesh.h b/src/helpers/BaseChatMesh.h index 65a5e558..a778323f 100644 --- a/src/helpers/BaseChatMesh.h +++ b/src/helpers/BaseChatMesh.h @@ -5,26 +5,8 @@ #include #include -// JP_STRICT: limit MAX_TEXT_LEN to keep TX time under 4s (ARIB STD-T108) -// SF12/BW125, packet overhead ~44 bytes -// CR4/5: 100 bytes total = ~3808ms → 56 bytes text → ~18 JP chars -// CR4/6: 80 bytes total = ~3530ms → 36 bytes text → ~12 JP chars -// CR4/7: 70 bytes total = ~3530ms → 26 bytes text → ~8 JP chars -// CR4/8: 60 bytes total = ~3809ms → 16 bytes text → ~5 JP chars -#ifdef JP_STRICT - #if defined(LORA_CR) && (LORA_CR == 5) - #define MAX_TEXT_LEN (3*CIPHER_BLOCK_SIZE) // 48 bytes ~18 JP chars - #elif defined(LORA_CR) && (LORA_CR == 6) - #define MAX_TEXT_LEN (2*CIPHER_BLOCK_SIZE) // 32 bytes ~10 JP chars - #elif defined(LORA_CR) && (LORA_CR == 7) - #define MAX_TEXT_LEN (1*CIPHER_BLOCK_SIZE+8) // 24 bytes ~8 JP chars - #else - #define MAX_TEXT_LEN (1*CIPHER_BLOCK_SIZE) // 16 bytes ~5 JP chars - #endif -#else - #define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) +#define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) // must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1) -#endif #include "ContactInfo.h" diff --git a/src/helpers/radiolib/CustomSX1262Wrapper.h b/src/helpers/radiolib/CustomSX1262Wrapper.h index 6499deb2..5edaa25e 100644 --- a/src/helpers/radiolib/CustomSX1262Wrapper.h +++ b/src/helpers/radiolib/CustomSX1262Wrapper.h @@ -36,4 +36,8 @@ public: bool getRxBoostedGainMode() const override { return ((CustomSX1262 *)_radio)->getRxBoostedGainMode(); } + + uint8_t getCodingRate() const override { + return ((CustomSX1262 *)_radio)->codingRate + 4; // RadioLib stores 1-4, return 5-8 + } }; diff --git a/src/helpers/radiolib/RadioLibWrappers.h b/src/helpers/radiolib/RadioLibWrappers.h index 7052ce02..51d928ca 100644 --- a/src/helpers/radiolib/RadioLibWrappers.h +++ b/src/helpers/radiolib/RadioLibWrappers.h @@ -7,7 +7,7 @@ class RadioLibWrapper : public mesh::Radio { protected: PhysicalLayer* _radio; mesh::MainBoard* _board; - uint32_t n_recv, n_sent, n_recv_errors; + uint32_t n_recv, n_sent, n_recv_errors, _tx_start_ms; int16_t _noise_floor, _threshold; uint16_t _num_floor_samples; int32_t _floor_sample_sum; @@ -38,8 +38,20 @@ public: } virtual float getCurrentRSSI() =0; - virtual int16_t performChannelScan(); + virtual uint8_t getCodingRate() const = 0; +#ifdef JP_STRICT + int getMaxTextLen() const { + uint8_t cr = getCodingRate(); + if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars + if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars + if (cr == 7) return 1 * 16 + 8; // 24 bytes ~8 JP chars + return 1 * 16; // 16 bytes ~5 JP chars + } +#endif + + virtual int16_t performChannelScan(); + int getNoiseFloor() const override { return _noise_floor; } void triggerNoiseFloorCalibrate(int threshold) override; void resetAGC() override;