forked from arduino/ArduinoCore-arc32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEPeripheral.cpp
405 lines (322 loc) · 10.7 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2015 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "BLEPeripheral.h"
#include "BLECharacteristic.h"
#include "BLEDescriptor.h"
#include "BLEService.h"
#include "BLEUuid.h"
#define BLE_DISCONNECT_REASON_LOCAL_TERMINATION 0x16
void
blePeripheralGapEventHandler(ble_client_gap_event_t event, struct ble_gap_event *event_data, void *param)
{
BLEPeripheral* p = (BLEPeripheral*)param;
p->handleGapEvent(event, event_data);
}
void
blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_evt_msg *event_data, void *param)
{
BLEPeripheral* p = (BLEPeripheral*)param;
p->handleGattsEvent(event, event_data);
}
BLEPeripheral::BLEPeripheral(void) :
_state(BLE_PERIPH_STATE_NOT_READY),
_advertise_service_uuid(NULL),
_local_name(NULL),
_appearance(0),
_central(this),
_attributes(NULL),
_num_attributes(0),
_last_added_characteritic(NULL)
{
memset(_event_handlers, 0x00, sizeof(_event_handlers));
ble_client_get_factory_config(&_local_bda, _device_name);
}
BLEPeripheral::~BLEPeripheral(void)
{
if (this->_attributes) {
free(this->_attributes);
}
}
bool BLEPeripheral::begin()
{
BleStatus status;
status = _init();
if (status != BLE_STATUS_SUCCESS) {
return false;
}
/* Populate advertising data
*/
_advDataInit();
status = ble_client_gap_wr_adv_data(_adv_data, _adv_data_len);
if (BLE_STATUS_SUCCESS != status) {
return false;
}
uint16_t lastServiceHandle = 0;
for (int i = 0; i < _num_attributes; i++) {
BLEAttribute* attribute = _attributes[i];
BLEAttributeType type = attribute->type();
bool addResult = false;
if (BLETypeService == type) {
BLEService* service = (BLEService*)attribute;
addResult = service->add();
lastServiceHandle = service->handle();
} else if (BLETypeCharacteristic == type) {
BLECharacteristic* characteristic = (BLECharacteristic*)attribute;
addResult = characteristic->add(lastServiceHandle);
} else if (BLETypeDescriptor == type) {
BLEDescriptor *descriptor = (BLEDescriptor*)attribute;
if (strcmp(descriptor->uuid(), "2901") == 0 ||
strcmp(descriptor->uuid(), "2902") == 0 ||
strcmp(descriptor->uuid(), "2903") == 0 ||
strcmp(descriptor->uuid(), "2904") == 0) {
continue; // skip
}
addResult = descriptor->add(lastServiceHandle);
}
if (!addResult) {
return false;
}
}
return (_startAdvertising() == BLE_STATUS_SUCCESS);
}
void
BLEPeripheral::poll()
{
// no-op for now
delay(1);
}
void
BLEPeripheral::end()
{
_stop();
}
void
BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
{
_advertise_service_uuid = advertisedServiceUuid;
}
void
BLEPeripheral::setLocalName(const char* localName)
{
_local_name = localName;
}
void
BLEPeripheral::setDeviceName(const char deviceName[])
{
memset(_device_name, 0, sizeof(_device_name));
if (deviceName && deviceName[0]) {
int len = strlen(deviceName);
if (len > BLE_MAX_DEVICE_NAME)
len = BLE_MAX_DEVICE_NAME;
memcpy(_device_name, deviceName, len);
}
}
void
BLEPeripheral::setAppearance(const uint16_t appearance)
{
_appearance = appearance;
}
void
BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler callback)
{
if (event < sizeof(_event_handlers)) {
_event_handlers[event] = callback;
}
}
void
BLEPeripheral::addAttribute(BLEAttribute& attribute)
{
if (_attributes == NULL) {
_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*));
}
_attributes[_num_attributes] = &attribute;
_num_attributes++;
BLEAttributeType type = attribute.type();
if (BLETypeCharacteristic == type) {
_last_added_characteritic = (BLECharacteristic*)&attribute;
} else if (BLETypeDescriptor == type) {
if (_last_added_characteritic) {
BLEDescriptor* descriptor = (BLEDescriptor*)&attribute;
if (strcmp("2901", descriptor->uuid()) == 0) {
_last_added_characteritic->setUserDescription(descriptor);
} else if (strcmp("2904", descriptor->uuid()) == 0) {
_last_added_characteritic->setPresentationFormat(descriptor);
}
}
}
}
bool
BLEPeripheral::disconnect()
{
BleStatus status;
if (BLE_PERIPH_STATE_CONNECTED == _state) {
status = ble_client_gap_disconnect(BLE_DISCONNECT_REASON_LOCAL_TERMINATION);
} else {
status = BLE_STATUS_WRONG_STATE;
}
return (status == BLE_STATUS_SUCCESS);
}
BLECentral
BLEPeripheral::central()
{
poll();
return _central;
}
bool
BLEPeripheral::connected()
{
poll();
return _central;
}
BleStatus
BLEPeripheral::_init()
{
BleStatus status;
int8_t txPower = 127;
if (BLE_PERIPH_STATE_NOT_READY != _state)
return BLE_STATUS_WRONG_STATE;
status = ble_client_init(blePeripheralGapEventHandler, this,
blePeripheralGattsEventHandler, this);
if (BLE_STATUS_SUCCESS != status) {
return status;
}
status = ble_client_gap_set_enable_config(_device_name, &_local_bda, _appearance, txPower);
if (BLE_STATUS_SUCCESS != status) {
return status;
}
_state = BLE_PERIPH_STATE_READY;
return BLE_STATUS_SUCCESS;
}
void
BLEPeripheral::_advDataInit(void)
{
uint8_t *adv_tmp = _adv_data;
memset(_adv_data, 0, sizeof(_adv_data));
/* Add flags */
*adv_tmp++ = 2;
*adv_tmp++ = BLE_ADV_TYPE_FLAGS;
*adv_tmp++ = BLE_SVC_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
_adv_data_len = 3;
if (_advertise_service_uuid) {
BLEUuid bleUuid = BLEUuid(_advertise_service_uuid);
struct bt_uuid uuid = bleUuid.uuid();
if (BT_UUID16 == uuid.type) {
uint8_t *adv_tmp = &_adv_data[_adv_data_len];
*adv_tmp++ = (1 + sizeof(uint16_t)); /* Segment data length */
*adv_tmp++ = BLE_ADV_TYPE_INC_16_UUID;
UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
_adv_data_len += (2 + sizeof(uint16_t));
} else if (BT_UUID128 == uuid.type) {
uint8_t *adv_tmp = &_adv_data[_adv_data_len];
*adv_tmp++ = (1 + MAX_UUID_SIZE); /* Segment data length */
*adv_tmp++ = BLE_ADV_TYPE_INC_128_UUID;
memcpy(adv_tmp, uuid.uuid128, MAX_UUID_SIZE);
_adv_data_len += (2 + MAX_UUID_SIZE);
}
}
if (_local_name) {
/* Add device name (truncated if too long) */
uint8_t calculated_len;
adv_tmp = &_adv_data[_adv_data_len];
if (_adv_data_len + strlen(_local_name) + 2 <= BLE_MAX_ADV_SIZE) {
*adv_tmp++ = strlen(_local_name) + 1;
*adv_tmp++ = BLE_ADV_TYPE_COMP_LOCAL_NAME;
calculated_len = strlen(_local_name);
} else {
*adv_tmp++ = BLE_MAX_ADV_SIZE - _adv_data_len - 1;
*adv_tmp++ = BLE_ADV_TYPE_SHORT_LOCAL_NAME;
calculated_len = BLE_MAX_ADV_SIZE - _adv_data_len - 2;
}
memcpy(adv_tmp, _local_name, calculated_len);
_adv_data_len += calculated_len + 2;
}
}
BleStatus
BLEPeripheral::_startAdvertising()
{
BleStatus status;
if (_state != BLE_PERIPH_STATE_READY)
return BLE_STATUS_WRONG_STATE;
status = ble_client_gap_start_advertise(0); // 0 = no timeout
if (BLE_STATUS_SUCCESS != status)
return status;
_state = BLE_PERIPH_STATE_ADVERTISING;
return BLE_STATUS_SUCCESS;
}
BleStatus
BLEPeripheral::_stop(void)
{
BleStatus status;
if (BLE_PERIPH_STATE_ADVERTISING == _state)
status = ble_client_gap_stop_advertise();
else
status = disconnect();
if (BLE_STATUS_SUCCESS != status)
return status;
_state = BLE_PERIPH_STATE_READY;
return BLE_STATUS_SUCCESS;
}
void
BLEPeripheral::handleGapEvent(ble_client_gap_event_t event, struct ble_gap_event *event_data)
{
if (BLE_CLIENT_GAP_EVENT_CONNECTED == event) {
_state = BLE_PERIPH_STATE_CONNECTED;
_central.setAddress(event_data->connected.peer_bda);
if (_event_handlers[BLEConnected]) {
_event_handlers[BLEConnected](_central);
}
} else if (BLE_CLIENT_GAP_EVENT_DISCONNECTED == event) {
for (int i = 0; i < _num_attributes; i++) {
BLEAttribute* attribute = _attributes[i];
if (attribute->type() == BLETypeCharacteristic) {
BLECharacteristic* characteristic = (BLECharacteristic*)attribute;
characteristic->setCccdValue(_central, 0x0000); // reset CCCD
}
}
if (_event_handlers[BLEDisconnected])
_event_handlers[BLEDisconnected](_central);
_state = BLE_PERIPH_STATE_READY;
_central.clearAddress();
_startAdvertising();
} else if (BLE_CLIENT_GAP_EVENT_CONN_TIMEOUT == event) {
_state = BLE_PERIPH_STATE_READY;
_startAdvertising();
}
}
void
BLEPeripheral::handleGattsEvent(ble_client_gatts_event_t event, struct ble_gatts_evt_msg *event_data)
{
if (BLE_CLIENT_GATTS_EVENT_WRITE == event) {
uint16_t handle = event_data->wr.attr_handle;
for (int i = 0; i < _num_attributes; i++) {
BLEAttribute* attribute = _attributes[i];
if (attribute->type() != BLETypeCharacteristic) {
continue;
}
BLECharacteristic* characteristic = (BLECharacteristic*)attribute;
if (characteristic->valueHandle() == handle) {
characteristic->setValue(_central, event_data->wr.data, event_data->wr.len);
break;
} else if (characteristic->cccdHandle() == handle) {
uint16_t cccdValue = 0;
memcpy(&cccdValue, event_data->wr.data, event_data->wr.len);
characteristic->setCccdValue(_central, cccdValue);
break;
}
}
}
}