-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPBLECharacteristic+Broadcast.c
395 lines (359 loc) · 13.3 KB
/
HAPBLECharacteristic+Broadcast.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
// 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" };
HAP_RESULT_USE_CHECK
bool HAPBLECharacteristicIsValidBroadcastInterval(uint8_t value) {
switch (value) {
case kHAPBLECharacteristicBroadcastInterval_20Ms:
case kHAPBLECharacteristicBroadcastInterval_1280Ms:
case kHAPBLECharacteristicBroadcastInterval_2560Ms: {
return true;
}
default: {
return false;
}
}
}
typedef struct {
uint16_t aid;
bool* found;
void* bytes;
size_t maxBytes;
size_t* numBytes;
HAPPlatformKeyValueStoreKey* key;
} GetBroadcastParametersEnumerateContext;
HAP_RESULT_USE_CHECK
static HAPError GetBroadcastConfigurationEnumerateCallback(
void* _Nullable context,
HAPPlatformKeyValueStoreRef keyValueStore,
HAPPlatformKeyValueStoreDomain domain,
HAPPlatformKeyValueStoreKey key,
bool* shouldContinue) {
HAPPrecondition(context);
GetBroadcastParametersEnumerateContext* arguments = context;
HAPPrecondition(arguments->aid);
HAPPrecondition(arguments->aid == 1);
HAPPrecondition(arguments->found);
HAPPrecondition(!*arguments->found);
HAPPrecondition(arguments->bytes);
HAPPrecondition(arguments->maxBytes >= 2);
HAPPrecondition(arguments->numBytes);
HAPPrecondition(arguments->key);
HAPPrecondition(domain == kHAPKeyValueStoreDomain_CharacteristicConfiguration);
HAPPrecondition(shouldContinue);
HAPPrecondition(*shouldContinue);
HAPError err;
// Load.
err = HAPPlatformKeyValueStoreGet(
keyValueStore, domain, key, arguments->bytes, arguments->maxBytes, arguments->numBytes, arguments->found);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPAssert(arguments->found);
if (*arguments->numBytes < 2 || *arguments->numBytes == arguments->maxBytes || (*arguments->numBytes - 2) % 3) {
HAPLog(&logObject,
"Invalid characteristic configuration 0x%02X size %lu.",
key,
(unsigned long) *arguments->numBytes);
return kHAPError_Unknown;
}
// Check for match.
if (HAPReadLittleUInt16(arguments->bytes) != arguments->aid) {
*arguments->found = false;
return kHAPError_None;
}
// Match found.
*arguments->key = key;
*shouldContinue = false;
return kHAPError_None;
}
/**
* Fetches the characteristic configuration for an accessory.
*
* @param aid Accessory ID.
* @param[out] found Whether the characteristic configuration has been found.
* @param[out] bytes Buffer to store characteristic configuration, if found.
* @param maxBytes Capacity of buffer. Must be at least 2 + 3 * #<concurrent active broadcasts> + 1.
* @param[out] numBytes Effective length of buffer, if found.
* @param[out] key Key, if found.
* @param keyValueStore Key-value store.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If an I/O error occurred.
*/
HAP_RESULT_USE_CHECK
static HAPError GetBroadcastConfiguration(
uint16_t aid,
bool* found,
void* bytes,
size_t maxBytes,
size_t* numBytes,
HAPPlatformKeyValueStoreKey* key,
HAPPlatformKeyValueStoreRef keyValueStore) {
HAPPrecondition(aid);
HAPPrecondition(found);
HAPPrecondition(bytes);
HAPPrecondition(maxBytes >= 3);
HAPPrecondition(numBytes);
HAPPrecondition(key);
HAPPrecondition(keyValueStore);
HAPError err;
*found = false;
GetBroadcastParametersEnumerateContext context = {
.aid = aid, .found = found, .bytes = bytes, .maxBytes = maxBytes, .numBytes = numBytes, .key = key
};
err = HAPPlatformKeyValueStoreEnumerate(
keyValueStore,
kHAPKeyValueStoreDomain_CharacteristicConfiguration,
GetBroadcastConfigurationEnumerateCallback,
&context);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLECharacteristicGetBroadcastConfiguration(
const HAPCharacteristic* characteristic_,
const HAPService* service,
const HAPAccessory* accessory,
bool* broadcastsEnabled,
HAPBLECharacteristicBroadcastInterval* broadcastInterval,
HAPPlatformKeyValueStoreRef keyValueStore) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(characteristic->properties.ble.supportsBroadcastNotification);
HAPPrecondition(service);
HAPPrecondition(accessory);
HAPPrecondition(broadcastsEnabled);
HAPPrecondition(broadcastInterval);
HAPPrecondition(keyValueStore);
HAPError err;
HAPAssert(accessory->aid == 1);
uint16_t aid = (uint16_t) accessory->aid;
HAPAssert(characteristic->iid <= UINT16_MAX);
uint16_t cid = (uint16_t) characteristic->iid;
// Get configuration.
HAPPlatformKeyValueStoreKey key;
size_t numBytes;
uint8_t bytes[2 + 3 * 42 + 1]; // 128 + 1, allows for 42 concurrent broadcasts on a single KVS key.
bool found;
err = GetBroadcastConfiguration(aid, &found, bytes, sizeof bytes, &numBytes, &key, keyValueStore);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!found) {
*broadcastsEnabled = false;
return kHAPError_None;
}
HAPAssert(numBytes >= 2 && !((numBytes - 2) % 3));
HAPAssert(HAPReadLittleUInt16(bytes) == aid);
// Find characteristic.
for (size_t i = 2; i < numBytes; i += 3) {
uint16_t itemCID = HAPReadLittleUInt16(&bytes[i]);
if (itemCID < cid) {
continue;
}
if (itemCID > cid) {
break;
}
// Found. Extract configuration.
uint8_t broadcastConfiguration = bytes[i + 2];
if (!HAPBLECharacteristicIsValidBroadcastInterval(broadcastConfiguration)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Invalid stored broadcast interval: 0x%02x.",
broadcastConfiguration);
return kHAPError_Unknown;
}
*broadcastsEnabled = true;
*broadcastInterval = (HAPBLECharacteristicBroadcastInterval) broadcastConfiguration;
return kHAPError_None;
}
// Not found.
*broadcastsEnabled = false;
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLECharacteristicEnableBroadcastNotifications(
const HAPCharacteristic* characteristic_,
const HAPService* service,
const HAPAccessory* accessory,
HAPBLECharacteristicBroadcastInterval broadcastInterval,
HAPPlatformKeyValueStoreRef keyValueStore) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(characteristic->properties.ble.supportsBroadcastNotification);
HAPPrecondition(service);
HAPPrecondition(accessory);
HAPPrecondition(HAPBLECharacteristicIsValidBroadcastInterval(broadcastInterval));
HAPPrecondition(keyValueStore);
HAPError err;
HAPLogCharacteristicInfo(
&logObject,
characteristic,
service,
accessory,
"Enabling broadcasts (interval = 0x%02x).",
broadcastInterval);
HAPAssert(accessory->aid == 1);
uint16_t aid = (uint16_t) accessory->aid;
HAPAssert(characteristic->iid <= UINT16_MAX);
uint16_t cid = (uint16_t) characteristic->iid;
// Get configuration.
HAPPlatformKeyValueStoreKey key;
size_t numBytes;
uint8_t bytes[2 + 3 * 42 + 1]; // 128 + 1, allows for 42 concurrent broadcasts on a single KVS key.
bool found;
err = GetBroadcastConfiguration(aid, &found, bytes, sizeof bytes, &numBytes, &key, keyValueStore);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!found) {
key = 0;
HAPWriteLittleUInt16(bytes, aid);
numBytes = 2;
}
HAPAssert(numBytes >= 2 && !((numBytes - 2) % 3));
HAPAssert(HAPReadLittleUInt16(bytes) == aid);
// Find characteristic.
size_t i;
for (i = 2; i < numBytes; i += 3) {
uint16_t itemCID = HAPReadLittleUInt16(&bytes[i]);
if (itemCID < cid) {
continue;
}
if (itemCID > cid) {
break;
}
// Found. Extract configuration.
uint8_t broadcastConfiguration = bytes[i + 2];
if (!HAPBLECharacteristicIsValidBroadcastInterval(broadcastConfiguration)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Invalid stored broadcast interval: 0x%02x.",
broadcastConfiguration);
return kHAPError_Unknown;
}
// Update configuration.
if ((HAPBLECharacteristicBroadcastInterval) broadcastConfiguration == broadcastInterval) {
return kHAPError_None;
}
bytes[i + 2] = broadcastInterval;
err = HAPPlatformKeyValueStoreSet(
keyValueStore, kHAPKeyValueStoreDomain_CharacteristicConfiguration, key, bytes, numBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
return kHAPError_None;
}
// Add configuration.
if (numBytes >= sizeof bytes - 1 - 3) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Not enough space to store characteristic configuration.");
return kHAPError_Unknown;
}
HAPRawBufferCopyBytes(&bytes[i + 3], &bytes[i], numBytes - i);
HAPWriteLittleUInt16(&bytes[i], cid);
bytes[i + 2] = broadcastInterval;
numBytes += 3;
err = HAPPlatformKeyValueStoreSet(
keyValueStore, kHAPKeyValueStoreDomain_CharacteristicConfiguration, key, bytes, numBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPBLECharacteristicDisableBroadcastNotifications(
const HAPCharacteristic* characteristic_,
const HAPService* service,
const HAPAccessory* accessory,
HAPPlatformKeyValueStoreRef keyValueStore) {
HAPPrecondition(characteristic_);
const HAPBaseCharacteristic* characteristic = characteristic_;
HAPPrecondition(characteristic->properties.ble.supportsBroadcastNotification);
HAPPrecondition(service);
HAPPrecondition(accessory);
HAPPrecondition(keyValueStore);
HAPError err;
HAPLogCharacteristicInfo(&logObject, characteristic, service, accessory, "Disabling broadcasts.");
HAPAssert(accessory->aid == 1);
uint16_t aid = (uint16_t) accessory->aid;
HAPAssert(characteristic->iid <= UINT16_MAX);
uint16_t cid = (uint16_t) characteristic->iid;
// Get configuration.
HAPPlatformKeyValueStoreKey key;
size_t numBytes;
uint8_t bytes[2 + 3 * 42 + 1]; // 128 + 1, allows for 42 concurrent broadcasts on a single KVS key.
bool found;
err = GetBroadcastConfiguration(aid, &found, bytes, sizeof bytes, &numBytes, &key, keyValueStore);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!found) {
return kHAPError_None;
}
HAPAssert(numBytes >= 2 && !((numBytes - 2) % 3));
HAPAssert(HAPReadLittleUInt16(bytes) == aid);
// Find characteristic.
size_t i;
for (i = 2; i < numBytes; i += 3) {
uint16_t itemCID = HAPReadLittleUInt16(&bytes[i]);
if (itemCID < cid) {
continue;
}
if (itemCID > cid) {
break;
}
// Found. Extract configuration.
uint8_t broadcastConfiguration = bytes[i + 2];
if (!HAPBLECharacteristicIsValidBroadcastInterval(broadcastConfiguration)) {
HAPLogCharacteristic(
&logObject,
characteristic,
service,
accessory,
"Invalid stored broadcast interval: 0x%02x.",
broadcastConfiguration);
return kHAPError_Unknown;
}
// Remove configuration.
numBytes -= 3;
HAPRawBufferCopyBytes(&bytes[i], &bytes[i + 3], numBytes - i);
if (numBytes == 2) {
err = HAPPlatformKeyValueStoreRemove(
keyValueStore, kHAPKeyValueStoreDomain_CharacteristicConfiguration, key);
} else {
err = HAPPlatformKeyValueStoreSet(
keyValueStore, kHAPKeyValueStoreDomain_CharacteristicConfiguration, key, bytes, numBytes);
}
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
return kHAPError_None;
}
return kHAPError_None;
}