-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPAccessoryValidation.c
809 lines (763 loc) · 35.5 KB
/
HAPAccessoryValidation.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
// 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 = "AccessoryValidation" };
/**
* Maximum length of an accessory's display name.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.62 Name
*/
#define kHAPAccessory_MaxNameBytes ((size_t) 64)
/**
* Maximum length of an accessory's manufacturer.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.58 Manufacturer
*/
#define kHAPAccessory_MaxManufacturerBytes ((size_t) 64)
/**
* Minimum length of an accessory's model name.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.59 Model
*/
#define kHAPAccessory_MinModelBytes ((size_t) 1)
/**
* Maximum length of an accessory's model name.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.59 Model
*/
#define kHAPAccessory_MaxModelBytes ((size_t) 64)
/**
* Minimum length of an accessory's serial number.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.87 Serial Number
*/
#define kHAPAccessory_MinSerialNumberBytes ((size_t) 2)
/**
* Maximum length of an accessory's serial number.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 9.87 Serial Number
*/
#define kHAPAccessory_MaxSerialNumberBytes ((size_t) 64)
/**
* Validates generic rules of an accessory definition.
*
* @param accessory Accessory to validate.
*
* @return true If the accessory definition is valid.
* @return false Otherwise.
*/
static bool AccessoryIsValid(const HAPAccessory* accessory) {
HAPPrecondition(accessory);
// Validate accessory information.
if (!accessory->name) {
HAPLogError(&logObject, "Accessory 0x%016llX %s is not set.", (unsigned long long) accessory->aid, "name");
return false;
}
size_t numNameBytes = HAPStringGetNumBytes(accessory->name);
if (numNameBytes > kHAPAccessory_MaxNameBytes) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s has invalid length (%zu) - expected: max %zu.",
"name",
accessory->name,
numNameBytes,
kHAPAccessory_MaxNameBytes);
return false;
}
if (!HAPUTF8IsValidData(accessory->name, numNameBytes)) {
HAPLogAccessoryError(
&logObject, accessory, "Accessory %s %s is not a valid UTF-8 string.", "name", accessory->name);
return false;
}
if (!accessory->manufacturer) {
HAPLogError(
&logObject, "Accessory 0x%016llX %s is not set.", (unsigned long long) accessory->aid, "manufacturer");
return false;
}
size_t numManufacturerBytes = HAPStringGetNumBytes(accessory->manufacturer);
if (numManufacturerBytes > kHAPAccessory_MaxManufacturerBytes) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s has invalid length (%zu) - expected: max %zu.",
"manufacturer",
accessory->manufacturer,
numManufacturerBytes,
kHAPAccessory_MaxManufacturerBytes);
return false;
}
if (!HAPUTF8IsValidData(accessory->manufacturer, numManufacturerBytes)) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s is not a valid UTF-8 string.",
"manufacturer",
accessory->manufacturer);
return false;
}
if (!accessory->model) {
HAPLogError(&logObject, "Accessory 0x%016llX %s is not set.", (unsigned long long) accessory->aid, "model");
return false;
}
size_t numModelBytes = HAPStringGetNumBytes(accessory->model);
if (numModelBytes < kHAPAccessory_MinModelBytes || numModelBytes > kHAPAccessory_MaxModelBytes) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s has invalid length (%zu) - expected: min %zu, max %zu.",
"model",
accessory->model,
numModelBytes,
kHAPAccessory_MinModelBytes,
kHAPAccessory_MaxModelBytes);
return false;
}
if (!HAPUTF8IsValidData(accessory->model, numModelBytes)) {
HAPLogAccessoryError(
&logObject, accessory, "Accessory %s %s is not a valid UTF-8 string.", "model", accessory->model);
return false;
}
if (!accessory->serialNumber) {
HAPLogError(
&logObject, "Accessory 0x%016llX %s is not set.", (unsigned long long) accessory->aid, "serialNumber");
return false;
}
size_t numSerialNumberBytes = HAPStringGetNumBytes(accessory->serialNumber);
if (numSerialNumberBytes < kHAPAccessory_MinSerialNumberBytes ||
numSerialNumberBytes > kHAPAccessory_MaxSerialNumberBytes) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s has invalid length (%zu) - expected: min %zu, max %zu.",
"serial number",
accessory->serialNumber,
numSerialNumberBytes,
kHAPAccessory_MinSerialNumberBytes,
kHAPAccessory_MaxSerialNumberBytes);
return false;
}
if (!HAPUTF8IsValidData(accessory->serialNumber, numSerialNumberBytes)) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s is not a valid UTF-8 string.",
"serialNumber",
accessory->serialNumber);
return false;
}
if (!accessory->firmwareVersion) {
HAPLogError(
&logObject,
"Accessory 0x%016llX %s is not set.",
(unsigned long long) accessory->aid,
"firmwareVersion");
return false;
}
size_t numFirmwareVersionBytes = HAPStringGetNumBytes(accessory->firmwareVersion);
if (!HAPUTF8IsValidData(accessory->firmwareVersion, numFirmwareVersionBytes)) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s is not a valid UTF-8 string.",
"firmwareVersion",
accessory->firmwareVersion);
return false;
}
if (accessory->hardwareVersion) {
size_t numHardwareVersionBytes = HAPStringGetNumBytes(HAPNonnull(accessory->hardwareVersion));
if (!HAPUTF8IsValidData(HAPNonnull(accessory->hardwareVersion), numHardwareVersionBytes)) {
HAPLogAccessoryError(
&logObject,
accessory,
"Accessory %s %s is not a valid UTF-8 string.",
"hardwareVersion",
HAPNonnull(accessory->hardwareVersion));
return false;
}
}
if (!accessory->services) {
HAPLogAccessoryError(
&logObject, accessory, "Accessory must at least contain the Accessory Information Service.");
return false;
}
for (size_t i = 0; accessory->services[i]; i++) {
const HAPService* service = accessory->services[i];
if (!service->debugDescription) {
HAPLogAccessoryError(
&logObject,
accessory,
"Service 0x%016llX debugDescription is not set.",
(unsigned long long) service->iid);
return false;
}
size_t numServiceDebugDescriptionBytes = HAPStringGetNumBytes(service->debugDescription);
if (!HAPUTF8IsValidData(service->debugDescription, numServiceDebugDescriptionBytes)) {
HAPLogServiceError(
&logObject,
service,
accessory,
"Service debugDescription %s is not a valid UTF-8 string.",
service->debugDescription);
return false;
}
if (service->name) {
size_t numServiceNameBytes = HAPStringGetNumBytes(HAPNonnull(service->name));
if (!HAPUTF8IsValidData(HAPNonnull(service->name), numServiceNameBytes)) {
HAPLogServiceError(
&logObject, service, accessory, "Service name %s is not a valid UTF-8 string.", service->name);
return false;
}
}
if (service->linkedServices) {
for (size_t j = 0; service->linkedServices[j]; j++) {
uint16_t linkedService = service->linkedServices[j];
for (size_t k = 0; k < j; k++) {
if (linkedService == service->linkedServices[k]) {
HAPLogServiceError(
&logObject,
service,
accessory,
"linkedServices entry 0x%016llX specified multiple times.",
(unsigned long long) service->linkedServices[k]);
return false;
}
}
bool found = false;
for (size_t k = 0; accessory->services[k]; k++) {
const HAPService* otherService = accessory->services[k];
if (otherService->iid == linkedService) {
found = true;
break;
}
}
if (!found) {
HAPLogServiceError(
&logObject,
service,
accessory,
"linkedServices entry 0x%016llX does not correspond to a specified service.",
(unsigned long long) linkedService);
return false;
}
}
}
bool allCharacteristicsHidden = true;
if (service->characteristics) {
for (size_t j = 0; service->characteristics[j]; j++) {
const HAPBaseCharacteristic* characteristic = service->characteristics[j];
// Validate characteristic.
if (!characteristic->debugDescription) {
HAPLogServiceError(
&logObject,
service,
accessory,
"Characteristic 0x%016llX debugDescription is not set.",
(unsigned long long) characteristic->iid);
return false;
}
size_t numCharacteristicDebugDescriptionBytes = HAPStringGetNumBytes(characteristic->debugDescription);
if (!HAPUTF8IsValidData(characteristic->debugDescription, numCharacteristicDebugDescriptionBytes)) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic debugDescription %s is not a valid UTF-8 string.",
characteristic->debugDescription);
return false;
}
if (characteristic->manufacturerDescription) {
size_t numManufacturerDescriptionBytes =
HAPStringGetNumBytes(HAPNonnull(characteristic->manufacturerDescription));
if (!HAPUTF8IsValidData(
HAPNonnull(characteristic->manufacturerDescription), numManufacturerDescriptionBytes)) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic manufacturerDescription %s is not a valid UTF-8 string.",
characteristic->manufacturerDescription);
return false;
}
}
allCharacteristicsHidden &= characteristic->properties.hidden;
#define BASE_CHARACTERISTIC_CHECKS(chr) \
do { \
/* readable. */ \
if (chr->properties.readable && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as readable but no handleRead callback set."); \
return false; \
} \
\
/* writable. */ \
if (chr->properties.writable && !chr->callbacks.handleWrite) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as writable but no handleWrite callback set."); \
return false; \
} \
\
/* supportsEventNotification. */ \
if (chr->properties.supportsEventNotification && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as supportsEventNotification but no handleRead callback set."); \
return false; \
} \
\
/* readRequiresAdminPermissions. */ \
if (chr->properties.readRequiresAdminPermissions && !chr->properties.readable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as readRequiresAdminPermissions but not as readable."); \
return false; \
} \
\
/* writeRequiresAdminPermissions. */ \
if (chr->properties.writeRequiresAdminPermissions && !chr->properties.writable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as writeRequiresAdminPermissions but not as writable."); \
return false; \
} \
\
/* readRequiresAdminPermissions, writeRequiresAdminPermissions. */ \
if (HAPCharacteristicReadRequiresAdminPermissions(chr) && chr->properties.writable && \
!HAPCharacteristicWriteRequiresAdminPermissions(chr)) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as readRequiresAdminPermissions and writable " \
"but not as writeRequiresAdminPermissions."); \
return false; \
} \
\
/* requiresTimedWrite. */ \
if (chr->properties.requiresTimedWrite && !chr->properties.writable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as requiresTimedWrite but not as writable."); \
return false; \
} \
\
/* supportsAuthorizationData. */ \
if (chr->properties.supportsAuthorizationData && !chr->properties.writable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as supportsAuthorizationData but not as writable."); \
return false; \
} \
\
/* ip.supportsWriteResponse. */ \
if (chr->properties.ip.supportsWriteResponse && !chr->properties.writable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ip.supportsWriteResponse but not as writable."); \
return false; \
} \
if (chr->properties.ip.supportsWriteResponse && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ip.supportsWriteResponse but no handleRead callback set."); \
return false; \
} \
if (chr->properties.ip.supportsWriteResponse && !chr->callbacks.handleWrite) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ip.supportsWriteResponse but no handleWrite callback set."); \
return false; \
} \
\
/* ble.supportsBroadcastNotification */ \
if (chr->properties.ble.supportsBroadcastNotification && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.supportsBroadcastNotification " \
"but no handleRead callback set."); \
return false; \
} \
\
/* ble.supportsDisconnectedNotification */ \
if (chr->properties.ble.supportsDisconnectedNotification && !chr->properties.readable) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.supportsDisconnectedNotification " \
"but not as readable."); \
return false; \
} \
if (chr->properties.ble.supportsDisconnectedNotification && !chr->properties.supportsEventNotification) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.supportsDisconnectedNotification " \
"but not as supportsEventNotification."); \
return false; \
} \
if (chr->properties.ble.supportsDisconnectedNotification && \
!chr->properties.ble.supportsBroadcastNotification) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.supportsDisconnectedNotification " \
"but not as ble.supportsBroadcastNotification."); \
return false; \
} \
if (chr->properties.ble.supportsDisconnectedNotification && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.supportsDisconnectedNotification " \
"but no handleRead callback set."); \
return false; \
} \
\
/* ble.readableWithoutSecurity. */ \
if (chr->properties.readable && !chr->callbacks.handleRead) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.readableWithoutSecurity " \
"but no handleRead callback set."); \
return false; \
} \
\
/* ble.writableWithoutSecurity. */ \
if (chr->properties.writable && !chr->callbacks.handleWrite) { \
HAPLogCharacteristicError( \
&logObject, \
characteristic, \
service, \
accessory, \
"Characteristic marked as ble.writableWithoutSecurity " \
"but no handleWrite callback set."); \
return false; \
} \
} while (0)
switch (characteristic->format) {
case kHAPCharacteristicFormat_Data: {
const HAPDataCharacteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
} break;
case kHAPCharacteristicFormat_Bool: {
const HAPBoolCharacteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
} break;
case kHAPCharacteristicFormat_UInt8: {
const HAPUInt8Characteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
if (chr->constraints.minimumValue > chr->constraints.maximumValue) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %u / maximumValue = %u / stepValue = %u).",
chr->constraints.minimumValue,
chr->constraints.maximumValue,
chr->constraints.stepValue);
return false;
}
const uint8_t* _Nullable const* _Nullable validValues = chr->constraints.validValues;
const HAPUInt8CharacteristicValidValuesRange* _Nullable const* _Nullable validValuesRanges =
chr->constraints.validValuesRanges;
if (validValues || validValuesRanges) {
if (!HAPUUIDIsAppleDefined(chr->characteristicType)) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Only Apple-defined characteristics can specify "
"validValues and validValuesRanges constraints.");
return false;
}
if (validValues) {
for (size_t k = 0; validValues[k]; k++) {
if (k) {
if (*validValues[k] <= *validValues[k - 1]) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic validValues must be sorted in ascending order "
"(%u is listed before %u).",
*validValues[k - 1],
*validValues[k]);
return false;
}
}
}
}
if (validValuesRanges) {
for (size_t k = 0; validValuesRanges[k]; k++) {
if (validValuesRanges[k]->start > validValuesRanges[k]->end) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic validValuesRanges invalid ([%u ... %u]).",
validValuesRanges[k]->start,
validValuesRanges[k]->end);
return false;
}
if (k) {
if (validValuesRanges[k]->start < validValuesRanges[k - 1]->end) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic validValuesRanges must be sorted in ascending "
"order "
"([%u ... %u] is listed before [%u ... %u]).",
validValuesRanges[k - 1]->start,
validValuesRanges[k - 1]->end,
validValuesRanges[k]->start,
validValuesRanges[k]->end);
return false;
}
}
}
}
}
} break;
case kHAPCharacteristicFormat_UInt16: {
const HAPUInt16Characteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
if (chr->constraints.minimumValue > chr->constraints.maximumValue) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %u / maximumValue = %u / stepValue = %u).",
chr->constraints.minimumValue,
chr->constraints.maximumValue,
chr->constraints.stepValue);
return false;
}
} break;
case kHAPCharacteristicFormat_UInt32: {
const HAPUInt32Characteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
if (chr->constraints.minimumValue > chr->constraints.maximumValue) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %lu / maximumValue = %lu / stepValue = %lu).",
(unsigned long) chr->constraints.minimumValue,
(unsigned long) chr->constraints.maximumValue,
(unsigned long) chr->constraints.stepValue);
return false;
}
} break;
case kHAPCharacteristicFormat_UInt64: {
const HAPUInt64Characteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
if (chr->constraints.minimumValue > chr->constraints.maximumValue) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %llu / maximumValue = %llu / stepValue = %llu).",
(unsigned long long) chr->constraints.minimumValue,
(unsigned long long) chr->constraints.maximumValue,
(unsigned long long) chr->constraints.stepValue);
return false;
}
} break;
case kHAPCharacteristicFormat_Int: {
const HAPIntCharacteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
if (chr->constraints.minimumValue > chr->constraints.maximumValue ||
chr->constraints.stepValue < 0) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %ld / maximumValue = %ld / stepValue = %ld).",
(long) chr->constraints.minimumValue,
(long) chr->constraints.maximumValue,
(long) chr->constraints.stepValue);
return false;
}
} break;
case kHAPCharacteristicFormat_Float: {
const HAPFloatCharacteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
float minimumValue = chr->constraints.minimumValue;
float maximumValue = chr->constraints.maximumValue;
float stepValue = chr->constraints.stepValue;
if ((!HAPFloatIsFinite(minimumValue) && !HAPFloatIsInfinite(minimumValue)) ||
(!HAPFloatIsFinite(maximumValue) && !HAPFloatIsInfinite(maximumValue)) ||
chr->constraints.minimumValue > chr->constraints.maximumValue ||
!HAPFloatIsFinite(stepValue) || chr->constraints.stepValue < 0) {
HAPLogCharacteristicError(
&logObject,
characteristic,
service,
accessory,
"Characteristic constraints invalid "
"(constraints: minimumValue = %g / maximumValue = %g / stepValue = %g).",
(double) chr->constraints.minimumValue,
(double) chr->constraints.maximumValue,
(double) chr->constraints.stepValue);
return false;
}
} break;
case kHAPCharacteristicFormat_String: {
const HAPStringCharacteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
} break;
case kHAPCharacteristicFormat_TLV8: {
const HAPTLV8Characteristic* chr = service->characteristics[j];
BASE_CHARACTERISTIC_CHECKS(chr);
} break;
}
#undef BASE_CHARACTERISTIC_CHECKS
}
}
// When all characteristics in a services are marked hidden then the service must also be marked as hidden.
// See HomeKit Accessory Protocol Specification R14
// Section 2.3.2.4 Hidden Service
if (allCharacteristicsHidden && !service->properties.hidden) {
HAPLogServiceError(
&logObject,
service,
accessory,
"Service must be marked hidden if all of its characteristics are marked hidden.");
return false;
}
// iOS 11: The configuration attribute is only working on HAP Protocol Information service.
if (service->properties.ble.supportsConfiguration &&
!HAPUUIDAreEqual(service->serviceType, &kHAPServiceType_HAPProtocolInformation)) {
HAPLogServiceError(
&logObject,
service,
accessory,
"Only the HAP Protocol Information service may support configuration.");
return false;
}
}
return true;
}
bool HAPRegularAccessoryIsValid(HAPAccessoryServerRef* server_, const HAPAccessory* accessory) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(accessory);
HAPPrecondition(accessory->name);
if (accessory->aid != 1) {
HAPLogAccessoryError(&logObject, accessory, "Primary accessory must have aid 1.");
return false;
}
// Validate category.
if (!accessory->category) {
HAPLogAccessoryError(
&logObject, accessory, "Invalid accessory category has been selected (%u).", accessory->category);
return false;
}
// Validate BLE specific requirements.
if (server->transports.ble) {
// Work around iOS Bluetooth limitations.
// "The Local Name should match the accessory's markings and packaging and not contain ':' or ';'."
// See Accessory Design Guidelines for Apple Devices R7
// Section 11.4 Advertising Data
for (const char* c = accessory->name; *c; c++) {
if (*c == ':' || *c == ';') {
HAPLogAccessoryError(&logObject, accessory, "The accessory name should not contain ':' or ';'.");
}
}
}
if (!AccessoryIsValid(accessory)) {
return false;
}
return true;
}
bool HAPBridgedAccessoryIsValid(const HAPAccessory* bridgedAccessory) {
HAPPrecondition(bridgedAccessory);
if (bridgedAccessory->aid == 1) {
HAPLogAccessoryError(&logObject, bridgedAccessory, "Bridged accessory must have aid other than 1.");
return false;
}
if (bridgedAccessory->category != kHAPAccessoryCategory_BridgedAccessory) {
HAPLogAccessoryError(
&logObject,
bridgedAccessory,
"Bridged accessory must have category kHAPAccessoryCategory_BridgedAccessory.");
return false;
}
if (!AccessoryIsValid(bridgedAccessory)) {
return false;
}
return true;
}