Skip to content

Commit c725b82

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 73ec376 commit c725b82

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
@@ -35,4 +35,7 @@ class CustomLR1110Wrapper : public RadioLibWrapper {
3535
uint8_t getCodingRate() const override {
3636
return ((CustomLR1110 *)_radio)->getCodingRate();
3737
}
38+
float getFreqMHz() const override {
39+
return ((CustomLR1110 *)_radio)->getFreqMHz();
40+
}
3841
};

src/helpers/radiolib/CustomSX1262Wrapper.h

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

src/helpers/radiolib/RadioLibWrappers.cpp

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

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

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

213-
#else
214215
// Non-JP: original behavior (RSSI threshold only)
215216
return getCurrentRSSI() > _noise_floor + _threshold;
216-
#endif
217217
}
218218

219-
220219
float RadioLibWrapper::getLastRSSI() const {
221220
return _radio->getRSSI();
222221
}

src/helpers/radiolib/RadioLibWrappers.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ class RadioLibWrapper : public mesh::Radio {
4040

4141
virtual float getCurrentRSSI() =0;
4242
virtual uint8_t getCodingRate() const = 0;
43+
virtual float getFreqMHz() const = 0;
44+
45+
bool isJapanMode() const {
46+
float freq = getFreqMHz();
47+
return (fabsf(freq - 920.800f) < 0.05f ||
48+
fabsf(freq - 921.000f) < 0.05f ||
49+
fabsf(freq - 921.200f) < 0.05f);
50+
}
4351

44-
#ifdef JP_STRICT
4552
int getMaxTextLen() const {
53+
if (!isJapanMode()) return 10 * 16; // default
4654
uint8_t cr = getCodingRate();
47-
if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars
48-
if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars
55+
if (cr <= 5) return 3 * 16; // 48 bytes ~16 JP chars
56+
if (cr == 6) return 2 * 16; // 32 bytes ~10 JP chars
4957
if (cr == 7) return 1 * 16 + 8; // 24 bytes ~8 JP chars
50-
return 1 * 16; // 16 bytes ~5 JP chars
58+
return 1 * 16; // 16 bytes ~5 JP chars
5159
}
52-
#endif
5360

5461
virtual int16_t performChannelScan();
5562

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)