forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBLEPeripheral.cpp
375 lines (282 loc) · 11.9 KB
/
BLEPeripheral.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "BLEUuid.h"
#include "BLEDeviceLimits.h"
#include "BLEUtil.h"
#include "BLEPeripheral.h"
//#define BLE_PERIPHERAL_DEBUG
#define DEFAULT_DEVICE_NAME "Arduino"
#define DEFAULT_APPEARANCE 0x0000
BLEPeripheral::BLEPeripheral(unsigned char req, unsigned char rdy, unsigned char rst) :
#if defined(NRF51) || defined(__RFduino__)
_nRF51822(),
#else
_nRF8001(req, rdy, rst),
#endif
_localName(NULL),
_advertisedServiceUuid(NULL),
_serviceSolicitationUuid(NULL),
_manufacturerData(NULL),
_manufacturerDataLength(0),
_localAttributes(NULL),
_numLocalAttributes(0),
_remoteAttributes(NULL),
_numRemoteAttributes(0),
_genericAccessService("1800"),
_deviceNameCharacteristic("2a00", BLERead, 19),
_appearanceCharacteristic("2a01", BLERead, 2),
_genericAttributeService("1801"),
_servicesChangedCharacteristic("2a05", BLEIndicate, 4),
_remoteGenericAttributeService("1801"),
_remoteServicesChangedCharacteristic("2a05", BLEIndicate),
_central(this)
{
#if defined(NRF51) || defined(__RFduino__)
this->_device = &this->_nRF51822;
#else
this->_device = &this->_nRF8001;
#endif
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
this->setDeviceName(DEFAULT_DEVICE_NAME);
this->setAppearance(DEFAULT_APPEARANCE);
this->_device->setEventListener(this);
}
BLEPeripheral::~BLEPeripheral() {
if (this->_remoteAttributes) {
free(this->_remoteAttributes);
}
if (this->_localAttributes) {
free(this->_localAttributes);
}
}
void BLEPeripheral::begin() {
unsigned char advertisementDataType = 0;
unsigned char scanDataType = 0;
unsigned char advertisementDataLength = 0;
unsigned char scanDataLength = 0;
unsigned char advertisementData[BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH];
unsigned char scanData[BLE_SCAN_DATA_MAX_VALUE_LENGTH];
if (this->_serviceSolicitationUuid){
BLEUuid serviceSolicitationUuid = BLEUuid(this->_serviceSolicitationUuid);
advertisementDataLength = serviceSolicitationUuid.length();
advertisementDataType = (advertisementDataLength > 2) ? 0x15 : 0x14;
memcpy(advertisementData, serviceSolicitationUuid.data(), advertisementDataLength);
} else if (this->_advertisedServiceUuid){
BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid);
advertisementDataLength = advertisedServiceUuid.length();
advertisementDataType = (advertisementDataLength > 2) ? 0x06 : 0x02;
memcpy(advertisementData, advertisedServiceUuid.data(), advertisementDataLength);
} else if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
advertisementDataLength = this->_manufacturerDataLength;
if (advertisementDataLength > sizeof(advertisementData)) {
advertisementDataLength = sizeof(advertisementData);
}
advertisementDataType = 0xff;
memcpy(advertisementData, this->_manufacturerData, advertisementDataLength);
}
if (this->_localName){
unsigned char localNameLength = strlen(this->_localName);
scanDataLength = localNameLength;
if (scanDataLength > sizeof(scanData)) {
scanDataLength = sizeof(scanData);
}
scanDataType = (localNameLength > scanDataLength) ? 0x08 : 0x09;
memcpy(scanData, this->_localName, scanDataLength);
}
if (this->_localAttributes == NULL) {
this->initLocalAttributes();
}
for (int i = 0; i < this->_numLocalAttributes; i++) {
BLELocalAttribute* localAttribute = this->_localAttributes[i];
if (localAttribute->type() == BLETypeCharacteristic) {
BLECharacteristic* characteristic = (BLECharacteristic*)localAttribute;
characteristic->setValueChangeListener(*this);
}
}
for (int i = 0; i < this->_numRemoteAttributes; i++) {
BLERemoteAttribute* remoteAttribute = this->_remoteAttributes[i];
if (remoteAttribute->type() == BLETypeCharacteristic) {
BLERemoteCharacteristic* remoteCharacteristic = (BLERemoteCharacteristic*)remoteAttribute;
remoteCharacteristic->setValueChangeListener(*this);
}
}
if (this->_numRemoteAttributes) {
this->addRemoteAttribute(this->_remoteGenericAttributeService);
this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic);
}
this->_device->begin(advertisementDataType, advertisementDataLength, advertisementData,
scanDataType, scanDataLength, scanData,
this->_localAttributes, this->_numLocalAttributes,
this->_remoteAttributes, this->_numRemoteAttributes);
this->_device->requestAddress();
}
void BLEPeripheral::poll() {
this->_device->poll();
}
void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) {
this->_advertisedServiceUuid = advertisedServiceUuid;
}
void BLEPeripheral::setServiceSolicitationUuid(const char* serviceSolicitationUuid) {
this->_serviceSolicitationUuid = serviceSolicitationUuid;
}
void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) {
this->_manufacturerData = manufacturerData;
this->_manufacturerDataLength = manufacturerDataLength;
}
void BLEPeripheral::setLocalName(const char* localName) {
this->_localName = localName;
}
void BLEPeripheral::setConnectable(bool connectable) {
this->_device->setConnectable(connectable);
}
void BLEPeripheral::setBondStore(BLEBondStore& bondStore) {
this->_device->setBondStore(bondStore);
}
void BLEPeripheral::setDeviceName(const char* deviceName) {
this->_deviceNameCharacteristic.setValue(deviceName);
}
void BLEPeripheral::setAppearance(unsigned short appearance) {
this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance));
}
void BLEPeripheral::addAttribute(BLELocalAttribute& attribute) {
this->addLocalAttribute(attribute);
}
void BLEPeripheral::addLocalAttribute(BLELocalAttribute& localAttribute) {
if (this->_localAttributes == NULL) {
this->initLocalAttributes();
}
this->_localAttributes[this->_numLocalAttributes] = &localAttribute;
this->_numLocalAttributes++;
}
void BLEPeripheral::addRemoteAttribute(BLERemoteAttribute& remoteAttribute) {
if (this->_remoteAttributes == NULL) {
this->_remoteAttributes = (BLERemoteAttribute**)malloc(BLERemoteAttribute::numAttributes() * sizeof(BLERemoteAttribute*));
}
this->_remoteAttributes[this->_numRemoteAttributes] = &remoteAttribute;
this->_numRemoteAttributes++;
}
void BLEPeripheral::setAdvertisingInterval(unsigned short advertisingInterval) {
this->_device->setAdvertisingInterval(advertisingInterval);
}
void BLEPeripheral::disconnect() {
this->_device->disconnect();
}
BLECentral BLEPeripheral::central() {
this->poll();
return this->_central;
}
bool BLEPeripheral::connected() {
this->poll();
return this->_central;
}
void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) {
if (event < sizeof(this->_eventHandlers)) {
this->_eventHandlers[event] = eventHandler;
}
}
bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) {
return this->_device->updateCharacteristicValue(characteristic);
}
bool BLEPeripheral::broadcastCharacteristic(BLECharacteristic& characteristic) {
return this->_device->broadcastCharacteristic(characteristic);
}
bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) {
return this->_device->canNotifyCharacteristic(characteristic);
}
bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) {
return this->_device->canIndicateCharacteristic(characteristic);
}
bool BLEPeripheral::canReadRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->canReadRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::readRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->readRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::canWriteRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->canWriteRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::writeRemoteCharacteristic(BLERemoteCharacteristic& characteristic, const unsigned char value[], unsigned char length) {
return this->_device->writeRemoteCharacteristic(characteristic, value, length);
}
bool BLEPeripheral::canSubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->canSubscribeRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::subscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->subscribeRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::canUnsubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->canUnsubscribeRemoteCharacteristic(characteristic);
}
bool BLEPeripheral::unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) {
return this->_device->unsubcribeRemoteCharacteristic(characteristic);
}
void BLEPeripheral::BLEDeviceConnected(BLEDevice& device, const unsigned char* address) {
this->_central.setAddress(address);
#ifdef BLE_PERIPHERAL_DEBUG
Serial.print(F("Peripheral connected to central: "));
Serial.println(this->_central.address());
#endif
BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected];
if (eventHandler) {
eventHandler(this->_central);
}
}
void BLEPeripheral::BLEDeviceDisconnected(BLEDevice& device) {
#ifdef BLE_PERIPHERAL_DEBUG
Serial.print(F("Peripheral disconnected from central: "));
Serial.println(this->_central.address());
#endif
BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected];
if (eventHandler) {
eventHandler(this->_central);
}
this->_central.clearAddress();
}
void BLEPeripheral::BLEDeviceBonded(BLEDevice& device) {
#ifdef BLE_PERIPHERAL_DEBUG
Serial.print(F("Peripheral bonded: "));
Serial.println(this->_central.address());
#endif
BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEBonded];
if (eventHandler) {
eventHandler(this->_central);
}
}
void BLEPeripheral::BLEDeviceRemoteServicesDiscovered(BLEDevice& device) {
#ifdef BLE_PERIPHERAL_DEBUG
Serial.print(F("Peripheral discovered central remote services: "));
Serial.println(this->_central.address());
#endif
BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLERemoteServicesDiscovered];
if (eventHandler) {
eventHandler(this->_central);
}
}
void BLEPeripheral::BLEDeviceCharacteristicValueChanged(BLEDevice& device, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) {
characteristic.setValue(this->_central, value, valueLength);
}
void BLEPeripheral::BLEDeviceCharacteristicSubscribedChanged(BLEDevice& device, BLECharacteristic& characteristic, bool subscribed) {
characteristic.setSubscribed(this->_central, subscribed);
}
void BLEPeripheral::BLEDeviceRemoteCharacteristicValueChanged(BLEDevice& device, BLERemoteCharacteristic& remoteCharacteristic, const unsigned char* value, unsigned char valueLength) {
remoteCharacteristic.setValue(this->_central, value, valueLength);
}
void BLEPeripheral::BLEDeviceAddressReceived(BLEDevice& device, const unsigned char* address) {
#ifdef BLE_PERIPHERAL_DEBUG
char addressStr[18];
BLEUtil::addressToString(address, addressStr);
Serial.print(F("Peripheral address: "));
Serial.println(addressStr);
#endif
}
void BLEPeripheral::BLEDeviceTemperatureReceived(BLEDevice& device, float temperature) {
}
void BLEPeripheral::BLEDeviceBatteryLevelReceived(BLEDevice& device, float batteryLevel) {
}
void BLEPeripheral::initLocalAttributes() {
this->_localAttributes = (BLELocalAttribute**)malloc(BLELocalAttribute::numAttributes() * sizeof(BLELocalAttribute*));
this->_localAttributes[0] = &this->_genericAccessService;
this->_localAttributes[1] = &this->_deviceNameCharacteristic;
this->_localAttributes[2] = &this->_appearanceCharacteristic;
this->_localAttributes[3] = &this->_genericAttributeService;
this->_localAttributes[4] = &this->_servicesChangedCharacteristic;
this->_numLocalAttributes = 5;
}