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