forked from arduino/ArduinoCore-arc32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEDeviceManager.h
497 lines (442 loc) · 17.8 KB
/
BLEDeviceManager.h
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
BLE Device API
Copyright (c) 2016 Arduino LLC. All right 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ARDUINO_BLE_DEVICE_MANAGER_H
#define ARDUINO_BLE_DEVICE_MANAGER_H
#include <Arduino.h>
class BLEDeviceManager
{
public:
/**
* @brief The BLE device constructure
*
* @param bleaddress BLE device address
*
* @return none
*
* @note none
*/
BLEDeviceManager();
virtual ~BLEDeviceManager();
/**
* @brief Initiliaze the BLE hardware
*
* @return bool indicating success or error
*
* @note This method are for real BLE device.
* Not for peer BLE device.
*/
bool begin(BLEDevice *device);
/**
* @brief Poll for events
*
* @param none
*
* @return none
*
* @note This method are for real BLE device.
* Not for peer BLE device.
*/
void poll(); // Do we need add the return value or
// input parameter to get the events?
// Events may inlcue:
// GAP : Connected, Disconnected, Update connetion parameter
// GATT: Discovered
/**
* @brief Deinitiliaze the BLE hardware
*
* @param none
*
* @return none
*
* @note This method are for real BLE device.
* Not for peer BLE device.
*/
void end();
/**
* @brief Is the device connected with another BLE device.
*
* @param none
*
* @return none
*
* @note none
*/
bool connected(const BLEDevice *device) const;
/**
* @brief Disconnect the connected device/s.
*
* @param none
*
* @return none
*
* @note The BLE may connected multiple devices.
* This call will disconnect all conected devices.
*/
bool disconnect(BLEDevice *device);
void setEventHandler(BLEDeviceEvent event,
BLEDeviceEventHandler eventHandler);
/**
* @brief Set the service UUID that the BLE Peripheral Device advertises
*
* @param[in] advertisedServiceUuid 16-bit or 128-bit UUID to advertis
* (in string form)
*
* @note This method must be called before the begin method
* Only for peripheral mode.
*/
void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
/**
* @brief Set the service UUID that is solicited in the BLE Peripheral
* Device advertises
*
* @param[in] advertisedServiceUuid 16-bit or 128-bit UUID to advertis
* (in string form)
*
* @note This method must be called before the begin method
* Only for peripheral mode.
*/
void setServiceSolicitationUuid(const char* serviceSolicitationUuid);
void setAdvertisedServiceData(const bt_uuid_t* serviceDataUuid,
const uint8_t* serviceData,
uint8_t serviceDataLength);
/**
* @brief Set the manufacturer data in the BLE Peripheral Device advertises
*
* @param[in] manufacturerData The data about manufacturer will
* be set in advertisement
* @param[in] manufacturerDataLength The length of the manufacturer data
*
* @note This method must be called before the begin method
* Only for peripheral mode.
*/
void setManufacturerData(const unsigned char manufacturerData[],
unsigned char manufacturerDataLength);
bool getManufacturerData (const BLEDevice* device,
uint8_t* manu_data,
uint8_t&manu_data_len) const;
bool hasManufacturerData(const BLEDevice* device) const;
/**
* Set the local name that the BLE Peripheral Device advertises
*
* @param[in] localName local name to advertise
*
* @note This method must be called before the begin method
*/
void setLocalName(const char *localName);
/**
* @brief Set advertising interval
*
* @param[in] advertisingInterval Advertising Interval in ms
*
* @return none
*
* @note none
*/
void setAdvertisingInterval(float advertisingInterval);
/**
* @brief Set the connection parameters and send connection
* update request in both BLE peripheral and central
*
* @param[in] intervalmin Minimum Connection Interval (ms)
*
* @param[in] intervalmax Maximum Connection Interval (ms)
*
* @param[in] latency Connection Latency
*
* @param[in] timeout Supervision Timeout (ms)
*
* @return none
*
* @note none
*/
void setConnectionInterval(float minimumConnectionInterval,
float maximumConnectionInterval,
uint16_t latency,
uint16_t timeout);
/**
* @brief Set the min and max connection interval and send connection
* update request in both BLE peripheral and central
*
* @param[in] intervalmin Minimum Connection Interval (ms)
*
* @param[in] intervalmax Maximum Connection Interval (ms)
*
* @return none
*
* @note none
*/
void setConnectionInterval(float minimumConnectionInterval,
float maximumConnectionInterval);
/**
* @brief Set TX power of the radio in dBM
*
* @param[in] tx_power The antenna TX power
*
* @return boolean_t true if established connection, otherwise false
*/
bool setTxPower(int txPower);
/**
* @brief Set advertising type as connectable/non-connectable
*
* @param[in] connectable true - The device connectable
* false - The device non-connectable
*
* @return none
*
* @note Only for peripheral mode.
* Default value is connectable
*/
void setConnectable(bool connectable);
/**
* @brief Set the value of the device name characteristic
*
* @param[in] device User-defined name string for this device. Truncated if
* more than maximum allowed string length (20 bytes).
*
* @note This method must be called before the begin method
* If device name is not set, a default name will be used
*/
void setDeviceName(const char* deviceName);
void setDeviceName();
/**
* @brief Set the appearance type for the BLE Peripheral Device
*
* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
* for available options.
*
* @param[in] appearance Appearance category identifier as defined by BLE Standard
*
* @return BleStatus indicating success or error
*
* @note This method must be called before the begin method
*/
void setAppearance(unsigned short appearance);
/**
* @brief Add a Service to the BLE Peripheral Device
*
* @param[in] attribute The service that will add to Peripheral
*
* @return BLE_STATUS_T Indicating success or error type
*
* @note This method must be called before the begin method
*/
BLE_STATUS_T addService(BLEService& attribute);
/**
* @brief Construct the ADV data and start send advertisement
*
* @param none
*
* @return BLE_STATUS_T 0 - Success. Others - error code
*
* @note none
*/
BLE_STATUS_T startAdvertising();
bool advertising();
/**
* @brief Stop send advertisement
*
* @param none
*
* @return none
*
* @note none
*/
BLE_STATUS_T stopAdvertising();
/**
* @brief Get currently connected central
*
* @return BLEDeviceManager Connected central device
*
* @note Peripheral mode only
*/
BLEDevice central();
/**
* @brief Get currently connected peripheral
*
* @param none
*
* @return none
*
* @note Central mode only. How to distinguish the peripheral?
*/
BLEDevice peripheral();
operator bool() const;
// central mode
void clearAdvertiseCritical();
void setAdvertiseCritical(String name);
void setAdvertiseCritical(BLEService& service);
void setAdvertiseCritical(const char* macaddress);
bool startScanningNewPeripherals(); // start scanning for new peripherals, don't report the detected ones
bool startScanningWithDuplicates(); // start scanning for peripherals, and report all duplicates
bool stopScanning(); // stop scanning for peripherals
void setAcceptAdvertiseLocalName(String name);
void setAcceptAdvertiseLocalName(BLEService& service);
void setAcceptAdvertiseCallback(String name);
BLEDevice available(); // retrieve a discovered peripheral
bool hasLocalName(const BLEDevice* device) const; // does the peripheral advertise a local name
bool hasAdvertisedServiceUuid(const BLEDevice* device) const; // does the peripheral advertise a service
bool hasAdvertisedServiceUuid(const BLEDevice* device, int index) const; // does the peripheral advertise a service n
int advertisedServiceUuidCount(const BLEDevice* device) const; // number of services the peripheral is advertising
String localName(const BLEDevice* device) const; // returns the advertised local name as a String
String advertisedServiceUuid(const BLEDevice* device) const; // returns the advertised service as a UUID String
String advertisedServiceUuid(const BLEDevice* device, int index) const; // returns the nth advertised service as a UUID String
int rssi(const BLEDevice* device) const; // returns the RSSI of the peripheral at discovery
bool connect(BLEDevice &device); // connect to the peripheral
bool connectToDevice(BLEDevice &device);
String deviceName(const BLEDevice* device); // read the device name attribute of the peripheral, and return String value
int appearance(); // read the appearance attribute of the peripheral and return value as int
static BLEDeviceManager* instance();
void handleConnectEvent(bt_conn_t *conn, uint8_t err);
void handleDisconnectEvent(bt_conn_t *conn, uint8_t reason);
void handleParamUpdated (bt_conn_t *conn,
uint16_t interval,
uint16_t latency,
uint16_t timeout);
void handleDeviceFound(const bt_addr_le_t *addr,
int8_t rssi,
uint8_t type,
const uint8_t *ad,
uint8_t data_len);
protected:
private:
BLE_STATUS_T setAdvertiseData (uint8_t type,
const uint8_t* data,
uint8_t length);
BLE_STATUS_T _advDataInit(void);
void _clearAdvertiseBuffer();
bool advertiseDataProc(uint8_t type,
const uint8_t *dataPtr,
uint8_t data_len);
bool setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
const uint8_t *ad,
uint8_t data_len,
int8_t rssi,
bool connectable);
void getDeviceAdvertiseBuffer(const bt_addr_le_t* addr,
const uint8_t* &adv_data,
uint8_t &adv_len) const;
bool setScanRespBuffer(const bt_addr_le_t* bt_addr,
const uint8_t *ad,
uint8_t data_len,
int8_t rssi);
void getDeviceScanResponseBuffer(const bt_addr_le_t* addr,
const uint8_t* &adv_data,
uint8_t &adv_len) const;
bool getDataFromAdvertiseByType(const BLEDevice* device,
const uint8_t eir_type,
const uint8_t* &data,
uint8_t &data_len) const;
bool disconnectSingle(const bt_addr_le_t *peer);
void updateDuplicateFilter(const bt_addr_le_t* addr);
bool deviceInDuplicateFilterBuffer(const bt_addr_le_t* addr);
void advertiseAcceptHandler(const bt_addr_le_t *addr,
int8_t rssi,
uint8_t type,
const uint8_t *ad,
uint8_t data_len);
void setTempAdvertiseBuffer(const bt_addr_le_t* bt_addr,
int8_t rssi,
const uint8_t *ad,
uint8_t data_len,
bool connectable);
uint8_t getTempAdvertiseIndexFromBuffer(const bt_addr_le_t* bt_addr);
private:
uint16_t _min_conn_interval;
uint16_t _max_conn_interval;
bt_addr_le_t _local_bda;
char _device_name[BLE_MAX_DEVICE_NAME + 1];
// For Central
bt_le_scan_param_t _scan_param; // Scan parameter
bt_addr_le_t _peer_adv_buffer[BLE_MAX_ADV_BUFFER_CFG]; // Accepted peer device adress
uint64_t _peer_adv_mill[BLE_MAX_ADV_BUFFER_CFG]; // The ADV found time stamp
uint8_t _peer_adv_data[BLE_MAX_ADV_BUFFER_CFG][BLE_MAX_ADV_SIZE];
uint8_t _peer_adv_data_len[BLE_MAX_ADV_BUFFER_CFG];
uint8_t _peer_scan_rsp_data[BLE_MAX_ADV_BUFFER_CFG][BLE_MAX_ADV_SIZE];
int8_t _peer_scan_rsp_data_len[BLE_MAX_ADV_BUFFER_CFG];
int8_t _peer_adv_rssi[BLE_MAX_ADV_BUFFER_CFG];
bool _peer_adv_connectable[BLE_MAX_ADV_BUFFER_CFG];
// The accept critical may include in scan response
bt_addr_le_t _peer_temp_adv_buffer[BLE_MAX_ADV_BUFFER_CFG];
uint8_t _peer_temp_dev_index;
uint8_t _peer_temp_adv_data[BLE_MAX_ADV_BUFFER_CFG][BLE_MAX_ADV_SIZE];
uint8_t _peer_temp_adv_data_len[BLE_MAX_ADV_BUFFER_CFG];
bool _peer_temp_adv_connectable[BLE_MAX_ADV_BUFFER_CFG];
// The critical for central scan
bt_data_t _adv_accept_critical; // The filters for central device
String _adv_critical_local_name;
bt_uuid_128_t _adv_critical_service_uuid;
bt_addr_le_t _adv_accept_device;
bt_addr_le_t _wait_for_connect_peripheral;
uint8_t _wait_for_connect_peripheral_adv_data[BLE_MAX_ADV_SIZE];
uint8_t _wait_for_connect_peripheral_adv_data_len;
uint8_t _wait_for_connect_peripheral_scan_rsp_data[BLE_MAX_ADV_SIZE];
uint8_t _wait_for_connect_peripheral_scan_rsp_data_len;
int8_t _wait_for_connect_peripheral_adv_rssi;
bt_addr_le_t _available_for_connect_peripheral;
uint8_t _available_for_connect_peripheral_adv_data[BLE_MAX_ADV_SIZE];
uint8_t _available_for_connect_peripheral_adv_data_len;
uint8_t _available_for_connect_peripheral_scan_rsp_data[BLE_MAX_ADV_SIZE];
uint8_t _available_for_connect_peripheral_scan_rsp_data_len;
int8_t _available_for_connect_peripheral_adv_rssi;
bool _available_for_connect_peripheral_connectable;
volatile bool _connecting;
// For peripheral
struct bt_le_adv_param _adv_param;
bool _has_service_uuid;
bt_uuid_128_t _service_uuid;
bool _has_service_solicit_uuid;
bt_uuid_128_t _service_solicit_uuid;
uint16_t _appearance;
uint8_t _manufacturer_data[BLE_MAX_ADV_SIZE];
uint8_t _manufacturer_data_length;
bt_uuid_128_t _service_data_uuid;
uint8_t _service_data[BLE_MAX_ADV_SIZE];
uint8_t _service_data_buf[BLE_MAX_ADV_SIZE];
uint8_t _service_data_length;
// ADV data for peripheral
uint8_t _adv_type;
bt_data_t _adv_data[6]; // KW: fount _advDataInit() can use 6 slots.
size_t _adv_data_idx;
bt_data_t _scan_rsp_data[6];
size_t _scan_rsp_data_idx;
String _local_name;
// Peripheral states
enum BLEPeripheralState {
BLE_PERIPH_STATE_NOT_READY = 0,
BLE_PERIPH_STATE_READY,
BLE_PERIPH_STATE_ADVERTISING,
BLE_PERIPH_STATE_CONNECTED,
};
BLEPeripheralState _state;
// Local
static BLEDeviceManager* _instance;
BLEDevice *_local_ble;
// Connected device object
bt_addr_le_t _peer_central;
bt_addr_le_t _peer_peripheral[BLE_MAX_CONN_CFG];
uint8_t _peer_peripheral_index;
uint8_t _peer_peripheral_adv_data[BLE_MAX_CONN_CFG][BLE_MAX_ADV_SIZE];
uint8_t _peer_peripheral_adv_data_len[BLE_MAX_CONN_CFG];
uint8_t _peer_peripheral_scan_rsp_data[BLE_MAX_CONN_CFG][BLE_MAX_ADV_SIZE];
uint8_t _peer_peripheral_scan_rsp_data_len[BLE_MAX_CONN_CFG];
uint8_t _peer_peripheral_adv_rssi[BLE_MAX_CONN_CFG];
bt_addr_le_t _peer_duplicate_address_buffer[BLE_MAX_ADV_FILTER_SIZE_CFG];
uint8_t _duplicate_filter_header;
uint8_t _duplicate_filter_tail;
bool _adv_duplicate_filter_enabled;
BLEDeviceEventHandler _device_events[BLEDeviceLastEvent];
};
#endif