Skip to content

Commit d1f5015

Browse files
committed
MotionService : fix typo and characteristic array size + send notification only if the host subscribed to them.
1 parent bf83493 commit d1f5015

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/components/ble/HeartRateService.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid;
88
constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid;
99

1010
namespace {
11-
int HeartRateServiceServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
11+
int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
1212
auto* heartRateService = static_cast<HeartRateService*>(arg);
1313
return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt);
1414
}
@@ -19,7 +19,7 @@ HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Control
1919
: system {system},
2020
heartRateController {heartRateController},
2121
characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
22-
.access_cb = HeartRateServiceServiceCallback,
22+
.access_cb = HeartRateServiceCallback,
2323
.arg = this,
2424
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
2525
.val_handle = &heartRateMeasurementHandle},
@@ -56,6 +56,8 @@ int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t a
5656
}
5757

5858
void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
59+
if(!heartRateMeasurementNotificationEnable) return;
60+
5961
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
6062
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
6163

@@ -67,3 +69,13 @@ void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
6769

6870
ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om);
6971
}
72+
73+
void HeartRateService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
74+
if(attributeHandle == heartRateMeasurementHandle)
75+
heartRateMeasurementNotificationEnable = true;
76+
}
77+
78+
void HeartRateService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
79+
if(attributeHandle == heartRateMeasurementHandle)
80+
heartRateMeasurementNotificationEnable = false;
81+
}

src/components/ble/HeartRateService.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define min // workaround: nimble's min/max macros conflict with libstdc++
33
#define max
44
#include <host/ble_gap.h>
5+
#include <atomic>
56
#undef max
67
#undef min
78

@@ -18,6 +19,9 @@ namespace Pinetime {
1819
int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
1920
void OnNewHeartRateValue(uint8_t hearRateValue);
2021

22+
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
23+
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
24+
2125
private:
2226
Pinetime::System::SystemTask& system;
2327
Controllers::HeartRateController& heartRateController;
@@ -28,10 +32,11 @@ namespace Pinetime {
2832

2933
static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId};
3034

31-
struct ble_gatt_chr_def characteristicDefinition[3];
35+
struct ble_gatt_chr_def characteristicDefinition[2];
3236
struct ble_gatt_svc_def serviceDefinition[2];
3337

3438
uint16_t heartRateMeasurementHandle;
39+
std::atomic_bool heartRateMeasurementNotificationEnable {false};
3540
};
3641
}
3742
}

src/components/ble/NimbleController.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
215215
event->subscribe.prev_notify,
216216
event->subscribe.cur_notify,
217217
event->subscribe.prev_indicate);
218+
219+
if(event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) {
220+
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
221+
}
222+
else if(event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) {
223+
heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
224+
}
225+
else if(event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) {
226+
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
227+
}
218228
break;
219229

220230
case BLE_GAP_EVENT_MTU:

0 commit comments

Comments
 (0)