-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPBLEAccessoryServer+Advertising.c
1144 lines (1050 loc) · 50.2 KB
/
HAPBLEAccessoryServer+Advertising.c
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
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.
#include "HAP+Internal.h"
static const HAPLogObject logObject = { .subsystem = kHAP_LogSubsystem, .category = "BLEAccessoryServer" };
HAP_RESULT_USE_CHECK
HAPError HAPBLEAccessoryServerGetGSN(HAPPlatformKeyValueStoreRef keyValueStore, HAPBLEAccessoryServerGSN* gsn) {
HAPPrecondition(keyValueStore);
HAPPrecondition(gsn);
HAPError err;
bool found;
size_t numBytes;
uint8_t gsnBytes[sizeof(uint16_t) + sizeof(uint8_t)];
err = HAPPlatformKeyValueStoreGet(
keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_BLEGSN,
gsnBytes,
sizeof gsnBytes,
&numBytes,
&found);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!found) {
HAPWriteLittleUInt16(&gsnBytes[0], 1U);
gsnBytes[2] = 0x00;
} else if (numBytes != sizeof gsnBytes) {
HAPLog(&logObject, "Invalid GSN length %lu.", (unsigned long) numBytes);
return kHAPError_Unknown;
}
HAPRawBufferZero(gsn, sizeof *gsn);
gsn->gsn = HAPReadLittleUInt16(&gsnBytes[0]);
gsn->didIncrement = (gsnBytes[2] & 0x01u) == 0x01;
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLEAccessoryServerGetAdvertisingParameters(
HAPAccessoryServerRef* server_,
bool* isActive,
uint16_t* advertisingInterval,
void* advertisingBytes,
size_t maxAdvertisingBytes,
size_t* numAdvertisingBytes,
void* scanResponseBytes,
size_t maxScanResponseBytes,
size_t* numScanResponseBytes)
HAP_DIAGNOSE_ERROR(maxAdvertisingBytes < 31, "maxAdvertisingBytes must be at least 31")
HAP_DIAGNOSE_WARNING(maxScanResponseBytes < 2, "maxScanResponseBytes should be at least 2") {
HAPPrecondition(server_);
const HAPAccessoryServer* server = (const HAPAccessoryServer*) server_;
HAPPrecondition(isActive);
HAPPrecondition(advertisingInterval);
HAPPrecondition(advertisingBytes);
HAPPrecondition(maxAdvertisingBytes >= 31);
HAPPrecondition(numAdvertisingBytes);
HAPPrecondition(scanResponseBytes);
HAPPrecondition(numScanResponseBytes);
HAPError err;
*isActive = true;
*advertisingInterval = 0;
*numAdvertisingBytes = 0;
*numScanResponseBytes = 0;
// The accessory shall not advertise while it is connected to a HomeKit controller.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.1.4 Advertising Interval
if (server->ble.adv.connected) {
*isActive = false;
return kHAPError_None;
}
if (server->ble.adv.broadcastedEvent.iid) {
// HAP BLE Encrypted Notification Advertisement Format.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2 HAP BLE Encrypted Notification Advertisement Format
uint16_t keyExpirationGSN;
HAPBLEAccessoryServerBroadcastEncryptionKey broadcastKey;
HAPDeviceID advertisingID;
err = HAPBLEAccessoryServerBroadcastGetParameters(
server->platform.keyValueStore, &keyExpirationGSN, &broadcastKey, &advertisingID);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!keyExpirationGSN) {
HAPLog(&logObject, "Started broadcasted event without valid key. Corrupted data?");
return kHAPError_Unknown;
}
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Interval.
*advertisingInterval = 0;
switch (server->ble.adv.broadcastedEvent.interval) {
case kHAPBLECharacteristicBroadcastInterval_20Ms: {
*advertisingInterval = HAPBLEAdvertisingIntervalCreateFromMilliseconds(20);
} break;
case kHAPBLECharacteristicBroadcastInterval_1280Ms: {
*advertisingInterval = HAPBLEAdvertisingIntervalCreateFromMilliseconds(1280);
} break;
case kHAPBLECharacteristicBroadcastInterval_2560Ms: {
*advertisingInterval = HAPBLEAdvertisingIntervalCreateFromMilliseconds(2560);
} break;
}
HAPAssert(*advertisingInterval);
uint8_t* adv = advertisingBytes;
// Flags.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.1 Flags
/* 0x00 LEN */ *adv++ = 0x02;
/* 0x01 ADT */ *adv++ = 0x01;
/* 0x02 Flags */ *adv++ = 0U << 0U | // NO: LE Limited Discoverable Mode.
1U << 1U | // YES: LE General Discoverable Mode.
1U << 2U | // YES: BR/EDR Not Supported.
0U << 3U | // NO: Simultaneous LE and BR/EDR to Same Device Capable (Controller).
0U << 4U | // NO: Simultaneous LE and BR/EDR to Same Device Capable (Host).
0U << 5U | 0U << 6U | 0U << 7U; // Reserved.
// Manufacturer data.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.2 Manufacturer Data
/* 0x00 LEN */ *adv++ = 0x1B;
/* 0x01 ADT */ *adv++ = 0xFF;
/* 0x02 CoID */ HAPWriteLittleUInt16(adv, 0x004CU);
adv += 2;
/* 0x04 TY */ *adv++ = 0x11;
/* 0x05 STL */ *adv++ = 0x36;
/* 0x06 AdvID */ {
HAPAssert(sizeof advertisingID.bytes == 6);
HAPRawBufferCopyBytes(adv, advertisingID.bytes, sizeof advertisingID.bytes);
adv += sizeof advertisingID.bytes;
}
uint8_t* encryptedBytes = adv;
/* 0x0C GSN */ HAPWriteLittleUInt16(adv, gsn.gsn);
adv += 2;
/* 0x0E IID */ HAPWriteLittleUInt16(adv, server->ble.adv.broadcastedEvent.iid);
adv += 2;
/* 0x10 Value */ {
HAPRawBufferCopyBytes(
adv, server->ble.adv.broadcastedEvent.value, sizeof server->ble.adv.broadcastedEvent.value);
adv += sizeof server->ble.adv.broadcastedEvent.value;
}
/* 0x18 Tag */ {
// See HomeKit Accessory Protocol Specification R14
// Section 5.9 AEAD Algorithm.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.7.3 Broadcast Encryption Key Generation
uint8_t tagBytes[CHACHA20_POLY1305_TAG_BYTES];
uint8_t nonceBytes[] = { HAPExpandLittleUInt64((uint64_t) gsn.gsn) };
HAP_chacha20_poly1305_encrypt_aad(
tagBytes,
encryptedBytes,
encryptedBytes,
(size_t)(adv - encryptedBytes),
advertisingID.bytes,
sizeof advertisingID.bytes,
nonceBytes,
sizeof nonceBytes,
broadcastKey.value);
HAPRawBufferCopyBytes(adv, tagBytes, 4);
adv += 4;
}
*numAdvertisingBytes = (size_t)(adv - (uint8_t*) advertisingBytes);
HAPAssert(*numAdvertisingBytes <= maxAdvertisingBytes);
// Log.
adv = advertisingBytes;
adv += 3;
HAPLogInfo(
&logObject,
"HAP BLE Encrypted Notification Advertisement Format (Manufacturer Data).\n"
"- LEN = 0x%02X\n"
"- ADT = 0x%02X\n"
"- CoID = 0x%04X\n"
"- TY = 0x%02X\n"
"- STL = 0x%02X\n"
"- AdvID = %02X:%02X:%02X:%02X:%02X:%02X\n"
"- Ev = 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n"
" - GSN = %u\n"
" - IID = 0x%04X\n"
" - Value = 0x%02X%02X%02X%02X%02X%02X%02X%02X\n"
"- Tag = 0x%02X%02X%02X%02X",
adv[0x00],
adv[0x01],
HAPReadLittleUInt16(&adv[0x02]),
adv[0x04],
adv[0x05],
adv[0x06],
adv[0x07],
adv[0x08],
adv[0x09],
adv[0x0A],
adv[0x0B],
adv[0x0C],
adv[0x0D],
adv[0x0E],
adv[0x0F],
adv[0x10],
adv[0x11],
adv[0x12],
adv[0x13],
adv[0x14],
adv[0x15],
adv[0x16],
adv[0x17],
gsn.gsn,
server->ble.adv.broadcastedEvent.iid,
server->ble.adv.broadcastedEvent.value[0],
server->ble.adv.broadcastedEvent.value[1],
server->ble.adv.broadcastedEvent.value[2],
server->ble.adv.broadcastedEvent.value[3],
server->ble.adv.broadcastedEvent.value[4],
server->ble.adv.broadcastedEvent.value[5],
server->ble.adv.broadcastedEvent.value[6],
server->ble.adv.broadcastedEvent.value[7],
adv[0x18],
adv[0x19],
adv[0x1A],
adv[0x1B]);
} else {
// HAP BLE Regular Advertisement Format.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.1 HAP BLE Regular Advertisement Format
// Interval.
// - 20 ms for first 30 seconds after boot.
// See Accessory Design Guidelines for Apple Devices R7
// Section 11.5 Advertising Interval
// - 20 ms for first 3 seconds after Disconnected Event.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.3 Disconnected Events
// - Regular advertising interval, otherwise.
*advertisingInterval = (server->ble.adv.timer || !server->ble.adv.fast_started || server->ble.adv.fast_timer) ?
HAPBLEAdvertisingIntervalCreateFromMilliseconds(20) :
server->ble.adv.interval;
uint8_t* adv = advertisingBytes;
// Get setup ID.
HAPSetupID setupID;
bool hasSetupID = false;
HAPPlatformAccessorySetupLoadSetupID(server->platform.accessorySetup, &hasSetupID, &setupID);
// Flags.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.1.1 Flags
/* 0x00 LEN */ *adv++ = 0x02;
/* 0x01 ADT */ *adv++ = 0x01;
/* 0x02 Flags */ *adv++ = 0U << 0U | // NO: LE Limited Discoverable Mode.
1U << 1U | // YES: LE General Discoverable Mode.
1U << 2U | // YES: BR/EDR Not Supported.
0U << 3U | // NO: Simultaneous LE and BR/EDR to Same Device Capable (Controller).
0U << 4U | // NO: Simultaneous LE and BR/EDR to Same Device Capable (Host).
0U << 5U | 0U << 6U | 0U << 7U; // Reserved.
// Manufacturer data.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.1.2 Manufacturer Data
/* 0x00 LEN */ *adv++ = (uint8_t)(0x12 + (hasSetupID ? 4 : 0));
/* 0x01 ADT */ *adv++ = 0xFF;
/* 0x02 CoID */ HAPWriteLittleUInt16(adv, 0x004CU);
adv += 2;
/* 0x04 TY */ *adv++ = 0x06;
/* 0x05 STL */ *adv++ = (uint8_t)(0x2D + (hasSetupID ? 4 : 0));
/* 0x06 SF */ *adv++ = (uint8_t)(HAPAccessoryServerIsPaired(server_) ? 0U << 0U : 1U << 0U);
/* 0x07 DevID */ {
HAPDeviceID deviceID;
err = HAPDeviceIDGet(server->platform.keyValueStore, &deviceID);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPAssert(sizeof deviceID.bytes == 6);
HAPRawBufferCopyBytes(adv, deviceID.bytes, sizeof deviceID.bytes);
adv += sizeof deviceID.bytes;
}
/* 0x0D ACID */ HAPWriteLittleUInt16(adv, (uint16_t) server->primaryAccessory->category);
adv += 2;
/* 0x0F GSN */ {
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPWriteLittleUInt16(adv, gsn.gsn);
adv += 2;
}
/* 0x11 CN */ {
uint16_t cn;
err = HAPAccessoryServerGetCN(server->platform.keyValueStore, &cn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
*adv++ = (uint8_t)((cn - 1) % UINT8_MAX + 1);
}
/* 0x12 CV */ *adv++ = 0x02;
/* 0x13 SH */ {
if (hasSetupID) {
// Get Device ID string.
HAPDeviceIDString deviceIDString;
err = HAPDeviceIDGetAsString(server->platform.keyValueStore, &deviceIDString);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Get setup hash.
HAPAccessorySetupSetupHash setupHash;
HAPAccessorySetupGetSetupHash(&setupHash, &setupID, &deviceIDString);
// Append.
HAPAssert(sizeof setupHash.bytes == 4);
HAPRawBufferCopyBytes(adv, setupHash.bytes, sizeof setupHash.bytes);
adv += sizeof setupHash.bytes;
}
}
// Name.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.1.3 Local Name
size_t numNameBytes = HAPStringGetNumBytes(server->primaryAccessory->name);
HAPAssert(numNameBytes < UINT8_MAX);
size_t maxNameBytesInAdvertisingData = maxAdvertisingBytes - (size_t)(adv - (uint8_t*) advertisingBytes) - 2;
if (numNameBytes > maxNameBytesInAdvertisingData) {
// When the advertisement includes the shortened local name
// the accessory should include the complete local name in the Scan Response.
if (maxScanResponseBytes >= 2) {
uint8_t* sr = scanResponseBytes;
size_t maxNameBytesInScanResponseData = maxScanResponseBytes - 2;
if (numNameBytes > maxNameBytesInScanResponseData) {
numNameBytes = maxNameBytesInScanResponseData;
/* 0x00 LEN */ *sr++ = (uint8_t)(numNameBytes + 1);
/* 0x01 ADT */ *sr++ = 0x08;
} else {
/* 0x00 LEN */ *sr++ = (uint8_t)(numNameBytes + 1);
/* 0x01 ADT */ *sr++ = 0x09;
}
/* 0x02 Name */ {
HAPRawBufferCopyBytes(sr, server->primaryAccessory->name, numNameBytes);
sr += numNameBytes;
}
*numScanResponseBytes = (size_t)(sr - (uint8_t*) scanResponseBytes);
HAPAssert(*numScanResponseBytes <= maxScanResponseBytes);
}
numNameBytes = maxNameBytesInAdvertisingData;
/* 0x00 LEN */ *adv++ = (uint8_t)(numNameBytes + 1);
/* 0x01 ADT */ *adv++ = 0x08;
} else {
/* 0x00 LEN */ *adv++ = (uint8_t)(numNameBytes + 1);
/* 0x01 ADT */ *adv++ = 0x09;
}
/* 0x02 Name */ {
HAPRawBufferCopyBytes(adv, server->primaryAccessory->name, numNameBytes);
adv += numNameBytes;
}
*numAdvertisingBytes = (size_t)(adv - (uint8_t*) advertisingBytes);
HAPAssert(*numAdvertisingBytes <= maxAdvertisingBytes);
// Log.
adv = advertisingBytes;
adv += 3;
char setupHashLog[12 + 4 * 2 + 1 + 1];
if (hasSetupID) {
err = HAPStringWithFormat(
setupHashLog,
sizeof setupHashLog,
"\n- SH = 0x%02X%02X%02X%02X",
adv[0x13],
adv[0x14],
adv[0x15],
adv[0x16]);
HAPAssert(!err);
}
HAPLogInfo(
&logObject,
"HAP BLE Regular Advertisement Format (Manufacturer Data).\n"
"- LEN = 0x%02X\n"
"- ADT = 0x%02X\n"
"- CoID = 0x%04X\n"
"- TY = 0x%02X\n"
"- STL = 0x%02X\n"
"- SF = 0x%02X\n"
"- DevID = %02X:%02X:%02X:%02X:%02X:%02X\n"
"- ACID = %u\n"
"- GSN = %u\n"
"- CN = %u\n"
"- CV = 0x%02X"
"%s",
adv[0x00],
adv[0x01],
HAPReadLittleUInt16(&adv[0x02]),
adv[0x04],
adv[0x05],
adv[0x06],
adv[0x07],
adv[0x08],
adv[0x09],
adv[0x0A],
adv[0x0B],
adv[0x0C],
HAPReadLittleUInt16(&adv[0x0D]),
HAPReadLittleUInt16(&adv[0x0F]),
adv[0x11],
adv[0x12],
hasSetupID ? setupHashLog : "");
}
float advertisingIntervalMilliseconds = HAPBLEAdvertisingIntervalGetMilliseconds(*advertisingInterval);
HAPLogBufferInfo(
&logObject,
advertisingBytes,
*numAdvertisingBytes,
"ADV data: Active = %d, Interval = %u.%03u ms.",
*isActive,
(uint16_t) advertisingIntervalMilliseconds,
(uint16_t)((uint32_t)(advertisingIntervalMilliseconds * 1000) % 1000));
if (scanResponseBytes && *numScanResponseBytes) {
HAPLogBufferInfo(&logObject, scanResponseBytes, *numScanResponseBytes, "SR data.");
}
return kHAPError_None;
}
static void AdvertisingTimerExpired(HAPPlatformTimerRef timer, void* _Nullable context) {
HAPPrecondition(context);
HAPAccessoryServerRef* server_ = context;
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
if (timer == server->ble.adv.fast_timer) {
HAPLogDebug(&logObject, "Fast advertisement timer expired.");
server->ble.adv.fast_timer = 0;
} else if (timer == server->ble.adv.timer) {
HAPLogDebug(&logObject, "Advertisement timer expired.");
server->ble.adv.timer = 0;
} else {
HAPPreconditionFailure();
}
HAPError err;
HAPAssert(!server->ble.adv.connected);
// If no controller connects to the accessory within the 3 second broadcast period then the accessory must fall
// back to the Disconnected Events advertisement rule with its current GSN as specified in `Disconnected Events`.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.2 Broadcasted Events
if (server->ble.adv.broadcastedEvent.iid && !server->ble.adv.timer) {
HAPRawBufferZero(&server->ble.adv.broadcastedEvent, sizeof server->ble.adv.broadcastedEvent);
// After updating the GSN as specified in Section `HAP BLE Regular Advertisement Format` in the disconnected
// state the accessory must use a 20 ms advertising interval for at least 3 seconds.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.3 Disconnected Events
err = HAPPlatformTimerRegister(
&server->ble.adv.timer,
HAPPlatformClockGetCurrent() + server->ble.adv.ev_duration,
AdvertisingTimerExpired,
server_);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLog(&logObject, "Not enough resources to start disconnected event timer!");
}
}
// Update advertisement parameters.
HAPAccessoryServerUpdateAdvertisingData(server_);
}
void HAPBLEAccessoryServerDidStartAdvertising(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPError err;
// For first 30 seconds after boot, use 20ms as regular advertisement interval.
if (!server->ble.adv.fast_started) {
HAPAssert(!server->ble.adv.fast_timer);
server->ble.adv.fast_started = true;
err = HAPPlatformTimerRegister(
&server->ble.adv.fast_timer,
HAPPlatformClockGetCurrent() + 30 * HAPSecond,
AdvertisingTimerExpired,
server_);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLog(&logObject,
"Not enough resources to start fast initial advertisement timer. Using regular interval!");
}
}
}
HAP_RESULT_USE_CHECK
HAPError HAPBLEAccessoryServerDidConnect(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(!server->ble.adv.connected);
HAPError err;
server->ble.adv.connected = true;
// Stop fast advertisement timer.
if (server->ble.adv.fast_timer) {
HAPPlatformTimerDeregister(server->ble.adv.fast_timer);
server->ble.adv.fast_timer = 0;
}
// Stop timer for disconnected and broadcasted events.
// If a controller connects to the accessory before the completion of the 3 second advertising period the accessory
// should abort the encrypted advertisement and continue with its regular advertisement at the regular advertising
// period after the controller disconnects.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.2 Broadcasted Events
if (server->ble.adv.timer) {
HAPPlatformTimerDeregister(server->ble.adv.timer);
server->ble.adv.timer = 0;
}
// Reset disconnected events coalescing.
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
gsn.didIncrement = false;
uint8_t gsnBytes[] = { HAPExpandLittleUInt16(gsn.gsn), gsn.didIncrement ? (uint8_t) 0x01 : (uint8_t) 0x00 };
err = HAPPlatformKeyValueStoreSet(
server->platform.keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_BLEGSN,
gsnBytes,
sizeof gsnBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Reset broadcasted events.
HAPRawBufferZero(&server->ble.adv.broadcastedEvent, sizeof server->ble.adv.broadcastedEvent);
// Update advertisement parameters.
HAPAccessoryServerUpdateAdvertisingData(server_);
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLEAccessoryServerDidDisconnect(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(server->ble.adv.connected);
HAPError err;
server->ble.adv.connected = false;
HAPAssert(server->ble.adv.fast_started);
HAPAssert(!server->ble.adv.fast_timer);
HAPAssert(!server->ble.adv.timer);
// Allow quick reconnection.
err = HAPPlatformTimerRegister(
&server->ble.adv.fast_timer,
HAPPlatformClockGetCurrent() + server->ble.adv.ev_duration,
AdvertisingTimerExpired,
server_);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLog(&logObject, "Not enough resources to start quick reconnection timer. Using regular interval!");
}
// Reset GSN update coalescing.
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
gsn.didIncrement = false;
uint8_t gsnBytes[] = { HAPExpandLittleUInt16(gsn.gsn), gsn.didIncrement ? (uint8_t) 0x01 : (uint8_t) 0x00 };
err = HAPPlatformKeyValueStoreSet(
server->platform.keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_BLEGSN,
gsnBytes,
sizeof gsnBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPAssert(!server->ble.adv.broadcastedEvent.iid);
// Update advertisement parameters.
HAPAccessoryServerUpdateAdvertisingData(server_);
// Proceed with shutdown, if requested.
if (server->state != kHAPAccessoryServerState_Running) {
HAPLogInfo(&logObject, "BLE connection disconnected. Proceeding with shutdown.");
HAPAccessoryServerStop(server_);
}
return kHAPError_None;
}
/**
* Increments GSN, invalidating broadcast encryption key if necessary.
*
* @param server_ Accessory server.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If an I/O error occurred.
*/
HAP_RESULT_USE_CHECK
static HAPError HAPBLEAccessoryServerIncrementGSN(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPError err;
// Get key expiration GSN.
uint16_t keyExpirationGSN;
err = HAPBLEAccessoryServerBroadcastGetParameters(server->platform.keyValueStore, &keyExpirationGSN, NULL, NULL);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Get GSN.
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Expire broadcast encryption key if necessary.
if (gsn.gsn == keyExpirationGSN) {
err = HAPBLEAccessoryServerBroadcastExpireKey(server->platform.keyValueStore);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
}
// Increment GSN.
if (gsn.gsn == UINT16_MAX) {
gsn.gsn = 1;
} else {
gsn.gsn++;
}
gsn.didIncrement = true;
HAPLogInfo(&logObject, "New GSN: %u.", gsn.gsn);
// Save GSN state.
uint8_t gsnBytes[] = { HAPExpandLittleUInt16(gsn.gsn), gsn.didIncrement ? (uint8_t) 0x01 : (uint8_t) 0x00 };
err = HAPPlatformKeyValueStoreSet(
server->platform.keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_BLEGSN,
gsnBytes,
sizeof gsnBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLEAccessoryServerDidRaiseEvent(
HAPAccessoryServerRef* server_,
const HAPCharacteristic* characteristic_,
const HAPService* service,
const HAPAccessory* accessory,
HAPSessionRef* _Nullable session) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(service);
HAPPrecondition(accessory);
HAPPrecondition(accessory->aid == 1);
HAPError err;
if (characteristic->properties.supportsEventNotification) {
// Connected event.
if (server->ble.connection.connected && (!session || session == server->ble.storage->session)) {
const HAPCharacteristic* writtenCharacteristic = server->ble.connection.write.characteristic;
const HAPService* writtenService = server->ble.connection.write.service;
const HAPAccessory* writtenAccessory = server->ble.connection.write.accessory;
if (characteristic_ == writtenCharacteristic && service == writtenService &&
accessory == writtenAccessory) {
HAPLogCharacteristicInfo(
&logObject,
characteristic,
service,
accessory,
"Suppressing notification as the characteristic is currently being written.");
} else {
HAPBLEPeripheralManagerRaiseEvent(server_, characteristic_, service, accessory);
}
}
}
if (characteristic->properties.ble.supportsBroadcastNotification) {
// Broadcasted event.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.2 Broadcasted Events
// If a controller connects to the accessory before the completion of the 3 second advertising period the
// accessory should abort the encrypted advertisement and continue with its regular advertisement at the regular
// advertising period after the controller disconnects.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.2 Broadcasted Events
if (!server->ble.adv.connected) {
uint16_t keyExpirationGSN;
err = HAPBLEAccessoryServerBroadcastGetParameters(
server->platform.keyValueStore, &keyExpirationGSN, NULL, NULL);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPBLEAccessoryServerGSN gsn;
err = HAPBLEAccessoryServerGetGSN(server->platform.keyValueStore, &gsn);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
// Characteristic changes while in a broadcast encryption key expired state shall not use broadcasted events
// and must fall back to disconnected/connected events until the controller has re-generated a new broadcast
// encryption key and re-registered characteristics for broadcasted notification.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.7.4 Broadcast Encryption Key expiration and refresh
if (keyExpirationGSN && keyExpirationGSN != gsn.gsn) {
HAPBLECharacteristicBroadcastInterval interval;
bool enabled;
err = HAPBLECharacteristicGetBroadcastConfiguration(
characteristic, service, accessory, &enabled, &interval, server->platform.keyValueStore);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (enabled) {
// For additional characteristic changes before the completion of the 3 second period and before
// a controller connection, the GSN should be updated again and the accessory must reflect the
// latest changed characteristic value in its encrypted advertisement and continue to broadcast
// for an additional 3 seconds from the last change.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.6.2 Broadcasted Events
if (server->ble.adv.timer) {
HAPPlatformTimerDeregister(server->ble.adv.timer);
server->ble.adv.timer = 0;
}
// Cancel current broadcast.
server->ble.adv.broadcastedEvent.iid = 0;
HAPRawBufferZero(
server->ble.adv.broadcastedEvent.value, sizeof server->ble.adv.broadcastedEvent.value);
HAPAccessoryServerUpdateAdvertisingData(server_);
// Fetch characteristic value.
// When the characteristic value is less than 8 bytes the remaining bytes shall be set to 0.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.2 Manufacturer Data
err = kHAPError_Unknown;
uint8_t* bytes = server->ble.adv.broadcastedEvent.value;
HAPAssert(sizeof server->ble.adv.broadcastedEvent.value == 8);
switch (characteristic->format) {
case kHAPCharacteristicFormat_Data: {
// Characteristics with format of string or data/tlv8 cannot be used
// in broadcast notifications
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.2 Manufacturer Data
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"%s characteristic cannot be used in broadcast notifications.",
"Data");
} break;
case kHAPCharacteristicFormat_Bool: {
bool value;
err = HAPBoolCharacteristicHandleRead(
server_,
&(const HAPBoolCharacteristicReadRequest) { .transportType = kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
bytes[0] = (uint8_t)(value ? 1 : 0);
} break;
case kHAPCharacteristicFormat_UInt8: {
uint8_t value;
err = HAPUInt8CharacteristicHandleRead(
server_,
&(const HAPUInt8CharacteristicReadRequest) { .transportType = kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
bytes[0] = value;
} break;
case kHAPCharacteristicFormat_UInt16: {
uint16_t value;
err = HAPUInt16CharacteristicHandleRead(
server_,
&(const HAPUInt16CharacteristicReadRequest) { .transportType =
kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
HAPWriteLittleUInt16(bytes, value);
} break;
case kHAPCharacteristicFormat_UInt32: {
uint32_t value;
err = HAPUInt32CharacteristicHandleRead(
server_,
&(const HAPUInt32CharacteristicReadRequest) { .transportType =
kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
HAPWriteLittleUInt32(bytes, value);
} break;
case kHAPCharacteristicFormat_UInt64: {
uint64_t value;
err = HAPUInt64CharacteristicHandleRead(
server_,
&(const HAPUInt64CharacteristicReadRequest) { .transportType =
kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
HAPWriteLittleUInt64(bytes, value);
} break;
case kHAPCharacteristicFormat_Int: {
int32_t value;
err = HAPIntCharacteristicHandleRead(
server_,
&(const HAPIntCharacteristicReadRequest) { .transportType = kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
HAPWriteLittleInt32(bytes, value);
} break;
case kHAPCharacteristicFormat_Float: {
float value;
err = HAPFloatCharacteristicHandleRead(
server_,
&(const HAPFloatCharacteristicReadRequest) { .transportType = kHAPTransportType_BLE,
.session = NULL,
.characteristic = characteristic_,
.service = service,
.accessory = accessory },
&value,
server->context);
if (err) {
break;
}
uint32_t bitPattern = HAPFloatGetBitPattern(value);
HAPWriteLittleUInt32(bytes, bitPattern);
} break;
case kHAPCharacteristicFormat_String: {
// Characteristics with format of string or data/tlv8 cannot be used
// in broadcast notifications.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.2 Manufacturer Data
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"%s characteristic cannot be used in broadcast notifications.",
"String");
}
HAPFatalError();
case kHAPCharacteristicFormat_TLV8: {
// Characteristics with format of string or data/tlv8 cannot be used
// in broadcast notifications".
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.2.2.2 Manufacturer Data
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"%s characteristic cannot be used in broadcast notifications.",
"TLV8");
}
HAPFatalError();
}
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState ||
err == kHAPError_OutOfResources || err == kHAPError_Busy);
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Value for broadcast notification could not be received. Skipping event!");
} else {
// Increment GSN.
err = HAPBLEAccessoryServerIncrementGSN(server_);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
err = HAPPlatformTimerRegister(
&server->ble.adv.timer,
HAPPlatformClockGetCurrent() + server->ble.adv.ev_duration,
AdvertisingTimerExpired,
server_);
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Not enough resources to start broadcast event timer. Skipping event!");
} else {
// Initialize broadcasted event.
server->ble.adv.broadcastedEvent.interval = interval;
HAPAssert(characteristic->iid <= UINT16_MAX);
server->ble.adv.broadcastedEvent.iid = (uint16_t) characteristic->iid;
HAPLogCharacteristicInfo(
&logObject, characteristic, service, accessory, "Broadcasted Event.");
}
}
// Update advertisement parameters.
HAPAccessoryServerUpdateAdvertisingData(server_);