Persist Companion Bluetooth on-off preference across reboots

This commit is contained in:
mikecarper committed 2026-10-03 03:09:20 -07:00
1 parent 8519638367
commit 4ca66c2ebb
11 files changed
+528 -21

No files matched your search

+15
View File
@@ -999,6 +999,9 @@ bool DataStore::loadPrefsInt(const char *filename,
File file = openRead(_fs, filename);
if (file) {
CompanionNodePrefs loaded_prefs = _prefs;
// Files written before this tail existed always started Bluetooth at boot.
// Do not inherit a runtime off value when loading one of those images.
loaded_prefs.bluetooth_enabled = 1;
double loaded_lat = node_lat;
double loaded_lon = node_lon;
// The original image ended after ble_pin at byte 84. Later releases only
@@ -1046,6 +1049,13 @@ bool DataStore::loadPrefsInt(const char *filename,
#endif
#if defined(RP2040_PLATFORM) && defined(ENABLE_WIFI_INTERFACE)
328,
#endif
#ifdef TBEAM_1W
239, // Bluetooth enable preference, appended after one-key DM policy
#elif defined(RP2040_PLATFORM) && defined(ENABLE_WIFI_INTERFACE)
329,
#else
232,
#endif
};
const uint32_t prefs_size = file.size();
@@ -1188,10 +1198,13 @@ bool DataStore::loadPrefsInt(const char *filename,
sizeof(loaded_prefs.flood_retry_advert_enabled));
readOptionalField(&loaded_prefs.one_key_dm_enabled,
sizeof(loaded_prefs.one_key_dm_enabled));
readOptionalField(&loaded_prefs.bluetooth_enabled,
sizeof(loaded_prefs.bluetooth_enabled));
// Any bytes left over form only part of a historically appended field.
// Preserve the file and defaults rather than treating that tail as EOF.
success = success && file.available() == 0;
success = success && loaded_prefs.bluetooth_enabled <= 1;
file.close();
if (!success) return false;
_prefs = loaded_prefs;
@@ -1348,6 +1361,8 @@ bool DataStore::savePrefs(const CompanionNodePrefs& _prefs, double node_lat, dou
sizeof(_prefs.flood_retry_advert_enabled)) == sizeof(_prefs.flood_retry_advert_enabled);
success = success && file.write((uint8_t *)&_prefs.one_key_dm_enabled,
sizeof(_prefs.one_key_dm_enabled)) == sizeof(_prefs.one_key_dm_enabled);
success = success && file.write((uint8_t *)&_prefs.bluetooth_enabled,
sizeof(_prefs.bluetooth_enabled)) == sizeof(_prefs.bluetooth_enabled);
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
success = file.commit(success);
+10 -1
View File
@@ -2310,6 +2310,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
_prefs.wifi_enabled = 0;
#endif
memset(_prefs.bluetooth_name, 0, sizeof(_prefs.bluetooth_name));
_prefs.bluetooth_enabled = 1;
_prefs.display_rotation_degrees = 0;
_prefs.cad_enabled = DEFAULT_CAD_ENABLED ? 1 : 0;
_prefs.cad_scan_timeout_ms = 0;
@@ -4137,6 +4138,14 @@ uint32_t MyMesh::getBLEPin() {
return _active_ble_pin;
}
bool MyMesh::setBluetoothEnabledPreference(bool enabled) {
return savePreference(_prefs.bluetooth_enabled, static_cast<uint8_t>(enabled));
}
bool MyMesh::isBluetoothEnabledPreference() const {
return _prefs.bluetooth_enabled != 0;
}
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
static void copyMqttString(char* dest, size_t dest_size, const char* src) {
strncpy(dest, src ? src : "", dest_size - 1);
@@ -9161,7 +9170,7 @@ void MyMesh::handleTerminalCommand(char* command) {
terminalOutput().print(" set bluetooth.name <name|default>\r\n");
#if defined(BLE_PIN_CODE)
terminalOutput().print(" get bluetooth (alias: get ble)\r\n");
terminalOutput().print(" set bluetooth <on|off> [force] (this boot; force only with off)\r\n");
terminalOutput().print(" set bluetooth <on|off> [force] (saved; force only with off)\r\n");
terminalOutput().print(" get bluetooth.mac\r\n");
terminalOutput().print(
" set bluetooth.mac <address|random|random-every-boot|random-after-connect|default>\r\n");
+2
View File
@@ -194,6 +194,8 @@ public:
const char *getNodeName();
CompanionNodePrefs *getNodePrefs();
uint32_t getBLEPin();
bool setBluetoothEnabledPreference(bool enabled);
bool isBluetoothEnabledPreference() const;
#if defined(BLE_PIN_CODE)
bool prepareBluetoothMacForBoot(bool& address_rotated);
bool armBluetoothMacRotationAfterConnection();
+2
View File
@@ -108,6 +108,8 @@ public:
uint8_t flood_retry_advert_enabled = 1;
// Appended persisted field: old preferences images default to consent required.
uint8_t one_key_dm_enabled = 0;
// Appended after all existing fields so older images keep Bluetooth enabled.
uint8_t bluetooth_enabled = 1;
private:
class RadioPrefs : public CommonRadioPrefs {
+51 -13
View File
@@ -2340,7 +2340,9 @@ void halt() {
#endif
companion_bluetooth_initialized = true;
companion_bluetooth_start_failure[0] = 0;
if (interface_manager.isEnabled()) bluetooth_interface.enable();
if (interface_manager.isEnabled() && the_mesh.isBluetoothEnabledPreference()) {
bluetooth_interface.enable();
}
}
static void serviceDeferredCompanionBluetooth() {
@@ -2539,6 +2541,13 @@ static void serviceCompanionBluetoothControl() {
// Give the requesting BLE client its reply, with a bounded wait even when
// its notification queue is stuck. The command itself reports a request.
if (bluetooth_interface.hasPendingIO() && elapsed < 1750) return;
if (!the_mesh.setBluetoothEnabledPreference(false)) {
companion_bluetooth_off_at = 0;
mesh::usbLoggingPort().println(
"Bluetooth off cancelled: preference save failed");
return;
}
interface_manager.setBluetoothAutoEnable(false);
disableCompanionBluetoothForCli();
}
#endif
@@ -2554,12 +2563,6 @@ bool handleCompanionBluetoothCommand(const char* command, char* reply,
return true;
}
#if defined(BLE_PIN_CODE)
if (!companion_bluetooth_initialized) {
snprintf(reply, reply_size,
"Error: Bluetooth unavailable in this boot: %s",
companion_bluetooth_start_failure);
return true;
}
if (action == CompanionBluetoothCommand::Get) {
snprintf(reply, reply_size, "bluetooth %s%s",
interface_manager.isBluetoothEnabled() ? "on" : "off",
@@ -2567,19 +2570,43 @@ bool handleCompanionBluetoothCommand(const char* command, char* reply,
return true;
}
if (action == CompanionBluetoothCommand::On) {
if (!companion_bluetooth_initialized) {
snprintf(reply, reply_size,
"Error: Bluetooth unavailable in this boot: %s",
companion_bluetooth_start_failure);
return true;
}
if (!mesh::wireless::control().allowService(mesh::wireless::Bluetooth)) {
snprintf(reply, reply_size, "Error: 2.4ghz is off or changing; use set 2.4ghz on first");
return true;
}
companion_bluetooth_off_at = 0;
const bool previous_preference = the_mesh.isBluetoothEnabledPreference();
if (!the_mesh.setBluetoothEnabledPreference(true)) {
snprintf(reply, reply_size, "Error: Bluetooth preference save failed; unchanged");
return true;
}
interface_manager.setBluetoothAutoEnable(true);
interface_manager.enableBluetooth();
snprintf(reply, reply_size, "%s", interface_manager.isBluetoothEnabled()
? "OK - Bluetooth on (this boot)" : "Error: Bluetooth enable failed");
if (interface_manager.isBluetoothEnabled()) {
companion_bluetooth_off_at = 0;
snprintf(reply, reply_size, "OK - Bluetooth on (saved)");
} else if (the_mesh.setBluetoothEnabledPreference(previous_preference)) {
interface_manager.setBluetoothAutoEnable(previous_preference);
snprintf(reply, reply_size, "Error: Bluetooth enable failed; preference restored");
} else {
snprintf(reply, reply_size,
"Error: Bluetooth enable failed; rollback save failed (saved on)");
}
return true;
}
if (!interface_manager.isBluetoothEnabled()) {
if (!the_mesh.setBluetoothEnabledPreference(false)) {
snprintf(reply, reply_size, "Error: Bluetooth preference save failed; unchanged");
return true;
}
companion_bluetooth_off_at = 0;
snprintf(reply, reply_size, "OK - Bluetooth already off (this boot)");
interface_manager.setBluetoothAutoEnable(false);
snprintf(reply, reply_size, "OK - Bluetooth already off (saved)");
return true;
}
const bool force = action == CompanionBluetoothCommand::ForceOff;
@@ -2598,13 +2625,18 @@ bool handleCompanionBluetoothCommand(const char* command, char* reply,
return true;
}
if (non_bluetooth_requester) {
if (!the_mesh.setBluetoothEnabledPreference(false)) {
snprintf(reply, reply_size, "Error: Bluetooth preference save failed; unchanged");
return true;
}
interface_manager.setBluetoothAutoEnable(false);
disableCompanionBluetoothForCli();
snprintf(reply, reply_size, "OK - Bluetooth off (this boot)");
snprintf(reply, reply_size, "OK - Bluetooth off (saved)");
} else {
companion_bluetooth_force_off = force;
companion_bluetooth_off_at = millis() + 250;
if (companion_bluetooth_off_at == 0) companion_bluetooth_off_at = 1;
snprintf(reply, reply_size, "OK - Bluetooth off requested (this boot)");
snprintf(reply, reply_size, "OK - Bluetooth off requested (save pending)");
}
#else
(void)source;
@@ -3021,6 +3053,12 @@ void setup() {
// platforms have no separate port, so this is a harmless no-op there.
mesh::beginUsbLoggingPort();
#if defined(BLE_PIN_CODE)
// Keep stack registration available for a later USB `set bluetooth on`,
// without advertising even briefly when startup enables all interfaces.
interface_manager.setBluetoothAutoEnable(the_mesh.isBluetoothEnabledPreference());
#endif
// Lock the saved transport selection before bringing up either wireless stack.
#if defined(COMPANION_EXCLUSIVE_WIFI_BLE)
loadCompanionTransportModeForBoot();