BLE: Update state when scan started/stopped callback received.

pull/13759/head
Vincent Coubard 2020-09-09 22:49:13 +01:00
parent d8c6820cbd
commit 924f8b1dc9
2 changed files with 26 additions and 23 deletions

View File

@ -459,12 +459,10 @@ ble_error_t Gap::stopScan()
{ {
ble_error_t err; ble_error_t err;
if (!_scan_enabled) { if ((!_scan_enabled && !_scan_pending) || _scan_pending) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
_scan_enabled = false;
if (is_extended_advertising_available()) { if (is_extended_advertising_available()) {
err = _pal_gap.extended_scan_enable(false, duplicates_filter_t::DISABLE, 0, 0); err = _pal_gap.extended_scan_enable(false, duplicates_filter_t::DISABLE, 0, 0);
} else { } else {
@ -472,10 +470,10 @@ ble_error_t Gap::stopScan()
} }
if (err) { if (err) {
_scan_enabled = true;
return err; return err;
} }
_scan_pending = true;
_scan_timeout.detach(); _scan_timeout.detach();
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -1053,10 +1051,19 @@ Gap::GapShutdownCallbackChain_t &Gap::onShutdown()
void Gap::on_scan_started(bool success) void Gap::on_scan_started(bool success)
{ {
if (success) {
_scan_pending = false;
_scan_enabled = true;
} else {
_scan_pending = false;
_scan_enabled = true;
}
} }
void Gap::on_scan_stopped(bool success) void Gap::on_scan_stopped(bool success)
{ {
_scan_pending = false;
_scan_enabled = false;
} }
@ -1067,29 +1074,21 @@ void Gap::on_scan_timeout()
} }
_scan_enabled = false; _scan_enabled = false;
_scan_pending = false;
if (!is_extended_advertising_available()) { if (_event_handler) {
/* if timeout happened on a 4.2 chip this means legacy scanning and a timer timeout _event_handler->onScanTimeout(ScanTimeoutEvent());
* but we need to handle the event from user context - use the event queue to handle it */
_event_queue.post(
mbed::callback(
this,
&Gap::process_legacy_scan_timeout
)
);
} else {
if (_event_handler) {
_event_handler->onScanTimeout(ScanTimeoutEvent());
}
} }
} }
void Gap::process_legacy_scan_timeout() void Gap::process_legacy_scan_timeout()
{ {
if (!_scan_enabled) {
return;
}
/* legacy scanning timed out is based on timer so we need to stop the scan manually */ /* legacy scanning timed out is based on timer so we need to stop the scan manually */
_pal_gap.scan_enable(false, false); _pal_gap.scan_enable(false, false);
_scan_enabled = false;
if (_event_handler) { if (_event_handler) {
_event_handler->onScanTimeout(ScanTimeoutEvent()); _event_handler->onScanTimeout(ScanTimeoutEvent());
@ -2666,14 +2665,17 @@ ble_error_t Gap::startScan(
_scan_timeout.detach(); _scan_timeout.detach();
if (duration.value()) { if (duration.value()) {
_scan_timeout.attach( _scan_timeout.attach([this]() {
mbed::callback(this, &Gap::on_scan_timeout), _event_queue.post([this] { process_legacy_scan_timeout(); });
},
duration.valueChrono() duration.valueChrono()
); );
} }
} }
_scan_enabled = true; if (!_scan_enabled) {
_scan_pending = true;
}
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
@ -2957,7 +2959,7 @@ bool Gap::is_advertising() const
} }
bool Gap::is_radio_active() const { bool Gap::is_radio_active() const {
return _initiating || _scan_enabled || is_advertising(); return _initiating || _scan_enabled || _scan_pending || is_advertising();
} }
void Gap::update_advertising_set_connectable_attribute( void Gap::update_advertising_set_connectable_attribute(

View File

@ -585,7 +585,8 @@ private:
peripheral_privacy_configuration_t _peripheral_privacy_configuration; peripheral_privacy_configuration_t _peripheral_privacy_configuration;
central_privacy_configuration_t _central_privacy_configuration; central_privacy_configuration_t _central_privacy_configuration;
bool _scan_enabled; bool _scan_enabled = false;
bool _scan_pending = false;
mbed::LowPowerTimeout _advertising_timeout; mbed::LowPowerTimeout _advertising_timeout;
mbed::LowPowerTimeout _scan_timeout; mbed::LowPowerTimeout _scan_timeout;
mbed::LowPowerTicker _address_rotation_ticker; mbed::LowPowerTicker _address_rotation_ticker;