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