Skip to content

Commit a38cb05

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 c3da84b commit a38cb05

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
@@ -87,6 +87,7 @@ Libraries
8787
* :ref:`lib_ble_service_hrs` service.
8888
* :ref:`lib_ble_service_lbs` service.
8989
* :ref:`lib_ble_service_mcumgr` service.
90+
* :ref:`lib_ble_service_nus` service.
9091
* BLE Record Access Control Point library.
9192

9293
* :ref:`lib_ble_conn_params` library:

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;
@@ -488,9 +488,9 @@ int main(void)
488488
goto idle;
489489
}
490490

491-
err = ble_nus_init(&ble_nus, &nus_cfg);
492-
if (err) {
493-
LOG_ERR("Failed to initialize Nordic uart service, err %d", err);
491+
nrf_err = ble_nus_init(&ble_nus, &nus_cfg);
492+
if (nrf_err) {
493+
LOG_ERR("Failed to initialize Nordic uart service, nrf_error %#x", nrf_err);
494494
goto idle;
495495
}
496496

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>
@@ -100,7 +101,7 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
100101
*/
101102
static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
102103
{
103-
int err;
104+
uint32_t nrf_err;
104105
const uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle;
105106
struct ble_nus_evt evt = {
106107
.type = BLE_NUS_EVT_COMM_STARTED,
@@ -123,8 +124,8 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt)
123124
/* Check the hosts CCCD value to inform of readiness to send data using the
124125
* RX characteristic
125126
*/
126-
err = sd_ble_gatts_value_get(conn_handle, nus->tx_handles.cccd_handle, &gatts_val);
127-
if ((err == 0) && (nus->evt_handler != NULL) &&
127+
nrf_err = sd_ble_gatts_value_get(conn_handle, nus->tx_handles.cccd_handle, &gatts_val);
128+
if ((nrf_err == NRF_SUCCESS) && (nus->evt_handler != NULL) &&
128129
is_notification_enabled(gatts_val.p_value)) {
129130
if (ctx != NULL) {
130131
ctx->is_notification_enabled = true;
@@ -243,60 +244,59 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *ctx)
243244
}
244245
}
245246

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

252253
if (!nus || !cfg) {
253-
return -EFAULT;
254+
return NRF_ERROR_NULL;
254255
}
255256

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

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

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

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

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

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

293294
return 0;
294295
}
295296

296-
int ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
297+
uint32_t ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
297298
uint16_t *len, uint16_t conn_handle)
298299
{
299-
int err;
300300
ble_gatts_hvx_params_t hvx_params = {
301301
.p_data = data,
302302
.p_len = len,
@@ -305,45 +305,27 @@ int ble_nus_data_send(struct ble_nus *nus, uint8_t *data,
305305
struct ble_nus_client_context *ctx;
306306

307307
if (!nus || !data || !len) {
308-
return -EFAULT;
308+
return NRF_ERROR_NULL;
309309
}
310310

311311
if (*len > BLE_NUS_MAX_DATA_LEN) {
312-
return -EINVAL;
312+
return NRF_ERROR_INVALID_PARAM;
313313
}
314314

315315
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
316-
return -ENOENT;
316+
return NRF_ERROR_NOT_FOUND;
317317
}
318318

319319
ctx = ble_nus_client_context_get(nus, conn_handle);
320320
if (ctx == NULL) {
321-
return -ENOENT;
321+
return NRF_ERROR_NOT_FOUND;
322322
}
323323

324324
if (!ctx->is_notification_enabled) {
325-
return -EINVAL;
325+
return NRF_ERROR_INVALID_PARAM;
326326
}
327327

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

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

0 commit comments

Comments
 (0)