forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnRF8001.cpp
1563 lines (1203 loc) · 47.1 KB
/
nRF8001.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if !defined(NRF51) && !defined(__RFduino__)
// #define NRF_8001_DEBUG
// #define NRF_8001_ENABLE_DC_DC_CONVERTER
#include <SPI.h>
#include "BLECharacteristic.h"
#include "BLEDescriptor.h"
#include "BLERemoteService.h"
#include "BLERemoteCharacteristic.h"
#include "BLEService.h"
#include "BLEUtil.h"
#include "BLEUuid.h"
#include "nRF8001.h"
struct setupMsgData {
unsigned char length;
unsigned char cmd;
unsigned char type;
unsigned char offset;
unsigned char data[28];
};
#define NB_BASE_SETUP_MESSAGES 7
#define MAX_ATTRIBUTE_VALUE_PER_SETUP_MSG 28
#define DYNAMIC_DATA_MAX_CHUNK_SIZE 26
#define DYNAMIC_DATA_SIZE (DYNAMIC_DATA_MAX_CHUNK_SIZE * 6)
#if defined (__AVR__)
/* Store the setup for the nRF8001 in the flash of the AVR to save on RAM */
#if ARDUINO < 150
static /*const*/ hal_aci_data_t baseSetupMsgs[NB_BASE_SETUP_MESSAGES] PROGMEM =
#else
static const hal_aci_data_t baseSetupMsgs[NB_BASE_SETUP_MESSAGES] PROGMEM =
#endif
#else
/* Having PROGMEM here caused the setup messages to be zeroed out */
static const hal_aci_data_t baseSetupMsgs[NB_BASE_SETUP_MESSAGES] /*PROGMEM*/ =
#endif
{\
{0x00,\
{\
0x07,0x06,0x00,0x00,0x03,0x02,0x41,0xfe,\
},\
},\
{0x00,\
{\
0x1f,0x06,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x06,0x00,0x06,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
},\
},\
{0x00,\
{\
0x1f,0x06,0x10,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x03,0x90,0x00,0xff,\
},\
},\
{0x00,\
{\
0x1f,0x06,0x10,0x38,0xff,0xff,0x02,0x58,0x0a,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x10,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x01,0x02,0x01,0x02,\
},\
},\
{0x00,\
{\
0x05,0x06,0x10,0x54,0x00,0x02,\
},\
},\
{0x00,\
{\
0x19,0x06,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,\
},\
},\
{0x00,\
{\
0x19,0x06,0x70,0x16,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,\
},\
},\
};
/** crc function to re-calulate the CRC after making changes to the setup data.
*/
uint16_t crc_16_ccitt(uint16_t crc, uint8_t * data_in, uint16_t data_len) {
uint16_t i;
for(i = 0; i < data_len; i++)
{
crc = (unsigned char)(crc >> 8) | (crc << 8);
crc ^= data_in[i];
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
}
return crc;
}
nRF8001::nRF8001(BLE_Tx_Power_Level power_level, unsigned char req, unsigned char rdy, unsigned char rst) :
BLEDevice(),
_localPipeInfo(NULL),
_numLocalPipeInfo(0),
_broadcastPipe(0),
_closedPipesCleared(false),
_remoteServicesDiscovered(false),
_remotePipeInfo(NULL),
_numRemotePipeInfo(0),
_dynamicDataOffset(0),
_dynamicDataSequenceNo(0),
_storeDynamicData(false),
_crcSeed(0xFFFF),
_tx_power_level(power_level)
{
this->_aciState.aci_pins.reqn_pin = req;
this->_aciState.aci_pins.rdyn_pin = rdy;
#if !defined(SPI_HAS_TRANSACTION) || defined(__SAMD21G18A__)
#if defined(__SAM3X8E__)
this->_aciState.aci_pins.spi_clock_divider = 42;
#else
this->_aciState.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8;
#endif
#endif
this->_aciState.aci_pins.reset_pin = rst;
this->_aciState.aci_pins.active_pin = UNUSED;
this->_aciState.aci_pins.optional_chip_sel_pin = UNUSED;
}
nRF8001::~nRF8001() {
if (this->_localPipeInfo) {
free(this->_localPipeInfo);
}
if (this->_remotePipeInfo) {
free(this->_remotePipeInfo);
}
}
void nRF8001::begin(unsigned char advertisementDataType,
unsigned char advertisementDataLength,
const unsigned char* advertisementData,
unsigned char scanDataType,
unsigned char scanDataLength,
const unsigned char* scanData,
BLELocalAttribute** localAttributes,
unsigned char numLocalAttributes,
BLERemoteAttribute** remoteAttributes,
unsigned char numRemoteAttributes)
{
unsigned char numLocalPipedCharacteristics = 0;
unsigned char numLocalPipes = 0;
for (int i = 0; i < numLocalAttributes; i++) {
BLELocalAttribute* localAttribute = localAttributes[i];
if (localAttribute->type() == BLETypeCharacteristic) {
BLECharacteristic* characteristic = (BLECharacteristic *)localAttribute;
unsigned char properties = characteristic->properties();
if (properties) {
numLocalPipedCharacteristics++;
if (properties & BLEBroadcast) {
numLocalPipes++;
}
if (properties & BLENotify) {
numLocalPipes++;
}
if (properties & BLEIndicate) {
numLocalPipes++;
}
if (properties & BLEWriteWithoutResponse) {
numLocalPipes++;
}
if (properties & BLEWrite) {
numLocalPipes++;
}
if (properties & BLERead) {
numLocalPipes++;
}
}
}
}
this->_localPipeInfo = (struct localPipeInfo*)malloc(sizeof(struct localPipeInfo) * numLocalPipedCharacteristics);
unsigned char numRemoteServices = 0;
unsigned char numRemotePipedCharacteristics = 0;
unsigned char numRemotePipes = 0;
unsigned char numCustomUuids = 0;
for (int i = 0; i < numRemoteAttributes; i++) {
BLERemoteAttribute* remoteAttribute = remoteAttributes[i];
unsigned short remoteAttributeType = remoteAttribute->type();
BLEUuid uuid = BLEUuid(remoteAttribute->uuid());
if (uuid.length() > 2) {
numCustomUuids++;
}
if (remoteAttributeType == BLETypeService) {
numRemoteServices++;
} else if (remoteAttributeType == BLETypeCharacteristic) {
BLERemoteCharacteristic* characteristic = (BLERemoteCharacteristic *)remoteAttribute;
unsigned char properties = characteristic->properties();
if (properties) {
numRemotePipedCharacteristics++;
if (properties & BLENotify) {
numRemotePipes++;
}
if (properties & BLEIndicate) {
numRemotePipes++;
}
if (properties & BLEWriteWithoutResponse) {
numRemotePipes++;
}
if (properties & BLEWrite) {
numRemotePipes++;
}
if (properties & BLERead) {
numRemotePipes++;
}
}
}
}
this->_remotePipeInfo = (struct remotePipeInfo*)malloc(sizeof(struct remotePipeInfo) * numRemotePipedCharacteristics);
lib_aci_init(&this->_aciState, false);
if (this->_bondStore) {
this->_aciState.bonded = ACI_BOND_STATUS_FAILED;
this->_storeDynamicData = false;
}
this->waitForSetupMode();
hal_aci_data_t setupMsg;
struct setupMsgData* setupMsgData = (struct setupMsgData*)setupMsg.buffer;
setupMsg.status_byte = 0;
bool hasAdvertisementData = advertisementDataType && advertisementDataLength && advertisementData;
bool hasScanData = scanDataType && scanDataLength && scanData;
for (int i = 0; i < NB_BASE_SETUP_MESSAGES; i++) {
int setupMsgSize = pgm_read_byte_near(&baseSetupMsgs[i].buffer[0]) + 2;
memcpy_P(&setupMsg, &baseSetupMsgs[i], setupMsgSize);
if (i == 1) {
setupMsgData->data[5] = numRemoteServices;
setupMsgData->data[6] = numLocalPipedCharacteristics;
setupMsgData->data[7] = numRemotePipedCharacteristics;
setupMsgData->data[8] = (numLocalPipes + numRemotePipes);
if (this->_bondStore) {
setupMsgData->data[4] |= 0x02;
}
#ifdef NRF_8001_ENABLE_DC_DC_CONVERTER
setupMsgData->data[13] |= 0x01;
#endif
} else if (i == 2 && hasAdvertisementData) {
setupMsgData->data[18] |= 0x40;
setupMsgData->data[22] |= 0x40;
setupMsgData->data[26] = numCustomUuids;
} else if (i == 3) {
if (hasAdvertisementData) {
setupMsgData->data[16] |= 0x40;
}
if (hasScanData) {
setupMsgData->data[8] |= 0x40;
setupMsgData->data[12] |= 0x40;
setupMsgData->data[20] |= 0x40;
}
} else if (i == 4) {
if (this->_bondStore) {
setupMsgData->data[0] |= 0x01;
}
} else if (i == 5 && hasAdvertisementData) {
setupMsgData->data[0] = advertisementDataType;
setupMsgData->data[1] = advertisementDataLength;
memcpy(&setupMsgData->data[2], advertisementData, advertisementDataLength);
} else if (i == 6 && hasScanData) {
setupMsgData->data[0] = scanDataType;
setupMsgData->data[1] = scanDataLength;
memcpy(&setupMsgData->data[2], scanData, scanDataLength);
}
this->sendSetupMessage(&setupMsg);
}
// GATT
unsigned short gattSetupMsgOffset = 0;
unsigned short handle = 1;
unsigned char pipe = 1;
unsigned char numLocalPiped = 0;
unsigned char numRemotePiped = 0;
for (int i = 0; i < numLocalAttributes; i++) {
BLELocalAttribute* localAttribute = localAttributes[i];
unsigned short localAttributeType = localAttribute->type();
BLEUuid uuid = BLEUuid(localAttribute->uuid());
const unsigned char* uuidData = uuid.data();
unsigned char uuidLength = uuid.length();
if (localAttributeType == BLETypeService) {
BLEService* service = (BLEService *)localAttribute;
setupMsgData->length = 12 + uuidLength;
setupMsgData->data[0] = 0x04;
setupMsgData->data[1] = 0x04;
setupMsgData->data[2] = uuidLength;
setupMsgData->data[3] = uuidLength;
setupMsgData->data[4] = (handle >> 8) & 0xff;
setupMsgData->data[5] = handle & 0xff;
handle++;
setupMsgData->data[6] = (localAttributeType >> 8) & 0xff;
setupMsgData->data[7] = localAttributeType & 0xff;
setupMsgData->data[8] = ACI_STORE_LOCAL;
memcpy(&setupMsgData->data[9], uuidData, uuidLength);
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
} else if (localAttributeType == BLETypeCharacteristic) {
BLECharacteristic* characteristic = (BLECharacteristic *)localAttribute;
unsigned char properties = characteristic->properties();
const char* characteristicUuid = characteristic->uuid();
struct localPipeInfo* localPipeInfo = &this->_localPipeInfo[numLocalPiped];
memset(localPipeInfo, 0, sizeof(struct localPipeInfo));
localPipeInfo->characteristic = characteristic;
if (properties) {
numLocalPiped++;
if (properties & BLEBroadcast) {
localPipeInfo->advPipe = pipe;
pipe++;
}
if (properties & BLENotify) {
localPipeInfo->txPipe = pipe;
pipe++;
}
if (properties & BLEIndicate) {
localPipeInfo->txAckPipe = pipe;
pipe++;
}
if (properties & BLEWriteWithoutResponse) {
localPipeInfo->rxPipe = pipe;
pipe++;
}
if (properties & BLEWrite) {
localPipeInfo->rxAckPipe = pipe;
pipe++;
}
if (properties & BLERead) {
localPipeInfo->setPipe = pipe;
pipe++;
}
}
setupMsgData->length = 15 + uuidLength;
setupMsgData->data[0] = 0x04;
setupMsgData->data[1] = 0x04;
setupMsgData->data[2] = 3 + uuidLength;
setupMsgData->data[3] = 3 + uuidLength;
setupMsgData->data[4] = (handle >> 8) & 0xff;
setupMsgData->data[5] = handle & 0xff;
handle++;
setupMsgData->data[6] = (localAttributeType >> 8) & 0xff;
setupMsgData->data[7] = localAttributeType & 0xff;
setupMsgData->data[8] = ACI_STORE_LOCAL;
setupMsgData->data[9] = properties & 0xfe;
setupMsgData->data[10] = handle & 0xff;
setupMsgData->data[11] = (handle >> 8) & 0xff;
localPipeInfo->valueHandle = handle;
handle++;
memcpy(&setupMsgData->data[12], uuidData, uuidLength);
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
setupMsgData->length = 12;
setupMsgData->data[0] = 0x04;
setupMsgData->data[1] = 0x00;
if (characteristic->fixedLength()) {
setupMsgData->data[0] |= 0x02;
}
if (properties & BLERead) {
setupMsgData->data[1] |= 0x04;
}
if (this->_bondStore &&
strcmp(characteristicUuid, "2a00") != 0 &&
strcmp(characteristicUuid, "2a01") != 0 &&
strcmp(characteristicUuid, "2a05") != 0) {
setupMsgData->data[1] |= 0x08;
}
if (properties & (BLEWrite | BLEWriteWithoutResponse)) {
setupMsgData->data[0] |= 0x40;
setupMsgData->data[1] |= 0x10;
}
if (properties & BLENotify) {
setupMsgData->data[0] |= 0x10;
}
if (properties & BLEIndicate) {
setupMsgData->data[0] |= 0x20;
}
unsigned char valueSize = characteristic->valueSize();
unsigned char valueLength = characteristic->valueLength();
setupMsgData->data[2] = valueSize;
if (characteristic->fixedLength()) {
setupMsgData->data[2]++;
}
setupMsgData->data[3] = valueLength;
setupMsgData->data[4] = (localPipeInfo->valueHandle >> 8) & 0xff;
setupMsgData->data[5] = localPipeInfo->valueHandle & 0xff;
if (uuidLength == 2) {
setupMsgData->data[6] = uuidData[1];
setupMsgData->data[7] = uuidData[0];
} else {
setupMsgData->data[6] = 0x00;
setupMsgData->data[7] = 0x00;
}
setupMsgData->data[8] = ACI_STORE_LOCAL;
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
int valueOffset = 0;
while(valueOffset < valueSize) {
int chunkSize = min(valueSize - valueOffset, MAX_ATTRIBUTE_VALUE_PER_SETUP_MSG);
int valueCopySize = min(valueLength - valueOffset, chunkSize);
setupMsgData->length = 3 + chunkSize;
memset(setupMsgData->data, 0x00, chunkSize);
if (valueCopySize > 0) {
for (int j = 0; j < chunkSize; j++) {
setupMsgData->data[j] = (*characteristic)[j + valueOffset];
}
}
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
valueOffset += chunkSize;
}
if (properties & (BLENotify | BLEIndicate)) {
setupMsgData->length = 14;
setupMsgData->data[0] = 0x46;
setupMsgData->data[1] = 0x14;
setupMsgData->data[2] = 0x03;
setupMsgData->data[3] = 0x02;
setupMsgData->data[4] = (handle >> 8) & 0xff;
setupMsgData->data[5] = handle & 0xff;
localPipeInfo->configHandle = handle;
handle++;
setupMsgData->data[6] = 0x29;
setupMsgData->data[7] = 0x02;
setupMsgData->data[8] = ACI_STORE_LOCAL;
setupMsgData->data[9] = 0x00;
setupMsgData->data[10] = 0x00;
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
}
} else if (localAttributeType == BLETypeDescriptor) {
BLEDescriptor* descriptor = (BLEDescriptor *)localAttribute;
setupMsgData->length = 12;
setupMsgData->data[0] = 0x04;
setupMsgData->data[1] = 0x04;
unsigned char valueLength = descriptor->valueLength();
setupMsgData->data[2] = valueLength;
setupMsgData->data[3] = valueLength;
setupMsgData->data[4] = (handle >> 8) & 0xff;
setupMsgData->data[5] = handle & 0xff;
handle++;
setupMsgData->data[6] = uuidData[1];
setupMsgData->data[7] = uuidData[0];
setupMsgData->data[8] = ACI_STORE_LOCAL;
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
int valueOffset = 0;
while(valueOffset < valueLength) {
int chunkSize = min(valueLength - valueOffset, MAX_ATTRIBUTE_VALUE_PER_SETUP_MSG);
setupMsgData->length = 3 + chunkSize;
for (int j = 0; j < chunkSize; j++) {
setupMsgData->data[j] = (*descriptor)[j + valueOffset];
}
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
valueOffset += chunkSize;
}
}
}
this->_numLocalPipeInfo = numLocalPiped;
// terminator
setupMsgData->length = 4;
setupMsgData->data[0] = 0x00;
this->sendSetupMessage(&setupMsg, 0x2, gattSetupMsgOffset);
unsigned short remoteServiceSetupMsgOffset = 0;
unsigned char customUuidIndex = 2;
for (int i = 0; i < numRemoteAttributes; i++) {
BLERemoteAttribute* remoteAttribute = remoteAttributes[i];
BLEUuid uuid = BLEUuid(remoteAttribute->uuid());
const unsigned char* uuidData = uuid.data();
unsigned char uuidLength = uuid.length();
if (remoteAttribute->type() == BLETypeService) {
BLERemoteService *remoteService = (BLERemoteService*)remoteAttributes[i];
unsigned char numRemoteCharacteristics = 0;
setupMsgData->data[3] = (pipe - 1);
for (int j = (i + 1); j < numRemoteAttributes; j++) {
if (remoteAttributes[j]->type() == BLETypeCharacteristic) {
BLERemoteCharacteristic* remoteCharateristic = (BLERemoteCharacteristic*)remoteAttributes[j];
struct remotePipeInfo* remotePipeInfo = &this->_remotePipeInfo[numRemotePiped];
memset(remotePipeInfo, 0, sizeof(struct remotePipeInfo));
unsigned char properties = remoteCharateristic->properties();
remotePipeInfo->characteristic = remoteCharateristic;
if (properties & BLEWriteWithoutResponse) {
remotePipeInfo->txPipe = pipe;
pipe++;
}
if (properties & BLEWrite) {
remotePipeInfo->txAckPipe = pipe;
pipe++;
}
if (properties & BLERead) {
remotePipeInfo->rxReqPipe = pipe;
pipe++;
}
if (properties & BLENotify) {
remotePipeInfo->rxPipe = pipe;
pipe++;
}
if (properties & BLEIndicate) {
remotePipeInfo->rxAckPipe = pipe;
pipe++;
}
numRemoteCharacteristics++;
numRemotePiped++;
} else {
break;
}
}
setupMsgData->length = 8;
if (uuidLength == 2) {
setupMsgData->data[0] = uuidData[1];
setupMsgData->data[1] = uuidData[0];
setupMsgData->data[2] = 0x01;
} else {
setupMsgData->data[0] = uuidData[13];
setupMsgData->data[1] = uuidData[12];
setupMsgData->data[2] = customUuidIndex;
customUuidIndex++;
}
setupMsgData->data[4] = numRemoteCharacteristics;
this->sendSetupMessage(&setupMsg, 0x3, remoteServiceSetupMsgOffset);
}
}
this->_numRemotePipeInfo = numRemotePiped;
// pipes
unsigned short pipeSetupMsgOffset = 0;
for (int i = 0; i < numLocalPiped; i++) {
struct localPipeInfo localPipeInfo = this->_localPipeInfo[i];
setupMsgData->length = 13;
setupMsgData->data[0] = 0x00;
setupMsgData->data[1] = 0x00;
setupMsgData->data[2] = 0x01;
setupMsgData->data[3] = 0x00;
setupMsgData->data[4] = 0x00;
setupMsgData->data[5] = 0x04;
setupMsgData->data[6] = (localPipeInfo.valueHandle >> 8) & 0xff;
setupMsgData->data[7] = localPipeInfo.valueHandle & 0xff;
setupMsgData->data[8] = (localPipeInfo.configHandle >> 8) & 0xff;
setupMsgData->data[9] = localPipeInfo.configHandle & 0xff;
unsigned char properties = localPipeInfo.characteristic->properties();
if (properties & BLEBroadcast) {
setupMsgData->data[4] |= 0x01; // Adv
}
if (properties & BLENotify) {
setupMsgData->data[4] |= 0x02; // TX
}
if (properties & BLEIndicate) {
setupMsgData->data[4] |= 0x04; // TX Ack
}
if (properties & BLEWriteWithoutResponse) {
setupMsgData->data[4] |= 0x08; // RX Ack
}
if (properties & BLEWrite) {
setupMsgData->data[4] |= 0x10; // RX Ack
}
if (properties & BLERead) {
setupMsgData->data[4] |= 0x80; // Set
}
this->sendSetupMessage(&setupMsg, 0x4, pipeSetupMsgOffset);
}
for (int i = 0; i < numRemotePiped; i++) {
struct remotePipeInfo remotePipeInfo = this->_remotePipeInfo[i];
BLERemoteCharacteristic *remoteCharacteristic = remotePipeInfo.characteristic;
BLEUuid uuid = BLEUuid(remoteCharacteristic->uuid());
const unsigned char* uuidData = uuid.data();
unsigned char uuidLength = uuid.length();
setupMsgData->length = 13;
if (uuidLength== 2) {
setupMsgData->data[0] = uuidData[1];
setupMsgData->data[1] = uuidData[0];
setupMsgData->data[2] = 0x01;
} else {
setupMsgData->data[0] = uuidData[13];
setupMsgData->data[1] = uuidData[12];
setupMsgData->data[2] = customUuidIndex;
customUuidIndex++;
}
setupMsgData->data[3] = 0x00; // pipe properties
setupMsgData->data[4] = 0x00; // pipe properties
setupMsgData->data[5] = 0x04;
setupMsgData->data[6] = 0x00;
setupMsgData->data[7] = 0x00;
setupMsgData->data[8] = 0x00;
setupMsgData->data[9] = 0x00;
unsigned char properties = remoteCharacteristic->properties();
if (properties & BLERead) {
setupMsgData->data[4] |= 0x40; // ACI_RX_REQ
}
if (properties & BLEWriteWithoutResponse) {
setupMsgData->data[4] |= 0x02; // ACI_TX
}
if (properties & BLEWrite) {
setupMsgData->data[4] |= 0x04; // ACI_TX_ACK
}
if (properties & BLENotify) {
setupMsgData->data[4] |= 0x08; // ACI_RX
}
if (properties & BLEIndicate) {
setupMsgData->data[4] |= 0x10; // ACI_RX_ACK
}
this->sendSetupMessage(&setupMsg, 0x4, pipeSetupMsgOffset);
}
// custom uuid's (remote only for now)
unsigned short customUuidSetupMsgOffset = 0;
for (int i = 0; i < numRemoteAttributes; i++) {
BLERemoteAttribute* remoteAttribute = remoteAttributes[i];
BLEUuid uuid = BLEUuid(remoteAttribute->uuid());
const unsigned char* uuidData = uuid.data();
unsigned char uuidLength = uuid.length();
setupMsgData->length = 3 + uuidLength;
memcpy(&setupMsgData->data, uuidData, uuidLength);
setupMsgData->data[12] = 0;
setupMsgData->data[13] = 0;
this->sendSetupMessage(&setupMsg, 0x5, customUuidSetupMsgOffset);
}
// crc
unsigned short crcOffset = 0;
setupMsgData->length = 6;
setupMsgData->data[0] = 3;
this->sendSetupMessage(&setupMsg, 0xf, crcOffset, true);
// Set BLE RF output power
// Patch added by Arne Brune Olsen (https://github.com/linuxdevel/arduino-BLEPeripheral)
// 2015
{
aci_device_output_power_t device_tx_power;
switch (this->_tx_power_level) {
case BLE_OUTPUT_POWER_MINUS_18DBM:
device_tx_power = ACI_DEVICE_OUTPUT_POWER_MINUS_18DBM;
break;
case BLE_OUTPUT_POWER_MINUS_12DBM:
device_tx_power = ACI_DEVICE_OUTPUT_POWER_MINUS_12DBM;
break;
case BLE_OUTPUT_POWER_MINUS_6DBM:
device_tx_power = ACI_DEVICE_OUTPUT_POWER_MINUS_6DBM;
break;
case BLE_OUTPUT_POWER_0DBM:
default:
device_tx_power = ACI_DEVICE_OUTPUT_POWER_0DBM;
break;
}
if (!lib_aci_set_tx_power(device_tx_power)) {
#ifdef NRF_8001_DEBUG
Serial.println("FAILED lib_aci_set_tx_power");
#endif
} else {
#ifdef NRF_8001_DEBUG
Serial.println("lib_aci_set_tx_power SUCCESS");
#endif
}
}
}
void nRF8001::poll() {
// We enter the if statement only when there is a ACI event available to be processed
if (lib_aci_event_get(&this->_aciState, &this->_aciData)) {
aci_evt_t* aciEvt = &this->_aciData.evt;
switch(aciEvt->evt_opcode) {
/**
As soon as you reset the nRF8001 you will get an ACI Device Started Event
*/
case ACI_EVT_DEVICE_STARTED: {
this->_aciState.data_credit_total = aciEvt->params.device_started.credit_available;
switch(aciEvt->params.device_started.device_mode) {
case ACI_DEVICE_SETUP:
/**
When the device is in the setup mode
*/
#ifdef NRF_8001_DEBUG
Serial.println(F("Evt Device Started: Setup"));
#endif
break;
case ACI_DEVICE_STANDBY:
#ifdef NRF_8001_DEBUG
Serial.println(F("Evt Device Started: Standby"));
#endif
//Looking for an iPhone by sending radio advertisements
//When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001
if (aciEvt->params.device_started.hw_error) {
delay(20); //Handle the HW error event correctly.
} else if (this->_bondStore && this->_bondStore->hasData()) {
this->_dynamicDataSequenceNo = 1;
this->_dynamicDataOffset = 0;
unsigned char chunkSize;
this->_bondStore->getData(&chunkSize, this->_dynamicDataOffset, sizeof(chunkSize));
this->_dynamicDataOffset++;
unsigned char chunkData[DYNAMIC_DATA_MAX_CHUNK_SIZE];
this->_bondStore->getData(chunkData, this->_dynamicDataOffset, chunkSize);
this->_dynamicDataOffset += chunkSize;
lib_aci_write_dynamic_data(this->_dynamicDataSequenceNo, chunkData, chunkSize);
} else {
this->startAdvertising();
}
break;
}
}
break; //ACI Device Started Event
case ACI_EVT_CMD_RSP:
//If an ACI command response event comes with an error -> stop
if (ACI_STATUS_SUCCESS != aciEvt->params.cmd_rsp.cmd_status &&
ACI_STATUS_TRANSACTION_CONTINUE != aciEvt->params.cmd_rsp.cmd_status &&
ACI_STATUS_TRANSACTION_COMPLETE != aciEvt->params.cmd_rsp.cmd_status) {
//ACI ReadDynamicData and ACI WriteDynamicData will have status codes of
//TRANSACTION_CONTINUE and TRANSACTION_COMPLETE
//all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command
#ifdef NRF_8001_DEBUG
Serial.print(F("ACI Command "));
Serial.println(aciEvt->params.cmd_rsp.cmd_opcode, HEX);
Serial.print(F("Evt Cmd respone: Status "));
Serial.println(aciEvt->params.cmd_rsp.cmd_status, HEX);
#endif
} else {
switch (aciEvt->params.cmd_rsp.cmd_opcode) {
case ACI_CMD_READ_DYNAMIC_DATA: {
#ifdef NRF_8001_DEBUG
Serial.print(F("Dynamic data read sequence "));
Serial.print(aciEvt->params.cmd_rsp.params.read_dynamic_data.seq_no);
Serial.print(F(": "));
Serial.print(aciEvt->len - 4);
Serial.print(F(": "));
BLEUtil::printBuffer(aciEvt->params.cmd_rsp.params.read_dynamic_data.dynamic_data, aciEvt->len - 4);
#endif
if (aciEvt->params.cmd_rsp.params.read_dynamic_data.seq_no == 1) {
this->_dynamicDataOffset = 0;
}
unsigned char chunkLength = aciEvt->len - 4;
this->_bondStore->putData(&chunkLength, this->_dynamicDataOffset, sizeof(chunkLength));
this->_dynamicDataOffset++;
this->_bondStore->putData(aciEvt->params.cmd_rsp.params.read_dynamic_data.dynamic_data, this->_dynamicDataOffset, chunkLength);
this->_dynamicDataOffset += chunkLength;
if (aciEvt->params.cmd_rsp.cmd_status == ACI_STATUS_TRANSACTION_CONTINUE) {
lib_aci_read_dynamic_data();
} else if (aciEvt->params.cmd_rsp.cmd_status == ACI_STATUS_TRANSACTION_COMPLETE) {
this->startAdvertising();
}
break;
}
case ACI_CMD_WRITE_DYNAMIC_DATA: {
#ifdef NRF_8001_DEBUG
Serial.print(F("Dynamic data write sequence "));
Serial.print(this->_dynamicDataSequenceNo);
Serial.println(F(" complete"));
#endif
if (aciEvt->params.cmd_rsp.cmd_status == ACI_STATUS_TRANSACTION_CONTINUE) {
this->_dynamicDataSequenceNo++;
unsigned char chunkSize;
this->_bondStore->getData(&chunkSize, this->_dynamicDataOffset, sizeof(chunkSize));
this->_dynamicDataOffset++;
unsigned char chunkData[DYNAMIC_DATA_MAX_CHUNK_SIZE];
this->_bondStore->getData(chunkData, this->_dynamicDataOffset, chunkSize);
this->_dynamicDataOffset += chunkSize;
lib_aci_write_dynamic_data(this->_dynamicDataSequenceNo, chunkData, chunkSize);
} else if (aciEvt->params.cmd_rsp.cmd_status == ACI_STATUS_TRANSACTION_COMPLETE) {
delay(20);
this->startAdvertising();
}
break;
}
case ACI_CMD_GET_DEVICE_VERSION:
break;
case ACI_CMD_GET_DEVICE_ADDRESS: {
#ifdef NRF_8001_DEBUG
char address[18];
BLEUtil::addressToString(aciEvt->params.cmd_rsp.params.get_device_address.bd_addr_own, address);
Serial.print(F("Device address = "));
Serial.println(address);
Serial.print(F("Device address type = "));
Serial.println(aciEvt->params.cmd_rsp.params.get_device_address.bd_addr_type, DEC);
#endif
if (this->_eventListener) {
this->_eventListener->BLEDeviceAddressReceived(*this, aciEvt->params.cmd_rsp.params.get_device_address.bd_addr_own);
}
break;
}
case ACI_CMD_GET_BATTERY_LEVEL: {
float batteryLevel = aciEvt->params.cmd_rsp.params.get_battery_level.battery_level * 0.00352;
#ifdef NRF_8001_DEBUG
Serial.print(F("Battery level = "));
Serial.println(batteryLevel);
#endif
if (this->_eventListener) {
this->_eventListener->BLEDeviceBatteryLevelReceived(*this, batteryLevel);
}
break;