Harden retry, power, and companion behavior

This commit is contained in:
mikecarper committed 2026-07-20 10:50:55 -07:00
1 parent 67cfc30588
commit 57cc977e58
23 files changed
+938 -115

No files matched your search

+13
View File
@@ -154,6 +154,9 @@ static const uint32_t COMMAND_RADIO_APPLY_TIMEOUT_MS = 5000UL;
#ifndef DEFAULT_AUTOADD_CONFIG
#define DEFAULT_AUTOADD_CONFIG 0
#endif
#ifndef DEFAULT_BUZZER_QUIET
#define DEFAULT_BUZZER_QUIET 0
#endif
#ifndef EMERGENCY_CLIENT_REPEAT_HOLD_MS
#define EMERGENCY_CLIENT_REPEAT_HOLD_MS 120000UL
@@ -728,6 +731,15 @@ bool MyMesh::allowPacketForward(const mesh::Packet* packet) {
return _prefs.client_repeat != 0;
}
bool MyMesh::allowFloodRetry(const mesh::Packet* packet) const {
if (packet == NULL) return false;
// A companion may retry its own advert once, using the core's deliberately
// slow origin-advert delay. Do not add retries while relaying a neighbour's
// advert; the ordinary forwarding and recent-echo guard still apply.
return packet->getPayloadType() != PAYLOAD_TYPE_ADVERT
|| isSelfOriginAdvert(packet);
}
bool MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis) {
if (scope.isNull()) {
return sendFlood(pkt, delay_millis, _prefs.path_hash_mode + 1);
@@ -1166,6 +1178,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
_prefs.multi_acks = DEFAULT_MULTI_ACKS;
_prefs.manual_add_contacts = DEFAULT_MANUAL_ADD_CONTACTS;
_prefs.tx_power_dbm = LORA_TX_POWER;
_prefs.buzzer_quiet = DEFAULT_BUZZER_QUIET ? 1 : 0;
_prefs.gps_enabled = 0; // GPS disabled by default
_prefs.gps_interval = 0; // No automatic GPS updates by default
_prefs.autoadd_config = DEFAULT_AUTOADD_CONFIG;
+1
View File
@@ -149,6 +149,7 @@ protected:
uint8_t getExtraAckTransmitCount() const override;
bool filterRecvFloodPacket(mesh::Packet* packet) override;
bool allowPacketForward(const mesh::Packet* packet) override;
bool allowFloodRetry(const mesh::Packet* packet) const override;
bool sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis);
bool sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0) override;
+92 -3
View File
@@ -9,6 +9,12 @@
#ifndef AUTO_OFF_MILLIS
#define AUTO_OFF_MILLIS 15000 // 15 seconds
#endif
#ifndef USB_MESSAGE_PREVIEW_MILLIS
#define USB_MESSAGE_PREVIEW_MILLIS 15000UL
#endif
#ifndef BLE_PAIRING_DISPLAY_MILLIS
#define BLE_PAIRING_DISPLAY_MILLIS 120000UL
#endif
#define BOOT_SCREEN_MILLIS 3000 // 3 seconds
#ifdef PIN_STATUS_LED
@@ -181,6 +187,8 @@ public:
: _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0),
_shutdown_init(false), sensors_lpp(200) { }
void showFirstPage() { _page = HomePage::FIRST; }
void poll() override {
if (_shutdown_init && !_task->isButtonPressed()) { // must wait for USR button to be released
_task->shutdown();
@@ -629,7 +637,10 @@ switch(t){
void UITask::msgRead(int msgcount) {
_msgcount = msgcount;
if (msgcount == 0) {
gotoHomeScreen();
_deferred_msg_preview = false;
const bool holding_usb_preview = curr == msg_preview && _msg_preview_until != 0
&& static_cast<int32_t>(millis() - _msg_preview_until) < 0;
if (!holding_usb_preview) gotoHomeScreen();
}
}
@@ -637,7 +648,20 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i
_msgcount = msgcount;
((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text);
setCurrScreen(msg_preview);
if (isPairingScreenActive()) {
// Keep the PIN visible, but retain the preview so it can be shown after
// pairing completes or the pairing display window expires.
_deferred_msg_preview = true;
} else {
setCurrScreen(msg_preview);
}
// A connected app drains the offline queue almost immediately, which calls
// msgRead(0). While attached to a computer, retain the actual message screen
// for 15 seconds even though the app has already consumed the message.
_msg_preview_until = _board->isUsbHostConnected()
? millis() + USB_MESSAGE_PREVIEW_MILLIS
: 0;
if (_display != NULL) {
if (!_display->isOn() && shouldWakeDisplayForMessage()) {
@@ -676,6 +700,47 @@ void UITask::setCurrScreen(UIScreen* c) {
_next_refresh = 100;
}
bool UITask::isPairingScreenActive() const {
return _pairing_screen_until != 0
&& !hasConnection()
&& static_cast<int32_t>(millis() - _pairing_screen_until) < 0;
}
void UITask::showPairingPin() {
const unsigned long now = millis();
_pairing_screen_until = now + BLE_PAIRING_DISPLAY_MILLIS;
if (curr == msg_preview && _msgcount > 0) {
_deferred_msg_preview = true;
}
static_cast<HomeScreen*>(home)->showFirstPage();
setCurrScreen(home);
if (_display != NULL) {
if (!_display->isOn()) _display->turnOn();
_auto_off = now + AUTO_OFF_MILLIS;
_next_refresh = 0;
}
}
void UITask::finishPairingScreen(bool timed_out) {
_pairing_screen_until = 0;
if (_deferred_msg_preview && _msgcount > 0) {
_deferred_msg_preview = false;
setCurrScreen(msg_preview);
_auto_off = millis() + AUTO_OFF_MILLIS;
} else {
_deferred_msg_preview = false;
gotoHomeScreen();
if (timed_out && _display != NULL) {
_display->turnOff();
} else {
_auto_off = millis() + AUTO_OFF_MILLIS;
_next_refresh = 0;
}
}
}
/*
hardware-agnostic pre-shutdown activity should be done here
*/
@@ -711,6 +776,17 @@ bool UITask::isButtonPressed() const {
}
void UITask::loop() {
if (_serial->takePairingRequest()) {
showPairingPin();
}
if (_pairing_screen_until != 0) {
const bool timed_out = static_cast<int32_t>(millis() - _pairing_screen_until) >= 0;
if (hasConnection() || timed_out) {
finishPairingScreen(timed_out);
}
}
char c = 0;
#if UI_HAS_JOYSTICK
int ev = user_btn.check();
@@ -784,6 +860,13 @@ void UITask::loop() {
}
#endif
if (isPairingScreenActive()) {
// Pairing has visual priority over navigation and asynchronous screens.
static_cast<HomeScreen*>(home)->showFirstPage();
if (curr != home) setCurrScreen(home);
c = 0;
}
if (c != 0 && curr) {
curr->handleInput(c);
_auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
@@ -798,6 +881,12 @@ void UITask::loop() {
if (curr) curr->poll();
if (curr == msg_preview && _msgcount == 0 && _msg_preview_until != 0
&& static_cast<int32_t>(millis() - _msg_preview_until) >= 0) {
_msg_preview_until = 0;
gotoHomeScreen();
}
if (_display != NULL && _display->isOn()) {
if (millis() >= _next_refresh && curr) {
_display->startFrame();
@@ -827,7 +916,7 @@ void UITask::loop() {
_auto_off = millis() + AUTO_OFF_MILLIS;
}
#endif
if (isDisplayAutoOffDue(_auto_off, AUTO_OFF_MILLIS)) {
if (!isPairingScreenActive() && isDisplayAutoOffDue(_auto_off, AUTO_OFF_MILLIS)) {
_display->turnOff();
}
#endif
+9
View File
@@ -32,6 +32,9 @@ class UITask : public AbstractUITask {
GenericVibration vibration;
#endif
unsigned long _next_refresh, _auto_off;
unsigned long _msg_preview_until;
unsigned long _pairing_screen_until;
bool _deferred_msg_preview;
NodePrefs* _node_prefs;
char _alert[80];
unsigned long _alert_expiry;
@@ -62,11 +65,17 @@ class UITask : public AbstractUITask {
char handleTripleClick(char c);
void setCurrScreen(UIScreen* c);
bool isPairingScreenActive() const;
void showPairingPin();
void finishPairingScreen(bool timed_out);
public:
UITask(mesh::MainBoard* board, BaseSerialInterface* serial) : AbstractUITask(board, serial), _display(NULL), _sensors(NULL) {
next_batt_chck = _next_refresh = 0;
_msg_preview_until = 0;
_pairing_screen_until = 0;
_deferred_msg_preview = false;
ui_started_at = 0;
curr = NULL;
}
+16 -2
View File
@@ -90,15 +90,29 @@ void UITask::begin(DisplayDriver* display, SensorManager* sensors, NodePrefs* no
ui_started_at = millis();
}
bool UITask::shouldPlayMessageTone() const {
#ifdef BLE_PIN_CODE
return !hasConnection();
#else
// A USB or Ethernet companion connection is not a Bluetooth connection and
// should not silence the standalone new-message alert.
return true;
#endif
}
void UITask::notify(UIEventType t) {
#if defined(PIN_BUZZER)
switch(t){
case UIEventType::contactMessage:
// gemini's pick
buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
if (shouldPlayMessageTone()) {
buzzer.play("MsgRcv3:d=4,o=6,b=200:32e,32g,32b,16c7");
}
break;
case UIEventType::channelMessage:
buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
if (shouldPlayMessageTone()) {
buzzer.play("kerplop:d=16,o=6,b=120:32g#,32c#");
}
break;
case UIEventType::ack:
buzzer.play("ack:d=32,o=8,b=120:c");
@@ -50,6 +50,7 @@ class UITask : public AbstractUITask {
void handleButtonTriplePress();
void handleButtonQuadruplePress();
void handleButtonLongPress();
bool shouldPlayMessageTone() const;
public:
+26 -8
View File
@@ -1578,6 +1578,25 @@ uint8_t MyMesh::floodRetryBridgeHeardMask(const mesh::Packet* packet, uint8_t so
return mask;
}
bool MyMesh::floodRetryBridgeEligible(const mesh::Packet* packet) const {
FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, false);
if (state != NULL) {
return (state->heard_mask & state->target_mask) != state->target_mask;
}
uint8_t source_mask = floodRetrySourceMask(packet);
if (source_mask == 0) {
return false;
}
uint8_t target_mask = floodRetryBridgeTargetMask(source_mask);
if (target_mask == 0) {
return false;
}
uint8_t progress_marker = packet->getPathHashCount();
uint8_t heard_mask = floodRetryBridgeHeardMask(packet, source_mask, progress_marker) & target_mask;
return (heard_mask & target_mask) != target_mask;
}
MyMesh::FloodRetryBridgeState* MyMesh::floodRetryBridgeStateFor(const mesh::Packet* packet, bool create) const {
if (packet == NULL) {
return NULL;
@@ -1635,15 +1654,14 @@ bool MyMesh::allowFloodRetry(const mesh::Packet* packet) const {
if (!_prefs.flood_retry_bridge_enabled) {
return true;
}
FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, true);
if (state == NULL) {
return false;
return floodRetryBridgeEligible(packet);
}
bool MyMesh::prepareFloodRetry(const mesh::Packet* packet) const {
if (!_prefs.flood_retry_bridge_enabled) {
return true;
}
if ((state->heard_mask & state->target_mask) == state->target_mask) {
state->active = false;
return false;
}
return true;
return floodRetryBridgeStateFor(packet, true) != NULL;
}
void MyMesh::clearFloodRetryBridgeStateByKey(const uint8_t* retry_key) {
+2
View File
@@ -376,6 +376,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
uint8_t floodRetryBridgeTargetMask(uint8_t source_mask) const;
uint8_t floodRetryBridgeHeardMask(const mesh::Packet* packet, uint8_t source_mask,
uint8_t progress_marker) const;
bool floodRetryBridgeEligible(const mesh::Packet* packet) const;
FloodRetryBridgeState* floodRetryBridgeStateFor(const mesh::Packet* packet, bool create) const;
void clearFloodRetryBridgeStateByKey(const uint8_t* retry_key);
void refreshFloodRetryReachability(const mesh::Packet* packet);
@@ -510,6 +511,7 @@ protected:
void onDirectRetryFailed(const uint8_t* next_hop_hash, uint8_t next_hop_hash_len) override;
void onDirectRetrySucceeded(const uint8_t* next_hop_hash, uint8_t next_hop_hash_len, int8_t snr_x4) override;
bool allowFloodRetry(const mesh::Packet* packet) const override;
bool prepareFloodRetry(const mesh::Packet* packet) const override;
void onFloodRetryEvent(const char* event, const mesh::Packet* packet, uint32_t delay_millis, uint8_t retry_attempt) override;
void onFloodRetrySlotReleased(const uint8_t* retry_key) override;
bool hasFloodRetryTargetPrefix(const mesh::Packet* packet) const override;