Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Airplane mode feature (redesigned) #888

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/settings/SettingPineTimeStyle.cpp
displayapp/screens/settings/SettingSetDate.cpp
displayapp/screens/settings/SettingSetTime.cpp
displayapp/screens/settings/SettingAirplaneMode.cpp

## Watch faces
displayapp/icons/bg_clock.c
Expand Down
8 changes: 4 additions & 4 deletions src/components/ble/BleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

using namespace Pinetime::Controllers;

void Ble::Connect() {
isConnected = true;
void Ble::SetConnectState(Ble::ConnectStates newState) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should mix "connection state" (connected and disconnected are implemented, and other future states could be "error", "failed", "refused",...) with an "operating mode" (normal and airplane mode).
I think they are 2 distinct concept that should be kept separate :

  • ConnectionMode : What is the status of the BLE connectivity : are we connected or not ?
  • OperatingMode (or ConnectionAllowed, or something similar?) : Do we accept new BLE connections ?

What do you think?

connectionState = newState;
}

void Ble::Disconnect() {
isConnected = false;
Ble::ConnectStates Ble::GetConnectState() const {
return connectionState;
}

void Ble::StartFirmwareUpdate() {
Expand Down
9 changes: 5 additions & 4 deletions src/components/ble/BleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace Pinetime {
using BleAddress = std::array<uint8_t, 6>;
enum class FirmwareUpdateStates { Idle, Running, Validated, Error };
enum class AddressTypes { Public, Random, RPA_Public, RPA_Random };
enum class ConnectStates { Disconnected, Connected, Airplane };

Ble() = default;
bool IsConnected() const {
return isConnected;
return (connectionState == ConnectStates::Connected);
}
void Connect();
void Disconnect();
void SetConnectState(ConnectStates newState);
ConnectStates GetConnectState() const;

void StartFirmwareUpdate();
void StopFirmwareUpdate();
Expand Down Expand Up @@ -56,7 +57,7 @@ namespace Pinetime {
}

private:
bool isConnected = false;
ConnectStates connectionState = ConnectStates::Disconnected;
bool isFirmwareUpdating = false;
uint32_t firmwareUpdateTotalBytes = 0;
uint32_t firmwareUpdateCurrentBytes = 0;
Expand Down
44 changes: 32 additions & 12 deletions src/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
using namespace Pinetime::Controllers;

NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
Pinetime::Controllers::Ble& bleController,
Ble& bleController,
DateTime& dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager,
Controllers::Battery& batteryController,
NotificationManager& notificationManager,
Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Controllers::HeartRateController& heartRateController,
Controllers::MotionController& motionController,
Controllers::FS& fs)
HeartRateController& heartRateController,
MotionController& motionController,
FS& fs)
: systemTask {systemTask},
bleController {bleController},
dateTimeController {dateTimeController},
Expand Down Expand Up @@ -184,7 +184,9 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
case BLE_GAP_EVENT_ADV_COMPLETE:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
NRF_LOG_INFO("reason=%d; status=%0X", event->adv_complete.reason, event->connect.status);
StartAdvertising();
if (bleController.GetConnectState() == Ble::ConnectStates::Disconnected) {
StartAdvertising();
}
break;

case BLE_GAP_EVENT_CONNECT:
Expand All @@ -197,12 +199,12 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
currentTimeClient.Reset();
alertNotificationClient.Reset();
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
bleController.Disconnect();
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
fastAdvCount = 0;
StartAdvertising();
} else {
connectionHandle = event->connect.conn_handle;
bleController.Connect();
bleController.SetConnectState(Ble::ConnectStates::Connected);
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
// Service discovery is deferred via systemtask
}
Expand All @@ -220,9 +222,11 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
currentTimeClient.Reset();
alertNotificationClient.Reset();
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
bleController.Disconnect();
fastAdvCount = 0;
StartAdvertising();
if (bleController.GetConnectState() == Ble::ConnectStates::Connected) {
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
fastAdvCount = 0;
StartAdvertising();
}
break;

case BLE_GAP_EVENT_CONN_UPDATE:
Expand Down Expand Up @@ -376,6 +380,22 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) {
}
}

void NimbleController::SwitchAirplaneMode(bool enabled) {
if (enabled) {
if (bleController.IsConnected()) {
bleController.SetConnectState(Ble::ConnectStates::Airplane);
ble_gap_terminate(connectionHandle, BLE_ERR_REM_USER_CONN_TERM);
} else {
bleController.SetConnectState(Ble::ConnectStates::Airplane);
ble_gap_adv_stop();
}
} else {
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
fastAdvCount = 0;
StartAdvertising();
}
}

Comment on lines +383 to +398
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have this be two functions without a boolean parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. I see no reason to make the code larger.

void NimbleController::PersistBond(struct ble_gap_conn_desc& desc) {
union ble_store_key key;
union ble_store_value our_sec, peer_sec, peer_cccd_set[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)] = {0};
Expand Down
28 changes: 16 additions & 12 deletions src/components/ble/NimbleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "components/ble/CurrentTimeService.h"
#include "components/ble/DeviceInformationService.h"
#include "components/ble/DfuService.h"
#include "components/ble/FSService.h"
#include "components/ble/HeartRateService.h"
#include "components/ble/ImmediateAlertService.h"
#include "components/ble/MusicService.h"
Expand All @@ -22,7 +23,6 @@
#include "components/ble/MotionService.h"
#include "components/ble/weather/WeatherService.h"
#include "components/fs/FS.h"
#include "components/ble/FSService.h"

namespace Pinetime {
namespace Drivers {
Expand All @@ -42,18 +42,19 @@ namespace Pinetime {

public:
NimbleController(Pinetime::System::SystemTask& systemTask,
Pinetime::Controllers::Ble& bleController,
Ble& bleController,
DateTime& dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager,
Controllers::Battery& batteryController,
NotificationManager& notificationManager,
Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Controllers::HeartRateController& heartRateController,
Controllers::MotionController& motionController,
Pinetime::Controllers::FS& fs);
HeartRateController& heartRateController,
MotionController& motionController,
FS& fs);
void Init();
void StartAdvertising();
int OnGAPEvent(ble_gap_event* event);

/* these are not implemented yet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can safely remove those declarations of the methods are not implemented in NimbleController.cpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't remove them because I wasn't sure if they had any value from a documentation standpoint.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I forgot to remove them a long time ago. They are not needed since these functionalities are implemented is services.

int OnDiscoveryEvent(uint16_t i, const ble_gatt_error* pError, const ble_gatt_svc* pSvc);
int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
Expand All @@ -62,6 +63,7 @@ namespace Pinetime {
const ble_gatt_error* error,
uint16_t characteristicValueHandle,
const ble_gatt_dsc* descriptor);
*/

void StartDiscovery();

Expand All @@ -83,20 +85,22 @@ namespace Pinetime {

void RestartFastAdv() {
fastAdvCount = 0;
}
};

void SwitchAirplaneMode(bool enabled);

private:
void PersistBond(struct ble_gap_conn_desc& desc);
void RestoreBond();

static constexpr const char* deviceName = "InfiniTime";
Pinetime::System::SystemTask& systemTask;
Pinetime::Controllers::Ble& bleController;
Ble& bleController;
DateTime& dateTimeController;
Pinetime::Controllers::NotificationManager& notificationManager;
NotificationManager& notificationManager;
Pinetime::Drivers::SpiNorFlash& spiNorFlash;
Pinetime::Controllers::FS& fs;
Pinetime::Controllers::DfuService dfuService;
FS& fs;
DfuService dfuService;

DeviceInformationService deviceInformationService;
CurrentTimeClient currentTimeClient;
Expand Down
39 changes: 35 additions & 4 deletions src/components/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ namespace Pinetime {
RaiseWrist = 2,
};
enum class Colors : uint8_t {
White, Silver, Gray, Black, Red, Maroon, Yellow, Olive, Lime, Green, Cyan, Teal, Blue, Navy, Magenta, Purple, Orange
White,
Silver,
Gray,
Black,
Red,
Maroon,
Yellow,
Olive,
Lime,
Green,
Cyan,
Teal,
Blue,
Navy,
Magenta,
Purple,
Orange
};
struct PineTimeStyle {
Colors ColorTime = Colors::Teal;
Expand Down Expand Up @@ -146,18 +162,29 @@ namespace Pinetime {
}
settings.brightLevel = level;
};

Controllers::BrightnessController::Levels GetBrightness() const {
return settings.brightLevel;
};

void SetStepsGoal( uint32_t goal ) {
if ( goal != settings.stepsGoal ) {
void SetStepsGoal(uint32_t goal) {
if (goal != settings.stepsGoal) {
settingsChanged = true;
}
settings.stepsGoal = goal;
};

uint32_t GetStepsGoal() const { return settings.stepsGoal; };
uint32_t GetStepsGoal() const {
return settings.stepsGoal;
};

void SetAirplaneMode(bool mode) {
airplaneMode = mode;
};

bool GetAirplaneMode() const {
return airplaneMode;
};

private:
Pinetime::Controllers::FS& fs;
Expand Down Expand Up @@ -185,6 +212,10 @@ namespace Pinetime {

uint8_t appMenu = 0;
uint8_t settingsMenu = 0;
/* airplaneMode is intentionally not saved with the other watch settings and initialized
* to off (false) on every boot because we always want ble to be enabled on startup
*/
bool airplaneMode = false;

void LoadSettingsFromFile();
void SaveSettingsToFile();
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace Pinetime {
SettingPineTimeStyle,
SettingSetDate,
SettingSetTime,
Error,
SettingAirplaneMode,
Error
};
}
}
8 changes: 8 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "displayapp/screens/settings/SettingPineTimeStyle.h"
#include "displayapp/screens/settings/SettingSetDate.h"
#include "displayapp/screens/settings/SettingSetTime.h"
#include "displayapp/screens/settings/SettingAirplaneMode.h"

#include "libs/lv_conf.h"

Expand Down Expand Up @@ -289,6 +290,9 @@ void DisplayApp::Refresh() {
case Messages::BleFirmwareUpdateStarted:
LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down);
break;
case Messages::AirplaneModeToggle:
PushMessageToSystemTask(System::Messages::AirplaneModeToggle);
break;
case Messages::UpdateDateTime:
// Added to remove warning
// What should happen here?
Expand Down Expand Up @@ -420,6 +424,10 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
currentScreen = std::make_unique<Screens::SettingPineTimeStyle>(this, settingsController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break;
case Apps::SettingAirplaneMode:
currentScreen = std::make_unique<Screens::SettingAirplaneMode>(this, settingsController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break;
case Apps::BatteryInfo:
currentScreen = std::make_unique<Screens::BatteryInfo>(this, batteryController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/Messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace Pinetime {
DimScreen,
RestoreBrightness,
ShowPairingKey,
AlarmTriggered
AlarmTriggered,
AirplaneModeToggle
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/fonts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Do not enable font compression and horizontal subpixel hinting
* Load the file `JetBrainsMono-Bold.tff` (use the file in this repo to ensure the version matches) and specify the following range : `0x20-0x7f, 0x410-0x44f`
* Add a 2nd font, load the file `FontAwesome5-Solid+Brands+Regular.woff` and specify the following
range : `0xf293, 0xf294, 0xf244, 0xf240, 0xf242, 0xf243, 0xf241, 0xf54b, 0xf21e, 0xf1e6, 0xf54b, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf069, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf029, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf201, 0xf06e, 0xf015`
range : `0xf293, 0xf294, 0xf244, 0xf240, 0xf242, 0xf243, 0xf241, 0xf54b, 0xf21e, 0xf1e6, 0xf54b, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf069, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf029, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf201, 0xf06e, 0xf015, 0xf072`
* Click on Convert, and download the file `jetbrains_mono_bold_20.c` and copy it in `src/DisplayApp/Fonts`
* Add the font .c file path to src/CMakeLists.txt
* Add an LV_FONT_DECLARE line in src/libs/lv_conf.h
Expand Down
Loading