Skip to content

Commit

Permalink
Merge pull request #2714 from particle-iot/fix/sc-123341/rtl872x-ble-…
Browse files Browse the repository at this point in the history
…wifi-scan

Fix BLE/Wi-Fi concurrency issues
  • Loading branch information
XuGuohui committed Dec 13, 2023
2 parents 3185fa5 + 034d1a0 commit 3fd1fc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 15 additions & 1 deletion hal/network/ncp_client/realtek/rtl_ncp_client.cpp
Expand Up @@ -51,6 +51,7 @@ extern "C" {

#undef IFNAMSIZ // AMBD SDK and LWIP both define this symbol...
#include "wlan_hal.h"
#include "bt_intf.h"

extern "C" void rtw_efuse_boot_write(void);

Expand Down Expand Up @@ -309,6 +310,8 @@ int RealtekNcpClient::connect(const char* ssid, const MacAddress& bssid, WifiSec
const NcpClientLock lock(this);
CHECK_TRUE(connState_ == NcpConnectionState::DISCONNECTED, SYSTEM_ERROR_INVALID_STATE);

rtlk_bt_set_gnt_bt(PTA_AUTO);

LOG(INFO, "Try to connect to ssid: %s", ssid);
rtlError = wifi_connect((char*)ssid,
wifiSecurityToRtlSecurity(sec),
Expand Down Expand Up @@ -380,6 +383,9 @@ int RealtekNcpClient::getNetworkInfo(WifiNetworkInfo* info) {

int RealtekNcpClient::scan(WifiScanCallback callback, void* data) {
const NcpClientLock lock(this);

rtlk_bt_set_gnt_bt(PTA_AUTO);

CHECK_TRUE(ncpState_ == NcpState::ON, SYSTEM_ERROR_INVALID_STATE);
struct Context {
WifiScanCallback callback = nullptr;
Expand Down Expand Up @@ -447,11 +453,16 @@ int RealtekNcpClient::scan(WifiScanCallback callback, void* data) {
for (int i = 0; i < ctx.results.size(); i++) {
callback(ctx.results[i], data);
}
if (ctx.results.size() == 0) {
if (rtlError && ctx.results.size() == 0) {
// Workaround for a weird state we might enter where the wifi driver
// is not returning any results
rtwRadioReset();
}

if (connectionState() == NcpConnectionState::DISCONNECTED) {
rtlk_bt_set_gnt_bt(PTA_BT);
}

return rtl_error_to_system(rtlError);
}

Expand Down Expand Up @@ -516,6 +527,9 @@ void RealtekNcpClient::connectionState(NcpConnectionState state) {
event.state = connState_;
handler(event, conf_.eventHandlerData());
}
if (state == NcpConnectionState::DISCONNECTED) {
rtlk_bt_set_gnt_bt(PTA_BT);
}
}

int RealtekNcpClient::rltkOff() {
Expand Down
18 changes: 2 additions & 16 deletions hal/src/rtl872x/ble_hal.cpp
Expand Up @@ -107,20 +107,6 @@ extern "C" void __real_bt_coex_handle_specific_evt(uint8_t* p, uint8_t len);
extern "C" void __wrap_bt_coex_handle_specific_evt(uint8_t* p, uint8_t len);

void __wrap_bt_coex_handle_specific_evt(uint8_t* p, uint8_t len) {
const auto BT_COEX_EVENT_SCAN_MASK = 0xf0;
const auto BT_COEX_EVENT_SCAN_START = 0x20;
const auto BT_COEX_EVENT_SCAN_STOP = 0x00;
// FIXME: This is a hack to prioritize BLE over WiFI while performing a BLE scan
// This blocks any wifi comms (both RX and TX paths) while the scan is performing,
// but for now this is the best solution we have for the coexistence issue.
if (len >= 6) {
if ((p[5] & BT_COEX_EVENT_SCAN_MASK) == BT_COEX_EVENT_SCAN_START) {
// Scan start
rtlk_bt_set_gnt_bt(PTA_BT);
} else if ((p[5] & BT_COEX_EVENT_SCAN_MASK) == BT_COEX_EVENT_SCAN_STOP) {
rtlk_bt_set_gnt_bt(PTA_AUTO);
}
}
__real_bt_coex_handle_specific_evt(p, len);
}

Expand Down Expand Up @@ -1625,7 +1611,7 @@ int BleGap::setScanParams(const hal_ble_scan_params_t* params) {
memcpy(&scanParams_, params, std::min(scanParams_.size, params->size));
scanParams_.size = sizeof(hal_ble_scan_params_t);
scanParams_.version = BLE_API_VERSION;
uint8_t filterDuplicate = GAP_SCAN_FILTER_DUPLICATE_ENABLE;
uint8_t filterDuplicate = GAP_SCAN_FILTER_DUPLICATE_DISABLE;
CHECK_RTL(le_scan_set_param(GAP_PARAM_SCAN_MODE, sizeof(uint8_t), &scanParams_.active));
CHECK_RTL(le_scan_set_param(GAP_PARAM_SCAN_INTERVAL, sizeof(uint16_t), &scanParams_.interval));
CHECK_RTL(le_scan_set_param(GAP_PARAM_SCAN_WINDOW, sizeof(uint16_t), &scanParams_.window));
Expand Down Expand Up @@ -1681,7 +1667,7 @@ int BleGap::startScanning(hal_ble_on_scan_result_cb_t callback, void* context) {
CHECK_RTL(le_scan_start());
isScanning_ = true;
// GAP_SCAN_STATE_SCANNING may be propagated immediately following the GAP_SCAN_STATE_START
if (waitState(BleGapDevState().scan(GAP_SCAN_STATE_START), 10, true) && waitState(BleGapDevState().scan(GAP_SCAN_STATE_SCANNING), 10, true)) {
if (waitState(BleGapDevState().scan(GAP_SCAN_STATE_SCANNING), 10, true)) {
LOCAL_DEBUG("wait GAP_SCAN_STATE_SCANNING");
if (waitState(BleGapDevState().scan(GAP_SCAN_STATE_SCANNING))) {
LOG(TRACE, "failed to start scanning.");
Expand Down

0 comments on commit 3fd1fc3

Please sign in to comment.