Skip to content

Commit c12373f

Browse files
bluetooth: services: ble_nus: change error codes to nrf_error
Change error codes to nrf_errors. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 2b55f7a commit c12373f

File tree

5 files changed

+126
-178
lines changed

5 files changed

+126
-178
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Libraries
8181
* :ref:`lib_ble_service_hrs` service.
8282
* :ref:`lib_ble_service_lbs` service.
8383
* :ref:`lib_ble_service_mcumgr` service.
84+
* :ref:`lib_ble_service_nus` service.
8485

8586
* :ref:`lib_ble_conn_params` library:
8687

include/bm/bluetooth/services/ble_nus.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ struct ble_nus {
162162
* later be used to identify this particular service instance.
163163
* @param[in] nus_init Information needed to initialize the service.
164164
*
165-
* @retval 0 On success.
166-
* @retval -EFAULT If @p nus or @p nus_config is @c NULL.
167-
* @retval -EINVAL Invalid parameters.
165+
* @retval NRF_SUCCESS On success.
166+
* @retval NRF_ERROR_NULL If @p nus or @p nus_config is @c NULL.
167+
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
168168
*/
169-
int ble_nus_init(struct ble_nus *nus, struct ble_nus_config const *nus_config);
169+
uint32_t ble_nus_init(struct ble_nus *nus, struct ble_nus_config const *nus_config);
170170

171171
/**
172172
* @brief Function for handling the Nordic UART Service's BLE events.
@@ -192,17 +192,11 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *context);
192192
* @param[in,out] length In: Length of the @p data string. Out: Number of bytes sent.
193193
* @param[in] conn_handle Connection handle of the destination client.
194194
*
195-
* @retval 0 On success.
196-
* @retval -EFAULT If @p nus, @p data or @p length is @c NULL.
197-
* @retval -EINVAL Invalid parameters.
198-
* @retval -ENOENT Invalid @p conn_handle.
199-
* @retval -EIO Failed to send notification.
200-
* @retval -ENOTCONN Connection handle unknown to the softdevice.
201-
* @retval -EPIPE Notifications not enabled in the CCCD.
202-
* @retval -EBADF Not found.
203-
* @retval -EAGAIN Not enough resources for operation.
195+
* @retval NRF_SUCCESS On success.
196+
* @return nrf_error on failure.
204197
*/
205-
int ble_nus_data_send(struct ble_nus *nus, uint8_t *data, uint16_t *length, uint16_t conn_handle);
198+
uint32_t ble_nus_data_send(struct ble_nus *nus, uint8_t *data, uint16_t *length,
199+
uint16_t conn_handle);
206200

207201
#ifdef __cplusplus
208202
}

samples/bluetooth/ble_nus/src/main.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,26 @@ static int buf_idx;
7171
#if defined(CONFIG_NUS_LPUARTE)
7272
static void lpuarte_rx_handler(char *data, size_t data_len)
7373
{
74-
int err;
74+
uint32_t nrf_err;
7575
uint16_t len = data_len;
7676

7777
LOG_INF("Sending data over BLE NUS, len %d", len);
7878

7979
do {
80-
err = ble_nus_data_send(&ble_nus, data, &len, conn_handle);
81-
if ((err != 0) &&
82-
(err != -EPIPE) &&
83-
(err != -EAGAIN) &&
84-
(err != -EBADF)) {
85-
LOG_ERR("Failed to send NUS data, err %d", err);
80+
nrf_err = ble_nus_data_send(&ble_nus, data, &len, conn_handle);
81+
if ((nrf_err) &&
82+
(nrf_err != NRF_ERROR_INVALID_STATE) &&
83+
(nrf_err != NRF_ERROR_RESOURCES) &&
84+
(nrf_err != NRF_ERROR_NOT_FOUND)) {
85+
LOG_ERR("Failed to send NUS data, nrf_error %#x", nrf_err);
8686
return;
8787
}
88-
} while (err == -EAGAIN);
88+
} while (nrf_err == NRF_ERROR_RESOURCES);
8989
}
9090
#else
9191
static void uarte_rx_handler(char *data, size_t data_len)
9292
{
93-
int err;
93+
uint32_t nrf_err;
9494
uint8_t c;
9595
/* receive buffer used in UART ISR callback */
9696
static char rx_buf[BLE_NUS_MAX_DATA_LEN];
@@ -114,15 +114,15 @@ static void uarte_rx_handler(char *data, size_t data_len)
114114
LOG_INF("Sending data over BLE NUS, len %d", len);
115115

116116
do {
117-
err = ble_nus_data_send(&ble_nus, rx_buf, &len, conn_handle);
118-
if ((err != 0) &&
119-
(err != -EPIPE) &&
120-
(err != -EAGAIN) &&
121-
(err != -EBADF)) {
122-
LOG_ERR("Failed to send NUS data, err %d", err);
117+
nrf_err = ble_nus_data_send(&ble_nus, rx_buf, &len, conn_handle);
118+
if ((nrf_err) &&
119+
(nrf_err != NRF_ERROR_INVALID_STATE) &&
120+
(nrf_err != NRF_ERROR_RESOURCES) &&
121+
(nrf_err != NRF_ERROR_NOT_FOUND)) {
122+
LOG_ERR("Failed to send NUS data, nrf_error %#x", nrf_err);
123123
return;
124124
}
125-
} while (err == -EAGAIN);
125+
} while (nrf_err == NRF_ERROR_RESOURCES);
126126

127127
if (len == rx_buf_idx) {
128128
rx_buf_idx = 0;
@@ -468,9 +468,9 @@ int main(void)
468468
goto idle;
469469
}
470470

471-
err = ble_nus_init(&ble_nus, &nus_cfg);
472-
if (err) {
473-
LOG_ERR("Failed to initialize Nordic uart service, err %d", err);
471+
nrf_err = ble_nus_init(&ble_nus, &nus_cfg);
472+
if (nrf_err) {
473+
LOG_ERR("Failed to initialize Nordic uart service, nrf_error %#x", nrf_err);
474474
goto idle;
475475
}
476476

subsys/bluetooth/services/ble_nus/nus.c

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#include <nrf_error.h>
78
#include <stdbool.h>
89
#include <stdint.h>
910
#include <bm/bluetooth/services/ble_nus.h>
@@ -101,7 +102,7 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
101102
*/
102103
static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
103104
{
104-
int err;
105+
uint32_t nrf_err;
105106
const uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle;
106107
struct ble_nus_evt evt = {
107108
.type = BLE_NUS_EVT_COMM_STARTED,
@@ -124,8 +125,8 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
124125
/* Check the hosts CCCD value to inform of readiness to send data using the
125126
* RX characteristic
126127
*/
127-
err = sd_ble_gatts_value_get(conn_handle, nus->tx_handles.cccd_handle, &gatts_val);
128-
if ((err == 0) && (nus->evt_handler != NULL) &&
128+
nrf_err = sd_ble_gatts_value_get(conn_handle, nus->tx_handles.cccd_handle, &gatts_val);
129+
if ((nrf_err == NRF_SUCCESS) && (nus->evt_handler != NULL) &&
129130
is_notification_enabled(gatts_val.p_value)) {
130131
if (ctx != NULL) {
131132
ctx->is_notification_enabled = true;
@@ -244,60 +245,59 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *ctx)
244245
}
245246
}
246247

247-
int ble_nus_init(struct ble_nus *nus, struct ble_nus_config const *cfg)
248+
uint32_t ble_nus_init(struct ble_nus *nus, struct ble_nus_config const *cfg)
248249
{
249-
int err;
250+
uint32_t nrf_err;
250251
ble_uuid_t ble_uuid;
251252
ble_uuid128_t uuid_base = { .uuid128 = BLE_NUS_UUID_BASE };
252253

253254
if (!nus || !cfg) {
254-
return -EFAULT;
255+
return NRF_ERROR_NULL;
255256
}
256257

257258
/* Initialize the service structure. */
258259
nus->evt_handler = cfg->evt_handler;
259260
nus->service_handle = BLE_CONN_HANDLE_INVALID;
260261

261262
/* Add a custom base UUID. */
262-
err = sd_ble_uuid_vs_add(&uuid_base, &nus->uuid_type);
263-
if (err) {
264-
LOG_ERR("sd_ble_uuid_vs_add failed, nrf_error %#x", err);
265-
return -EINVAL;
263+
nrf_err = sd_ble_uuid_vs_add(&uuid_base, &nus->uuid_type);
264+
if (nrf_err) {
265+
LOG_ERR("sd_ble_uuid_vs_add failed, nrf_error %#x", nrf_err);
266+
return NRF_ERROR_INVALID_PARAM;
266267
}
267268

268269
ble_uuid.type = nus->uuid_type;
269270
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
270271

271272
/* Add the service. */
272-
err = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
273-
&ble_uuid,
274-
&nus->service_handle);
275-
if (err) {
276-
LOG_ERR("Failed to add NUS service, nrf_error %#x", err);
277-
return -EINVAL;
273+
nrf_err = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
274+
&ble_uuid,
275+
&nus->service_handle);
276+
if (nrf_err) {
277+
LOG_ERR("Failed to add NUS service, nrf_error %#x", nrf_err);
278+
return NRF_ERROR_INVALID_PARAM;
278279
}
279280

280281
/* Add NUS RX characteristic. */
281-
err = nus_rx_char_add(nus, cfg);
282-
if (err) {
283-
LOG_ERR("nus_rx_char_add failed, nrf_error %#x", err);
284-
return -EINVAL;
282+
nrf_err = nus_rx_char_add(nus, cfg);
283+
if (nrf_err) {
284+
LOG_ERR("nus_rx_char_add failed, nrf_error %#x", nrf_err);
285+
return NRF_ERROR_INVALID_PARAM;
285286
}
286287

287288
/* Add NUS TX characteristic. */
288-
err = nus_tx_char_add(nus, cfg);
289-
if (err) {
290-
LOG_ERR("nus_tx_char_add failed, nrf_error %#x", err);
291-
return -EINVAL;
289+
nrf_err = nus_tx_char_add(nus, cfg);
290+
if (nrf_err) {
291+
LOG_ERR("nus_tx_char_add failed, nrf_error %#x", nrf_err);
292+
return NRF_ERROR_INVALID_PARAM;
292293
}
293294

294295
return 0;
295296
}
296297

297-
int ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
298+
uint32_t ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
298299
uint16_t *len, uint16_t conn_handle)
299300
{
300-
int err;
301301
ble_gatts_hvx_params_t hvx_params = {
302302
.p_data = data,
303303
.p_len = len,
@@ -306,45 +306,27 @@ int ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
306306
struct ble_nus_client_context *ctx;
307307

308308
if (!nus || !data || !len) {
309-
return -EFAULT;
309+
return NRF_ERROR_NULL;
310310
}
311311

312312
if (*len > BLE_NUS_MAX_DATA_LEN) {
313-
return -EINVAL;
313+
return NRF_ERROR_INVALID_PARAM;
314314
}
315315

316316
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
317-
return -ENOENT;
317+
return NRF_ERROR_NOT_FOUND;
318318
}
319319

320320
ctx = ble_nus_client_context_get(conn_handle);
321321
if (ctx == NULL) {
322-
return -ENOENT;
322+
return NRF_ERROR_NOT_FOUND;
323323
}
324324

325325
if (!ctx->is_notification_enabled) {
326-
return -EINVAL;
326+
return NRF_ERROR_INVALID_PARAM;
327327
}
328328

329329
hvx_params.handle = nus->tx_handles.value_handle;
330330

331-
err = sd_ble_gatts_hvx(conn_handle, &hvx_params);
332-
switch (err) {
333-
case NRF_SUCCESS:
334-
return 0;
335-
case BLE_ERROR_INVALID_CONN_HANDLE:
336-
return -ENOTCONN;
337-
case NRF_ERROR_INVALID_STATE:
338-
case BLE_ERROR_GATTS_SYS_ATTR_MISSING:
339-
return -EPIPE;
340-
case NRF_ERROR_RESOURCES:
341-
return -EAGAIN;
342-
case NRF_ERROR_NOT_FOUND:
343-
return -EBADF;
344-
default:
345-
LOG_ERR("Failed to send NUS data, nrf_error %#x", err);
346-
return -EIO;
347-
}
348-
349-
return 0;
331+
return sd_ble_gatts_hvx(conn_handle, &hvx_params);
350332
}

0 commit comments

Comments
 (0)