Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ Unit tests in `test/` directory. The canonical suite count is in `test/native-su
- `test_packet_signing/` - Packet signing
- `test_position_module/` - Position module behaviour
- `test_position_precision/` - Position precision helpers
- `test_power_fsm/` - PowerFSM timeout helpers
- `test_radio/` - Radio interface
- `test_rtc/` - RTC / time handling
- `test_serial/` - Serial communication
Expand Down
29 changes: 15 additions & 14 deletions src/PowerFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ static uint32_t getBluetoothWaitMs()
if (!isBluetoothEnabledForPowerFSM())
return 0;

return Default::getConfiguredOrDefaultMs(config.power.wait_bluetooth_secs, default_wait_bluetooth_secs);
return Default::getTimeoutMs(config.power.wait_bluetooth_secs, default_wait_bluetooth_secs);
}

// A zero display timeout means "go dark immediately", which as a DARK self-transition period
// would re-enter DARK on every FSM tick, so fall back to the default period.
uint32_t getDarkRecheckMs()
{
uint32_t timeoutMs = Default::getTimeoutMs(config.display.screen_on_secs, default_screen_on_secs);
return timeoutMs > 0 ? timeoutMs : Default::getConfiguredOrDefaultMs(0, default_screen_on_secs);
}

#if defined(T5_S3_EPAPER_PRO)
Expand Down Expand Up @@ -325,6 +333,7 @@ void PowerFSM_setup()
? 1
: 0);
bool hasPower = isPowered();
uint32_t displayTimeoutMs = Default::getTimeoutMs(config.display.screen_on_secs, default_screen_on_secs);

LOG_INFO("PowerFSM init, USB power=%d", hasPower ? 1 : 0);
powerFSM.add_timed_transition(&stateBOOT, hasPower ? &statePOWER : &stateON, 3 * 1000, NULL, "boot timeout");
Expand Down Expand Up @@ -420,12 +429,8 @@ void PowerFSM_setup()
if (config.display.screen_on_secs > 0)
#endif
{
powerFSM.add_timed_transition(&stateON, &stateDARK,
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
NULL, "Screen-on timeout");
powerFSM.add_timed_transition(&statePOWER, &stateDARK,
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
NULL, "Screen-on timeout");
powerFSM.add_timed_transition(&stateON, &stateDARK, displayTimeoutMs, NULL, "Screen-on timeout");
powerFSM.add_timed_transition(&statePOWER, &stateDARK, displayTimeoutMs, NULL, "Screen-on timeout");
}

// We never enter light-sleep or NB states on NRF52 (because the CPU uses so little power normally)
Expand All @@ -441,25 +446,21 @@ void PowerFSM_setup()

if ((isRouter || config.power.is_power_saving) && !isWifiAvailable() && !isTrackerOrSensor) {
powerFSM.add_timed_transition(&stateNB, &stateLS,
Default::getConfiguredOrDefaultMs(config.power.min_wake_secs, default_min_wake_secs), NULL,
Default::getTimeoutMs(config.power.min_wake_secs, default_min_wake_secs), NULL,
"Min wake timeout");

// If ESP32 and using power-saving, timer mover from DARK to light-sleep
// Also serves purpose of the old DARK to DARK transition(?) See https://github.com/meshtastic/firmware/issues/3517
powerFSM.add_timed_transition(&stateDARK, &stateLS, getBluetoothWaitMs(), NULL, "Bluetooth timeout");
} else {
// If ESP32, but not using power-saving, check periodically if config has drifted out of stateDark
powerFSM.add_timed_transition(&stateDARK, &stateDARK,
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
NULL, "Screen-on timeout");
powerFSM.add_timed_transition(&stateDARK, &stateDARK, getDarkRecheckMs(), NULL, "Screen-on timeout");
}
#endif // HAS_WIFI || !defined(MESHTASTIC_EXCLUDE_WIFI)

#else // (not) ARCH_ESP32
// If not ESP32, light-sleep not used. Check periodically if config has drifted out of stateDark
powerFSM.add_timed_transition(&stateDARK, &stateDARK,
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs), NULL,
"Screen-on timeout");
powerFSM.add_timed_transition(&stateDARK, &stateDARK, getDarkRecheckMs(), NULL, "Screen-on timeout");
#endif

powerFSM.run_machine(); // run one iteration of the state machine, so we run our on enter tasks for the initial DARK state
Expand Down
2 changes: 2 additions & 0 deletions src/PowerFSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ extern Fsm powerFSM;
extern State stateON, statePOWER, stateSERIAL, stateDARK;

void PowerFSM_setup();
/// Period of the DARK self-transition that re-checks for config drift. Never zero.
uint32_t getDarkRecheckMs();
#endif
5 changes: 5 additions & 0 deletions src/mesh/Default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ uint32_t Default::getConfiguredOrDefaultMs(uint32_t configuredInterval)
return secondsToMsClamped(configuredInterval > 0 ? configuredInterval : default_broadcast_interval_secs);
}

uint32_t Default::getTimeoutMs(uint32_t configuredInterval, uint32_t defaultInterval)
{
return configuredInterval == DECAF_ZERO_TIMEOUT_SECS ? 0 : getConfiguredOrDefaultMs(configuredInterval, defaultInterval);
}

uint32_t Default::getConfiguredOrDefault(uint32_t configured, uint32_t defaultValue)
{
if (configured > 0)
Expand Down
5 changes: 4 additions & 1 deletion src/mesh/Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define FIVE_SECONDS_MS 5 * 1000
#define TEN_SECONDS_MS 10 * 1000
#define MAX_INTERVAL INT32_MAX // FIXME: INT32_MAX to avoid overflow issues with Apple clients but should be UINT32_MAX
// 0x000DECAF means "actual zero"; 0 still means default.
#define DECAF_ZERO_TIMEOUT_SECS 0x000DECAF
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

#define min_default_telemetry_interval_secs IF_ROUTER(ONE_DAY / 2, 30 * 60)
#define default_gps_update_interval IF_ROUTER(ONE_DAY, 2 * 60)
Expand Down Expand Up @@ -77,6 +79,7 @@ class Default
public:
static uint32_t getConfiguredOrDefaultMs(uint32_t configuredInterval);
static uint32_t getConfiguredOrDefaultMs(uint32_t configuredInterval, uint32_t defaultInterval);
static uint32_t getTimeoutMs(uint32_t configuredInterval, uint32_t defaultInterval);
static uint32_t getConfiguredOrDefault(uint32_t configured, uint32_t defaultValue);
// Note: numOnlineNodes uses uint32_t to match the public API and allow flexibility,
// even though internal node counts use uint16_t (max 65535 nodes)
Expand Down Expand Up @@ -128,4 +131,4 @@ class Default
return 1.0 + (nodesOverForty * throttlingFactor); // Each number of online node scales by throttle factor
}
}
};
};
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ A well-structured test suite follows this pattern:
| `test_mqtt` | MQTT integration |
| `test_packet_history` | Packet history tracking |
| `test_position_precision` | Position precision helpers |
| `test_power_fsm` | PowerFSM timeout helpers |
| `test_radio` | Radio interface |
| `test_serial` | Serial communication |
| `test_traffic_management` | Traffic management |
Expand Down
2 changes: 1 addition & 1 deletion test/native-suite-count
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24
25
8 changes: 8 additions & 0 deletions test/test_default/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ void test_ms_result_is_int32_safe()
TEST_ASSERT_GREATER_OR_EQUAL_INT32(0, result);
}

void test_ms_decaf_zero_timeout()
{
TEST_ASSERT_EQUAL_UINT32(0U, Default::getTimeoutMs(DECAF_ZERO_TIMEOUT_SECS, 10));
TEST_ASSERT_EQUAL_UINT32(10000U, Default::getTimeoutMs(0, 10));
TEST_ASSERT_EQUAL_UINT32(2000U, Default::getTimeoutMs(2, 10));
}

void test_scaled_overflow_saturates()
{
// long_fast (SF11/BW250) with a 24h base and heavy congestion overflows
Expand Down Expand Up @@ -200,6 +207,7 @@ void setup()
RUN_TEST(test_ms_uint32_max);
RUN_TEST(test_ms_default_clamps);
RUN_TEST(test_ms_result_is_int32_safe);
RUN_TEST(test_ms_decaf_zero_timeout);
RUN_TEST(test_scaled_overflow_saturates);
exit(UNITY_END());
}
Expand Down
54 changes: 54 additions & 0 deletions test/test_power_fsm/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Unit tests for PowerFSM timeout helpers
#include "Default.h"
#include "PowerFSM.h"
#include "TestUtil.h"
#include <unity.h>

void test_dark_recheck_uses_configured_screen_on()
{
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
config.display.screen_on_secs = 30;

TEST_ASSERT_EQUAL_UINT32(30000U, getDarkRecheckMs());
}

void test_dark_recheck_uses_default_when_unset()
{
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
config.display.screen_on_secs = 0;

TEST_ASSERT_EQUAL_UINT32(600000U, getDarkRecheckMs());
}

void test_dark_recheck_never_zero_and_never_sentinel()
{
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
config.display.screen_on_secs = DECAF_ZERO_TIMEOUT_SECS;

// The sentinel means "no screen-on time", but it is not a usable poll period: the
// re-check must be neither zero nor the sentinel interpreted as a seconds count.
TEST_ASSERT_EQUAL_UINT32(0U, Default::getTimeoutMs(config.display.screen_on_secs, default_screen_on_secs));
TEST_ASSERT_EQUAL_UINT32(600000U, getDarkRecheckMs());
}

void test_dark_recheck_sentinel_uses_router_default()
{
config.device.role = meshtastic_Config_DeviceConfig_Role_ROUTER;
config.display.screen_on_secs = DECAF_ZERO_TIMEOUT_SECS;

TEST_ASSERT_EQUAL_UINT32(1000U, getDarkRecheckMs());
}

void setup()
{
delay(10);
initializeTestEnvironment();
UNITY_BEGIN();
RUN_TEST(test_dark_recheck_uses_configured_screen_on);
RUN_TEST(test_dark_recheck_uses_default_when_unset);
RUN_TEST(test_dark_recheck_never_zero_and_never_sentinel);
RUN_TEST(test_dark_recheck_sentinel_uses_router_default);
exit(UNITY_END());
}

void loop() {}