Skip to content

Commit 15fb8e1

Browse files
committed
[Gen4] fixes BLE race condition while scanning
1 parent fec628a commit 15fb8e1

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

hal/src/rtl872x/ble_hal.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ class BleGap {
507507
advParams_{},
508508
advTimeoutTimer_(nullptr),
509509
isScanning_(false),
510+
notifyScanResult_(false),
510511
scanParams_{},
511512
scanSemaphore_(nullptr),
512513
scanResultCallback_(nullptr),
@@ -640,6 +641,7 @@ class BleGap {
640641
hal_ble_adv_params_t advParams_;
641642
os_timer_t advTimeoutTimer_; /**< Timer for advertising timeout. */
642643
volatile bool isScanning_; /**< If it is scanning or not. */
644+
volatile bool notifyScanResult_;
643645
hal_ble_scan_params_t scanParams_; /**< BLE scanning parameters. */
644646
os_semaphore_t scanSemaphore_; /**< Semaphore to wait until the scan procedure completed. */
645647
hal_ble_on_scan_result_cb_t scanResultCallback_; /**< Callback function on scan result. */
@@ -1672,6 +1674,7 @@ int BleGap::startScanning(hal_ble_on_scan_result_cb_t callback, void* context) {
16721674

16731675
CHECK_RTL(WRAP_BLE_EVT_LOCK(le_scan_start()));
16741676
isScanning_ = true;
1677+
notifyScanResult_ = true;
16751678
// GAP_SCAN_STATE_SCANNING may be propagated immediately following the GAP_SCAN_STATE_START
16761679
// IMPORTANT: have to poll here, for some reason we may not get notified
16771680
if (waitState(BleGapDevState().scan(GAP_SCAN_STATE_SCANNING), BLE_STATE_DEFAULT_TIMEOUT, true /* forcePoll */)) {
@@ -1694,7 +1697,7 @@ int BleGap::startScanning(hal_ble_on_scan_result_cb_t callback, void* context) {
16941697

16951698
int BleGap::stopScanning(bool resetStack) {
16961699
BleLock lk;
1697-
1700+
notifyScanResult_ = false;
16981701
if (!isScanning_) {
16991702
return SYSTEM_ERROR_NONE;
17001703
}
@@ -1754,7 +1757,7 @@ int BleGap::stopScanning(bool resetStack) {
17541757

17551758
// This function is called from thread
17561759
void BleGap::notifyScanResult(T_LE_SCAN_INFO* advReport) {
1757-
if (!isScanning_ || !scanResultCallback_) {
1760+
if (!isScanning_ || !scanResultCallback_ || !notifyScanResult_) {
17581761
return;
17591762
}
17601763

0 commit comments

Comments
 (0)