Skip to content

Commit 6d07430

Browse files
committed
bluetooth: services: ble_hrs_client: refactor _on_db_disc_evt()
Refactor function to exit early and reduce indentation. Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
1 parent 46610a7 commit 6d07430

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

subsys/bluetooth/services/ble_hrs_client/ble_hrs_client.c

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -136,37 +136,39 @@ void ble_hrs_on_db_disc_evt(struct ble_hrs_client *ble_hrs_client,
136136
struct hrs_db *hrs_db = &ble_hrs_client->peer_hrs_db;
137137

138138
/* Check if the Heart Rate Service was discovered. */
139-
if (evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
140-
evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_HEART_RATE_SERVICE &&
141-
evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE) {
142-
/* Find the CCCD Handle of the Heart Rate Measurement characteristic. */
143-
for (uint32_t i = 0; i < evt->params.discovered_db.char_count; i++) {
144-
db_char = &evt->params.discovered_db.charateristics[i];
145-
146-
if (db_char->characteristic.uuid.uuid ==
147-
BLE_UUID_HEART_RATE_MEASUREMENT_CHAR) {
148-
/* Found Heart Rate characteristic. Store CCCD handle and break. */
149-
hrs_c_evt.params.peer_db.hrm_cccd_handle =
150-
db_char->cccd_handle;
151-
hrs_c_evt.params.peer_db.hrm_handle =
152-
db_char->characteristic.handle_value;
153-
break;
154-
}
155-
}
139+
if (evt->evt_type != BLE_DB_DISCOVERY_COMPLETE ||
140+
evt->params.discovered_db.srv_uuid.uuid != BLE_UUID_HEART_RATE_SERVICE ||
141+
evt->params.discovered_db.srv_uuid.type != BLE_UUID_TYPE_BLE) {
142+
return;
143+
}
156144

157-
LOG_DBG("HRS discovered");
158-
/* If the instance has been assigned prior to db_discovery,
159-
* assign the db_handles.
160-
*/
161-
if (ble_hrs_client->conn_handle != BLE_CONN_HANDLE_INVALID) {
162-
if ((hrs_db->hrm_cccd_handle == BLE_GATT_HANDLE_INVALID) &&
163-
(hrs_db->hrm_handle == BLE_GATT_HANDLE_INVALID)) {
164-
ble_hrs_client->peer_hrs_db = hrs_c_evt.params.peer_db;
165-
}
145+
/* Find the CCCD Handle of the Heart Rate Measurement characteristic. */
146+
for (uint32_t i = 0; i < evt->params.discovered_db.char_count; i++) {
147+
db_char = &evt->params.discovered_db.charateristics[i];
148+
149+
if (db_char->characteristic.uuid.uuid ==
150+
BLE_UUID_HEART_RATE_MEASUREMENT_CHAR) {
151+
/* Found Heart Rate characteristic. Store CCCD handle and break. */
152+
hrs_c_evt.params.peer_db.hrm_cccd_handle =
153+
db_char->cccd_handle;
154+
hrs_c_evt.params.peer_db.hrm_handle =
155+
db_char->characteristic.handle_value;
156+
break;
166157
}
158+
}
167159

168-
ble_hrs_client->evt_handler(ble_hrs_client, &hrs_c_evt);
160+
LOG_DBG("HRS discovered");
161+
/* If the instance has been assigned prior to db_discovery,
162+
* assign the db_handles.
163+
*/
164+
if (ble_hrs_client->conn_handle != BLE_CONN_HANDLE_INVALID) {
165+
if ((hrs_db->hrm_cccd_handle == BLE_GATT_HANDLE_INVALID) &&
166+
(hrs_db->hrm_handle == BLE_GATT_HANDLE_INVALID)) {
167+
ble_hrs_client->peer_hrs_db = hrs_c_evt.params.peer_db;
168+
}
169169
}
170+
171+
ble_hrs_client->evt_handler(ble_hrs_client, &hrs_c_evt);
170172
}
171173

172174
uint32_t ble_hrs_client_init(struct ble_hrs_client *ble_hrs_client,

0 commit comments

Comments
 (0)