From a2d414a3cbb45b4cd65d780b48a098e726af5631 Mon Sep 17 00:00:00 2001 From: Davis Mosenkovs Date: Sat, 21 Oct 2023 00:40:21 +0300 Subject: [PATCH] Add setting to disable DFU and FS access --- src/CMakeLists.txt | 1 + src/components/ble/DfuService.cpp | 20 ++++++- src/components/ble/DfuService.h | 11 +++- src/components/ble/FSService.cpp | 21 ++++++- src/components/ble/FSService.h | 13 ++++- src/components/ble/NimbleController.cpp | 7 ++- src/components/ble/NimbleController.h | 3 +- src/components/settings/Settings.cpp | 3 + src/components/settings/Settings.h | 16 ++++- src/displayapp/Apps.h | 1 + src/displayapp/DisplayApp.cpp | 4 ++ src/displayapp/fonts/fonts.json | 2 +- src/displayapp/screens/Symbols.h | 1 + .../screens/settings/SettingOTA.cpp | 58 +++++++++++++++++++ src/displayapp/screens/settings/SettingOTA.h | 28 +++++++++ src/displayapp/screens/settings/Settings.h | 4 +- src/systemtask/SystemTask.cpp | 3 +- 17 files changed, 184 insertions(+), 12 deletions(-) create mode 100644 src/displayapp/screens/settings/SettingOTA.cpp create mode 100644 src/displayapp/screens/settings/SettingOTA.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8e8e96863b..4eed643758 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -425,6 +425,7 @@ list(APPEND SOURCE_FILES displayapp/screens/settings/SettingChimes.cpp displayapp/screens/settings/SettingShakeThreshold.cpp displayapp/screens/settings/SettingBluetooth.cpp + displayapp/screens/settings/SettingOTA.cpp ## Watch faces displayapp/screens/WatchFaceAnalog.cpp diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp index 1f06b69ee8..d6bd0f14bf 100644 --- a/src/components/ble/DfuService.cpp +++ b/src/components/ble/DfuService.cpp @@ -1,6 +1,8 @@ #include "components/ble/DfuService.h" #include #include "components/ble/BleController.h" +#include "components/ble/NotificationManager.h" +#include "components/settings/Settings.h" #include "drivers/SpiNorFlash.h" #include "systemtask/SystemTask.h" #include @@ -29,9 +31,13 @@ void TimeoutTimerCallback(TimerHandle_t xTimer) { DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash) + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Controllers::Settings& settingsController, + Pinetime::Controllers::NotificationManager& controllerNotificationManager) : systemTask {systemTask}, bleController {bleController}, + settingsController {settingsController}, + controllerNotificationManager {controllerNotificationManager}, dfuImage {spiNorFlash}, characteristicDefinition {{ .uuid = &packetCharacteristicUuid.u, @@ -78,6 +84,18 @@ void DfuService::Init() { } int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { +#ifndef PINETIME_IS_RECOVERY + if (__builtin_expect(settingsController.GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Disabled, 0)) { + Pinetime::Controllers::NotificationManager::Notification notif; + memcpy(notif.message.data(), denyAlert, denyAlertLength); + notif.size = denyAlertLength; + notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; + controllerNotificationManager.Push(std::move(notif)); + systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification); + return BLE_ATT_ERR_INSUFFICIENT_RES; + } +#endif + if (bleController.IsFirmwareUpdating()) { xTimerStart(timeoutTimer, 0); } diff --git a/src/components/ble/DfuService.h b/src/components/ble/DfuService.h index b56911b905..073d9aa635 100644 --- a/src/components/ble/DfuService.h +++ b/src/components/ble/DfuService.h @@ -20,12 +20,16 @@ namespace Pinetime { namespace Controllers { class Ble; + class Settings; + class NotificationManager; class DfuService { public: DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash); + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Controllers::Settings& settingsController, + Pinetime::Controllers::NotificationManager& controllerNotificationManager); void Init(); int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); void OnTimeout(); @@ -80,9 +84,14 @@ namespace Pinetime { private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::Ble& bleController; + Pinetime::Controllers::Settings& settingsController; + Pinetime::Controllers::NotificationManager& controllerNotificationManager; DfuImage dfuImage; NotificationManager notificationManager; + static constexpr const char* denyAlert = "InfiniTime\0Firmware update attempted, but disabled."; + static constexpr const uint8_t denyAlertLength = 52; + static constexpr uint16_t dfuServiceId {0x1530}; static constexpr uint16_t packetCharacteristicId {0x1532}; static constexpr uint16_t controlPointCharacteristicId {0x1531}; diff --git a/src/components/ble/FSService.cpp b/src/components/ble/FSService.cpp index fda6b392c0..36d9b3c4f8 100644 --- a/src/components/ble/FSService.cpp +++ b/src/components/ble/FSService.cpp @@ -1,6 +1,8 @@ #include #include "FSService.h" #include "components/ble/BleController.h" +#include "components/ble/NotificationManager.h" +#include "components/settings/Settings.h" #include "systemtask/SystemTask.h" using namespace Pinetime::Controllers; @@ -14,9 +16,14 @@ int FSServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gat return fsService->OnFSServiceRequested(conn_handle, attr_handle, ctxt); } -FSService::FSService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::FS& fs) +FSService::FSService(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::FS& fs, + Pinetime::Controllers::Settings& settingsController, + Pinetime::Controllers::NotificationManager& notificationManager) : systemTask {systemTask}, fs {fs}, + settingsController {settingsController}, + notificationManager {notificationManager}, characteristicDefinition {{.uuid = &fsVersionUuid.u, .access_cb = FSServiceCallback, .arg = this, @@ -49,6 +56,18 @@ void FSService::Init() { } int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { +#ifndef PINETIME_IS_RECOVERY + if (__builtin_expect(settingsController.GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Disabled, 0)) { + Pinetime::Controllers::NotificationManager::Notification notif; + memcpy(notif.message.data(), denyAlert, denyAlertLength); + notif.size = denyAlertLength; + notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; + notificationManager.Push(std::move(notif)); + systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification); + return BLE_ATT_ERR_INSUFFICIENT_RES; + } +#endif + if (attributeHandle == versionCharacteristicHandle) { NRF_LOG_INFO("FS_S : handle = %d", versionCharacteristicHandle); int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion)); diff --git a/src/components/ble/FSService.h b/src/components/ble/FSService.h index b2299623a7..c265f706d9 100644 --- a/src/components/ble/FSService.h +++ b/src/components/ble/FSService.h @@ -14,10 +14,15 @@ namespace Pinetime { namespace Controllers { class Ble; + class Settings; + class NotificationManager; class FSService { public: - FSService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::FS& fs); + FSService(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::FS& fs, + Pinetime::Controllers::Settings& settingsController, + Pinetime::Controllers::NotificationManager& notificationManager); void Init(); int OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); @@ -26,6 +31,12 @@ namespace Pinetime { private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::FS& fs; + Pinetime::Controllers::Settings& settingsController; + Pinetime::Controllers::NotificationManager& notificationManager; + + static constexpr const char* denyAlert = "InfiniTime\0File access attempted, but disabled."; + static constexpr const uint8_t denyAlertLength = 48; + static constexpr uint16_t FSServiceId {0xFEBB}; static constexpr uint16_t fsVersionId {0x0100}; static constexpr uint16_t fsTransferId {0x0200}; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 2e7f8003e7..5af01b83af 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -30,13 +30,14 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Drivers::SpiNorFlash& spiNorFlash, HeartRateController& heartRateController, MotionController& motionController, - FS& fs) + FS& fs, + Settings& settingsController) : systemTask {systemTask}, bleController {bleController}, dateTimeController {dateTimeController}, spiNorFlash {spiNorFlash}, fs {fs}, - dfuService {systemTask, bleController, spiNorFlash}, + dfuService {systemTask, bleController, spiNorFlash, settingsController, notificationManager}, currentTimeClient {dateTimeController}, anService {systemTask, notificationManager}, @@ -48,7 +49,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, immediateAlertService {systemTask, notificationManager}, heartRateService {*this, heartRateController}, motionService {*this, motionController}, - fsService {systemTask, fs}, + fsService {systemTask, fs, settingsController, notificationManager}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 8f1dfed7cd..bf31d5f1b6 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -49,7 +49,8 @@ namespace Pinetime { Pinetime::Drivers::SpiNorFlash& spiNorFlash, HeartRateController& heartRateController, MotionController& motionController, - FS& fs); + FS& fs, + Settings& settingsController); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event* event); diff --git a/src/components/settings/Settings.cpp b/src/components/settings/Settings.cpp index 1ae00a2dbc..bf12da6db2 100644 --- a/src/components/settings/Settings.cpp +++ b/src/components/settings/Settings.cpp @@ -34,6 +34,9 @@ void Settings::LoadSettingsFromFile() { if (bufferSettings.version == settingsVersion) { settings = bufferSettings; } + if (settings.dfuAndFsMode == DfuAndFsMode::EnabledTillReboot) { + settings.dfuAndFsMode = DfuAndFsMode::Disabled; + } } void Settings::SaveSettingsToFile() { diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 81cf492332..e867e1ee64 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -35,6 +35,7 @@ namespace Pinetime { }; enum class PTSGaugeStyle : uint8_t { Full, Half, Numeric }; enum class PTSWeather : uint8_t { On, Off }; + enum class DfuAndFsMode : uint8_t { Disabled = 0, Enabled = 1, EnabledTillReboot = 2 }; struct PineTimeStyle { Colors ColorTime = Colors::Teal; @@ -271,10 +272,21 @@ namespace Pinetime { return bleRadioEnabled; }; + void SetDfuAndFsMode(DfuAndFsMode mode) { + if (mode != settings.dfuAndFsMode) { + settingsChanged = true; + } + settings.dfuAndFsMode = mode; + }; + + DfuAndFsMode GetDfuAndFsMode() const { + return settings.dfuAndFsMode; + }; + private: Pinetime::Controllers::FS& fs; - static constexpr uint32_t settingsVersion = 0x0006; + static constexpr uint32_t settingsVersion = 0x0007; struct SettingsData { uint32_t version = settingsVersion; @@ -295,6 +307,8 @@ namespace Pinetime { uint16_t shakeWakeThreshold = 150; Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; + + DfuAndFsMode dfuAndFsMode = DfuAndFsMode::Disabled; }; SettingsData settings; diff --git a/src/displayapp/Apps.h b/src/displayapp/Apps.h index f253bc0387..e58f568e32 100644 --- a/src/displayapp/Apps.h +++ b/src/displayapp/Apps.h @@ -37,6 +37,7 @@ namespace Pinetime { SettingChimes, SettingShakeThreshold, SettingBluetooth, + SettingOTA, Error }; } diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index cd941f16cc..4186873823 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -48,6 +48,7 @@ #include "displayapp/screens/settings/SettingChimes.h" #include "displayapp/screens/settings/SettingShakeThreshold.h" #include "displayapp/screens/settings/SettingBluetooth.h" +#include "displayapp/screens/settings/SettingOTA.h" #include "libs/lv_conf.h" @@ -500,6 +501,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio case Apps::SettingBluetooth: currentScreen = std::make_unique(this, settingsController); break; + case Apps::SettingOTA: + currentScreen = std::make_unique(this, settingsController); + break; case Apps::BatteryInfo: currentScreen = std::make_unique(batteryController); break; diff --git a/src/displayapp/fonts/fonts.json b/src/displayapp/fonts/fonts.json index bcfc365f03..48611060ef 100644 --- a/src/displayapp/fonts/fonts.json +++ b/src/displayapp/fonts/fonts.json @@ -7,7 +7,7 @@ }, { "file": "FontAwesome5-Solid+Brands+Regular.woff", - "range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c" + "range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf3ed" } ], "bpp": 1, diff --git a/src/displayapp/screens/Symbols.h b/src/displayapp/screens/Symbols.h index 7154ff44c1..3c1122288c 100644 --- a/src/displayapp/screens/Symbols.h +++ b/src/displayapp/screens/Symbols.h @@ -8,6 +8,7 @@ namespace Pinetime { static constexpr const char* batteryHalf = "\xEF\x89\x82"; static constexpr const char* heartBeat = "\xEF\x88\x9E"; static constexpr const char* bluetooth = "\xEF\x8A\x94"; + static constexpr const char* shieldAlt = "\xEF\x8F\xAD"; static constexpr const char* plug = "\xEF\x87\xA6"; static constexpr const char* shoe = "\xEF\x95\x8B"; static constexpr const char* clock = "\xEF\x80\x97"; diff --git a/src/displayapp/screens/settings/SettingOTA.cpp b/src/displayapp/screens/settings/SettingOTA.cpp new file mode 100644 index 0000000000..ad123dec51 --- /dev/null +++ b/src/displayapp/screens/settings/SettingOTA.cpp @@ -0,0 +1,58 @@ +#include "displayapp/screens/settings/SettingOTA.h" +#include +#include "displayapp/DisplayApp.h" +#include "displayapp/Messages.h" +#include "displayapp/screens/Styles.h" +#include "displayapp/screens/Screen.h" +#include "displayapp/screens/Symbols.h" + +using namespace Pinetime::Applications::Screens; + +namespace { + struct Option { + const char* name; + Pinetime::Controllers::Settings::DfuAndFsMode mode; + }; + + constexpr std::array options = {{ + {"Enabled", Pinetime::Controllers::Settings::DfuAndFsMode::Enabled}, + {"Disabled", Pinetime::Controllers::Settings::DfuAndFsMode::Disabled}, + {"Till reboot", Pinetime::Controllers::Settings::DfuAndFsMode::EnabledTillReboot}, + }}; + + std::array CreateOptionArray() { + std::array optionArray; + for (size_t i = 0; i < CheckboxList::MaxItems; i++) { + if (i >= options.size()) { + optionArray[i].name = ""; + optionArray[i].enabled = false; + } else { + optionArray[i].name = options[i].name; + optionArray[i].enabled = true; + } + } + return optionArray; + }; +} + +SettingOTA::SettingOTA(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController) + : app {app}, + settingsController {settingsController}, + checkboxList( + 0, + 1, + "Firmware & files", + Symbols::shieldAlt, + settingsController.GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Enabled ? 0 + : settingsController.GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::EnabledTillReboot ? 2 + : 1, + [&settings = settingsController](uint32_t index) { + settings.SetDfuAndFsMode(options[index].mode); + }, + CreateOptionArray()) { +} + +SettingOTA::~SettingOTA() { + lv_obj_clean(lv_scr_act()); + settingsController.SaveSettings(); +} diff --git a/src/displayapp/screens/settings/SettingOTA.h b/src/displayapp/screens/settings/SettingOTA.h new file mode 100644 index 0000000000..13c9a33dd1 --- /dev/null +++ b/src/displayapp/screens/settings/SettingOTA.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +#include "components/settings/Settings.h" +#include "displayapp/screens/Screen.h" +#include "displayapp/screens/CheckboxList.h" + +namespace Pinetime { + + namespace Applications { + namespace Screens { + + class SettingOTA : public Screen { + public: + SettingOTA(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); + ~SettingOTA() override; + + private: + DisplayApp* app; + Pinetime::Controllers::Settings& settingsController; + CheckboxList checkboxList; + }; + } + } +} diff --git a/src/displayapp/screens/settings/Settings.h b/src/displayapp/screens/settings/Settings.h index 3f8097538e..dc92271844 100644 --- a/src/displayapp/screens/settings/Settings.h +++ b/src/displayapp/screens/settings/Settings.h @@ -29,7 +29,7 @@ namespace Pinetime { static constexpr int entriesPerScreen = 4; // Increment this when more space is needed - static constexpr int nScreens = 3; + static constexpr int nScreens = 4; static constexpr std::array entries {{ {Symbols::sun, "Display", Apps::SettingDisplay}, @@ -44,7 +44,9 @@ namespace Pinetime { {Symbols::tachometer, "Shake Calib.", Apps::SettingShakeThreshold}, {Symbols::check, "Firmware", Apps::FirmwareValidation}, + {Symbols::shieldAlt, "Over-the-air", Apps::SettingOTA}, {Symbols::bluetooth, "Bluetooth", Apps::SettingBluetooth}, + {Symbols::list, "About", Apps::SysInfo}, // {Symbols::none, "None", Apps::None}, diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index e59a0ff7a4..d719710557 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -80,7 +80,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, spiNorFlash, heartRateController, motionController, - fs) { + fs, + settingsController) { } void SystemTask::Start() {