From 60a49af886f16b4bbd8012cd711374f3fdb94efc Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 17 Oct 2021 08:23:44 +0200 Subject: Add MotionService : expose step count and RAW X/Y/Z values to the host. --- src/components/ble/MotionService.cpp | 124 +++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/components/ble/MotionService.cpp (limited to 'src/components/ble/MotionService.cpp') diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp new file mode 100644 index 00000000..2bb5289c --- /dev/null +++ b/src/components/ble/MotionService.cpp @@ -0,0 +1,124 @@ +#include "MotionService.h" +#include "components/motion//MotionController.h" +#include "systemtask/SystemTask.h" + +using namespace Pinetime::Controllers; + +namespace { + // 0002yyxx-78fc-48fe-8e23-433b3a1942d0 + constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { + return ble_uuid128_t{ + .u = {.type = BLE_UUID_TYPE_128}, + .value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x02, 0x00 } + }; + } + + // 00020000-78fc-48fe-8e23-433b3a1942d0 + constexpr ble_uuid128_t BaseUuid() { + return CharUuid(0x00, 0x00); + } + + constexpr ble_uuid128_t motionServiceUuid {BaseUuid()}; + constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)}; + constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)}; + + int MotionServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto* motionService = static_cast(arg); + return motionService->OnStepCountRequested(conn_handle, attr_handle, ctxt); + } +} + +// TODO Refactoring - remove dependency to SystemTask +MotionService::MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController) + : system {system}, + motionController {motionController}, + characteristicDefinition {{.uuid = &stepCountCharUuid.u, + .access_cb = MotionServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .val_handle = &stepCountHandle}, + {.uuid = &motionValuesCharUuid.u, + .access_cb = MotionServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .val_handle = &motionValuesHandle}, + {0}}, + serviceDefinition { + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = &motionServiceUuid.u, + .characteristics = characteristicDefinition + }, + {0}, + } { + // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) + motionController.SetService(this); +} + +void MotionService::Init() { + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { + if (attributeHandle == stepCountHandle) { + NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle); + uint32_t buffer = motionController.NbSteps(); + + int res = os_mbuf_append(context->om, &buffer, 4); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } else if(attributeHandle == motionValuesHandle) { + int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() }; + + int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + return 0; +} + +void MotionService::OnNewStepCountValue(uint8_t stepCount) { + if(!stepCountNoficationEnabled) return; + + uint32_t buffer = stepCount; + auto* om = ble_hs_mbuf_from_flat(&buffer, 4); + + uint16_t connectionHandle = system.nimble().connHandle(); + + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { + return; + } + + ble_gattc_notify_custom(connectionHandle, stepCountHandle, om); +} +void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) { + if(!motionValuesNoficationEnabled) return; + + int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() }; + auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t)); + + uint16_t connectionHandle = system.nimble().connHandle(); + + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { + return; + } + + ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om); +} + +void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { + if(attributeHandle == stepCountHandle) + stepCountNoficationEnabled = true; + else if(attributeHandle == motionValuesHandle) + motionValuesNoficationEnabled = true; +} + +void MotionService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { + if(attributeHandle == stepCountHandle) + stepCountNoficationEnabled = false; + else if(attributeHandle == motionValuesHandle) + motionValuesNoficationEnabled = false; +} -- cgit v1.2.3 From b3a82288997556b04a64d452ec2067747f1fb706 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Wed, 20 Oct 2021 20:52:04 +0200 Subject: Add mention to Call characteristic (which was missing in the doc) and change the UUID of the new Motion service from 00020000-* to 00030000-*. --- doc/ble.md | 6 +++++- src/components/ble/MotionService.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/components/ble/MotionService.cpp') diff --git a/doc/ble.md b/doc/ble.md index 89ad877f..8125e985 100644 --- a/doc/ble.md +++ b/doc/ble.md @@ -34,10 +34,14 @@ The following custom services are implemented in InfiniTime: - Since InfiniTime 0.11: * [Navigation Service](NavigationService.md) : 00010000-78fc-48fe-8e23-433b3a1942d0 + + + - Since InfiniTime 0.13 + * Call characteristic (extension to the Alert Notification Service): 00020001-78fc-48fe-8e23-433b3a1942d0 - Since InfiniTime 1.7: - * [Motion Service](MotionService.md) : 00020000-78fc-48fe-8e23-433b3a1942d0 + * [Motion Service](MotionService.md) : 00030000-78fc-48fe-8e23-433b3a1942d0 ## BLE services [List of standard BLE services](https://www.bluetooth.com/specifications/gatt/services/) diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp index 2bb5289c..e305021a 100644 --- a/src/components/ble/MotionService.cpp +++ b/src/components/ble/MotionService.cpp @@ -9,7 +9,7 @@ namespace { constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { return ble_uuid128_t{ .u = {.type = BLE_UUID_TYPE_128}, - .value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x02, 0x00 } + .value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00 } }; } -- cgit v1.2.3 From 71ce13d3095febf9dcdb0ddc6ca03b515fd7d57a Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Tue, 26 Oct 2021 20:31:18 +0200 Subject: Motion service : fix step notifications that were sent as a single byte instead of 4 (uint32_t). --- src/components/ble/MotionService.cpp | 2 +- src/components/ble/MotionService.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/components/ble/MotionService.cpp') diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp index e305021a..b4786ab5 100644 --- a/src/components/ble/MotionService.cpp +++ b/src/components/ble/MotionService.cpp @@ -80,7 +80,7 @@ int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attr return 0; } -void MotionService::OnNewStepCountValue(uint8_t stepCount) { +void MotionService::OnNewStepCountValue(uint32_t stepCount) { if(!stepCountNoficationEnabled) return; uint32_t buffer = stepCount; diff --git a/src/components/ble/MotionService.h b/src/components/ble/MotionService.h index 75ad5182..1b4ac0a3 100644 --- a/src/components/ble/MotionService.h +++ b/src/components/ble/MotionService.h @@ -17,7 +17,7 @@ namespace Pinetime { MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController); void Init(); int OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context); - void OnNewStepCountValue(uint8_t stepCount); + void OnNewStepCountValue(uint32_t stepCount); void OnNewMotionValues(int16_t x, int16_t y, int16_t z); void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle); -- cgit v1.2.3