From 5d3a9744938b38c064379c3f78ace5a7dd87926f Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 27 Sep 2020 20:02:47 +0200 Subject: Add BatteryInformationService to expose the battery level to BLE host. --- src/Components/Ble/BatteryInformationService.cpp | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/Components/Ble/BatteryInformationService.cpp (limited to 'src/Components/Ble/BatteryInformationService.cpp') diff --git a/src/Components/Ble/BatteryInformationService.cpp b/src/Components/Ble/BatteryInformationService.cpp new file mode 100644 index 00000000..c86830b8 --- /dev/null +++ b/src/Components/Ble/BatteryInformationService.cpp @@ -0,0 +1,62 @@ +#include "BatteryInformationService.h" +#include "../Battery/BatteryController.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t BatteryInformationService::batteryInformationServiceUuid; +constexpr ble_uuid16_t BatteryInformationService::batteryLevelUuid; + + + +int BatteryInformationServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto* batteryInformationService = static_cast(arg); + return batteryInformationService->OnBatteryServiceRequested(conn_handle, attr_handle, ctxt); +} + +BatteryInformationService::BatteryInformationService(Controllers::Battery& batteryController) : + batteryController{batteryController}, + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &batteryLevelUuid, + .access_cb = BatteryInformationServiceCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + .val_handle = &batteryLevelHandle + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &batteryInformationServiceUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + }{ + +} + +void BatteryInformationService::Init() { + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, + ble_gatt_access_ctxt *context) { + if(attributeHandle == batteryLevelHandle) { + NRF_LOG_INFO("BATTERY : handle = %d", batteryLevelHandle); + static uint8_t batteryValue = batteryController.PercentRemaining(); + int res = os_mbuf_append(context->om, &batteryValue, 1); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + return 0; +} \ No newline at end of file -- cgit v1.2.3