Skip to content

Commit f32141a

Browse files
committed
BLE: Notify application when advertising start or stop
Applicable for legacy and extended advertising.
1 parent 6ee5740 commit f32141a

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

connectivity/FEATURE_BLE/include/ble/Gap.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ class Gap {
314314
{
315315
}
316316

317+
/**
318+
* Called when advertising starts.
319+
*
320+
* @param event Advertising start event.
321+
*
322+
* @see startAdvertising()
323+
*/
324+
virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
325+
{
326+
}
327+
317328
/**
318329
* Called when advertising ends.
319330
*
@@ -732,6 +743,7 @@ class Gap {
732743
* @param maxEvents Max number of events produced during advertising - 0 means no limit.
733744
* @return BLE_ERROR_NONE on success.
734745
*
746+
* @see EventHandler::onAdvertisingStart when the advertising starts.
735747
* @see EventHandler::onScanRequestReceived when a scan request is received.
736748
* @see EventHandler::onAdvertisingEnd when the advertising ends.
737749
* @see EventHandler::onConnectionComplete when the device gets connected

connectivity/FEATURE_BLE/include/ble/gap/Events.h

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,47 @@ struct PeriodicAdvertisingSyncLoss {
575575
*/
576576
struct ScanTimeoutEvent { };
577577

578+
/**
579+
* Event produced when advertising start.
580+
*
581+
* @see ble::Gap::EventHandler::onAdvertisingStart().
582+
*/
583+
struct AdvertisingStartEvent {
584+
#if !defined(DOXYGEN_ONLY)
585+
586+
/** Create an advertising start event.
587+
*
588+
* @param advHandle Advertising set handle.
589+
*/
590+
AdvertisingStartEvent(advertising_handle_t advHandle) :
591+
advHandle(advHandle)
592+
{
593+
}
594+
595+
#endif
596+
597+
/** Get advertising handle. */
598+
advertising_handle_t getAdvHandle() const
599+
{
600+
return advHandle;
601+
}
602+
603+
private:
604+
advertising_handle_t advHandle;
605+
};
606+
578607
/**
579608
* Event produced when advertising ends.
580609
*
581610
* @see ble::Gap::EventHandler::onAdvertisingEnd().
611+
*
612+
* @note The connection handle, connected flag and completed_event fields are
613+
* valid if the flag legacy is not set to true.
582614
*/
583615
struct AdvertisingEndEvent {
584616
#if !defined(DOXYGEN_ONLY)
585617

586-
/** Create advertising end event.
618+
/** Create an extended advertising end event.
587619
*
588620
* @param advHandle Advertising set handle.
589621
* @param connection Connection handle.
@@ -599,7 +631,19 @@ struct AdvertisingEndEvent {
599631
advHandle(advHandle),
600632
connection(connection),
601633
completed_events(completed_events),
602-
connected(connected)
634+
connected(connected),
635+
legacy(false)
636+
{
637+
}
638+
639+
/** Create a legacy advertising end event.
640+
*/
641+
AdvertisingEndEvent() :
642+
advHandle(LEGACY_ADVERTISING_HANDLE),
643+
connection(),
644+
completed_events(0),
645+
connected(false),
646+
legacy(true)
603647
{
604648
}
605649

@@ -629,11 +673,22 @@ struct AdvertisingEndEvent {
629673
return connected;
630674
}
631675

676+
/** Is the end of legacy advertising.
677+
*
678+
* If it is the return of getConnection() getCompleted_events() and isConnected()
679+
* must be discarded
680+
*/
681+
bool isLegacy() const
682+
{
683+
return legacy;
684+
}
685+
632686
private:
633687
advertising_handle_t advHandle;
634688
connection_handle_t connection;
635689
uint8_t completed_events;
636690
bool connected;
691+
bool legacy;
637692
};
638693

639694
/**

connectivity/FEATURE_BLE/source/generic/GapImpl.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ void Gap::on_scan_stopped(bool success)
11991199
if (restart_advertising) {
12001200
_address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE);
12011201
startAdvertising(LEGACY_ADVERTISING_HANDLE);
1202+
_adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE);
12021203
}
12031204

12041205
_scan_address_refresh = false;
@@ -2804,11 +2805,18 @@ void Gap::on_legacy_advertising_started()
28042805
{
28052806
_active_sets.set(LEGACY_ADVERTISING_HANDLE);
28062807
_pending_sets.clear(LEGACY_ADVERTISING_HANDLE);
2808+
2809+
if (_adv_started_from_refresh.get(LEGACY_ADVERTISING_HANDLE)) {
2810+
_adv_started_from_refresh.clear(LEGACY_ADVERTISING_HANDLE);
2811+
} else if(_event_handler) {
2812+
_event_handler->onAdvertisingStart(
2813+
AdvertisingStartEvent(LEGACY_ADVERTISING_HANDLE)
2814+
);
2815+
}
28072816
}
28082817

28092818
void Gap::on_legacy_advertising_stopped()
28102819
{
2811-
28122820
_active_sets.clear(LEGACY_ADVERTISING_HANDLE);
28132821
_pending_sets.clear(LEGACY_ADVERTISING_HANDLE);
28142822

@@ -2819,10 +2827,13 @@ void Gap::on_legacy_advertising_stopped()
28192827
if (_address_refresh_sets.get(LEGACY_ADVERTISING_HANDLE) && !wait_for_scan_stop) {
28202828
_address_refresh_sets.clear(LEGACY_ADVERTISING_HANDLE);
28212829
startAdvertising(LEGACY_ADVERTISING_HANDLE);
2830+
_adv_started_from_refresh.set(LEGACY_ADVERTISING_HANDLE);
28222831
if (restart_scan) {
28232832
_scan_address_refresh = false;
28242833
startScan();
28252834
}
2835+
} else if (_event_handler) {
2836+
_event_handler->onAdvertisingEnd(AdvertisingEndEvent());
28262837
}
28272838
}
28282839

@@ -2831,6 +2842,13 @@ void Gap::on_advertising_set_started(const mbed::Span<const uint8_t>& handles)
28312842
for (const auto &handle : handles) {
28322843
_active_sets.set(handle);
28332844
_pending_sets.clear(handle);
2845+
if (_adv_started_from_refresh.get(handle)) {
2846+
_adv_started_from_refresh.clear(handle);
2847+
} else if (_event_handler) {
2848+
_event_handler->onAdvertisingStart(
2849+
AdvertisingStartEvent(LEGACY_ADVERTISING_HANDLE)
2850+
);
2851+
}
28342852
}
28352853
}
28362854

@@ -2848,6 +2866,7 @@ void Gap::on_advertising_set_terminated(
28482866
if (_address_refresh_sets.get(advertising_handle) && !connection_handle) {
28492867
_address_refresh_sets.clear(advertising_handle);
28502868
startAdvertising(advertising_handle);
2869+
_adv_started_from_refresh.set(advertising_handle);
28512870
return;
28522871
}
28532872

connectivity/FEATURE_BLE/source/generic/GapImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ class Gap :
923923
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _pending_sets;
924924
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _address_refresh_sets;
925925
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _interruptible_sets;
926+
BitArray<BLE_GAP_MAX_ADVERTISING_SETS> _adv_started_from_refresh;
926927

927928

928929
bool _user_manage_connection_parameter_requests : 1;

0 commit comments

Comments
 (0)