forked from arduino-libraries/ArduinoBLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHCI.cpp
689 lines (568 loc) · 20.2 KB
/
HCI.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
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 "ATT.h"
#include "GAP.h"
#include "HCITransport.h"
#include "L2CAPSignaling.h"
#include "HCI.h"
#define HCI_COMMAND_PKT 0x01
#define HCI_ACLDATA_PKT 0x02
#define HCI_EVENT_PKT 0x04
#define EVT_DISCONN_COMPLETE 0x05
#define EVT_CMD_COMPLETE 0xe
#define EVT_CMD_STATUS 0x0f
#define EVT_NUM_COMP_PKTS 0x13
#define EVT_LE_META_EVENT 0x3e
#define EVT_LE_CONN_COMPLETE 0x01
#define EVT_LE_ADVERTISING_REPORT 0x02
#define OGF_LINK_CTL 0x01
#define OGF_HOST_CTL 0x03
#define OGF_INFO_PARAM 0x04
#define OGF_STATUS_PARAM 0x05
#define OGF_LE_CTL 0x08
// OGF_LINK_CTL
#define OCF_DISCONNECT 0x0006
// OGF_HOST_CTL
#define OCF_SET_EVENT_MASK 0x0001
#define OCF_RESET 0x0003
// OGF_INFO_PARAM
#define OCF_READ_LOCAL_VERSION 0x0001
#define OCF_READ_BD_ADDR 0x0009
// OGF_STATUS_PARAM
#define OCF_READ_RSSI 0x0005
// OGF_LE_CTL
#define OCF_LE_READ_BUFFER_SIZE 0x0002
#define OCF_LE_SET_RANDOM_ADDRESS 0x0005
#define OCF_LE_SET_ADVERTISING_PARAMETERS 0x0006
#define OCF_LE_SET_ADVERTISING_DATA 0x0008
#define OCF_LE_SET_SCAN_RESPONSE_DATA 0x0009
#define OCF_LE_SET_ADVERTISE_ENABLE 0x000a
#define OCF_LE_SET_SCAN_PARAMETERS 0x000b
#define OCF_LE_SET_SCAN_ENABLE 0x000c
#define OCF_LE_CREATE_CONN 0x000d
#define OCF_LE_CANCEL_CONN 0x000e
#define OCF_LE_CONN_UPDATE 0x0013
#define HCI_OE_USER_ENDED_CONNECTION 0x13
HCIClass::HCIClass() :
_debug(NULL),
_recvIndex(0),
_pendingPkt(0)
{
}
HCIClass::~HCIClass()
{
}
int HCIClass::begin()
{
_recvIndex = 0;
return HCITransport.begin();
}
void HCIClass::end()
{
HCITransport.end();
}
void HCIClass::poll()
{
poll(0);
}
void HCIClass::poll(unsigned long timeout)
{
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, LOW);
#endif
if (timeout) {
HCITransport.wait(timeout);
}
while (HCITransport.available()) {
byte b = HCITransport.read();
_recvBuffer[_recvIndex++] = b;
if (_recvBuffer[0] == HCI_ACLDATA_PKT) {
if (_recvIndex > 5 && _recvIndex >= (5 + (_recvBuffer[3] + (_recvBuffer[4] << 8)))) {
if (_debug) {
dumpPkt("HCI ACLDATA RX <- ", _recvIndex, _recvBuffer);
}
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, HIGH);
#endif
int pktLen = _recvIndex - 1;
_recvIndex = 0;
handleAclDataPkt(pktLen, &_recvBuffer[1]);
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, LOW);
#endif
}
} else if (_recvBuffer[0] == HCI_EVENT_PKT) {
if (_recvIndex > 3 && _recvIndex >= (3 + _recvBuffer[2])) {
if (_debug) {
dumpPkt("HCI EVENT RX <- ", _recvIndex, _recvBuffer);
}
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, HIGH);
#endif
// received full event
int pktLen = _recvIndex - 1;
_recvIndex = 0;
handleEventPkt(pktLen, &_recvBuffer[1]);
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, LOW);
#endif
}
} else {
_recvIndex = 0;
if (_debug) {
_debug->println(b, HEX);
}
}
}
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
digitalWrite(NINA_RTS, HIGH);
#endif
}
int HCIClass::reset()
{
return sendCommand(OGF_HOST_CTL << 10 | OCF_RESET);
}
int HCIClass::readLocalVersion(uint8_t& hciVer, uint16_t& hciRev, uint8_t& lmpVer, uint16_t& manufacturer, uint16_t& lmpSubVer)
{
int result = sendCommand(OGF_INFO_PARAM << 10 | OCF_READ_LOCAL_VERSION);
if (result == 0) {
struct __attribute__ ((packed)) HCILocalVersion {
uint8_t hciVer;
uint16_t hciRev;
uint8_t lmpVer;
uint16_t manufacturer;
uint16_t lmpSubVer;
} *localVersion = (HCILocalVersion*)_cmdResponse;
hciVer = localVersion->hciVer;
hciRev = localVersion->hciRev;
lmpVer = localVersion->lmpVer;
manufacturer = localVersion->manufacturer;
lmpSubVer = localVersion->lmpSubVer;
}
return result;
}
int HCIClass::readBdAddr(uint8_t addr[6])
{
int result = sendCommand(OGF_INFO_PARAM << 10 | OCF_READ_BD_ADDR);
if (result == 0) {
memcpy(addr, _cmdResponse, 6);
}
return result;
}
int HCIClass::readRssi(uint16_t handle)
{
int result = sendCommand(OGF_STATUS_PARAM << 10 | OCF_READ_RSSI, sizeof(handle), &handle);
int rssi = 127;
if (result == 0) {
struct __attribute__ ((packed)) HCIReadRssi {
uint16_t handle;
int8_t rssi;
} *readRssi = (HCIReadRssi*)_cmdResponse;
if (readRssi->handle == handle) {
rssi = readRssi->rssi;
}
}
return rssi;
}
int HCIClass::setEventMask(uint64_t eventMask)
{
return sendCommand(OGF_HOST_CTL << 10 | OCF_SET_EVENT_MASK, sizeof(eventMask), &eventMask);
}
int HCIClass::readLeBufferSize(uint16_t& pktLen, uint8_t& maxPkt)
{
int result = sendCommand(OGF_LE_CTL << 10 | OCF_LE_READ_BUFFER_SIZE);
if (result == 0) {
struct __attribute__ ((packed)) HCILeBufferSize {
uint16_t pktLen;
uint8_t maxPkt;
} *leBufferSize = (HCILeBufferSize*)_cmdResponse;
pktLen = leBufferSize->pktLen;
_maxPkt = maxPkt = leBufferSize->maxPkt;
#ifndef __AVR__
ATT.setMaxMtu(pktLen - 9); // max pkt len - ACL header size
#endif
}
return result;
}
int HCIClass::leSetRandomAddress(uint8_t addr[6])
{
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_RANDOM_ADDRESS, 6, addr);
}
int HCIClass::leSetAdvertisingParameters(uint16_t minInterval, uint16_t maxInterval,
uint8_t advType, uint8_t ownBdaddrType,
uint8_t directBdaddrType, uint8_t directBdaddr[6],
uint8_t chanMap,
uint8_t filter)
{
struct __attribute__ ((packed)) HCILeAdvertisingParameters {
uint16_t minInterval;
uint16_t maxInterval;
uint8_t advType;
uint8_t ownBdaddrType;
uint8_t directBdaddrType;
uint8_t directBdaddr[6];
uint8_t chanMap;
uint8_t filter;
} leAdvertisingParamters;
leAdvertisingParamters.minInterval = minInterval;
leAdvertisingParamters.maxInterval = maxInterval;
leAdvertisingParamters.advType = advType;
leAdvertisingParamters.ownBdaddrType = ownBdaddrType;
leAdvertisingParamters.directBdaddrType = directBdaddrType;
memcpy(leAdvertisingParamters.directBdaddr, directBdaddr, 6);
leAdvertisingParamters.chanMap = chanMap;
leAdvertisingParamters.filter = filter;
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_ADVERTISING_PARAMETERS, sizeof(leAdvertisingParamters), &leAdvertisingParamters);
}
int HCIClass::leSetAdvertisingData(uint8_t length, uint8_t data[])
{
struct __attribute__ ((packed)) HCILeAdvertisingData {
uint8_t length;
uint8_t data[31];
} leAdvertisingData;
memset(&leAdvertisingData, 0, sizeof(leAdvertisingData));
leAdvertisingData.length = length;
memcpy(leAdvertisingData.data, data, length);
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_ADVERTISING_DATA, sizeof(leAdvertisingData), &leAdvertisingData);
}
int HCIClass::leSetScanResponseData(uint8_t length, uint8_t data[])
{
struct __attribute__ ((packed)) HCILeScanResponseData {
uint8_t length;
uint8_t data[31];
} leScanResponseData;
memset(&leScanResponseData, 0, sizeof(leScanResponseData));
leScanResponseData.length = length;
memcpy(leScanResponseData.data, data, length);
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_SCAN_RESPONSE_DATA, sizeof(leScanResponseData), &leScanResponseData);
}
int HCIClass::leSetAdvertiseEnable(uint8_t enable)
{
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_ADVERTISE_ENABLE, sizeof(enable), &enable);
}
int HCIClass::leSetScanParameters(uint8_t type, uint16_t interval, uint16_t window,
uint8_t ownBdaddrType, uint8_t filter)
{
struct __attribute__ ((packed)) HCILeSetScanParameters {
uint8_t type;
uint16_t interval;
uint16_t window;
uint8_t ownBdaddrType;
uint8_t filter;
} leScanParameters;
leScanParameters.type = type;
leScanParameters.interval = interval;
leScanParameters.window = window;
leScanParameters.ownBdaddrType = ownBdaddrType;
leScanParameters.filter = filter;
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_SCAN_PARAMETERS, sizeof(leScanParameters), &leScanParameters);
}
int HCIClass::leSetScanEnable(uint8_t enabled, uint8_t duplicates)
{
struct __attribute__ ((packed)) HCILeSetScanEnableData {
uint8_t enabled;
uint8_t duplicates;
} leScanEnableData;
leScanEnableData.enabled = enabled;
leScanEnableData.duplicates = duplicates;
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_SET_SCAN_ENABLE, sizeof(leScanEnableData), &leScanEnableData);
}
int HCIClass::leCreateConn(uint16_t interval, uint16_t window, uint8_t initiatorFilter,
uint8_t peerBdaddrType, uint8_t peerBdaddr[6], uint8_t ownBdaddrType,
uint16_t minInterval, uint16_t maxInterval, uint16_t latency,
uint16_t supervisionTimeout, uint16_t minCeLength, uint16_t maxCeLength)
{
struct __attribute__ ((packed)) HCILeCreateConnData {
uint16_t interval;
uint16_t window;
uint8_t initiatorFilter;
uint8_t peerBdaddrType;
uint8_t peerBdaddr[6];
uint8_t ownBdaddrType;
uint16_t minInterval;
uint16_t maxInterval;
uint16_t latency;
uint16_t supervisionTimeout;
uint16_t minCeLength;
uint16_t maxCeLength;
} leCreateConnData;
leCreateConnData.interval = interval;
leCreateConnData.window = window;
leCreateConnData.initiatorFilter = initiatorFilter;
leCreateConnData.peerBdaddrType = peerBdaddrType;
memcpy(leCreateConnData.peerBdaddr, peerBdaddr, sizeof(leCreateConnData.peerBdaddr));
leCreateConnData.ownBdaddrType = ownBdaddrType;
leCreateConnData.minInterval = minInterval;
leCreateConnData.maxInterval = maxInterval;
leCreateConnData.latency = latency;
leCreateConnData.supervisionTimeout = supervisionTimeout;
leCreateConnData.minCeLength = minCeLength;
leCreateConnData.maxCeLength = maxCeLength;
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_CREATE_CONN, sizeof(leCreateConnData), &leCreateConnData);
}
int HCIClass::leCancelConn()
{
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_CANCEL_CONN, 0, NULL);
}
int HCIClass::leConnUpdate(uint16_t handle, uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t supervisionTimeout)
{
struct __attribute__ ((packed)) HCILeConnUpdateData {
uint16_t handle;
uint16_t minInterval;
uint16_t maxInterval;
uint16_t latency;
uint16_t supervisionTimeout;
uint16_t minCeLength;
uint16_t maxCeLength;
} leConnUpdateData;
leConnUpdateData.handle = handle;
leConnUpdateData.minInterval = minInterval;
leConnUpdateData.maxInterval = maxInterval;
leConnUpdateData.latency = latency;
leConnUpdateData.supervisionTimeout = supervisionTimeout;
leConnUpdateData.minCeLength = 0x0004;
leConnUpdateData.maxCeLength = 0x0006;
return sendCommand(OGF_LE_CTL << 10 | OCF_LE_CONN_UPDATE, sizeof(leConnUpdateData), &leConnUpdateData);
}
int HCIClass::sendAclPkt(uint16_t handle, uint8_t cid, uint8_t plen, void* data)
{
while (_pendingPkt >= _maxPkt) {
poll();
}
struct __attribute__ ((packed)) HCIACLHdr {
uint8_t pktType;
uint16_t handle;
uint16_t dlen;
uint16_t plen;
uint16_t cid;
} aclHdr = { HCI_ACLDATA_PKT, handle, uint8_t(plen + 4), plen, cid };
uint8_t txBuffer[sizeof(aclHdr) + plen];
memcpy(txBuffer, &aclHdr, sizeof(aclHdr));
memcpy(&txBuffer[sizeof(aclHdr)], data, plen);
if (_debug) {
dumpPkt("HCI ACLDATA TX -> ", sizeof(aclHdr) + plen, txBuffer);
}
_pendingPkt++;
HCITransport.write(txBuffer, sizeof(aclHdr) + plen);
return 0;
}
int HCIClass::disconnect(uint16_t handle)
{
struct __attribute__ ((packed)) HCIDisconnectData {
uint16_t handle;
uint8_t reason;
} disconnectData = { handle, HCI_OE_USER_ENDED_CONNECTION };
return sendCommand(OGF_LINK_CTL << 10 | OCF_DISCONNECT, sizeof(disconnectData), &disconnectData);
}
void HCIClass::debug(Stream& stream)
{
_debug = &stream;
}
void HCIClass::noDebug()
{
_debug = NULL;
}
int HCIClass::sendCommand(uint16_t opcode, uint8_t plen, void* parameters)
{
struct __attribute__ ((packed)) {
uint8_t pktType;
uint16_t opcode;
uint8_t plen;
} pktHdr = {HCI_COMMAND_PKT, opcode, plen};
uint8_t txBuffer[sizeof(pktHdr) + plen];
memcpy(txBuffer, &pktHdr, sizeof(pktHdr));
memcpy(&txBuffer[sizeof(pktHdr)], parameters, plen);
if (_debug) {
dumpPkt("HCI COMMAND TX -> ", sizeof(pktHdr) + plen, txBuffer);
}
HCITransport.write(txBuffer, sizeof(pktHdr) + plen);
_cmdCompleteOpcode = 0xffff;
_cmdCompleteStatus = -1;
for (unsigned long start = millis(); _cmdCompleteOpcode != opcode && millis() < (start + 1000);) {
poll();
}
return _cmdCompleteStatus;
}
void HCIClass::handleAclDataPkt(uint8_t /*plen*/, uint8_t pdata[])
{
struct __attribute__ ((packed)) HCIACLHdr {
uint16_t handle;
uint16_t dlen;
uint16_t len;
uint16_t cid;
} *aclHdr = (HCIACLHdr*)pdata;
uint16_t aclFlags = (aclHdr->handle & 0xf000) >> 12;
if ((aclHdr->dlen - 4) != aclHdr->len) {
// packet is fragmented
if (aclFlags != 0x01) {
// copy into ACL buffer
memcpy(_aclPktBuffer, &_recvBuffer[1], sizeof(HCIACLHdr) + aclHdr->dlen - 4);
} else {
// copy next chunk into the buffer
HCIACLHdr* aclBufferHeader = (HCIACLHdr*)_aclPktBuffer;
memcpy(&_aclPktBuffer[sizeof(HCIACLHdr) + aclBufferHeader->dlen - 4], &_recvBuffer[1 + sizeof(aclHdr->handle) + sizeof(aclHdr->dlen)], aclHdr->dlen);
aclBufferHeader->dlen += aclHdr->dlen;
aclHdr = aclBufferHeader;
}
}
if ((aclHdr->dlen - 4) != aclHdr->len) {
// don't have the full packet yet
return;
}
if (aclHdr->cid == ATT_CID) {
if (aclFlags == 0x01) {
// use buffered packet
ATT.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_aclPktBuffer[sizeof(HCIACLHdr)]);
} else {
// use the recv buffer
ATT.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]);
}
} else if (aclHdr->cid == SIGNALING_CID) {
L2CAPSignaling.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]);
} else {
struct __attribute__ ((packed)) {
uint8_t op;
uint8_t id;
uint16_t length;
uint16_t reason;
uint16_t localCid;
uint16_t remoteCid;
} l2capRejectCid= { 0x01, 0x00, 0x006, 0x0002, aclHdr->cid, 0x0000 };
sendAclPkt(aclHdr->handle & 0x0fff, 0x0005, sizeof(l2capRejectCid), &l2capRejectCid);
}
}
void HCIClass::handleNumCompPkts(uint16_t /*handle*/, uint16_t numPkts)
{
if (numPkts && _pendingPkt > numPkts) {
_pendingPkt -= numPkts;
} else {
_pendingPkt = 0;
}
}
void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
{
struct __attribute__ ((packed)) HCIEventHdr {
uint8_t evt;
uint8_t plen;
} *eventHdr = (HCIEventHdr*)pdata;
if (eventHdr->evt == EVT_DISCONN_COMPLETE) {
struct __attribute__ ((packed)) DisconnComplete {
uint8_t status;
uint16_t handle;
uint8_t reason;
} *disconnComplete = (DisconnComplete*)&pdata[sizeof(HCIEventHdr)];
ATT.removeConnection(disconnComplete->handle, disconnComplete->reason);
L2CAPSignaling.removeConnection(disconnComplete->handle, disconnComplete->reason);
HCI.leSetAdvertiseEnable(0x01);
} else if (eventHdr->evt == EVT_CMD_COMPLETE) {
struct __attribute__ ((packed)) CmdComplete {
uint8_t ncmd;
uint16_t opcode;
uint8_t status;
} *cmdCompleteHeader = (CmdComplete*)&pdata[sizeof(HCIEventHdr)];
_cmdCompleteOpcode = cmdCompleteHeader->opcode;
_cmdCompleteStatus = cmdCompleteHeader->status;
_cmdResponseLen = pdata[1] - sizeof(CmdComplete);
_cmdResponse = &pdata[sizeof(HCIEventHdr) + sizeof(CmdComplete)];
} else if (eventHdr->evt == EVT_CMD_STATUS) {
struct __attribute__ ((packed)) CmdStatus {
uint8_t status;
uint8_t ncmd;
uint16_t opcode;
} *cmdStatusHeader = (CmdStatus*)&pdata[sizeof(HCIEventHdr)];
_cmdCompleteOpcode = cmdStatusHeader->opcode;
_cmdCompleteStatus = cmdStatusHeader->status;
_cmdResponseLen = 0;
} else if (eventHdr->evt == EVT_NUM_COMP_PKTS) {
uint8_t numHandles = pdata[sizeof(HCIEventHdr)];
uint16_t* data = (uint16_t*)&pdata[sizeof(HCIEventHdr) + sizeof(numHandles)];
for (uint8_t i = 0; i < numHandles; i++) {
handleNumCompPkts(data[0], data[1]);
data += 2;
}
} else if (eventHdr->evt == EVT_LE_META_EVENT) {
struct __attribute__ ((packed)) LeMetaEventHeader {
uint8_t subevent;
} *leMetaHeader = (LeMetaEventHeader*)&pdata[sizeof(HCIEventHdr)];
if (leMetaHeader->subevent == EVT_LE_CONN_COMPLETE) {
struct __attribute__ ((packed)) EvtLeConnectionComplete {
uint8_t status;
uint16_t handle;
uint8_t role;
uint8_t peerBdaddrType;
uint8_t peerBdaddr[6];
uint16_t interval;
uint16_t latency;
uint16_t supervisionTimeout;
uint8_t masterClockAccuracy;
} *leConnectionComplete = (EvtLeConnectionComplete*)&pdata[sizeof(HCIEventHdr) + sizeof(LeMetaEventHeader)];
if (leConnectionComplete->status == 0x00) {
ATT.addConnection(leConnectionComplete->handle,
leConnectionComplete->role,
leConnectionComplete->peerBdaddrType,
leConnectionComplete->peerBdaddr,
leConnectionComplete->interval,
leConnectionComplete->latency,
leConnectionComplete->supervisionTimeout,
leConnectionComplete->masterClockAccuracy);
L2CAPSignaling.addConnection(leConnectionComplete->handle,
leConnectionComplete->role,
leConnectionComplete->peerBdaddrType,
leConnectionComplete->peerBdaddr,
leConnectionComplete->interval,
leConnectionComplete->latency,
leConnectionComplete->supervisionTimeout,
leConnectionComplete->masterClockAccuracy);
}
} else if (leMetaHeader->subevent == EVT_LE_ADVERTISING_REPORT) {
struct __attribute__ ((packed)) EvtLeAdvertisingReport {
uint8_t status;
uint8_t type;
uint8_t peerBdaddrType;
uint8_t peerBdaddr[6];
uint8_t eirLength;
uint8_t eirData[31];
} *leAdvertisingReport = (EvtLeAdvertisingReport*)&pdata[sizeof(HCIEventHdr) + sizeof(LeMetaEventHeader)];
if (leAdvertisingReport->status == 0x01) {
// last byte is RSSI
int8_t rssi = leAdvertisingReport->eirData[leAdvertisingReport->eirLength];
GAP.handleLeAdvertisingReport(leAdvertisingReport->type,
leAdvertisingReport->peerBdaddrType,
leAdvertisingReport->peerBdaddr,
leAdvertisingReport->eirLength,
leAdvertisingReport->eirData,
rssi);
}
}
}
}
void HCIClass::dumpPkt(const char* prefix, uint8_t plen, uint8_t pdata[])
{
if (_debug) {
_debug->print(prefix);
for (uint8_t i = 0; i < plen; i++) {
byte b = pdata[i];
if (b < 16) {
_debug->print("0");
}
_debug->print(b, HEX);
}
_debug->println();
_debug->flush();
}
}
HCIClass HCI;