From d8c6820cbd21f6d3e36d5ce779cb681cb47913b9 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Wed, 9 Sep 2020 22:43:43 +0100 Subject: [PATCH] BLE: Add callback for scan started and stoped. --- .../source/cordio/source/PalGapImpl.cpp | 40 ++++++++++++++----- .../source/cordio/source/PalGapImpl.h | 1 + .../FEATURE_BLE/source/generic/GapImpl.cpp | 8 ++++ .../FEATURE_BLE/source/generic/GapImpl.h | 4 ++ connectivity/FEATURE_BLE/source/pal/PalGap.h | 11 +++++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp index f7c7ba29a1..644c76e8ad 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.cpp @@ -683,16 +683,34 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) } break; #endif // BLE_FEATURE_EXTENDED_ADVERTISING && BLE_ROLE_BROADCASTER -#if BLE_FEATURE_EXTENDED_ADVERTISING && BLE_ROLE_OBSERVER +#if BLE_ROLE_OBSERVER + case DM_SCAN_START_IND: + if (!handler) { break; } + handler->on_scan_started(msg->status == HCI_SUCCESS); + break; + + case DM_SCAN_STOP_IND: + if (!handler) { break; } + handler->on_scan_stopped(msg->status == HCI_SUCCESS); + break; + +#if BLE_FEATURE_EXTENDED_ADVERTISING + case DM_EXT_SCAN_START_IND: + if (!handler) { break; } + handler->on_scan_started(msg->status == HCI_SUCCESS); + break; + case DM_EXT_SCAN_STOP_IND: { - if (!handler) { - break; + if (!handler) { break; } + + if (get_gap().ext_scan_stopping) { + get_gap().ext_scan_stopping = false; + handler->on_scan_stopped(msg->status == HCI_SUCCESS); + } else { + handler->on_scan_timeout(); } - //const hciLeScanTimeoutEvt_t *evt = (const hciLeScanTimeoutEvt_t *) msg; - handler->on_scan_timeout(); - } - break; + } break; case DM_EXT_SCAN_REPORT_IND: { if (!handler) { @@ -718,9 +736,9 @@ void PalGap::gap_handler(const wsfMsgHdr_t *msg) evt->len, evt->pData ); - } - break; -#endif // BLE_FEATURE_EXTENDED_ADVERTISING && BLE_ROLE_OBSERVER + } break; +#endif // BLE_FEATURE_EXTENDED_ADVERTISING +#endif // BLE_ROLE_OBSERVER #if BLE_ROLE_CENTRAL || BLE_ROLE_PERIPHERAL case DM_REM_CONN_PARAM_REQ_IND: { @@ -1243,6 +1261,7 @@ ble_error_t PalGap::extended_scan_enable( if (enable) { uint32_t duration_ms = duration * 10; + DmScanStart( scanning_phys.value(), DM_DISC_MODE_NONE, @@ -1253,6 +1272,7 @@ ble_error_t PalGap::extended_scan_enable( ); } else { DmScanStop(); + ext_scan_stopping = true; } return BLE_ERROR_NONE; diff --git a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h index cc581ba0d1..9b4e99fbbc 100644 --- a/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h +++ b/connectivity/FEATURE_BLE/source/cordio/source/PalGapImpl.h @@ -535,6 +535,7 @@ private: uint8_t extended_scan_type[3]; phy_set_t scanning_phys; direct_adv_cb_t direct_adv_cb[DM_NUM_ADV_SETS]; + bool ext_scan_stopping = false; /** * Callback called when an event is emitted by the LE subsystem. diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index bd9f6a03e1..633cf91193 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -1051,6 +1051,14 @@ Gap::GapShutdownCallbackChain_t &Gap::onShutdown() return shutdownCallChain; } +void Gap::on_scan_started(bool success) +{ +} + +void Gap::on_scan_stopped(bool success) +{ +} + void Gap::on_scan_timeout() { diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.h b/connectivity/FEATURE_BLE/source/generic/GapImpl.h index 508405f36d..1ba1146aa4 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.h +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.h @@ -525,6 +525,10 @@ private: uint16_t supervision_timeout ) override; + void on_scan_started(bool success) override; + + void on_scan_stopped(bool success) override; + void on_scan_timeout() override; void process_legacy_scan_timeout(); diff --git a/connectivity/FEATURE_BLE/source/pal/PalGap.h b/connectivity/FEATURE_BLE/source/pal/PalGap.h index 425d561ab9..1a844f8d4c 100644 --- a/connectivity/FEATURE_BLE/source/pal/PalGap.h +++ b/connectivity/FEATURE_BLE/source/pal/PalGap.h @@ -194,6 +194,17 @@ public: sync_handle_t sync_handle ) = 0; + /** + * Called when scanning start + */ + virtual void on_scan_started(bool success) = 0; + + /** + * Called when scanning stop + */ + virtual void on_scan_stopped(bool success) = 0; + + /** Called when scanning times out. */ virtual void on_scan_timeout( ) = 0;