Skip to content

Commit ceb0b72

Browse files
anhmoltlemrey
authored andcommitted
bluetooth: services: nus: small rework of the on event functions
Save conn_handle to variable and use that to avoid some long lines. Compare ctx pointer against NULL to make the condition more explicit. Update some identical error logs. Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent ccf6d67 commit ceb0b72

File tree

1 file changed

+19
-21
lines changed
  • subsys/bluetooth/services

1 file changed

+19
-21
lines changed

subsys/bluetooth/services/nus.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,29 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
111111
static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
112112
{
113113
int err;
114-
struct ble_nus_client_context *ctx;
114+
const uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle;
115115
struct ble_nus_evt evt = {
116116
.type = BLE_NUS_EVT_COMM_STARTED,
117117
.nus = nus,
118-
.conn_handle = ble_evt->evt.gap_evt.conn_handle,
118+
.conn_handle = conn_handle,
119119
};
120120
uint8_t cccd_value[2];
121121
ble_gatts_value_t gatts_val = {
122122
.p_value = cccd_value,
123123
.len = sizeof(cccd_value),
124124
.offset = 0,
125125
};
126+
struct ble_nus_client_context *ctx;
126127

127-
ctx = ble_nus_client_context_get(ble_evt->evt.gap_evt.conn_handle);
128-
if (!ctx) {
129-
LOG_ERR("Link context for 0x%02X connection handle could not be fetched.",
130-
ble_evt->evt.gap_evt.conn_handle);
128+
ctx = ble_nus_client_context_get(conn_handle);
129+
if (ctx == NULL) {
130+
LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle);
131131
}
132132

133133
/* Check the hosts CCCD value to inform of readiness to send data using the
134134
* RX characteristic
135135
*/
136-
err = sd_ble_gatts_value_get(ble_evt->evt.gap_evt.conn_handle,
137-
nus->tx_handles.cccd_handle,
138-
&gatts_val);
136+
err = sd_ble_gatts_value_get(conn_handle, nus->tx_handles.cccd_handle, &gatts_val);
139137
if ((err == 0) && (nus->data_handler != NULL) &&
140138
is_notification_enabled(gatts_val.p_value)) {
141139
if (ctx != NULL) {
@@ -155,17 +153,17 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
155153
*/
156154
static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt)
157155
{
156+
const uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle;
157+
const ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write;
158158
struct ble_nus_evt evt = {
159159
.nus = nus,
160-
.conn_handle = ble_evt->evt.gatts_evt.conn_handle,
160+
.conn_handle = conn_handle,
161161
};
162-
ble_gatts_evt_write_t const *evt_write = &ble_evt->evt.gatts_evt.params.write;
163162
struct ble_nus_client_context *ctx;
164163

165-
ctx = ble_nus_client_context_get(ble_evt->evt.gatts_evt.conn_handle);
166-
if (!ctx) {
167-
LOG_ERR("Link context for 0x%02X connection handle could not be fetched.",
168-
ble_evt->evt.gatts_evt.conn_handle);
164+
ctx = ble_nus_client_context_get(conn_handle);
165+
if (ctx == NULL) {
166+
LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle);
169167
}
170168

171169
LOG_DBG("Link ctx %p", ctx);
@@ -205,17 +203,17 @@ static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt)
205203
*/
206204
static void on_hvx_tx_complete(struct ble_nus *nus, ble_evt_t const *ble_evt)
207205
{
206+
const uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle;
208207
struct ble_nus_evt evt = {
209208
.type = BLE_NUS_EVT_TX_RDY,
210209
.nus = nus,
211-
.conn_handle = ble_evt->evt.gatts_evt.conn_handle,
210+
.conn_handle = conn_handle,
212211
};
213212
struct ble_nus_client_context *ctx;
214213

215-
ctx = ble_nus_client_context_get(ble_evt->evt.gatts_evt.conn_handle);
216-
if (!ctx) {
217-
LOG_ERR("Link context for 0x%02X connection handle could not be fetched.",
218-
ble_evt->evt.gatts_evt.conn_handle);
214+
ctx = ble_nus_client_context_get(conn_handle);
215+
if (ctx == NULL) {
216+
LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle);
219217
return;
220218
}
221219

@@ -330,7 +328,7 @@ int ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
330328
}
331329

332330
ctx = ble_nus_client_context_get(conn_handle);
333-
if (!ctx) {
331+
if (ctx == NULL) {
334332
return -ENOENT;
335333
}
336334

0 commit comments

Comments
 (0)