Skip to content

Commit e03414c

Browse files
authored
Setting to disable DFU and FS access (#1891)
* Expose SystemTask dependency controllers Expose NotificationManager and Settings for use by the feature in next commit. This is a memory efficient way for accessing SystemTask dependencies from controllers that have SystemTask injected as a dependency. Looks like each direct dependency injection uses 4 bytes RAM. As InfiniTime is close to running out of RAM (using 16 more bytes causes build to fail with "ld: region RAM overflowed with stack") it might be helpful to use this approach more. * Add setting to disable DFU and FS access
1 parent 9afc23c commit e03414c

File tree

14 files changed

+171
-2
lines changed

14 files changed

+171
-2
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ list(APPEND SOURCE_FILES
419419
displayapp/screens/settings/SettingChimes.cpp
420420
displayapp/screens/settings/SettingShakeThreshold.cpp
421421
displayapp/screens/settings/SettingBluetooth.cpp
422+
displayapp/screens/settings/SettingOTA.cpp
422423

423424
## Watch faces
424425
displayapp/screens/WatchFaceAnalog.cpp

src/components/ble/DfuService.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "components/ble/DfuService.h"
22
#include <cstring>
33
#include "components/ble/BleController.h"
4+
#include "components/ble/NotificationManager.h"
5+
#include "components/settings/Settings.h"
46
#include "drivers/SpiNorFlash.h"
57
#include "systemtask/SystemTask.h"
68
#include <nrf_log.h>
@@ -78,6 +80,18 @@ void DfuService::Init() {
7880
}
7981

8082
int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
83+
#ifndef PINETIME_IS_RECOVERY
84+
if (systemTask.GetSettings().GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Disabled) {
85+
Pinetime::Controllers::NotificationManager::Notification notif;
86+
memcpy(notif.message.data(), denyAlert, denyAlertLength);
87+
notif.size = denyAlertLength;
88+
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
89+
systemTask.GetNotificationManager().Push(std::move(notif));
90+
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
91+
return BLE_ATT_ERR_INSUFFICIENT_AUTHOR;
92+
}
93+
#endif
94+
8195
if (bleController.IsFirmwareUpdating()) {
8296
xTimerStart(timeoutTimer, 0);
8397
}

src/components/ble/DfuService.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Pinetime {
2020

2121
namespace Controllers {
2222
class Ble;
23+
class Settings;
24+
class NotificationManager;
2325

2426
class DfuService {
2527
public:
@@ -87,6 +89,9 @@ namespace Pinetime {
8789
DfuImage dfuImage;
8890
NotificationManager notificationManager;
8991

92+
static constexpr const char denyAlert[] = "InfiniTime\0Firmware update attempted, but disabled in settings.";
93+
static constexpr const uint8_t denyAlertLength = sizeof(denyAlert); // for this to work denyAlert MUST be array
94+
9095
static constexpr uint16_t dfuServiceId {0x1530};
9196
static constexpr uint16_t packetCharacteristicId {0x1532};
9297
static constexpr uint16_t controlPointCharacteristicId {0x1531};

src/components/ble/FSService.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <nrf_log.h>
22
#include "FSService.h"
33
#include "components/ble/BleController.h"
4+
#include "components/ble/NotificationManager.h"
5+
#include "components/settings/Settings.h"
46
#include "systemtask/SystemTask.h"
57

68
using namespace Pinetime::Controllers;
@@ -49,6 +51,18 @@ void FSService::Init() {
4951
}
5052

5153
int FSService::OnFSServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
54+
#ifndef PINETIME_IS_RECOVERY
55+
if (systemTask.GetSettings().GetDfuAndFsMode() == Pinetime::Controllers::Settings::DfuAndFsMode::Disabled) {
56+
Pinetime::Controllers::NotificationManager::Notification notif;
57+
memcpy(notif.message.data(), denyAlert, denyAlertLength);
58+
notif.size = denyAlertLength;
59+
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
60+
systemTask.GetNotificationManager().Push(std::move(notif));
61+
systemTask.PushMessage(Pinetime::System::Messages::OnNewNotification);
62+
return BLE_ATT_ERR_INSUFFICIENT_AUTHOR;
63+
}
64+
#endif
65+
5266
if (attributeHandle == versionCharacteristicHandle) {
5367
NRF_LOG_INFO("FS_S : handle = %d", versionCharacteristicHandle);
5468
int res = os_mbuf_append(context->om, &fsVersion, sizeof(fsVersion));

src/components/ble/FSService.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Pinetime {
1414

1515
namespace Controllers {
1616
class Ble;
17+
class Settings;
18+
class NotificationManager;
1719

1820
class FSService {
1921
public:
@@ -26,6 +28,10 @@ namespace Pinetime {
2628
private:
2729
Pinetime::System::SystemTask& systemTask;
2830
Pinetime::Controllers::FS& fs;
31+
32+
static constexpr const char denyAlert[] = "InfiniTime\0File access attempted, but disabled in settings.";
33+
static constexpr const uint8_t denyAlertLength = sizeof(denyAlert); // for this to work denyAlert MUST be array
34+
2935
static constexpr uint16_t FSServiceId {0xFEBB};
3036
static constexpr uint16_t fsVersionId {0x0100};
3137
static constexpr uint16_t fsTransferId {0x0200};

src/components/settings/Settings.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "components/brightness/BrightnessController.h"
55
#include "components/fs/FS.h"
66
#include "displayapp/apps/Apps.h"
7+
#include <nrf_log.h>
78

89
namespace Pinetime {
910
namespace Controllers {
@@ -37,6 +38,7 @@ namespace Pinetime {
3738
enum class PTSGaugeStyle : uint8_t { Full, Half, Numeric };
3839
enum class PTSWeather : uint8_t { On, Off };
3940
enum class PrideFlag : uint8_t { Gay, Trans, Bi, Lesbian };
41+
enum class DfuAndFsMode : uint8_t { Disabled, Enabled, EnabledTillReboot };
4042

4143
struct PineTimeStyle {
4244
Colors ColorTime = Colors::Teal;
@@ -309,6 +311,29 @@ namespace Pinetime {
309311
return bleRadioEnabled;
310312
};
311313

314+
void SetDfuAndFsMode(DfuAndFsMode mode) {
315+
if (mode == GetDfuAndFsMode()) {
316+
return;
317+
}
318+
if (mode == DfuAndFsMode::Enabled || GetDfuAndFsMode() == DfuAndFsMode::Enabled) {
319+
settingsChanged = true;
320+
}
321+
settings.dfuAndFsEnabledOnBoot = (mode == DfuAndFsMode::Enabled);
322+
dfuAndFsEnabledTillReboot = (mode == DfuAndFsMode::EnabledTillReboot);
323+
};
324+
325+
DfuAndFsMode GetDfuAndFsMode() {
326+
if (dfuAndFsEnabledTillReboot) {
327+
if (settings.dfuAndFsEnabledOnBoot) { // ensure both variables are in consistent state
328+
settingsChanged = true;
329+
settings.dfuAndFsEnabledOnBoot = false;
330+
NRF_LOG_ERROR("Settings: DfuAndFsMode data corrupted");
331+
}
332+
return DfuAndFsMode::EnabledTillReboot;
333+
}
334+
return (settings.dfuAndFsEnabledOnBoot ? DfuAndFsMode::Enabled : DfuAndFsMode::Disabled);
335+
};
336+
312337
private:
313338
Pinetime::Controllers::FS& fs;
314339

@@ -338,6 +363,8 @@ namespace Pinetime {
338363
uint16_t shakeWakeThreshold = 150;
339364

340365
Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;
366+
367+
bool dfuAndFsEnabledOnBoot = false;
341368
};
342369

343370
SettingsData settings;
@@ -350,6 +377,7 @@ namespace Pinetime {
350377
* to off (false) on every boot because we always want ble to be enabled on startup
351378
*/
352379
bool bleRadioEnabled = true;
380+
bool dfuAndFsEnabledTillReboot = false;
353381

354382
void LoadSettingsFromFile();
355383
void SaveSettingsToFile();

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "displayapp/screens/settings/SettingChimes.h"
5151
#include "displayapp/screens/settings/SettingShakeThreshold.h"
5252
#include "displayapp/screens/settings/SettingBluetooth.h"
53+
#include "displayapp/screens/settings/SettingOTA.h"
5354

5455
#include "libs/lv_conf.h"
5556
#include "UserApps.h"
@@ -620,6 +621,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
620621
case Apps::SettingBluetooth:
621622
currentScreen = std::make_unique<Screens::SettingBluetooth>(this, settingsController);
622623
break;
624+
case Apps::SettingOTA:
625+
currentScreen = std::make_unique<Screens::SettingOTA>(this, settingsController);
626+
break;
623627
case Apps::BatteryInfo:
624628
currentScreen = std::make_unique<Screens::BatteryInfo>(batteryController);
625629
break;

src/displayapp/apps/Apps.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace Pinetime {
4343
SettingChimes,
4444
SettingShakeThreshold,
4545
SettingBluetooth,
46+
SettingOTA,
4647
Error
4748
};
4849

src/displayapp/fonts/fonts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
{
99
"file": "FontAwesome5-Solid+Brands+Regular.woff",
10-
"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, 0xf0f3, 0xf522, 0xf743, 0xf1ec, 0xf55a"
10+
"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, 0xf0f3, 0xf522, 0xf743, 0xf1ec, 0xf55a, 0xf3ed"
1111
}
1212
],
1313
"bpp": 1,

src/displayapp/screens/Symbols.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Pinetime {
88
static constexpr const char* batteryHalf = "\xEF\x89\x82";
99
static constexpr const char* heartBeat = "\xEF\x88\x9E";
1010
static constexpr const char* bluetooth = "\xEF\x8A\x94";
11+
static constexpr const char* shieldAlt = "\xEF\x8F\xAD";
1112
static constexpr const char* plug = "\xEF\x87\xA6";
1213
static constexpr const char* shoe = "\xEF\x95\x8B";
1314
static constexpr const char* clock = "\xEF\x80\x97";

0 commit comments

Comments
 (0)