|
| 1 | +#include "MotionService.h" |
| 2 | +#include "components/motion//MotionController.h" |
| 3 | +#include "systemtask/SystemTask.h" |
| 4 | + |
| 5 | +using namespace Pinetime::Controllers; |
| 6 | + |
| 7 | +namespace { |
| 8 | + // 0002yyxx-78fc-48fe-8e23-433b3a1942d0 |
| 9 | + constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { |
| 10 | + return ble_uuid128_t{ |
| 11 | + .u = {.type = BLE_UUID_TYPE_128}, |
| 12 | + .value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00 } |
| 13 | + }; |
| 14 | + } |
| 15 | + |
| 16 | + // 00020000-78fc-48fe-8e23-433b3a1942d0 |
| 17 | + constexpr ble_uuid128_t BaseUuid() { |
| 18 | + return CharUuid(0x00, 0x00); |
| 19 | + } |
| 20 | + |
| 21 | + constexpr ble_uuid128_t motionServiceUuid {BaseUuid()}; |
| 22 | + constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)}; |
| 23 | + constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)}; |
| 24 | + |
| 25 | + int MotionServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { |
| 26 | + auto* motionService = static_cast<MotionService*>(arg); |
| 27 | + return motionService->OnStepCountRequested(conn_handle, attr_handle, ctxt); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// TODO Refactoring - remove dependency to SystemTask |
| 32 | +MotionService::MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController) |
| 33 | + : system {system}, |
| 34 | + motionController {motionController}, |
| 35 | + characteristicDefinition {{.uuid = &stepCountCharUuid.u, |
| 36 | + .access_cb = MotionServiceCallback, |
| 37 | + .arg = this, |
| 38 | + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, |
| 39 | + .val_handle = &stepCountHandle}, |
| 40 | + {.uuid = &motionValuesCharUuid.u, |
| 41 | + .access_cb = MotionServiceCallback, |
| 42 | + .arg = this, |
| 43 | + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, |
| 44 | + .val_handle = &motionValuesHandle}, |
| 45 | + {0}}, |
| 46 | + serviceDefinition { |
| 47 | + { |
| 48 | + .type = BLE_GATT_SVC_TYPE_PRIMARY, |
| 49 | + .uuid = &motionServiceUuid.u, |
| 50 | + .characteristics = characteristicDefinition |
| 51 | + }, |
| 52 | + {0}, |
| 53 | + } { |
| 54 | + // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) |
| 55 | + motionController.SetService(this); |
| 56 | +} |
| 57 | + |
| 58 | +void MotionService::Init() { |
| 59 | + int res = 0; |
| 60 | + res = ble_gatts_count_cfg(serviceDefinition); |
| 61 | + ASSERT(res == 0); |
| 62 | + |
| 63 | + res = ble_gatts_add_svcs(serviceDefinition); |
| 64 | + ASSERT(res == 0); |
| 65 | +} |
| 66 | + |
| 67 | +int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) { |
| 68 | + if (attributeHandle == stepCountHandle) { |
| 69 | + NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle); |
| 70 | + uint32_t buffer = motionController.NbSteps(); |
| 71 | + |
| 72 | + int res = os_mbuf_append(context->om, &buffer, 4); |
| 73 | + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; |
| 74 | + } else if(attributeHandle == motionValuesHandle) { |
| 75 | + int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() }; |
| 76 | + |
| 77 | + int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t)); |
| 78 | + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +void MotionService::OnNewStepCountValue(uint8_t stepCount) { |
| 84 | + if(!stepCountNoficationEnabled) return; |
| 85 | + |
| 86 | + uint32_t buffer = stepCount; |
| 87 | + auto* om = ble_hs_mbuf_from_flat(&buffer, 4); |
| 88 | + |
| 89 | + uint16_t connectionHandle = system.nimble().connHandle(); |
| 90 | + |
| 91 | + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + ble_gattc_notify_custom(connectionHandle, stepCountHandle, om); |
| 96 | +} |
| 97 | +void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) { |
| 98 | + if(!motionValuesNoficationEnabled) return; |
| 99 | + |
| 100 | + int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() }; |
| 101 | + auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t)); |
| 102 | + |
| 103 | + uint16_t connectionHandle = system.nimble().connHandle(); |
| 104 | + |
| 105 | + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om); |
| 110 | +} |
| 111 | + |
| 112 | +void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { |
| 113 | + if(attributeHandle == stepCountHandle) |
| 114 | + stepCountNoficationEnabled = true; |
| 115 | + else if(attributeHandle == motionValuesHandle) |
| 116 | + motionValuesNoficationEnabled = true; |
| 117 | +} |
| 118 | + |
| 119 | +void MotionService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) { |
| 120 | + if(attributeHandle == stepCountHandle) |
| 121 | + stepCountNoficationEnabled = false; |
| 122 | + else if(attributeHandle == motionValuesHandle) |
| 123 | + motionValuesNoficationEnabled = false; |
| 124 | +} |
0 commit comments