Skip to content

Commit c5bacd7

Browse files
committed
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.
1 parent eb58523 commit c5bacd7

5 files changed

Lines changed: 30 additions & 21 deletions

File tree

src/Dispatcher.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Radio {
3838

3939
virtual float packetScore(float snr, int packet_len) = 0;
4040

41+
#ifdef JP_STRICT
42+
virtual int getMaxTextLen() const { return 1 * 16; } // default: CR4/8
43+
#endif
44+
4145
/**
4246
* \brief starts the raw packet send. (no wait)
4347
* \param bytes the raw packet data

src/helpers/BaseChatMesh.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,15 @@ void BaseChatMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mes
395395

396396
mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack) {
397397
int text_len = strlen(text);
398+
399+
#ifdef JP_STRICT
400+
int max_len = _radio->getMaxTextLen();
401+
if (text_len > max_len) return NULL;
402+
if (attempt > 3 && text_len > max_len - 2) return NULL;
403+
#else
398404
if (text_len > MAX_TEXT_LEN) return NULL;
399405
if (attempt > 3 && text_len > MAX_TEXT_LEN-2) return NULL;
406+
#endif
400407

401408
uint8_t temp[5+MAX_TEXT_LEN+1];
402409
memcpy(temp, &timestamp, 4); // mostly an extra blob to help make packet_hash unique

src/helpers/BaseChatMesh.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,8 @@
55
#include <helpers/AdvertDataHelpers.h>
66
#include <helpers/TxtDataHelpers.h>
77

8-
// JP_STRICT: limit MAX_TEXT_LEN to keep TX time under 4s (ARIB STD-T108)
9-
// SF12/BW125, packet overhead ~44 bytes
10-
// CR4/5: 100 bytes total = ~3808ms → 56 bytes text → ~18 JP chars
11-
// CR4/6: 80 bytes total = ~3530ms → 36 bytes text → ~12 JP chars
12-
// CR4/7: 70 bytes total = ~3530ms → 26 bytes text → ~8 JP chars
13-
// CR4/8: 60 bytes total = ~3809ms → 16 bytes text → ~5 JP chars
14-
#ifdef JP_STRICT
15-
#if defined(LORA_CR) && (LORA_CR == 5)
16-
#define MAX_TEXT_LEN (3*CIPHER_BLOCK_SIZE) // 48 bytes ~18 JP chars
17-
#elif defined(LORA_CR) && (LORA_CR == 6)
18-
#define MAX_TEXT_LEN (2*CIPHER_BLOCK_SIZE) // 32 bytes ~10 JP chars
19-
#elif defined(LORA_CR) && (LORA_CR == 7)
20-
#define MAX_TEXT_LEN (1*CIPHER_BLOCK_SIZE+8) // 24 bytes ~8 JP chars
21-
#else
22-
#define MAX_TEXT_LEN (1*CIPHER_BLOCK_SIZE) // 16 bytes ~5 JP chars
23-
#endif
24-
#else
25-
#define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE)
8+
#define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE)
269
// must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1)
27-
#endif
2810

2911
#include "ContactInfo.h"
3012

src/helpers/radiolib/CustomSX1262Wrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ class CustomSX1262Wrapper : public RadioLibWrapper {
3636
bool getRxBoostedGainMode() const override {
3737
return ((CustomSX1262 *)_radio)->getRxBoostedGainMode();
3838
}
39+
40+
uint8_t getCodingRate() const override {
41+
return ((CustomSX1262 *)_radio)->codingRate + 4; // RadioLib stores 1-4, return 5-8
42+
}
3943
};

src/helpers/radiolib/RadioLibWrappers.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class RadioLibWrapper : public mesh::Radio {
77
protected:
88
PhysicalLayer* _radio;
99
mesh::MainBoard* _board;
10-
uint32_t n_recv, n_sent, n_recv_errors;
10+
uint32_t n_recv, n_sent, n_recv_errors, _tx_start_ms;
1111
int16_t _noise_floor, _threshold;
1212
uint16_t _num_floor_samples;
1313
int32_t _floor_sample_sum;
@@ -38,8 +38,20 @@ class RadioLibWrapper : public mesh::Radio {
3838
}
3939

4040
virtual float getCurrentRSSI() =0;
41-
virtual int16_t performChannelScan();
41+
virtual uint8_t getCodingRate() const = 0;
42+
43+
#ifdef JP_STRICT
44+
int getMaxTextLen() const {
45+
uint8_t cr = getCodingRate();
46+
if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars
47+
if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars
48+
if (cr == 7) return 1 * 16 + 8; // 24 bytes ~8 JP chars
49+
return 1 * 16; // 16 bytes ~5 JP chars
50+
}
51+
#endif
4252

53+
virtual int16_t performChannelScan();
54+
4355
int getNoiseFloor() const override { return _noise_floor; }
4456
void triggerNoiseFloorCalibrate(int threshold) override;
4557
void resetAGC() override;

0 commit comments

Comments
 (0)