This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE_NUS_C_EVT_NUS_TX_EVT has handle of 0xffff

I have following problem:

I am using a multilink NUS Central. With one peripheral connection everything works fine. But when make a second connection I sometimes get an error when I try to send data back in received event.

In ble_nus_c_evt_handler event with event type BLE_NUS_C_EVT_NUS_TX_EVT the p_ble_nus_c sometimes has an invalid connection handle (0xFFFF).

If I try to send data back I get following warning: <warning> ble_nus_c: Connection handle invalid.

How is this possible? I am assigning the handles with  ble_nus_c_handles_assign on discovery complete.

Does anyone know about this issue?

Parents Reply Children
  • yes I am assigning the handles with  ble_nus_c_handles_assign on discovery complete

    void ble_nus_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_nus_c_t * p_ble_nus_c = (ble_nus_c_t *)p_context;
    
        if ((p_ble_nus_c == NULL) || (p_ble_evt == NULL))
        {
            return;
        }
    
        if ( (p_ble_nus_c->conn_handle != BLE_CONN_HANDLE_INVALID)
           &&(p_ble_nus_c->conn_handle != p_ble_evt->evt.gap_evt.conn_handle)
           )
        {
            return;
        }
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GATTC_EVT_HVX:
    						
                on_hvx(p_ble_nus_c, p_ble_evt);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                if (p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle
                        && p_ble_nus_c->evt_handler != NULL)
                {
                    ble_nus_c_evt_t nus_c_evt;
    
                    nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCONNECTED;
    
                    p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
                    p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
                }
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    

    p_ble_evt->evt.gap_evt.conn_handle=0xffff

  • Hello,

    There is a bug in ble_nus_c when you are dealing with several connections.

    If you look in the ble_nus_c.c file, find the function called on_hvx. I am not sure what SDK version you use, but in SDK15.3.0 it should look like this:

    static void on_hvx(ble_nus_c_t * p_ble_nus_c, ble_evt_t const * p_ble_evt)
    {
        // HVX can only occur from client sending.
        if (   (p_ble_nus_c->handles.nus_tx_handle != BLE_GATT_HANDLE_INVALID)
            && (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_handle)
            && (p_ble_nus_c->evt_handler != NULL))
        {
            ble_nus_c_evt_t ble_nus_c_evt;
    
            ble_nus_c_evt.evt_type = BLE_NUS_C_EVT_NUS_TX_EVT;
            ble_nus_c_evt.p_data   = (uint8_t *)p_ble_evt->evt.gattc_evt.params.hvx.data;
            ble_nus_c_evt.data_len = p_ble_evt->evt.gattc_evt.params.hvx.len;
            ble_nus_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
    
            p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);
            NRF_LOG_DEBUG("Client sending data.");
        }
    }

    So the only line that is added is:

    ble_nus_c_evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

    This line will forward the connection handle to the ble_nus_c_on_ble_evt() (which it doesn't do by default).

    Best regards,

    Edvin

  • thanks your answer!

    now. how to modify the code ?

  • Did you try to implement something similar as in the previous snippet? You need to look for the place that forwards the BLE_NUS_C_EVT_NUS_TX_EVT, and add the connection handle in this event.

    look in ble_nus_c.c, and search for BLE_NUS_C_EVT_NUS_TX_EVT. 

    Add this line:

    ble_nus_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;

    before:

    p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);

Related