Skip to content

Commit 4867d36

Browse files
committed
Replace JP_STRICT build flag with runtime frequency detection
JP LBT mode now activates automatically based on operating frequency: - CH25: 920.800MHz - CH26: 921.000MHz - CH27: 921.200MHz (ARIB STD-T108, 200kHz grid) Changes: - Add isJapanMode() to RadioLibWrapper using getFreqMHz() - Add getFreqMHz() to CustomSX1262Wrapper and CustomLR1110Wrapper - Remove #ifdef JP_STRICT throughout, replaced by isJapanMode() - Remove -D JP_STRICT build flag from platformio.ini - MAX_TEXT_LEN dynamically determined by CR at runtime via getMaxTextLen() No build flags required: JP compliance activates automatically when device is configured to Japan 3 frequencies.
1 parent 3c0ed55 commit 4867d36

8 files changed

Lines changed: 48 additions & 49 deletions

File tree

src/Dispatcher.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ 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
41+
virtual int getMaxTextLen() const { return 10 * 16; } // default: non-JP
4442

4543
/**
4644
* \brief starts the raw packet send. (no wait)

src/helpers/BaseChatMesh.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,9 @@ 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
400398
int max_len = _radio->getMaxTextLen();
401399
if (text_len > max_len) return NULL;
402400
if (attempt > 3 && text_len > max_len - 2) return NULL;
403-
#else
404-
if (text_len > MAX_TEXT_LEN) return NULL;
405-
if (attempt > 3 && text_len > MAX_TEXT_LEN-2) return NULL;
406-
#endif
407-
408401
uint8_t temp[5+MAX_TEXT_LEN+1];
409402
memcpy(temp, &timestamp, 4); // mostly an extra blob to help make packet_hash unique
410403
temp[4] = (attempt & 3);

src/helpers/radiolib/CustomLR1110Wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ class CustomLR1110Wrapper : public RadioLibWrapper {
3737
uint8_t getCodingRate() const override {
3838
return ((CustomLR1110 *)_radio)->getCodingRate();
3939
}
40+
float getFreqMHz() const override {
41+
return ((CustomLR1110 *)_radio)->getFreqMHz();
42+
}
4043
};

src/helpers/radiolib/CustomSX1262Wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ class CustomSX1262Wrapper : public RadioLibWrapper {
4141
uint8_t getCodingRate() const override {
4242
return ((CustomSX1262 *)_radio)->codingRate + 4; // RadioLib stores 1-4, return 5-8
4343
}
44+
float getFreqMHz() const override {
45+
return ((CustomSX1262 *)_radio)->freqMHz;
46+
}
4447
};

src/helpers/radiolib/RadioLibWrappers.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ bool RadioLibWrapper::isSendComplete() {
171171
void RadioLibWrapper::onSendFinished() {
172172
_radio->finishTransmit();
173173
_board->onAfterTransmit();
174-
#ifdef JP_STRICT
175-
// ARIB STD-T108: wait >= 50ms after TX before next transmission
176-
delay(50);
177-
#endif
174+
if (isJapanMode()) {
175+
// ARIB STD-T108: wait >= 50ms after TX before next transmission
176+
delay(50);
177+
}
178178
state = STATE_IDLE;
179179
}
180180

@@ -185,40 +185,39 @@ int16_t RadioLibWrapper::performChannelScan() {
185185
bool RadioLibWrapper::isChannelActive() {
186186
if (_threshold == 0) return false; // interference check is disabled
187187

188-
#ifdef JP_STRICT
189-
// ARIB STD-T108 compliant LBT: continuous RSSI sensing for >= 5ms
190-
// Energy-based sensing required; LoRa CAD not used here —
191-
// CAD detects only LoRa preambles and is not required by ARIB STD-T108
192-
uint32_t sense_start = millis();
193-
while (millis() - sense_start < 5) {
194-
if (getCurrentRSSI() > -80.0f) {
195-
// Channel busy: exponential backoff (tuned for JP 4s airtime)
196-
_busy_count++;
197-
uint32_t base_ms = 2000;
198-
uint32_t max_backoff = min(base_ms * (1u << _busy_count), (uint32_t)16000);
199-
uint32_t backoff_until = millis() + random(max_backoff / 2, max_backoff);
200-
while (millis() < backoff_until) {
201-
vTaskDelay(1); // yield CPU to FreeRTOS tasks including BLE
188+
// Activate JP_STRICT LBT on Japan 920MHz band 3 channels only
189+
// CH25=920.800MHz, CH26=921.000MHz, CH27=921.200MHz (ARIB STD-T108)
190+
if (isJapanMode()) {
191+
// ARIB STD-T108 compliant LBT: continuous RSSI sensing for >= 5ms
192+
// Energy-based sensing required; LoRa CAD not used
193+
uint32_t sense_start = millis();
194+
while (millis() - sense_start < 5) {
195+
if (getCurrentRSSI() > -80.0f) {
196+
// Channel busy: exponential backoff (tuned for JP 4s airtime)
197+
_busy_count++;
198+
uint32_t base_ms = 2000;
199+
uint32_t max_backoff = min(base_ms * (1u << _busy_count), (uint32_t)16000);
200+
uint32_t backoff_until = millis() + random(max_backoff / 2, max_backoff);
201+
while (millis() < backoff_until) {
202+
vTaskDelay(1);
203+
}
204+
return true;
202205
}
203-
return true;
206+
vTaskDelay(1);
204207
}
205-
vTaskDelay(1); // yield CPU between RSSI samples
206-
}
207-
// Channel free: reset busy counter and add small jitter
208-
_busy_count = 0;
209-
uint32_t jitter_until = millis() + random(0, 500);
210-
while (millis() < jitter_until) {
211-
vTaskDelay(1); // yield CPU to FreeRTOS tasks including BLE
208+
// Channel free: reset busy counter and add small jitter
209+
_busy_count = 0;
210+
uint32_t jitter_until = millis() + random(0, 500);
211+
while (millis() < jitter_until) {
212+
vTaskDelay(1);
213+
}
214+
return false;
212215
}
213-
return false;
214216

215-
#else
216217
// Non-JP: original behavior (RSSI threshold only)
217218
return getCurrentRSSI() > _noise_floor + _threshold;
218-
#endif
219219
}
220220

221-
222221
float RadioLibWrapper::getLastRSSI() const {
223222
return _radio->getRSSI();
224223
}

src/helpers/radiolib/RadioLibWrappers.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ class RadioLibWrapper : public mesh::Radio {
4444
static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }
4545
void updatePreamble(uint8_t sf) { _preamble_sf = sf; _radio->setPreambleLength(preambleLengthForSF(sf)); }
4646
virtual uint8_t getCodingRate() const = 0;
47+
virtual float getFreqMHz() const = 0;
48+
49+
bool isJapanMode() const {
50+
float freq = getFreqMHz();
51+
return (fabsf(freq - 920.800f) < 0.05f ||
52+
fabsf(freq - 921.000f) < 0.05f ||
53+
fabsf(freq - 921.200f) < 0.05f);
54+
}
4755

48-
#ifdef JP_STRICT
4956
int getMaxTextLen() const {
57+
if (!isJapanMode()) return 10 * 16; // default
5058
uint8_t cr = getCodingRate();
51-
if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars
52-
if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars
59+
if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars
60+
if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars
5361
if (cr == 7) return 1 * 16 + 8; // 24 bytes ~8 JP chars
54-
return 1 * 16; // 16 bytes ~5 JP chars
62+
return 1 * 16; // 16 bytes ~5 JP chars
5563
}
56-
#endif
5764

5865
virtual int16_t performChannelScan();
5966

variants/rak_wismesh_tag/platformio.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ build_flags = ${nrf52_base.build_flags}
2525
-D PIN_BUZZER=21
2626
-D PIN_BOARD_SDA=PIN_WIRE_SDA
2727
-D PIN_BOARD_SCL=PIN_WIRE_SCL
28-
-D JP_STRICT
29-
-D LORA_CR=5
3028
build_src_filter = ${nrf52_base.build_src_filter}
3129
+<../variants/rak_wismesh_tag>
3230
+<helpers/ui/MomentaryButton.cpp>

variants/wio-tracker-l1/platformio.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ build_flags = ${nrf52_base.build_flags}
1616
-D SX126X_RX_BOOSTED_GAIN=1
1717
-D PIN_OLED_RESET=-1
1818
-D GPS_BAUD_RATE=9600
19-
-D JP_STRICT
20-
-D LORA_CR=5
2119
build_src_filter = ${nrf52_base.build_src_filter}
2220
+<WioTrackerL1Board.cpp>
2321
+<../variants/wio-tracker-l1>

0 commit comments

Comments
 (0)