forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBLEPeripheral.cpp
235 lines (174 loc) · 6.93 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
#include "BLEUuid.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) :
_nRF8001(req, rdy, rst),
_localName(NULL),
_manufacturerData(NULL),
_manufacturerDataLength(0),
_attributes(NULL),
_numAttributes(0),
_genericAccessService("1800"),
_deviceNameCharacteristic("2a00", BLERead, 19),
_appearanceCharacteristic("2a01", BLERead, 2),
_genericAttributeService("1801"),
_servicesChangedCharacteristic("2a05", BLEIndicate, 4),
_central(this)
{
memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers));
this->setDeviceName(DEFAULT_DEVICE_NAME);
this->setAppearance(DEFAULT_APPEARANCE);
this->_nRF8001.setEventListener(this);
}
BLEPeripheral::~BLEPeripheral() {
if (this->_attributes) {
free(this->_attributes);
}
}
void BLEPeripheral::begin() {
unsigned char advertisementData[20];
unsigned char scanData[20];
unsigned char advertisementDataLength = 0;
unsigned char scanDataLength = 0;
if (this->_advertisedServiceUuid){
BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid);
unsigned char advertisedServiceUuidLength = advertisedServiceUuid.length();
advertisementDataLength = 2 + advertisedServiceUuidLength;
advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02;
advertisementData[1] = advertisedServiceUuidLength;
memcpy(&advertisementData[2], advertisedServiceUuid.data(), advertisedServiceUuidLength);
} else if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
if (this->_manufacturerDataLength > sizeof(advertisementData)) {
this->_manufacturerDataLength = sizeof(advertisementData);
}
advertisementDataLength = 2 + this->_manufacturerDataLength;
advertisementData[0] = 0xff;
advertisementData[1] = this->_manufacturerDataLength;
memcpy(&advertisementData[2], this->_manufacturerData, this->_manufacturerDataLength);
}
if (this->_localName){
unsigned char originalLocalNameLength = strlen(this->_localName);
unsigned char localNameLength = originalLocalNameLength;
if (localNameLength > sizeof(scanData)) {
localNameLength = sizeof(scanData);
}
scanDataLength = 2 + localNameLength;
scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09;
scanData[1] = localNameLength;
memcpy(&scanData[2], this->_localName, localNameLength);
}
for (int i = 0; i < this->_numAttributes; i++) {
BLEAttribute* attribute = this->_attributes[i];
if (attribute->type() == BLETypeCharacteristic) {
BLECharacteristic* characteristic = (BLECharacteristic*)attribute;
characteristic->setValueChangeListener(*this);
}
}
this->_nRF8001.begin(advertisementData, advertisementDataLength, scanData, scanDataLength, this->_attributes, this->_numAttributes);
this->_nRF8001.requestAddress();
}
void BLEPeripheral::poll() {
this->_nRF8001.poll();
}
void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) {
this->_advertisedServiceUuid = advertisedServiceUuid;
}
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::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(BLEAttribute& attribute) {
if (this->_attributes == NULL) {
this->_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*));
this->_attributes[0] = &this->_genericAccessService;
this->_attributes[1] = &this->_deviceNameCharacteristic;
this->_attributes[2] = &this->_appearanceCharacteristic;
this->_attributes[3] = &this->_genericAttributeService;
this->_attributes[4] = &this->_servicesChangedCharacteristic;
this->_numAttributes = 5;
}
this->_attributes[this->_numAttributes] = &attribute;
this->_numAttributes++;
}
void BLEPeripheral::disconnect() {
this->_nRF8001.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->_nRF8001.updateCharacteristicValue(characteristic);
}
bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) {
return this->_nRF8001.canNotifyCharacteristic(characteristic);
}
bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) {
return this->_nRF8001.canIndicateCharacteristic(characteristic);
}
void BLEPeripheral::nRF8001Connected(nRF8001& nRF8001, 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::nRF8001Disconnected(nRF8001& nRF8001) {
#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::nRF8001CharacteristicValueChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) {
characteristic.setValue(this->_central, value, valueLength);
}
void BLEPeripheral::nRF8001CharacteristicSubscribedChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, bool subscribed) {
characteristic.setSubscribed(this->_central, subscribed);
}
void BLEPeripheral::nRF8001AddressReceived(nRF8001& nRF8001, const unsigned char* address) {
#ifdef BLE_PERIPHERAL_DEBUG
char addressStr[18];
sprintf(addressStr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
address[5],
address[4],
address[3],
address[2],
address[1],
address[0]);
Serial.print(F("Peripheral address: "));
Serial.println(addressStr);
#endif
}
void BLEPeripheral::nRF8001TemperatureReceived(nRF8001& nRF8001, float temperature) {
}
void BLEPeripheral::nRF8001BatteryLevelReceived(nRF8001& nRF8001, float batteryLevel) {
}