Skip to content

Commit

Permalink
BLE: Notify application when advertising start or stop
Browse files Browse the repository at this point in the history
Applicable for legacy and extended advertising.
  • Loading branch information
pan- committed Oct 5, 2020
1 parent 6ee5740 commit f32141a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
12 changes: 12 additions & 0 deletions connectivity/FEATURE_BLE/include/ble/Gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ class Gap {
{
}

/**
* Called when advertising starts.
*
* @param event Advertising start event.
*
* @see startAdvertising()
*/
virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
{
}

/**
* Called when advertising ends.
*
Expand Down Expand Up @@ -732,6 +743,7 @@ class Gap {
* @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
Expand Down
59 changes: 57 additions & 2 deletions connectivity/FEATURE_BLE/include/ble/gap/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,47 @@ struct PeriodicAdvertisingSyncLoss {
*/
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.
Expand All @@ -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)
{
}

Expand Down Expand Up @@ -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;
};

/**
Expand Down
21 changes: 20 additions & 1 deletion connectivity/FEATURE_BLE/source/generic/GapImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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());
}
}

Expand All @@ -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)
);
}
}
}

Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions connectivity/FEATURE_BLE/source/generic/GapImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ class Gap :
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;
Expand Down

0 comments on commit f32141a

Please sign in to comment.