Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/cli_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,20 @@ This document provides an overview of CLI commands that can be sent to MeshCore

---

#### View or change the CAD busy wait, in full-size packets
**Usage:**
- `get cad.wait.packets`
- `set cad.wait.packets <value>`

**Description:** When the radio reports a busy channel, transmission is deferred until it clears, up to a maximum wait. That maximum is now calculated as `value` full-size (255-byte) packet airtimes, using the current LoRa settings (spreading factor, bandwidth and coding rate). `get cad.wait.packets` reports the resulting timeout in milliseconds, along with the packet count and per-packet airtime.

**Parameters:**
- `value`: number of full-size packet airtimes to wait, 1-16

**Default:** `4` (approximately 4 seconds at SF10 / BW250)

---

#### View or change the AGC Reset Interval
**Usage:**
- `get agc.reset.interval`
Expand Down
4 changes: 4 additions & 0 deletions examples/companion_radio/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ bool MyMesh::getCADEnabled() const {
return _prefs.cad_enabled;
}

uint32_t MyMesh::getCADFailMaxDuration() const {
return _prefs.getRadioPrefs()->getCADFailWaitMs();
}

int MyMesh::calcRxDelay(float score, uint32_t air_time) const {
if (_prefs.rx_delay_base <= 0.0f) return 0;
return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time);
Expand Down
1 change: 1 addition & 0 deletions examples/companion_radio/MyMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
float getAirtimeBudgetFactor() const override;
int getInterferenceThreshold() const override;
bool getCADEnabled() const override;
uint32_t getCADFailMaxDuration() const override;
int getAGCResetInterval() const override {
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
}
Expand Down
5 changes: 5 additions & 0 deletions examples/companion_radio/NodePrefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class NodePrefs : public ConfigSerializer { // persisted to file
uint8_t path_hash_mode = 0; // which path mode to use when sending
uint8_t autoadd_max_hops = 0; // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops (max 64)
uint8_t cad_enabled = 0;
uint8_t cad_wait_packets = 4; // full-size packet airtimes to wait for CAD busy to clear
uint8_t interference_threshold = 0;
uint8_t agc_reset_interval = 0; // secs / 4
char default_scope_name[31];
Expand All @@ -69,6 +70,7 @@ class NodePrefs : public ConfigSerializer { // persisted to file
def("sf", _parent->sf);
def("cr", _parent->cr);
def("cad", _parent->cad_enabled);
def("cad_wait_pkts", _parent->cad_wait_packets);
def("int_thr", _parent->interference_threshold);
def("rxgain", _parent->rx_boosted_gain);
def("fem_rxgain", _parent->radio_fem_rxgain); // fem_rxgain WAS mapped to wrong JSON property previously
Expand Down Expand Up @@ -98,6 +100,8 @@ class NodePrefs : public ConfigSerializer { // persisted to file
void setAirtimeFactor(float af) override { _parent->airtime_factor = af; markDirty(); }
bool isCadEnabled() const override { return _parent->cad_enabled; }
void setCadEnabled(bool en) override { _parent->cad_enabled = en; markDirty(); }
uint8_t getCADWaitPackets() const override { return _parent->cad_wait_packets; }
void setCADWaitPackets(uint8_t num_packets) override { _parent->cad_wait_packets = num_packets; markDirty(); }
uint8_t getIntThresh() const override { return _parent->interference_threshold; }
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
Expand Down Expand Up @@ -220,6 +224,7 @@ class NodePrefs : public ConfigSerializer { // persisted to file
void setRepeatEn(bool en) { repeat.disable_fwd = en ? 0 : 1; }

CommonRadioPrefs* getRadioPrefs() { return &radio; }
const CommonRadioPrefs* getRadioPrefs() const { return &radio; }
KeyValueStore* getCustom() { return &custom; }

bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); }
Expand Down
3 changes: 3 additions & 0 deletions examples/simple_repeater/MyMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
bool getCADEnabled() const override {
return _prefs.cad_enabled;
}
uint32_t getCADFailMaxDuration() const override {
return _prefs.getRadioPrefs()->getCADFailWaitMs();
}
int getAGCResetInterval() const override {
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
}
Expand Down
3 changes: 3 additions & 0 deletions examples/simple_room_server/MyMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
bool getCADEnabled() const override {
return _prefs.cad_enabled;
}
uint32_t getCADFailMaxDuration() const override {
return _prefs.getRadioPrefs()->getCADFailWaitMs();
}
int getAGCResetInterval() const override {
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
}
Expand Down
3 changes: 3 additions & 0 deletions examples/simple_sensor/SensorMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ int SensorMesh::getInterferenceThreshold() const {
bool SensorMesh::getCADEnabled() const {
return _prefs.cad_enabled;
}
uint32_t SensorMesh::getCADFailMaxDuration() const {
return _prefs.getRadioPrefs()->getCADFailWaitMs();
}
int SensorMesh::getAGCResetInterval() const {
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
}
Expand Down
1 change: 1 addition & 0 deletions examples/simple_sensor/SensorMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
int getInterferenceThreshold() const override;
bool getCADEnabled() const override;
uint32_t getCADFailMaxDuration() const override;
int getAGCResetInterval() const override;
mesh::DispatcherAction onRecvPacket(mesh::Packet* pkt) override;
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
Expand Down
18 changes: 17 additions & 1 deletion src/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace mesh {

#define MAX_RX_DELAY_MILLIS 32000 // 32 seconds
#define MAX_CAD_FAIL_DURATION_MS 300000 // 5 minutes - upper bound on CAD busy timeout
#define DEFAULT_CAD_FAIL_PACKETS 4 // full-size packet airtimes (approx 4 secs at SF10/BW250)
#define MIN_TX_BUDGET_RESERVE_MS 100 // min budget (ms) required before allowing next TX
#define MIN_TX_BUDGET_AIRTIME_DIV 2 // require at least 1/N of estimated airtime as budget before TX

Expand Down Expand Up @@ -59,8 +61,22 @@ int Dispatcher::calcRxDelay(float score, uint32_t air_time) const {
uint32_t Dispatcher::getCADFailRetryDelay() const {
return 200;
}

uint32_t Dispatcher::getCADFailWaitPackets() const {
return DEFAULT_CAD_FAIL_PACKETS;
}

uint32_t Dispatcher::calcCADFailMaxDuration(Radio& radio, uint32_t num_packets) {
if (num_packets == 0) num_packets = 1; // never allow a zero-length wait
uint32_t packet_airtime = radio.getEstAirtimeFor(MAX_TRANS_UNIT);
if (packet_airtime > MAX_CAD_FAIL_DURATION_MS / num_packets) {
return MAX_CAD_FAIL_DURATION_MS;
}
return packet_airtime * num_packets;
}

uint32_t Dispatcher::getCADFailMaxDuration() const {
return 4000; // 4 seconds
return calcCADFailMaxDuration(*_radio, getCADFailWaitPackets());
}

void Dispatcher::loop() {
Expand Down
11 changes: 11 additions & 0 deletions src/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ class Dispatcher {
virtual float getAirtimeBudgetFactor() const;
virtual int calcRxDelay(float score, uint32_t air_time) const;
virtual uint32_t getCADFailRetryDelay() const;

/**
* \returns number of full-size packet airtimes to wait for channel activity to clear before forcing a TX.
*/
virtual uint32_t getCADFailWaitPackets() const;

virtual uint32_t getCADFailMaxDuration() const;
virtual int getInterferenceThreshold() const { return 0; } // disabled by default
virtual bool getCADEnabled() const { return false; } // hardware CAD disabled by default
Expand All @@ -176,6 +182,11 @@ class Dispatcher {
void begin();
void loop();

/**
* \returns CAD busy timeout, in milliseconds, for 'num_packets' full-size packets on the given radio.
*/
static uint32_t calcCADFailMaxDuration(Radio& radio, uint32_t num_packets);

Packet* obtainNewPacket();
void releasePacket(Packet* packet);
void sendPacket(Packet* packet, uint8_t priority, uint32_t delay_millis=0);
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/CommonCLI.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class NodePrefs : public ConfigSerializer {
uint8_t path_hash_mode = 0; // which path mode to use when sending
uint8_t loop_detect = 0;
uint8_t cad_enabled = 0; // hardware Channel Activity Detection before TX (boolean)
uint8_t cad_wait_packets = 4; // full-size packet airtimes to wait for CAD busy to clear
uint8_t extra_sf[4];

private:
Expand All @@ -83,6 +84,7 @@ class NodePrefs : public ConfigSerializer {
def("sf", _parent->sf);
def("cr", _parent->cr);
def("cad", _parent->cad_enabled);
def("cad_wait_pkts", _parent->cad_wait_packets);
def("int_thr", _parent->interference_threshold);
def("rxgain", _parent->rx_boosted_gain);
def("fem_rxgain", _parent->radio_fem_rxgain);
Expand Down Expand Up @@ -111,6 +113,8 @@ class NodePrefs : public ConfigSerializer {
void setAirtimeFactor(float af) override { _parent->airtime_factor = af; markDirty(); }
bool isCadEnabled() const override { return _parent->cad_enabled; }
void setCadEnabled(bool en) override { _parent->cad_enabled = en; markDirty(); }
uint8_t getCADWaitPackets() const override { return _parent->cad_wait_packets; }
void setCADWaitPackets(uint8_t num_packets) override { _parent->cad_wait_packets = num_packets; markDirty(); }
uint8_t getIntThresh() const override { return _parent->interference_threshold; }
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
Expand Down Expand Up @@ -234,6 +238,7 @@ class NodePrefs : public ConfigSerializer {
}

CommonRadioPrefs* getRadioPrefs() { return &radio; }
const CommonRadioPrefs* getRadioPrefs() const { return &radio; }
KeyValueStore* getCustom() { return &custom; }

bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); }
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/CommonRadioPrefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include "TxtDataHelpers.h"
#include "Utils.h"
#include "target.h"
#include <MeshCore.h> // MAX_TRANS_UNIT

uint32_t CommonRadioPrefs::getCADFailWaitMs() const {
return mesh::Dispatcher::calcCADFailMaxDuration(radio_driver, getCADWaitPackets());
}

bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
if (strcmp(key, "fem_rxgain") == 0) {
Expand Down Expand Up @@ -119,6 +124,24 @@ bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timest
return true;
}

if (strcmp(command, "get cad.wait.packets") == 0) {
uint8_t num_packets = getCADWaitPackets();
if (num_packets == 0) num_packets = 1; // matches Dispatcher's zero-count fallback
uint32_t packet_airtime = radio_driver.getEstAirtimeFor(MAX_TRANS_UNIT);
sprintf(reply, "> %lu ms (%u x %lu ms packets)", (unsigned long) getCADFailWaitMs(), (unsigned int) num_packets, (unsigned long) packet_airtime);
return true;
}
if (memcmp(command, "set cad.wait.packets ", 21) == 0) {
int num_packets = atoi(&command[21]);
if (num_packets < 1 || num_packets > 16) {
strcpy(reply, "ERROR: cad.wait.packets must be 1-16");
} else {
setCADWaitPackets((uint8_t) num_packets);
sprintf(reply, "OK - %u packets (%lu ms)", (unsigned int) num_packets, (unsigned long) getCADFailWaitMs());
}
return true;
}

if (strcmp(command, "get radio.rxgain") == 0) {
sprintf(reply, "> %s", getRxGain() != 0 ? "on" : "off");
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/CommonRadioPrefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class CommonRadioPrefs : public ConfigSerializer, public KeyValueStore {
virtual bool isCadEnabled() const = 0;
virtual void setCadEnabled(bool en) = 0;

virtual uint8_t getCADWaitPackets() const = 0;
virtual void setCADWaitPackets(uint8_t num_packets) = 0;

/**
* \returns CAD busy timeout in milliseconds, using the current radio settings.
*/
uint32_t getCADFailWaitMs() const;

virtual uint8_t getIntThresh() const = 0;
virtual void setIntThresh(uint8_t t) = 0;

Expand Down
19 changes: 19 additions & 0 deletions test/test_companion_node_prefs/test_companion_node_prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ TEST(CompanionNodePrefs, RxGainSettingsRoundTripIndependently) {
}
#endif

TEST(CompanionNodePrefs, CADWaitPacketsRoundTripAndDefault) {
NodePrefs saved;
saved.cad_wait_packets = 7;

CaptureStream output;
ASSERT_TRUE(saved.saveSerial(output));
EXPECT_NE(std::string::npos, output.text().find("cad_wait_pkts:7"));

ReplayStream input("{radio:{cad_wait_pkts:9}}");
NodePrefs loaded;
ASSERT_TRUE(loaded.loadSerial(input));
EXPECT_EQ(9, loaded.cad_wait_packets);

ReplayStream empty("{}");
NodePrefs defaulted;
ASSERT_TRUE(defaulted.loadSerial(empty));
EXPECT_EQ(4, defaulted.cad_wait_packets);
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down