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

ble_nus_c.c ...

Hi,

I have problem with ble_nus_c.c in SDK v11...

Normal operation is:

Connecting to target 57E19A190FDF
Connected to target
[DB]: Starting discovery of service with UUID 0x1 for Connection handle 0
Found service UUID 0x1
[DB]: Discovery of service with UUID 0x1 completed with success for Connectionhandle 0
The device has the Nordic UART Service

But some times I have only this:

Connecting to target 57E19A190FDF
Connected to target
[DB]: Starting discovery of service with UUID 0x1 for Connection handle 0

After some exploration, I found the problem: Sometimes ble_db_discovery_on_ble_evt(...) is in this

case BLE_GAP_EVT_DISCONNECTED:
    on_disconnected(p_db_discovery, &(p_ble_evt->evt.gap_evt));
    break;

and specialy in this case in ble_nus_c_on_ble_evt(...)

p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle

is not true, and this code is not launched

           {
                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);
            }

result: the BLE_NUS_C_EVT_DISCONNECTED is never fired and the scan_start() is never called !!!

My workaround:

        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;

Please can you confirm ?

Regards, Gaétan

  • FormerMember
    0 FormerMember

    The reason that p_ble_evt->evt.gap_evt.conn_handle is not equal to p_ble_nus_c->conn_handle when the disconnect occurs, is that the disconnect occurs before the service discovery has finished, and p_ble_nus_c->conn_handle is set when the service discovery has finished. m_ble_nus_c/p_ble_nus_c->conn_handleis set in ble_nus_c_handles_assign(..). The result is that the corner case "disconnect before service discovery has finished" not will be handled correctly.

    The purpose of p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle is to check that the disconnect was on the nus service connection (in case of connection to multiple devices with different services).

    Yes, if you want ble_nus_c_on_ble_evt --> BLE_GAP_EVT_DISCONNECTED to handle all disconnect events the same way (no filtering on connection handle), checking for event handler only (like you do) will be sufficient.

Related