-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathBLELocalDevice.cpp
319 lines (251 loc) · 6.61 KB
/
BLELocalDevice.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
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. 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 "utility/ATT.h"
#include "utility/HCI.h"
#include "utility/GAP.h"
#include "utility/GATT.h"
#include "utility/L2CAPSignaling.h"
#include "BLELocalDevice.h"
#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
#ifndef BT_REG_ON
#define BT_REG_ON PJ_12
#endif
#endif
BLELocalDevice::BLELocalDevice()
{
}
BLELocalDevice::~BLELocalDevice()
{
}
int BLELocalDevice::begin()
{
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_NANO_33_IOT)
// reset the NINA in BLE mode
pinMode(SPIWIFI_SS, OUTPUT);
pinMode(NINA_RESETN, OUTPUT);
digitalWrite(SPIWIFI_SS, LOW);
#endif
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
digitalWrite(NINA_RESETN, HIGH);
delay(100);
digitalWrite(NINA_RESETN, LOW);
delay(750);
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
// inverted reset
digitalWrite(NINA_RESETN, LOW);
delay(100);
digitalWrite(NINA_RESETN, HIGH);
delay(750);
#elif defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
// BT_REG_ON -> HIGH
pinMode(BT_REG_ON, OUTPUT);
digitalWrite(BT_REG_ON, HIGH);
#endif
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
// set SS HIGH
digitalWrite(SPIWIFI_SS, HIGH);
// set RTS HIGH
pinMode(NINA_RTS, OUTPUT);
digitalWrite(NINA_RTS, HIGH);
// set CTS as input
pinMode(NINA_CTS, INPUT);
#endif
if (!HCI.begin()) {
end();
return 0;
}
delay(100);
if (HCI.reset() != 0) {
end();
return 0;
}
uint8_t hciVer;
uint16_t hciRev;
uint8_t lmpVer;
uint16_t manufacturer;
uint16_t lmpSubVer;
if (HCI.readLocalVersion(hciVer, hciRev, lmpVer, manufacturer, lmpSubVer) != 0) {
end();
return 0;
}
if (HCI.setEventMask(0x3FFFFFFFFFFFFFFF) != 0) {
end();
return 0;
}
uint16_t pktLen;
uint8_t maxPkt;
if (HCI.readLeBufferSize(pktLen, maxPkt) != 0) {
end();
return 0;
}
GATT.begin();
return 1;
}
void BLELocalDevice::end()
{
GATT.end();
HCI.end();
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
// disable the NINA
digitalWrite(NINA_RESETN, HIGH);
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
// disable the NINA
digitalWrite(NINA_RESETN, LOW);
#elif defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
// BT_REG_ON -> LOW
digitalWrite(BT_REG_ON, LOW);
#endif
}
void BLELocalDevice::poll()
{
HCI.poll();
}
void BLELocalDevice::poll(unsigned long timeout)
{
HCI.poll(timeout);
}
bool BLELocalDevice::connected() const
{
HCI.poll();
return ATT.connected();
}
bool BLELocalDevice::disconnect()
{
return ATT.disconnect();
}
String BLELocalDevice::address() const
{
uint8_t addr[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
HCI.readBdAddr(addr);
char result[18];
sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x", addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
return result;
}
int BLELocalDevice::rssi()
{
BLEDevice central = ATT.central();
if (central) {
return central.rssi();
}
return 127;
}
void BLELocalDevice::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
{
GAP.setAdvertisedServiceUuid(advertisedServiceUuid);
}
void BLELocalDevice::setAdvertisedService(const BLEService& service)
{
setAdvertisedServiceUuid(service.uuid());
}
void BLELocalDevice::setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length)
{
GAP.setAdvertisedServiceData(uuid, data, length);
}
void BLELocalDevice::setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength)
{
GAP.setManufacturerData(manufacturerData, manufacturerDataLength);
}
void BLELocalDevice::setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength)
{
GAP.setManufacturerData(companyId, manufacturerData, manufacturerDataLength);
}
void BLELocalDevice::setLocalName(const char *localName)
{
GAP.setLocalName(localName);
}
void BLELocalDevice::setDeviceName(const char* deviceName)
{
GATT.setDeviceName(deviceName);
}
void BLELocalDevice::setAppearance(uint16_t appearance)
{
GATT.setAppearance(appearance);
}
void BLELocalDevice::addService(BLEService& service)
{
GATT.addService(service);
}
int BLELocalDevice::advertise()
{
return GAP.advertise();
}
void BLELocalDevice::stopAdvertise()
{
GAP.stopAdvertise();
}
int BLELocalDevice::scan(bool withDuplicates)
{
return GAP.scan(withDuplicates);
}
int BLELocalDevice::scanForName(String name, bool withDuplicates)
{
return GAP.scanForName(name, withDuplicates);
}
int BLELocalDevice::scanForUuid(String uuid, bool withDuplicates)
{
return GAP.scanForUuid(uuid, withDuplicates);
}
int BLELocalDevice::scanForAddress(String address, bool withDuplicates)
{
return GAP.scanForAddress(address, withDuplicates);
}
void BLELocalDevice::stopScan()
{
GAP.stopScan();
}
BLEDevice BLELocalDevice::central()
{
HCI.poll();
return ATT.central();
}
BLEDevice BLELocalDevice::available()
{
HCI.poll();
return GAP.available();
}
void BLELocalDevice::setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler)
{
if (event == BLEDiscovered) {
GAP.setEventHandler(event, eventHandler);
} else {
ATT.setEventHandler(event, eventHandler);
}
}
void BLELocalDevice::setAdvertisingInterval(uint16_t advertisingInterval)
{
GAP.setAdvertisingInterval(advertisingInterval);
}
void BLELocalDevice::setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval)
{
L2CAPSignaling.setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval);
}
void BLELocalDevice::setConnectable(bool connectable)
{
GAP.setConnectable(connectable);
}
void BLELocalDevice::setTimeout(unsigned long timeout)
{
ATT.setTimeout(timeout);
}
void BLELocalDevice::debug(Stream& stream)
{
HCI.debug(stream);
}
void BLELocalDevice::noDebug()
{
HCI.noDebug();
}
BLELocalDevice BLE;