Skip to content

Commit 75f3b6c

Browse files
authored
Merge pull request #13717 from ARMmbed/feature-ble-host-privacy
Feature ble host privacy
2 parents 2514479 + 5f1b716 commit 75f3b6c

32 files changed

+4007
-1248
lines changed

connectivity/FEATURE_BLE/include/ble/Gap.h

Lines changed: 30 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
*
@@ -538,6 +549,14 @@ class Gap {
538549
)
539550
{
540551
}
552+
553+
/**
554+
* Function invoked when the privacy subsystem has been enabled and is
555+
* ready to be used.
556+
*/
557+
virtual void onPrivacyEnabled()
558+
{
559+
}
541560
protected:
542561
/**
543562
* Prevent polymorphic deletion and avoid unnecessary virtual destructor
@@ -724,6 +743,7 @@ class Gap {
724743
* @param maxEvents Max number of events produced during advertising - 0 means no limit.
725744
* @return BLE_ERROR_NONE on success.
726745
*
746+
* @see EventHandler::onAdvertisingStart when the advertising starts.
727747
* @see EventHandler::onScanRequestReceived when a scan request is received.
728748
* @see EventHandler::onAdvertisingEnd when the advertising ends.
729749
* @see EventHandler::onConnectionComplete when the device gets connected
@@ -1244,6 +1264,14 @@ class Gap {
12441264
* resolved and advertisement packets are forwarded to the application
12451265
* even if the advertiser private address is unknown.
12461266
*
1267+
* @par Initialization of the privacy subsystem
1268+
*
1269+
* When privacy is enabled, the system generates new resolvable and non
1270+
* resolvable private addresses. Scan, Advertising and Connecting to a peer
1271+
* won't be available until the generation process completes. When addresses
1272+
* have been generated, the application is notified that privacy
1273+
* initialisation as completed with a call to EventHandler::onPrivacyEnabled .
1274+
*
12471275
* @param[in] enable Should be set to true to enable the privacy mode and
12481276
* false to disable it.
12491277
*
@@ -1448,6 +1476,8 @@ class Gap {
14481476
* forbidden by the Bluetooth specification.
14491477
*/
14501478
ble_error_t setRandomStaticAddress(const ble::address_t& address);
1479+
1480+
ble::address_t getRandomStaticAddress();
14511481
#endif // !defined(DOXYGEN_ONLY)
14521482

14531483
private:

connectivity/FEATURE_BLE/include/ble/SecurityManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ class SecurityManager
835835
// Privacy
836836
//
837837

838+
#if BLE_FEATURE_PRIVACY
838839
/**
839840
* Sets how often the address is rotated when privacy is enabled.
840841
*
@@ -845,6 +846,7 @@ class SecurityManager
845846
ble_error_t setPrivateAddressTimeout(
846847
uint16_t timeout_in_seconds
847848
);
849+
#endif // BLE_FEATURE_PRIVACY
848850

849851
/* Event callback handlers. */
850852
public:

connectivity/FEATURE_BLE/include/ble/common/Duration.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ namespace ble {
3333
* @tparam Min left-bound
3434
* @tparam Max right-bound
3535
*/
36-
template<uint32_t Min, uint32_t Max>
36+
template<uint32_t Min, uint32_t Max, uint32_t Default = Min>
3737
struct Range {
3838
static const uint32_t MIN = Min;
3939
static const uint32_t MAX = Max;
40+
static const uint32_t DEFAULT = Default;
4041
};
4142

4243
/**
@@ -111,7 +112,7 @@ struct Duration {
111112
*
112113
* It is initialized with the minimum value acceptable.
113114
*/
114-
Duration() : duration(Range::MIN)
115+
Duration() : duration(Range::DEFAULT)
115116
{
116117
}
117118

@@ -598,11 +599,14 @@ bool operator>(Duration<Rep, Us, Range, F> lhs, Duration<Rep, Us, Range, F> rhs)
598599

599600
#if !defined(DOXYGEN_ONLY)
600601

601-
template<uint32_t Min, uint32_t Max>
602-
const uint32_t Range<Min, Max>::MIN;
602+
template<uint32_t Min, uint32_t Max, uint32_t Default>
603+
const uint32_t Range<Min, Max, Default>::MIN;
603604

604-
template<uint32_t Min, uint32_t Max>
605-
const uint32_t Range<Min, Max>::MAX;
605+
template<uint32_t Min, uint32_t Max, uint32_t Default>
606+
const uint32_t Range<Min, Max, Default>::MAX;
607+
608+
template<uint32_t Min, uint32_t Max, uint32_t Default>
609+
const uint32_t Range<Min, Max, Default>::DEFAULT;
606610

607611
template<typename T, T V>
608612
const T Value<T, V>::VALUE;

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

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,22 @@ struct AdvertisingReportEvent {
6969
const address_t &directAddress,
7070
const mbed::Span<const uint8_t> &advertisingData
7171
) :
72+
peerAddress(peerAddress),
7273
type(type),
7374
peerAddressType(peerAddressType),
74-
peerAddress(peerAddress),
7575
primaryPhy(primaryPhy),
7676
secondaryPhy(secondaryPhy),
7777
SID(SID),
7878
txPower(txPower),
79-
rssi(rssi),
8079
periodicInterval(periodicInterval),
80+
rssi(rssi),
8181
directAddressType(directAddressType),
8282
directAddress(directAddress),
8383
advertisingData(advertisingData)
8484
{
8585
}
8686

8787
#endif
88-
8988
/** Get event type. */
9089
const advertising_event_t &getType() const
9190
{
@@ -163,18 +162,44 @@ struct AdvertisingReportEvent {
163162
return advertisingData;
164163
}
165164

165+
/** Set peer address. */
166+
void setPeerAddress(
167+
const address_t &newPeerAddress
168+
)
169+
{
170+
peerAddress = newPeerAddress;
171+
}
172+
173+
174+
/** Set peer address type. */
175+
void setPeerAddressType(
176+
const peer_address_type_t &newPeerAddressType
177+
)
178+
{
179+
peerAddressType = newPeerAddressType;
180+
}
181+
182+
/** Set new advertising payload. */
183+
void setAdvertisingData(
184+
const mbed::Span<const uint8_t> &newAdvertisingData
185+
)
186+
{
187+
advertisingData = newAdvertisingData;
188+
}
189+
166190
private:
191+
address_t peerAddress;
167192
advertising_event_t type;
168193
peer_address_type_t peerAddressType;
169-
address_t const &peerAddress;
170194
phy_t primaryPhy;
171195
phy_t secondaryPhy;
172196
advertising_sid_t SID;
173197
advertising_power_t txPower;
174-
rssi_t rssi;
175198
uint16_t periodicInterval;
199+
rssi_t rssi;
176200
peer_address_type_t directAddressType;
177-
const address_t &directAddress;
201+
address_t directAddress;
202+
ble_error_t status;
178203
mbed::Span<const uint8_t> advertisingData;
179204
};
180205

@@ -215,12 +240,12 @@ struct ConnectionCompleteEvent {
215240
) :
216241
status(status),
217242
connectionHandle(connectionHandle),
243+
peerAddress(peerAddress),
218244
ownRole(ownRole),
219245
peerAddressType(peerAddressType),
220-
peerAddress(peerAddress),
221246
localResolvablePrivateAddress(localResolvablePrivateAddress),
222-
peerResolvablePrivateAddress(peerResolvablePrivateAddress),
223247
connectionInterval(connectionInterval),
248+
peerResolvablePrivateAddress(peerResolvablePrivateAddress),
224249
connectionLatency(connectionLatency),
225250
supervisionTimeout(supervisionTimeout),
226251
masterClockAccuracy(masterClockAccuracy)
@@ -295,15 +320,46 @@ struct ConnectionCompleteEvent {
295320
return masterClockAccuracy;
296321
}
297322

323+
324+
/** Set connection complete event status. */
325+
void setStatus(ble_error_t new_status)
326+
{
327+
status = new_status;
328+
}
329+
330+
/** Set peer address type. */
331+
void setPeerAddressType(const peer_address_type_t& address_type)
332+
{
333+
peerAddressType = address_type;
334+
}
335+
336+
/** Set peer address. */
337+
void setPeerAddress(const address_t &address)
338+
{
339+
peerAddress = address;
340+
}
341+
342+
/** Set get local resolvable random address if privacy is used. */
343+
void setLocalResolvablePrivateAddress(const address_t &address)
344+
{
345+
localResolvablePrivateAddress = address;
346+
}
347+
348+
/** Set peer resolvable private address if privacy is used. */
349+
void setPeerResolvablePrivateAddress(const address_t &address)
350+
{
351+
peerResolvablePrivateAddress = address;
352+
}
353+
298354
private:
299355
ble_error_t status;
300356
connection_handle_t connectionHandle;
357+
address_t peerAddress;
301358
connection_role_t ownRole;
302359
peer_address_type_t peerAddressType;
303-
const address_t &peerAddress;
304-
const address_t &localResolvablePrivateAddress;
305-
const address_t &peerResolvablePrivateAddress;
360+
address_t localResolvablePrivateAddress;
306361
conn_interval_t connectionInterval;
362+
address_t peerResolvablePrivateAddress;
307363
slave_latency_t connectionLatency;
308364
supervision_timeout_t supervisionTimeout;
309365
uint16_t masterClockAccuracy;
@@ -519,15 +575,47 @@ struct PeriodicAdvertisingSyncLoss {
519575
*/
520576
struct ScanTimeoutEvent { };
521577

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+
522607
/**
523608
* Event produced when advertising ends.
524609
*
525610
* @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.
526614
*/
527615
struct AdvertisingEndEvent {
528616
#if !defined(DOXYGEN_ONLY)
529617

530-
/** Create advertising end event.
618+
/** Create an extended advertising end event.
531619
*
532620
* @param advHandle Advertising set handle.
533621
* @param connection Connection handle.
@@ -543,7 +631,19 @@ struct AdvertisingEndEvent {
543631
advHandle(advHandle),
544632
connection(connection),
545633
completed_events(completed_events),
546-
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)
547647
{
548648
}
549649

@@ -573,11 +673,22 @@ struct AdvertisingEndEvent {
573673
return connected;
574674
}
575675

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+
576686
private:
577687
advertising_handle_t advHandle;
578688
connection_handle_t connection;
579689
uint8_t completed_events;
580690
bool connected;
691+
bool legacy;
581692
};
582693

583694
/**

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ typedef Duration<uint16_t, 10000, Range<0x0A, 0x4000> > sync_timeout_t;
121121
*/
122122
typedef Duration<uint16_t, 1250, Range<0x06, 0xFFFF> > periodic_interval_t;
123123

124+
/**
125+
* Resolvable address timeout.
126+
*
127+
* The duration is in seconds and ranges from 1 to 0xA1B8. The default value is
128+
* 900 seconds.
129+
*/
130+
using resolvable_address_timeout_t = Duration<
131+
uint16_t, second_t::TIME_BASE, Range<1, 0xA1B8, 0x0384>
132+
>;
133+
124134
/**
125135
* Number of connection events that can be skipped by the slave.
126136
*
@@ -445,8 +455,8 @@ struct scanning_filter_policy_t : SafeEnum<scanning_filter_policy_t, uint8_t> {
445455
NO_FILTER = 0x00,
446456

447457
/**
448-
* Accept only advertising packets from devices in the whitelist except
449-
* directed advertising packets not addressed to this device.
458+
* Accept only advertising packets from devices in the whitelist.
459+
* Directed advertising packets not addressed to this device will be ignored.
450460
*/
451461
FILTER_ADVERTISING = 0x01,
452462

0 commit comments

Comments
 (0)