From 40d45d923b033363ff1304b47eac238dd4495a57 Mon Sep 17 00:00:00 2001 From: Avamander Date: Sun, 18 Apr 2021 20:28:14 +0300 Subject: Reformatted all the files according to clang-format style --- src/components/battery/BatteryController.cpp | 78 +++---- src/components/battery/BatteryController.h | 89 +++---- src/components/ble/AlertNotificationClient.cpp | 91 ++++---- src/components/ble/AlertNotificationClient.h | 100 ++++---- src/components/ble/AlertNotificationService.cpp | 75 +++--- src/components/ble/AlertNotificationService.h | 84 +++---- src/components/ble/BatteryInformationService.cpp | 52 ++--- src/components/ble/BatteryInformationService.h | 34 ++- src/components/ble/BleClient.h | 6 +- src/components/ble/BleController.cpp | 2 - src/components/ble/BleController.h | 74 +++--- src/components/ble/CurrentTimeClient.cpp | 48 ++-- src/components/ble/CurrentTimeClient.h | 83 ++++--- src/components/ble/CurrentTimeService.cpp | 61 ++--- src/components/ble/CurrentTimeService.h | 65 +++--- src/components/ble/DeviceInformationService.cpp | 110 ++++----- src/components/ble/DeviceInformationService.h | 79 +++---- src/components/ble/DfuService.cpp | 228 +++++++++--------- src/components/ble/DfuService.h | 257 ++++++++++----------- src/components/ble/HeartRateService.cpp | 51 ++-- src/components/ble/HeartRateService.h | 37 ++- src/components/ble/ImmediateAlertService.cpp | 67 +++--- src/components/ble/ImmediateAlertService.h | 55 ++--- src/components/ble/MusicService.cpp | 163 ++++++------- src/components/ble/MusicService.h | 134 ++++------- src/components/ble/NavigationService.cpp | 82 +++---- src/components/ble/NavigationService.h | 44 ++-- src/components/ble/NimbleController.cpp | 107 ++++----- src/components/ble/NimbleController.h | 119 +++++----- src/components/ble/NotificationManager.cpp | 58 +++-- src/components/ble/NotificationManager.h | 64 +++-- src/components/ble/ServiceDiscovery.cpp | 7 +- src/components/ble/ServiceDiscovery.h | 17 +- src/components/brightness/BrightnessController.cpp | 82 ++++--- src/components/brightness/BrightnessController.h | 6 +- src/components/datetime/DateTimeController.cpp | 175 ++++---------- src/components/datetime/DateTimeController.h | 123 ++++++---- .../firmwarevalidator/FirmwareValidator.cpp | 4 +- .../firmwarevalidator/FirmwareValidator.h | 15 +- src/components/gfx/Gfx.cpp | 55 +++-- src/components/gfx/Gfx.h | 85 ++++--- src/components/heartrate/Biquad.cpp | 3 +- src/components/heartrate/Biquad.h | 6 +- src/components/heartrate/HeartRateController.cpp | 15 +- src/components/heartrate/HeartRateController.h | 16 +- src/components/heartrate/Ppg.cpp | 40 ++-- src/components/heartrate/Ppg.h | 5 +- src/components/heartrate/Ptagc.cpp | 7 +- src/components/heartrate/Ptagc.h | 5 +- src/components/motion/MotionController.h | 24 +- src/components/motor/MotorController.cpp | 26 ++- src/components/motor/MotorController.h | 14 +- src/components/rle/RleDecoder.cpp | 11 +- src/components/rle/RleDecoder.h | 4 +- src/components/settings/Settings.cpp | 127 +++++----- src/components/settings/Settings.h | 207 +++++++++-------- 56 files changed, 1713 insertions(+), 1963 deletions(-) (limited to 'src/components') diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 99afa55c..b39efefb 100644 --- a/src/components/battery/BatteryController.cpp +++ b/src/components/battery/BatteryController.cpp @@ -7,33 +7,33 @@ using namespace Pinetime::Controllers; -Battery *Battery::instance = nullptr; +Battery* Battery::instance = nullptr; Battery::Battery() { instance = this; } void Battery::Init() { - nrf_gpio_cfg_input(chargingPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); - nrf_gpio_cfg_input(powerPresentPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); + nrf_gpio_cfg_input(chargingPin, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup); + nrf_gpio_cfg_input(powerPresentPin, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup); } void Battery::Update() { isCharging = !nrf_gpio_pin_read(chargingPin); isPowerPresent = !nrf_gpio_pin_read(powerPresentPin); - - if ( isReading ) return; + + if (isReading) + return; // Non blocking read samples = 0; isReading = true; - SaadcInit(); + SaadcInit(); - nrfx_saadc_sample(); - + nrfx_saadc_sample(); } -void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) { +void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) { instance->SaadcEventHandler(event); } @@ -41,48 +41,44 @@ void Battery::SaadcInit() { nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG; APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic)); - nrf_saadc_channel_config_t adcChannelConfig = { - .resistor_p = NRF_SAADC_RESISTOR_DISABLED, - .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_5, - .reference = NRF_SAADC_REFERENCE_INTERNAL, - .acq_time = NRF_SAADC_ACQTIME_3US, - .mode = NRF_SAADC_MODE_SINGLE_ENDED, - .burst = NRF_SAADC_BURST_ENABLED, - .pin_p = batteryVoltageAdcInput, - .pin_n = NRF_SAADC_INPUT_DISABLED - }; + nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED, + .resistor_n = NRF_SAADC_RESISTOR_DISABLED, + .gain = NRF_SAADC_GAIN1_5, + .reference = NRF_SAADC_REFERENCE_INTERNAL, + .acq_time = NRF_SAADC_ACQTIME_3US, + .mode = NRF_SAADC_MODE_SINGLE_ENDED, + .burst = NRF_SAADC_BURST_ENABLED, + .pin_p = batteryVoltageAdcInput, + .pin_n = NRF_SAADC_INPUT_DISABLED}; APP_ERROR_CHECK(nrfx_saadc_channel_init(0, &adcChannelConfig)); APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); - } -void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * p_event) { +void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { + + const float battery_max = 4.18; // maximum voltage of battery ( max charging voltage is 4.21 ) + const float battery_min = 3.20; // minimum voltage of battery before shutdown ( depends on the battery ) - const float battery_max = 4.18; // maximum voltage of battery ( max charging voltage is 4.21 ) - const float battery_min = 3.20; // minimum voltage of battery before shutdown ( depends on the battery ) + if (p_event->type == NRFX_SAADC_EVT_DONE) { - if (p_event->type == NRFX_SAADC_EVT_DONE) { - - APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); + APP_ERROR_CHECK(nrfx_saadc_buffer_convert(&saadc_value, 1)); - voltage = (static_cast(p_event->data.done.p_buffer[0]) * 2.04f) / (1024 / 3.0f); - voltage = roundf(voltage * 100) / 100; + voltage = (static_cast(p_event->data.done.p_buffer[0]) * 2.04f) / (1024 / 3.0f); + voltage = roundf(voltage * 100) / 100; - percentRemaining = static_cast(((voltage - battery_min) / (battery_max - battery_min)) * 100); + percentRemaining = static_cast(((voltage - battery_min) / (battery_max - battery_min)) * 100); - percentRemaining = std::max(percentRemaining, 0); - percentRemaining = std::min(percentRemaining, 100); + percentRemaining = std::max(percentRemaining, 0); + percentRemaining = std::min(percentRemaining, 100); - percentRemainingBuffer.insert(percentRemaining); + percentRemainingBuffer.insert(percentRemaining); - samples++; - if ( samples > percentRemainingSamples ) { - nrfx_saadc_uninit(); - isReading = false; - } else { - nrfx_saadc_sample(); - } + samples++; + if (samples > percentRemainingSamples) { + nrfx_saadc_uninit(); + isReading = false; + } else { + nrfx_saadc_sample(); } } - +} diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 47d7a6d1..e5c734d2 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -7,18 +7,18 @@ namespace Pinetime { namespace Controllers { - /** A simple circular buffer that can be used to average - out the sensor values. The total capacity of the CircBuffer + /** A simple circular buffer that can be used to average + out the sensor values. The total capacity of the CircBuffer is given as the template parameter N. - */ - template - class CircBuffer { - public: - CircBuffer() : arr{}, sz{}, cap{N}, head{} {} + */ + template class CircBuffer { + public: + CircBuffer() : arr {}, sz {}, cap {N}, head {} { + } /** - insert member function overwrites the next data to the current + insert member function overwrites the next data to the current HEAD and moves the HEAD to the newly inserted value. - */ + */ void insert(const int num) { head %= cap; arr[head++] = num; @@ -32,51 +32,58 @@ namespace Pinetime { return (sum / sz); } - private: + private: std::array arr; /**< internal array used to store the values*/ - uint8_t sz; /**< The current size of the array.*/ - uint8_t cap; /**< Total capacity of the CircBuffer.*/ - uint8_t head; /**< The current head of the CircBuffer*/ + uint8_t sz; /**< The current size of the array.*/ + uint8_t cap; /**< Total capacity of the CircBuffer.*/ + uint8_t head; /**< The current head of the CircBuffer*/ }; class Battery { - public: + public: + Battery(); + + void Init(); + void Update(); - Battery(); + int PercentRemaining() const { + return percentRemainingBuffer.GetAverage(); + } - void Init(); - void Update(); - - int PercentRemaining() const { return percentRemainingBuffer.GetAverage(); } + float Voltage() const { + return voltage; + } + + bool IsCharging() const { + return isCharging; + } + bool IsPowerPresent() const { + return isPowerPresent; + } - float Voltage() const { return voltage; } + private: + static Battery* instance; + nrf_saadc_value_t saadc_value; - bool IsCharging() const { return isCharging; } - bool IsPowerPresent() const { return isPowerPresent; } + static constexpr uint8_t percentRemainingSamples = 5; + CircBuffer percentRemainingBuffer {}; - private: - static Battery *instance; - nrf_saadc_value_t saadc_value; - - static constexpr uint8_t percentRemainingSamples = 5; - CircBuffer percentRemainingBuffer {}; + static constexpr uint32_t chargingPin = 12; + static constexpr uint32_t powerPresentPin = 19; + static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; + float voltage = 0.0f; + int percentRemaining = -1; - static constexpr uint32_t chargingPin = 12; - static constexpr uint32_t powerPresentPin = 19; - static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; - float voltage = 0.0f; - int percentRemaining = -1; + bool isCharging = false; + bool isPowerPresent = false; - bool isCharging = false; - bool isPowerPresent = false; - - void SaadcInit(); + void SaadcInit(); - void SaadcEventHandler(nrfx_saadc_evt_t const * p_event); - static void adcCallbackStatic(nrfx_saadc_evt_t const *event); + void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); + static void adcCallbackStatic(nrfx_saadc_evt_t const* event); - bool isReading = false; - uint8_t samples = 0; + bool isReading = false; + uint8_t samples = 0; }; } } \ No newline at end of file diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp index 9efede39..6043a129 100644 --- a/src/components/ble/AlertNotificationClient.cpp +++ b/src/components/ble/AlertNotificationClient.cpp @@ -12,50 +12,42 @@ constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid; constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid; namespace { - int - OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, - void *arg) { - auto client = static_cast(arg); + int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) { + auto client = static_cast(arg); return client->OnDiscoveryEvent(conn_handle, error, service); } - int OnAlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); + int OnAlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle, + const struct ble_gatt_error* error, + const struct ble_gatt_chr* chr, + void* arg) { + auto client = static_cast(arg); return client->OnCharacteristicsDiscoveryEvent(conn_handle, error, chr); } - int OnAlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg) { - auto client = static_cast(arg); + int OnAlertNotificationDescriptorDiscoveryEventCallback( + uint16_t conn_handle, const struct ble_gatt_error* error, uint16_t chr_val_handle, const struct ble_gatt_dsc* dsc, void* arg) { + auto client = static_cast(arg); return client->OnDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); } - int NewAlertSubcribeCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg) { - auto client = static_cast(arg); + int NewAlertSubcribeCallback(uint16_t conn_handle, const struct ble_gatt_error* error, struct ble_gatt_attr* attr, void* arg) { + auto client = static_cast(arg); return client->OnNewAlertSubcribe(conn_handle, error, attr); } } -AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager ¬ificationManager) : - systemTask{systemTask}, notificationManager{notificationManager} { +AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NotificationManager& notificationManager) + : systemTask {systemTask}, notificationManager {notificationManager} { } -bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_svc *service) { +bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) { if (service == nullptr && error->status == BLE_HS_EDONE) { if (isDiscovered) { NRF_LOG_INFO("ANS Discovery found, starting characteristics discovery"); - ble_gattc_disc_all_chrs(connectionHandle, ansStartHandle, ansEndHandle, - OnAlertNotificationCharacteristicDiscoveredCallback, this); + ble_gattc_disc_all_chrs(connectionHandle, ansStartHandle, ansEndHandle, OnAlertNotificationCharacteristicDiscoveredCallback, this); } else { NRF_LOG_INFO("ANS not found"); onServiceDiscovered(connectionHandle); @@ -63,7 +55,7 @@ bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const return true; } - if (service != nullptr && ble_uuid_cmp(((ble_uuid_t *) &ansServiceUuid), &service->uuid.u) == 0) { + if (service != nullptr && ble_uuid_cmp(((ble_uuid_t*) &ansServiceUuid), &service->uuid.u) == 0) { NRF_LOG_INFO("ANS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle); ansStartHandle = service->start_handle; ansEndHandle = service->end_handle; @@ -72,8 +64,9 @@ bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const return false; } -int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic) { +int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, + const ble_gatt_error* error, + const ble_gatt_chr* characteristic) { if (error->status != 0 && error->status != BLE_HS_EDONE) { NRF_LOG_INFO("ANS Characteristic discovery ERROR"); onServiceDiscovered(connectionHandle); @@ -83,41 +76,34 @@ int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connection if (characteristic == nullptr && error->status == BLE_HS_EDONE) { NRF_LOG_INFO("ANS Characteristic discovery complete"); if (isCharacteristicDiscovered) { - ble_gattc_disc_all_dscs(connectionHandle, - newAlertHandle, ansEndHandle, - OnAlertNotificationDescriptorDiscoveryEventCallback, this); + ble_gattc_disc_all_dscs(connectionHandle, newAlertHandle, ansEndHandle, OnAlertNotificationDescriptorDiscoveryEventCallback, this); } else onServiceDiscovered(connectionHandle); } else { - if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) &supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) { + if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid"); supportedNewAlertCategoryHandle = characteristic->val_handle; - } else if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) &supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) { + } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid"); supportedUnreadAlertCategoryHandle = characteristic->val_handle; - } else if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) &newAlertUuid), &characteristic->uuid.u) == 0) { + } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid"); newAlertHandle = characteristic->val_handle; newAlertDefHandle = characteristic->def_handle; isCharacteristicDiscovered = true; - } else if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) &unreadAlertStatusUuid), &characteristic->uuid.u) == 0) { + } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &unreadAlertStatusUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid"); unreadAlertStatusHandle = characteristic->val_handle; - } else if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) &controlPointUuid), &characteristic->uuid.u) == 0) { + } else if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) &controlPointUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid"); controlPointHandle = characteristic->val_handle; - } else NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle); + } else + NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle); } return 0; } -int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, - ble_gatt_attr *attribute) { +int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute) { if (error->status == 0) { NRF_LOG_INFO("ANS New alert subscribe OK"); } else { @@ -128,12 +114,12 @@ int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const return 0; } -int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, +int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, + const ble_gatt_error* error, uint16_t characteristicValueHandle, - const ble_gatt_dsc *descriptor) { + const ble_gatt_dsc* descriptor) { if (error->status == 0) { - if (characteristicValueHandle == newAlertHandle && - ble_uuid_cmp(((ble_uuid_t *) &newAlertUuid), &descriptor->uuid.u)) { + if (characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*) &newAlertUuid), &descriptor->uuid.u)) { if (newAlertDescriptorHandle == 0) { NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle); newAlertDescriptorHandle = descriptor->handle; @@ -151,16 +137,17 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect return 0; } -void AlertNotificationClient::OnNotification(ble_gap_event *event) { +void AlertNotificationClient::OnNotification(ble_gap_event* event) { if (event->notify_rx.attr_handle == newAlertHandle) { constexpr size_t stringTerminatorSize = 1; // end of string '\0' constexpr size_t headerSize = 3; - const auto maxMessageSize{NotificationManager::MaximumMessageSize()}; - const auto maxBufferSize{maxMessageSize + headerSize}; + const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; + const auto maxBufferSize {maxMessageSize + headerSize}; // Ignore notifications with empty message const auto packetLen = OS_MBUF_PKTLEN(event->notify_rx.om); - if(packetLen <= headerSize) return; + if (packetLen <= headerSize) + return; size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize)); diff --git a/src/components/ble/AlertNotificationClient.h b/src/components/ble/AlertNotificationClient.h index d49205e3..469bd4af 100644 --- a/src/components/ble/AlertNotificationClient.h +++ b/src/components/ble/AlertNotificationClient.h @@ -19,68 +19,52 @@ namespace Pinetime { class NotificationManager; class AlertNotificationClient : public BleClient { - public: - explicit AlertNotificationClient(Pinetime::System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager ¬ificationManager); + public: + explicit AlertNotificationClient(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NotificationManager& notificationManager); - bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); - int OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic); - int OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); - int OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, - uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); - void OnNotification(ble_gap_event *event); - void Reset(); - void Discover(uint16_t connectionHandle, std::function lambda) override; + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service); + int OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic); + int OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute); + int OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, + const ble_gatt_error* error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc* descriptor); + void OnNotification(ble_gap_event* event); + void Reset(); + void Discover(uint16_t connectionHandle, std::function lambda) override; - private: - static constexpr uint16_t ansServiceId{0x1811}; - static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; - static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; - static constexpr uint16_t newAlertId = 0x2a46; - static constexpr uint16_t unreadAlertStatusId = 0x2a45; - static constexpr uint16_t controlPointId = 0x2a44; + private: + static constexpr uint16_t ansServiceId {0x1811}; + static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; + static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; + static constexpr uint16_t newAlertId = 0x2a46; + static constexpr uint16_t unreadAlertStatusId = 0x2a45; + static constexpr uint16_t controlPointId = 0x2a44; - static constexpr ble_uuid16_t ansServiceUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = ansServiceId - }; - static constexpr ble_uuid16_t supportedNewAlertCategoryUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = supportedNewAlertCategoryId - }; - static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = supportedUnreadAlertCategoryId - }; - static constexpr ble_uuid16_t newAlertUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = newAlertId - }; - static constexpr ble_uuid16_t unreadAlertStatusUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = unreadAlertStatusId - }; - static constexpr ble_uuid16_t controlPointUuid{ - .u {.type = BLE_UUID_TYPE_16}, - .value = controlPointId - }; + static constexpr ble_uuid16_t ansServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ansServiceId}; + static constexpr ble_uuid16_t supportedNewAlertCategoryUuid {.u {.type = BLE_UUID_TYPE_16}, .value = supportedNewAlertCategoryId}; + static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid {.u {.type = BLE_UUID_TYPE_16}, + .value = supportedUnreadAlertCategoryId}; + static constexpr ble_uuid16_t newAlertUuid {.u {.type = BLE_UUID_TYPE_16}, .value = newAlertId}; + static constexpr ble_uuid16_t unreadAlertStatusUuid {.u {.type = BLE_UUID_TYPE_16}, .value = unreadAlertStatusId}; + static constexpr ble_uuid16_t controlPointUuid {.u {.type = BLE_UUID_TYPE_16}, .value = controlPointId}; - uint16_t ansStartHandle = 0; - uint16_t ansEndHandle = 0; - uint16_t supportedNewAlertCategoryHandle = 0; - uint16_t supportedUnreadAlertCategoryHandle = 0; - uint16_t newAlertHandle = 0; - uint16_t newAlertDescriptorHandle = 0; - uint16_t newAlertDefHandle = 0; - uint16_t unreadAlertStatusHandle = 0; - uint16_t controlPointHandle = 0; - bool isDiscovered = false; - Pinetime::System::SystemTask &systemTask; - Pinetime::Controllers::NotificationManager ¬ificationManager; - std::function onServiceDiscovered; - bool isCharacteristicDiscovered = false; - bool isDescriptorFound = false; + uint16_t ansStartHandle = 0; + uint16_t ansEndHandle = 0; + uint16_t supportedNewAlertCategoryHandle = 0; + uint16_t supportedUnreadAlertCategoryHandle = 0; + uint16_t newAlertHandle = 0; + uint16_t newAlertDescriptorHandle = 0; + uint16_t newAlertDefHandle = 0; + uint16_t unreadAlertStatusHandle = 0; + uint16_t controlPointHandle = 0; + bool isDiscovered = false; + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::NotificationManager& notificationManager; + std::function onServiceDiscovered; + bool isCharacteristicDiscovered = false; + bool isDescriptorFound = false; }; } } diff --git a/src/components/ble/AlertNotificationService.cpp b/src/components/ble/AlertNotificationService.cpp index d91e2090..e9f5941e 100644 --- a/src/components/ble/AlertNotificationService.cpp +++ b/src/components/ble/AlertNotificationService.cpp @@ -11,8 +11,7 @@ constexpr ble_uuid16_t AlertNotificationService::ansUuid; constexpr ble_uuid16_t AlertNotificationService::ansCharUuid; constexpr ble_uuid128_t AlertNotificationService::notificationEventUuid; - -int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { +int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto anService = static_cast(arg); return anService->OnAlert(conn_handle, attr_handle, ctxt); } @@ -26,62 +25,52 @@ void AlertNotificationService::Init() { ASSERT(res == 0); } -AlertNotificationService::AlertNotificationService ( System::SystemTask& systemTask, NotificationManager& notificationManager ) - : characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &ansCharUuid, - .access_cb = AlertNotificationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE - }, - { - .uuid = (ble_uuid_t *) ¬ificationEventUuid, - .access_cb = AlertNotificationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_NOTIFY, - .val_handle = &eventHandle - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &ansUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - }, systemTask{systemTask}, notificationManager{notificationManager} { +AlertNotificationService::AlertNotificationService(System::SystemTask& systemTask, NotificationManager& notificationManager) + : characteristicDefinition {{.uuid = (ble_uuid_t*) &ansCharUuid, + .access_cb = AlertNotificationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE}, + {.uuid = (ble_uuid_t*) ¬ificationEventUuid, + .access_cb = AlertNotificationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_NOTIFY, + .val_handle = &eventHandle}, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &ansUuid, + .characteristics = characteristicDefinition}, + {0}, + }, + systemTask {systemTask}, + notificationManager {notificationManager} { } -int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { +int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { constexpr size_t stringTerminatorSize = 1; // end of string '\0' constexpr size_t headerSize = 3; const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; - const auto maxBufferSize{maxMessageSize + headerSize}; + const auto maxBufferSize {maxMessageSize + headerSize}; // Ignore notifications with empty message const auto packetLen = OS_MBUF_PKTLEN(ctxt->om); - if(packetLen <= headerSize) return 0; + if (packetLen <= headerSize) + return 0; size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); - auto messageSize = std::min(maxMessageSize, (bufferSize-headerSize)); + auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize)); Categories category; NotificationManager::Notification notif; - os_mbuf_copydata(ctxt->om, headerSize, messageSize-1, notif.message.data()); + os_mbuf_copydata(ctxt->om, headerSize, messageSize - 1, notif.message.data()); os_mbuf_copydata(ctxt->om, 0, 1, &category); - notif.message[messageSize-1] = '\0'; + notif.message[messageSize - 1] = '\0'; notif.size = messageSize; // TODO convert all ANS categories to NotificationController categories - switch(category) { + switch (category) { case Categories::Call: notif.category = Pinetime::Controllers::NotificationManager::Categories::IncomingCall; break; @@ -99,7 +88,7 @@ int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle void AlertNotificationService::AcceptIncomingCall() { auto response = IncomingCallResponses::Answer; - auto *om = ble_hs_mbuf_from_flat(&response, 1); + auto* om = ble_hs_mbuf_from_flat(&response, 1); uint16_t connectionHandle = systemTask.nimble().connHandle(); @@ -112,7 +101,7 @@ void AlertNotificationService::AcceptIncomingCall() { void AlertNotificationService::RejectIncomingCall() { auto response = IncomingCallResponses::Reject; - auto *om = ble_hs_mbuf_from_flat(&response, 1); + auto* om = ble_hs_mbuf_from_flat(&response, 1); uint16_t connectionHandle = systemTask.nimble().connHandle(); @@ -125,7 +114,7 @@ void AlertNotificationService::RejectIncomingCall() { void AlertNotificationService::MuteIncomingCall() { auto response = IncomingCallResponses::Mute; - auto *om = ble_hs_mbuf_from_flat(&response, 1); + auto* om = ble_hs_mbuf_from_flat(&response, 1); uint16_t connectionHandle = systemTask.nimble().connHandle(); diff --git a/src/components/ble/AlertNotificationService.h b/src/components/ble/AlertNotificationService.h index caad7a2b..66bb9be4 100644 --- a/src/components/ble/AlertNotificationService.h +++ b/src/components/ble/AlertNotificationService.h @@ -7,8 +7,9 @@ #undef max #undef min -//00020001-78fc-48fe-8e23-433b3a1942d0 -#define NOTIFICATION_EVENT_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x01, 0x00, 0x02, 0x00} +// 00020001-78fc-48fe-8e23-433b3a1942d0 +#define NOTIFICATION_EVENT_SERVICE_UUID_BASE \ + { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x01, 0x00, 0x02, 0x00 } namespace Pinetime { @@ -19,64 +20,49 @@ namespace Pinetime { class NotificationManager; class AlertNotificationService { - public: - AlertNotificationService(Pinetime::System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager ¬ificationManager); - void Init(); + public: + AlertNotificationService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); + void Init(); - int OnAlert(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); + int OnAlert(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - void AcceptIncomingCall(); - void RejectIncomingCall(); - void MuteIncomingCall(); + void AcceptIncomingCall(); + void RejectIncomingCall(); + void MuteIncomingCall(); - enum class IncomingCallResponses : uint8_t { - Reject = 0x00, - Answer = 0x01, - Mute = 0x02 - }; + enum class IncomingCallResponses : uint8_t { Reject = 0x00, Answer = 0x01, Mute = 0x02 }; - private: - enum class Categories : uint8_t { - SimpleAlert = 0x00, - Email = 0x01, - News = 0x02, - Call = 0x03, - MissedCall = 0x04, - MmsSms = 0x05, - VoiceMail = 0x06, - Schedule = 0x07, - HighPrioritizedAlert = 0x08, - InstantMessage = 0x09, - All = 0xff - }; + private: + enum class Categories : uint8_t { + SimpleAlert = 0x00, + Email = 0x01, + News = 0x02, + Call = 0x03, + MissedCall = 0x04, + MmsSms = 0x05, + VoiceMail = 0x06, + Schedule = 0x07, + HighPrioritizedAlert = 0x08, + InstantMessage = 0x09, + All = 0xff + }; - static constexpr uint16_t ansId {0x1811}; - static constexpr uint16_t ansCharId {0x2a46}; + static constexpr uint16_t ansId {0x1811}; + static constexpr uint16_t ansCharId {0x2a46}; - static constexpr ble_uuid16_t ansUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = ansId - }; + static constexpr ble_uuid16_t ansUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ansId}; - static constexpr ble_uuid16_t ansCharUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = ansCharId - }; + static constexpr ble_uuid16_t ansCharUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ansCharId}; - static constexpr ble_uuid128_t notificationEventUuid { - .u { .type = BLE_UUID_TYPE_128 }, - .value = NOTIFICATION_EVENT_SERVICE_UUID_BASE - }; + static constexpr ble_uuid128_t notificationEventUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NOTIFICATION_EVENT_SERVICE_UUID_BASE}; - struct ble_gatt_chr_def characteristicDefinition[3]; - struct ble_gatt_svc_def serviceDefinition[2]; + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; - Pinetime::System::SystemTask &systemTask; - NotificationManager ¬ificationManager; + Pinetime::System::SystemTask& systemTask; + NotificationManager& notificationManager; - uint16_t eventHandle; + uint16_t eventHandle; }; } } diff --git a/src/components/ble/BatteryInformationService.cpp b/src/components/ble/BatteryInformationService.cpp index bc66ff72..10a78d67 100644 --- a/src/components/ble/BatteryInformationService.cpp +++ b/src/components/ble/BatteryInformationService.cpp @@ -7,39 +7,26 @@ using namespace Pinetime::Controllers; constexpr ble_uuid16_t BatteryInformationService::batteryInformationServiceUuid; constexpr ble_uuid16_t BatteryInformationService::batteryLevelUuid; - - -int BatteryInformationServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { +int BatteryInformationServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto* batteryInformationService = static_cast(arg); return batteryInformationService->OnBatteryServiceRequested(conn_handle, attr_handle, ctxt); } -BatteryInformationService::BatteryInformationService(Controllers::Battery& batteryController) : - batteryController{batteryController}, - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &batteryLevelUuid, - .access_cb = BatteryInformationServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - .val_handle = &batteryLevelHandle - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &batteryInformationServiceUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - }{ - +BatteryInformationService::BatteryInformationService(Controllers::Battery& batteryController) + : batteryController {batteryController}, + characteristicDefinition {{.uuid = (ble_uuid_t*) &batteryLevelUuid, + .access_cb = BatteryInformationServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + .val_handle = &batteryLevelHandle}, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &batteryInformationServiceUuid, + .characteristics = characteristicDefinition}, + {0}, + } { } void BatteryInformationService::Init() { @@ -51,9 +38,10 @@ void BatteryInformationService::Init() { ASSERT(res == 0); } -int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, - ble_gatt_access_ctxt *context) { - if(attributeHandle == batteryLevelHandle) { +int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHandle, + uint16_t attributeHandle, + ble_gatt_access_ctxt* context) { + if (attributeHandle == batteryLevelHandle) { NRF_LOG_INFO("BATTERY : handle = %d", batteryLevelHandle); static uint8_t batteryValue = batteryController.PercentRemaining(); int res = os_mbuf_append(context->om, &batteryValue, 1); diff --git a/src/components/ble/BatteryInformationService.h b/src/components/ble/BatteryInformationService.h index b00000a4..59ed9c64 100644 --- a/src/components/ble/BatteryInformationService.h +++ b/src/components/ble/BatteryInformationService.h @@ -12,33 +12,25 @@ namespace Pinetime { namespace Controllers { class Battery; class BatteryInformationService { - public: - BatteryInformationService(Controllers::Battery& batteryController); - void Init(); + public: + BatteryInformationService(Controllers::Battery& batteryController); + void Init(); - int - OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); + int OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - private: - Controllers::Battery& batteryController; - static constexpr uint16_t batteryInformationServiceId {0x180F}; - static constexpr uint16_t batteryLevelId {0x2A19}; + private: + Controllers::Battery& batteryController; + static constexpr uint16_t batteryInformationServiceId {0x180F}; + static constexpr uint16_t batteryLevelId {0x2A19}; - static constexpr ble_uuid16_t batteryInformationServiceUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = batteryInformationServiceId - }; + static constexpr ble_uuid16_t batteryInformationServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = batteryInformationServiceId}; - static constexpr ble_uuid16_t batteryLevelUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = batteryLevelId - }; + static constexpr ble_uuid16_t batteryLevelUuid {.u {.type = BLE_UUID_TYPE_16}, .value = batteryLevelId}; - struct ble_gatt_chr_def characteristicDefinition[3]; - struct ble_gatt_svc_def serviceDefinition[2]; - - uint16_t batteryLevelHandle; + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; + uint16_t batteryLevelHandle; }; } } diff --git a/src/components/ble/BleClient.h b/src/components/ble/BleClient.h index 559f6c8d..d8a58e1c 100644 --- a/src/components/ble/BleClient.h +++ b/src/components/ble/BleClient.h @@ -3,10 +3,10 @@ #include namespace Pinetime { - namespace Controllers{ + namespace Controllers { class BleClient { - public: - virtual void Discover(uint16_t connectionHandle, std::function lambda) = 0; + public: + virtual void Discover(uint16_t connectionHandle, std::function lambda) = 0; }; } } \ No newline at end of file diff --git a/src/components/ble/BleController.cpp b/src/components/ble/BleController.cpp index 650ec79f..7c14aeb7 100644 --- a/src/components/ble/BleController.cpp +++ b/src/components/ble/BleController.cpp @@ -25,5 +25,3 @@ void Ble::FirmwareUpdateTotalBytes(uint32_t totalBytes) { void Ble::FirmwareUpdateCurrentBytes(uint32_t currentBytes) { firmwareUpdateCurrentBytes = currentBytes; } - - diff --git a/src/components/ble/BleController.h b/src/components/ble/BleController.h index 0570c8d0..6f5c2367 100644 --- a/src/components/ble/BleController.h +++ b/src/components/ble/BleController.h @@ -6,39 +6,57 @@ namespace Pinetime { namespace Controllers { class Ble { - public: - using BleAddress = std::array; - enum class FirmwareUpdateStates {Idle, Running, Validated, Error}; - enum class AddressTypes { Public, Random }; + public: + using BleAddress = std::array; + enum class FirmwareUpdateStates { Idle, Running, Validated, Error }; + enum class AddressTypes { Public, Random }; - Ble() = default; - bool IsConnected() const {return isConnected;} - void Connect(); - void Disconnect(); + Ble() = default; + bool IsConnected() const { + return isConnected; + } + void Connect(); + void Disconnect(); - void StartFirmwareUpdate(); - void StopFirmwareUpdate(); - void FirmwareUpdateTotalBytes(uint32_t totalBytes); - void FirmwareUpdateCurrentBytes(uint32_t currentBytes); - void State(FirmwareUpdateStates state) { firmwareUpdateState = state; } + void StartFirmwareUpdate(); + void StopFirmwareUpdate(); + void FirmwareUpdateTotalBytes(uint32_t totalBytes); + void FirmwareUpdateCurrentBytes(uint32_t currentBytes); + void State(FirmwareUpdateStates state) { + firmwareUpdateState = state; + } - bool IsFirmwareUpdating() const { return isFirmwareUpdating; } - uint32_t FirmwareUpdateTotalBytes() const { return firmwareUpdateTotalBytes; } - uint32_t FirmwareUpdateCurrentBytes() const { return firmwareUpdateCurrentBytes; } - FirmwareUpdateStates State() const { return firmwareUpdateState; } + bool IsFirmwareUpdating() const { + return isFirmwareUpdating; + } + uint32_t FirmwareUpdateTotalBytes() const { + return firmwareUpdateTotalBytes; + } + uint32_t FirmwareUpdateCurrentBytes() const { + return firmwareUpdateCurrentBytes; + } + FirmwareUpdateStates State() const { + return firmwareUpdateState; + } - void Address(BleAddress&& addr) { address = addr; } - const BleAddress& Address() const { return address; } - void AddressType(AddressTypes t) { addressType = t;} - private: - bool isConnected = false; - bool isFirmwareUpdating = false; - uint32_t firmwareUpdateTotalBytes = 0; - uint32_t firmwareUpdateCurrentBytes = 0; - FirmwareUpdateStates firmwareUpdateState = FirmwareUpdateStates::Idle; - BleAddress address; - AddressTypes addressType; + void Address(BleAddress&& addr) { + address = addr; + } + const BleAddress& Address() const { + return address; + } + void AddressType(AddressTypes t) { + addressType = t; + } + private: + bool isConnected = false; + bool isFirmwareUpdating = false; + uint32_t firmwareUpdateTotalBytes = 0; + uint32_t firmwareUpdateCurrentBytes = 0; + FirmwareUpdateStates firmwareUpdateState = FirmwareUpdateStates::Idle; + BleAddress address; + AddressTypes addressType; }; } } \ No newline at end of file diff --git a/src/components/ble/CurrentTimeClient.cpp b/src/components/ble/CurrentTimeClient.cpp index 758f636c..c6e68312 100644 --- a/src/components/ble/CurrentTimeClient.cpp +++ b/src/components/ble/CurrentTimeClient.cpp @@ -9,39 +9,37 @@ constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid; constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid; namespace { - int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) { - auto client = static_cast(arg); + int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) { + auto client = static_cast(arg); return client->OnDiscoveryEvent(conn_handle, error, service); } - int OnCurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); + int OnCurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, + const struct ble_gatt_error* error, + const struct ble_gatt_chr* chr, + void* arg) { + auto client = static_cast(arg); return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); } - int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - auto client = static_cast(arg); + int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error* error, struct ble_gatt_attr* attr, void* arg) { + auto client = static_cast(arg); return client->OnCurrentTimeReadResult(conn_handle, error, attr); } } -CurrentTimeClient::CurrentTimeClient(DateTime &dateTimeController) : dateTimeController{dateTimeController} { - +CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController {dateTimeController} { } void CurrentTimeClient::Init() { - } -bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_svc *service) { +bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) { if (service == nullptr && error->status == BLE_HS_EDONE) { if (isDiscovered) { NRF_LOG_INFO("CTS found, starting characteristics discovery"); - ble_gattc_disc_all_chrs(connectionHandle, ctsStartHandle, ctsEndHandle, - OnCurrentTimeCharacteristicDiscoveredCallback, this); + ble_gattc_disc_all_chrs(connectionHandle, ctsStartHandle, ctsEndHandle, OnCurrentTimeCharacteristicDiscoveredCallback, this); } else { NRF_LOG_INFO("CTS not found"); onServiceDiscovered(connectionHandle); @@ -49,7 +47,7 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga return true; } - if (service != nullptr && ble_uuid_cmp(((ble_uuid_t *) &ctsServiceUuid), &service->uuid.u) == 0) { + if (service != nullptr && ble_uuid_cmp(((ble_uuid_t*) &ctsServiceUuid), &service->uuid.u) == 0) { NRF_LOG_INFO("CTS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle); isDiscovered = true; ctsStartHandle = service->start_handle; @@ -59,8 +57,9 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga return false; } -int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic) { +int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, + const ble_gatt_error* error, + const ble_gatt_chr* characteristic) { if (characteristic == nullptr && error->status == BLE_HS_EDONE) { if (isCharacteristicDiscovered) { NRF_LOG_INFO("CTS Characteristic discovery complete, fetching time"); @@ -73,8 +72,7 @@ int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, cons return 0; } - if (characteristic != nullptr && - ble_uuid_cmp(((ble_uuid_t *) ¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { + if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*) ¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); isCharacteristicDiscovered = true; currentTimeHandle = characteristic->val_handle; @@ -82,17 +80,15 @@ int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, cons return 0; } -int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, - const ble_gatt_attr *attribute) { +int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_attr* attribute) { if (error->status == 0) { // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent CtsData result; os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result); - NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, - result.month, result.dayofmonth, - result.hour, result.minute, result.second); - dateTimeController.SetTime(result.year, result.month, result.dayofmonth, - 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + NRF_LOG_INFO( + "Received data: %d-%d-%d %d:%d:%d", result.year, result.month, result.dayofmonth, result.hour, result.minute, result.second); + dateTimeController.SetTime( + result.year, result.month, result.dayofmonth, 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); } else { NRF_LOG_INFO("Error retrieving current time: %d", error->status); } diff --git a/src/components/ble/CurrentTimeClient.h b/src/components/ble/CurrentTimeClient.h index 1b2e018c..c48a016d 100644 --- a/src/components/ble/CurrentTimeClient.h +++ b/src/components/ble/CurrentTimeClient.h @@ -8,54 +8,51 @@ #include "BleClient.h" namespace Pinetime { - namespace Controllers { - class DateTime; + namespace Controllers { + class DateTime; - class CurrentTimeClient : public BleClient { + class CurrentTimeClient : public BleClient { public: - explicit CurrentTimeClient(DateTime& dateTimeController); - void Init(); - void Reset(); - bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); - int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic); - int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); - static constexpr const ble_uuid16_t* Uuid() { return &CurrentTimeClient::ctsServiceUuid; } - static constexpr const ble_uuid16_t* CurrentTimeCharacteristicUuid() { return &CurrentTimeClient::currentTimeCharacteristicUuid; } - void Discover(uint16_t connectionHandle, std::function lambda) override; + explicit CurrentTimeClient(DateTime& dateTimeController); + void Init(); + void Reset(); + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service); + int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_chr* characteristic); + int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_attr* attribute); + static constexpr const ble_uuid16_t* Uuid() { + return &CurrentTimeClient::ctsServiceUuid; + } + static constexpr const ble_uuid16_t* CurrentTimeCharacteristicUuid() { + return &CurrentTimeClient::currentTimeCharacteristicUuid; + } + void Discover(uint16_t connectionHandle, std::function lambda) override; - private: - typedef struct __attribute__((packed)) { - uint16_t year; - uint8_t month; - uint8_t dayofmonth; - uint8_t hour; - uint8_t minute; - uint8_t second; - uint8_t millis; - uint8_t reason; - } CtsData; + private: + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; - static constexpr uint16_t ctsServiceId {0x1805}; - static constexpr uint16_t currentTimeCharacteristicId {0x2a2b}; + static constexpr uint16_t ctsServiceId {0x1805}; + static constexpr uint16_t currentTimeCharacteristicId {0x2a2b}; - static constexpr ble_uuid16_t ctsServiceUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = ctsServiceId - }; - static constexpr ble_uuid16_t currentTimeCharacteristicUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = currentTimeCharacteristicId - }; + static constexpr ble_uuid16_t ctsServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ctsServiceId}; + static constexpr ble_uuid16_t currentTimeCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = currentTimeCharacteristicId}; - DateTime& dateTimeController; - bool isDiscovered = false; - uint16_t ctsStartHandle; - uint16_t ctsEndHandle; + DateTime& dateTimeController; + bool isDiscovered = false; + uint16_t ctsStartHandle; + uint16_t ctsEndHandle; - bool isCharacteristicDiscovered = false; - uint16_t currentTimeHandle; - std::function onServiceDiscovered; - }; - } + bool isCharacteristicDiscovered = false; + uint16_t currentTimeHandle; + std::function onServiceDiscovered; + }; + } } \ No newline at end of file diff --git a/src/components/ble/CurrentTimeService.cpp b/src/components/ble/CurrentTimeService.cpp index 9f14edc7..b49be39c 100644 --- a/src/components/ble/CurrentTimeService.cpp +++ b/src/components/ble/CurrentTimeService.cpp @@ -7,8 +7,7 @@ using namespace Pinetime::Controllers; constexpr ble_uuid16_t CurrentTimeService::ctsUuid; constexpr ble_uuid16_t CurrentTimeService::ctChrUuid; - -int CTSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { +int CTSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto cts = static_cast(arg); return cts->OnTimeAccessed(conn_handle, attr_handle, ctxt); } @@ -22,22 +21,19 @@ void CurrentTimeService::Init() { ASSERT(res == 0); } +int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { -int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { - - NRF_LOG_INFO("Setting time..."); + NRF_LOG_INFO("Setting time..."); if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { CtsData result; os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); - NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, - result.month, result.dayofmonth, - result.hour, result.minute, result.second); + NRF_LOG_INFO( + "Received data: %d-%d-%d %d:%d:%d", result.year, result.month, result.dayofmonth, result.hour, result.minute, result.second); - m_dateTimeController.SetTime(result.year, result.month, result.dayofmonth, - 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + m_dateTimeController.SetTime( + result.year, result.month, result.dayofmonth, 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { CtsData currentDateTime; @@ -49,39 +45,26 @@ int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handl currentDateTime.second = m_dateTimeController.Seconds(); currentDateTime.millis = 0; - int res = os_mbuf_append(ctxt->om, ¤tDateTime, sizeof(CtsData)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; - } return 0; } -CurrentTimeService::CurrentTimeService(DateTime &dateTimeController) : - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &ctChrUuid, - .access_cb = CTSCallback, - - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &ctsUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - }, m_dateTimeController{dateTimeController} { - +CurrentTimeService::CurrentTimeService(DateTime& dateTimeController) + : characteristicDefinition {{.uuid = (ble_uuid_t*) &ctChrUuid, + .access_cb = CTSCallback, + + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &ctsUuid, + .characteristics = characteristicDefinition}, + {0}, + }, + m_dateTimeController {dateTimeController} { } - diff --git a/src/components/ble/CurrentTimeService.h b/src/components/ble/CurrentTimeService.h index 23956283..0a9a18a8 100644 --- a/src/components/ble/CurrentTimeService.h +++ b/src/components/ble/CurrentTimeService.h @@ -12,42 +12,35 @@ namespace Pinetime { namespace Controllers { class CurrentTimeService { - public: - CurrentTimeService(DateTime &dateTimeController); - void Init(); - - int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); - - private: - static constexpr uint16_t ctsId {0x1805}; - static constexpr uint16_t ctsCharId {0x2a2b}; - - static constexpr ble_uuid16_t ctsUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = ctsId - }; - - static constexpr ble_uuid16_t ctChrUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = ctsCharId - }; - - struct ble_gatt_chr_def characteristicDefinition[2]; - struct ble_gatt_svc_def serviceDefinition[2]; - - typedef struct __attribute__((packed)) { - uint16_t year; - uint8_t month; - uint8_t dayofmonth; - uint8_t hour; - uint8_t minute; - uint8_t second; - uint8_t millis; - uint8_t reason; - } CtsData; - - DateTime &m_dateTimeController; + public: + CurrentTimeService(DateTime& dateTimeController); + void Init(); + + int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); + + private: + static constexpr uint16_t ctsId {0x1805}; + static constexpr uint16_t ctsCharId {0x2a2b}; + + static constexpr ble_uuid16_t ctsUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ctsId}; + + static constexpr ble_uuid16_t ctChrUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ctsCharId}; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + DateTime& m_dateTimeController; }; } } diff --git a/src/components/ble/DeviceInformationService.cpp b/src/components/ble/DeviceInformationService.cpp index 406db1cf..cf482079 100644 --- a/src/components/ble/DeviceInformationService.cpp +++ b/src/components/ble/DeviceInformationService.cpp @@ -10,8 +10,7 @@ constexpr ble_uuid16_t DeviceInformationService::deviceInfoUuid; constexpr ble_uuid16_t DeviceInformationService::hwRevisionUuid; constexpr ble_uuid16_t DeviceInformationService::swRevisionUuid; - -int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { +int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto deviceInformationService = static_cast(arg); return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); } @@ -25,10 +24,8 @@ void DeviceInformationService::Init() { ASSERT(res == 0); } - -int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { - const char *str; +int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { + const char* str; switch (ble_uuid_u16(ctxt->chr->uuid)) { case manufacturerNameId: @@ -57,60 +54,49 @@ int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16 return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } -DeviceInformationService::DeviceInformationService() : - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &manufacturerNameUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t *) &modelNumberUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t *) &serialNumberUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t *) &fwRevisionUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t *) &hwRevisionUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t *) &swRevisionUuid, - .access_cb = DeviceInformationCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &deviceInfoUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - } - { - +DeviceInformationService::DeviceInformationService() + : characteristicDefinition {{ + .uuid = (ble_uuid_t*) &manufacturerNameUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*) &modelNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*) &serialNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*) &fwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*) &hwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*) &swRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &deviceInfoUuid, + .characteristics = characteristicDefinition}, + {0}, + } { } - diff --git a/src/components/ble/DeviceInformationService.h b/src/components/ble/DeviceInformationService.h index 94ca7bbf..3ff48b6d 100644 --- a/src/components/ble/DeviceInformationService.h +++ b/src/components/ble/DeviceInformationService.h @@ -9,69 +9,44 @@ namespace Pinetime { namespace Controllers { class DeviceInformationService { - public: - DeviceInformationService(); - void Init(); + public: + DeviceInformationService(); + void Init(); - int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - private: - static constexpr uint16_t deviceInfoId {0x180a}; - static constexpr uint16_t manufacturerNameId {0x2a29}; - static constexpr uint16_t modelNumberId {0x2a24}; - static constexpr uint16_t serialNumberId {0x2a25}; - static constexpr uint16_t fwRevisionId {0x2a26}; - static constexpr uint16_t hwRevisionId {0x2a27}; - static constexpr uint16_t swRevisionId {0x2a28}; + private: + static constexpr uint16_t deviceInfoId {0x180a}; + static constexpr uint16_t manufacturerNameId {0x2a29}; + static constexpr uint16_t modelNumberId {0x2a24}; + static constexpr uint16_t serialNumberId {0x2a25}; + static constexpr uint16_t fwRevisionId {0x2a26}; + static constexpr uint16_t hwRevisionId {0x2a27}; + static constexpr uint16_t swRevisionId {0x2a28}; - static constexpr const char* manufacturerName = "PINE64"; - static constexpr const char* modelNumber = "PineTime"; - static constexpr const char* hwRevision = "1.0.0"; - static constexpr const char* serialNumber = "0"; - static constexpr const char* fwRevision = Version::VersionString(); - static constexpr const char* swRevision = "InfiniTime"; + static constexpr const char* manufacturerName = "PINE64"; + static constexpr const char* modelNumber = "PineTime"; + static constexpr const char* hwRevision = "1.0.0"; + static constexpr const char* serialNumber = "0"; + static constexpr const char* fwRevision = Version::VersionString(); + static constexpr const char* swRevision = "InfiniTime"; + static constexpr ble_uuid16_t deviceInfoUuid {.u {.type = BLE_UUID_TYPE_16}, .value = deviceInfoId}; - static constexpr ble_uuid16_t deviceInfoUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = deviceInfoId - }; + static constexpr ble_uuid16_t manufacturerNameUuid {.u {.type = BLE_UUID_TYPE_16}, .value = manufacturerNameId}; - static constexpr ble_uuid16_t manufacturerNameUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = manufacturerNameId - }; + static constexpr ble_uuid16_t modelNumberUuid {.u {.type = BLE_UUID_TYPE_16}, .value = modelNumberId}; - static constexpr ble_uuid16_t modelNumberUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = modelNumberId - }; + static constexpr ble_uuid16_t serialNumberUuid {.u {.type = BLE_UUID_TYPE_16}, .value = serialNumberId}; - static constexpr ble_uuid16_t serialNumberUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = serialNumberId - }; + static constexpr ble_uuid16_t fwRevisionUuid {.u {.type = BLE_UUID_TYPE_16}, .value = fwRevisionId}; - static constexpr ble_uuid16_t fwRevisionUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = fwRevisionId - }; - - static constexpr ble_uuid16_t hwRevisionUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = hwRevisionId - }; - - static constexpr ble_uuid16_t swRevisionUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = swRevisionId - }; - - struct ble_gatt_chr_def characteristicDefinition[7]; - struct ble_gatt_svc_def serviceDefinition[2]; + static constexpr ble_uuid16_t hwRevisionUuid {.u {.type = BLE_UUID_TYPE_16}, .value = hwRevisionId}; + static constexpr ble_uuid16_t swRevisionUuid {.u {.type = BLE_UUID_TYPE_16}, .value = swRevisionId}; + struct ble_gatt_chr_def characteristicDefinition[7]; + struct ble_gatt_svc_def serviceDefinition[2]; }; } } \ No newline at end of file diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp index 90795b49..2031668e 100644 --- a/src/components/ble/DfuService.cpp +++ b/src/components/ble/DfuService.cpp @@ -11,67 +11,60 @@ constexpr ble_uuid128_t DfuService::controlPointCharacteristicUuid; constexpr ble_uuid128_t DfuService::revisionCharacteristicUuid; constexpr ble_uuid128_t DfuService::packetCharacteristicUuid; -int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt, void *arg) { - auto dfuService = static_cast(arg); +int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto dfuService = static_cast(arg); return dfuService->OnServiceData(conn_handle, attr_handle, ctxt); } void NotificationTimerCallback(TimerHandle_t xTimer) { - auto notificationManager = static_cast(pvTimerGetTimerID(xTimer)); + auto notificationManager = static_cast(pvTimerGetTimerID(xTimer)); notificationManager->OnNotificationTimer(); } void TimeoutTimerCallback(TimerHandle_t xTimer) { - auto dfuService = static_cast(pvTimerGetTimerID(xTimer)); + auto dfuService = static_cast(pvTimerGetTimerID(xTimer)); dfuService->OnTimeout(); } -DfuService::DfuService(Pinetime::System::SystemTask &systemTask, Pinetime::Controllers::Ble &bleController, - Pinetime::Drivers::SpiNorFlash &spiNorFlash) : - systemTask{systemTask}, - bleController{bleController}, - dfuImage{spiNorFlash}, - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &packetCharacteristicUuid, - .access_cb = DfuServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, - .val_handle = nullptr, - }, - { - .uuid = (ble_uuid_t *) &controlPointCharacteristicUuid, - .access_cb = DfuServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY, - .val_handle = nullptr, - }, - { - .uuid = (ble_uuid_t *) &revisionCharacteristicUuid, - .access_cb = DfuServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ, - .val_handle = &revision, - - }, - { - 0 - } - - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &serviceUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - } { - timeoutTimer = xTimerCreate ("notificationTimer", 10000, pdFALSE, this, TimeoutTimerCallback); +DfuService::DfuService(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::Ble& bleController, + Pinetime::Drivers::SpiNorFlash& spiNorFlash) + : systemTask {systemTask}, + bleController {bleController}, + dfuImage {spiNorFlash}, + characteristicDefinition {{ + .uuid = (ble_uuid_t*) &packetCharacteristicUuid, + .access_cb = DfuServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + .val_handle = nullptr, + }, + { + .uuid = (ble_uuid_t*) &controlPointCharacteristicUuid, + .access_cb = DfuServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY, + .val_handle = nullptr, + }, + { + .uuid = (ble_uuid_t*) &revisionCharacteristicUuid, + .access_cb = DfuServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + .val_handle = &revision, + + }, + {0} + + }, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &serviceUuid, + .characteristics = characteristicDefinition}, + {0}, + } { + timeoutTimer = xTimerCreate("notificationTimer", 10000, pdFALSE, this, TimeoutTimerCallback); } void DfuService::Init() { @@ -83,55 +76,54 @@ void DfuService::Init() { ASSERT(res == 0); } -int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context) { - if(bleController.IsFirmwareUpdating()){ +int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { + if (bleController.IsFirmwareUpdating()) { xTimerStart(timeoutTimer, 0); } - - ble_gatts_find_chr((ble_uuid_t *) &serviceUuid, (ble_uuid_t *) &packetCharacteristicUuid, nullptr, - &packetCharacteristicHandle); - ble_gatts_find_chr((ble_uuid_t *) &serviceUuid, (ble_uuid_t *) &controlPointCharacteristicUuid, nullptr, - &controlPointCharacteristicHandle); - ble_gatts_find_chr((ble_uuid_t *) &serviceUuid, (ble_uuid_t *) &revisionCharacteristicUuid, nullptr, - &revisionCharacteristicHandle); + ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &packetCharacteristicUuid, nullptr, &packetCharacteristicHandle); + ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &controlPointCharacteristicUuid, nullptr, &controlPointCharacteristicHandle); + ble_gatts_find_chr((ble_uuid_t*) &serviceUuid, (ble_uuid_t*) &revisionCharacteristicUuid, nullptr, &revisionCharacteristicHandle); if (attributeHandle == packetCharacteristicHandle) { if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) return WritePacketHandler(connectionHandle, context->om); - else return 0; + else + return 0; } else if (attributeHandle == controlPointCharacteristicHandle) { if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) return ControlPointHandler(connectionHandle, context->om); - else return 0; + else + return 0; } else if (attributeHandle == revisionCharacteristicHandle) { if (context->op == BLE_GATT_ACCESS_OP_READ_CHR) return SendDfuRevision(context->om); - else return 0; + else + return 0; } else { NRF_LOG_INFO("[DFU] Unknown Characteristic : %d", attributeHandle); return 0; } } -int DfuService::SendDfuRevision(os_mbuf *om) const { +int DfuService::SendDfuRevision(os_mbuf* om) const { int res = os_mbuf_append(om, &revision, sizeof(revision)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } -int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { +int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf* om) { switch (state) { case States::Start: { softdeviceSize = om->om_data[0] + (om->om_data[1] << 8) + (om->om_data[2] << 16) + (om->om_data[3] << 24); bootloaderSize = om->om_data[4] + (om->om_data[5] << 8) + (om->om_data[6] << 16) + (om->om_data[7] << 24); applicationSize = om->om_data[8] + (om->om_data[9] << 8) + (om->om_data[10] << 16) + (om->om_data[11] << 24); bleController.FirmwareUpdateTotalBytes(applicationSize); - NRF_LOG_INFO("[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, - bootloaderSize, applicationSize); + NRF_LOG_INFO( + "[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, bootloaderSize, applicationSize); dfuImage.Erase(); - uint8_t data[]{16, 1, 1}; + uint8_t data[] {16, 1, 1}; notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 3); state = States::Init; } @@ -139,19 +131,22 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { case States::Init: { uint16_t deviceType = om->om_data[0] + (om->om_data[1] << 8); uint16_t deviceRevision = om->om_data[2] + (om->om_data[3] << 8); - uint32_t applicationVersion = - om->om_data[4] + (om->om_data[5] << 8) + (om->om_data[6] << 16) + (om->om_data[7] << 24); + uint32_t applicationVersion = om->om_data[4] + (om->om_data[5] << 8) + (om->om_data[6] << 16) + (om->om_data[7] << 24); uint16_t softdeviceArrayLength = om->om_data[8] + (om->om_data[9] << 8); uint16_t sd[softdeviceArrayLength]; for (int i = 0; i < softdeviceArrayLength; i++) { sd[i] = om->om_data[10 + (i * 2)] + (om->om_data[10 + (i * 2) + 1] << 8); } - expectedCrc = - om->om_data[10 + (softdeviceArrayLength * 2)] + (om->om_data[10 + (softdeviceArrayLength * 2) + 1] << 8); + expectedCrc = om->om_data[10 + (softdeviceArrayLength * 2)] + (om->om_data[10 + (softdeviceArrayLength * 2) + 1] << 8); NRF_LOG_INFO( - "[DFU] -> Init data received : deviceType = %d, deviceRevision = %d, applicationVersion = %d, nb SD = %d, First SD = %d, CRC = %u", - deviceType, deviceRevision, applicationVersion, softdeviceArrayLength, sd[0], expectedCrc); + "[DFU] -> Init data received : deviceType = %d, deviceRevision = %d, applicationVersion = %d, nb SD = %d, First SD = %d, CRC = %u", + deviceType, + deviceRevision, + applicationVersion, + softdeviceArrayLength, + sd[0], + expectedCrc); return 0; } @@ -163,16 +158,18 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { bleController.FirmwareUpdateCurrentBytes(bytesReceived); if ((nbPacketReceived % nbPacketsToNotify) == 0 && bytesReceived != applicationSize) { - uint8_t data[5]{static_cast(Opcodes::PacketReceiptNotification), - (uint8_t) (bytesReceived & 0x000000FFu), (uint8_t) (bytesReceived >> 8u), - (uint8_t) (bytesReceived >> 16u), (uint8_t) (bytesReceived >> 24u)}; + uint8_t data[5] {static_cast(Opcodes::PacketReceiptNotification), + (uint8_t) (bytesReceived & 0x000000FFu), + (uint8_t) (bytesReceived >> 8u), + (uint8_t) (bytesReceived >> 16u), + (uint8_t) (bytesReceived >> 24u)}; NRF_LOG_INFO("[DFU] -> Send packet notification: %d bytes received", bytesReceived); notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 5); } if (dfuImage.IsComplete()) { - uint8_t data[3]{static_cast(Opcodes::Response), - static_cast(Opcodes::ReceiveFirmwareImage), - static_cast(ErrorCodes::NoError)}; + uint8_t data[3] {static_cast(Opcodes::Response), + static_cast(Opcodes::ReceiveFirmwareImage), + static_cast(ErrorCodes::NoError)}; NRF_LOG_INFO("[DFU] -> Send packet notification : all bytes received!"); notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 3); state = States::Validate; @@ -186,7 +183,7 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { return 0; } -int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { +int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf* om) { auto opcode = static_cast(om->om_data[0]); NRF_LOG_INFO("[DFU] -> ControlPointHandler"); @@ -214,8 +211,7 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { NRF_LOG_INFO("[DFU] -> Start DFU, mode %d not supported!", imageType); return 0; } - } - break; + } break; case Opcodes::InitDFUParameters: { if (state != States::Init) { NRF_LOG_INFO("[DFU] -> Init DFU requested, but we are not in Init state"); @@ -225,11 +221,9 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { NRF_LOG_INFO("[DFU] -> Init DFU parameters %s", isInitComplete ? " complete" : " not complete"); if (isInitComplete) { - uint8_t data[3] { - static_cast(Opcodes::Response), - static_cast(Opcodes::InitDFUParameters), - (isInitComplete ? uint8_t{1} : uint8_t{0}) - }; + uint8_t data[3] {static_cast(Opcodes::Response), + static_cast(Opcodes::InitDFUParameters), + (isInitComplete ? uint8_t {1} : uint8_t {0})}; notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3); return 0; } @@ -257,26 +251,22 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { NRF_LOG_INFO("[DFU] -> Validate firmware image requested -- %d", connectionHandle); - if(dfuImage.Validate()){ + if (dfuImage.Validate()) { state = States::Validated; bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated); NRF_LOG_INFO("Image OK"); - uint8_t data[3] { - static_cast(Opcodes::Response), - static_cast(Opcodes::ValidateFirmware), - static_cast(ErrorCodes::NoError) - }; + uint8_t data[3] {static_cast(Opcodes::Response), + static_cast(Opcodes::ValidateFirmware), + static_cast(ErrorCodes::NoError)}; notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3); } else { bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Error); NRF_LOG_INFO("Image Error : bad CRC"); - uint8_t data[3] { - static_cast(Opcodes::Response), - static_cast(Opcodes::ValidateFirmware), - static_cast(ErrorCodes::CrcError) - }; + uint8_t data[3] {static_cast(Opcodes::Response), + static_cast(Opcodes::ValidateFirmware), + static_cast(ErrorCodes::CrcError)}; notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3); } @@ -318,11 +308,11 @@ void DfuService::Reset() { } DfuService::NotificationManager::NotificationManager() { - timer = xTimerCreate ("notificationTimer", 1000, pdFALSE, this, NotificationTimerCallback); + timer = xTimerCreate("notificationTimer", 1000, pdFALSE, this, NotificationTimerCallback); } -bool DfuService::NotificationManager::AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t *data, size_t s) { - if(size != 0 || s > 10) +bool DfuService::NotificationManager::AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t* data, size_t s) { + if (size != 0 || s > 10) return false; connectionHandle = connection; @@ -334,14 +324,14 @@ bool DfuService::NotificationManager::AsyncSend(uint16_t connection, uint16_t ch } void DfuService::NotificationManager::OnNotificationTimer() { - if(size > 0) { + if (size > 0) { Send(connectionHandle, characteristicHandle, buffer, size); size = 0; } } -void DfuService::NotificationManager::Send(uint16_t connection, uint16_t charactHandle, const uint8_t *data, const size_t s) { - auto *om = ble_hs_mbuf_from_flat(data, s); +void DfuService::NotificationManager::Send(uint16_t connection, uint16_t charactHandle, const uint8_t* data, const size_t s) { + auto* om = ble_hs_mbuf_from_flat(data, s); auto ret = ble_gattc_notify_custom(connection, charactHandle, om); ASSERT(ret == 0); } @@ -354,27 +344,29 @@ void DfuService::NotificationManager::Reset() { } void DfuService::DfuImage::Init(size_t chunkSize, size_t totalSize, uint16_t expectedCrc) { - if(chunkSize != 20) return; + if (chunkSize != 20) + return; this->chunkSize = chunkSize; this->totalSize = totalSize; this->expectedCrc = expectedCrc; this->ready = true; } -void DfuService::DfuImage::Append(uint8_t *data, size_t size) { - if(!ready) return; +void DfuService::DfuImage::Append(uint8_t* data, size_t size) { + if (!ready) + return; ASSERT(size <= 20); std::memcpy(tempBuffer + bufferWriteIndex, data, size); bufferWriteIndex += size; - if(bufferWriteIndex == bufferSize) { + if (bufferWriteIndex == bufferSize) { spiNorFlash.Write(writeOffset + totalWriteIndex, tempBuffer, bufferWriteIndex); totalWriteIndex += bufferWriteIndex; bufferWriteIndex = 0; } - if(bufferWriteIndex > 0 && totalWriteIndex + bufferWriteIndex == totalSize) { + if (bufferWriteIndex > 0 && totalWriteIndex + bufferWriteIndex == totalSize) { spiNorFlash.Write(writeOffset + totalWriteIndex, tempBuffer, bufferWriteIndex); totalWriteIndex += bufferWriteIndex; if (totalSize < maxSize) @@ -383,15 +375,16 @@ void DfuService::DfuImage::Append(uint8_t *data, size_t size) { } void DfuService::DfuImage::WriteMagicNumber() { - uint32_t magic[4] = { // TODO When this variable is a static constexpr, the values written to the memory are not correct. Why? - 0xf395c277, - 0x7fefd260, - 0x0f505235, - 0x8079b62c, + uint32_t magic[4] = { + // TODO When this variable is a static constexpr, the values written to the memory are not correct. Why? + 0xf395c277, + 0x7fefd260, + 0x0f505235, + 0x8079b62c, }; uint32_t offset = writeOffset + (maxSize - (4 * sizeof(uint32_t))); - spiNorFlash.Write(offset, reinterpret_cast(magic), 4 * sizeof(uint32_t)); + spiNorFlash.Write(offset, reinterpret_cast(magic), 4 * sizeof(uint32_t)); } void DfuService::DfuImage::Erase() { @@ -421,7 +414,7 @@ bool DfuService::DfuImage::Validate() { return (crc == expectedCrc); } -uint16_t DfuService::DfuImage::ComputeCrc(uint8_t const *p_data, uint32_t size, uint16_t const *p_crc) { +uint16_t DfuService::DfuImage::ComputeCrc(uint8_t const* p_data, uint32_t size, uint16_t const* p_crc) { uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc; for (uint32_t i = 0; i < size; i++) { @@ -436,6 +429,7 @@ uint16_t DfuService::DfuImage::ComputeCrc(uint8_t const *p_data, uint32_t size, } bool DfuService::DfuImage::IsComplete() { - if(!ready) return false; + if (!ready) + return false; return totalWriteIndex == totalSize; } diff --git a/src/components/ble/DfuService.h b/src/components/ble/DfuService.h index 096bd99a..b932d80f 100644 --- a/src/components/ble/DfuService.h +++ b/src/components/ble/DfuService.h @@ -20,146 +20,139 @@ namespace Pinetime { class Ble; class DfuService { - public: - DfuService(Pinetime::System::SystemTask &systemTask, Pinetime::Controllers::Ble &bleController, - Pinetime::Drivers::SpiNorFlash &spiNorFlash); - void Init(); - int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); - void OnTimeout(); - void Reset(); - - class NotificationManager { + public: + DfuService(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::Ble& bleController, + Pinetime::Drivers::SpiNorFlash& spiNorFlash); + void Init(); + int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); + void OnTimeout(); + void Reset(); + + class NotificationManager { public: - NotificationManager(); - bool AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t *data, size_t size); - void Send(uint16_t connection, uint16_t characteristicHandle, const uint8_t *data, const size_t s); + NotificationManager(); + bool AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t* data, size_t size); + void Send(uint16_t connection, uint16_t characteristicHandle, const uint8_t* data, const size_t s); + private: - TimerHandle_t timer; - uint16_t connectionHandle = 0; - uint16_t characteristicHandle = 0; - size_t size = 0; - uint8_t buffer[10]; + TimerHandle_t timer; + uint16_t connectionHandle = 0; + uint16_t characteristicHandle = 0; + size_t size = 0; + uint8_t buffer[10]; + public: - void OnNotificationTimer(); - void Reset(); - }; - class DfuImage { + void OnNotificationTimer(); + void Reset(); + }; + class DfuImage { public: - DfuImage(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash{spiNorFlash} {} - void Init(size_t chunkSize, size_t totalSize, uint16_t expectedCrc); - void Erase(); - void Append(uint8_t* data, size_t size); - bool Validate(); - bool IsComplete(); + DfuImage(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash {spiNorFlash} { + } + void Init(size_t chunkSize, size_t totalSize, uint16_t expectedCrc); + void Erase(); + void Append(uint8_t* data, size_t size); + bool Validate(); + bool IsComplete(); private: - Pinetime::Drivers::SpiNorFlash& spiNorFlash; - static constexpr size_t bufferSize = 200; - bool ready = false; - size_t chunkSize = 0; - size_t totalSize = 0; - size_t maxSize = 475136; - size_t bufferWriteIndex = 0; - size_t totalWriteIndex = 0; - static constexpr size_t writeOffset = 0x40000; - uint8_t tempBuffer[bufferSize]; - uint16_t expectedCrc = 0; - - void WriteMagicNumber(); - uint16_t ComputeCrc(uint8_t const *p_data, uint32_t size, uint16_t const *p_crc); - - }; - - private: - Pinetime::System::SystemTask &systemTask; - Pinetime::Controllers::Ble &bleController; - DfuImage dfuImage; - NotificationManager notificationManager; - - static constexpr uint16_t dfuServiceId{0x1530}; - static constexpr uint16_t packetCharacteristicId{0x1532}; - static constexpr uint16_t controlPointCharacteristicId{0x1531}; - static constexpr uint16_t revisionCharacteristicId{0x1534}; - - uint16_t revision{0x0008}; - - static constexpr ble_uuid128_t serviceUuid{ - .u {.type = BLE_UUID_TYPE_128}, - .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, - 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00} - }; - - static constexpr ble_uuid128_t packetCharacteristicUuid{ - .u {.type = BLE_UUID_TYPE_128}, - .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, - 0xDE, 0xEF, 0x12, 0x12, 0x32, 0x15, 0x00, 0x00} - }; - - static constexpr ble_uuid128_t controlPointCharacteristicUuid{ - .u {.type = BLE_UUID_TYPE_128}, - .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, - 0xDE, 0xEF, 0x12, 0x12, 0x31, 0x15, 0x00, 0x00} - }; - - static constexpr ble_uuid128_t revisionCharacteristicUuid{ - .u {.type = BLE_UUID_TYPE_128}, - .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, - 0xDE, 0xEF, 0x12, 0x12, 0x34, 0x15, 0x00, 0x00} - }; - - struct ble_gatt_chr_def characteristicDefinition[4]; - struct ble_gatt_svc_def serviceDefinition[2]; - uint16_t packetCharacteristicHandle; - uint16_t controlPointCharacteristicHandle; - uint16_t revisionCharacteristicHandle; - - enum class States : uint8_t { - Idle, Init, Start, Data, Validate, Validated - }; - States state = States::Idle; - - enum class ImageTypes : uint8_t { - NoImage = 0x00, - SoftDevice = 0x01, - Bootloader = 0x02, - SoftDeviceAndBootloader = 0x03, - Application = 0x04 - }; - - enum class Opcodes : uint8_t { - StartDFU = 0x01, - InitDFUParameters = 0x02, - ReceiveFirmwareImage = 0x03, - ValidateFirmware = 0x04, - ActivateImageAndReset = 0x05, - PacketReceiptNotificationRequest = 0x08, - Response = 0x10, - PacketReceiptNotification = 0x11 - }; - - enum class ErrorCodes { - NoError = 0x01, - InvalidState = 0x02, - NotSupported = 0x03, - DataSizeExceedsLimits = 0x04, - CrcError = 0x05, - OperationFailed = 0x06 - }; - - uint8_t nbPacketsToNotify = 0; - uint32_t nbPacketReceived = 0; - uint32_t bytesReceived = 0; - - uint32_t softdeviceSize = 0; - uint32_t bootloaderSize = 0; - uint32_t applicationSize = 0; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; + static constexpr size_t bufferSize = 200; + bool ready = false; + size_t chunkSize = 0; + size_t totalSize = 0; + size_t maxSize = 475136; + size_t bufferWriteIndex = 0; + size_t totalWriteIndex = 0; + static constexpr size_t writeOffset = 0x40000; + uint8_t tempBuffer[bufferSize]; uint16_t expectedCrc = 0; - int SendDfuRevision(os_mbuf *om) const; - int WritePacketHandler(uint16_t connectionHandle, os_mbuf *om); - int ControlPointHandler(uint16_t connectionHandle, os_mbuf *om); - - TimerHandle_t timeoutTimer; + void WriteMagicNumber(); + uint16_t ComputeCrc(uint8_t const* p_data, uint32_t size, uint16_t const* p_crc); + }; + + private: + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::Ble& bleController; + DfuImage dfuImage; + NotificationManager notificationManager; + + static constexpr uint16_t dfuServiceId {0x1530}; + static constexpr uint16_t packetCharacteristicId {0x1532}; + static constexpr uint16_t controlPointCharacteristicId {0x1531}; + static constexpr uint16_t revisionCharacteristicId {0x1534}; + + uint16_t revision {0x0008}; + + static constexpr ble_uuid128_t serviceUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}}; + + static constexpr ble_uuid128_t packetCharacteristicUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x32, 0x15, 0x00, 0x00}}; + + static constexpr ble_uuid128_t controlPointCharacteristicUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x31, 0x15, 0x00, 0x00}}; + + static constexpr ble_uuid128_t revisionCharacteristicUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x34, 0x15, 0x00, 0x00}}; + + struct ble_gatt_chr_def characteristicDefinition[4]; + struct ble_gatt_svc_def serviceDefinition[2]; + uint16_t packetCharacteristicHandle; + uint16_t controlPointCharacteristicHandle; + uint16_t revisionCharacteristicHandle; + + enum class States : uint8_t { Idle, Init, Start, Data, Validate, Validated }; + States state = States::Idle; + + enum class ImageTypes : uint8_t { + NoImage = 0x00, + SoftDevice = 0x01, + Bootloader = 0x02, + SoftDeviceAndBootloader = 0x03, + Application = 0x04 + }; + + enum class Opcodes : uint8_t { + StartDFU = 0x01, + InitDFUParameters = 0x02, + ReceiveFirmwareImage = 0x03, + ValidateFirmware = 0x04, + ActivateImageAndReset = 0x05, + PacketReceiptNotificationRequest = 0x08, + Response = 0x10, + PacketReceiptNotification = 0x11 + }; + + enum class ErrorCodes { + NoError = 0x01, + InvalidState = 0x02, + NotSupported = 0x03, + DataSizeExceedsLimits = 0x04, + CrcError = 0x05, + OperationFailed = 0x06 + }; + + uint8_t nbPacketsToNotify = 0; + uint32_t nbPacketReceived = 0; + uint32_t bytesReceived = 0; + + uint32_t softdeviceSize = 0; + uint32_t bootloaderSize = 0; + uint32_t applicationSize = 0; + uint16_t expectedCrc = 0; + + int SendDfuRevision(os_mbuf* om) const; + int WritePacketHandler(uint16_t connectionHandle, os_mbuf* om); + int ControlPointHandler(uint16_t connectionHandle, os_mbuf* om); + + TimerHandle_t timeoutTimer; }; } } \ No newline at end of file diff --git a/src/components/ble/HeartRateService.cpp b/src/components/ble/HeartRateService.cpp index ee115ed0..c556566b 100644 --- a/src/components/ble/HeartRateService.cpp +++ b/src/components/ble/HeartRateService.cpp @@ -8,39 +8,29 @@ constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid; constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid; namespace { - int HeartRateServiceServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + int HeartRateServiceServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto* heartRateService = static_cast(arg); return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt); } } // TODO Refactoring - remove dependency to SystemTask -HeartRateService::HeartRateService(Pinetime::System::SystemTask &system, Controllers::HeartRateController& heartRateController) : - system{system}, - heartRateController{heartRateController}, - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &heartRateMeasurementUuid, - .access_cb = HeartRateServiceServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = &heartRateMeasurementHandle - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &heartRateServiceUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - }{ +HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController) + : system {system}, + heartRateController {heartRateController}, + characteristicDefinition {{.uuid = (ble_uuid_t*) &heartRateMeasurementUuid, + .access_cb = HeartRateServiceServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .val_handle = &heartRateMeasurementHandle}, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &heartRateServiceUuid, + .characteristics = characteristicDefinition}, + {0}, + } { // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) heartRateController.SetService(this); } @@ -54,9 +44,8 @@ void HeartRateService::Init() { ASSERT(res == 0); } -int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, - ble_gatt_access_ctxt *context) { - if(attributeHandle == heartRateMeasurementHandle) { +int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { + if (attributeHandle == heartRateMeasurementHandle) { NRF_LOG_INFO("HEARTRATE : handle = %d", heartRateMeasurementHandle); uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value @@ -68,7 +57,7 @@ int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t a void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) { uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value - auto *om = ble_hs_mbuf_from_flat(buffer, 2); + auto* om = ble_hs_mbuf_from_flat(buffer, 2); uint16_t connectionHandle = system.nimble().connHandle(); diff --git a/src/components/ble/HeartRateService.h b/src/components/ble/HeartRateService.h index 835e2941..7422445a 100644 --- a/src/components/ble/HeartRateService.h +++ b/src/components/ble/HeartRateService.h @@ -12,33 +12,26 @@ namespace Pinetime { namespace Controllers { class HeartRateController; class HeartRateService { - public: - HeartRateService(Pinetime::System::SystemTask &system, Controllers::HeartRateController& heartRateController); - void Init(); - int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); - void OnNewHeartRateValue(uint8_t hearRateValue); + public: + HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController); + void Init(); + int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); + void OnNewHeartRateValue(uint8_t hearRateValue); - private: - Pinetime::System::SystemTask &system; - Controllers::HeartRateController& heartRateController; - static constexpr uint16_t heartRateServiceId {0x180D}; - static constexpr uint16_t heartRateMeasurementId {0x2A37}; + private: + Pinetime::System::SystemTask& system; + Controllers::HeartRateController& heartRateController; + static constexpr uint16_t heartRateServiceId {0x180D}; + static constexpr uint16_t heartRateMeasurementId {0x2A37}; - static constexpr ble_uuid16_t heartRateServiceUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = heartRateServiceId - }; + static constexpr ble_uuid16_t heartRateServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateServiceId}; - static constexpr ble_uuid16_t heartRateMeasurementUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = heartRateMeasurementId - }; + static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId}; - struct ble_gatt_chr_def characteristicDefinition[3]; - struct ble_gatt_svc_def serviceDefinition[2]; - - uint16_t heartRateMeasurementHandle; + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; + uint16_t heartRateMeasurementHandle; }; } } diff --git a/src/components/ble/ImmediateAlertService.cpp b/src/components/ble/ImmediateAlertService.cpp index 0f6bf6cf..fd6430af 100644 --- a/src/components/ble/ImmediateAlertService.cpp +++ b/src/components/ble/ImmediateAlertService.cpp @@ -9,49 +9,42 @@ constexpr ble_uuid16_t ImmediateAlertService::immediateAlertServiceUuid; constexpr ble_uuid16_t ImmediateAlertService::alertLevelUuid; namespace { - int AlertLevelCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - auto *immediateAlertService = static_cast(arg); + int AlertLevelCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto* immediateAlertService = static_cast(arg); return immediateAlertService->OnAlertLevelChanged(conn_handle, attr_handle, ctxt); } const char* ToString(ImmediateAlertService::Levels level) { switch (level) { - case ImmediateAlertService::Levels::NoAlert: return "Alert : None"; - case ImmediateAlertService::Levels::HighAlert: return "Alert : High"; - case ImmediateAlertService::Levels::MildAlert: return "Alert : Mild"; - default: return ""; + case ImmediateAlertService::Levels::NoAlert: + return "Alert : None"; + case ImmediateAlertService::Levels::HighAlert: + return "Alert : High"; + case ImmediateAlertService::Levels::MildAlert: + return "Alert : Mild"; + default: + return ""; } } } -ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager ¬ificationManager) : - systemTask{systemTask}, - notificationManager{notificationManager}, - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &alertLevelUuid, - .access_cb = AlertLevelCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, - .val_handle = &alertLevelHandle - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &immediateAlertServiceUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - }{ - +ImmediateAlertService::ImmediateAlertService(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NotificationManager& notificationManager) + : systemTask {systemTask}, + notificationManager {notificationManager}, + characteristicDefinition {{.uuid = (ble_uuid_t*) &alertLevelUuid, + .access_cb = AlertLevelCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + .val_handle = &alertLevelHandle}, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &immediateAlertServiceUuid, + .characteristics = characteristicDefinition}, + {0}, + } { } void ImmediateAlertService::Init() { @@ -63,9 +56,9 @@ void ImmediateAlertService::Init() { ASSERT(res == 0); } -int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context) { - if(attributeHandle == alertLevelHandle) { - if(context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { +int ImmediateAlertService::OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { + if (attributeHandle == alertLevelHandle) { + if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { auto alertLevel = static_cast(context->om->om_data[0]); auto* alertString = ToString(alertLevel); diff --git a/src/components/ble/ImmediateAlertService.h b/src/components/ble/ImmediateAlertService.h index 2bc9cba5..6bd11bd7 100644 --- a/src/components/ble/ImmediateAlertService.h +++ b/src/components/ble/ImmediateAlertService.h @@ -12,39 +12,28 @@ namespace Pinetime { namespace Controllers { class NotificationManager; class ImmediateAlertService { - public: - enum class Levels : uint8_t { - NoAlert = 0, - MildAlert = 1, - HighAlert = 2 - }; - - ImmediateAlertService(Pinetime::System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager ¬ificationManager); - void Init(); - int OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); - - private: - Pinetime::System::SystemTask& systemTask; - NotificationManager& notificationManager; - - static constexpr uint16_t immediateAlertServiceId {0x1802}; - static constexpr uint16_t alertLevelId {0x2A06}; - - static constexpr ble_uuid16_t immediateAlertServiceUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = immediateAlertServiceId - }; - - static constexpr ble_uuid16_t alertLevelUuid { - .u {.type = BLE_UUID_TYPE_16}, - .value = alertLevelId - }; - - struct ble_gatt_chr_def characteristicDefinition[3]; - struct ble_gatt_svc_def serviceDefinition[2]; - - uint16_t alertLevelHandle; + public: + enum class Levels : uint8_t { NoAlert = 0, MildAlert = 1, HighAlert = 2 }; + + ImmediateAlertService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); + void Init(); + int OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); + + private: + Pinetime::System::SystemTask& systemTask; + NotificationManager& notificationManager; + + static constexpr uint16_t immediateAlertServiceId {0x1802}; + static constexpr uint16_t alertLevelId {0x2A06}; + + static constexpr ble_uuid16_t immediateAlertServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = immediateAlertServiceId}; + + static constexpr ble_uuid16_t alertLevelUuid {.u {.type = BLE_UUID_TYPE_16}, .value = alertLevelId}; + + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; + + uint16_t alertLevelHandle; }; } } diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index 1230ad57..36bf2709 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -18,12 +18,12 @@ #include "MusicService.h" #include "systemtask/SystemTask.h" -int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - auto musicService = static_cast(arg); +int MSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto musicService = static_cast(arg); return musicService->OnCommand(conn_handle, attr_handle, ctxt); } -Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask &system) : m_system(system) { +Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask& system) : m_system(system) { msUuid.value[14] = msId[0]; msUuid.value[15] = msId[1]; @@ -86,82 +86,51 @@ Pinetime::Controllers::MusicService::MusicService(Pinetime::System::SystemTask & msShuffleCharUuid.value[13] = msShuffleCharId[1]; msShuffleCharUuid.value[14] = msId[0]; msShuffleCharUuid.value[15] = msId[1]; - - characteristicDefinition[0] = {.uuid = (ble_uuid_t *) (&msEventCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_NOTIFY, - .val_handle = &eventHandle - }; - characteristicDefinition[1] = {.uuid = (ble_uuid_t *) (&msStatusCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[2] = {.uuid = (ble_uuid_t *) (&msTrackCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[3] = {.uuid = (ble_uuid_t *) (&msArtistCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[4] = {.uuid = (ble_uuid_t *) (&msAlbumCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[5] = {.uuid = (ble_uuid_t *) (&msPositionCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[6] = {.uuid = (ble_uuid_t *) (&msTotalLengthCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[7] = {.uuid = (ble_uuid_t *) (&msTotalLengthCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[8] = {.uuid = (ble_uuid_t *) (&msTrackNumberCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[9] = {.uuid = (ble_uuid_t *) (&msTrackTotalCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[10] = {.uuid = (ble_uuid_t *) (&msPlaybackSpeedCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[11] = {.uuid = (ble_uuid_t *) (&msRepeatCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[12] = {.uuid = (ble_uuid_t *) (&msShuffleCharUuid), - .access_cb = MSCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; + + characteristicDefinition[0] = {.uuid = (ble_uuid_t*) (&msEventCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_NOTIFY, + .val_handle = &eventHandle}; + characteristicDefinition[1] = { + .uuid = (ble_uuid_t*) (&msStatusCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[2] = { + .uuid = (ble_uuid_t*) (&msTrackCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[3] = { + .uuid = (ble_uuid_t*) (&msArtistCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[4] = { + .uuid = (ble_uuid_t*) (&msAlbumCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[5] = { + .uuid = (ble_uuid_t*) (&msPositionCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[6] = {.uuid = (ble_uuid_t*) (&msTotalLengthCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[7] = {.uuid = (ble_uuid_t*) (&msTotalLengthCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[8] = {.uuid = (ble_uuid_t*) (&msTrackNumberCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[9] = {.uuid = (ble_uuid_t*) (&msTrackTotalCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[10] = {.uuid = (ble_uuid_t*) (&msPlaybackSpeedCharUuid), + .access_cb = MSCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[11] = { + .uuid = (ble_uuid_t*) (&msRepeatCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[12] = { + .uuid = (ble_uuid_t*) (&msShuffleCharUuid), .access_cb = MSCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; characteristicDefinition[13] = {0}; - - serviceDefinition[0] = { - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &msUuid, - .characteristics = characteristicDefinition - }; + + serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = (ble_uuid_t*) &msUuid, .characteristics = characteristicDefinition}; serviceDefinition[1] = {0}; - + artistName = "Waiting for"; albumName = ""; trackName = "track information.."; @@ -177,41 +146,40 @@ void Pinetime::Controllers::MusicService::Init() { int res = 0; res = ble_gatts_count_cfg(serviceDefinition); ASSERT(res == 0); - + res = ble_gatts_add_svcs(serviceDefinition); ASSERT(res == 0); } -int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { - +int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); uint8_t data[notifSize + 1]; data[notifSize] = '\0'; os_mbuf_copydata(ctxt->om, 0, notifSize, data); - char *s = (char *) &data[0]; - if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msArtistCharUuid) == 0) { + char* s = (char*) &data[0]; + if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msArtistCharUuid) == 0) { artistName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msTrackCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackCharUuid) == 0) { trackName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msAlbumCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msAlbumCharUuid) == 0) { albumName = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msStatusCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msStatusCharUuid) == 0) { playing = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msRepeatCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msRepeatCharUuid) == 0) { repeat = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msShuffleCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msShuffleCharUuid) == 0) { shuffle = s[0]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msPositionCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msPositionCharUuid) == 0) { trackProgress = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msTotalLengthCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTotalLengthCharUuid) == 0) { trackLength = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msTrackNumberCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackNumberCharUuid) == 0) { trackNumber = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msTrackTotalCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msTrackTotalCharUuid) == 0) { tracksTotal = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &msPlaybackSpeedCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &msPlaybackSpeedCharUuid) == 0) { playbackSpeed = static_cast(((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3])) / 100.0f; } } @@ -239,14 +207,14 @@ float Pinetime::Controllers::MusicService::getPlaybackSpeed() { } void Pinetime::Controllers::MusicService::event(char event) { - auto *om = ble_hs_mbuf_from_flat(&event, 1); - + auto* om = ble_hs_mbuf_from_flat(&event, 1); + uint16_t connectionHandle = m_system.nimble().connHandle(); - + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { return; } - + ble_gattc_notify_custom(connectionHandle, eventHandle, om); } @@ -257,4 +225,3 @@ int Pinetime::Controllers::MusicService::getProgress() { int Pinetime::Controllers::MusicService::getTrackLength() { return trackLength; } - diff --git a/src/components/ble/MusicService.h b/src/components/ble/MusicService.h index 172ab61c..5416219d 100644 --- a/src/components/ble/MusicService.h +++ b/src/components/ble/MusicService.h @@ -26,40 +26,40 @@ #undef max #undef min -//00000000-78fc-48fe-8e23-433b3a1942d0 -#define MUSIC_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00} +// 00000000-78fc-48fe-8e23-433b3a1942d0 +#define MUSIC_SERVICE_UUID_BASE \ + { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00 } namespace Pinetime { namespace System { class SystemTask; } namespace Controllers { - + class MusicService { - public: - explicit MusicService(Pinetime::System::SystemTask &system); - + public: + explicit MusicService(Pinetime::System::SystemTask& system); + void Init(); - - int OnCommand(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); - + + int OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); + void event(char event); - + std::string getArtist(); - + std::string getTrack(); - + std::string getAlbum(); - + int getProgress(); - + int getTrackLength(); - + float getPlaybackSpeed(); - + bool isPlaying(); - + static const char EVENT_MUSIC_OPEN = 0xe0; static const char EVENT_MUSIC_PLAY = 0x00; static const char EVENT_MUSIC_PAUSE = 0x01; @@ -67,12 +67,10 @@ namespace Pinetime { static const char EVENT_MUSIC_PREV = 0x04; static const char EVENT_MUSIC_VOLUP = 0x05; static const char EVENT_MUSIC_VOLDOWN = 0x06; - - enum MusicStatus { - NotPlaying = 0x00, - Playing = 0x01 - }; - private: + + enum MusicStatus { NotPlaying = 0x00, Playing = 0x01 }; + + private: static constexpr uint8_t msId[2] = {0x00, 0x00}; static constexpr uint8_t msEventCharId[2] = {0x01, 0x00}; static constexpr uint8_t msStatusCharId[2] = {0x02, 0x00}; @@ -86,84 +84,44 @@ namespace Pinetime { static constexpr uint8_t msPlaybackSpeedCharId[2] = {0x0a, 0x00}; static constexpr uint8_t msRepeatCharId[2] = {0x0b, 0x00}; static constexpr uint8_t msShuffleCharId[2] = {0x0c, 0x00}; - - ble_uuid128_t msUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - - ble_uuid128_t msEventCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msStatusCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msArtistCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msTrackCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msAlbumCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msPositionCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msTotalLengthCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msTrackNumberCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msTrackTotalCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msPlaybackSpeedCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msRepeatCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - ble_uuid128_t msShuffleCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = MUSIC_SERVICE_UUID_BASE - }; - + + ble_uuid128_t msUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + + ble_uuid128_t msEventCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msStatusCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msArtistCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msTrackCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msAlbumCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msPositionCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msTotalLengthCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msTrackNumberCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msTrackTotalCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msPlaybackSpeedCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msRepeatCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + ble_uuid128_t msShuffleCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = MUSIC_SERVICE_UUID_BASE}; + struct ble_gatt_chr_def characteristicDefinition[14]; struct ble_gatt_svc_def serviceDefinition[2]; - + uint16_t eventHandle; - + std::string artistName; std::string albumName; std::string trackName; - + bool playing; - + int trackProgress; int trackLength; int trackNumber; int tracksTotal; - + float playbackSpeed; - + bool repeat; bool shuffle; - - Pinetime::System::SystemTask &m_system; + + Pinetime::System::SystemTask& m_system; }; } } - diff --git a/src/components/ble/NavigationService.cpp b/src/components/ble/NavigationService.cpp index 545c44da..e1c20bf1 100644 --- a/src/components/ble/NavigationService.cpp +++ b/src/components/ble/NavigationService.cpp @@ -20,12 +20,12 @@ #include "systemtask/SystemTask.h" -int NAVCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - auto navService = static_cast(arg); +int NAVCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto navService = static_cast(arg); return navService->OnCommand(conn_handle, attr_handle, ctxt); } -Pinetime::Controllers::NavigationService::NavigationService(Pinetime::System::SystemTask &system) : m_system(system) { +Pinetime::Controllers::NavigationService::NavigationService(Pinetime::System::SystemTask& system) : m_system(system) { navUuid.value[14] = navId[0]; navUuid.value[15] = navId[1]; @@ -49,35 +49,25 @@ Pinetime::Controllers::NavigationService::NavigationService(Pinetime::System::Sy navProgressCharUuid.value[14] = navId[0]; navProgressCharUuid.value[15] = navId[1]; - characteristicDefinition[0] = {.uuid = (ble_uuid_t *) (&navFlagCharUuid), - .access_cb = NAVCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - - characteristicDefinition[1] = {.uuid = (ble_uuid_t *) (&navNarrativeCharUuid), - .access_cb = NAVCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[2] = {.uuid = (ble_uuid_t *) (&navManDistCharUuid), - .access_cb = NAVCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; - characteristicDefinition[3] = {.uuid = (ble_uuid_t *) (&navProgressCharUuid), - .access_cb = NAVCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ - }; + characteristicDefinition[0] = { + .uuid = (ble_uuid_t*) (&navFlagCharUuid), .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + + characteristicDefinition[1] = {.uuid = (ble_uuid_t*) (&navNarrativeCharUuid), + .access_cb = NAVCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[2] = {.uuid = (ble_uuid_t*) (&navManDistCharUuid), + .access_cb = NAVCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; + characteristicDefinition[3] = {.uuid = (ble_uuid_t*) (&navProgressCharUuid), + .access_cb = NAVCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; characteristicDefinition[4] = {0}; - serviceDefinition[0] = { - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &navUuid, - .characteristics = characteristicDefinition - }; + serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = (ble_uuid_t*) &navUuid, .characteristics = characteristicDefinition}; serviceDefinition[1] = {0}; m_progress = 0; @@ -92,45 +82,39 @@ void Pinetime::Controllers::NavigationService::Init() { ASSERT(res == 0); } -int Pinetime::Controllers::NavigationService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { +int Pinetime::Controllers::NavigationService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); uint8_t data[notifSize + 1]; data[notifSize] = '\0'; os_mbuf_copydata(ctxt->om, 0, notifSize, data); - char *s = (char *) &data[0]; - if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navFlagCharUuid) == 0) { + char* s = (char*) &data[0]; + if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navFlagCharUuid) == 0) { m_flag = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navNarrativeCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navNarrativeCharUuid) == 0) { m_narrative = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navManDistCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navManDistCharUuid) == 0) { m_manDist = s; - } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &navProgressCharUuid) == 0) { + } else if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t*) &navProgressCharUuid) == 0) { m_progress = data[0]; } } return 0; } -std::string Pinetime::Controllers::NavigationService::getFlag() -{ - return m_flag; +std::string Pinetime::Controllers::NavigationService::getFlag() { + return m_flag; } -std::string Pinetime::Controllers::NavigationService::getNarrative() -{ - return m_narrative; +std::string Pinetime::Controllers::NavigationService::getNarrative() { + return m_narrative; } -std::string Pinetime::Controllers::NavigationService::getManDist() -{ - return m_manDist; +std::string Pinetime::Controllers::NavigationService::getManDist() { + return m_manDist; } -int Pinetime::Controllers::NavigationService::getProgress() -{ - return m_progress; +int Pinetime::Controllers::NavigationService::getProgress() { + return m_progress; } - diff --git a/src/components/ble/NavigationService.h b/src/components/ble/NavigationService.h index 29b17582..dc4f0a0d 100644 --- a/src/components/ble/NavigationService.h +++ b/src/components/ble/NavigationService.h @@ -26,8 +26,9 @@ #undef max #undef min -//c7e60000-78fc-48fe-8e23-433b3a1942d0 -#define NAVIGATION_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00} +// c7e60000-78fc-48fe-8e23-433b3a1942d0 +#define NAVIGATION_SERVICE_UUID_BASE \ + { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00 } namespace Pinetime { namespace System { @@ -36,13 +37,12 @@ namespace Pinetime { namespace Controllers { class NavigationService { - public: - explicit NavigationService(Pinetime::System::SystemTask &system); + public: + explicit NavigationService(Pinetime::System::SystemTask& system); void Init(); - int OnCommand(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); + int OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); std::string getFlag(); @@ -52,34 +52,19 @@ namespace Pinetime { int getProgress(); - private: + private: static constexpr uint8_t navId[2] = {0x01, 0x00}; static constexpr uint8_t navFlagCharId[2] = {0x01, 0x00}; static constexpr uint8_t navNarrativeCharId[2] = {0x02, 0x00}; static constexpr uint8_t navManDistCharId[2] = {0x03, 0x00}; static constexpr uint8_t navProgressCharId[2] = {0x04, 0x00}; - ble_uuid128_t navUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = NAVIGATION_SERVICE_UUID_BASE - }; - - ble_uuid128_t navFlagCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = NAVIGATION_SERVICE_UUID_BASE - }; - ble_uuid128_t navNarrativeCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = NAVIGATION_SERVICE_UUID_BASE - }; - ble_uuid128_t navManDistCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = NAVIGATION_SERVICE_UUID_BASE - }; - ble_uuid128_t navProgressCharUuid{ - .u = {.type = BLE_UUID_TYPE_128}, - .value = NAVIGATION_SERVICE_UUID_BASE - }; + ble_uuid128_t navUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = NAVIGATION_SERVICE_UUID_BASE}; + + ble_uuid128_t navFlagCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = NAVIGATION_SERVICE_UUID_BASE}; + ble_uuid128_t navNarrativeCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = NAVIGATION_SERVICE_UUID_BASE}; + ble_uuid128_t navManDistCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = NAVIGATION_SERVICE_UUID_BASE}; + ble_uuid128_t navProgressCharUuid {.u = {.type = BLE_UUID_TYPE_128}, .value = NAVIGATION_SERVICE_UUID_BASE}; struct ble_gatt_chr_def characteristicDefinition[5]; struct ble_gatt_svc_def serviceDefinition[2]; @@ -89,8 +74,7 @@ namespace Pinetime { std::string m_manDist; int m_progress; - Pinetime::System::SystemTask &m_system; + Pinetime::System::SystemTask& m_system; }; } } - diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index eb83c709..67a6d691 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -19,36 +19,37 @@ using namespace Pinetime::Controllers; NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, - DateTime& dateTimeController, - Pinetime::Controllers::NotificationManager& notificationManager, - Controllers::Battery& batteryController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Controllers::HeartRateController& heartRateController) : - systemTask{systemTask}, - bleController{bleController}, - dateTimeController{dateTimeController}, - notificationManager{notificationManager}, - spiNorFlash{spiNorFlash}, - dfuService{systemTask, bleController, spiNorFlash}, - currentTimeClient{dateTimeController}, - anService{systemTask, notificationManager}, - alertNotificationClient{systemTask, notificationManager}, - currentTimeService{dateTimeController}, - musicService{systemTask}, - navService{systemTask}, - batteryInformationService{batteryController}, - immediateAlertService{systemTask, notificationManager}, - heartRateService{systemTask, heartRateController}, - serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { + DateTime& dateTimeController, + Pinetime::Controllers::NotificationManager& notificationManager, + Controllers::Battery& batteryController, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Controllers::HeartRateController& heartRateController) + : systemTask {systemTask}, + bleController {bleController}, + dateTimeController {dateTimeController}, + notificationManager {notificationManager}, + spiNorFlash {spiNorFlash}, + dfuService {systemTask, bleController, spiNorFlash}, + currentTimeClient {dateTimeController}, + anService {systemTask, notificationManager}, + alertNotificationClient {systemTask, notificationManager}, + currentTimeService {dateTimeController}, + musicService {systemTask}, + navService {systemTask}, + batteryInformationService {batteryController}, + immediateAlertService {systemTask, notificationManager}, + heartRateService {systemTask, heartRateController}, + serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } -int GAPEventCallback(struct ble_gap_event *event, void *arg) { +int GAPEventCallback(struct ble_gap_event* event, void* arg) { auto nimbleController = static_cast(arg); return nimbleController->OnGAPEvent(event); } void NimbleController::Init() { - while (!ble_hs_synced()) {} + while (!ble_hs_synced()) { + } ble_svc_gap_init(); ble_svc_gatt_init(); @@ -81,7 +82,8 @@ void NimbleController::Init() { } void NimbleController::StartAdvertising() { - if(bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active()) return; + if (bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active()) + return; ble_svc_gap_device_name_set(deviceName); @@ -101,29 +103,27 @@ void NimbleController::StartAdvertising() { adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; - fields.flags = BLE_HS_ADV_F_DISC_GEN | - BLE_HS_ADV_F_BREDR_UNSUP; -// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE( -// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, -// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)); + fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; + // fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE( + // 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + // 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)); fields.uuids128 = &dfuServiceUuid; fields.num_uuids128 = 1; fields.uuids128_is_complete = 1; fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; - rsp_fields.name = (uint8_t *)deviceName; + rsp_fields.name = (uint8_t*) deviceName; rsp_fields.name_len = strlen(deviceName); rsp_fields.name_is_complete = 1; ble_gap_adv_set_fields(&fields); -// ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync) + // ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync) ble_gap_adv_rsp_set_fields(&rsp_fields); -// ASSERT(res == 0); + // ASSERT(res == 0); - ble_gap_adv_start(addrType, NULL, 180000, - &adv_params, GAPEventCallback, this); -// ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu. + ble_gap_adv_start(addrType, NULL, 180000, &adv_params, GAPEventCallback, this); + // ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu. // For now, the advertising is restarted as soon as it ends. There may be a race condition // that prevent the advertising from restarting reliably. // I remove the assert to prevent this uncesseray crash, but in the long term, the management of @@ -131,7 +131,7 @@ void NimbleController::StartAdvertising() { // the application has been woken up, for example. } -int NimbleController::OnGAPEvent(ble_gap_event *event) { +int NimbleController::OnGAPEvent(ble_gap_event* event) { switch (event->type) { case BLE_GAP_EVENT_ADV_COMPLETE: NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE"); @@ -141,8 +141,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT"); /* A new connection was established or a connection attempt failed. */ - NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", - event->connect.status); + NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status); if (event->connect.status != 0) { /* Connection failed; resume advertising. */ @@ -154,8 +153,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { connectionHandle = event->connect.conn_handle; // Service discovery is deffered via systemtask } - } - break; + } break; case BLE_GAP_EVENT_DISCONNECT: NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT"); NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); @@ -178,19 +176,16 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { return 0; case BLE_GAP_EVENT_SUBSCRIBE: NRF_LOG_INFO("subscribe event; conn_handle=%d attr_handle=%d " - "reason=%d prevn=%d curn=%d previ=%d curi=???\n", - event->subscribe.conn_handle, - event->subscribe.attr_handle, - event->subscribe.reason, - event->subscribe.prev_notify, - event->subscribe.cur_notify, - event->subscribe.prev_indicate); + "reason=%d prevn=%d curn=%d previ=%d curi=???\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate); return 0; case BLE_GAP_EVENT_MTU: - NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", - event->mtu.conn_handle, - event->mtu.channel_id, - event->mtu.value); + NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); return 0; case BLE_GAP_EVENT_REPEAT_PAIRING: { @@ -216,9 +211,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { NRF_LOG_INFO("received %s; conn_handle=%d attr_handle=%d " "attr_len=%d", - event->notify_rx.indication ? - "indication" : - "notification", + event->notify_rx.indication ? "indication" : "notification", event->notify_rx.conn_handle, event->notify_rx.attr_handle, notifSize); @@ -229,7 +222,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { /* Attribute data is contained in event->notify_rx.attr_data. */ default: -// NRF_LOG_INFO("Advertising event : %d", event->type); + // NRF_LOG_INFO("Advertising event : %d", event->type); break; } return 0; @@ -239,8 +232,6 @@ void NimbleController::StartDiscovery() { serviceDiscovery.StartDiscovery(connectionHandle); } - uint16_t NimbleController::connHandle() { - return connectionHandle; + return connectionHandle; } - diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 7bb135da..6d4aa38a 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -36,62 +36,69 @@ namespace Pinetime { class NimbleController { - public: - NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, - DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager, - Controllers::Battery& batteryController, Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Controllers::HeartRateController& heartRateController); - void Init(); - void StartAdvertising(); - int OnGAPEvent(ble_gap_event *event); - - 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); - int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); - int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, - uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); - - void StartDiscovery(); - - Pinetime::Controllers::MusicService& music() {return musicService;}; - Pinetime::Controllers::NavigationService& navigation() {return navService;}; - Pinetime::Controllers::AlertNotificationService& alertService() {return anService;}; - - uint16_t connHandle(); - - private: - static constexpr const char* deviceName = "InfiniTime"; - Pinetime::System::SystemTask& systemTask; - Pinetime::Controllers::Ble& bleController; - DateTime& dateTimeController; - Pinetime::Controllers::NotificationManager& notificationManager; - Pinetime::Drivers::SpiNorFlash& spiNorFlash; - Pinetime::Controllers::DfuService dfuService; - - DeviceInformationService deviceInformationService; - CurrentTimeClient currentTimeClient; - AlertNotificationService anService; - AlertNotificationClient alertNotificationClient; - CurrentTimeService currentTimeService; - MusicService musicService; - NavigationService navService; - BatteryInformationService batteryInformationService; - ImmediateAlertService immediateAlertService; - HeartRateService heartRateService; - - uint8_t addrType; // 1 = Random, 0 = PUBLIC - uint16_t connectionHandle = 0; - - ble_uuid128_t dfuServiceUuid { - .u { .type = BLE_UUID_TYPE_128}, - .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, - 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00} - }; - - ServiceDiscovery serviceDiscovery; + public: + NimbleController(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::Ble& bleController, + DateTime& dateTimeController, + Pinetime::Controllers::NotificationManager& notificationManager, + Controllers::Battery& batteryController, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Controllers::HeartRateController& heartRateController); + void Init(); + void StartAdvertising(); + int OnGAPEvent(ble_gap_event* event); + + 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); + int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute); + int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, + const ble_gatt_error* error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc* descriptor); + + void StartDiscovery(); + + Pinetime::Controllers::MusicService& music() { + return musicService; + }; + Pinetime::Controllers::NavigationService& navigation() { + return navService; + }; + Pinetime::Controllers::AlertNotificationService& alertService() { + return anService; + }; + + uint16_t connHandle(); + + private: + static constexpr const char* deviceName = "InfiniTime"; + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::Ble& bleController; + DateTime& dateTimeController; + Pinetime::Controllers::NotificationManager& notificationManager; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; + Pinetime::Controllers::DfuService dfuService; + + DeviceInformationService deviceInformationService; + CurrentTimeClient currentTimeClient; + AlertNotificationService anService; + AlertNotificationClient alertNotificationClient; + CurrentTimeService currentTimeService; + MusicService musicService; + NavigationService navService; + BatteryInformationService batteryInformationService; + ImmediateAlertService immediateAlertService; + HeartRateService heartRateService; + + uint8_t addrType; // 1 = Random, 0 = PUBLIC + uint16_t connectionHandle = 0; + + ble_uuid128_t dfuServiceUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}}; + + ServiceDiscovery serviceDiscovery; }; } } diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 88e83b92..b1b0e6b2 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -6,15 +6,15 @@ using namespace Pinetime::Controllers; constexpr uint8_t NotificationManager::MessageSize; - -void NotificationManager::Push(NotificationManager::Notification &¬if) { +void NotificationManager::Push(NotificationManager::Notification&& notif) { notif.id = GetNextId(); notif.valid = true; notifications[writeIndex] = std::move(notif); writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; - if(!empty) + if (!empty) readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; - else empty = false; + else + empty = false; newNotification = true; } @@ -30,40 +30,48 @@ NotificationManager::Notification::Id NotificationManager::GetNextId() { } NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) { - auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); - if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) { + return n.valid && n.id == id; + }); + if (currentIterator == notifications.end() || currentIterator->id != id) + return Notification {}; auto& lastNotification = notifications[readIndex]; NotificationManager::Notification result; - if(currentIterator == (notifications.end()-1)) + if (currentIterator == (notifications.end() - 1)) result = *(notifications.begin()); else - result = *(currentIterator+1); + result = *(currentIterator + 1); - if(result.id <= id) return {}; + if (result.id <= id) + return {}; - result.index = (lastNotification.id - result.id)+1; + result.index = (lastNotification.id - result.id) + 1; return result; } NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) { - auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); - if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) { + return n.valid && n.id == id; + }); + if (currentIterator == notifications.end() || currentIterator->id != id) + return Notification {}; auto& lastNotification = notifications[readIndex]; NotificationManager::Notification result; - if(currentIterator == notifications.begin()) - result = *(notifications.end()-1); + if (currentIterator == notifications.begin()) + result = *(notifications.end() - 1); else - result = *(currentIterator-1); + result = *(currentIterator - 1); - if(result.id >= id) return {}; + if (result.id >= id) + return {}; - result.index = (lastNotification.id - result.id)+1; + result.index = (lastNotification.id - result.id) + 1; return result; } @@ -76,7 +84,7 @@ bool NotificationManager::IsVibrationEnabled() { } void NotificationManager::ToggleVibrations() { - vibrationEnabled = !vibrationEnabled; + vibrationEnabled = !vibrationEnabled; } bool NotificationManager::ClearNewNotificationFlag() { @@ -84,21 +92,23 @@ bool NotificationManager::ClearNewNotificationFlag() { } size_t NotificationManager::NbNotifications() const { - return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;}); + return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n) { + return n.valid; + }); } const char* NotificationManager::Notification::Message() const { - const char* itField = std::find(message.begin(), message.begin()+size-1, '\0'); - if(itField != message.begin()+size-1) { - const char* ptr = (itField)+1; + const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0'); + if (itField != message.begin() + size - 1) { + const char* ptr = (itField) + 1; return ptr; } return const_cast(message.data()); } const char* NotificationManager::Notification::Title() const { - const char * itField = std::find(message.begin(), message.begin()+size-1, '\0'); - if(itField != message.begin()+size-1) { + const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0'); + if (itField != message.begin() + size - 1) { return message.data(); } return {}; diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 486bba15..8e8fb374 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -8,23 +8,35 @@ namespace Pinetime { namespace Controllers { class NotificationManager { - public: - enum class Categories {Unknown, SimpleAlert, Email, News, IncomingCall, MissedCall, Sms, VoiceMail, Schedule, HighProriotyAlert, InstantMessage }; - static constexpr uint8_t MessageSize{100}; + public: + enum class Categories { + Unknown, + SimpleAlert, + Email, + News, + IncomingCall, + MissedCall, + Sms, + VoiceMail, + Schedule, + HighProriotyAlert, + InstantMessage + }; + static constexpr uint8_t MessageSize {100}; - struct Notification { - using Id = uint8_t; - Id id; - bool valid = false; - uint8_t index; - uint8_t size; - std::array message; - Categories category = Categories::Unknown; + struct Notification { + using Id = uint8_t; + Id id; + bool valid = false; + uint8_t index; + uint8_t size; + std::array message; + Categories category = Categories::Unknown; - const char* Message() const; - const char* Title() const; - }; - Notification::Id nextId {0}; + const char* Message() const; + const char* Title() const; + }; + Notification::Id nextId {0}; void Push(Notification&& notif); Notification GetLastNotification(); @@ -35,18 +47,20 @@ namespace Pinetime { bool IsVibrationEnabled(); void ToggleVibrations(); - static constexpr size_t MaximumMessageSize() { return MessageSize; }; + static constexpr size_t MaximumMessageSize() { + return MessageSize; + }; size_t NbNotifications() const; - private: - Notification::Id GetNextId(); - static constexpr uint8_t TotalNbNotifications = 5; - std::array notifications; - uint8_t readIndex = 0; - uint8_t writeIndex = 0; - bool empty = true; - std::atomic newNotification{false}; - bool vibrationEnabled = true; + private: + Notification::Id GetNextId(); + static constexpr uint8_t TotalNbNotifications = 5; + std::array notifications; + uint8_t readIndex = 0; + uint8_t writeIndex = 0; + bool empty = true; + std::atomic newNotification {false}; + bool vibrationEnabled = true; }; } } \ No newline at end of file diff --git a/src/components/ble/ServiceDiscovery.cpp b/src/components/ble/ServiceDiscovery.cpp index 4d37b8bd..b36b241c 100644 --- a/src/components/ble/ServiceDiscovery.cpp +++ b/src/components/ble/ServiceDiscovery.cpp @@ -4,8 +4,7 @@ using namespace Pinetime::Controllers; -ServiceDiscovery::ServiceDiscovery(std::array&& clients) : clients{clients} { - +ServiceDiscovery::ServiceDiscovery(std::array&& clients) : clients {clients} { } void ServiceDiscovery::StartDiscovery(uint16_t connectionHandle) { @@ -16,7 +15,7 @@ void ServiceDiscovery::StartDiscovery(uint16_t connectionHandle) { void ServiceDiscovery::OnServiceDiscovered(uint16_t connectionHandle) { clientIterator++; - if(clientIterator != clients.end()) { + if (clientIterator != clients.end()) { DiscoverNextService(connectionHandle); } else { NRF_LOG_INFO("End of service discovery"); @@ -26,7 +25,7 @@ void ServiceDiscovery::OnServiceDiscovered(uint16_t connectionHandle) { void ServiceDiscovery::DiscoverNextService(uint16_t connectionHandle) { NRF_LOG_INFO("[Discovery] Discover next service"); - auto discoverNextService = [this](uint16_t connectionHandle){ + auto discoverNextService = [this](uint16_t connectionHandle) { this->OnServiceDiscovered(connectionHandle); }; (*clientIterator)->Discover(connectionHandle, discoverNextService); diff --git a/src/components/ble/ServiceDiscovery.h b/src/components/ble/ServiceDiscovery.h index 0b26bd1d..56bb3e44 100644 --- a/src/components/ble/ServiceDiscovery.h +++ b/src/components/ble/ServiceDiscovery.h @@ -8,17 +8,16 @@ namespace Pinetime { class BleClient; class ServiceDiscovery { - public: - ServiceDiscovery(std::array&& bleClients); + public: + ServiceDiscovery(std::array&& bleClients); - void StartDiscovery(uint16_t connectionHandle); + void StartDiscovery(uint16_t connectionHandle); - - private: - BleClient** clientIterator; - std::array clients; - void OnServiceDiscovered(uint16_t connectionHandle); - void DiscoverNextService(uint16_t connectionHandle); + private: + BleClient** clientIterator; + std::array clients; + void OnServiceDiscovered(uint16_t connectionHandle); + void DiscoverNextService(uint16_t connectionHandle); }; } } diff --git a/src/components/brightness/BrightnessController.cpp b/src/components/brightness/BrightnessController.cpp index 0d8f366a..8ad987d1 100644 --- a/src/components/brightness/BrightnessController.cpp +++ b/src/components/brightness/BrightnessController.cpp @@ -4,7 +4,6 @@ using namespace Pinetime::Controllers; - void BrightnessController::Init() { nrf_gpio_cfg_output(pinLcdBacklight1); nrf_gpio_cfg_output(pinLcdBacklight2); @@ -14,7 +13,7 @@ void BrightnessController::Init() { void BrightnessController::Set(BrightnessController::Levels level) { this->level = level; - switch(level) { + switch (level) { default: case Levels::High: nrf_gpio_pin_clear(pinLcdBacklight1); @@ -40,20 +39,34 @@ void BrightnessController::Set(BrightnessController::Levels level) { } void BrightnessController::Lower() { - switch(level) { - case Levels::High: Set(Levels::Medium); break; - case Levels::Medium: Set(Levels::Low); break; - case Levels::Low: Set(Levels::Off); break; - default: break; + switch (level) { + case Levels::High: + Set(Levels::Medium); + break; + case Levels::Medium: + Set(Levels::Low); + break; + case Levels::Low: + Set(Levels::Off); + break; + default: + break; } } void BrightnessController::Higher() { - switch(level) { - case Levels::Off: Set(Levels::Low); break; - case Levels::Low: Set(Levels::Medium); break; - case Levels::Medium: Set(Levels::High); break; - default: break; + switch (level) { + case Levels::Off: + Set(Levels::Low); + break; + case Levels::Low: + Set(Levels::Medium); + break; + case Levels::Medium: + Set(Levels::High); + break; + default: + break; } } @@ -70,29 +83,44 @@ void BrightnessController::Restore() { } void BrightnessController::Step() { - switch(level) { - case Levels::Low: Set(Levels::Medium); break; - case Levels::Medium: Set(Levels::High); break; - case Levels::High: Set(Levels::Low); break; - default: break; + switch (level) { + case Levels::Low: + Set(Levels::Medium); + break; + case Levels::Medium: + Set(Levels::High); + break; + case Levels::High: + Set(Levels::Low); + break; + default: + break; } } const char* BrightnessController::GetIcon() { - switch(level) { - case Levels::Medium: return Applications::Screens::Symbols::brightnessMedium; - case Levels::High: return Applications::Screens::Symbols::brightnessHigh; - default: break; + switch (level) { + case Levels::Medium: + return Applications::Screens::Symbols::brightnessMedium; + case Levels::High: + return Applications::Screens::Symbols::brightnessHigh; + default: + break; } return Applications::Screens::Symbols::brightnessLow; } const char* BrightnessController::ToString() { - switch(level) { - case Levels::Off: return "Off"; - case Levels::Low: return "Low"; - case Levels::Medium: return "Medium"; - case Levels::High: return "High"; - default : return "???"; + switch (level) { + case Levels::Off: + return "Off"; + case Levels::Low: + return "Low"; + case Levels::Medium: + return "Medium"; + case Levels::High: + return "High"; + default: + return "???"; } } \ No newline at end of file diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h index efff7e81..e8c36e3b 100644 --- a/src/components/brightness/BrightnessController.h +++ b/src/components/brightness/BrightnessController.h @@ -5,8 +5,8 @@ namespace Pinetime { namespace Controllers { class BrightnessController { - public: - enum class Levels {Off, Low, Medium, High}; + public: + enum class Levels { Off, Low, Medium, High }; void Init(); void Set(Levels level); @@ -21,7 +21,7 @@ namespace Pinetime { const char* GetIcon(); const char* ToString(); - private: + private: static constexpr uint8_t pinLcdBacklight1 = 14; static constexpr uint8_t pinLcdBacklight2 = 22; static constexpr uint8_t pinLcdBacklight3 = 23; diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 9c1f2684..4f9302eb 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -5,22 +5,21 @@ using namespace Pinetime::Controllers; -DateTime::DateTime(System::SystemTask& systemTask) : systemTask{systemTask} { - +DateTime::DateTime(System::SystemTask& systemTask) : systemTask {systemTask} { } - -void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, - uint8_t second, uint32_t systickCounter) { - std::tm tm = { /* .tm_sec = */ second, - /* .tm_min = */ minute, - /* .tm_hour = */ hour, - /* .tm_mday = */ day, - /* .tm_mon = */ month - 1, - /* .tm_year = */ year - 1900, +void DateTime::SetTime( + uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) { + std::tm tm = { + /* .tm_sec = */ second, + /* .tm_min = */ minute, + /* .tm_hour = */ hour, + /* .tm_mday = */ day, + /* .tm_mon = */ month - 1, + /* .tm_year = */ year - 1900, }; tm.tm_isdst = -1; // Use DST value from local time zone - currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm)); + currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm)); NRF_LOG_INFO("%d %d %d ", day, month, year); NRF_LOG_INFO("%d %d %d ", hour, minute, second); @@ -34,7 +33,7 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfW void DateTime::UpdateTime(uint32_t systickCounter) { // Handle systick counter overflow uint32_t systickDelta = 0; - if(systickCounter < previousSystickCounter) { + if (systickCounter < previousSystickCounter) { systickDelta = 0xffffff - previousSystickCounter; systickDelta += systickCounter + 1; } else { @@ -42,11 +41,11 @@ void DateTime::UpdateTime(uint32_t systickCounter) { } /* - * 1000 ms = 1024 ticks - */ + * 1000 ms = 1024 ticks + */ auto correctedDelta = systickDelta / 1024; - auto rest = (systickDelta - (correctedDelta*1024)); - if(systickCounter >= rest) { + auto rest = (systickDelta - (correctedDelta * 1024)); + if (systickCounter >= rest) { previousSystickCounter = systickCounter - rest; } else { previousSystickCounter = 0xffffff - (rest - systickCounter); @@ -56,12 +55,12 @@ void DateTime::UpdateTime(uint32_t systickCounter) { uptime += std::chrono::seconds(correctedDelta); auto dp = date::floor(currentDateTime); - auto time = date::make_time(currentDateTime-dp); + auto time = date::make_time(currentDateTime - dp); auto yearMonthDay = date::year_month_day(dp); - year = (int)yearMonthDay.year(); - month = static_cast((unsigned)yearMonthDay.month()); - day = (unsigned)yearMonthDay.day(); + year = (int) yearMonthDay.year(); + month = static_cast((unsigned) yearMonthDay.month()); + day = (unsigned) yearMonthDay.day(); dayOfWeek = static_cast(date::weekday(yearMonthDay).iso_encoding()); hour = time.hours().count(); @@ -69,7 +68,7 @@ void DateTime::UpdateTime(uint32_t systickCounter) { second = time.seconds().count(); // Notify new day to SystemTask - if(hour == 0 and not isMidnightAlreadyNotified) { + if (hour == 0 and not isMidnightAlreadyNotified) { isMidnightAlreadyNotified = true; systemTask.PushMessage(System::SystemTask::Messages::OnNewDay); } else if (hour != 0) { @@ -77,123 +76,45 @@ void DateTime::UpdateTime(uint32_t systickCounter) { } } -const char *DateTime::MonthShortToString() { - return DateTime::MonthsString[(uint8_t)month]; +const char* DateTime::MonthShortToString() { + return DateTime::MonthsString[(uint8_t) month]; } -const char *DateTime::MonthShortToStringLow() { - return DateTime::MonthsStringLow[(uint8_t)month]; +const char* DateTime::MonthShortToStringLow() { + return DateTime::MonthsStringLow[(uint8_t) month]; } -const char *DateTime::MonthsToStringLow() { - return DateTime::MonthsLow[(uint8_t)month]; +const char* DateTime::MonthsToStringLow() { + return DateTime::MonthsLow[(uint8_t) month]; } -const char *DateTime::DayOfWeekToString() { - return DateTime::DaysString[(uint8_t)dayOfWeek]; +const char* DateTime::DayOfWeekToString() { + return DateTime::DaysString[(uint8_t) dayOfWeek]; } -const char *DateTime::DayOfWeekShortToString() { - return DateTime::DaysStringShort[(uint8_t)dayOfWeek]; +const char* DateTime::DayOfWeekShortToString() { + return DateTime::DaysStringShort[(uint8_t) dayOfWeek]; } -const char *DateTime::DayOfWeekToStringLow() { - return DateTime::DaysStringLow[(uint8_t)dayOfWeek]; +const char* DateTime::DayOfWeekToStringLow() { + return DateTime::DaysStringLow[(uint8_t) dayOfWeek]; } -const char *DateTime::DayOfWeekShortToStringLow() { - return DateTime::DaysStringShortLow[(uint8_t)dayOfWeek]; +const char* DateTime::DayOfWeekShortToStringLow() { + return DateTime::DaysStringShortLow[(uint8_t) dayOfWeek]; } +char const* DateTime::DaysStringLow[] = {"--", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; + +char const* DateTime::DaysStringShortLow[] = {"--", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; + +char const* DateTime::DaysStringShort[] = {"--", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; + +char const* DateTime::DaysString[] = {"--", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"}; + +char const* DateTime::MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; + +char const* DateTime::MonthsStringLow[] = {"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; -char const *DateTime::DaysStringLow[] = { - "--", - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - "Sunday" -}; - -char const *DateTime::DaysStringShortLow[] = { - "--", - "Mon", - "Tue", - "Wed", - "Thu", - "Fri", - "Sat", - "Sun" -}; - -char const *DateTime::DaysStringShort[] = { - "--", - "MON", - "TUE", - "WED", - "THU", - "FRI", - "SAT", - "SUN" -}; - -char const *DateTime::DaysString[] = { - "--", - "MONDAY", - "TUESDAY", - "WEDNESDAY", - "THURSDAY", - "FRIDAY", - "SATURDAY", - "SUNDAY" -}; - -char const *DateTime::MonthsString[] = { - "--", - "JAN", - "FEB", - "MAR", - "APR", - "MAY", - "JUN", - "JUL", - "AUG", - "SEP", - "OCT", - "NOV", - "DEC" -}; - -char const *DateTime::MonthsStringLow[] = { - "--", - "Jan", - "Feb", - "Mar", - "Apr", - "May", - "Jun", - "Jul", - "Aug", - "Sep", - "Oct", - "Nov", - "Dec" -}; - -char const *DateTime::MonthsLow[] = { - "--", - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December" -}; \ No newline at end of file +char const* DateTime::MonthsLow[] = { + "--", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; \ No newline at end of file diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index fb0969d1..823442b2 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -9,56 +9,95 @@ namespace Pinetime { } namespace Controllers { class DateTime { - public: - enum class Days : uint8_t {Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; - enum class Months : uint8_t {Unknown, January, February, March, April, May, June, July, August, September, October, November, December}; + public: + enum class Days : uint8_t { Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; + enum class Months : uint8_t { + Unknown, + January, + February, + March, + April, + May, + June, + July, + August, + September, + October, + November, + December + }; - DateTime(System::SystemTask& systemTask); + DateTime(System::SystemTask& systemTask); - void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter); - void UpdateTime(uint32_t systickCounter); - uint16_t Year() const { return year; } - Months Month() const { return month; } - uint8_t Day() const { return day; } - Days DayOfWeek() const { return dayOfWeek; } - uint8_t Hours() const { return hour; } - uint8_t Minutes() const { return minute; } - uint8_t Seconds() const { return second; } + void SetTime(uint16_t year, + uint8_t month, + uint8_t day, + uint8_t dayOfWeek, + uint8_t hour, + uint8_t minute, + uint8_t second, + uint32_t systickCounter); + void UpdateTime(uint32_t systickCounter); + uint16_t Year() const { + return year; + } + Months Month() const { + return month; + } + uint8_t Day() const { + return day; + } + Days DayOfWeek() const { + return dayOfWeek; + } + uint8_t Hours() const { + return hour; + } + uint8_t Minutes() const { + return minute; + } + uint8_t Seconds() const { + return second; + } - const char *MonthShortToString(); - const char *MonthShortToStringLow(); - const char *MonthsToStringLow(); - const char *DayOfWeekToString(); - const char *DayOfWeekShortToString(); - const char *DayOfWeekToStringLow(); - const char *DayOfWeekShortToStringLow(); + const char* MonthShortToString(); + const char* MonthShortToStringLow(); + const char* MonthsToStringLow(); + const char* DayOfWeekToString(); + const char* DayOfWeekShortToString(); + const char* DayOfWeekToStringLow(); + const char* DayOfWeekShortToStringLow(); - std::chrono::time_point CurrentDateTime() const { return currentDateTime; } - std::chrono::seconds Uptime() const { return uptime; } - private: - System::SystemTask& systemTask; - uint16_t year = 0; - Months month = Months::Unknown; - uint8_t day = 0; - Days dayOfWeek = Days::Unknown; - uint8_t hour = 0; - uint8_t minute = 0; - uint8_t second = 0; + std::chrono::time_point CurrentDateTime() const { + return currentDateTime; + } + std::chrono::seconds Uptime() const { + return uptime; + } - uint32_t previousSystickCounter = 0; - std::chrono::time_point currentDateTime; - std::chrono::seconds uptime {0}; + private: + System::SystemTask& systemTask; + uint16_t year = 0; + Months month = Months::Unknown; + uint8_t day = 0; + Days dayOfWeek = Days::Unknown; + uint8_t hour = 0; + uint8_t minute = 0; + uint8_t second = 0; - bool isMidnightAlreadyNotified = false; + uint32_t previousSystickCounter = 0; + std::chrono::time_point currentDateTime; + std::chrono::seconds uptime {0}; - static char const *DaysString[]; - static char const *DaysStringShort[]; - static char const *DaysStringLow[]; - static char const *DaysStringShortLow[]; - static char const *MonthsString[]; - static char const *MonthsStringLow[]; - static char const *MonthsLow[]; + bool isMidnightAlreadyNotified = false; + static char const* DaysString[]; + static char const* DaysStringShort[]; + static char const* DaysStringLow[]; + static char const* DaysStringShortLow[]; + static char const* MonthsString[]; + static char const* MonthsStringLow[]; + static char const* MonthsLow[]; }; } } \ No newline at end of file diff --git a/src/components/firmwarevalidator/FirmwareValidator.cpp b/src/components/firmwarevalidator/FirmwareValidator.cpp index 48f98550..68e66d37 100644 --- a/src/components/firmwarevalidator/FirmwareValidator.cpp +++ b/src/components/firmwarevalidator/FirmwareValidator.cpp @@ -6,12 +6,12 @@ using namespace Pinetime::Controllers; bool FirmwareValidator::IsValidated() const { - auto* imageOkPtr = reinterpret_cast(validBitAdress); + auto* imageOkPtr = reinterpret_cast(validBitAdress); return (*imageOkPtr) == validBitValue; } void FirmwareValidator::Validate() { - if(!IsValidated()) + if (!IsValidated()) Pinetime::Drivers::InternalFlash::WriteWord(validBitAdress, validBitValue); } diff --git a/src/components/firmwarevalidator/FirmwareValidator.h b/src/components/firmwarevalidator/FirmwareValidator.h index aa576d88..00b5a0bb 100644 --- a/src/components/firmwarevalidator/FirmwareValidator.h +++ b/src/components/firmwarevalidator/FirmwareValidator.h @@ -5,14 +5,15 @@ namespace Pinetime { namespace Controllers { class FirmwareValidator { - public: - void Validate(); - bool IsValidated() const; + public: + void Validate(); + bool IsValidated() const; - void Reset(); - private: - static constexpr uint32_t validBitAdress {0x7BFE8}; - static constexpr uint32_t validBitValue {1}; + void Reset(); + + private: + static constexpr uint32_t validBitAdress {0x7BFE8}; + static constexpr uint32_t validBitValue {1}; }; } } diff --git a/src/components/gfx/Gfx.cpp b/src/components/gfx/Gfx.cpp index 59fa8164..cf271032 100644 --- a/src/components/gfx/Gfx.cpp +++ b/src/components/gfx/Gfx.cpp @@ -2,11 +2,10 @@ #include "drivers/St7789.h" using namespace Pinetime::Components; -Gfx::Gfx(Pinetime::Drivers::St7789 &lcd) : lcd{lcd} { +Gfx::Gfx(Pinetime::Drivers::St7789& lcd) : lcd {lcd} { } void Gfx::Init() { - } void Gfx::ClearScreen() { @@ -17,10 +16,9 @@ void Gfx::ClearScreen() { state.busy = true; state.action = Action::FillRectangle; state.taskToNotify = xTaskGetCurrentTaskHandle(); - - lcd.DrawBuffer(0, 0, width, height, reinterpret_cast(buffer), width * 2); - WaitTransferFinished(); + lcd.DrawBuffer(0, 0, width, height, reinterpret_cast(buffer), width * 2); + WaitTransferFinished(); } void Gfx::FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color) { @@ -33,7 +31,7 @@ void Gfx::FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t col state.color = color; state.taskToNotify = xTaskGetCurrentTaskHandle(); - lcd.DrawBuffer(x, y, w, h, reinterpret_cast(buffer), width * 2); + lcd.DrawBuffer(x, y, w, h, reinterpret_cast(buffer), width * 2); WaitTransferFinished(); } @@ -46,12 +44,12 @@ void Gfx::FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t* b) state.color = 0x00; state.taskToNotify = xTaskGetCurrentTaskHandle(); - lcd.DrawBuffer(x, y, w, h, reinterpret_cast(b), width * 2); + lcd.DrawBuffer(x, y, w, h, reinterpret_cast(b), width * 2); WaitTransferFinished(); } -void Gfx::DrawString(uint8_t x, uint8_t y, uint16_t color, const char *text, const FONT_INFO *p_font, bool wrap) { +void Gfx::DrawString(uint8_t x, uint8_t y, uint16_t color, const char* text, const FONT_INFO* p_font, bool wrap) { if (y > (height - p_font->height)) { // Not enough space to write even single char. return; @@ -86,7 +84,7 @@ void Gfx::DrawString(uint8_t x, uint8_t y, uint16_t color, const char *text, con } } -void Gfx::DrawChar(const FONT_INFO *font, uint8_t c, uint8_t *x, uint8_t y, uint16_t color) { +void Gfx::DrawChar(const FONT_INFO* font, uint8_t c, uint8_t* x, uint8_t y, uint16_t color) { uint8_t char_idx = c - font->startChar; uint16_t bytes_in_line = CEIL_DIV(font->charInfo[char_idx].widthBits, 8); uint16_t bg = 0x0000; @@ -100,10 +98,9 @@ void Gfx::DrawChar(const FONT_INFO *font, uint8_t c, uint8_t *x, uint8_t y, uint for (uint16_t j = 0; j < bytes_in_line; j++) { for (uint8_t k = 0; k < 8; k++) { if ((1 << (7 - k)) & font->data[font->charInfo[char_idx].offset + j]) { - buffer[(j*8)+k] = color; - } - else { - buffer[(j*8)+k] = bg; + buffer[(j * 8) + k] = color; + } else { + buffer[(j * 8) + k] = bg; } } } @@ -112,12 +109,12 @@ void Gfx::DrawChar(const FONT_INFO *font, uint8_t c, uint8_t *x, uint8_t y, uint state.currentIteration = 0; state.busy = true; state.action = Action::DrawChar; - state.font = const_cast(font); + state.font = const_cast(font); state.character = c; state.color = color; state.taskToNotify = xTaskGetCurrentTaskHandle(); - lcd.DrawBuffer(*x, y, bytes_in_line*8, font->height, reinterpret_cast(&buffer), bytes_in_line*8*2); + lcd.DrawBuffer(*x, y, bytes_in_line * 8, font->height, reinterpret_cast(&buffer), bytes_in_line * 8 * 2); WaitTransferFinished(); *x += font->charInfo[char_idx].widthBits + font->spacePixels; @@ -136,13 +133,14 @@ void Gfx::Wakeup() { } void Gfx::SetBackgroundColor(uint16_t color) { - for(int i = 0; i < width; i++) { + for (int i = 0; i < width; i++) { buffer[i] = color; } } -bool Gfx::GetNextBuffer(uint8_t **data, size_t &size) { - if(!state.busy) return false; +bool Gfx::GetNextBuffer(uint8_t** data, size_t& size) { + if (!state.busy) + return false; state.remainingIterations--; if (state.remainingIterations == 0) { state.busy = false; @@ -150,27 +148,26 @@ bool Gfx::GetNextBuffer(uint8_t **data, size_t &size) { return false; } - if(state.action == Action::FillRectangle) { - *data = reinterpret_cast(buffer); + if (state.action == Action::FillRectangle) { + *data = reinterpret_cast(buffer); size = width * 2; - } else if(state.action == Action::DrawChar) { + } else if (state.action == Action::DrawChar) { uint16_t bg = 0x0000; uint8_t char_idx = state.character - state.font->startChar; uint16_t bytes_in_line = CEIL_DIV(state.font->charInfo[char_idx].widthBits, 8); for (uint16_t j = 0; j < bytes_in_line; j++) { for (uint8_t k = 0; k < 8; k++) { - if ((1 << (7 - k)) & state.font->data[state.font->charInfo[char_idx].offset + ((state.currentIteration+1) * bytes_in_line) + j]) { - buffer[(j*8)+k] = state.color; - } - else { - buffer[(j*8)+k] = bg; + if ((1 << (7 - k)) & state.font->data[state.font->charInfo[char_idx].offset + ((state.currentIteration + 1) * bytes_in_line) + j]) { + buffer[(j * 8) + k] = state.color; + } else { + buffer[(j * 8) + k] = bg; } } } - *data = reinterpret_cast(buffer); - size = bytes_in_line*8*2; + *data = reinterpret_cast(buffer); + size = bytes_in_line * 8 * 2; } state.currentIteration++; @@ -179,7 +176,7 @@ bool Gfx::GetNextBuffer(uint8_t **data, size_t &size) { } void Gfx::NotifyEndOfTransfer(TaskHandle_t task) { - if(task != nullptr) { + if (task != nullptr) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; vTaskNotifyGiveFromISR(task, &xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken); diff --git a/src/components/gfx/Gfx.h b/src/components/gfx/Gfx.h index 4c0fc8ee..b45fb45c 100644 --- a/src/components/gfx/Gfx.h +++ b/src/components/gfx/Gfx.h @@ -12,49 +12,48 @@ namespace Pinetime { } namespace Components { class Gfx : public Pinetime::Drivers::BufferProvider { - public: - explicit Gfx(Drivers::St7789& lcd); - void Init(); - void ClearScreen(); - void DrawString(uint8_t x, uint8_t y, uint16_t color, const char* text, const FONT_INFO *p_font, bool wrap); - void DrawChar(const FONT_INFO *font, uint8_t c, uint8_t *x, uint8_t y, uint16_t color); - void FillRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color); - void FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t* b); - void SetScrollArea(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines); - void SetScrollStartLine(uint16_t line); - - - void Sleep(); - void Wakeup(); - bool GetNextBuffer(uint8_t **buffer, size_t &size) override; - void pixel_draw(uint8_t x, uint8_t y, uint16_t color); - - - private: - static constexpr uint8_t width = 240; - static constexpr uint8_t height = 240; - - enum class Action { None, FillRectangle, DrawChar}; - struct State { - State() : busy{false}, action{Action::None}, remainingIterations{0}, currentIteration{0} {} - volatile bool busy; - volatile Action action; - volatile uint16_t remainingIterations; - volatile uint16_t currentIteration; - volatile FONT_INFO *font; - volatile uint16_t color; - volatile uint8_t character; - volatile TaskHandle_t taskToNotify = nullptr; - }; - - volatile State state; - - uint16_t buffer[width]; // 1 line buffer - Drivers::St7789& lcd; - - void SetBackgroundColor(uint16_t color); - void WaitTransferFinished() const; - void NotifyEndOfTransfer(TaskHandle_t task); + public: + explicit Gfx(Drivers::St7789& lcd); + void Init(); + void ClearScreen(); + void DrawString(uint8_t x, uint8_t y, uint16_t color, const char* text, const FONT_INFO* p_font, bool wrap); + void DrawChar(const FONT_INFO* font, uint8_t c, uint8_t* x, uint8_t y, uint16_t color); + void FillRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color); + void FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t* b); + void SetScrollArea(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines); + void SetScrollStartLine(uint16_t line); + + void Sleep(); + void Wakeup(); + bool GetNextBuffer(uint8_t** buffer, size_t& size) override; + void pixel_draw(uint8_t x, uint8_t y, uint16_t color); + + private: + static constexpr uint8_t width = 240; + static constexpr uint8_t height = 240; + + enum class Action { None, FillRectangle, DrawChar }; + struct State { + State() : busy {false}, action {Action::None}, remainingIterations {0}, currentIteration {0} { + } + volatile bool busy; + volatile Action action; + volatile uint16_t remainingIterations; + volatile uint16_t currentIteration; + volatile FONT_INFO* font; + volatile uint16_t color; + volatile uint8_t character; + volatile TaskHandle_t taskToNotify = nullptr; + }; + + volatile State state; + + uint16_t buffer[width]; // 1 line buffer + Drivers::St7789& lcd; + + void SetBackgroundColor(uint16_t color); + void WaitTransferFinished() const; + void NotifyEndOfTransfer(TaskHandle_t task); }; } } diff --git a/src/components/heartrate/Biquad.cpp b/src/components/heartrate/Biquad.cpp index 6a4b8181..0341eda6 100644 --- a/src/components/heartrate/Biquad.cpp +++ b/src/components/heartrate/Biquad.cpp @@ -9,8 +9,7 @@ using namespace Pinetime::Controllers; /** Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/ppg.py */ -Biquad::Biquad(float b0, float b1, float b2, float a1, float a2) : b0{b0}, b1{b1}, b2{b2}, a1{a1}, a2{a2} { - +Biquad::Biquad(float b0, float b1, float b2, float a1, float a2) : b0 {b0}, b1 {b1}, b2 {b2}, a1 {a1}, a2 {a2} { } float Biquad::Step(float x) { diff --git a/src/components/heartrate/Biquad.h b/src/components/heartrate/Biquad.h index dc9b97f6..0d62b1fe 100644 --- a/src/components/heartrate/Biquad.h +++ b/src/components/heartrate/Biquad.h @@ -4,11 +4,11 @@ namespace Pinetime { namespace Controllers { /// Direct Form II Biquad Filter class Biquad { - public: - Biquad(float b0, float b1, float b2, float a1, float a2); + public: + Biquad(float b0, float b1, float b2, float a1, float a2); float Step(float x); - private: + private: float b0; float b1; float b2; diff --git a/src/components/heartrate/HeartRateController.cpp b/src/components/heartrate/HeartRateController.cpp index d0b0d513..e84d665a 100644 --- a/src/components/heartrate/HeartRateController.cpp +++ b/src/components/heartrate/HeartRateController.cpp @@ -4,38 +4,35 @@ using namespace Pinetime::Controllers; -HeartRateController::HeartRateController(Pinetime::System::SystemTask &systemTask) : systemTask{systemTask} { - +HeartRateController::HeartRateController(Pinetime::System::SystemTask& systemTask) : systemTask {systemTask} { } - void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) { this->state = newState; - if(this->heartRate != heartRate) { + if (this->heartRate != heartRate) { this->heartRate = heartRate; service->OnNewHeartRateValue(heartRate); } } void HeartRateController::Start() { - if(task != nullptr) { + if (task != nullptr) { state = States::NotEnoughData; task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement); } } void HeartRateController::Stop() { - if(task != nullptr) { + if (task != nullptr) { state = States::Stopped; task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement); } } -void HeartRateController::SetHeartRateTask(Pinetime::Applications::HeartRateTask *task) { +void HeartRateController::SetHeartRateTask(Pinetime::Applications::HeartRateTask* task) { this->task = task; } -void HeartRateController::SetService(Pinetime::Controllers::HeartRateService *service) { +void HeartRateController::SetService(Pinetime::Controllers::HeartRateService* service) { this->service = service; } - diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h index 001111b5..5558d355 100644 --- a/src/components/heartrate/HeartRateController.h +++ b/src/components/heartrate/HeartRateController.h @@ -12,8 +12,8 @@ namespace Pinetime { } namespace Controllers { class HeartRateController { - public: - enum class States { Stopped, NotEnoughData, NoTouch, Running}; + public: + enum class States { Stopped, NotEnoughData, NoTouch, Running }; explicit HeartRateController(System::SystemTask& systemTask); @@ -22,12 +22,16 @@ namespace Pinetime { void Update(States newState, uint8_t heartRate); void SetHeartRateTask(Applications::HeartRateTask* task); - States State() const { return state; } - uint8_t HeartRate() const { return heartRate; } + States State() const { + return state; + } + uint8_t HeartRate() const { + return heartRate; + } - void SetService(Pinetime::Controllers::HeartRateService *service); + void SetService(Pinetime::Controllers::HeartRateService* service); - private: + private: System::SystemTask& systemTask; Applications::HeartRateTask* task = nullptr; States state = States::Stopped; diff --git a/src/components/heartrate/Ppg.cpp b/src/components/heartrate/Ppg.cpp index e84cbdf3..578e2895 100644 --- a/src/components/heartrate/Ppg.cpp +++ b/src/components/heartrate/Ppg.cpp @@ -13,7 +13,7 @@ using namespace Pinetime::Controllers; namespace { int Compare(int* d1, int* d2, size_t count) { int e = 0; - for(size_t i = 0; i < count; i++) { + for (size_t i = 0; i < count; i++) { auto d = d1[i] - d2[i]; e += d * d; } @@ -21,15 +21,15 @@ namespace { } int CompareShift(int* d, int shift, size_t count) { - return Compare(d +shift, d, count - shift); + return Compare(d + shift, d, count - shift); } int Trough(int* d, size_t size, float mn, float mx) { - auto z2 = CompareShift(d, mn-2, size); - auto z1 = CompareShift(d, mn-1, size); - for(int i = mn; i < mx + 1; i++) { + auto z2 = CompareShift(d, mn - 2, size); + auto z1 = CompareShift(d, mn - 1, size); + for (int i = mn; i < mx + 1; i++) { auto z = CompareShift(d, i, size); - if(z2 > z1 && z1 < z) + if (z2 > z1 && z1 < z) return i; z2 = z1; z1 = z; @@ -38,11 +38,11 @@ namespace { } } -Ppg::Ppg(float spl) : offset{spl}, - hpf{0.87033078, -1.74066156, 0.87033078,-1.72377617, 0.75754694}, - agc{20, 0.971, 2}, - lpf{0.11595249, 0.23190498, 0.11595249,-0.72168143, 0.18549138} { - +Ppg::Ppg(float spl) + : offset {spl}, + hpf {0.87033078, -1.74066156, 0.87033078, -1.72377617, 0.75754694}, + agc {20, 0.971, 2}, + lpf {0.11595249, 0.23190498, 0.11595249, -0.72168143, 0.18549138} { } int Ppg::Preprocess(float spl) { @@ -53,13 +53,13 @@ int Ppg::Preprocess(float spl) { auto spl_int = static_cast(spl); - if(dataIndex < 200) + if (dataIndex < 200) data[dataIndex++] = spl_int; return spl_int; } float Ppg::HeartRate() { - if(dataIndex < 200) + if (dataIndex < 200) return 0; NRF_LOG_INFO("PREPROCESS, offset = %d", offset); @@ -71,26 +71,26 @@ float Ppg::HeartRate() { int cccount = 0; float Ppg::ProcessHeartRate() { - if(cccount > 2) + if (cccount > 2) asm("nop"); - cccount ++; + cccount++; auto t0 = Trough(data.data(), dataIndex, 7, 48); - if(t0 < 0) + if (t0 < 0) return 0; float t1 = t0 * 2; - t1 = Trough(data.data(), dataIndex, t1-5, t1+5); - if(t1 < 0) + t1 = Trough(data.data(), dataIndex, t1 - 5, t1 + 5); + if (t1 < 0) return 0; float t2 = static_cast(t1 * 3) / 2; t2 = Trough(data.data(), dataIndex, t2 - 5, t2 + 5); - if(t2 < 0) + if (t2 < 0) return 0; float t3 = static_cast(t2 * 4) / 3; t3 = Trough(data.data(), dataIndex, t3 - 4, t3 + 4); - if(t3 < 0) + if (t3 < 0) return static_cast(60 * 24 * 3) / static_cast(t2); return static_cast(60 * 24 * 4) / static_cast(t3); diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h index 747ae019..51db7582 100644 --- a/src/components/heartrate/Ppg.h +++ b/src/components/heartrate/Ppg.h @@ -7,7 +7,7 @@ namespace Pinetime { namespace Controllers { class Ppg { - public: + public: explicit Ppg(float spl); int Preprocess(float spl); @@ -16,7 +16,7 @@ namespace Pinetime { void SetOffset(uint16_t i); void Reset(); - private: + private: std::array data; size_t dataIndex = 0; float offset; @@ -24,7 +24,6 @@ namespace Pinetime { Ptagc agc; Biquad lpf; - float ProcessHeartRate(); }; } diff --git a/src/components/heartrate/Ptagc.cpp b/src/components/heartrate/Ptagc.cpp index dd7c4411..e358371e 100644 --- a/src/components/heartrate/Ptagc.cpp +++ b/src/components/heartrate/Ptagc.cpp @@ -10,17 +10,16 @@ using namespace Pinetime::Controllers; /** Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/ppg.py */ -Ptagc::Ptagc(float start, float decay, float threshold) : peak{start}, decay{decay}, boost{1.0f/decay}, threshold{threshold} { - +Ptagc::Ptagc(float start, float decay, float threshold) : peak {start}, decay {decay}, boost {1.0f / decay}, threshold {threshold} { } float Ptagc::Step(float spl) { - if(std::abs(spl) > peak) + if (std::abs(spl) > peak) peak *= boost; else peak *= decay; - if((spl > (peak * threshold)) || (spl < (peak * -threshold))) + if ((spl > (peak * threshold)) || (spl < (peak * -threshold))) return 0.0f; spl = 100.0f * spl / (2.0f * peak); diff --git a/src/components/heartrate/Ptagc.h b/src/components/heartrate/Ptagc.h index c20de4c0..aaa6f1e6 100644 --- a/src/components/heartrate/Ptagc.h +++ b/src/components/heartrate/Ptagc.h @@ -3,16 +3,15 @@ namespace Pinetime { namespace Controllers { class Ptagc { - public: + public: Ptagc(float start, float decay, float threshold); float Step(float spl); - private: + private: float peak; float decay; float boost; float threshold; - }; } } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 5881997f..1a551196 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -5,19 +5,29 @@ namespace Pinetime { namespace Controllers { class MotionController { - public: + public: void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps); - uint16_t X() const { return x; } - uint16_t Y() const { return y; } - uint16_t Z() const { return z; } - uint32_t NbSteps() const { return nbSteps; } + uint16_t X() const { + return x; + } + uint16_t Y() const { + return y; + } + uint16_t Z() const { + return z; + } + uint32_t NbSteps() const { + return nbSteps; + } bool ShouldWakeUp(bool isSleeping); void IsSensorOk(bool isOk); - bool IsSensorOk() const { return isSensorOk; } + bool IsSensorOk() const { + return isSensorOk; + } - private: + private: uint32_t nbSteps; int16_t x; int16_t y; diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp index 345234b3..a834ab6b 100644 --- a/src/components/motor/MotorController.cpp +++ b/src/components/motor/MotorController.cpp @@ -7,24 +7,26 @@ APP_TIMER_DEF(vibTimer); using namespace Pinetime::Controllers; -MotorController::MotorController( Controllers::Settings &settingsController ) : settingsController{settingsController} {} +MotorController::MotorController(Controllers::Settings& settingsController) : settingsController {settingsController} { +} void MotorController::Init() { - nrf_gpio_cfg_output(pinMotor); - nrf_gpio_pin_set(pinMotor); - app_timer_init(); - app_timer_create(&vibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrate); + nrf_gpio_cfg_output(pinMotor); + nrf_gpio_pin_set(pinMotor); + app_timer_init(); + app_timer_create(&vibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrate); } void MotorController::SetDuration(uint8_t motorDuration) { - if ( settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF ) return; - - nrf_gpio_pin_clear(pinMotor); - /* Start timer for motorDuration miliseconds and timer triggers vibrate() when it finishes*/ - app_timer_start(vibTimer, APP_TIMER_TICKS(motorDuration), NULL); + if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) + return; + + nrf_gpio_pin_clear(pinMotor); + /* Start timer for motorDuration miliseconds and timer triggers vibrate() when it finishes*/ + app_timer_start(vibTimer, APP_TIMER_TICKS(motorDuration), NULL); } -void MotorController::vibrate(void * p_context) { - nrf_gpio_pin_set(pinMotor); +void MotorController::vibrate(void* p_context) { + nrf_gpio_pin_set(pinMotor); } \ No newline at end of file diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h index 2f2e0343..4662a8da 100644 --- a/src/components/motor/MotorController.h +++ b/src/components/motor/MotorController.h @@ -9,14 +9,14 @@ namespace Pinetime { static constexpr uint8_t pinMotor = 16; class MotorController { - public: - MotorController( Controllers::Settings &settingsController ); - void Init(); - void SetDuration(uint8_t motorDuration); + public: + MotorController(Controllers::Settings& settingsController); + void Init(); + void SetDuration(uint8_t motorDuration); - private: - Controllers::Settings& settingsController; - static void vibrate(void * p_context); + private: + Controllers::Settings& settingsController; + static void vibrate(void* p_context); }; } } diff --git a/src/components/rle/RleDecoder.cpp b/src/components/rle/RleDecoder.cpp index 19a90fda..df2bcb6b 100644 --- a/src/components/rle/RleDecoder.cpp +++ b/src/components/rle/RleDecoder.cpp @@ -2,18 +2,16 @@ using namespace Pinetime::Tools; -RleDecoder::RleDecoder(const uint8_t *buffer, size_t size) : buffer{buffer}, size{size} { - +RleDecoder::RleDecoder(const uint8_t* buffer, size_t size) : buffer {buffer}, size {size} { } -RleDecoder::RleDecoder(const uint8_t *buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor) : RleDecoder{buffer, size} { +RleDecoder::RleDecoder(const uint8_t* buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor) : RleDecoder {buffer, size} { this->foregroundColor = foregroundColor; this->backgroundColor = backgroundColor; } - -void RleDecoder::DecodeNext(uint8_t *output, size_t maxBytes) { - for (;encodedBufferIndex> 8; @@ -36,4 +34,3 @@ void RleDecoder::DecodeNext(uint8_t *output, size_t maxBytes) { color = backgroundColor; } } - diff --git a/src/components/rle/RleDecoder.h b/src/components/rle/RleDecoder.h index 0f607fb8..c21f45c2 100644 --- a/src/components/rle/RleDecoder.h +++ b/src/components/rle/RleDecoder.h @@ -11,13 +11,13 @@ namespace Pinetime { * Code from https://github.com/daniel-thompson/wasp-bootloader by Daniel Thompson released under the MIT license. */ class RleDecoder { - public: + public: RleDecoder(const uint8_t* buffer, size_t size); RleDecoder(const uint8_t* buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor); void DecodeNext(uint8_t* output, size_t maxBytes); - private: + private: const uint8_t* buffer; size_t size; diff --git a/src/components/settings/Settings.cpp b/src/components/settings/Settings.cpp index 454df577..071940b8 100644 --- a/src/components/settings/Settings.cpp +++ b/src/components/settings/Settings.cpp @@ -5,110 +5,107 @@ using namespace Pinetime::Controllers; struct SettingsHeader { - uint8_t isActive; // 0xF1 = Block is active, 0xF0 = Block is inactive - uint16_t version; // Current version, to verify if the saved data is for the current Version + uint8_t isActive; // 0xF1 = Block is active, 0xF0 = Block is inactive + uint16_t version; // Current version, to verify if the saved data is for the current Version }; #define HEADER_SIZE sizeof(SettingsHeader) - -Settings::Settings( Pinetime::Drivers::SpiNorFlash &spiNorFlash ) : spiNorFlash{spiNorFlash} {} +Settings::Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash {spiNorFlash} { +} void Settings::Init() { - // Load default settings from Flash - LoadSettingsFromFlash(); - + // Load default settings from Flash + LoadSettingsFromFlash(); } void Settings::SaveSettings() { - // verify if is necessary to save - if ( settingsChanged ) { - SaveSettingsToFlash(); - } - settingsChanged = false; + // verify if is necessary to save + if (settingsChanged) { + SaveSettingsToFlash(); + } + settingsChanged = false; } - bool Settings::FindHeader() { - SettingsHeader settingsHeader; - uint8_t bufferHead[sizeof(settingsHeader)]; - - for (uint8_t block = 0; block < 10; block++) { - - spiNorFlash.Read( settingsBaseAddr + (block * 0x1000), bufferHead, sizeof(settingsHeader) ); - std::memcpy(&settingsHeader, bufferHead, sizeof(settingsHeader)); - if ( settingsHeader.isActive == 0xF1 && settingsHeader.version == settingsVersion ) { - settingsFlashBlock = block; - return true; - } + SettingsHeader settingsHeader; + uint8_t bufferHead[sizeof(settingsHeader)]; + + for (uint8_t block = 0; block < 10; block++) { + + spiNorFlash.Read(settingsBaseAddr + (block * 0x1000), bufferHead, sizeof(settingsHeader)); + std::memcpy(&settingsHeader, bufferHead, sizeof(settingsHeader)); + if (settingsHeader.isActive == 0xF1 && settingsHeader.version == settingsVersion) { + settingsFlashBlock = block; + return true; } - return false; + } + return false; } void Settings::ReadSettingsData() { - uint8_t bufferSettings[sizeof(settings)]; - spiNorFlash.Read( settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings) ); - std::memcpy(&settings, bufferSettings, sizeof(settings)); + uint8_t bufferSettings[sizeof(settings)]; + spiNorFlash.Read(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); + std::memcpy(&settings, bufferSettings, sizeof(settings)); } void Settings::EraseBlock() { - spiNorFlash.SectorErase(settingsBaseAddr + (settingsFlashBlock * 0x1000)); + spiNorFlash.SectorErase(settingsBaseAddr + (settingsFlashBlock * 0x1000)); } -void Settings::SetHeader( bool state ) { - SettingsHeader settingsHeader; - uint8_t bufferHead[sizeof(settingsHeader)]; - settingsHeader.isActive = state ? 0xF1 : 0xF0; - settingsHeader.version = settingsVersion; - - std::memcpy(bufferHead, &settingsHeader, sizeof(settingsHeader)); - spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000), bufferHead, sizeof(settingsHeader)); +void Settings::SetHeader(bool state) { + SettingsHeader settingsHeader; + uint8_t bufferHead[sizeof(settingsHeader)]; + settingsHeader.isActive = state ? 0xF1 : 0xF0; + settingsHeader.version = settingsVersion; + std::memcpy(bufferHead, &settingsHeader, sizeof(settingsHeader)); + spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000), bufferHead, sizeof(settingsHeader)); } void Settings::SaveSettingsData() { - uint8_t bufferSettings[sizeof(settings)]; - std::memcpy(bufferSettings, &settings, sizeof(settings)); - spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); + uint8_t bufferSettings[sizeof(settings)]; + std::memcpy(bufferSettings, &settings, sizeof(settings)); + spiNorFlash.Write(settingsBaseAddr + (settingsFlashBlock * 0x1000) + HEADER_SIZE, bufferSettings, sizeof(settings)); } void Settings::LoadSettingsFromFlash() { - if ( settingsFlashBlock == 99 ) { - // Find current Block, if can't find use default settings and set block to 0 ans save ! - if ( FindHeader() ) { - ReadSettingsData(); - } else { - SaveSettingsToFlash(); - } + if (settingsFlashBlock == 99) { + // Find current Block, if can't find use default settings and set block to 0 ans save ! + if (FindHeader()) { + ReadSettingsData(); } else { - // Read Settings from flash... - // never used :) - ReadSettingsData(); + SaveSettingsToFlash(); } - + } else { + // Read Settings from flash... + // never used :) + ReadSettingsData(); + } } void Settings::SaveSettingsToFlash() { - - // calculate where to save... - // mark current to inactive - // erase the new location and save - // set settingsFlashBlock - // if first time hever, only saves to block 0 and set settingsFlashBlock + // calculate where to save... + // mark current to inactive + // erase the new location and save + // set settingsFlashBlock - if ( settingsFlashBlock != 99 ) { - SetHeader( false ); - } + // if first time hever, only saves to block 0 and set settingsFlashBlock + + if (settingsFlashBlock != 99) { + SetHeader(false); + } - settingsFlashBlock++; - if ( settingsFlashBlock > 9 ) settingsFlashBlock = 0; + settingsFlashBlock++; + if (settingsFlashBlock > 9) + settingsFlashBlock = 0; - EraseBlock(); - SetHeader( true ); - SaveSettingsData(); + EraseBlock(); + SetHeader(true); + SaveSettingsData(); } diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 9cbbee62..05d6e7ef 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -8,98 +8,121 @@ namespace Pinetime { namespace Controllers { class Settings { - public: - enum class ClockType {H24, H12}; - enum class Vibration {ON, OFF}; - enum class WakeUpMode {None, SingleTap, DoubleTap, RaiseWrist}; - - Settings( Pinetime::Drivers::SpiNorFlash &spiNorFlash ); - - void Init(); - void SaveSettings(); - - void SetClockFace( uint8_t face ) { - if ( face != settings.clockFace ) settingsChanged = true; - settings.clockFace = face; - }; - uint8_t GetClockFace() const { return settings.clockFace; }; - - void SetAppMenu( uint8_t menu ) { appMenu = menu; }; - uint8_t GetAppMenu() { return appMenu; }; - - void SetSettingsMenu( uint8_t menu ) { settingsMenu = menu; }; - uint8_t GetSettingsMenu() const { return settingsMenu; }; - - void SetClockType( ClockType clocktype ) { - if ( clocktype != settings.clockType ) settingsChanged = true; - settings.clockType = clocktype; - }; - ClockType GetClockType() const { return settings.clockType; }; - - void SetVibrationStatus( Vibration status ) { - if ( status != settings.vibrationStatus ) settingsChanged = true; - settings.vibrationStatus = status; - }; - Vibration GetVibrationStatus() const { return settings.vibrationStatus; }; - - void SetScreenTimeOut( uint32_t timeout ) { - if ( timeout != settings.screenTimeOut ) settingsChanged = true; - settings.screenTimeOut = timeout; - }; - uint32_t GetScreenTimeOut() const { return settings.screenTimeOut; }; - - void setWakeUpMode( WakeUpMode wakeUp ) { - if ( wakeUp != settings.wakeUpMode ) settingsChanged = true; - settings.wakeUpMode = wakeUp; - }; - WakeUpMode getWakeUpMode() const { return settings.wakeUpMode; }; - - void SetBrightness( Controllers::BrightnessController::Levels level ) { - if ( level != settings.brightLevel ) settingsChanged = true; - settings.brightLevel = level; - }; - Controllers::BrightnessController::Levels GetBrightness() const { return settings.brightLevel; }; - - private: - - Pinetime::Drivers::SpiNorFlash& spiNorFlash; - struct SettingsData { - - ClockType clockType = ClockType::H24; - Vibration vibrationStatus = Vibration::ON; - - uint8_t clockFace = 0; - - uint32_t stepsGoal = 1000; - uint32_t screenTimeOut = 15000; - - WakeUpMode wakeUpMode = WakeUpMode::None; - - Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; - - }; - - SettingsData settings; - bool settingsChanged = false; - - uint8_t appMenu = 0; - uint8_t settingsMenu = 0; - - // There are 10 blocks of reserved flash to save settings - // to minimize wear, the recording is done in a rotating way by the 10 blocks - uint8_t settingsFlashBlock = 99; // default to indicate it needs to find the active block - - static constexpr uint32_t settingsBaseAddr = 0x3F6000; // Flash Settings Location - static constexpr uint16_t settingsVersion = 0x0100; // Flash Settings Version - - bool FindHeader(); - void ReadSettingsData(); - void EraseBlock(); - void SetHeader( bool state ); - void SaveSettingsData(); - void LoadSettingsFromFlash(); - void SaveSettingsToFlash(); - + public: + enum class ClockType { H24, H12 }; + enum class Vibration { ON, OFF }; + enum class WakeUpMode { None, SingleTap, DoubleTap, RaiseWrist }; + + Settings(Pinetime::Drivers::SpiNorFlash& spiNorFlash); + + void Init(); + void SaveSettings(); + + void SetClockFace(uint8_t face) { + if (face != settings.clockFace) + settingsChanged = true; + settings.clockFace = face; + }; + uint8_t GetClockFace() const { + return settings.clockFace; + }; + + void SetAppMenu(uint8_t menu) { + appMenu = menu; + }; + uint8_t GetAppMenu() { + return appMenu; + }; + + void SetSettingsMenu(uint8_t menu) { + settingsMenu = menu; + }; + uint8_t GetSettingsMenu() const { + return settingsMenu; + }; + + void SetClockType(ClockType clocktype) { + if (clocktype != settings.clockType) + settingsChanged = true; + settings.clockType = clocktype; + }; + ClockType GetClockType() const { + return settings.clockType; + }; + + void SetVibrationStatus(Vibration status) { + if (status != settings.vibrationStatus) + settingsChanged = true; + settings.vibrationStatus = status; + }; + Vibration GetVibrationStatus() const { + return settings.vibrationStatus; + }; + + void SetScreenTimeOut(uint32_t timeout) { + if (timeout != settings.screenTimeOut) + settingsChanged = true; + settings.screenTimeOut = timeout; + }; + uint32_t GetScreenTimeOut() const { + return settings.screenTimeOut; + }; + + void setWakeUpMode(WakeUpMode wakeUp) { + if (wakeUp != settings.wakeUpMode) + settingsChanged = true; + settings.wakeUpMode = wakeUp; + }; + WakeUpMode getWakeUpMode() const { + return settings.wakeUpMode; + }; + + void SetBrightness(Controllers::BrightnessController::Levels level) { + if (level != settings.brightLevel) + settingsChanged = true; + settings.brightLevel = level; + }; + Controllers::BrightnessController::Levels GetBrightness() const { + return settings.brightLevel; + }; + + private: + Pinetime::Drivers::SpiNorFlash& spiNorFlash; + struct SettingsData { + + ClockType clockType = ClockType::H24; + Vibration vibrationStatus = Vibration::ON; + + uint8_t clockFace = 0; + + uint32_t stepsGoal = 1000; + uint32_t screenTimeOut = 15000; + + WakeUpMode wakeUpMode = WakeUpMode::None; + + Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; + }; + + SettingsData settings; + bool settingsChanged = false; + + uint8_t appMenu = 0; + uint8_t settingsMenu = 0; + + // There are 10 blocks of reserved flash to save settings + // to minimize wear, the recording is done in a rotating way by the 10 blocks + uint8_t settingsFlashBlock = 99; // default to indicate it needs to find the active block + + static constexpr uint32_t settingsBaseAddr = 0x3F6000; // Flash Settings Location + static constexpr uint16_t settingsVersion = 0x0100; // Flash Settings Version + + bool FindHeader(); + void ReadSettingsData(); + void EraseBlock(); + void SetHeader(bool state); + void SaveSettingsData(); + void LoadSettingsFromFlash(); + void SaveSettingsToFlash(); }; } } \ No newline at end of file -- cgit v1.2.3 From 69898545193a82f7d72c9f47c9d9de36167b157b Mon Sep 17 00:00:00 2001 From: Avamander Date: Sat, 24 Apr 2021 12:00:45 +0300 Subject: Changed access modified indentation --- .clang-format | 2 +- src/BootloaderVersion.h | 2 +- src/components/battery/BatteryController.h | 8 ++++---- src/components/ble/AlertNotificationClient.h | 4 ++-- src/components/ble/AlertNotificationService.h | 4 ++-- src/components/ble/BatteryInformationService.h | 4 ++-- src/components/ble/BleClient.h | 2 +- src/components/ble/BleController.h | 4 ++-- src/components/ble/CurrentTimeClient.h | 4 ++-- src/components/ble/CurrentTimeService.h | 4 ++-- src/components/ble/DeviceInformationService.h | 4 ++-- src/components/ble/DfuService.h | 14 +++++++------- src/components/ble/HeartRateService.h | 4 ++-- src/components/ble/ImmediateAlertService.h | 4 ++-- src/components/ble/MusicService.h | 4 ++-- src/components/ble/NavigationService.h | 4 ++-- src/components/ble/NimbleController.h | 4 ++-- src/components/ble/NotificationManager.h | 4 ++-- src/components/ble/ServiceDiscovery.h | 4 ++-- src/components/brightness/BrightnessController.h | 4 ++-- src/components/datetime/DateTimeController.h | 4 ++-- src/components/firmwarevalidator/FirmwareValidator.h | 4 ++-- src/components/gfx/Gfx.h | 4 ++-- src/components/heartrate/Biquad.h | 4 ++-- src/components/heartrate/HeartRateController.h | 4 ++-- src/components/heartrate/Ppg.h | 4 ++-- src/components/heartrate/Ptagc.h | 4 ++-- src/components/motion/MotionController.h | 4 ++-- src/components/motor/MotorController.h | 4 ++-- src/components/rle/RleDecoder.h | 4 ++-- src/components/settings/Settings.h | 4 ++-- src/displayapp/DisplayApp.h | 4 ++-- src/displayapp/DisplayAppRecovery.h | 4 ++-- src/displayapp/DummyLittleVgl.h | 2 +- src/displayapp/LittleVgl.h | 4 ++-- src/displayapp/screens/ApplicationList.h | 4 ++-- src/displayapp/screens/BatteryIcon.h | 2 +- src/displayapp/screens/BatteryInfo.h | 4 ++-- src/displayapp/screens/BleIcon.h | 2 +- src/displayapp/screens/Brightness.h | 4 ++-- src/displayapp/screens/Clock.h | 4 ++-- src/displayapp/screens/DropDownDemo.h | 4 ++-- src/displayapp/screens/FirmwareUpdate.cpp | 2 +- src/displayapp/screens/FirmwareUpdate.h | 4 ++-- src/displayapp/screens/FirmwareValidation.h | 4 ++-- src/displayapp/screens/FlashLight.h | 4 ++-- src/displayapp/screens/HeartRate.h | 4 ++-- src/displayapp/screens/InfiniPaint.h | 4 ++-- src/displayapp/screens/Label.h | 4 ++-- src/displayapp/screens/List.h | 4 ++-- src/displayapp/screens/Meter.h | 4 ++-- src/displayapp/screens/Motion.h | 4 ++-- src/displayapp/screens/Music.h | 4 ++-- src/displayapp/screens/Navigation.h | 4 ++-- src/displayapp/screens/NotificationIcon.h | 2 +- src/displayapp/screens/Notifications.h | 8 ++++---- src/displayapp/screens/Paddle.h | 4 ++-- src/displayapp/screens/Screen.h | 8 ++++---- src/displayapp/screens/ScreenList.h | 4 ++-- src/displayapp/screens/StopWatch.h | 6 +++--- src/displayapp/screens/SystemInfo.h | 4 ++-- src/displayapp/screens/Tile.h | 4 ++-- src/displayapp/screens/Twos.h | 4 ++-- src/displayapp/screens/WatchFaceAnalog.h | 4 ++-- src/displayapp/screens/WatchFaceDigital.h | 4 ++-- src/displayapp/screens/settings/QuickSettings.h | 4 ++-- src/displayapp/screens/settings/SettingDisplay.h | 4 ++-- src/displayapp/screens/settings/SettingTimeFormat.h | 4 ++-- src/displayapp/screens/settings/SettingWakeUp.h | 4 ++-- src/displayapp/screens/settings/SettingWatchFace.h | 4 ++-- src/displayapp/screens/settings/Settings.h | 4 ++-- src/drivers/Bma421.h | 4 ++-- src/drivers/BufferProvider.h | 2 +- src/drivers/Cst816s.h | 4 ++-- src/drivers/Hrs3300.h | 4 ++-- src/drivers/InternalFlash.h | 4 ++-- src/drivers/Spi.h | 4 ++-- src/drivers/SpiMaster.h | 4 ++-- src/drivers/SpiNorFlash.h | 4 ++-- src/drivers/St7789.h | 4 ++-- src/drivers/TwiMaster.h | 4 ++-- src/drivers/Watchdog.h | 8 ++++---- src/systemtask/SystemMonitor.h | 8 ++++---- src/systemtask/SystemTask.h | 4 ++-- 84 files changed, 175 insertions(+), 175 deletions(-) (limited to 'src/components') diff --git a/.clang-format b/.clang-format index 5ddb7206..7430d4d6 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,6 @@ --- Language: Cpp -AccessModifierOffset: 2 +AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignConsecutiveMacros: true AlignConsecutiveAssignments: false diff --git a/src/BootloaderVersion.h b/src/BootloaderVersion.h index f2fd9112..c1ede0f5 100644 --- a/src/BootloaderVersion.h +++ b/src/BootloaderVersion.h @@ -2,7 +2,7 @@ namespace Pinetime { class BootloaderVersion { - public: + public: static uint32_t Major(); static uint32_t Minor(); static uint32_t Patch(); diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index e5c734d2..04bcf6b8 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -12,7 +12,7 @@ namespace Pinetime { is given as the template parameter N. */ template class CircBuffer { - public: + public: CircBuffer() : arr {}, sz {}, cap {N}, head {} { } /** @@ -32,7 +32,7 @@ namespace Pinetime { return (sum / sz); } - private: + private: std::array arr; /**< internal array used to store the values*/ uint8_t sz; /**< The current size of the array.*/ uint8_t cap; /**< Total capacity of the CircBuffer.*/ @@ -40,7 +40,7 @@ namespace Pinetime { }; class Battery { - public: + public: Battery(); void Init(); @@ -61,7 +61,7 @@ namespace Pinetime { return isPowerPresent; } - private: + private: static Battery* instance; nrf_saadc_value_t saadc_value; diff --git a/src/components/ble/AlertNotificationClient.h b/src/components/ble/AlertNotificationClient.h index 469bd4af..dfba8143 100644 --- a/src/components/ble/AlertNotificationClient.h +++ b/src/components/ble/AlertNotificationClient.h @@ -19,7 +19,7 @@ namespace Pinetime { class NotificationManager; class AlertNotificationClient : public BleClient { - public: + public: explicit AlertNotificationClient(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); @@ -34,7 +34,7 @@ namespace Pinetime { void Reset(); void Discover(uint16_t connectionHandle, std::function lambda) override; - private: + private: static constexpr uint16_t ansServiceId {0x1811}; static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; diff --git a/src/components/ble/AlertNotificationService.h b/src/components/ble/AlertNotificationService.h index 66bb9be4..5c7d428c 100644 --- a/src/components/ble/AlertNotificationService.h +++ b/src/components/ble/AlertNotificationService.h @@ -20,7 +20,7 @@ namespace Pinetime { class NotificationManager; class AlertNotificationService { - public: + public: AlertNotificationService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); @@ -32,7 +32,7 @@ namespace Pinetime { enum class IncomingCallResponses : uint8_t { Reject = 0x00, Answer = 0x01, Mute = 0x02 }; - private: + private: enum class Categories : uint8_t { SimpleAlert = 0x00, Email = 0x01, diff --git a/src/components/ble/BatteryInformationService.h b/src/components/ble/BatteryInformationService.h index 59ed9c64..7d060909 100644 --- a/src/components/ble/BatteryInformationService.h +++ b/src/components/ble/BatteryInformationService.h @@ -12,13 +12,13 @@ namespace Pinetime { namespace Controllers { class Battery; class BatteryInformationService { - public: + public: BatteryInformationService(Controllers::Battery& batteryController); void Init(); int OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - private: + private: Controllers::Battery& batteryController; static constexpr uint16_t batteryInformationServiceId {0x180F}; static constexpr uint16_t batteryLevelId {0x2A19}; diff --git a/src/components/ble/BleClient.h b/src/components/ble/BleClient.h index d8a58e1c..6067c15f 100644 --- a/src/components/ble/BleClient.h +++ b/src/components/ble/BleClient.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Controllers { class BleClient { - public: + public: virtual void Discover(uint16_t connectionHandle, std::function lambda) = 0; }; } diff --git a/src/components/ble/BleController.h b/src/components/ble/BleController.h index 6f5c2367..2cba26a9 100644 --- a/src/components/ble/BleController.h +++ b/src/components/ble/BleController.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Controllers { class Ble { - public: + public: using BleAddress = std::array; enum class FirmwareUpdateStates { Idle, Running, Validated, Error }; enum class AddressTypes { Public, Random }; @@ -49,7 +49,7 @@ namespace Pinetime { addressType = t; } - private: + private: bool isConnected = false; bool isFirmwareUpdating = false; uint32_t firmwareUpdateTotalBytes = 0; diff --git a/src/components/ble/CurrentTimeClient.h b/src/components/ble/CurrentTimeClient.h index c48a016d..6424c035 100644 --- a/src/components/ble/CurrentTimeClient.h +++ b/src/components/ble/CurrentTimeClient.h @@ -12,7 +12,7 @@ namespace Pinetime { class DateTime; class CurrentTimeClient : public BleClient { - public: + public: explicit CurrentTimeClient(DateTime& dateTimeController); void Init(); void Reset(); @@ -27,7 +27,7 @@ namespace Pinetime { } void Discover(uint16_t connectionHandle, std::function lambda) override; - private: + private: typedef struct __attribute__((packed)) { uint16_t year; uint8_t month; diff --git a/src/components/ble/CurrentTimeService.h b/src/components/ble/CurrentTimeService.h index 0a9a18a8..ca87d970 100644 --- a/src/components/ble/CurrentTimeService.h +++ b/src/components/ble/CurrentTimeService.h @@ -12,13 +12,13 @@ namespace Pinetime { namespace Controllers { class CurrentTimeService { - public: + public: CurrentTimeService(DateTime& dateTimeController); void Init(); int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - private: + private: static constexpr uint16_t ctsId {0x1805}; static constexpr uint16_t ctsCharId {0x2a2b}; diff --git a/src/components/ble/DeviceInformationService.h b/src/components/ble/DeviceInformationService.h index 3ff48b6d..54b3e961 100644 --- a/src/components/ble/DeviceInformationService.h +++ b/src/components/ble/DeviceInformationService.h @@ -9,13 +9,13 @@ namespace Pinetime { namespace Controllers { class DeviceInformationService { - public: + public: DeviceInformationService(); void Init(); int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - private: + private: static constexpr uint16_t deviceInfoId {0x180a}; static constexpr uint16_t manufacturerNameId {0x2a29}; static constexpr uint16_t modelNumberId {0x2a24}; diff --git a/src/components/ble/DfuService.h b/src/components/ble/DfuService.h index b932d80f..4708a4a6 100644 --- a/src/components/ble/DfuService.h +++ b/src/components/ble/DfuService.h @@ -20,7 +20,7 @@ namespace Pinetime { class Ble; class DfuService { - public: + public: DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, Pinetime::Drivers::SpiNorFlash& spiNorFlash); @@ -30,24 +30,24 @@ namespace Pinetime { void Reset(); class NotificationManager { - public: + public: NotificationManager(); bool AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t* data, size_t size); void Send(uint16_t connection, uint16_t characteristicHandle, const uint8_t* data, const size_t s); - private: + private: TimerHandle_t timer; uint16_t connectionHandle = 0; uint16_t characteristicHandle = 0; size_t size = 0; uint8_t buffer[10]; - public: + public: void OnNotificationTimer(); void Reset(); }; class DfuImage { - public: + public: DfuImage(Pinetime::Drivers::SpiNorFlash& spiNorFlash) : spiNorFlash {spiNorFlash} { } void Init(size_t chunkSize, size_t totalSize, uint16_t expectedCrc); @@ -56,7 +56,7 @@ namespace Pinetime { bool Validate(); bool IsComplete(); - private: + private: Pinetime::Drivers::SpiNorFlash& spiNorFlash; static constexpr size_t bufferSize = 200; bool ready = false; @@ -73,7 +73,7 @@ namespace Pinetime { uint16_t ComputeCrc(uint8_t const* p_data, uint32_t size, uint16_t const* p_crc); }; - private: + private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::Ble& bleController; DfuImage dfuImage; diff --git a/src/components/ble/HeartRateService.h b/src/components/ble/HeartRateService.h index 7422445a..0b16703f 100644 --- a/src/components/ble/HeartRateService.h +++ b/src/components/ble/HeartRateService.h @@ -12,13 +12,13 @@ namespace Pinetime { namespace Controllers { class HeartRateController; class HeartRateService { - public: + public: HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController); void Init(); int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); void OnNewHeartRateValue(uint8_t hearRateValue); - private: + private: Pinetime::System::SystemTask& system; Controllers::HeartRateController& heartRateController; static constexpr uint16_t heartRateServiceId {0x180D}; diff --git a/src/components/ble/ImmediateAlertService.h b/src/components/ble/ImmediateAlertService.h index 6bd11bd7..1f778acd 100644 --- a/src/components/ble/ImmediateAlertService.h +++ b/src/components/ble/ImmediateAlertService.h @@ -12,14 +12,14 @@ namespace Pinetime { namespace Controllers { class NotificationManager; class ImmediateAlertService { - public: + public: enum class Levels : uint8_t { NoAlert = 0, MildAlert = 1, HighAlert = 2 }; ImmediateAlertService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); int OnAlertLevelChanged(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - private: + private: Pinetime::System::SystemTask& systemTask; NotificationManager& notificationManager; diff --git a/src/components/ble/MusicService.h b/src/components/ble/MusicService.h index 5416219d..5f5343e4 100644 --- a/src/components/ble/MusicService.h +++ b/src/components/ble/MusicService.h @@ -37,7 +37,7 @@ namespace Pinetime { namespace Controllers { class MusicService { - public: + public: explicit MusicService(Pinetime::System::SystemTask& system); void Init(); @@ -70,7 +70,7 @@ namespace Pinetime { enum MusicStatus { NotPlaying = 0x00, Playing = 0x01 }; - private: + private: static constexpr uint8_t msId[2] = {0x00, 0x00}; static constexpr uint8_t msEventCharId[2] = {0x01, 0x00}; static constexpr uint8_t msStatusCharId[2] = {0x02, 0x00}; diff --git a/src/components/ble/NavigationService.h b/src/components/ble/NavigationService.h index dc4f0a0d..5aab263c 100644 --- a/src/components/ble/NavigationService.h +++ b/src/components/ble/NavigationService.h @@ -37,7 +37,7 @@ namespace Pinetime { namespace Controllers { class NavigationService { - public: + public: explicit NavigationService(Pinetime::System::SystemTask& system); void Init(); @@ -52,7 +52,7 @@ namespace Pinetime { int getProgress(); - private: + private: static constexpr uint8_t navId[2] = {0x01, 0x00}; static constexpr uint8_t navFlagCharId[2] = {0x01, 0x00}; static constexpr uint8_t navNarrativeCharId[2] = {0x02, 0x00}; diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 6d4aa38a..5dd01e42 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -36,7 +36,7 @@ namespace Pinetime { class NimbleController { - public: + public: NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, @@ -71,7 +71,7 @@ namespace Pinetime { uint16_t connHandle(); - private: + private: static constexpr const char* deviceName = "InfiniTime"; Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::Ble& bleController; diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 8e8fb374..d4072cc2 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -8,7 +8,7 @@ namespace Pinetime { namespace Controllers { class NotificationManager { - public: + public: enum class Categories { Unknown, SimpleAlert, @@ -52,7 +52,7 @@ namespace Pinetime { }; size_t NbNotifications() const; - private: + private: Notification::Id GetNextId(); static constexpr uint8_t TotalNbNotifications = 5; std::array notifications; diff --git a/src/components/ble/ServiceDiscovery.h b/src/components/ble/ServiceDiscovery.h index 56bb3e44..fc3b38c0 100644 --- a/src/components/ble/ServiceDiscovery.h +++ b/src/components/ble/ServiceDiscovery.h @@ -8,12 +8,12 @@ namespace Pinetime { class BleClient; class ServiceDiscovery { - public: + public: ServiceDiscovery(std::array&& bleClients); void StartDiscovery(uint16_t connectionHandle); - private: + private: BleClient** clientIterator; std::array clients; void OnServiceDiscovered(uint16_t connectionHandle); diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h index e8c36e3b..c47158a9 100644 --- a/src/components/brightness/BrightnessController.h +++ b/src/components/brightness/BrightnessController.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Controllers { class BrightnessController { - public: + public: enum class Levels { Off, Low, Medium, High }; void Init(); @@ -21,7 +21,7 @@ namespace Pinetime { const char* GetIcon(); const char* ToString(); - private: + private: static constexpr uint8_t pinLcdBacklight1 = 14; static constexpr uint8_t pinLcdBacklight2 = 22; static constexpr uint8_t pinLcdBacklight3 = 23; diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 823442b2..d0ae727e 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -9,7 +9,7 @@ namespace Pinetime { } namespace Controllers { class DateTime { - public: + public: enum class Days : uint8_t { Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; enum class Months : uint8_t { Unknown, @@ -75,7 +75,7 @@ namespace Pinetime { return uptime; } - private: + private: System::SystemTask& systemTask; uint16_t year = 0; Months month = Months::Unknown; diff --git a/src/components/firmwarevalidator/FirmwareValidator.h b/src/components/firmwarevalidator/FirmwareValidator.h index 00b5a0bb..ce644f97 100644 --- a/src/components/firmwarevalidator/FirmwareValidator.h +++ b/src/components/firmwarevalidator/FirmwareValidator.h @@ -5,13 +5,13 @@ namespace Pinetime { namespace Controllers { class FirmwareValidator { - public: + public: void Validate(); bool IsValidated() const; void Reset(); - private: + private: static constexpr uint32_t validBitAdress {0x7BFE8}; static constexpr uint32_t validBitValue {1}; }; diff --git a/src/components/gfx/Gfx.h b/src/components/gfx/Gfx.h index b45fb45c..54c4a8b7 100644 --- a/src/components/gfx/Gfx.h +++ b/src/components/gfx/Gfx.h @@ -12,7 +12,7 @@ namespace Pinetime { } namespace Components { class Gfx : public Pinetime::Drivers::BufferProvider { - public: + public: explicit Gfx(Drivers::St7789& lcd); void Init(); void ClearScreen(); @@ -28,7 +28,7 @@ namespace Pinetime { bool GetNextBuffer(uint8_t** buffer, size_t& size) override; void pixel_draw(uint8_t x, uint8_t y, uint16_t color); - private: + private: static constexpr uint8_t width = 240; static constexpr uint8_t height = 240; diff --git a/src/components/heartrate/Biquad.h b/src/components/heartrate/Biquad.h index 0d62b1fe..7c8ca58f 100644 --- a/src/components/heartrate/Biquad.h +++ b/src/components/heartrate/Biquad.h @@ -4,11 +4,11 @@ namespace Pinetime { namespace Controllers { /// Direct Form II Biquad Filter class Biquad { - public: + public: Biquad(float b0, float b1, float b2, float a1, float a2); float Step(float x); - private: + private: float b0; float b1; float b2; diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h index 5558d355..d3a8460d 100644 --- a/src/components/heartrate/HeartRateController.h +++ b/src/components/heartrate/HeartRateController.h @@ -12,7 +12,7 @@ namespace Pinetime { } namespace Controllers { class HeartRateController { - public: + public: enum class States { Stopped, NotEnoughData, NoTouch, Running }; explicit HeartRateController(System::SystemTask& systemTask); @@ -31,7 +31,7 @@ namespace Pinetime { void SetService(Pinetime::Controllers::HeartRateService* service); - private: + private: System::SystemTask& systemTask; Applications::HeartRateTask* task = nullptr; States state = States::Stopped; diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h index 51db7582..6a2fcf18 100644 --- a/src/components/heartrate/Ppg.h +++ b/src/components/heartrate/Ppg.h @@ -7,7 +7,7 @@ namespace Pinetime { namespace Controllers { class Ppg { - public: + public: explicit Ppg(float spl); int Preprocess(float spl); @@ -16,7 +16,7 @@ namespace Pinetime { void SetOffset(uint16_t i); void Reset(); - private: + private: std::array data; size_t dataIndex = 0; float offset; diff --git a/src/components/heartrate/Ptagc.h b/src/components/heartrate/Ptagc.h index aaa6f1e6..3476636b 100644 --- a/src/components/heartrate/Ptagc.h +++ b/src/components/heartrate/Ptagc.h @@ -3,11 +3,11 @@ namespace Pinetime { namespace Controllers { class Ptagc { - public: + public: Ptagc(float start, float decay, float threshold); float Step(float spl); - private: + private: float peak; float decay; float boost; diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 1a551196..bf644812 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Controllers { class MotionController { - public: + public: void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps); uint16_t X() const { @@ -27,7 +27,7 @@ namespace Pinetime { return isSensorOk; } - private: + private: uint32_t nbSteps; int16_t x; int16_t y; diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h index 4662a8da..df61af78 100644 --- a/src/components/motor/MotorController.h +++ b/src/components/motor/MotorController.h @@ -9,12 +9,12 @@ namespace Pinetime { static constexpr uint8_t pinMotor = 16; class MotorController { - public: + public: MotorController(Controllers::Settings& settingsController); void Init(); void SetDuration(uint8_t motorDuration); - private: + private: Controllers::Settings& settingsController; static void vibrate(void* p_context); }; diff --git a/src/components/rle/RleDecoder.h b/src/components/rle/RleDecoder.h index c21f45c2..0f607fb8 100644 --- a/src/components/rle/RleDecoder.h +++ b/src/components/rle/RleDecoder.h @@ -11,13 +11,13 @@ namespace Pinetime { * Code from https://github.com/daniel-thompson/wasp-bootloader by Daniel Thompson released under the MIT license. */ class RleDecoder { - public: + public: RleDecoder(const uint8_t* buffer, size_t size); RleDecoder(const uint8_t* buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor); void DecodeNext(uint8_t* output, size_t maxBytes); - private: + private: const uint8_t* buffer; size_t size; diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 05d6e7ef..18c87fd4 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -8,7 +8,7 @@ namespace Pinetime { namespace Controllers { class Settings { - public: + public: enum class ClockType { H24, H12 }; enum class Vibration { ON, OFF }; enum class WakeUpMode { None, SingleTap, DoubleTap, RaiseWrist }; @@ -86,7 +86,7 @@ namespace Pinetime { return settings.brightLevel; }; - private: + private: Pinetime::Drivers::SpiNorFlash& spiNorFlash; struct SettingsData { diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 03afb4ca..ff5ddac8 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -35,7 +35,7 @@ namespace Pinetime { }; namespace Applications { class DisplayApp { - public: + public: enum class States { Idle, Running }; enum class FullRefreshDirections { None, Up, Down, Left, Right, LeftAnim, RightAnim }; enum class TouchModes { Gestures, Polling }; @@ -60,7 +60,7 @@ namespace Pinetime { void SetFullRefresh(FullRefreshDirections direction); void SetTouchMode(TouchModes mode); - private: + private: Pinetime::Drivers::St7789& lcd; Pinetime::Components::LittleVgl& lvgl; Pinetime::Drivers::Cst816S& touchPanel; diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h index b51d6b67..025be6c0 100644 --- a/src/displayapp/DisplayAppRecovery.h +++ b/src/displayapp/DisplayAppRecovery.h @@ -29,7 +29,7 @@ namespace Pinetime { }; namespace Applications { class DisplayApp { - public: + public: DisplayApp(Drivers::St7789& lcd, Components::LittleVgl& lvgl, Drivers::Cst816S&, @@ -45,7 +45,7 @@ namespace Pinetime { void Start(); void PushMessage(Pinetime::Applications::Display::Messages msg); - private: + private: TaskHandle_t taskHandle; static void Process(void* instance); void DisplayLogo(uint16_t color); diff --git a/src/displayapp/DummyLittleVgl.h b/src/displayapp/DummyLittleVgl.h index dda1f3b8..96cf153f 100644 --- a/src/displayapp/DummyLittleVgl.h +++ b/src/displayapp/DummyLittleVgl.h @@ -9,7 +9,7 @@ namespace Pinetime { namespace Components { class LittleVgl { - public: + public: enum class FullRefreshDirections { None, Up, Down }; LittleVgl(Pinetime::Drivers::St7789& lcd, Pinetime::Drivers::Cst816S& touchPanel) { } diff --git a/src/displayapp/LittleVgl.h b/src/displayapp/LittleVgl.h index b10b14fa..7f7b76e0 100644 --- a/src/displayapp/LittleVgl.h +++ b/src/displayapp/LittleVgl.h @@ -10,7 +10,7 @@ namespace Pinetime { namespace Components { class LittleVgl { - public: + public: enum class FullRefreshDirections { None, Up, Down, Left, Right, LeftAnim, RightAnim }; LittleVgl(Pinetime::Drivers::St7789& lcd, Pinetime::Drivers::Cst816S& touchPanel); @@ -24,7 +24,7 @@ namespace Pinetime { void SetFullRefresh(FullRefreshDirections direction); void SetNewTapEvent(uint16_t x, uint16_t y); - private: + private: void InitDisplay(); void InitTouchpad(); void InitTheme(); diff --git a/src/displayapp/screens/ApplicationList.h b/src/displayapp/screens/ApplicationList.h index 5d519355..88534ec4 100644 --- a/src/displayapp/screens/ApplicationList.h +++ b/src/displayapp/screens/ApplicationList.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class ApplicationList : public Screen { - public: + public: explicit ApplicationList(DisplayApp* app, Pinetime::Controllers::Settings& settingsController, Pinetime::Controllers::Battery& batteryController, @@ -21,7 +21,7 @@ namespace Pinetime { bool Refresh() override; bool OnTouchEvent(TouchEvents event) override; - private: + private: Controllers::Settings& settingsController; Pinetime::Controllers::Battery& batteryController; Controllers::DateTime& dateTimeController; diff --git a/src/displayapp/screens/BatteryIcon.h b/src/displayapp/screens/BatteryIcon.h index 2a40a9c1..9c192ff7 100644 --- a/src/displayapp/screens/BatteryIcon.h +++ b/src/displayapp/screens/BatteryIcon.h @@ -4,7 +4,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class BatteryIcon { - public: + public: static const char* GetUnknownIcon(); static const char* GetBatteryIcon(int batteryPercent); static const char* GetPlugIcon(bool isCharging); diff --git a/src/displayapp/screens/BatteryInfo.h b/src/displayapp/screens/BatteryInfo.h index e95267bd..8805db58 100644 --- a/src/displayapp/screens/BatteryInfo.h +++ b/src/displayapp/screens/BatteryInfo.h @@ -15,7 +15,7 @@ namespace Pinetime { namespace Screens { class BatteryInfo : public Screen { - public: + public: BatteryInfo(DisplayApp* app, Pinetime::Controllers::Battery& batteryController); ~BatteryInfo() override; @@ -24,7 +24,7 @@ namespace Pinetime { void UpdateScreen(); void UpdateAnim(); - private: + private: Pinetime::Controllers::Battery& batteryController; lv_obj_t* voltage; diff --git a/src/displayapp/screens/BleIcon.h b/src/displayapp/screens/BleIcon.h index 5fe139aa..c1398d2a 100644 --- a/src/displayapp/screens/BleIcon.h +++ b/src/displayapp/screens/BleIcon.h @@ -4,7 +4,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class BleIcon { - public: + public: static const char* GetIcon(bool isConnected); }; } diff --git a/src/displayapp/screens/Brightness.h b/src/displayapp/screens/Brightness.h index e2a5e7a1..9ee33753 100644 --- a/src/displayapp/screens/Brightness.h +++ b/src/displayapp/screens/Brightness.h @@ -9,7 +9,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class Brightness : public Screen { - public: + public: Brightness(DisplayApp* app, Controllers::BrightnessController& brightness); ~Brightness() override; bool Refresh() override; @@ -18,7 +18,7 @@ namespace Pinetime { void OnValueChanged(); - private: + private: Controllers::BrightnessController& brightness; lv_obj_t* slider_label; diff --git a/src/displayapp/screens/Clock.h b/src/displayapp/screens/Clock.h index ef008c8d..9879985f 100644 --- a/src/displayapp/screens/Clock.h +++ b/src/displayapp/screens/Clock.h @@ -23,7 +23,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class Clock : public Screen { - public: + public: Clock(DisplayApp* app, Controllers::DateTime& dateTimeController, Controllers::Battery& batteryController, @@ -38,7 +38,7 @@ namespace Pinetime { bool OnTouchEvent(TouchEvents event) override; - private: + private: Controllers::DateTime& dateTimeController; Controllers::Battery& batteryController; Controllers::Ble& bleController; diff --git a/src/displayapp/screens/DropDownDemo.h b/src/displayapp/screens/DropDownDemo.h index 94be89e4..ff388c57 100644 --- a/src/displayapp/screens/DropDownDemo.h +++ b/src/displayapp/screens/DropDownDemo.h @@ -9,7 +9,7 @@ namespace Pinetime { namespace Screens { class DropDownDemo : public Screen { - public: + public: DropDownDemo(DisplayApp* app); ~DropDownDemo() override; @@ -17,7 +17,7 @@ namespace Pinetime { bool OnTouchEvent(TouchEvents event) override; - private: + private: lv_obj_t* ddlist; bool isDropDownOpened = false; diff --git a/src/displayapp/screens/FirmwareUpdate.cpp b/src/displayapp/screens/FirmwareUpdate.cpp index 1ca9d7ce..4086b152 100644 --- a/src/displayapp/screens/FirmwareUpdate.cpp +++ b/src/displayapp/screens/FirmwareUpdate.cpp @@ -8,7 +8,7 @@ using namespace Pinetime::Applications::Screens; FirmwareUpdate::FirmwareUpdate(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Ble& bleController) : Screen(app), bleController {bleController} { - lv_obj_t * backgroundLabel = lv_label_create(lv_scr_act(), nullptr); + lv_obj_t* backgroundLabel = lv_label_create(lv_scr_act(), nullptr); lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP); lv_obj_set_size(backgroundLabel, 240, 240); lv_obj_set_pos(backgroundLabel, 0, 0); diff --git a/src/displayapp/screens/FirmwareUpdate.h b/src/displayapp/screens/FirmwareUpdate.h index 7e642477..f4d34df0 100644 --- a/src/displayapp/screens/FirmwareUpdate.h +++ b/src/displayapp/screens/FirmwareUpdate.h @@ -11,13 +11,13 @@ namespace Pinetime { namespace Screens { class FirmwareUpdate : public Screen { - public: + public: FirmwareUpdate(DisplayApp* app, Pinetime::Controllers::Ble& bleController); ~FirmwareUpdate() override; bool Refresh() override; - private: + private: enum class States { Idle, Running, Validated, Error }; Pinetime::Controllers::Ble& bleController; lv_obj_t* bar1; diff --git a/src/displayapp/screens/FirmwareValidation.h b/src/displayapp/screens/FirmwareValidation.h index f35a86b5..67662fd9 100644 --- a/src/displayapp/screens/FirmwareValidation.h +++ b/src/displayapp/screens/FirmwareValidation.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Screens { class FirmwareValidation : public Screen { - public: + public: FirmwareValidation(DisplayApp* app, Pinetime::Controllers::FirmwareValidator& validator); ~FirmwareValidation() override; @@ -20,7 +20,7 @@ namespace Pinetime { void OnButtonEvent(lv_obj_t* object, lv_event_t event); - private: + private: Pinetime::Controllers::FirmwareValidator& validator; lv_obj_t* labelVersionInfo; diff --git a/src/displayapp/screens/FlashLight.h b/src/displayapp/screens/FlashLight.h index 5bd188de..a862ffdb 100644 --- a/src/displayapp/screens/FlashLight.h +++ b/src/displayapp/screens/FlashLight.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Screens { class FlashLight : public Screen { - public: + public: FlashLight(DisplayApp* app, System::SystemTask& systemTask, Controllers::BrightnessController& brightness); ~FlashLight() override; @@ -21,7 +21,7 @@ namespace Pinetime { bool OnTouchEvent(Pinetime::Applications::TouchEvents event) override; void OnClickEvent(lv_obj_t* obj, lv_event_t event); - private: + private: Pinetime::System::SystemTask& systemTask; Controllers::BrightnessController& brightness; diff --git a/src/displayapp/screens/HeartRate.h b/src/displayapp/screens/HeartRate.h index 25be427f..a23c5af8 100644 --- a/src/displayapp/screens/HeartRate.h +++ b/src/displayapp/screens/HeartRate.h @@ -16,7 +16,7 @@ namespace Pinetime { namespace Screens { class HeartRate : public Screen { - public: + public: HeartRate(DisplayApp* app, Controllers::HeartRateController& HeartRateController, System::SystemTask& systemTask); ~HeartRate() override; @@ -24,7 +24,7 @@ namespace Pinetime { void OnStartStopEvent(lv_event_t event); - private: + private: Controllers::HeartRateController& heartRateController; Pinetime::System::SystemTask& systemTask; void UpdateStartStopButton(bool isRunning); diff --git a/src/displayapp/screens/InfiniPaint.h b/src/displayapp/screens/InfiniPaint.h index 220212b1..6251752a 100644 --- a/src/displayapp/screens/InfiniPaint.h +++ b/src/displayapp/screens/InfiniPaint.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Screens { class InfiniPaint : public Screen { - public: + public: InfiniPaint(DisplayApp* app, Pinetime::Components::LittleVgl& lvgl); ~InfiniPaint() override; @@ -23,7 +23,7 @@ namespace Pinetime { bool OnTouchEvent(uint16_t x, uint16_t y) override; - private: + private: Pinetime::Components::LittleVgl& lvgl; static constexpr uint16_t width = 10; static constexpr uint16_t height = 10; diff --git a/src/displayapp/screens/Label.h b/src/displayapp/screens/Label.h index b648fdb6..62b80bec 100644 --- a/src/displayapp/screens/Label.h +++ b/src/displayapp/screens/Label.h @@ -8,13 +8,13 @@ namespace Pinetime { namespace Screens { class Label : public Screen { - public: + public: Label(uint8_t screenID, uint8_t numScreens, DisplayApp* app, lv_obj_t* labelText); ~Label() override; bool Refresh() override; - private: + private: bool running = true; lv_obj_t* labelText = nullptr; diff --git a/src/displayapp/screens/List.h b/src/displayapp/screens/List.h index 4c2973aa..a45fd1d3 100644 --- a/src/displayapp/screens/List.h +++ b/src/displayapp/screens/List.h @@ -13,7 +13,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class List : public Screen { - public: + public: struct Applications { const char* icon; const char* name; @@ -31,7 +31,7 @@ namespace Pinetime { void OnButtonEvent(lv_obj_t* object, lv_event_t event); - private: + private: Controllers::Settings& settingsController; Pinetime::Applications::Apps apps[MAXLISTITEMS]; diff --git a/src/displayapp/screens/Meter.h b/src/displayapp/screens/Meter.h index 970e4233..24af15ad 100644 --- a/src/displayapp/screens/Meter.h +++ b/src/displayapp/screens/Meter.h @@ -10,13 +10,13 @@ namespace Pinetime { namespace Screens { class Meter : public Screen { - public: + public: Meter(DisplayApp* app); ~Meter() override; bool Refresh() override; - private: + private: lv_style_t style_lmeter; lv_obj_t* lmeter; diff --git a/src/displayapp/screens/Motion.h b/src/displayapp/screens/Motion.h index a06a5d1c..11007866 100644 --- a/src/displayapp/screens/Motion.h +++ b/src/displayapp/screens/Motion.h @@ -13,14 +13,14 @@ namespace Pinetime { namespace Screens { class Motion : public Screen { - public: + public: Motion(DisplayApp* app, Controllers::MotionController& motionController); ~Motion() override; bool Refresh() override; bool OnButtonPushed() override; - private: + private: Controllers::MotionController& motionController; lv_obj_t* chart; lv_chart_series_t* ser1; diff --git a/src/displayapp/screens/Music.h b/src/displayapp/screens/Music.h index c7da233e..ef8f1fec 100644 --- a/src/displayapp/screens/Music.h +++ b/src/displayapp/screens/Music.h @@ -30,7 +30,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class Music : public Screen { - public: + public: Music(DisplayApp* app, Pinetime::Controllers::MusicService& music); ~Music() override; @@ -39,7 +39,7 @@ namespace Pinetime { void OnObjectEvent(lv_obj_t* obj, lv_event_t event); - private: + private: bool OnTouchEvent(TouchEvents event); void UpdateLength(); diff --git a/src/displayapp/screens/Navigation.h b/src/displayapp/screens/Navigation.h index 65a87047..46816c33 100644 --- a/src/displayapp/screens/Navigation.h +++ b/src/displayapp/screens/Navigation.h @@ -31,13 +31,13 @@ namespace Pinetime { namespace Applications { namespace Screens { class Navigation : public Screen { - public: + public: Navigation(DisplayApp* app, Pinetime::Controllers::NavigationService& nav); ~Navigation() override; bool Refresh() override; - private: + private: lv_obj_t* imgFlag; lv_obj_t* txtNarrative; lv_obj_t* txtManDist; diff --git a/src/displayapp/screens/NotificationIcon.h b/src/displayapp/screens/NotificationIcon.h index 40546397..dc34c3f0 100644 --- a/src/displayapp/screens/NotificationIcon.h +++ b/src/displayapp/screens/NotificationIcon.h @@ -4,7 +4,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class NotificationIcon { - public: + public: static const char* GetIcon(bool newNotificationAvailable); }; } diff --git a/src/displayapp/screens/Notifications.h b/src/displayapp/screens/Notifications.h index d5ea5dcb..51ca81da 100644 --- a/src/displayapp/screens/Notifications.h +++ b/src/displayapp/screens/Notifications.h @@ -14,7 +14,7 @@ namespace Pinetime { namespace Screens { class Notifications : public Screen { - public: + public: enum class Modes { Normal, Preview }; explicit Notifications(DisplayApp* app, Pinetime::Controllers::NotificationManager& notificationManager, @@ -26,7 +26,7 @@ namespace Pinetime { bool OnTouchEvent(Pinetime::Applications::TouchEvents event) override; class NotificationItem { - public: + public: NotificationItem(const char* title, const char* msg, uint8_t notifNr, @@ -42,7 +42,7 @@ namespace Pinetime { void OnMuteIncomingCall(lv_event_t event); void OnRejectIncomingCall(lv_event_t event); - private: + private: uint8_t notifNr = 0; uint8_t notifNb = 0; char pageText[4]; @@ -62,7 +62,7 @@ namespace Pinetime { Pinetime::Controllers::AlertNotificationService& alertNotificationService; }; - private: + private: struct NotificationData { const char* title; const char* text; diff --git a/src/displayapp/screens/Paddle.h b/src/displayapp/screens/Paddle.h index 438b4992..e133244f 100644 --- a/src/displayapp/screens/Paddle.h +++ b/src/displayapp/screens/Paddle.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Screens { class Paddle : public Screen { - public: + public: Paddle(DisplayApp* app, Pinetime::Components::LittleVgl& lvgl); ~Paddle() override; @@ -21,7 +21,7 @@ namespace Pinetime { bool OnTouchEvent(TouchEvents event) override; bool OnTouchEvent(uint16_t x, uint16_t y) override; - private: + private: Pinetime::Components::LittleVgl& lvgl; int paddleBottomY = 90; // bottom extreme of the paddle diff --git a/src/displayapp/screens/Screen.h b/src/displayapp/screens/Screen.h index fb453aa8..8e49c9de 100644 --- a/src/displayapp/screens/Screen.h +++ b/src/displayapp/screens/Screen.h @@ -9,7 +9,7 @@ namespace Pinetime { namespace Screens { template class DirtyValue { - public: + public: DirtyValue() = default; // Use NSDMI explicit DirtyValue(T const& v) : value {v} { } // Use MIL and const-lvalue-ref @@ -28,13 +28,13 @@ namespace Pinetime { return *this; } - private: + private: T value {}; // NSDMI - default initialise type bool isUpdated {true}; // NSDMI - use brace initilisation }; class Screen { - public: + public: explicit Screen(DisplayApp* app) : app {app} { } virtual ~Screen() = default; @@ -67,7 +67,7 @@ namespace Pinetime { return false; } - protected: + protected: DisplayApp* app; bool running = true; }; diff --git a/src/displayapp/screens/ScreenList.h b/src/displayapp/screens/ScreenList.h index a9a176b3..73ea4610 100644 --- a/src/displayapp/screens/ScreenList.h +++ b/src/displayapp/screens/ScreenList.h @@ -12,7 +12,7 @@ namespace Pinetime { enum class ScreenListModes { UpDown, RightLeft, LongPress }; template class ScreenList : public Screen { - public: + public: ScreenList(DisplayApp* app, uint8_t initScreen, std::array()>, N>&& screens, @@ -95,7 +95,7 @@ namespace Pinetime { return false; } - private: + private: uint8_t initScreen = 0; std::array()>, N> screens; ScreenListModes mode = ScreenListModes::UpDown; diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 18b02069..ff604361 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -54,7 +54,7 @@ namespace Pinetime::Applications::Screens { return nullptr; } - private: + private: std::array buffer; uint8_t currentSize; uint8_t capacity; @@ -62,7 +62,7 @@ namespace Pinetime::Applications::Screens { }; class StopWatch : public Screen { - public: + public: StopWatch(DisplayApp* app); ~StopWatch() override; bool Refresh() override; @@ -70,7 +70,7 @@ namespace Pinetime::Applications::Screens { void playPauseBtnEventHandler(lv_event_t event); void stopLapBtnEventHandler(lv_event_t event); - private: + private: bool running; States currentState; Events currentEvent; diff --git a/src/displayapp/screens/SystemInfo.h b/src/displayapp/screens/SystemInfo.h index 463b8b9c..c0c65554 100644 --- a/src/displayapp/screens/SystemInfo.h +++ b/src/displayapp/screens/SystemInfo.h @@ -21,7 +21,7 @@ namespace Pinetime { namespace Screens { class SystemInfo : public Screen { - public: + public: explicit SystemInfo(DisplayApp* app, Pinetime::Controllers::DateTime& dateTimeController, Pinetime::Controllers::Battery& batteryController, @@ -33,7 +33,7 @@ namespace Pinetime { bool OnButtonPushed() override; bool OnTouchEvent(TouchEvents event) override; - private: + private: bool running = true; Pinetime::Controllers::DateTime& dateTimeController; diff --git a/src/displayapp/screens/Tile.h b/src/displayapp/screens/Tile.h index a372a852..4ebd81cd 100644 --- a/src/displayapp/screens/Tile.h +++ b/src/displayapp/screens/Tile.h @@ -14,7 +14,7 @@ namespace Pinetime { namespace Applications { namespace Screens { class Tile : public Screen { - public: + public: struct Applications { const char* icon; Pinetime::Applications::Apps application; @@ -34,7 +34,7 @@ namespace Pinetime { void UpdateScreen(); void OnObjectEvent(lv_obj_t* obj, lv_event_t event, uint32_t buttonId); - private: + private: Pinetime::Controllers::Battery& batteryController; Controllers::DateTime& dateTimeController; diff --git a/src/displayapp/screens/Twos.h b/src/displayapp/screens/Twos.h index ade5abb0..3367618f 100644 --- a/src/displayapp/screens/Twos.h +++ b/src/displayapp/screens/Twos.h @@ -11,14 +11,14 @@ namespace Pinetime { }; namespace Screens { class Twos : public Screen { - public: + public: Twos(DisplayApp* app); ~Twos() override; bool Refresh() override; bool OnTouchEvent(TouchEvents event) override; - private: + private: lv_style_t style_cell1; lv_style_t style_cell2; lv_style_t style_cell3; diff --git a/src/displayapp/screens/WatchFaceAnalog.h b/src/displayapp/screens/WatchFaceAnalog.h index 8957aca8..667f6241 100644 --- a/src/displayapp/screens/WatchFaceAnalog.h +++ b/src/displayapp/screens/WatchFaceAnalog.h @@ -22,7 +22,7 @@ namespace Pinetime { namespace Screens { class WatchFaceAnalog : public Screen { - public: + public: WatchFaceAnalog(DisplayApp* app, Controllers::DateTime& dateTimeController, Controllers::Battery& batteryController, @@ -34,7 +34,7 @@ namespace Pinetime { bool Refresh() override; - private: + private: uint8_t sHour, sMinute, sSecond; uint8_t hour; uint8_t minute; diff --git a/src/displayapp/screens/WatchFaceDigital.h b/src/displayapp/screens/WatchFaceDigital.h index fb6d9cbb..246efc95 100644 --- a/src/displayapp/screens/WatchFaceDigital.h +++ b/src/displayapp/screens/WatchFaceDigital.h @@ -22,7 +22,7 @@ namespace Pinetime { namespace Screens { class WatchFaceDigital : public Screen { - public: + public: WatchFaceDigital(DisplayApp* app, Controllers::DateTime& dateTimeController, Controllers::Battery& batteryController, @@ -37,7 +37,7 @@ namespace Pinetime { void OnObjectEvent(lv_obj_t* pObj, lv_event_t i); - private: + private: char displayedChar[5]; uint16_t currentYear = 1970; diff --git a/src/displayapp/screens/settings/QuickSettings.h b/src/displayapp/screens/settings/QuickSettings.h index 693fd757..8d04bec3 100644 --- a/src/displayapp/screens/settings/QuickSettings.h +++ b/src/displayapp/screens/settings/QuickSettings.h @@ -16,7 +16,7 @@ namespace Pinetime { namespace Screens { class QuickSettings : public Screen { - public: + public: QuickSettings(DisplayApp* app, Pinetime::Controllers::Battery& batteryController, Controllers::DateTime& dateTimeController, @@ -32,7 +32,7 @@ namespace Pinetime { void UpdateScreen(); - private: + private: Pinetime::Controllers::Battery& batteryController; Controllers::DateTime& dateTimeController; Controllers::BrightnessController& brightness; diff --git a/src/displayapp/screens/settings/SettingDisplay.h b/src/displayapp/screens/settings/SettingDisplay.h index ecd84465..b8ed87ec 100644 --- a/src/displayapp/screens/settings/SettingDisplay.h +++ b/src/displayapp/screens/settings/SettingDisplay.h @@ -11,14 +11,14 @@ namespace Pinetime { namespace Screens { class SettingDisplay : public Screen { - public: + public: SettingDisplay(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); ~SettingDisplay() override; bool Refresh() override; void UpdateSelected(lv_obj_t* object, lv_event_t event); - private: + private: Controllers::Settings& settingsController; uint8_t optionsTotal; lv_obj_t* cbOption[4]; diff --git a/src/displayapp/screens/settings/SettingTimeFormat.h b/src/displayapp/screens/settings/SettingTimeFormat.h index df08a944..9203b45b 100644 --- a/src/displayapp/screens/settings/SettingTimeFormat.h +++ b/src/displayapp/screens/settings/SettingTimeFormat.h @@ -11,14 +11,14 @@ namespace Pinetime { namespace Screens { class SettingTimeFormat : public Screen { - public: + public: SettingTimeFormat(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); ~SettingTimeFormat() override; bool Refresh() override; void UpdateSelected(lv_obj_t* object, lv_event_t event); - private: + private: Controllers::Settings& settingsController; uint8_t optionsTotal; lv_obj_t* cbOption[2]; diff --git a/src/displayapp/screens/settings/SettingWakeUp.h b/src/displayapp/screens/settings/SettingWakeUp.h index 4f476b0c..8b33eb06 100644 --- a/src/displayapp/screens/settings/SettingWakeUp.h +++ b/src/displayapp/screens/settings/SettingWakeUp.h @@ -11,14 +11,14 @@ namespace Pinetime { namespace Screens { class SettingWakeUp : public Screen { - public: + public: SettingWakeUp(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); ~SettingWakeUp() override; bool Refresh() override; void UpdateSelected(lv_obj_t* object, lv_event_t event); - private: + private: Controllers::Settings& settingsController; uint8_t optionsTotal; lv_obj_t* cbOption[4]; diff --git a/src/displayapp/screens/settings/SettingWatchFace.h b/src/displayapp/screens/settings/SettingWatchFace.h index 0963b4ca..1930a228 100644 --- a/src/displayapp/screens/settings/SettingWatchFace.h +++ b/src/displayapp/screens/settings/SettingWatchFace.h @@ -11,14 +11,14 @@ namespace Pinetime { namespace Screens { class SettingWatchFace : public Screen { - public: + public: SettingWatchFace(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); ~SettingWatchFace() override; bool Refresh() override; void UpdateSelected(lv_obj_t* object, lv_event_t event); - private: + private: Controllers::Settings& settingsController; uint8_t optionsTotal; lv_obj_t* cbOption[2]; diff --git a/src/displayapp/screens/settings/Settings.h b/src/displayapp/screens/settings/Settings.h index 1cec2b09..7e332dfe 100644 --- a/src/displayapp/screens/settings/Settings.h +++ b/src/displayapp/screens/settings/Settings.h @@ -10,7 +10,7 @@ namespace Pinetime { namespace Screens { class Settings : public Screen { - public: + public: Settings(DisplayApp* app, Pinetime::Controllers::Settings& settingsController); ~Settings() override; @@ -19,7 +19,7 @@ namespace Pinetime { void OnButtonEvent(lv_obj_t* object, lv_event_t event); bool OnTouchEvent(Pinetime::Applications::TouchEvents event) override; - private: + private: Controllers::Settings& settingsController; ScreenList<2> screens; diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index 3287ed9e..e4d925f5 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Drivers { class TwiMaster; class Bma421 { - public: + public: struct Values { uint32_t steps; int16_t x; @@ -30,7 +30,7 @@ namespace Pinetime { bool IsOk() const; - private: + private: void Reset(); TwiMaster& twiMaster; diff --git a/src/drivers/BufferProvider.h b/src/drivers/BufferProvider.h index fac03bdc..53571133 100644 --- a/src/drivers/BufferProvider.h +++ b/src/drivers/BufferProvider.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Drivers { class BufferProvider { - public: + public: virtual bool GetNextBuffer(uint8_t** buffer, size_t& size) = 0; }; } diff --git a/src/drivers/Cst816s.h b/src/drivers/Cst816s.h index 7664a83e..14c296ea 100644 --- a/src/drivers/Cst816s.h +++ b/src/drivers/Cst816s.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Drivers { class Cst816S { - public: + public: enum class Gestures : uint8_t { None = 0x00, SlideDown = 0x01, @@ -38,7 +38,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: static constexpr uint8_t pinIrq = 28; static constexpr uint8_t pinReset = 10; static constexpr uint8_t lastTouchId = 0x0f; diff --git a/src/drivers/Hrs3300.h b/src/drivers/Hrs3300.h index cf87f378..c4f28900 100644 --- a/src/drivers/Hrs3300.h +++ b/src/drivers/Hrs3300.h @@ -5,7 +5,7 @@ namespace Pinetime { namespace Drivers { class Hrs3300 { - public: + public: enum class Registers : uint8_t { Id = 0x00, Enable = 0x01, @@ -35,7 +35,7 @@ namespace Pinetime { void SetGain(uint8_t gain); void SetDrive(uint8_t drive); - private: + private: TwiMaster& twiMaster; uint8_t twiAddress; diff --git a/src/drivers/InternalFlash.h b/src/drivers/InternalFlash.h index cbf9a20b..3a3c79dc 100644 --- a/src/drivers/InternalFlash.h +++ b/src/drivers/InternalFlash.h @@ -5,11 +5,11 @@ namespace Pinetime { namespace Drivers { class InternalFlash { - public: + public: static void ErasePage(uint32_t address); static void WriteWord(uint32_t address, uint32_t value); - private: + private: static inline void Wait(); }; } diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 27000755..6875710d 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Drivers { class Spi { - public: + public: Spi(SpiMaster& spiMaster, uint8_t pinCsn); Spi(const Spi&) = delete; Spi& operator=(const Spi&) = delete; @@ -20,7 +20,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: SpiMaster& spiMaster; uint8_t pinCsn; }; diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index aa037504..dfc195b7 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -9,7 +9,7 @@ namespace Pinetime { namespace Drivers { class SpiMaster { - public: + public: ; enum class SpiModule : uint8_t { SPI0, SPI1 }; enum class BitOrder : uint8_t { Msb_Lsb, Lsb_Msb }; @@ -42,7 +42,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: void SetupWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel); void DisableWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel); void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size); diff --git a/src/drivers/SpiNorFlash.h b/src/drivers/SpiNorFlash.h index 77a470b7..ed6ab315 100644 --- a/src/drivers/SpiNorFlash.h +++ b/src/drivers/SpiNorFlash.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Drivers { class Spi; class SpiNorFlash { - public: + public: explicit SpiNorFlash(Spi& spi); SpiNorFlash(const SpiNorFlash&) = delete; SpiNorFlash& operator=(const SpiNorFlash&) = delete; @@ -38,7 +38,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: enum class Commands : uint8_t { PageProgram = 0x02, Read = 0x03, diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index cea9cf19..4fbccbeb 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Drivers { class Spi; class St7789 { - public: + public: explicit St7789(Spi& spi, uint8_t pinDataCommand); St7789(const St7789&) = delete; St7789& operator=(const St7789&) = delete; @@ -28,7 +28,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: Spi& spi; uint8_t pinDataCommand; uint8_t verticalScrollingStartAddress = 0; diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index fc3bd2a7..1c0648a2 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -7,7 +7,7 @@ namespace Pinetime { namespace Drivers { class TwiMaster { - public: + public: enum class Modules { TWIM1 }; enum class Frequencies { Khz100, Khz250, Khz400 }; enum class ErrorCodes { NoError, TransactionFailed }; @@ -26,7 +26,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); - private: + private: ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop); void FixHwFreezed(); diff --git a/src/drivers/Watchdog.h b/src/drivers/Watchdog.h index f1741892..03807d61 100644 --- a/src/drivers/Watchdog.h +++ b/src/drivers/Watchdog.h @@ -4,7 +4,7 @@ namespace Pinetime { namespace Drivers { class Watchdog { - public: + public: enum class ResetReasons { ResetPin, Watchdog, SoftReset, CpuLockup, SystemOff, LpComp, DebugInterface, NFC, HardReset }; void Setup(uint8_t timeoutSeconds); void Start(); @@ -14,20 +14,20 @@ namespace Pinetime { } static const char* ResetReasonToString(ResetReasons reason); - private: + private: ResetReasons resetReason; ResetReasons ActualResetReason() const; }; class WatchdogView { - public: + public: WatchdogView(const Watchdog& watchdog) : watchdog {watchdog} { } Watchdog::ResetReasons ResetReason() const { return watchdog.ResetReason(); } - private: + private: const Watchdog& watchdog; }; } diff --git a/src/systemtask/SystemMonitor.h b/src/systemtask/SystemMonitor.h index d24e9bf7..45c02c2c 100644 --- a/src/systemtask/SystemMonitor.h +++ b/src/systemtask/SystemMonitor.h @@ -9,18 +9,18 @@ namespace Pinetime { struct FreeRtosMonitor {}; template class SystemMonitor { - public: + public: SystemMonitor() = delete; }; template <> class SystemMonitor { - public: + public: void Process() const { } }; template <> class SystemMonitor { - public: + public: void Process() const { if (xTaskGetTickCount() - lastTick > 10000) { NRF_LOG_INFO("---------------------------------------\nFree heap : %d", xPortGetFreeHeapSize()); @@ -36,7 +36,7 @@ namespace Pinetime { } } - private: + private: mutable TickType_t lastTick = 0; mutable TaskStatus_t tasksStatus[10]; }; diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 67e62a2d..e65fbea0 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -38,7 +38,7 @@ namespace Pinetime { } namespace System { class SystemTask { - public: + public: enum class Messages { GoToSleep, GoToRunning, @@ -83,7 +83,7 @@ namespace Pinetime { return nimbleController; }; - private: + private: TaskHandle_t taskHandle; Pinetime::Drivers::SpiMaster& spi; -- cgit v1.2.3