Skip to content

Commit 4620fa5

Browse files
sondrepasilz
andcommitted
bluetooth: services: ble_hrs_client: add bsl 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 78f12eb commit 4620fa5

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

include/bm/bluetooth/services/ble_hrs_client.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ enum ble_hrs_client_evt_type {
5656
* received from the peer.
5757
*/
5858
BLE_HRS_CLIENT_EVT_HRM_NOTIFICATION,
59+
/** Event indicating that the Body Sensor Location characteristic was received from the
60+
* peer
61+
*/
62+
BLE_HRS_CLIENT_EVT_BSL_UPDATE,
5963
/** Error. */
6064
BLE_HRS_CLIENT_EVT_ERROR,
6165
};
@@ -80,6 +84,8 @@ struct ble_hrs_handles {
8084
uint16_t hrm_cccd_handle;
8185
/** Handle of the Heart Rate Measurement characteristic, as provided by the SoftDevice. */
8286
uint16_t hrm_handle;
87+
/** Handle of the Body Sensor Location characteristic, as provided by the SoftDevice */
88+
uint16_t bsl_handle;
8389
};
8490

8591
/**
@@ -98,6 +104,8 @@ struct ble_hrs_client_evt {
98104
} discovery_complete;
99105
/** @ref BLE_HRS_CLIENT_EVT_HRM_NOTIFICATION event data. */
100106
struct ble_hrs_measurement hrm_notification;
107+
/** @ref BLE_HRS_CLIENT_EVT_BSL_UPDATE event data. */
108+
uint8_t body_sensor_location;
101109
/** @ref BLE_HRS_CLIENT_EVT_ERROR event data. */
102110
struct {
103111
/** Error reason */

subsys/bluetooth/services/ble_hrs_client/ble_hrs_client.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ 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_handle = BLE_GATT_HANDLE_INVALID;
122123
}
123124
}
124125

@@ -128,6 +129,7 @@ void ble_hrs_on_db_disc_evt(struct ble_hrs_client *hrs_client,
128129
__ASSERT(hrs_client, "HRS client instance is NULL");
129130
__ASSERT(db_discovery_evt, "Discovery event is NULL");
130131

132+
uint32_t nrf_err;
131133
const struct ble_gatt_db_char *db_char;
132134
struct ble_hrs_client_evt hrs_client_evt = {
133135
.evt_type = BLE_HRS_CLIENT_EVT_DISCOVERY_COMPLETE,
@@ -152,7 +154,18 @@ 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+
} else if (db_char->characteristic.uuid.uuid ==
158+
BLE_UUID_BODY_SENSOR_LOCATION_CHAR) {
159+
hrs_client_evt.discovery_complete.handles.bsl_handle =
160+
db_char->characteristic.handle_value;
161+
nrf_err = sd_ble_gattc_read(db_discovery_evt->conn_handle,
162+
db_char->characteristic.handle_value, 0);
163+
if (nrf_err) {
164+
hrs_client_evt.evt_type = BLE_HRS_CLIENT_EVT_ERROR;
165+
hrs_client_evt.error.reason = nrf_err;
166+
hrs_client->evt_handler(hrs_client, &hrs_client_evt);
167+
return;
168+
}
156169
}
157170
}
158171

@@ -194,18 +207,52 @@ uint32_t ble_hrs_client_init(struct ble_hrs_client *hrs_client,
194207
return ble_db_discovery_service_register(hrs_client_config->db_discovery, &hrs_uuid);
195208
}
196209

210+
static void on_read_rsp(struct ble_hrs_client *hrs_client, const ble_evt_t *ble_evt)
211+
{
212+
const ble_gattc_evt_read_rsp_t *read_rsp = &ble_evt->evt.gattc_evt.params.read_rsp;
213+
struct ble_hrs_client_evt evt = {
214+
.conn_handle = ble_evt->evt.gattc_evt.conn_handle,
215+
.evt_type = BLE_HRS_CLIENT_EVT_BSL_UPDATE,
216+
.body_sensor_location = ble_evt->evt.gattc_evt.params.read_rsp.data[0],
217+
};
218+
219+
if (hrs_client->conn_handle != ble_evt->evt.gattc_evt.conn_handle) {
220+
return;
221+
}
222+
223+
/* Check if this is a body sensor location response. */
224+
if (read_rsp->handle != hrs_client->handles.bsl_handle) {
225+
return;
226+
}
227+
228+
if (read_rsp->len < 1) {
229+
return;
230+
}
231+
232+
hrs_client->evt_handler(hrs_client, &evt);
233+
}
234+
197235
void ble_hrs_client_on_ble_evt(const ble_evt_t *ble_evt, void *hrs_client)
198236
{
199237
__ASSERT(ble_evt, "BLE event is NULL");
200238
__ASSERT(hrs_client, "HRS central instance is NULL");
201239

240+
if (ble_evt->header.evt_id >= BLE_GAP_EVT_BASE &&
241+
ble_evt->header.evt_id <= BLE_GAP_EVT_LAST &&
242+
ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_PERIPH) {
243+
return;
244+
}
245+
202246
switch (ble_evt->header.evt_id) {
203247
case BLE_GATTC_EVT_HVX:
204248
on_hvx(hrs_client, ble_evt);
205249
break;
206250
case BLE_GAP_EVT_DISCONNECTED:
207251
on_disconnected(hrs_client, ble_evt);
208252
break;
253+
case BLE_GATTC_EVT_READ_RSP:
254+
on_read_rsp(hrs_client, ble_evt);
255+
break;
209256
default:
210257
break;
211258
}

tests/unit/subsys/bluetooth/services/ble_hrs_client/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010

1111
project(unit_test_ble_hrs_client)
1212

13-
set(SOFTDEVICE_VARIANT "s115")
13+
set(SOFTDEVICE_VARIANT "s145")
1414
set(SOFTDEVICE_INCLUDE_DIR "${ZEPHYR_NRF_BM_MODULE_DIR}/components/softdevice/nrf54l/\
1515
${SOFTDEVICE_VARIANT}/${SOFTDEVICE_VARIANT}_API/include")
1616

tests/unit/subsys/bluetooth/services/ble_hrs_client/src/unity_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ void test_ble_hrs_on_db_disc_evt_hrm_char_at_index_one(void)
520520

521521
__cmock_ble_db_discovery_service_register_ExpectAndReturn(&db_discovery, NULL, NRF_SUCCESS);
522522
__cmock_ble_db_discovery_service_register_IgnoreArg_uuid();
523+
__cmock_sd_ble_gattc_read_ExpectAnyArgsAndReturn(NRF_SUCCESS);
523524

524525
nrf_err = ble_hrs_client_init(&hrs_client, &config);
525526
TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err);

0 commit comments

Comments
 (0)