Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Advertiser.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Sep 13, 2019
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
10#include <ble/cs_Nordic.h>
11#include <ble/cs_ServiceData.h>
12#include <ble/cs_Stack.h>
13#include <ble/cs_iBeacon.h>
14#include <cfg/cs_Config.h>
15#include <cfg/cs_StaticConfig.h>
17
18#include <cstdint>
19
21private:
23
24public:
29 static Advertiser instance;
30 return instance;
31 }
32 Advertiser(Advertiser const&) = delete;
33 void operator=(Advertiser const&) = delete;
34
43 void init();
44
48 void setDeviceName(const std::string& deviceName);
49
53 void setAdvertisingInterval(uint16_t advertisingInterval);
54
60 void setTxPower(int8_t power);
61
66
71
77 void setConnectable(bool connectable);
78
86 void configureAdvertisement(IBeacon& beacon, bool asScanResponse = false);
87
95 void configureAdvertisement(ServiceData& serviceData, bool asScanResponse = false);
96
103
108
112 void handleEvent(event_t& event);
113
114private:
115 // Pointer to the BLE stack.
116 Stack* _stack = nullptr;
117
119
120 // Whether the advertiser has been initialized.
121 bool _isInitialized = false;
122
123 // Whether currently advertising. This value might be out of sync.
124 bool _advertising = false;
125
126 // Whether we should be advertising.
127 bool _wantAdvertising = false;
128
129 // Whether to start advertising on next tick.
130 bool _startOnNextTick = false;
131
132 // Advertisement handle for softdevice. Set by first call to: sd_ble_gap_adv_set_configure().
133 uint8_t _advHandle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
134
136
137 // Transmit power.
138 int8_t _txPower = 0;
139
140 // Advertisement parameter: interval.
141 uint16_t _advertisingInterval = g_ADVERTISEMENT_INTERVAL;
142
143 // Advertisement parameters for softdevice.
144 ble_gap_adv_params_t _advParams;
145
146 // Whether advertising parameters have been changed.
147 bool _advParamsChanged = false;
148
149 // Whether the advertisement is connectable.
150 bool _isConnectable = false;
151
152 // Whether we want to advertise being connectable.
153 bool _wantConnectable = true;
154
156
157 // iBeacon data field to be encoded into advertisement data.
158 ble_advdata_manuf_data_t _ibeaconManufData;
159
160 // Service data field to be encoded into advertisement data.
161 ble_advdata_service_data_t _crownstoneServiceData;
162
163 // Pointer to up to data crownstone service data.
165
166 // Advertised name
167 std::string _deviceName = "none";
168
170
176
181 ble_advdata_t _configScanResponse;
182
188
194
195 // Number of buffers for advertisement / scan response data.
196 const static uint8_t _advertisementDataBufferCount = 2;
197
198 // Size of advertisement / scan response data buffers.
199 const static uint8_t _advertisementDataBufferSize = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
200
201 // Pointers to advertisement data buffers. Half of these are scan response data buffers.
203
204 // Pointers to the currently advertised advertisement and scan response data buffer.
205 ble_gap_adv_data_t _advData;
206
207 // Keep up which buffers are currently being used in an advertisement.
209
214
221 void setAdvertisementData(ServiceData& serviceData, bool asScanResponse);
222
231 void setAdvertisementData(IBeacon& beacon, bool asScanResponse);
232
240
248
255
262
267
272
278 bool allocateAdvertisementDataBuffers(bool scanResponse);
279
286 uint8_t* getAdvertisementBuffer(bool scanResponse);
287
295 void markAdvertisementBuffer(const uint8_t* buffer, bool inUse, bool scanResponse);
296
297 void onConnect(const ble_connected_t& connectedData);
298
300
302
303 void onTick();
304
306};
Definition: cs_Advertiser.h:20
void restartAdvertising()
Stop and start advertising.
Advertiser(Advertiser const &)=delete
bool allocateAdvertisementDataBuffers(bool scanResponse)
Allocate the advertisement data buffers.
uint8_t _advHandle
Definition: cs_Advertiser.h:133
ServiceData * _serviceData
Definition: cs_Advertiser.h:164
void handleEvent(event_t &event)
Internal usage.
ble_gap_adv_data_t _advData
Definition: cs_Advertiser.h:205
bool _includeAdvertisementData
Whether the advertisement data config has been filled.
Definition: cs_Advertiser.h:187
void updateAdvertisementParams()
Updates the advertisement parameters.
void onTick()
void setTxPower(int8_t power)
Set the radio transmit power.
bool _wantAdvertising
Definition: cs_Advertiser.h:127
void operator=(Advertiser const &)=delete
ble_advdata_service_data_t _crownstoneServiceData
Definition: cs_Advertiser.h:161
ble_gap_adv_params_t _advParams
Definition: cs_Advertiser.h:144
void setConnectable(bool connectable)
Set whether to advertise being connectable.
void setNormalTxPower()
Sets TX power to normal TX power as stored in State.
int8_t _txPower
Definition: cs_Advertiser.h:138
void onDisconnect()
ble_advdata_t _configScanResponse
Scan response data config, filled by one or more advertisement data fields.
Definition: cs_Advertiser.h:181
void setConnectableAdvParams()
Sets advertisement parameters to be connectable.
Stack * _stack
Definition: cs_Advertiser.h:116
void setLowTxPower()
Sets TX power to low TX power as stored in State.
void setDeviceName(const std::string &deviceName)
Set the name of this Crownstone.
uint16_t _advertisingInterval
Definition: cs_Advertiser.h:141
bool _wantConnectable
Definition: cs_Advertiser.h:153
void configureAdvertisement(ServiceData &serviceData, bool asScanResponse=false)
Configure the advertisement to hold Crownstone service data.
void onConnectOutgoing()
void updateAdvertisementData()
Sets and updates advertisement data.
static const uint8_t _advertisementDataBufferSize
Definition: cs_Advertiser.h:199
bool _includeScanResponseData
Whether the scan response data config has been filled.
Definition: cs_Advertiser.h:193
void configureAdvertisementParameters()
Set advertisement parameters from member variables.
uint8_t * _advertisementDataBuffers[2 *_advertisementDataBufferCount]
Definition: cs_Advertiser.h:202
void onConnect(const ble_connected_t &connectedData)
std::string _deviceName
Definition: cs_Advertiser.h:167
uint8_t * getAdvertisementBuffer(bool scanResponse)
Gets an advertisement data buffer that's not in use.
void setAdvertisingInterval(uint16_t advertisingInterval)
Set the advertising interval in 0.625 ms units.
void configureAdvertisement(IBeacon &beacon, bool asScanResponse=false)
Configure the advertisement to hold iBeacon data.
ble_advdata_manuf_data_t _ibeaconManufData
Definition: cs_Advertiser.h:158
void stopAdvertising()
Stop advertising.
void printAdvertisement()
void updateTxPower()
Set the cached TX power at the softdevice.
bool _advParamsChanged
Definition: cs_Advertiser.h:147
void setNonConnectableAdvParams()
Sets advertisement parameters to be non-connectable.
static const uint8_t _advertisementDataBufferCount
Definition: cs_Advertiser.h:196
void setAdvertisementData(IBeacon &beacon, bool asScanResponse)
Sets the advertisement data.
bool _isConnectable
Definition: cs_Advertiser.h:150
bool _isInitialized
Definition: cs_Advertiser.h:121
static Advertiser & getInstance()
Get the static singleton instance.
Definition: cs_Advertiser.h:28
void init()
Initialize the advertiser.
void startAdvertising()
Start advertising.
bool _advertisementDataBuffersInUse[2 *_advertisementDataBufferCount]
Definition: cs_Advertiser.h:208
void setAdvertisementData(ServiceData &serviceData, bool asScanResponse)
Sets the advertisement data.
void markAdvertisementBuffer(const uint8_t *buffer, bool inUse, bool scanResponse)
Mark an advertisement buffer as in use / no longer in use.
ble_advdata_t _configAdvertisementData
Advertisement data config, filled by one or more advertisement data fields.
Definition: cs_Advertiser.h:175
bool _advertising
Definition: cs_Advertiser.h:124
bool _startOnNextTick
Definition: cs_Advertiser.h:130
Event listener.
Definition: cs_EventListener.h:17
Implementation of the iBeacon specification.
Definition: cs_iBeacon.h:34
Definition: cs_ServiceData.h:20
nRF51822 specific implementation of the BLEStack
Definition: cs_Stack.h:41
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
Service data.
Manufacturing data.
Universally Unique IDentifiers for BLE services and characteristics.
Definition: cs_PacketsInternal.h:214