-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPBLECharacteristicParseAndWriteValue.c
593 lines (560 loc) · 25 KB
/
HAPBLECharacteristicParseAndWriteValue.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
// 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 = "BLECharacteristic" };
/**
* Structure representing the Char Value TLV item.
*/
typedef struct {
void* bytes; /**< Start of value. */
size_t numBytes; /**< Length of value. */
size_t maxBytes; /**< Capacity of value, including free memory after the value. */
} HAPCharacteristicValueTLV;
/**
* Parses the body of a a HAP-Characteristic-Write-Request.
*
* @param characteristic_ Characteristic that received the request.
* @param requestReader Reader to parse Characteristic value from. Reader content will become invalid.
* @param[out] value Char Value TLV item.
* @param[out] remote Whether the request appears to be sent remotely.
* @param[out] authDataBytes Additional authorization data.
* @param[out] numAuthDataBytes Length of additional authorization data.
* @param[out] ttl TTL If a TTL TLV item was present; 0 Otherwise.
* @param[out] hasReturnResponse True If a Return-Response TLV item was present and set to 1; False Otherwise.
*
* @return kHAPError_None If successful.
* @return kHAPError_InvalidData If the controller sent a malformed request.
*/
HAP_RESULT_USE_CHECK
static HAPError ParseRequest(
const HAPCharacteristic* characteristic_,
HAPTLVReaderRef* requestReader,
HAPCharacteristicValueTLV* value,
bool* remote,
const void** authDataBytes,
size_t* numAuthDataBytes,
uint8_t* ttl,
bool* hasReturnResponse) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(requestReader);
HAPPrecondition(value);
HAPPrecondition(remote);
HAPPrecondition(authDataBytes);
HAPPrecondition(numAuthDataBytes);
HAPPrecondition(ttl);
HAPPrecondition(hasReturnResponse);
HAPError err;
HAPTLV valueTLV, authDataTLV, originTLV;
valueTLV.type = kHAPBLEPDUTLVType_Value;
authDataTLV.type = kHAPBLEPDUTLVType_AdditionalAuthorizationData;
originTLV.type = kHAPBLEPDUTLVType_Origin;
// See HomeKit Accessory Protocol Specification R14
// Section 7.3.5.4 HAP Characteristic Timed Write Procedure
HAPTLV ttlTLV;
ttlTLV.type = kHAPBLEPDUTLVType_TTL;
// See HomeKit Accessory Protocol Specification R14
// Section 7.3.5.5 HAP Characteristic Write-With-Response Procedure
HAPTLV returnResponseTLV;
returnResponseTLV.type = kHAPBLEPDUTLVType_ReturnResponse;
err = HAPTLVReaderGetAll(
requestReader,
(HAPTLV* const[]) { &valueTLV, &authDataTLV, &originTLV, &ttlTLV, &returnResponseTLV, NULL });
if (err) {
HAPAssert(err == kHAPError_InvalidData);
return err;
}
// HAP-Param-Value.
if (!valueTLV.value.bytes) {
HAPLog(&logObject, "HAP-Param-Value missing.");
return kHAPError_InvalidData;
}
// HAP-Param-Origin.
if (originTLV.value.bytes) {
if (originTLV.value.numBytes != 1) {
HAPLog(&logObject, "HAP-Param-Origin has invalid length (%lu).", (unsigned long) originTLV.value.numBytes);
return kHAPError_InvalidData;
}
uint8_t origin = ((const uint8_t*) originTLV.value.bytes)[0];
switch (origin) {
case 0: {
*remote = false;
} break;
case 1: {
*remote = true;
} break;
default: {
HAPLog(&logObject, "HAP-Param-Origin invalid: %u.", origin);
return kHAPError_InvalidData;
}
}
} else {
*remote = false;
}
// HAP-Param-Additional-Authorization-Data, HAP-Param-Origin.
*authDataBytes = NULL;
*numAuthDataBytes = 0;
if (characteristic->properties.supportsAuthorizationData) {
if (authDataTLV.value.bytes) {
if (!originTLV.value.bytes) {
// When additional authorization data is present it is included
// as an additional type to the TLV8 format along with the Value and Remote TLV types.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.5.2 Characteristic with Additional Authorization Data
HAPLog(&logObject, "HAP-Param-Origin missing but HAP-Param-Additional-Authorization-Data is present.");
return kHAPError_InvalidData;
}
*authDataBytes = authDataTLV.value.bytes;
*numAuthDataBytes = authDataTLV.value.numBytes;
}
} else if (authDataTLV.value.bytes) {
HAPLog(&logObject,
"HAP-Param-Additional-Authorization-Data present but Additional Authorization is not supported.");
}
// HAP-Param-TTL.
if (ttlTLV.value.bytes) {
if (ttlTLV.value.numBytes != 1) {
HAPLog(&logObject, "HAP-Param-TTL has invalid length (%lu).", (unsigned long) ttlTLV.value.numBytes);
return kHAPError_InvalidData;
}
*ttl = ((const uint8_t*) ttlTLV.value.bytes)[0];
} else {
*ttl = 0;
}
// HAP-Param-Return-Response.
if (returnResponseTLV.value.bytes) {
if (returnResponseTLV.value.numBytes != 1) {
HAPLog(&logObject,
"HAP-Param-Return-Response has invalid length (%lu).",
(unsigned long) returnResponseTLV.value.numBytes);
return kHAPError_InvalidData;
}
uint8_t returnResponse = ((const uint8_t*) returnResponseTLV.value.bytes)[0];
if (returnResponse != 1) {
HAPLog(&logObject, "HAP-Param-Return-Response invalid: %u.", returnResponse);
return kHAPError_InvalidData;
}
*hasReturnResponse = true;
} else {
*hasReturnResponse = false;
}
// Optimize memory. We want as much free space as possible after the value.
uint8_t* bytes = ((HAPTLVReader*) requestReader)->bytes;
size_t maxBytes = ((HAPTLVReader*) requestReader)->maxBytes;
// TLV values are always NULL terminated to simplify string handling. This property should be retained.
// The NULL terminator is not counted in the TLV value's numBytes.
// Case 1: [ AAD | VAL | empty ]
// Case 2: [ VAL | empty | AAD ]
// Case 3: [ VAL | empty ]
// AAD and VAL fields contain an additional NULL byte.
HAPRawBufferZero(value, sizeof *value);
if (*authDataBytes) {
size_t numValueBytes = valueTLV.value.numBytes;
size_t numValueBytesWithNull = numValueBytes + 1;
size_t numAuthDataBytesWithNull = *numAuthDataBytes + 1;
void* valueStart;
void* authDataStart;
if (*authDataBytes < valueTLV.value.bytes) {
// Case 1.
authDataStart = &bytes[0];
valueStart = &bytes[numAuthDataBytesWithNull];
} else {
// Case 2.
authDataStart = &bytes[maxBytes - numAuthDataBytesWithNull];
valueStart = &bytes[0];
}
// Move AAD.
HAPRawBufferCopyBytes(authDataStart, *authDataBytes, numAuthDataBytesWithNull);
*authDataBytes = authDataStart;
// Move VAL.
HAPRawBufferCopyBytes(valueStart, HAPNonnullVoid(valueTLV.value.bytes), numValueBytesWithNull);
value->bytes = valueStart;
value->numBytes = numValueBytes;
value->maxBytes = maxBytes - numAuthDataBytesWithNull;
} else {
size_t numValueBytes = valueTLV.value.numBytes;
size_t numValueBytesWithNULL = numValueBytes + 1;
// Case 3.
void* valueStart = &bytes[0];
// Move VAL.
HAPRawBufferCopyBytes(valueStart, HAPNonnullVoid(valueTLV.value.bytes), numValueBytesWithNULL);
value->bytes = valueStart;
value->numBytes = numValueBytes;
value->maxBytes = maxBytes;
}
HAPAssert(((const uint8_t*) value->bytes)[value->numBytes] == '\0');
if (*authDataBytes) {
HAPAssert(((const uint8_t*) *authDataBytes)[*numAuthDataBytes] == '\0');
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLECharacteristicParseAndWriteValue(
HAPAccessoryServerRef* server_,
HAPSessionRef* session,
const HAPCharacteristic* characteristic,
const HAPService* service,
const HAPAccessory* accessory,
HAPTLVReaderRef* requestReader,
const HAPTime* _Nullable timedWriteStartTime,
bool* hasExpired,
bool* hasReturnResponse) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(session);
HAPPrecondition(characteristic);
HAPPrecondition(service);
HAPPrecondition(accessory);
HAPPrecondition(requestReader);
HAPPrecondition(hasExpired);
HAPPrecondition(hasReturnResponse);
HAPError err;
*hasExpired = false;
*hasReturnResponse = false;
HAPCharacteristicValueTLV value = { .bytes = NULL, .numBytes = 0, .maxBytes = 0 };
bool remote = false;
const void* authDataBytes = NULL;
size_t numAuthDataBytes = 0;
uint8_t ttl = 0;
err = ParseRequest(
characteristic, requestReader, &value, &remote, &authDataBytes, &numAuthDataBytes, &ttl, hasReturnResponse);
if (err) {
HAPAssert(err == kHAPError_InvalidData);
return err;
}
// Handle Timed Write.
if (timedWriteStartTime) {
if (!ttl) {
HAPLog(&logObject, "Timed Write Request did not include valid TTL.");
return kHAPError_InvalidData;
}
HAPTime now = HAPPlatformClockGetCurrent();
*hasExpired = now >= ttl * 100 * HAPMillisecond && now - ttl * 100 * HAPMillisecond > *timedWriteStartTime;
if (*hasExpired) {
return kHAPError_None;
}
}
// The maximum length of an HAP characteristic value shall be 64000 bytes.
// See HomeKit Accessory Protocol Specification R14
// Section 7.4.1.7 Maximum Payload Size
if (value.numBytes > 64000) {
HAPLog(&logObject, "Value exceeds maximum allowed length of 64000 bytes.");
return kHAPError_InvalidData;
}
// Parse value and handle write.
uint8_t* bytes = value.bytes;
size_t numBytes = value.numBytes;
switch (*((const HAPCharacteristicFormat*) characteristic)) {
case kHAPCharacteristicFormat_Data: {
err = HAPDataCharacteristicHandleWrite(
server_,
&(const HAPDataCharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
bytes,
numBytes,
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Bool: {
if (numBytes != sizeof(bool)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
if (bytes[0] != 0 && bytes[0] != 1) {
HAPLogCharacteristic(
&logObject, characteristic, service, accessory, "Unexpected bool value: %u.", bytes[0]);
return kHAPError_InvalidData;
}
err = HAPBoolCharacteristicHandleWrite(
server_,
&(const HAPBoolCharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
bytes[0] != 0,
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt8: {
if (numBytes != sizeof(uint8_t)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
err = HAPUInt8CharacteristicHandleWrite(
server_,
&(const HAPUInt8CharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
bytes[0],
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt16: {
if (numBytes != sizeof(uint16_t)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
err = HAPUInt16CharacteristicHandleWrite(
server_,
&(const HAPUInt16CharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
HAPReadLittleUInt16(bytes),
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt32: {
if (numBytes != sizeof(uint32_t)) {
HAPLogCharacteristic(&logObject, characteristic, service, accessory, "Unexpected value length.");
return kHAPError_InvalidData;
}
err = HAPUInt32CharacteristicHandleWrite(
server_,
&(const HAPUInt32CharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
HAPReadLittleUInt32(bytes),
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_UInt64: {
if (numBytes != sizeof(uint64_t)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
err = HAPUInt64CharacteristicHandleWrite(
server_,
&(const HAPUInt64CharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
HAPReadLittleUInt64(bytes),
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Int: {
if (numBytes != sizeof(int32_t)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
err = HAPIntCharacteristicHandleWrite(
server_,
&(const HAPIntCharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
HAPReadLittleInt32(bytes),
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_Float: {
if (numBytes != sizeof(float)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Unexpected value length: %lu.",
(unsigned long) numBytes);
return kHAPError_InvalidData;
}
uint32_t bitPattern = HAPReadLittleUInt32(bytes);
err = HAPFloatCharacteristicHandleWrite(
server_,
&(const HAPFloatCharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
HAPFloatFromBitPattern(bitPattern),
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_String: {
if (numBytes != HAPStringGetNumBytes(value.bytes)) {
HAPLogSensitiveCharacteristicBuffer(
&logObject,
characteristic,
service,
accessory,
bytes,
numBytes,
"Unexpected string value (contains NULL bytes).");
return kHAPError_InvalidData;
}
if (!HAPUTF8IsValidData(bytes, numBytes)) {
HAPLogSensitiveCharacteristicBuffer(
&logObject,
characteristic,
service,
accessory,
bytes,
numBytes,
"Unexpected string value (invalid UTF-8 encoding).");
return kHAPError_InvalidData;
}
err = HAPStringCharacteristicHandleWrite(
server_,
&(const HAPStringCharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
(const char*) bytes,
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
case kHAPCharacteristicFormat_TLV8: {
HAPTLVReaderRef reader;
HAPTLVReaderCreateWithOptions(
&reader,
&(const HAPTLVReaderOptions) { .bytes = bytes, .numBytes = numBytes, .maxBytes = value.maxBytes });
err = HAPTLV8CharacteristicHandleWrite(
server_,
&(const HAPTLV8CharacteristicWriteRequest) {
.transportType = kHAPTransportType_BLE,
.session = session,
.characteristic = characteristic,
.service = service,
.accessory = accessory,
.remote = remote,
.authorizationData = { .bytes = authDataBytes, .numBytes = numAuthDataBytes } },
&reader,
server->context);
if (err) {
HAPAssert(
err == kHAPError_Unknown || err == kHAPError_InvalidState || err == kHAPError_InvalidData ||
err == kHAPError_OutOfResources || err == kHAPError_NotAuthorized || err == kHAPError_Busy);
return err;
}
}
return kHAPError_None;
}
HAPFatalError();
}