diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-03-30 20:50:42 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-03-30 20:50:42 (GMT) |
| commit | 5db030547eeb0aae422447ddb63f0bd2f9d5f384 (patch) | |
| tree | 648bdc9654a3d826eaef1cefaa4b329495016569 /src/components/ble | |
| parent | e1cb4f64097a7d084f178f762546cecb5bd3c6be (diff) | |
Revert "sans heart"ultraredux-heart
This reverts commit 6ef420d2407a4685b56a233f6b0f849e90c6cf49.
Diffstat (limited to 'src/components/ble')
| -rw-r--r-- | src/components/ble/HeartRateService.cpp | 82 | ||||
| -rw-r--r-- | src/components/ble/HeartRateService.h | 42 | ||||
| -rw-r--r-- | src/components/ble/NimbleController.cpp | 13 | ||||
| -rw-r--r-- | src/components/ble/NimbleController.h | 5 |
4 files changed, 140 insertions, 2 deletions
diff --git a/src/components/ble/HeartRateService.cpp b/src/components/ble/HeartRateService.cpp new file mode 100644 index 0000000..4824a6b --- /dev/null +++ b/src/components/ble/HeartRateService.cpp @@ -0,0 +1,82 @@ +#include "components/ble/HeartRateService.h" +#include "components/heartrate/HeartRateController.h" +#include "systemtask/SystemTask.h" +#include <nrf_log.h> + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid; +constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid; + +namespace { + int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto* heartRateService = static_cast<HeartRateService*>(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 = &heartRateMeasurementUuid.u, + .access_cb = HeartRateServiceCallback, + .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 = &heartRateServiceUuid.u, + .characteristics = characteristicDefinition}, + {0}, + } { + // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) + heartRateController.SetService(this); +} + +void HeartRateService::Init() { + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +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 + + int res = os_mbuf_append(context->om, buffer, 2); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + return 0; +} + +void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) { + if(!heartRateMeasurementNotificationEnable) return; + + uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value + auto* om = ble_hs_mbuf_from_flat(buffer, 2); + + uint16_t connectionHandle = system.nimble().connHandle(); + + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { + return; + } + + ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om); +} + +void HeartRateService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { + if(attributeHandle == heartRateMeasurementHandle) + heartRateMeasurementNotificationEnable = true; +} + +void HeartRateService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { + if(attributeHandle == heartRateMeasurementHandle) + heartRateMeasurementNotificationEnable = false; +}
\ No newline at end of file diff --git a/src/components/ble/HeartRateService.h b/src/components/ble/HeartRateService.h new file mode 100644 index 0000000..4e4a5a4 --- /dev/null +++ b/src/components/ble/HeartRateService.h @@ -0,0 +1,42 @@ +#pragma once +#define min // workaround: nimble's min/max macros conflict with libstdc++ +#define max +#include <host/ble_gap.h> +#include <atomic> +#undef max +#undef min + +namespace Pinetime { + namespace System { + class SystemTask; + } + 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); + + void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle); + void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle); + + 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 heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId}; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + uint16_t heartRateMeasurementHandle; + std::atomic_bool heartRateMeasurementNotificationEnable {false}; + }; + } +} diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index f63d98c..bc712f4 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -27,7 +27,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, NotificationManager& notificationManager, Battery& batteryController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + HeartRateController& heartRateController ) : systemTask {systemTask}, bleController {bleController}, @@ -42,6 +43,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeService {dateTimeController}, batteryInformationService {batteryController}, immediateAlertService {systemTask, notificationManager}, + heartRateService {systemTask, heartRateController}, serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } @@ -84,6 +86,7 @@ void NimbleController::Init() { dfuService.Init(); batteryInformationService.Init(); immediateAlertService.Init(); + heartRateService.Init(); int rc; rc = ble_hs_util_ensure_addr(0); @@ -240,6 +243,14 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { event->subscribe.prev_notify, event->subscribe.cur_notify, event->subscribe.prev_indicate); + + if (event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) { + heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); + } else if (event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) { + heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); + } else if (event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) { + heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle); + } break; case BLE_GAP_EVENT_MTU: diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index c9d6420..0facca6 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -14,6 +14,7 @@ #include "components/ble/CurrentTimeService.h" #include "components/ble/DeviceInformationService.h" #include "components/ble/DfuService.h" +#include "components/ble/HeartRateService.h" #include "components/ble/ImmediateAlertService.h" #include "components/ble/ServiceDiscovery.h" @@ -39,7 +40,8 @@ namespace Pinetime { DateTime& dateTimeController, NotificationManager& notificationManager, Battery& batteryController, - Pinetime::Drivers::SpiNorFlash& spiNorFlash); + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + HeartRateController& heartRateController); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event* event); @@ -72,6 +74,7 @@ namespace Pinetime { CurrentTimeService currentTimeService; BatteryInformationService batteryInformationService; ImmediateAlertService immediateAlertService; + HeartRateService heartRateService; ServiceDiscovery serviceDiscovery; uint8_t addrType; |
