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 */
101102static 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