-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPBLEPDU+TLV.c
812 lines (701 loc) · 31.7 KB
/
HAPBLEPDU+TLV.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
// 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 = "BLEPDU" };
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeCharacteristicType(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_CharacteristicType,
.value = { .bytes = characteristic->characteristicType->bytes,
.numBytes = sizeof characteristic->characteristicType->bytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeServiceType(const HAPService* service, HAPTLVWriterRef* responseWriter) {
HAPPrecondition(service);
HAPPrecondition(responseWriter);
HAPError err;
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_ServiceType,
.value = { .bytes = service->serviceType->bytes,
.numBytes = sizeof service->serviceType->bytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeServiceInstanceID(const HAPService* service, HAPTLVWriterRef* responseWriter) {
HAPPrecondition(service);
HAPPrecondition(responseWriter);
HAPError err;
HAPAssert(service->iid <= UINT16_MAX);
uint8_t sidBytes[] = { HAPExpandLittleUInt16(service->iid) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_ServiceInstanceID,
.value = { .bytes = sidBytes, .numBytes = sizeof sidBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPCharacteristicPropertiesDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
uint8_t propertiesBytes[] = { HAPExpandLittleUInt16(
(characteristic->properties.ble.readableWithoutSecurity ? 0x0001 : 0) |
(characteristic->properties.ble.writableWithoutSecurity ? 0x0002 : 0) |
(characteristic->properties.supportsAuthorizationData ? 0x0004 : 0) |
(characteristic->properties.requiresTimedWrite ? 0x0008 : 0) |
(characteristic->properties.readable ? 0x0010 : 0) | (characteristic->properties.writable ? 0x0020 : 0) |
(characteristic->properties.hidden ? 0x0040 : 0) |
(characteristic->properties.supportsEventNotification ? 0x0080 : 0) |
(characteristic->properties.ble.supportsDisconnectedNotification ? 0x0100 : 0) |
(characteristic->properties.ble.supportsBroadcastNotification ? 0x0200 : 0)) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPCharacteristicPropertiesDescriptor,
.value = { .bytes = propertiesBytes, .numBytes = sizeof propertiesBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeGATTUserDescriptionDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
if (!characteristic->manufacturerDescription) {
return kHAPError_None;
}
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTUserDescriptionDescriptor,
.value = { .bytes = characteristic->manufacturerDescription,
.numBytes = HAPStringGetNumBytes(
HAPNonnull(characteristic->manufacturerDescription)) } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Converts a HAP format to the corresponding BT SIG format.
*
* @param hapFormat HAP format to convert.
*
* @return BT SIG format.
*
* @see HomeKit Accessory Protocol Specification R14
* Table 7-51 HAP Format to BT SIG Format mapping
*/
HAP_RESULT_USE_CHECK
static uint8_t ConvertHAPFormatToBTSIGFormat(HAPCharacteristicFormat hapFormat) {
switch (hapFormat) {
case kHAPCharacteristicFormat_Data:
return 0x1B;
case kHAPCharacteristicFormat_Bool:
return 0x01;
case kHAPCharacteristicFormat_UInt8:
return 0x04;
case kHAPCharacteristicFormat_UInt16:
return 0x06;
case kHAPCharacteristicFormat_UInt32:
return 0x08;
case kHAPCharacteristicFormat_UInt64:
return 0x0A;
case kHAPCharacteristicFormat_Int:
return 0x10;
case kHAPCharacteristicFormat_Float:
return 0x14;
case kHAPCharacteristicFormat_String:
return 0x19;
case kHAPCharacteristicFormat_TLV8:
return 0x1B;
}
HAPFatalError();
}
/**
* Converts a HAP unit to the corresponding BT SIG unit.
*
* @param hapUnit HAP unit to convert.
*
* @return BT SIG unit.
*
* @see HomeKit Accessory Protocol Specification R14
* Table 7-52 HAP Unit to BT SIG Unit mapping
*/
HAP_RESULT_USE_CHECK
static uint16_t ConvertHAPUnitToBTSIGUnit(HAPCharacteristicUnits hapUnit) {
switch (hapUnit) {
case kHAPCharacteristicUnits_Celsius:
return 0x272F;
case kHAPCharacteristicUnits_ArcDegrees:
return 0x2763;
case kHAPCharacteristicUnits_Percentage:
return 0x27AD;
case kHAPCharacteristicUnits_None:
return 0x2700;
case kHAPCharacteristicUnits_Lux:
return 0x2731;
case kHAPCharacteristicUnits_Seconds:
return 0x2703;
}
HAPFatalError();
}
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeGATTPresentationFormatDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
uint8_t btSIGFormat = ConvertHAPFormatToBTSIGFormat(characteristic->format);
uint16_t btSIGUnit = 0;
switch (characteristic->format) {
case kHAPCharacteristicFormat_UInt8: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPUInt8Characteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_UInt16: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPUInt16Characteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_UInt32: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPUInt32Characteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_UInt64: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPUInt64Characteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_Int: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPIntCharacteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_Float: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(((const HAPFloatCharacteristic*) characteristic)->units);
}
goto unit_converted;
case kHAPCharacteristicFormat_Bool:
case kHAPCharacteristicFormat_String:
case kHAPCharacteristicFormat_TLV8:
case kHAPCharacteristicFormat_Data: {
btSIGUnit = ConvertHAPUnitToBTSIGUnit(kHAPCharacteristicUnits_None);
}
goto unit_converted;
}
HAPFatalError();
unit_converted:;
uint8_t formatBytes[] = { /* Format (8bit): */ btSIGFormat,
/* Exponent (sint8): */ 0,
/* Unit (uint16): */ HAPExpandLittleUInt16(btSIGUnit),
/* Namespace (8bit): */ 1,
/* Description (16bit): */ HAPExpandLittleUInt16(0) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTPresentationFormatDescriptor,
.value = { .bytes = formatBytes, .numBytes = sizeof formatBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError
HAPBLEPDUTLVSerializeGATTValidRange(const HAPCharacteristic* characteristic_, HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
HAPPrecondition(responseWriter);
HAPError err;
switch (*((const HAPCharacteristicFormat*) characteristic_)) {
case kHAPCharacteristicFormat_Data: {
}
return kHAPError_None;
case kHAPCharacteristicFormat_Bool: {
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt8: {
const HAPUInt8Characteristic* characteristic = characteristic_;
uint8_t minimumValue = characteristic->constraints.minimumValue;
uint8_t maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(minimumValue <= maximumValue);
if (!minimumValue && maximumValue == UINT8_MAX) {
return kHAPError_None;
}
uint8_t rangeBytes[] = { minimumValue, maximumValue };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt16: {
const HAPUInt16Characteristic* characteristic = characteristic_;
uint16_t minimumValue = characteristic->constraints.minimumValue;
uint16_t maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(minimumValue <= maximumValue);
if (!minimumValue && maximumValue == UINT16_MAX) {
return kHAPError_None;
}
uint8_t rangeBytes[] = { HAPExpandLittleUInt16(minimumValue), HAPExpandLittleUInt16(maximumValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt32: {
const HAPUInt32Characteristic* characteristic = characteristic_;
uint32_t minimumValue = characteristic->constraints.minimumValue;
uint32_t maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(minimumValue <= maximumValue);
if (!minimumValue && maximumValue == UINT32_MAX) {
return kHAPError_None;
}
uint8_t rangeBytes[] = { HAPExpandLittleUInt32(minimumValue), HAPExpandLittleUInt32(maximumValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt64: {
const HAPUInt64Characteristic* characteristic = characteristic_;
uint64_t minimumValue = characteristic->constraints.minimumValue;
uint64_t maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(minimumValue <= maximumValue);
if (!minimumValue && maximumValue == UINT64_MAX) {
return kHAPError_None;
}
uint8_t rangeBytes[] = { HAPExpandLittleUInt64(minimumValue), HAPExpandLittleUInt64(maximumValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Int: {
const HAPIntCharacteristic* characteristic = characteristic_;
int32_t minimumValue = characteristic->constraints.minimumValue;
int32_t maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(minimumValue <= maximumValue);
if (minimumValue == INT32_MIN && maximumValue == INT32_MAX) {
return kHAPError_None;
}
uint8_t rangeBytes[] = { HAPExpandLittleInt32(minimumValue), HAPExpandLittleInt32(maximumValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Float: {
const HAPFloatCharacteristic* characteristic = characteristic_;
float minimumValue = characteristic->constraints.minimumValue;
float maximumValue = characteristic->constraints.maximumValue;
HAPPrecondition(HAPFloatIsFinite(minimumValue) || HAPFloatIsInfinite(minimumValue));
HAPPrecondition(HAPFloatIsFinite(maximumValue) || HAPFloatIsInfinite(maximumValue));
HAPPrecondition(minimumValue <= maximumValue);
if (HAPFloatIsInfinite(minimumValue) && minimumValue < 0 && HAPFloatIsInfinite(maximumValue) &&
maximumValue > 0) {
return kHAPError_None;
}
uint32_t minimumBitPattern = HAPFloatGetBitPattern(minimumValue);
uint32_t maximumBitPattern = HAPFloatGetBitPattern(maximumValue);
uint8_t rangeBytes[] = { HAPExpandLittleUInt32(minimumBitPattern),
HAPExpandLittleUInt32(maximumBitPattern) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_String: {
const HAPStringCharacteristic* characteristic = characteristic_;
uint16_t maxLength = characteristic->constraints.maxLength;
// See HomeKit Accessory Protocol Specification R14
// Table 6-3 Properties of Characteristic Objects in JSON
if (maxLength == 64) {
return kHAPError_None;
}
// See HomeKit Accessory Protocol Specification - iOS 9 Developer Preview R3
// Section 5.12.10 Minimum and Maximum Length Descriptor
uint8_t rangeBytes[] = { HAPExpandLittleUInt16(0), HAPExpandLittleUInt16(maxLength) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_GATTValidRange,
.value = { .bytes = rangeBytes, .numBytes = sizeof rangeBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_TLV8: {
}
return kHAPError_None;
}
HAPFatalError();
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPStepValueDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
HAPPrecondition(responseWriter);
HAPError err;
switch (*((const HAPCharacteristicFormat*) characteristic_)) {
case kHAPCharacteristicFormat_Data:
case kHAPCharacteristicFormat_Bool:
case kHAPCharacteristicFormat_String:
case kHAPCharacteristicFormat_TLV8: {
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt8: {
const HAPUInt8Characteristic* characteristic = characteristic_;
uint8_t stepValue = characteristic->constraints.stepValue;
if (stepValue <= 1) {
return kHAPError_None;
}
uint8_t stepBytes[] = { stepValue };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt16: {
const HAPUInt16Characteristic* characteristic = characteristic_;
uint16_t stepValue = characteristic->constraints.stepValue;
if (stepValue <= 1) {
return kHAPError_None;
}
uint8_t stepBytes[] = { HAPExpandLittleUInt16(stepValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt32: {
const HAPUInt32Characteristic* characteristic = characteristic_;
uint32_t stepValue = characteristic->constraints.stepValue;
if (stepValue <= 1) {
return kHAPError_None;
}
uint8_t stepBytes[] = { HAPExpandLittleUInt32(stepValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt64: {
const HAPUInt64Characteristic* characteristic = characteristic_;
uint64_t stepValue = characteristic->constraints.stepValue;
if (stepValue <= 1) {
return kHAPError_None;
}
uint8_t stepBytes[] = { HAPExpandLittleUInt64(stepValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Int: {
const HAPIntCharacteristic* characteristic = characteristic_;
int32_t stepValue = characteristic->constraints.stepValue;
HAPPrecondition(stepValue >= 0);
if (stepValue <= 1) {
return kHAPError_None;
}
uint8_t stepBytes[] = { HAPExpandLittleInt32(stepValue) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Float: {
const HAPFloatCharacteristic* characteristic = characteristic_;
float stepValue = characteristic->constraints.stepValue;
HAPPrecondition(HAPFloatIsFinite(stepValue));
HAPPrecondition(stepValue >= 0);
if (HAPFloatIsZero(stepValue)) {
return kHAPError_None;
}
uint32_t bitPattern = HAPFloatGetBitPattern(stepValue);
uint8_t stepBytes[] = { HAPExpandLittleUInt32(bitPattern) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPStepValueDescriptor,
.value = { .bytes = stepBytes, .numBytes = sizeof stepBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
}
return kHAPError_None;
}
HAPFatalError();
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPServiceProperties(
const HAPService* _Nullable service,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(responseWriter);
HAPError err;
// See HomeKit Accessory Protocol Specification R14
// Table 7-49 HAP Service Properties
uint16_t properties = 0;
if (service) {
properties = (uint16_t)(
(service->properties.primaryService ? 0x0001 : 0) | (service->properties.hidden ? 0x0002 : 0) |
(service->properties.ble.supportsConfiguration ? 0x0004 : 0));
}
// Accessories must include the "HAP Service Properties" characteristic only if it supports non-default properties
// or has linked services. Other services must not include this characteristic.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.4.4 HAP Service Properties
if (!properties && (!service || !service->linkedServices || !service->linkedServices[0])) {
return kHAPError_None;
}
uint8_t propertiesBytes[] = { HAPExpandLittleUInt16(properties) };
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPServiceProperties,
.value = { .bytes = propertiesBytes, .numBytes = sizeof propertiesBytes } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPLinkedServices(const HAPService* _Nullable service, HAPTLVWriterRef* responseWriter) {
HAPPrecondition(responseWriter);
HAPError err;
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.4.4.1 HAP Linked Services
size_t linkedServicesCount = 0;
if (service && service->linkedServices) {
while (service->linkedServices[linkedServicesCount]) {
linkedServicesCount++;
}
}
void* bytes;
size_t maxBytes;
HAPTLVWriterGetScratchBytes(responseWriter, &bytes, &maxBytes);
uint8_t* linkedServicesBytes = HAPTLVScratchBufferAllocUnaligned(&bytes, &maxBytes, 2 * linkedServicesCount);
if (!linkedServicesBytes) {
HAPLog(&logObject, "Not enough memory to allocate HAP-Param-HAP-Linked-Services.");
return kHAPError_OutOfResources;
}
for (size_t i = 0; i < linkedServicesCount; i++) {
HAPWriteLittleUInt16(&linkedServicesBytes[2 * i], service->linkedServices[i]);
}
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPLinkedServices,
.value = { .bytes = linkedServicesBytes, .numBytes = 2 * linkedServicesCount } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPValidValuesDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.5.3 Valid Values Descriptor
if (characteristic->format != kHAPCharacteristicFormat_UInt8) {
return kHAPError_None;
}
const uint8_t* const* validValues = ((const HAPUInt8Characteristic*) characteristic)->constraints.validValues;
size_t validValuesCount = 0;
if (validValues) {
while (validValues[validValuesCount]) {
validValuesCount++;
}
}
if (!validValuesCount) {
return kHAPError_None;
}
// See HomeKit Accessory Protocol Specification R14
// Section 2.3.3.1 Valid Characteristic Values
HAPPrecondition(HAPUUIDIsAppleDefined(characteristic->characteristicType));
void* bytes;
size_t maxBytes;
HAPTLVWriterGetScratchBytes(responseWriter, &bytes, &maxBytes);
uint8_t* validValuesBytes = HAPTLVScratchBufferAllocUnaligned(&bytes, &maxBytes, validValuesCount);
if (!validValuesBytes) {
HAPLog(&logObject, "Not enough memory to allocate HAP-Param-HAP-Valid-Values-Descriptor.");
return kHAPError_OutOfResources;
}
for (size_t i = 0; i < validValuesCount; i++) {
validValuesBytes[i] = *validValues[i];
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.5.3 Valid Values Descriptor
if (i) {
HAPPrecondition(validValuesBytes[i] > validValuesBytes[i - 1]);
}
}
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPValidValuesDescriptor,
.value = { .bytes = validValuesBytes, .numBytes = validValuesCount } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}
//----------------------------------------------------------------------------------------------------------------------
HAP_RESULT_USE_CHECK
HAPError HAPBLEPDUTLVSerializeHAPValidValuesRangeDescriptor(
const HAPCharacteristic* characteristic_,
HAPTLVWriterRef* responseWriter) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(responseWriter);
HAPError err;
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.5.4 Valid Values Range Descriptor
if (characteristic->format != kHAPCharacteristicFormat_UInt8) {
return kHAPError_None;
}
const HAPUInt8CharacteristicValidValuesRange* const* validValuesRanges =
((const HAPUInt8Characteristic*) characteristic)->constraints.validValuesRanges;
size_t validValuesRangesCount = 0;
if (validValuesRanges) {
while (validValuesRanges[validValuesRangesCount]) {
validValuesRangesCount++;
}
}
if (!validValuesRangesCount) {
return kHAPError_None;
}
// See HomeKit Accessory Protocol Specification R14
// Section 2.3.3.1 Valid Characteristic Values
HAPPrecondition(HAPUUIDIsAppleDefined(characteristic->characteristicType));
void* bytes;
size_t maxBytes;
HAPTLVWriterGetScratchBytes(responseWriter, &bytes, &maxBytes);
uint8_t* validValuesRangesBytes = HAPTLVScratchBufferAllocUnaligned(&bytes, &maxBytes, 2 * validValuesRangesCount);
if (!validValuesRangesBytes) {
HAPLog(&logObject, "Not enough memory to allocate HAP-Param-HAP-Valid-Values-Range-Descriptor.");
return kHAPError_OutOfResources;
}
for (size_t i = 0; i < validValuesRangesCount; i++) {
HAPPrecondition(validValuesRanges[i]->start <= validValuesRanges[i]->end);
validValuesRangesBytes[2 * i + 0] = validValuesRanges[i]->start;
validValuesRangesBytes[2 * i + 1] = validValuesRanges[i]->end;
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.5.4 Valid Values Range Descriptor
if (i) {
HAPPrecondition(validValuesRangesBytes[2 * i] > validValuesRangesBytes[2 * i - 1]);
}
}
err = HAPTLVWriterAppend(
responseWriter,
&(const HAPTLV) { .type = kHAPBLEPDUTLVType_HAPValidValuesRangeDescriptor,
.value = { .bytes = validValuesRangesBytes, .numBytes = 2 * validValuesRangesCount } });
if (err) {
HAPAssert(err == kHAPError_OutOfResources);
return err;
}
return kHAPError_None;
}