Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.8 kB
2
Indexable
Never
bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue) {
    NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration);

    // Save the callback to be invoked when the scan completes.
    m_scanCompleteCB = scanCompleteCB;
    // Save the duration in the case that the host is reset so we can reuse it.
    m_duration = duration;

    // If 0 duration specified then we assume a continuous scan is desired.
    if(duration == 0){
        duration = BLE_HS_FOREVER;
    }
    else{
        // convert duration to milliseconds
        duration = duration * 1000;
    }

    // Set the flag to ignore the results while we are deleting the vector
    if(!is_continue) {
        m_ignoreResults = true;
    }

# if CONFIG_BT_NIMBLE_EXT_ADV
    ble_gap_ext_disc_params scan_params;
    scan_params.passive = m_scan_params.passive;
    scan_params.itvl    = m_scan_params.itvl;
    scan_params.window  = m_scan_params.window;
    int rc = ble_gap_ext_disc(NimBLEDevice::m_own_addr_type,
                              duration/10,
                              0,
                              m_scan_params.filter_duplicates,
                              m_scan_params.filter_policy,
                              m_scan_params.limited,
                              &scan_params,
                              &scan_params,
                              NimBLEScan::handleGapEvent,
                              NULL);
#else
    int rc = ble_gap_disc(NimBLEDevice::m_own_addr_type,
                          duration,
                          &m_scan_params,
                          NimBLEScan::handleGapEvent,
                          NULL);
#endif
    switch(rc) {
        case 0:
            if(!is_continue) {
                clearResults();
            }
            break;

        case BLE_HS_EALREADY:
            // Clear the cache if already scanning in case an advertiser was missed.
            clearDuplicateCache();
            break;

        case BLE_HS_EBUSY:
            NIMBLE_LOGE(LOG_TAG, "Unable to scan - connection in progress.");
            break;

        case BLE_HS_ETIMEOUT_HCI:
        case BLE_HS_EOS:
        case BLE_HS_ECONTROLLER:
        case BLE_HS_ENOTSYNCED:
            NIMBLE_LOGC(LOG_TAG, "Unable to scan - Host Reset");
            break;

        default:
            NIMBLE_LOGE(LOG_TAG, "Error initiating GAP discovery procedure; rc=%d, %s",
                        rc, NimBLEUtils::returnCodeToString(rc));
            break;
    }

    m_ignoreResults = false;
    NIMBLE_LOGD(LOG_TAG, "<< start()");

    if(rc != 0 && rc != BLE_HS_EALREADY) {
        return false;
    }
    return true;
} // start