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

How to SD assign connection handle after reconnect ?

Hi!
We develop multilink central FW for nRF 52840 having NRF_SDH_BLE_CENTRAL_LINK_COUNT = 6.
For enumerating connections and get info about concret connection, we use array with size of NRF_SDH_BLE_CENTRAL_LINK_COUNT (6).
If to see an example "ble_app_multilink_central" we see that to assign a connection handle, a current conn_handle is used as index of array of m_lbs_c and m_db_disc. 
But, I thought about this: For example: we have the maximum number of connections (6), and we have last active connection handle = 5.
After which there is one disconnection (for exaple conn_handle was 3), and then reconnection to the same device what will the connection handle be? 6 or 3 ? 
Because if the handle number increment by SD and in that situation next handle may be set to array, it exceeds the size of the array, there will be an exception.

My question is: Is it safe to use the connection handle as an index of array, or still need to implement own control algorithm?

Parents
  • My question is: Is it safe to use the connection handle as an index of array, or still need to implement own control algorithm?

     Not really, precisely because of the issues you've raised. You need to handle the state when a disconnected link is re-established. You can have an array of structs: 

    typedef struct{
        /** Flag used to indicate available slot.*/
        bool taken;
        
        /** Connection handle.*/
        uint16_t conn_handle;
    }conn_handles_t;
    
    conn_handles_t conn_handles[NRF_SDH_BLE_CENTRAL_LINK_COUNT];
    
    memset(conn_handles[0], '0', sizeof(conn_handles));
    
    on_connect()
    {
        for(i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
        {
            // Assing the connection handle to first available slot
            if(!conn_handles[i].taken)
            {
                conn_handles[i].taken = true;
                conn_handles[i].conn_handle = current_handle;
                
                //TODO: Save the index before exiting
                
                break;
            }
        }
    }
    
    on_disconnect()
    {
        conn_handles[current_handle].taken = false;
        conn_handles[current_handle].conn_handle = BLE_CONN_HANDLE_INVALID;
    }


    Or you can find other ways to keep track of your handles. 

Reply
  • My question is: Is it safe to use the connection handle as an index of array, or still need to implement own control algorithm?

     Not really, precisely because of the issues you've raised. You need to handle the state when a disconnected link is re-established. You can have an array of structs: 

    typedef struct{
        /** Flag used to indicate available slot.*/
        bool taken;
        
        /** Connection handle.*/
        uint16_t conn_handle;
    }conn_handles_t;
    
    conn_handles_t conn_handles[NRF_SDH_BLE_CENTRAL_LINK_COUNT];
    
    memset(conn_handles[0], '0', sizeof(conn_handles));
    
    on_connect()
    {
        for(i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
        {
            // Assing the connection handle to first available slot
            if(!conn_handles[i].taken)
            {
                conn_handles[i].taken = true;
                conn_handles[i].conn_handle = current_handle;
                
                //TODO: Save the index before exiting
                
                break;
            }
        }
    }
    
    on_disconnect()
    {
        conn_handles[current_handle].taken = false;
        conn_handles[current_handle].conn_handle = BLE_CONN_HANDLE_INVALID;
    }


    Or you can find other ways to keep track of your handles. 

Children
Related