Skip to content

Commit 6c42f8e

Browse files
sondrepasilz
andcommitted
subsys: bluetooth: services: ble_hrs_client: add event
Add body sensor location event Signed-off-by: Sondre Pettersen <sondre.pettersen@nordicsemi.no> Co-authored-by: Asil Zogby <asil.zogby@nordicsemi.no>
1 parent e41329f commit 6c42f8e

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

include/bm/bluetooth/services/ble_hrs_client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum ble_hrs_client_evt_type {
5656
* received from the peer.
5757
*/
5858
BLE_HRS_CLIENT_EVT_HRM_NOTIFICATION,
59+
BLE_HRS_CLIENT_EVT_BSL_UPDATE,
5960
/** Error. */
6061
BLE_HRS_CLIENT_EVT_ERROR,
6162
};
@@ -80,6 +81,8 @@ struct ble_hrs_handles {
8081
uint16_t hrm_cccd_handle;
8182
/** Handle of the Heart Rate Measurement characteristic, as provided by the SoftDevice. */
8283
uint16_t hrm_handle;
84+
uint16_t bsl_handle;
85+
uint16_t bsl_cccd_handle;
8386
};
8487

8588
/**
@@ -95,9 +98,12 @@ struct ble_hrs_client_evt {
9598
struct {
9699
/** Handles related to the Heart Rate, found on the peer device. */
97100
struct ble_hrs_handles handles;
101+
uint8_t body_sensor_location;
98102
} discovery_complete;
99103
/** @ref BLE_HRS_CLIENT_EVT_HRM_NOTIFICATION event data. */
100104
struct ble_hrs_measurement hrm_notification;
105+
106+
uint8_t body_sensor_location;
101107
/** @ref BLE_HRS_CLIENT_EVT_ERROR event data. */
102108
struct {
103109
/** Error reason */

subsys/bluetooth/services/ble_hrs_client/ble_hrs_client.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ static void on_disconnected(struct ble_hrs_client *hrs_client, const ble_evt_t *
119119
hrs_client->conn_handle = BLE_CONN_HANDLE_INVALID;
120120
hrs_client->handles.hrm_cccd_handle = BLE_GATT_HANDLE_INVALID;
121121
hrs_client->handles.hrm_handle = BLE_GATT_HANDLE_INVALID;
122+
hrs_client->handles.bsl_cccd_handle = BLE_GATT_HANDLE_INVALID;
123+
hrs_client->handles.bsl_handle = BLE_GATT_HANDLE_INVALID;
122124
}
123125
}
124126

@@ -152,7 +154,16 @@ void ble_hrs_on_db_disc_evt(struct ble_hrs_client *hrs_client,
152154
db_char->cccd_handle;
153155
hrs_client_evt.discovery_complete.handles.hrm_handle =
154156
db_char->characteristic.handle_value;
155-
break;
157+
}
158+
else if (db_char->characteristic.uuid.uuid == BLE_UUID_BODY_SENSOR_LOCATION_CHAR) {
159+
hrs_client_evt.discovery_complete.handles.bsl_cccd_handle = db_char->cccd_handle;
160+
hrs_client_evt.discovery_complete.handles.bsl_handle = db_char->characteristic.handle_value;
161+
ble_gatts_value_t val = {
162+
.p_value = &hrs_client_evt.discovery_complete.body_sensor_location,
163+
.len = sizeof(hrs_client_evt.discovery_complete.body_sensor_location),
164+
};
165+
//LOG_INF("sd_ble_gatts_value_get returned %#x, location = %u" ,sd_ble_gatts_value_get(db_discovery_evt->conn_handle, db_char->characteristic.handle_value, &val), hrs_client_evt.discovery_complete.body_sensor_location);
166+
sd_ble_gattc_read(db_discovery_evt->conn_handle, db_char->characteristic.handle_value, 0);
156167
}
157168
}
158169

@@ -194,6 +205,27 @@ uint32_t ble_hrs_client_init(struct ble_hrs_client *hrs_client,
194205
return ble_db_discovery_service_register(hrs_client_config->db_discovery, &hrs_uuid);
195206
}
196207

208+
static void on_read_rsp(struct ble_hrs_client *hrs_client, const ble_evt_t *ble_evt) {
209+
210+
const ble_gattc_evt_read_rsp_t *read_rsp = &ble_evt->evt.gattc_evt.params.read_rsp;
211+
struct ble_hrs_client_evt evt = {.conn_handle = ble_evt->evt.gattc_evt.conn_handle, .evt_type = BLE_HRS_CLIENT_EVT_BSL_UPDATE, .body_sensor_location = ble_evt->evt.gattc_evt.params.read_rsp.data[0]};
212+
213+
if (hrs_client->conn_handle != ble_evt->evt.gattc_evt.conn_handle) {
214+
return;
215+
}
216+
217+
/* Check if this is a heart rate notification. */
218+
if (read_rsp->handle != hrs_client->handles.bsl_handle) {
219+
return;
220+
}
221+
222+
if (read_rsp->len < 1) {
223+
return;
224+
}
225+
226+
hrs_client->evt_handler(hrs_client, &evt);
227+
}
228+
197229
void ble_hrs_client_on_ble_evt(const ble_evt_t *ble_evt, void *hrs_client)
198230
{
199231
__ASSERT(ble_evt, "BLE event is NULL");
@@ -204,8 +236,15 @@ void ble_hrs_client_on_ble_evt(const ble_evt_t *ble_evt, void *hrs_client)
204236
on_hvx(hrs_client, ble_evt);
205237
break;
206238
case BLE_GAP_EVT_DISCONNECTED:
239+
const ble_gap_evt_t *gap_evt = &ble_evt->evt.gap_evt;
240+
if (gap_evt->params.connected.role != BLE_GAP_ROLE_CENTRAL){
241+
return;
242+
}
207243
on_disconnected(hrs_client, ble_evt);
208244
break;
245+
case BLE_GATTC_EVT_READ_RSP:
246+
on_read_rsp(hrs_client, ble_evt);
247+
break;
209248
default:
210249
break;
211250
}

0 commit comments

Comments
 (0)