Ejemplo n.º 1
0
void im_ble_evt_handler(ble_evt_t * ble_evt)
{
    ble_gap_evt_t gap_evt;
    pm_peer_id_t  bonded_matching_peer_id;

    NRF_PM_DEBUG_CHECK(m_module_initialized);

    if (ble_evt->header.evt_id != BLE_GAP_EVT_CONNECTED)
    {
        // Nothing to do.
        return;
    }

    gap_evt                 = ble_evt->evt.gap_evt;
    bonded_matching_peer_id = PM_PEER_ID_INVALID;

    if (   gap_evt.params.connected.peer_addr.addr_type
        != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE)
    {
        /* Search the database for bonding data matching the one that triggered the event.
         * Public and static addresses can be matched on address alone, while resolvable
         * random addresses can be resolved agains known IRKs. Non-resolvable random addresses
         * are never matching because they are not longterm form of identification.
         */

        pm_peer_id_t         peer_id;
        pm_peer_data_flash_t peer_data;

        pds_peer_data_iterate_prepare();

        switch (gap_evt.params.connected.peer_addr.addr_type)
        {
            case BLE_GAP_ADDR_TYPE_PUBLIC:
            case BLE_GAP_ADDR_TYPE_RANDOM_STATIC:
            {
                while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data))
                {
                    if (addr_compare(&gap_evt.params.connected.peer_addr,
                                     &peer_data.p_bonding_data->peer_id.id_addr_info))
                    {
                        bonded_matching_peer_id = peer_id;
                        break;
                    }
                }
            }
            break;

            case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE:
            {
                while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data))
                {
                    if (im_address_resolve(&gap_evt.params.connected.peer_addr,
                                           &peer_data.p_bonding_data->peer_id.id_info))
                    {
                        bonded_matching_peer_id = peer_id;
                        break;
                    }
                }
            }
            break;

            default:
                NRF_PM_DEBUG_CHECK(false);
                break;
        }
    }

    uint8_t new_index = new_connection(gap_evt.conn_handle,
                                       &gap_evt.params.connected.peer_addr);
    UNUSED_VARIABLE(new_index);

    if (bonded_matching_peer_id != PM_PEER_ID_INVALID)
    {
        im_new_peer_id(gap_evt.conn_handle, bonded_matching_peer_id);

        // Send a bonded peer event
        im_evt_t im_evt;
        im_evt.conn_handle = gap_evt.conn_handle;
        im_evt.evt_id      = IM_EVT_BONDED_PEER_CONNECTED;
        evt_send(&im_evt);
    }
}
Ejemplo n.º 2
0
void im_ble_evt_handler(ble_evt_t * ble_evt)
{
    ret_code_t err_code;
    switch (ble_evt->header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:
    {
        pm_peer_id_t bonded_matching_peer_id = PM_PEER_ID_INVALID;

        if (ble_evt->evt.gap_evt.params.connected.irk_match == 1)
        {
            // The peer was matched using a whitelist.
            bonded_matching_peer_id
                = m_im.irk_whitelist_peer_ids[ble_evt->evt.gap_evt.params.connected.irk_match_idx];
        }
        else if (   ble_evt->evt.gap_evt.params.connected.peer_addr.addr_type
                    != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE)
        {
            /* Search the database for bonding data matching the one that triggered the event.
             * Public and static addresses can be matched on address alone, while resolvable
             * random addresses can be resolved agains known IRKs. Non-resolvable random addresses
             * are never matching because they are not longterm form of identification.
             */
            pm_peer_id_t compared_peer_id = pdb_next_peer_id_get(PM_PEER_ID_INVALID);
            while (   (compared_peer_id        != PM_PEER_ID_INVALID)
                      && (bonded_matching_peer_id == PM_PEER_ID_INVALID))
            {
                pm_peer_data_flash_t compared_data;
                switch (ble_evt->evt.gap_evt.params.connected.peer_addr.addr_type)
                {
                case BLE_GAP_ADDR_TYPE_PUBLIC:
                /* fall-through */
                case BLE_GAP_ADDR_TYPE_RANDOM_STATIC:
                    err_code = pdb_read_buf_get(compared_peer_id,
                                                PM_PEER_DATA_ID_BONDING,
                                                &compared_data,
                                                NULL);
                    if ((err_code == NRF_SUCCESS) &&
                            addr_compare(&ble_evt->evt.gap_evt.params.connected.peer_addr,
                                         &compared_data.p_bonding_data->peer_id.id_addr_info)
                       )
                    {
                        bonded_matching_peer_id = compared_peer_id;
                    }
                    break;

                case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE:
                    err_code = pdb_read_buf_get(compared_peer_id,
                                                PM_PEER_DATA_ID_BONDING,
                                                &compared_data,
                                                NULL);
                    if (err_code == NRF_SUCCESS &&
                            im_address_resolve(&ble_evt->evt.gap_evt.params.connected.peer_addr,
                                               &compared_data.p_bonding_data->peer_id.id_info)
                       )
                    {
                        bonded_matching_peer_id = compared_peer_id;
                    }
                    break;

                case BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE:
                    // Should not happen.
                    break;

                default:
                    break;
                }
                compared_peer_id = pdb_next_peer_id_get(compared_peer_id);
            }
        }
        uint8_t new_index = new_connection(ble_evt->evt.gap_evt.conn_handle, &ble_evt->evt.gap_evt.params.connected.peer_addr);
        UNUSED_VARIABLE(new_index);

        if (bonded_matching_peer_id != PM_PEER_ID_INVALID)
        {
            im_new_peer_id(ble_evt->evt.gap_evt.conn_handle, bonded_matching_peer_id);

            // Send a bonded peer event
            im_evt_t im_evt;
            im_evt.conn_handle = ble_evt->evt.gap_evt.conn_handle;
            im_evt.evt_id = IM_EVT_BONDED_PEER_CONNECTED;
            evt_send(&im_evt);
        }
    }
    }
}