MotionService : fix typo and characteristic array size + send notification only if the host subscribed to them.
This commit is contained in:
parent
bf83493c8d
commit
d1f50157c7
|
@ -8,7 +8,7 @@ constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid;
|
||||||
constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid;
|
constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
int HeartRateServiceServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
||||||
auto* heartRateService = static_cast<HeartRateService*>(arg);
|
auto* heartRateService = static_cast<HeartRateService*>(arg);
|
||||||
return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt);
|
return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Control
|
||||||
: system {system},
|
: system {system},
|
||||||
heartRateController {heartRateController},
|
heartRateController {heartRateController},
|
||||||
characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
|
characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
|
||||||
.access_cb = HeartRateServiceServiceCallback,
|
.access_cb = HeartRateServiceCallback,
|
||||||
.arg = this,
|
.arg = this,
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
|
||||||
.val_handle = &heartRateMeasurementHandle},
|
.val_handle = &heartRateMeasurementHandle},
|
||||||
|
@ -56,6 +56,8 @@ int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t a
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
|
void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
|
||||||
|
if(!heartRateMeasurementNotificationEnable) return;
|
||||||
|
|
||||||
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
|
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
|
||||||
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
|
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
|
||||||
|
|
||||||
|
@ -67,3 +69,13 @@ void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
|
||||||
|
|
||||||
ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om);
|
ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
||||||
#define max
|
#define max
|
||||||
#include <host/ble_gap.h>
|
#include <host/ble_gap.h>
|
||||||
|
#include <atomic>
|
||||||
#undef max
|
#undef max
|
||||||
#undef min
|
#undef min
|
||||||
|
|
||||||
|
@ -18,6 +19,9 @@ namespace Pinetime {
|
||||||
int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
|
int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
|
||||||
void OnNewHeartRateValue(uint8_t hearRateValue);
|
void OnNewHeartRateValue(uint8_t hearRateValue);
|
||||||
|
|
||||||
|
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
|
||||||
|
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pinetime::System::SystemTask& system;
|
Pinetime::System::SystemTask& system;
|
||||||
Controllers::HeartRateController& heartRateController;
|
Controllers::HeartRateController& heartRateController;
|
||||||
|
@ -28,10 +32,11 @@ namespace Pinetime {
|
||||||
|
|
||||||
static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId};
|
static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId};
|
||||||
|
|
||||||
struct ble_gatt_chr_def characteristicDefinition[3];
|
struct ble_gatt_chr_def characteristicDefinition[2];
|
||||||
struct ble_gatt_svc_def serviceDefinition[2];
|
struct ble_gatt_svc_def serviceDefinition[2];
|
||||||
|
|
||||||
uint16_t heartRateMeasurementHandle;
|
uint16_t heartRateMeasurementHandle;
|
||||||
|
std::atomic_bool heartRateMeasurementNotificationEnable {false};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,6 +215,16 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
|
||||||
event->subscribe.prev_notify,
|
event->subscribe.prev_notify,
|
||||||
event->subscribe.cur_notify,
|
event->subscribe.cur_notify,
|
||||||
event->subscribe.prev_indicate);
|
event->subscribe.prev_indicate);
|
||||||
|
|
||||||
|
if(event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) {
|
||||||
|
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
||||||
|
}
|
||||||
|
else if(event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) {
|
||||||
|
heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
||||||
|
}
|
||||||
|
else if(event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) {
|
||||||
|
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BLE_GAP_EVENT_MTU:
|
case BLE_GAP_EVENT_MTU:
|
||||||
|
|
Loading…
Reference in a new issue