Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BLE/Wi-Fi concurrency issues #2714

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

XuGuohui
Copy link
Member

@XuGuohui XuGuohui commented Dec 7, 2023

Problem

When both BLE and Wi-Fi are working, BLE scanning may has no result returned.

Solution

Dynamically set the PTA (Packets Traffic Arbitration) mode according to the Wi-Fi state.

Example App

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

Serial1LogHandler logHandler(115200, LOG_LEVEL_ALL);

constexpr system_tick_t SCAN_DURATION_MS = 30000;
system_tick_t scan_start_ms = 0;

volatile size_t ble_advert_found = 0;
size_t ble_scans_started = 0;
void scan_result_cb(const BleScanResult *result, void *context) {
    ble_advert_found++;
}

void scan_thread_f() {
    while (true) {
        unsigned int t0, t1;
        if (scan_start_ms && ((millis() - scan_start_ms) <= SCAN_DURATION_MS)) {
            ble_scans_started++;
            t0 = millis();
            BLE.scan(scan_result_cb, nullptr);
            t1 = millis();
            if (t1 - t0 > 500) {
                // Log.info("    scan took too long: %dms", t1 - t0);
            }
        }
        delay(1);
    }
}

size_t wifi_ap_found = 0;
void wifi_scan_callback(WiFiAccessPoint* wap, void* data) {
    // Log.info("ssid=%s security=%d channel=%d rssi=%d", wap->ssid, (int)wap->security, (int)wap->channel, wap->rssi);
    wifi_ap_found++;
}

void setup() {
    BLE.on();

    BleScanParams params = {};
    params.version = BLE_API_VERSION;
    params.size = sizeof(BleScanParams);
    BLE.getScanParameters(params);
    params.active = false;
    params.interval = 1600; // 1600 * 0.625ms = 1s
    params.window = 1600; // 1600 * 0.625ms = 1s
    params.timeout = 100; // 100 * 10ms = 1s
    BLE.setScanParameters(params);

    static Thread *thread = new Thread("sensor_ble_scan", scan_thread_f, OS_THREAD_PRIORITY_NETWORK);

    WiFi.on();
}

void loop() {
    static uint8_t wifi_scan = 2;

    if (!scan_start_ms || ((millis() - scan_start_ms) > (SCAN_DURATION_MS + 300))) {
        if (scan_start_ms) {
            Log.info("    ble adverts found total: %u (scans %d) (WiFi: %d)",
                ble_advert_found,
                ble_scans_started,
                wifi_scan);
            
            if (ble_advert_found == 0  && !wifi_scan) {
                while (1); // We have encountered problem
            }
        }
        wifi_scan++;
        wifi_scan %= 3;
        ble_advert_found = 0;
        ble_scans_started = 0;
        scan_start_ms = millis();
    }

    if (wifi_scan == 1) {  //  Wi-Fi scanning
        wifi_ap_found = 0;
        WiFi.scan(wifi_scan_callback);
        Log.info("wifi ap found: %u", wifi_ap_found);
    } else if (wifi_scan == 2) {  // Wi-Fi connected
        if (!WiFi.ready()) {
            WiFi.connect();
            waitUntil(WiFi.ready);
            Log.info("wifi connected");
        }
    } else { // No Wi-Fi
        if (WiFi.ready()) {
            WiFi.disconnect();
            while (WiFi.ready());
            Log.info("wifi disconnected");
        }
    }
}

References

N/A


Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

@XuGuohui XuGuohui force-pushed the fix/sc-123341/rtl872x-ble-wifi-scan branch from bf6dd84 to 034d1a0 Compare December 13, 2023 07:17
@XuGuohui XuGuohui merged commit 3fd1fc3 into develop Dec 13, 2023
13 checks passed
@XuGuohui XuGuohui deleted the fix/sc-123341/rtl872x-ble-wifi-scan branch December 13, 2023 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants