Reduce background polling and radio power use
This commit is contained in:
1 parent
b9e7e416be
commit
4f839d3477
19 files changed
+802
-273
No files matched your search
@@ -3,6 +3,13 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
uint32_t delay_ms = 1000UL << shift;
|
||||
return delay_ms > 30000UL ? 30000UL : delay_ms;
|
||||
}
|
||||
|
||||
#ifndef RXPS_FIXED_ENABLED
|
||||
#define RXPS_FIXED_ENABLED 1
|
||||
#endif
|
||||
@@ -1082,6 +1089,8 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
_iter_started = false;
|
||||
_cli_rescue = false;
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
offline_queue_len = 0;
|
||||
app_target_ver = 0;
|
||||
clearPendingReqs();
|
||||
@@ -2573,7 +2582,8 @@ void MyMesh::checkSerialInterface() {
|
||||
|
||||
void MyMesh::loop() {
|
||||
BaseChatMesh::loop();
|
||||
if (saved_radio_apply_pending && !hasOutbound()) {
|
||||
if (saved_radio_apply_pending && !hasOutbound()
|
||||
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at))) {
|
||||
// A power-saving wake can enter begin() with a complete packet already
|
||||
// waiting. Preserve that packet, then apply the persisted radio settings
|
||||
// once the receive/response path is idle.
|
||||
@@ -2581,6 +2591,10 @@ void MyMesh::loop() {
|
||||
if (applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
} else {
|
||||
radio_apply_retry_at = futureMillis(nextRadioApplyRetryDelay(radio_apply_failures));
|
||||
}
|
||||
}
|
||||
if (has_next_ack_expiry
|
||||
@@ -2627,6 +2641,11 @@ bool MyMesh::advert() {
|
||||
|
||||
// To check if there is pending work
|
||||
bool MyMesh::hasPendingWork() const {
|
||||
return _mgr->getOutboundTotal() > 0 || dirty_contacts_expiry != 0
|
||||
|| emergency_client_repeat_packet != NULL;
|
||||
if (radio_driver.isWatchdogObserving() || radio_driver.isCalibratingNoiseFloor()) return true;
|
||||
return hasQueuedWorkDue() || hasRetryWorkDue()
|
||||
|| (saved_radio_apply_pending
|
||||
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at)))
|
||||
|| (dirty_contacts_expiry != 0 && millisHasNowPassed(dirty_contacts_expiry))
|
||||
|| (emergency_client_repeat_packet != NULL
|
||||
&& millisHasNowPassed(emergency_client_repeat_send_at));
|
||||
}
|
||||
@@ -224,6 +224,8 @@ private:
|
||||
bool _iter_started;
|
||||
bool _cli_rescue;
|
||||
bool saved_radio_apply_pending;
|
||||
unsigned long radio_apply_retry_at;
|
||||
uint8_t radio_apply_failures;
|
||||
bool send_unscoped; // force un-scoped flood (instead of using send_scope)
|
||||
char cli_command[80];
|
||||
uint8_t app_target_ver;
|
||||
|
||||
@@ -105,6 +105,13 @@
|
||||
#define LOW_BATTERY_CHECK_INTERVAL (30UL * 60UL * 1000UL)
|
||||
#define LOW_BATTERY_ALERT_INTERVAL (12UL * 60UL * 60UL * 1000UL)
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
uint32_t delay_ms = 1000UL << shift;
|
||||
return delay_ms > 30000UL ? 30000UL : delay_ms;
|
||||
}
|
||||
|
||||
static const char* skipLocalSpaces(const char* text) {
|
||||
while (text != NULL && *text == ' ') text++;
|
||||
return text;
|
||||
@@ -2322,6 +2329,14 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
active_cr = 0;
|
||||
saved_radio_apply_pending = false;
|
||||
temp_radio_handoff_pending = false;
|
||||
scheduled_temp_radio_started = false;
|
||||
next_scheduled_radio_time = 0;
|
||||
next_scheduled_radio_check_at = 0;
|
||||
scheduled_temp_radio_end_time = 0;
|
||||
scheduled_temp_radio_end_check_at = 0;
|
||||
scheduled_temp_radio_end_check_final = false;
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
memset(scheduled_radio_settings, 0, sizeof(scheduled_radio_settings));
|
||||
_logging = false;
|
||||
region_load_active = false;
|
||||
@@ -2750,26 +2765,83 @@ bool MyMesh::applySavedRadioParams() {
|
||||
return applyRadioParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
|
||||
}
|
||||
|
||||
bool MyMesh::hasStartedScheduledTempRadio() const {
|
||||
void MyMesh::queueSavedRadioApply() {
|
||||
saved_radio_apply_pending = true;
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
}
|
||||
|
||||
void MyMesh::refreshScheduledRadioState() {
|
||||
next_scheduled_radio_time = 0;
|
||||
scheduled_temp_radio_started = false;
|
||||
scheduled_temp_radio_end_time = 0;
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && setting.started) {
|
||||
return true;
|
||||
if (!setting.active) continue;
|
||||
|
||||
uint32_t deadline = setting.start_time;
|
||||
if (setting.temporary && setting.started) {
|
||||
scheduled_temp_radio_started = true;
|
||||
if (scheduled_temp_radio_end_time == 0 || setting.end_time < scheduled_temp_radio_end_time) {
|
||||
scheduled_temp_radio_end_time = setting.end_time;
|
||||
}
|
||||
deadline = setting.end_time;
|
||||
}
|
||||
if (next_scheduled_radio_time == 0 || deadline < next_scheduled_radio_time) {
|
||||
next_scheduled_radio_time = deadline;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
const uint32_t now = (next_scheduled_radio_time != 0 || scheduled_temp_radio_end_time != 0)
|
||||
? getRTCClock()->getCurrentTime()
|
||||
: 0;
|
||||
if (next_scheduled_radio_time != 0) {
|
||||
uint32_t delay_ms = 0;
|
||||
if (next_scheduled_radio_time > now) {
|
||||
uint32_t delay_secs = next_scheduled_radio_time - now;
|
||||
// millis timers are only unambiguous for half of their rollover range.
|
||||
// A minute checkpoint handles RTC corrections without per-loop RTC reads
|
||||
// or table scans, while bounding a forward clock-sync delay to one minute.
|
||||
if (delay_secs > SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS) {
|
||||
delay_secs = SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS;
|
||||
}
|
||||
delay_ms = delay_secs * 1000UL;
|
||||
}
|
||||
next_scheduled_radio_check_at = futureMillis(delay_ms);
|
||||
} else {
|
||||
next_scheduled_radio_check_at = 0;
|
||||
}
|
||||
if (scheduled_temp_radio_end_time != 0) {
|
||||
uint32_t delay_ms = 0;
|
||||
scheduled_temp_radio_end_check_final = true;
|
||||
if (scheduled_temp_radio_end_time > now) {
|
||||
uint32_t delay_secs = scheduled_temp_radio_end_time - now;
|
||||
if (delay_secs > SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS) {
|
||||
delay_secs = SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS;
|
||||
scheduled_temp_radio_end_check_final = false;
|
||||
}
|
||||
delay_ms = delay_secs * 1000UL;
|
||||
}
|
||||
scheduled_temp_radio_end_check_at = futureMillis(delay_ms);
|
||||
} else {
|
||||
scheduled_temp_radio_end_check_at = 0;
|
||||
scheduled_temp_radio_end_check_final = false;
|
||||
}
|
||||
if (next_scheduled_radio_time == 0 && !saved_radio_apply_pending) {
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool MyMesh::hasStartedScheduledTempRadio() const {
|
||||
return scheduled_temp_radio_started;
|
||||
}
|
||||
|
||||
#if defined(ENABLE_OTA)
|
||||
bool MyMesh::isTempRadioActive() const {
|
||||
const uint32_t now = getRTCClock()->getCurrentTime();
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && setting.started && now < setting.end_time) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return scheduled_temp_radio_started
|
||||
&& (!scheduled_temp_radio_end_check_final
|
||||
|| scheduled_temp_radio_end_check_at == 0
|
||||
|| !millisHasNowPassed(scheduled_temp_radio_end_check_at));
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2863,13 +2935,14 @@ void MyMesh::clearScheduledRadioSetting(int idx, bool restore_if_started) {
|
||||
&& scheduled_radio_settings[idx].started;
|
||||
scheduled_radio_settings[idx].active = false;
|
||||
scheduled_radio_settings[idx].started = false;
|
||||
refreshScheduledRadioState();
|
||||
if (scheduled_radio_settings[idx].temporary && temp_radio_handoff_pending
|
||||
&& countScheduledRadioSettings(true) == 0) {
|
||||
temp_radio_handoff_pending = false;
|
||||
saved_radio_apply_pending = true;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
if (restore_radio && !hasStartedScheduledTempRadio()) {
|
||||
saved_radio_apply_pending = true;
|
||||
if ((restore_radio || saved_radio_apply_pending) && !hasStartedScheduledTempRadio()) {
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2984,6 +3057,11 @@ void MyMesh::addScheduledRadioParams(bool temporary, float freq, float bw, uint8
|
||||
scheduled_radio_settings[slot].cr = cr;
|
||||
scheduled_radio_settings[slot].start_time = start_time;
|
||||
scheduled_radio_settings[slot].end_time = temporary ? end_time : 0;
|
||||
// A newly requested schedule must not inherit the backoff of an older radio
|
||||
// apply failure, especially when its deadline is sooner than that retry.
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
refreshScheduledRadioState();
|
||||
|
||||
char delay[16];
|
||||
formatScheduledRadioDuration(delay, sizeof(delay), start_time);
|
||||
@@ -3060,12 +3138,13 @@ void MyMesh::deleteScheduledRadioParams(bool temporary, const char* selector, ch
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
if (restore_radio && !hasStartedScheduledTempRadio()) {
|
||||
saved_radio_apply_pending = true;
|
||||
refreshScheduledRadioState();
|
||||
if ((restore_radio || saved_radio_apply_pending) && !hasStartedScheduledTempRadio()) {
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
if (temporary && temp_radio_handoff_pending) {
|
||||
temp_radio_handoff_pending = false;
|
||||
saved_radio_apply_pending = true;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
snprintf(reply, 160, "OK - deleted %d", deleted);
|
||||
return;
|
||||
@@ -3087,14 +3166,33 @@ void MyMesh::deleteScheduledRadioParams(bool temporary, const char* selector, ch
|
||||
}
|
||||
|
||||
void MyMesh::processScheduledRadioSettings() {
|
||||
// Never touch modulation registers while a packet is still on air. Due work
|
||||
// remains queued and is retried on the first loop after TX completion.
|
||||
if (hasOutbound()) return;
|
||||
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) return;
|
||||
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
const bool schedule_check_due = next_scheduled_radio_time != 0
|
||||
&& (next_scheduled_radio_check_at == 0
|
||||
|| millisHasNowPassed(next_scheduled_radio_check_at));
|
||||
uint32_t now = 0;
|
||||
bool schedule_due = false;
|
||||
if (schedule_check_due) {
|
||||
now = getRTCClock()->getCurrentTime();
|
||||
schedule_due = now >= next_scheduled_radio_time;
|
||||
if (!schedule_due) refreshScheduledRadioState();
|
||||
}
|
||||
bool saved_apply_due = saved_radio_apply_pending && !temp_radio_handoff_pending
|
||||
&& !scheduled_temp_radio_started;
|
||||
if (!schedule_due && !saved_apply_due) return;
|
||||
|
||||
// Never touch modulation registers while a packet is still on air. Back off
|
||||
// this check too; a long packet should not make the scheduler poll every loop.
|
||||
if (hasOutbound()) {
|
||||
scheduled_radio_retry_at = futureMillis(RADIO_APPLY_RETRY_INTERVAL_MILLIS);
|
||||
return;
|
||||
}
|
||||
|
||||
bool apply_failed = false;
|
||||
bool saved_params_changed = false;
|
||||
|
||||
while (true) {
|
||||
while (schedule_due) {
|
||||
int due_idx = -1;
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
@@ -3125,36 +3223,46 @@ void MyMesh::processScheduledRadioSettings() {
|
||||
// persisted SF/BW. Manual RX/sleep timings intentionally remain fixed.
|
||||
CommonCLI::recalculateRxPowerSavingFromLevel(&_prefs);
|
||||
savePrefs();
|
||||
saved_radio_apply_pending = true;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && setting.started && now >= setting.end_time) {
|
||||
setting.active = false;
|
||||
setting.started = false;
|
||||
saved_radio_apply_pending = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && !setting.started && now >= setting.start_time) {
|
||||
if (now >= setting.end_time) {
|
||||
if (schedule_due) {
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && setting.started && now >= setting.end_time) {
|
||||
setting.active = false;
|
||||
if (temp_radio_handoff_pending) {
|
||||
setting.started = false;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (setting.active && setting.temporary && !setting.started && now >= setting.start_time) {
|
||||
if (now >= setting.end_time) {
|
||||
setting.active = false;
|
||||
if (temp_radio_handoff_pending) {
|
||||
temp_radio_handoff_pending = false;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
} else if (applyRadioParams(setting.freq, setting.bw, setting.sf, setting.cr)) {
|
||||
setting.started = true;
|
||||
temp_radio_handoff_pending = false;
|
||||
} else {
|
||||
// setParams() can fail after changing only part of the modulation
|
||||
// tuple. Restore the saved tuple if this temporary window expires
|
||||
// before a later retry succeeds.
|
||||
saved_radio_apply_pending = true;
|
||||
apply_failed = true;
|
||||
break;
|
||||
}
|
||||
} else if (applyRadioParams(setting.freq, setting.bw, setting.sf, setting.cr)) {
|
||||
setting.started = true;
|
||||
temp_radio_handoff_pending = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
refreshScheduledRadioState();
|
||||
if (saved_radio_apply_pending && !temp_radio_handoff_pending
|
||||
&& !hasStartedScheduledTempRadio()) {
|
||||
&& !scheduled_temp_radio_started && !apply_failed) {
|
||||
// If begin() deferred the saved params to preserve a wake packet, its gain
|
||||
// update was deferred for the same reason. Retry both at the first safe
|
||||
// handoff; unsupported boosted-gain modes remain harmless here.
|
||||
@@ -3162,8 +3270,16 @@ void MyMesh::processScheduledRadioSettings() {
|
||||
if (applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
} else {
|
||||
apply_failed = true;
|
||||
}
|
||||
}
|
||||
if (apply_failed) {
|
||||
scheduled_radio_retry_at = futureMillis(nextRadioApplyRetryDelay(scheduled_radio_retry_failures));
|
||||
} else {
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool MyMesh::isMillisTimerDue(unsigned long timestamp) const {
|
||||
@@ -3171,27 +3287,12 @@ bool MyMesh::isMillisTimerDue(unsigned long timestamp) const {
|
||||
}
|
||||
|
||||
bool MyMesh::hasScheduledRadioWorkDue() const {
|
||||
if (saved_radio_apply_pending) return true;
|
||||
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (!setting.active) {
|
||||
continue;
|
||||
}
|
||||
if (!setting.temporary && now >= setting.start_time) {
|
||||
return true;
|
||||
}
|
||||
if (setting.temporary) {
|
||||
if (!setting.started && now >= setting.start_time) {
|
||||
return true;
|
||||
}
|
||||
if (setting.started && now >= setting.end_time) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) return false;
|
||||
if (saved_radio_apply_pending && !temp_radio_handoff_pending
|
||||
&& !scheduled_temp_radio_started) return true;
|
||||
return next_scheduled_radio_time != 0
|
||||
&& (next_scheduled_radio_check_at == 0
|
||||
|| millisHasNowPassed(next_scheduled_radio_check_at));
|
||||
}
|
||||
|
||||
uint32_t MyMesh::limitSleepToMillisTimer(unsigned long timestamp, uint32_t sleep_secs) const {
|
||||
@@ -3207,32 +3308,11 @@ uint32_t MyMesh::limitSleepToMillisTimer(unsigned long timestamp, uint32_t sleep
|
||||
return remaining_secs < sleep_secs ? remaining_secs : sleep_secs;
|
||||
}
|
||||
|
||||
uint32_t MyMesh::limitSleepToRtcTime(uint32_t timestamp, uint32_t sleep_secs) const {
|
||||
if (!timestamp || sleep_secs == 0) {
|
||||
return sleep_secs;
|
||||
}
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
if (now >= timestamp) {
|
||||
return 0;
|
||||
}
|
||||
uint32_t remaining_secs = timestamp - now;
|
||||
return remaining_secs < sleep_secs ? remaining_secs : sleep_secs;
|
||||
}
|
||||
|
||||
uint32_t MyMesh::limitSleepToScheduledRadioWork(uint32_t sleep_secs) const {
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
|
||||
if (!setting.active) {
|
||||
continue;
|
||||
}
|
||||
if (!setting.temporary || !setting.started) {
|
||||
sleep_secs = limitSleepToRtcTime(setting.start_time, sleep_secs);
|
||||
}
|
||||
if (setting.temporary && setting.started) {
|
||||
sleep_secs = limitSleepToRtcTime(setting.end_time, sleep_secs);
|
||||
}
|
||||
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) {
|
||||
return limitSleepToMillisTimer(scheduled_radio_retry_at, sleep_secs);
|
||||
}
|
||||
return sleep_secs;
|
||||
return limitSleepToMillisTimer(next_scheduled_radio_check_at, sleep_secs);
|
||||
}
|
||||
|
||||
uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
|
||||
@@ -3241,6 +3321,16 @@ uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
|
||||
}
|
||||
|
||||
uint32_t sleep_secs = max_secs;
|
||||
uint32_t queue_delay_ms;
|
||||
if (getNextQueueWakeDelay(queue_delay_ms)) {
|
||||
uint32_t queue_delay_secs = (queue_delay_ms + 999UL) / 1000UL;
|
||||
if (queue_delay_secs < sleep_secs) sleep_secs = queue_delay_secs;
|
||||
}
|
||||
uint32_t retry_delay_ms;
|
||||
if (getNextRetryWakeDelay(retry_delay_ms)) {
|
||||
uint32_t retry_delay_secs = (retry_delay_ms + 999UL) / 1000UL;
|
||||
if (retry_delay_secs < sleep_secs) sleep_secs = retry_delay_secs;
|
||||
}
|
||||
sleep_secs = limitSleepToMillisTimer(next_flood_advert, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(next_local_advert, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(dirty_contacts_expiry, sleep_secs);
|
||||
@@ -3253,6 +3343,8 @@ uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
|
||||
}
|
||||
|
||||
void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
|
||||
scheduled_radio_retry_at = 0;
|
||||
scheduled_radio_retry_failures = 0;
|
||||
bool cancelled_started_temp = false;
|
||||
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
|
||||
if (scheduled_radio_settings[i].active && scheduled_radio_settings[i].temporary) {
|
||||
@@ -3272,8 +3364,9 @@ void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr,
|
||||
if (slot < 0) {
|
||||
if (temp_radio_handoff_pending) {
|
||||
temp_radio_handoff_pending = false;
|
||||
saved_radio_apply_pending = true;
|
||||
queueSavedRadioApply();
|
||||
}
|
||||
refreshScheduledRadioState();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3287,6 +3380,7 @@ void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr,
|
||||
scheduled_radio_settings[slot].cr = cr;
|
||||
scheduled_radio_settings[slot].start_time = start_time;
|
||||
scheduled_radio_settings[slot].end_time = start_time + ((uint32_t)timeout_mins * 60);
|
||||
refreshScheduledRadioState();
|
||||
}
|
||||
|
||||
bool MyMesh::formatFileSystem() {
|
||||
@@ -4291,7 +4385,7 @@ bool MyMesh::hasPendingWork() const {
|
||||
#endif
|
||||
if (radio_driver.isWatchdogObserving()) return true; // keep MCU awake for one radio duty cycle
|
||||
if (radio_driver.isCalibratingNoiseFloor()) return true; // keep MCU awake for the noise-floor window
|
||||
if (_mgr->getOutboundTotal() > 0) return true;
|
||||
if (hasQueuedWorkDue() || hasRetryWorkDue()) return true;
|
||||
if (isMillisTimerDue(next_flood_advert) || isMillisTimerDue(next_local_advert)) return true;
|
||||
if (isMillisTimerDue(dirty_contacts_expiry)) return true;
|
||||
if (isMillisTimerDue(next_recent_repeater_sweep)) return true;
|
||||
|
||||
@@ -115,6 +115,8 @@ struct NeighbourInfo {
|
||||
|
||||
#define RECENT_REPEATER_MAX_AGE_MILLIS (24UL * 60UL * 60UL * 1000UL)
|
||||
#define RECENT_REPEATER_SWEEP_INTERVAL_MILLIS (3UL * 60UL * 60UL * 1000UL)
|
||||
#define RADIO_APPLY_RETRY_INTERVAL_MILLIS 1000UL
|
||||
#define SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS 60UL
|
||||
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
struct ScheduledRadioSetting {
|
||||
@@ -189,6 +191,14 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
uint8_t active_cr; // live CR, including temporary radio overrides
|
||||
bool saved_radio_apply_pending;
|
||||
bool temp_radio_handoff_pending;
|
||||
bool scheduled_temp_radio_started;
|
||||
uint32_t next_scheduled_radio_time;
|
||||
unsigned long next_scheduled_radio_check_at;
|
||||
uint32_t scheduled_temp_radio_end_time;
|
||||
unsigned long scheduled_temp_radio_end_check_at;
|
||||
bool scheduled_temp_radio_end_check_final;
|
||||
unsigned long scheduled_radio_retry_at;
|
||||
uint8_t scheduled_radio_retry_failures;
|
||||
ScheduledRadioSetting scheduled_radio_settings[MAX_SCHEDULED_RADIO_SETTINGS];
|
||||
int matching_peer_indexes[MAX_CLIENTS];
|
||||
#if defined(WITH_MQTT_BRIDGE)
|
||||
@@ -265,6 +275,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
bool isLooped(const mesh::Packet* packet, const uint8_t max_counters[]);
|
||||
bool applyRadioParams(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
bool applySavedRadioParams();
|
||||
void queueSavedRadioApply();
|
||||
void refreshScheduledRadioState();
|
||||
void processScheduledRadioSettings();
|
||||
bool isMillisTimerDue(unsigned long timestamp) const;
|
||||
void loadFloodChannelBlocks();
|
||||
@@ -283,7 +295,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
void formatFloodChannelBlockDetail(char* reply, int idx) const;
|
||||
bool hasScheduledRadioWorkDue() const;
|
||||
uint32_t limitSleepToMillisTimer(unsigned long timestamp, uint32_t sleep_secs) const;
|
||||
uint32_t limitSleepToRtcTime(uint32_t timestamp, uint32_t sleep_secs) const;
|
||||
uint32_t limitSleepToScheduledRadioWork(uint32_t sleep_secs) const;
|
||||
bool hasStartedScheduledTempRadio() const;
|
||||
int findFreeScheduledRadioSlot() const;
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
#include "MyMesh.h"
|
||||
#include <helpers/RxReservePacketManager.h>
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
uint32_t delay_ms = 1000UL << shift;
|
||||
return delay_ms > 30000UL ? 30000UL : delay_ms;
|
||||
}
|
||||
|
||||
#define REPLY_DELAY_MILLIS 1500
|
||||
#define PUSH_NOTIFY_DELAY_MILLIS 2000
|
||||
#define SYNC_PUSH_INTERVAL 1200
|
||||
@@ -655,6 +662,8 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
active_cr = LORA_CR;
|
||||
temp_radio_applied = false;
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
recv_pkt_region = NULL;
|
||||
|
||||
// defaults
|
||||
@@ -853,6 +862,8 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui
|
||||
}
|
||||
|
||||
void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
|
||||
pending_freq = freq;
|
||||
pending_bw = bw;
|
||||
@@ -1146,12 +1157,16 @@ void MyMesh::loop() {
|
||||
}
|
||||
|
||||
const bool revert_radio_due = revert_radio_at && millisHasNowPassed(revert_radio_at);
|
||||
const bool radio_apply_ready = !radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at);
|
||||
bool radio_apply_failed = false;
|
||||
if (revert_radio_due && !temp_radio_applied) {
|
||||
// The temporary window ended before it could be applied. Drop both timers
|
||||
// so a previously busy radio cannot switch to the expired channel later.
|
||||
set_radio_at = revert_radio_at = 0;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Temp radio params expired before apply");
|
||||
} else if (revert_radio_due && !hasOutbound()) {
|
||||
} else if (revert_radio_due && !hasOutbound() && radio_apply_ready) {
|
||||
if (applySavedRadioParams()) {
|
||||
if (saved_radio_apply_pending) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
@@ -1159,9 +1174,14 @@ void MyMesh::loop() {
|
||||
set_radio_at = revert_radio_at = 0;
|
||||
temp_radio_applied = false;
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Radio params restored");
|
||||
} else {
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
} else if (set_radio_at && millisHasNowPassed(set_radio_at) && !hasOutbound()) {
|
||||
} else if (set_radio_at && millisHasNowPassed(set_radio_at) && !hasOutbound()
|
||||
&& radio_apply_ready) {
|
||||
uint32_t rx_us = _prefs.rx_ps_rx_us;
|
||||
uint32_t sleep_us = _prefs.rx_ps_sleep_us;
|
||||
bool timing_ok = true;
|
||||
@@ -1179,14 +1199,30 @@ void MyMesh::loop() {
|
||||
set_radio_at = 0;
|
||||
active_cr = pending_cr;
|
||||
temp_radio_applied = true;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Temp radio params");
|
||||
} else {
|
||||
// A failed setParams() may have applied only a prefix of the tuple.
|
||||
// Ensure expiry restores the complete saved configuration.
|
||||
saved_radio_apply_pending = true;
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (saved_radio_apply_pending && !temp_radio_applied && !hasOutbound()
|
||||
&& applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
&& radio_apply_ready && !radio_apply_failed) {
|
||||
if (applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
} else {
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
}
|
||||
if (radio_apply_failed) {
|
||||
radio_apply_retry_at = futureMillis(nextRadioApplyRetryDelay(radio_apply_failures));
|
||||
}
|
||||
|
||||
// is pending dirty contacts write needed?
|
||||
@@ -1230,13 +1266,28 @@ uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
|
||||
}
|
||||
|
||||
uint32_t sleep_secs = max_secs;
|
||||
uint32_t queue_delay_ms;
|
||||
if (getNextQueueWakeDelay(queue_delay_ms)) {
|
||||
uint32_t queue_delay_secs = (queue_delay_ms + 999UL) / 1000UL;
|
||||
if (queue_delay_secs < sleep_secs) sleep_secs = queue_delay_secs;
|
||||
}
|
||||
uint32_t retry_delay_ms;
|
||||
if (getNextRetryWakeDelay(retry_delay_ms)) {
|
||||
uint32_t retry_delay_secs = (retry_delay_ms + 999UL) / 1000UL;
|
||||
if (retry_delay_secs < sleep_secs) sleep_secs = retry_delay_secs;
|
||||
}
|
||||
if (acl.getNumClients() > 0) {
|
||||
sleep_secs = limitSleepToMillisTimer(next_push, sleep_secs);
|
||||
}
|
||||
sleep_secs = limitSleepToMillisTimer(next_flood_advert, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(next_local_advert, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(set_radio_at, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(revert_radio_at, sleep_secs);
|
||||
const bool radio_apply_backoff = radio_apply_retry_at && !millisHasNowPassed(radio_apply_retry_at);
|
||||
if (radio_apply_backoff) {
|
||||
sleep_secs = limitSleepToMillisTimer(radio_apply_retry_at, sleep_secs);
|
||||
} else {
|
||||
sleep_secs = limitSleepToMillisTimer(set_radio_at, sleep_secs);
|
||||
sleep_secs = limitSleepToMillisTimer(revert_radio_at, sleep_secs);
|
||||
}
|
||||
sleep_secs = limitSleepToMillisTimer(dirty_contacts_expiry, sleep_secs);
|
||||
return sleep_secs;
|
||||
}
|
||||
@@ -1248,9 +1299,12 @@ bool MyMesh::hasPendingWork() const {
|
||||
#endif
|
||||
if (radio_driver.isWatchdogObserving()) return true; // keep MCU awake for one radio duty cycle
|
||||
if (radio_driver.isCalibratingNoiseFloor()) return true; // keep MCU awake for the noise-floor window
|
||||
if (_mgr->getOutboundTotal() > 0) return true;
|
||||
if (hasQueuedWorkDue() || hasRetryWorkDue()) return true;
|
||||
if (acl.getNumClients() > 0 && isMillisTimerDue(next_push)) return true;
|
||||
if (isMillisTimerDue(next_flood_advert) || isMillisTimerDue(next_local_advert)) return true;
|
||||
if (isMillisTimerDue(set_radio_at) || isMillisTimerDue(revert_radio_at)) return true;
|
||||
const bool radio_apply_backoff = radio_apply_retry_at && !isMillisTimerDue(radio_apply_retry_at);
|
||||
if (!radio_apply_backoff
|
||||
&& (isMillisTimerDue(set_radio_at) || isMillisTimerDue(revert_radio_at)
|
||||
|| (saved_radio_apply_pending && !temp_radio_applied))) return true;
|
||||
return isMillisTimerDue(dirty_contacts_expiry);
|
||||
}
|
||||
@@ -125,6 +125,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
uint8_t active_cr;
|
||||
bool temp_radio_applied;
|
||||
bool saved_radio_apply_pending;
|
||||
unsigned long radio_apply_retry_at;
|
||||
uint8_t radio_apply_failures;
|
||||
int matching_peer_indexes[MAX_CLIENTS];
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
MQTTBridge* bridge;
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
#include "SensorMesh.h"
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
uint32_t delay_ms = 1000UL << shift;
|
||||
return delay_ms > 30000UL ? 30000UL : delay_ms;
|
||||
}
|
||||
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
#ifndef LORA_FREQ
|
||||
@@ -753,6 +760,8 @@ SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::Millise
|
||||
active_cr = LORA_CR;
|
||||
temp_radio_applied = false;
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
|
||||
// defaults
|
||||
memset(&_prefs, 0, sizeof(_prefs));
|
||||
@@ -875,6 +884,8 @@ void SensorMesh::saveIdentity(const mesh::LocalIdentity& new_id) {
|
||||
}
|
||||
|
||||
void SensorMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
|
||||
pending_freq = freq;
|
||||
pending_bw = bw;
|
||||
@@ -987,11 +998,15 @@ void SensorMesh::loop() {
|
||||
}
|
||||
|
||||
const bool revert_radio_due = revert_radio_at && millisHasNowPassed(revert_radio_at);
|
||||
const bool radio_apply_ready = !radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at);
|
||||
bool radio_apply_failed = false;
|
||||
if (revert_radio_due && !temp_radio_applied) {
|
||||
// Never apply a temporary channel after its window has already ended.
|
||||
set_radio_at = revert_radio_at = 0;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Temp radio params expired before apply");
|
||||
} else if (revert_radio_due && !hasOutbound()) {
|
||||
} else if (revert_radio_due && !hasOutbound() && radio_apply_ready) {
|
||||
if (applySavedRadioParams()) {
|
||||
if (saved_radio_apply_pending) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
@@ -999,9 +1014,14 @@ void SensorMesh::loop() {
|
||||
set_radio_at = revert_radio_at = 0;
|
||||
temp_radio_applied = false;
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Radio params restored");
|
||||
} else {
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
} else if (set_radio_at && millisHasNowPassed(set_radio_at) && !hasOutbound()) {
|
||||
} else if (set_radio_at && millisHasNowPassed(set_radio_at) && !hasOutbound()
|
||||
&& radio_apply_ready) {
|
||||
uint32_t rx_us = _prefs.rx_ps_rx_us;
|
||||
uint32_t sleep_us = _prefs.rx_ps_sleep_us;
|
||||
bool timing_ok = true;
|
||||
@@ -1019,14 +1039,30 @@ void SensorMesh::loop() {
|
||||
set_radio_at = 0;
|
||||
active_cr = pending_cr;
|
||||
temp_radio_applied = true;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
MESH_DEBUG_PRINTLN("Temp radio params");
|
||||
} else {
|
||||
// A failed setParams() may have applied only a prefix of the tuple.
|
||||
// Ensure expiry restores the complete saved configuration.
|
||||
saved_radio_apply_pending = true;
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (saved_radio_apply_pending && !temp_radio_applied && !hasOutbound()
|
||||
&& applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
&& radio_apply_ready && !radio_apply_failed) {
|
||||
if (applySavedRadioParams()) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
saved_radio_apply_pending = false;
|
||||
radio_apply_retry_at = 0;
|
||||
radio_apply_failures = 0;
|
||||
} else {
|
||||
radio_apply_failed = true;
|
||||
}
|
||||
}
|
||||
if (radio_apply_failed) {
|
||||
radio_apply_retry_at = futureMillis(nextRadioApplyRetryDelay(radio_apply_failures));
|
||||
}
|
||||
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
|
||||
@@ -165,6 +165,8 @@ private:
|
||||
uint8_t active_cr;
|
||||
bool temp_radio_applied;
|
||||
bool saved_radio_apply_pending;
|
||||
unsigned long radio_apply_retry_at;
|
||||
uint8_t radio_apply_failures;
|
||||
|
||||
bool applySavedRadioParams();
|
||||
|
||||
|
||||
Reference in new issue
Block a user