Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USART BLE Nus service error handler

Hi,

i have a question regarding the USART NUS error handler.

In my setup, two Nordic BLE Chips are communicating via the USART service.

One chip is acting as a central unit, the other as a peripheral. The central is the the one sending cyclic messages, the peripheral is only receiving.

If the Peripheral chip looses power or is out of range which is a valid scenario, the Central is running into the NUS error handler.

See the code below:

static void nus_error_handler(uint32_t nrf_error) {
  APP_ERROR_HANDLER(nrf_error);
}


static void nus_c_init(void) {
  ret_code_t err_code;
  ble_nus_c_init_t init;

  init.evt_handler = ble_nus_c_evt_handler;
  init.error_handler = nus_error_handler;
  init.p_gatt_queue = &m_ble_gatt_queue;

  err_code = ble_nus_c_init(&m_ble_nus_c, &init);
  APP_ERROR_CHECK(err_code);
}

void appl_txble_callback(uint8_t *Data, uint16_t Length, uint8_t bleChannel) {
  ret_code_t ret_val = NRF_SUCCESS;

  if (m_ble_gatt_queue.p_conn_handles[bleChannel] !=
        BLE_CONN_HANDLE_INVALID) 
  {
    do {
        ret_val = ble_nus_c_string_send(&m_ble_nus_c, Data, Length);

        if(ret_val == NRF_SUCCESS)
        {
          nrf_gpio_pin_toggle(LED_MESSAGE_TOGGLE);
        }

        if ((ret_val != NRF_SUCCESS) && (ret_val != NRF_ERROR_RESOURCES)) {
          NRF_LOG_ERROR("Failed sending NUS message. Error 0x%x. ", ret_val);
          APP_ERROR_CHECK(ret_val);
        } else {
          ret_val = NRF_ERROR_SVC_HANDLER_MISSING;
        }
    } while (ret_val == NRF_ERROR_RESOURCES);
  }
}

In the described scenario the Central unit will throw an nus error, thus invoking the callback "nus_error_handler" with the error code 19  NRF_ERROR_RESOURCES.

I am unsure how to handle the error in the callback, since i only get the error code in the handler, without any context of the error.

What handling would be intented? No context is known, thus it is possible to have another source of the error as the described case.

Is there an API to distinguish between the channel on which the error occured, or to shut the channel down? Altering the GATT Queue variable m_ble_gatt_queue does not seems correct.

Thanks for your help!

Jan

Parents
  • Hi,

    In the described scenario the Central unit will throw an nus error, thus invoking the callback "nus_error_handler" with the error code 19  NRF_ERROR_RESOURCES.

    Let's say that you are in a connection, continuously sending data from your device. Then the other device suddenly turns off. In this case, the disconnect event will not happen before the supervision timeout(typically 4 seconds). In this 4 seconds window, you are still trying to send data, the internal SD buffers are filling up, but the packets will not be sent since the other device is turned off. When the buffer is full, you get NRF_ERROR_RESOURCES. Since you’re the central here, stop trying to send data, and wait for a BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event before resuming. If the connection is lost, you get the BLE_GAP_EVT_DISCONNECTED event after the supervision timeout.

Reply
  • Hi,

    In the described scenario the Central unit will throw an nus error, thus invoking the callback "nus_error_handler" with the error code 19  NRF_ERROR_RESOURCES.

    Let's say that you are in a connection, continuously sending data from your device. Then the other device suddenly turns off. In this case, the disconnect event will not happen before the supervision timeout(typically 4 seconds). In this 4 seconds window, you are still trying to send data, the internal SD buffers are filling up, but the packets will not be sent since the other device is turned off. When the buffer is full, you get NRF_ERROR_RESOURCES. Since you’re the central here, stop trying to send data, and wait for a BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event before resuming. If the connection is lost, you get the BLE_GAP_EVT_DISCONNECTED event after the supervision timeout.

Children
No Data
Related