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.
This commit is contained in:
jirogit 2026-03-27 23:42:37 -07:00
parent 73ec37657c
commit c725b82e77
8 changed files with 51 additions and 52 deletions

View file

@ -38,9 +38,7 @@ public:
virtual float packetScore(float snr, int packet_len) = 0;
#ifdef JP_STRICT
virtual int getMaxTextLen() const { return 1 * 16; } // default: CR4/8
#endif
virtual int getMaxTextLen() const { return 10 * 16; } // default: non-JP
/**
* \brief starts the raw packet send. (no wait)

View file

@ -395,16 +395,9 @@ 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, &timestamp, 4); // mostly an extra blob to help make packet_hash unique
temp[4] = (attempt & 3);

View file

@ -35,4 +35,7 @@ public:
uint8_t getCodingRate() const override {
return ((CustomLR1110 *)_radio)->getCodingRate();
}
float getFreqMHz() const override {
return ((CustomLR1110 *)_radio)->getFreqMHz();
}
};

View file

@ -40,4 +40,7 @@ public:
uint8_t getCodingRate() const override {
return ((CustomSX1262 *)_radio)->codingRate + 4; // RadioLib stores 1-4, return 5-8
}
float getFreqMHz() const override {
return ((CustomSX1262 *)_radio)->freqMHz;
}
};

View file

@ -169,10 +169,10 @@ bool RadioLibWrapper::isSendComplete() {
void RadioLibWrapper::onSendFinished() {
_radio->finishTransmit();
_board->onAfterTransmit();
#ifdef JP_STRICT
// ARIB STD-T108: wait >= 50ms after TX before next transmission
delay(50);
#endif
if (isJapanMode()) {
// ARIB STD-T108: wait >= 50ms after TX before next transmission
delay(50);
}
state = STATE_IDLE;
}
@ -183,40 +183,39 @@ int16_t RadioLibWrapper::performChannelScan() {
bool RadioLibWrapper::isChannelActive() {
if (_threshold == 0) return false; // interference check is disabled
#ifdef JP_STRICT
// ARIB STD-T108 compliant LBT: continuous RSSI sensing for >= 5ms
// Energy-based sensing required; LoRa CAD not used here —
// CAD detects only LoRa preambles and is not required by ARIB STD-T108
uint32_t sense_start = millis();
while (millis() - sense_start < 5) {
if (getCurrentRSSI() > -80.0f) {
// Channel busy: exponential backoff (tuned for JP 4s airtime)
_busy_count++;
uint32_t base_ms = 2000;
uint32_t max_backoff = min(base_ms * (1u << _busy_count), (uint32_t)16000);
uint32_t backoff_until = millis() + random(max_backoff / 2, max_backoff);
while (millis() < backoff_until) {
vTaskDelay(1); // yield CPU to FreeRTOS tasks including BLE
// Activate JP_STRICT LBT on Japan 920MHz band 3 channels only
// CH25=920.800MHz, CH26=921.000MHz, CH27=921.200MHz (ARIB STD-T108)
if (isJapanMode()) {
// ARIB STD-T108 compliant LBT: continuous RSSI sensing for >= 5ms
// Energy-based sensing required; LoRa CAD not used
uint32_t sense_start = millis();
while (millis() - sense_start < 5) {
if (getCurrentRSSI() > -80.0f) {
// Channel busy: exponential backoff (tuned for JP 4s airtime)
_busy_count++;
uint32_t base_ms = 2000;
uint32_t max_backoff = min(base_ms * (1u << _busy_count), (uint32_t)16000);
uint32_t backoff_until = millis() + random(max_backoff / 2, max_backoff);
while (millis() < backoff_until) {
vTaskDelay(1);
}
return true;
}
return true;
vTaskDelay(1);
}
vTaskDelay(1); // yield CPU between RSSI samples
// Channel free: reset busy counter and add small jitter
_busy_count = 0;
uint32_t jitter_until = millis() + random(0, 500);
while (millis() < jitter_until) {
vTaskDelay(1);
}
return false;
}
// Channel free: reset busy counter and add small jitter
_busy_count = 0;
uint32_t jitter_until = millis() + random(0, 500);
while (millis() < jitter_until) {
vTaskDelay(1); // yield CPU to FreeRTOS tasks including BLE
}
return false;
#else
// Non-JP: original behavior (RSSI threshold only)
return getCurrentRSSI() > _noise_floor + _threshold;
#endif
}
float RadioLibWrapper::getLastRSSI() const {
return _radio->getRSSI();
}

View file

@ -40,16 +40,23 @@ public:
virtual float getCurrentRSSI() =0;
virtual uint8_t getCodingRate() const = 0;
virtual float getFreqMHz() 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
bool isJapanMode() const {
float freq = getFreqMHz();
return (fabsf(freq - 920.800f) < 0.05f ||
fabsf(freq - 921.000f) < 0.05f ||
fabsf(freq - 921.200f) < 0.05f);
}
int getMaxTextLen() const {
if (!isJapanMode()) return 10 * 16; // default
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();

View file

@ -25,8 +25,6 @@ build_flags = ${nrf52_base.build_flags}
-D PIN_BUZZER=21
-D PIN_BOARD_SDA=PIN_WIRE_SDA
-D PIN_BOARD_SCL=PIN_WIRE_SCL
-D JP_STRICT
-D LORA_CR=5
build_src_filter = ${nrf52_base.build_src_filter}
+<../variants/rak_wismesh_tag>
+<helpers/ui/MomentaryButton.cpp>

View file

@ -16,8 +16,6 @@ build_flags = ${nrf52_base.build_flags}
-D SX126X_RX_BOOSTED_GAIN=1
-D PIN_OLED_RESET=-1
-D GPS_BAUD_RATE=9600
-D JP_STRICT
-D LORA_CR=5
build_src_filter = ${nrf52_base.build_src_filter}
+<WioTrackerL1Board.cpp>
+<../variants/wio-tracker-l1>