Skip to content

Commit d07de63

Browse files
sondrepasilz
authored 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 6365539 commit d07de63

5 files changed

Lines changed: 70 additions & 14 deletions

File tree

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ Libraries
8484
Bluetooth LE Services
8585
---------------------
8686

87-
* :ref:`lib_ble_scan`
87+
* :ref:`lib_ble_scan`:
8888

8989
* Changed :c:member:`ble_scan_filter_data.addr_filter.addr` and :c:member:`ble_scan_filter_data.name_filter.name` to ``const`` in the :c:struct:`ble_scan_filter_data` structure.
9090

91+
* :ref:`lib_ble_service_hrs_client`:
92+
93+
* Added the :c:enumerator:`BLE_HRS_CLIENT_EVT_BSL_UPDATE` event to the :c:enum:`ble_hrs_client_evt_type` enum.
94+
9195
Libraries for NFC
9296
-----------------
9397

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 value was received from
60+
* the 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: 55 additions & 12 deletions
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,12 +129,13 @@ 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,
134136
.conn_handle = db_discovery_evt->conn_handle,
135137
};
136-
struct ble_hrs_handles *handles = &hrs_client->handles;
138+
struct ble_hrs_handles *const evt_handles = &hrs_client_evt.discovery_complete.handles;
137139

138140
/* Check if the Heart Rate Service was discovered. */
139141
if (db_discovery_evt->evt_type != BLE_DB_DISCOVERY_COMPLETE ||
@@ -142,27 +144,40 @@ void ble_hrs_on_db_disc_evt(struct ble_hrs_client *hrs_client,
142144
return;
143145
}
144146

145-
/* Find the CCCD Handle of the Heart Rate Measurement characteristic. */
147+
/* Iterate discovered characteristics. */
146148
for (uint32_t i = 0; i < db_discovery_evt->discovered_db->char_count; i++) {
147149
db_char = &db_discovery_evt->discovered_db->characteristics[i];
148150

149151
if (db_char->characteristic.uuid.uuid == BLE_UUID_HEART_RATE_MEASUREMENT_CHAR) {
150-
/* Found Heart Rate characteristic. Store CCCD handle and break. */
151-
hrs_client_evt.discovery_complete.handles.hrm_cccd_handle =
152-
db_char->cccd_handle;
153-
hrs_client_evt.discovery_complete.handles.hrm_handle =
154-
db_char->characteristic.handle_value;
155-
break;
152+
/* Found Heart Rate characteristic. Store value and CCCD handle for later
153+
* notification setup.
154+
*/
155+
evt_handles->hrm_cccd_handle = db_char->cccd_handle;
156+
evt_handles->hrm_handle = db_char->characteristic.handle_value;
157+
158+
} else if (db_char->characteristic.uuid.uuid ==
159+
BLE_UUID_BODY_SENSOR_LOCATION_CHAR) {
160+
/* Found body sensor location characteristic. Store value handle and issue
161+
* a read request. The value will arrive asynchronously in a
162+
* BLE_GATTC_EVT_READ_RSP event.
163+
*/
164+
evt_handles->bsl_handle = db_char->characteristic.handle_value;
165+
166+
nrf_err = sd_ble_gattc_read(db_discovery_evt->conn_handle,
167+
db_char->characteristic.handle_value, 0);
168+
if (nrf_err) {
169+
LOG_ERR("Failed to read bsl char value, nrf_err %#x", nrf_err);
170+
}
156171
}
157172
}
158173

159174
LOG_DBG("HRS discovered");
160175

161-
/* If the instance has been assigned prior to db_discovery, assign the db_handles. */
176+
/* If the instance has been assigned prior to db_discovery, do not assign the handles. */
162177
if (hrs_client->conn_handle != BLE_CONN_HANDLE_INVALID) {
163-
if ((handles->hrm_cccd_handle == BLE_GATT_HANDLE_INVALID) &&
164-
(handles->hrm_handle == BLE_GATT_HANDLE_INVALID)) {
165-
hrs_client->handles = hrs_client_evt.discovery_complete.handles;
178+
if ((hrs_client->handles.hrm_cccd_handle == BLE_GATT_HANDLE_INVALID) &&
179+
(hrs_client->handles.hrm_handle == BLE_GATT_HANDLE_INVALID)) {
180+
hrs_client->handles = *evt_handles;
166181
}
167182
}
168183

@@ -194,6 +209,31 @@ uint32_t ble_hrs_client_init(struct ble_hrs_client *hrs_client,
194209
return ble_db_discovery_service_register(hrs_client_config->db_discovery, &hrs_uuid);
195210
}
196211

212+
static void on_read_rsp(struct ble_hrs_client *hrs_client, const ble_evt_t *ble_evt)
213+
{
214+
const ble_gattc_evt_read_rsp_t *read_rsp = &ble_evt->evt.gattc_evt.params.read_rsp;
215+
struct ble_hrs_client_evt evt = {
216+
.conn_handle = ble_evt->evt.gattc_evt.conn_handle,
217+
.evt_type = BLE_HRS_CLIENT_EVT_BSL_UPDATE,
218+
.body_sensor_location = ble_evt->evt.gattc_evt.params.read_rsp.data[0],
219+
};
220+
221+
if (hrs_client->conn_handle != ble_evt->evt.gattc_evt.conn_handle) {
222+
return;
223+
}
224+
225+
/* Check if this is a body sensor location response. */
226+
if (read_rsp->handle != hrs_client->handles.bsl_handle) {
227+
return;
228+
}
229+
230+
if (read_rsp->len < 1) {
231+
return;
232+
}
233+
234+
hrs_client->evt_handler(hrs_client, &evt);
235+
}
236+
197237
void ble_hrs_client_on_ble_evt(const ble_evt_t *ble_evt, void *hrs_client)
198238
{
199239
__ASSERT(ble_evt, "BLE event is NULL");
@@ -206,6 +246,9 @@ void ble_hrs_client_on_ble_evt(const ble_evt_t *ble_evt, void *hrs_client)
206246
case BLE_GAP_EVT_DISCONNECTED:
207247
on_disconnected(hrs_client, ble_evt);
208248
break;
249+
case BLE_GATTC_EVT_READ_RSP:
250+
on_read_rsp(hrs_client, ble_evt);
251+
break;
209252
default:
210253
break;
211254
}

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)