summaryrefslogtreecommitdiff
path: root/src/components/ble/BatteryInformationService.cpp
diff options
context:
space:
mode:
authorJF002 <JF002@users.noreply.github.com>2020-10-04 12:21:22 +0200
committerGitHub <noreply@github.com>2020-10-04 12:21:22 +0200
commit39954bbd3afb592a0c3109e3479594183e8d0778 (patch)
tree58efd04aa38b8dc7989c51fe3c9cdb9a3fb46a54 /src/components/ble/BatteryInformationService.cpp
parent5adc97702c326d0252df6da75ce4ac244a4b3553 (diff)
parent6c86d1d9d706706fcb6f214aba8259e61ed68755 (diff)
Merge pull request #68 from Avamander/patch-1
Rename folders to follow a consistent style
Diffstat (limited to 'src/components/ble/BatteryInformationService.cpp')
-rw-r--r--src/components/ble/BatteryInformationService.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/components/ble/BatteryInformationService.cpp b/src/components/ble/BatteryInformationService.cpp
new file mode 100644
index 00000000..f7d895c2
--- /dev/null
+++ b/src/components/ble/BatteryInformationService.cpp
@@ -0,0 +1,62 @@
+#include "BatteryInformationService.h"
+#include "components/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<BatteryInformationService*>(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