nrfcxx
0.1.0
C++-17 Framework for Nordic nRF5 Devices
|
Helper to schedule and construct beacons. More...
#include <nrfcxx/sd/beacon.hpp>
Data Structures | |
struct | frame_prefix_s |
Structure providing the prefix for most beacon content. More... | |
struct | telemetry_state_type |
State retained across resets that is used in telemetry beacons. More... | |
Public Types | |
enum | state_e : uint8_t { INVALID = 0, FAILED, INACTIVE, CANCELLED, SCHEDULED_PREPARE, SCHEDULED, READY, ACTIVE } |
Values representing the beacon state. More... | |
using | crc_type = uint16_t |
Natively-supported checksum is CRC-16/DNP. | |
Public Member Functions | |
virtual | ~Beacon () |
The beacon is stopped on destruction. | |
state_e | state () const |
Return the current beacon state. More... | |
int | validate () const |
Test whether the beacon is in a valid state. More... | |
unsigned int | min_interval_utt () const |
Minimum interval between transmissions, in uptime ticks. | |
unsigned int | interval_utt () const |
Current interval between transmissions, in uptime ticks. | |
unsigned int | max_interval_utt () const |
Maximum interval between transmissions, in uptime ticks. | |
unsigned int | prepare_backoff_utt () const |
Duration before beacon transmission that a pre-transmission notification will occur, in uptime ticks. | |
int | set_interval (unsigned int utt) |
Configure a fixed interval. More... | |
int | set_interval (unsigned int min_utt, unsigned int max_utt) |
Configure an exponential back-off interval. More... | |
int | set_prepare_backoff (notifier_type notify, unsigned int backoff_utt) |
Configure notification that a beacon will transmit soon. More... | |
void | set_tx_notify (notifier_type notify) |
Configure notification that a beacon is being sent. More... | |
uint8_t | dt_flags () const |
Get the GAP ASD Flags data type value to use in the beacon. | |
void | dt_flags (uint8_t v) |
Set the GAP ASD Flags data type value to use in the beacon. | |
int8_t | dt_tx_power () const |
Get the GAP ASD Tx Power data type value to use in the beacon. | |
void | dt_tx_power (int8_t v) |
Set the GAP ASD Tx Power data type value to use in the beacon. | |
int | reset_interval () |
Reset the interval so the beacon is retransmitted as quickly as possible. More... | |
int | activate () |
Enable the beacon. More... | |
int | deactivate () |
Disable the beacon. | |
unsigned int | tx_count () const |
Return the number of transmissions since activation. | |
template<typename FT > | |
crc_type | update_crc (FT *bp) |
Helper to store a checksum immediately following a frame instance. More... | |
Static Public Member Functions | |
static const telemetry_state_type & | telemetry_state () |
Access the current telemetry state. | |
static const Beacon * | active () |
static void | set_notify (notifier_type notify) |
Register the notifier that signals application to process a beacon event. | |
static int | process_event () |
Method to be invoked by main loop when a beacon-related event occurs. | |
static int | process_completion () |
Method to be invoked by main loop when the beacon has been transmitted. | |
static void | telemetry_state_setup (const systemState::state_type &ss, bool is_reset, bool retained) |
Function to maintain beacon telemetry state across resets. More... | |
Static Public Attributes | |
static constexpr size_t | MAX_MSD_LENGTH = 19 + sizeof(crc_type) |
The maximum length of a Manufacturer Specific Data PDU. More... | |
static constexpr uint16_t | COMPANY_ID = -1 |
The value to be used as the company ID for manufacturer specific data. More... | |
static constexpr uint8_t | APP_FRAME_TYPE_BASE = 0x80 |
Minimum value for frame_prefix_s::frame_type available for application beacons. More... | |
static constexpr uint8_t | APP_FRAME_TYPE_LIMIT = 0xEF |
Maximum value for frame_prefix_s::frame_type available for application beacons. More... | |
static constexpr uint8_t | FRAME_TYPE_TEST = 0xFF |
Frame type reserved for test applications. | |
Protected Member Functions | |
Beacon () | |
Construct a new beacon. | |
virtual int | pre_activate_ () |
Called by activate() to make sure everything needed for activation is present. | |
crc_type | update_crc_ (uint8_t *sp, size_t span) |
Function to calculate a checksum and store it after the data. More... | |
virtual int | populate_ (pabigot::ble::gap::adv_data &ad) |
Function that must be overridden to fill in beacon content. More... | |
Helper to schedule and construct beacons.
This is a base class associated with a specific beacon, e.g. sensor values or telemetry. The beacon interval is configured to range from a minimum to a maximum, with exponential back-off until a reset_interval() operation causes the interval to be reduced to its minimum. Prior to transmission an optional callback is invoked to update the beacon content. Beacons can be disabled. Multiple beacons can be active simultaneously.
Subclasses should override populate_(). When designing a beacon payload, consider that the standard BLE 4 advertising packet is 31 octets, which may be used this way:
Applications must configure the Beacon infrastructure using set_notify(), then invoke process_event() when the corresponding notification event is received. They must also register for soft-device radio notifications and invoke process_completion() when the radio off event is received.
Currently defined infrastructure frame types include
Registered application beacons supported by various examples include:
enum nrfcxx::sd::Beacon::state_e : uint8_t |
Values representing the beacon state.
Most transitions are performed under application control, either during setup or in response to events. The following transitions will occur asynchronously and may be delayed using nrfcxx::clock::uptime::mutex_type:
Enumerator | |
---|---|
INVALID | Indicates that the beacon is unusable. Generally this means some configurable parameter such as interval is not valid. Transition out of this state can only occur if the invalid state is corrected. |
FAILED | Indicates that an attempt to transmit the beacon failed. This may be due to invalid parameters or an overflow in packing the beacon content. In either case, transition out of this state is not supported. |
INACTIVE | Indicates the beacon is not scheduled to transmit, but may be activated in the future. Transitions to SCHEDULED_PREPARE or SCHEDULED on activate(). |
CANCELLED | Indicates a beacon that was deactivated while it was active. Transitions to INACTIVE when the transmission completes. |
SCHEDULED_PREPARE | Indicates that the beacon is scheduled for a pre-transmission notification. Transitions to INACTIVE on deactivate(), and to SCHEDULED when the alarm fires.
|
SCHEDULED | Indicates the beacon is scheduled for a future transmission. Transitions to INACTIVE on deactivate(), and to READY when the alarm fires.
|
READY | Indicates the beacon should transmit as soon as the radio is available. Transitions to ACTIVE or FAILED when the radio becomes available and all earlier READY beacons complete, and to INACTIVE if cancelled before transmission starts. |
ACTIVE | Indicates the beacon is actively transmitting. Transitions to INACTIVE, SCHEDULED_PREPARE, or SCHEDULED when the transmission completes, and to CANCELLED on deactivate(). |
int nrfcxx::sd::Beacon::activate | ( | ) |
Enable the beacon.
Subclasses may override pre_activate_() to confirm that their requirements have been met. Any non-zero value returned from that function will be returned from this function, aborting the activation process.
|
inlineprotectedvirtual |
Function that must be overridden to fill in beacon content.
The return value is interpreted in this way:
ad
is not valid the result will be as if the function had returned -EINVAL
and the beacon will be failed.ad | an advertising data structure, with the Flags and (if nonzero) TxPower data items already populated. |
Reimplemented in nrfcxx::sd::GenericBeacon< CONTENT_TYPE, ID >.
int nrfcxx::sd::Beacon::reset_interval | ( | ) |
Reset the interval so the beacon is retransmitted as quickly as possible.
min_interval_utt() still applies: a reset immediately after a transmission will incur a delay of at least min_interval_utt() until the next transmission. A reset at least min_interval_utt() after the last transmission will result in a transmission as soon as the radio is available.
int nrfcxx::sd::Beacon::set_interval | ( | unsigned int | min_utt, |
unsigned int | max_utt | ||
) |
Configure an exponential back-off interval.
Invoking reset_interval() sets the interval back to the minimum and transmits ASAP; afterwards the interval doubles until it reachs the maximum, where it stays until reset again.
min_utt | the minimum interval between scheduled beacons, in uptime ticks. The value must be nonzero and strictly greater than prepare_backoff_utt(). |
max_utt | the maximum interval between scheduled beacons, in uptime ticks. The value must be no less than min_utt . |
|
inline |
Configure a fixed interval.
utt | the interval between scheduled beacons, in uptime ticks. The value must be nonzero. |
int nrfcxx::sd::Beacon::set_prepare_backoff | ( | notifier_type | notify, |
unsigned int | backoff_utt | ||
) |
Configure notification that a beacon will transmit soon.
This can be used to collect information for an upcoming beacon, e.g. measuring Vdd just before a telemetry beacon will be generated.
There is no coordination between this notification and the transmission; if the application does not react to the notification in a timely manner the beacon will still transmit on schedule.
notify | the notifier that the beacon will transmit soon. Pass a null notifier to disable the feature. |
backoff_utt | the duration before the next transmission at which the notifier will be invoked. This parameter must be strictly less than min_interval_utt(). |
|
inline |
Configure notification that a beacon is being sent.
notify
will be invoked when the soft-device is successfully invoked to transmit the beacon.
notify | the notifier that the beacon will transmit soon. |
|
inline |
Return the current beacon state.
|
static |
Function to maintain beacon telemetry state across resets.
This must be invoked from the application's systemState app_handler to ensure beacon telemetry state is properly managed.
|
inline |
Helper to store a checksum immediately following a frame instance.
This calculates a CRC-16/DNP checksum over the entire value of a (packed) structure, and stores it in residue-compatible format immediately following the data.
FT | a structure containing beacon data, often within a Manufacturer Specific Data block that's allocated to hold the structure plus the checksum. |
bp | pointer to the prepared beacon data. The checksum will be stored immediate after object. |
|
protected |
Function to calculate a checksum and store it after the data.
sp | pointer to a block of data |
span | the number of octets at sp to be covered by the checksum |
sp
+ span
. int nrfcxx::sd::Beacon::validate | ( | ) | const |
|
staticconstexpr |
Minimum value for frame_prefix_s::frame_type available for application beacons.
Values less than this are reserved for infrastructure-defined beacons.
|
staticconstexpr |
Maximum value for frame_prefix_s::frame_type available for application beacons.
Values less than this are reserved for infrastructure-defined beacons.
|
staticconstexpr |
The value to be used as the company ID for manufacturer specific data.
The standard beacons convey information in an MSD data instance, where the first two octets correspond to frame_prefix_s.
|
staticconstexpr |
The maximum length of a Manufacturer Specific Data PDU.
This excludes the the length, flag, and company id, but includes the terminating CRC-16/DNP present in some beacon payloads.