Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for directed advertising mode #188

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions ble/Gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,12 @@ class Gap {
* value is used instead. This minimum value can be discovered
* using getMinAdvertisingInterval().
*
* This field must be set to 0 if connectionMode is equal
* to ADV_CONNECTABLE_DIRECTED.
* If the advertising type is equal to ADV_CONNECTABLE_DIRECTED
* then setting this to 0 enables high duty advertising. Otherwise
* the advertising interval may be set as normal. High duty mode
* sends advertisements at least every 3.75 ms. It is power and
* bandwidth intensive and is generally used for reconnections.
* See the BLE specification, Volume 6, Part B, Section 4.4.2.4.
*
* @note Decreasing this value will allow central devices to detect a
* peripheral faster, at the expense of more power being used by the radio
Expand Down Expand Up @@ -927,11 +931,27 @@ class Gap {
* @param[in] timeout
* Advertising timeout (in seconds) between 0x1 and 0x3FFF (1
* and 16383). Use 0 to disable the advertising timeout.
* This must be 0 if the advertising type is ADV_CONNECTABLE_DIRECTED
* and the advertising interval is 0 (high duty).
*/
void setAdvertisingTimeout(uint16_t timeout) {
_advParams.setTimeout(timeout);
}

/**
* Set the peer address to advertise to when the advertising mode is
* ADV_CONNECTABLE_DIRECTED. Otherwise it is ignored.
*
* Directed advertising mode includes a peer address in the advertisement
* packet. Only connection requests from that peer will be accepted.
*
* @param[in] address
* The address of the peer to advertise to.
*/
void setAdvertisingDirectAddress(BLEProtocol::Address_t address) {
_advParams.setDirectedAddress(address);
}

/**
* Start advertising.
*
Expand Down
44 changes: 34 additions & 10 deletions ble/GapAdvertisingParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef __GAP_ADVERTISING_PARAMS_H__
#define __GAP_ADVERTISING_PARAMS_H__

#include "ble/BLEProtocol.h"

/**
* This class provides a wrapper for the core advertising parameters,
* including the advertising type (Connectable Undirected,
Expand Down Expand Up @@ -78,18 +80,15 @@ class GapAdvertisingParams {
uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
uint16_t timeout = 0) : _advType(advType), _interval(interval), _timeout(timeout) {
/* Interval checks. */
if (_advType == ADV_CONNECTABLE_DIRECTED) {
/* Interval must be 0 in directed connectable mode. */
_interval = 0;
} else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED || _advType == ADV_SCANNABLE_UNDIRECTED) {
/* Min interval is slightly larger than in other modes. */
if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
_interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
}
if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
_interval = GAP_ADV_PARAMS_INTERVAL_MAX;
}
} else {
} else if (!(_advType == ADV_CONNECTABLE_DIRECTED && _interval == 0)) {
/* Stay within interval limits. */
if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) {
_interval = GAP_ADV_PARAMS_INTERVAL_MIN;
Expand All @@ -100,8 +99,11 @@ class GapAdvertisingParams {
}

/* Timeout checks. */
if (timeout) {
/* Stay within timeout limits. */
if (_timeout) {
/* Timeout must be 0 for high duty directed advertisements. */
if (_advType == ADV_CONNECTABLE_DIRECTED && _interval == 0) {
_timeout = 0;
}
if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) {
_timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
}
Expand Down Expand Up @@ -168,6 +170,16 @@ class GapAdvertisingParams {
return _timeout;
}

/**
* Get the address for directed advertisements.
* This is only used if the advertising type is set to ADV_CONNECTABLE_DIRECTED.
*
* @return The address to include in directed advertisements.
*/
BLEProtocol::Address_t getDirectAddress(void) const {
return _address;
}

/**
* Set the advertising type.
*
Expand Down Expand Up @@ -198,10 +210,22 @@ class GapAdvertisingParams {
_timeout = newTimeout;
}

/**
* Set the address to connect to for directed advertisements.
* This is only used if the advertising type is set to ADV_CONNECTABLE_DIRECTED.
*
* @param[in] newAddress
* The new address to connect to.
*/
void setDirectedAddress(BLEProtocol::Address_t newAddress) {
_address = newAddress;
}

private:
AdvertisingType_t _advType; /**< The advertising type. */
uint16_t _interval; /**< The advertising interval in ADV duration units (i.e. 0.625ms). */
uint16_t _timeout; /**< The advertising timeout in seconds. */
AdvertisingType_t _advType; /**< The advertising type. */
uint16_t _interval; /**< The advertising interval in ADV duration units (i.e. 0.625ms). */
uint16_t _timeout; /**< The advertising timeout in seconds. */
BLEProtocol::Address_t _address; /**< The address to connect to for directed advertisements. */
};

#endif /* ifndef __GAP_ADVERTISING_PARAMS_H__ */