BLE: Notify application when advertising start or stop

Applicable for legacy and extended advertising.
pull/13759/head
Vincent Coubard 2020-10-02 17:20:21 +01:00
parent 6ee5740941
commit f32141a051
4 changed files with 90 additions and 3 deletions

View File

@ -314,6 +314,17 @@ public:
{
}
/**
* Called when advertising starts.
*
* @param event Advertising start event.
*
* @see startAdvertising()
*/
virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
{
}
/**
* Called when advertising ends.
*
@ -732,6 +743,7 @@ public:
* @param maxEvents Max number of events produced during advertising - 0 means no limit.
* @return BLE_ERROR_NONE on success.
*
* @see EventHandler::onAdvertisingStart when the advertising starts.
* @see EventHandler::onScanRequestReceived when a scan request is received.
* @see EventHandler::onAdvertisingEnd when the advertising ends.
* @see EventHandler::onConnectionComplete when the device gets connected

View File

@ -575,15 +575,47 @@ private:
*/
struct ScanTimeoutEvent { };
/**
* Event produced when advertising start.
*
* @see ble::Gap::EventHandler::onAdvertisingStart().
*/
struct AdvertisingStartEvent {
#if !defined(DOXYGEN_ONLY)
/** Create an advertising start event.
*
* @param advHandle Advertising set handle.
*/
AdvertisingStartEvent(advertising_handle_t advHandle) :
advHandle(advHandle)
{
}
#endif
/** Get advertising handle. */
advertising_handle_t getAdvHandle() const
{
return advHandle;
}
private:
advertising_handle_t advHandle;
};
/**
* Event produced when advertising ends.
*
* @see ble::Gap::EventHandler::onAdvertisingEnd().
*
* @note The connection handle, connected flag and completed_event fields are
* valid if the flag legacy is not set to true.
*/
struct AdvertisingEndEvent {
#if !defined(DOXYGEN_ONLY)
/** Create advertising end event.
/** Create an extended advertising end event.
*
* @param advHandle Advertising set handle.
* @param connection Connection handle.
@ -599,7 +631,19 @@ struct AdvertisingEndEvent {
advHandle(advHandle),
connection(connection),
completed_events(completed_events),
connected(connected)
connected(connected),
legacy(false)
{
}
/** Create a legacy advertising end event.
*/
AdvertisingEndEvent() :
advHandle(LEGACY_ADVERTISING_HANDLE),
connection(),
completed_events(0),
connected(false),
legacy(true)
{
}
@ -629,11 +673,22 @@ struct AdvertisingEndEvent {
return connected;
}
/** Is the end of legacy advertising.
*
* If it is the return of getConnection() getCompleted_events() and isConnected()
* must be discarded
*/
bool isLegacy() const
{
return legacy;
}
private:
advertising_handle_t advHandle;
connection_handle_t connection;
uint8_t completed_events;
bool connected;
bool legacy;
};
/**

View File

@ -1199,6 +1199,7 @@ void Gap::on_scan_stopped(bool success)
if (restart_advertising) {
_address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE);
startAdvertising(LEGACY_ADVERTISING_HANDLE);
_adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE);
}
_scan_address_refresh = false;
@ -2804,11 +2805,18 @@ void Gap::on_legacy_advertising_started()
{
_active_sets.set(LEGACY_ADVERTISING_HANDLE);
_pending_sets.clear(LEGACY_ADVERTISING_HANDLE);
if (_adv_started_from_refresh.get(LEGACY_ADVERTISING_HANDLE)) {
_adv_started_from_refresh.clear(LEGACY_ADVERTISING_HANDLE);
} else if(_event_handler) {
_event_handler->onAdvertisingStart(
AdvertisingStartEvent(LEGACY_ADVERTISING_HANDLE)
);
}
}
void Gap::on_legacy_advertising_stopped()
{
_active_sets.clear(LEGACY_ADVERTISING_HANDLE);
_pending_sets.clear(LEGACY_ADVERTISING_HANDLE);
@ -2819,10 +2827,13 @@ void Gap::on_legacy_advertising_stopped()
if (_address_refresh_sets.get(LEGACY_ADVERTISING_HANDLE) && !wait_for_scan_stop) {
_address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE);
startAdvertising(LEGACY_ADVERTISING_HANDLE);
_adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE);
if (restart_scan) {
_scan_address_refresh = false;
startScan();
}
} else if (_event_handler) {
_event_handler->onAdvertisingEnd(AdvertisingEndEvent());
}
}
@ -2831,6 +2842,13 @@ void Gap::on_advertising_set_started(const mbed::Span<const uint8_t>& handles)
for (const auto &handle : handles) {
_active_sets.set(handle);
_pending_sets.clear(handle);
if (_adv_started_from_refresh.get(handle)) {
_adv_started_from_refresh.clear(handle);
} else if (_event_handler) {
_event_handler->onAdvertisingStart(
AdvertisingStartEvent(LEGACY_ADVERTISING_HANDLE)
);
}
}
}
@ -2848,6 +2866,7 @@ void Gap::on_advertising_set_terminated(
if (_address_refresh_sets.get(advertising_handle) && !connection_handle) {
_address_refresh_sets.clear(advertising_handle);
startAdvertising(advertising_handle);
_adv_started_from_refresh.set(advertising_handle);
return;
}

View File

@ -923,6 +923,7 @@ private:
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _pending_sets;
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _address_refresh_sets;
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _interruptible_sets;
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _adv_started_from_refresh;
bool _user_manage_connection_parameter_requests : 1;