From 09f5db5a91ff0ba00d1ce756527b710d619b3e09 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 18 Aug 2022 12:32:50 -0500 Subject: [PATCH] Reboot on saved lora or bluetooth settings --- src/commands.h | 1 + src/graphics/Screen.cpp | 23 ++++++++ src/graphics/Screen.h | 8 +++ src/mesh/NodeDB.h | 2 +- src/mesh/PhoneAPI.cpp | 9 +-- src/mesh/PhoneAPI.h | 4 +- src/modules/AdminModule.cpp | 80 +++++++++++++++------------ src/platform/nrf52/NRF52Bluetooth.cpp | 9 ++- src/shutdown.h | 1 + 9 files changed, 90 insertions(+), 47 deletions(-) diff --git a/src/commands.h b/src/commands.h index 13ddc7284..b52ed5db4 100644 --- a/src/commands.h +++ b/src/commands.h @@ -14,4 +14,5 @@ enum class Cmd { STOP_BOOT_SCREEN, PRINT, START_SHUTDOWN_SCREEN, + START_REBOOT_SCREEN, }; \ No newline at end of file diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index beb8a3c97..4298b1578 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -317,6 +317,14 @@ static void drawFrameShutdown(OLEDDisplay *display, OLEDDisplayUiState *state, i display->drawString(64 + x, 26 + y, "Shutting down..."); } +static void drawFrameReboot(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + display->setTextAlignment(TEXT_ALIGN_CENTER); + + display->setFont(FONT_MEDIUM); + display->drawString(64 + x, 26 + y, "Rebooting..."); +} + static void drawFrameFirmware(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { display->setTextAlignment(TEXT_ALIGN_CENTER); @@ -1042,6 +1050,9 @@ int32_t Screen::runOnce() case Cmd::START_SHUTDOWN_SCREEN: handleShutdownScreen(); break; + case Cmd::START_REBOOT_SCREEN: + handleShutdownScreen(); + break; default: DEBUG_MSG("BUG: invalid cmd\n"); } @@ -1234,6 +1245,18 @@ void Screen::handleShutdownScreen() setFastFramerate(); } +void Screen::handleRebootScreen() +{ + DEBUG_MSG("showing reboot screen\n"); + showingNormalScreen = false; + + static FrameCallback rebootFrames[] = {drawFrameReboot}; + + ui.disableAllIndicators(); + ui.setFrames(rebootFrames, 1); + setFastFramerate(); +} + void Screen::handleStartFirmwareUpdateScreen() { DEBUG_MSG("showing firmware screen\n"); diff --git a/src/graphics/Screen.h b/src/graphics/Screen.h index b8d3f6f58..bdd7107d1 100644 --- a/src/graphics/Screen.h +++ b/src/graphics/Screen.h @@ -167,6 +167,13 @@ class Screen : public concurrency::OSThread enqueueCmd(cmd); } + void startRebootScreen() + { + ScreenCmd cmd; + cmd.cmd = Cmd::START_REBOOT_SCREEN; + enqueueCmd(cmd); + } + /// Stops showing the bluetooth PIN screen. void stopBluetoothPinScreen() { enqueueCmd(ScreenCmd{.cmd = Cmd::STOP_BLUETOOTH_PIN_SCREEN}); } @@ -280,6 +287,7 @@ class Screen : public concurrency::OSThread void handlePrint(const char *text); void handleStartFirmwareUpdateScreen(); void handleShutdownScreen(); + void handleRebootScreen(); /// Rebuilds our list of frames (screens) to default ones. void setFrames(); diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 952b684aa..89582c261 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -13,7 +13,7 @@ DeviceState versions used to be defined in the .proto file but really only this #define here. */ -#define DEVICESTATE_CUR_VER 15 +#define DEVICESTATE_CUR_VER 16 #define DEVICESTATE_MIN_VER DEVICESTATE_CUR_VER extern DeviceState devicestate; diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 16a7319bc..9a0066bad 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -116,13 +116,11 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) */ size_t PhoneAPI::getFromRadio(uint8_t *buf) { + DEBUG_MSG("getFromRadio, state=%d\n", state); if (!available()) { // DEBUG_MSG("PhoneAPI::getFromRadio, !available\n"); return 0; } - - DEBUG_MSG("getFromRadio, state=%d\n", state); - // In case we send a FromRadio packet memset(&fromRadioScratch, 0, sizeof(fromRadioScratch)); @@ -278,7 +276,10 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) return 0; } -void PhoneAPI::handleDisconnect() {} +void PhoneAPI::handleDisconnect() +{ + DEBUG_MSG("PhoneAPI disconnect\n"); +} void PhoneAPI::releasePhonePacket() { diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 649217243..178e34c83 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -59,7 +59,7 @@ class PhoneAPI // Call this when the client drops the connection, resets the state to STATE_SEND_NOTHING // Unregisters our observer. A closed connection **can** be reopened by calling init again. virtual void close(); - + /** * Handle a ToRadio protobuf * @return true true if a packet was queued for sending (so that caller can yield) @@ -81,6 +81,8 @@ class PhoneAPI bool isConnected() { return state != STATE_SEND_NOTHING; } + void setInitalState() { state = STATE_SEND_MY_INFO; } + /// emit a debugging log character, FIXME - implement void debugOut(char c) { } diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index acd8d3d6e..29c8518b9 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -170,45 +170,53 @@ void AdminModule::handleSetOwner(const User &o) void AdminModule::handleSetConfig(const Config &c) { + bool requiresReboot = false; switch (c.which_payloadVariant) { - case Config_device_tag: - DEBUG_MSG("Setting config: Device\n"); - config.has_device = true; - config.device = c.payloadVariant.device; - break; - case Config_position_tag: - DEBUG_MSG("Setting config: Position\n"); - config.has_position = true; - config.position = c.payloadVariant.position; - break; - case Config_power_tag: - DEBUG_MSG("Setting config: Power\n"); - config.has_power = true; - config.power = c.payloadVariant.power; - break; - case Config_wifi_tag: - DEBUG_MSG("Setting config: WiFi\n"); - config.has_wifi = true; - config.wifi = c.payloadVariant.wifi; - break; - case Config_display_tag: - DEBUG_MSG("Setting config: Display\n"); - config.has_display = true; - config.display = c.payloadVariant.display; - break; - case Config_lora_tag: - DEBUG_MSG("Setting config: LoRa\n"); - config.has_lora = true; - config.lora = c.payloadVariant.lora; - break; - case Config_bluetooth_tag: - DEBUG_MSG("Setting config: Bluetooth\n"); - config.has_bluetooth = true; - config.bluetooth = c.payloadVariant.bluetooth; - break; + case Config_device_tag: + DEBUG_MSG("Setting config: Device\n"); + config.has_device = true; + config.device = c.payloadVariant.device; + break; + case Config_position_tag: + DEBUG_MSG("Setting config: Position\n"); + config.has_position = true; + config.position = c.payloadVariant.position; + break; + case Config_power_tag: + DEBUG_MSG("Setting config: Power\n"); + config.has_power = true; + config.power = c.payloadVariant.power; + break; + case Config_wifi_tag: + DEBUG_MSG("Setting config: WiFi\n"); + config.has_wifi = true; + config.wifi = c.payloadVariant.wifi; + break; + case Config_display_tag: + DEBUG_MSG("Setting config: Display\n"); + config.has_display = true; + config.display = c.payloadVariant.display; + break; + case Config_lora_tag: + DEBUG_MSG("Setting config: LoRa\n"); + config.has_lora = true; + config.lora = c.payloadVariant.lora; + requiresReboot = true; + break; + case Config_bluetooth_tag: + DEBUG_MSG("Setting config: Bluetooth\n"); + config.has_bluetooth = true; + config.bluetooth = c.payloadVariant.bluetooth; + requiresReboot = true; + break; } - service.reloadConfig(); + bool didSave = service.reloadConfig(); + + // Reboot 5 seconds after a config that requires rebooting is set + if (didSave && requiresReboot) { + rebootAtMsec = millis() + 5 * 1000; + } } void AdminModule::handleSetModuleConfig(const ModuleConfig &c) diff --git a/src/platform/nrf52/NRF52Bluetooth.cpp b/src/platform/nrf52/NRF52Bluetooth.cpp index 598630306..22335aa8b 100644 --- a/src/platform/nrf52/NRF52Bluetooth.cpp +++ b/src/platform/nrf52/NRF52Bluetooth.cpp @@ -57,6 +57,7 @@ void onConnect(uint16_t conn_handle) connection->getPeerName(central_name, sizeof(central_name)); DEBUG_MSG("BLE Connected to %s\n", central_name); + bluetoothPhoneAPI->setInitalState(); } /** @@ -221,7 +222,6 @@ void NRF52Bluetooth::setup() Bluefruit.Advertising.stop(); Bluefruit.Advertising.clearData(); Bluefruit.ScanResponse.clearData(); - config.bluetooth.mode = Config_BluetoothConfig_PairingMode_NoPin; if (config.bluetooth.mode != Config_BluetoothConfig_PairingMode_NoPin) { configuredPasskey = config.bluetooth.mode == Config_BluetoothConfig_PairingMode_FixedPin ? @@ -308,12 +308,11 @@ bool NRF52Bluetooth::onPairingPasskey(uint16_t conn_handle, uint8_t const passke sprintf(specified, "%.3s%.3s", passkey, passkey+3); static char configured[6]; sprintf(configured, "%i", configuredPasskey); - + powerFSM.trigger(EVENT_BLUETOOTH_PAIR); + screen->startBluetoothPinScreen(configuredPasskey); + if (match_request) { - DEBUG_MSG("*** Enter passkey %d on the peer side ***\n", passkey); - powerFSM.trigger(EVENT_BLUETOOTH_PAIR); - screen->startBluetoothPinScreen(configuredPasskey); bool accepted = false; uint32_t start_time = millis(); while(millis() < start_time + 30000) diff --git a/src/shutdown.h b/src/shutdown.h index 306f84d22..43094a120 100644 --- a/src/shutdown.h +++ b/src/shutdown.h @@ -8,6 +8,7 @@ void powerCommandsCheck() { if (rebootAtMsec && millis() > rebootAtMsec) { DEBUG_MSG("Rebooting\n"); + screen->startRebootScreen(); #if defined(ARCH_ESP32) ESP.restart(); #elif defined(ARCH_NRF52)