2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/HeartRateService.h"
|
2021-01-17 10:34:14 -05:00
|
|
|
#include "components/heartrate/HeartRateController.h"
|
|
|
|
#include "systemtask/SystemTask.h"
|
2022-02-20 08:45:59 -05:00
|
|
|
#include <nrf_log.h>
|
2021-01-17 10:34:14 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid;
|
|
|
|
constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid;
|
|
|
|
|
|
|
|
namespace {
|
2021-10-17 02:23:01 -04:00
|
|
|
int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
2021-01-17 10:34:14 -05:00
|
|
|
auto* heartRateService = static_cast<HeartRateService*>(arg);
|
|
|
|
return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Refactoring - remove dependency to SystemTask
|
2021-04-18 13:28:14 -04:00
|
|
|
HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Controllers::HeartRateController& heartRateController)
|
|
|
|
: system {system},
|
|
|
|
heartRateController {heartRateController},
|
2021-07-24 11:40:06 -04:00
|
|
|
characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
|
2021-10-17 02:23:01 -04:00
|
|
|
.access_cb = HeartRateServiceCallback,
|
2021-04-18 13:28:14 -04:00
|
|
|
.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,
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &heartRateServiceUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
} {
|
2021-01-17 10:34:14 -05:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
|
|
|
|
if (attributeHandle == heartRateMeasurementHandle) {
|
2021-01-25 08:18:57 -05:00
|
|
|
NRF_LOG_INFO("HEARTRATE : handle = %d", heartRateMeasurementHandle);
|
2021-01-17 10:34:14 -05:00
|
|
|
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) {
|
2021-10-17 02:23:01 -04:00
|
|
|
if(!heartRateMeasurementNotificationEnable) return;
|
|
|
|
|
2021-01-17 10:34:14 -05:00
|
|
|
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
|
2021-04-18 13:28:14 -04:00
|
|
|
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
|
2021-01-17 10:34:14 -05:00
|
|
|
|
|
|
|
uint16_t connectionHandle = system.nimble().connHandle();
|
|
|
|
|
|
|
|
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om);
|
|
|
|
}
|
2021-10-17 02:23:01 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|