-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPAccessoryServer+Internal.h
579 lines (487 loc) · 19.1 KB
/
HAPAccessoryServer+Internal.h
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
// 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.
#ifndef HAP_ACCESSORY_SERVER_INTERNAL_H
#define HAP_ACCESSORY_SERVER_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include "HAP+Internal.h"
#if __has_feature(nullability)
#pragma clang assume_nonnull begin
#endif
/**
* IP specific accessory server state.
*/
HAP_ENUM_BEGIN(uint8_t, HAPIPAccessoryServerState) { /** Server state is undefined. */
kHAPIPAccessoryServerState_Undefined,
/** Server is initialized but not running. */
kHAPIPAccessoryServerState_Idle,
/** Server is running. */
kHAPIPAccessoryServerState_Running,
/** Server is shutting down. */
kHAPIPAccessoryServerState_Stopping
} HAP_ENUM_END(uint8_t, HAPIPAccessoryServerState);
typedef struct {
/** Transports. */
struct {
/** HAP over IP. */
const HAPIPAccessoryServerTransport* _Nullable ip;
/** HAP over Bluetooth LE. */
const HAPBLEAccessoryServerTransport* _Nullable ble;
} transports;
/** Accessory server state. */
HAPAccessoryServerState state;
/** Platform. */
HAPPlatform platform;
/** Callbacks. */
HAPAccessoryServerCallbacks callbacks;
/** Timer that on expiry triggers pending callbacks. */
HAPPlatformTimerRef callbackTimer;
/** Maximum number of allowed pairings. */
HAPPlatformKeyValueStoreKey maxPairings;
/** Accessory to serve. */
const HAPAccessory* _Nullable primaryAccessory;
/** Apple Authentication Coprocessor manager. */
HAPMFiHWAuth mfi;
/** Pairing identity. */
struct {
/** Long-term public key. */
uint8_t ed_LTPK[ED25519_PUBLIC_KEY_BYTES];
/** Long-term secret key. */
HAPAccessoryServerLongTermSecretKey ed_LTSK;
} identity;
/**
* Accessory setup state.
*/
struct {
/** Timer until the dynamic setup info needs to be refreshed. */
HAPPlatformTimerRef dynamicRefreshTimer;
/** Timer until NFC pairing mode expires. 0 if NFC pairing mode is not active. */
HAPPlatformTimerRef nfcPairingModeTimer;
/**
* Current setup info state.
*/
struct {
/**
* Setup info.
*/
HAPSetupInfo setupInfo;
/** Setup code (display / programmable NFC). */
HAPSetupCode setupCode;
/** Whether Setup info has been loaded / generated. */
bool setupInfoIsAvailable : 1;
/** Whether Setup code has been loaded / generated. */
bool setupCodeIsAvailable : 1;
/** Whether setup info should be kept on expiration of timers. */
bool lockSetupInfo : 1;
/** Whether setup info should be kept across pairing attempts. */
bool keepSetupInfo : 1;
} state;
} accessorySetup;
/**
* Pair Setup procedure state.
*/
struct {
/**
* Session where the current pairing takes place.
* NULL if no pairing in progress.
*/
HAPSessionRef* _Nullable sessionThatIsCurrentlyPairing;
/**
* Time at which the current pairing operation was started.
* Undefined if no pairing in progress.
*/
HAPTime operationStartTime;
uint8_t A[SRP_PUBLIC_KEY_BYTES]; // M2, M4
uint8_t b[SRP_SECRET_KEY_BYTES]; // M2, M4
uint8_t B[SRP_PUBLIC_KEY_BYTES]; // M2, M4
uint8_t K[SRP_SESSION_KEY_BYTES]; // SRP Session Key
uint8_t SessionKey[CHACHA20_POLY1305_KEY_BYTES]; // SessionKey for the Pair Setup procedure
uint8_t M1[SRP_PROOF_BYTES];
uint8_t M2[SRP_PROOF_BYTES];
/** Pairing Type flags. */
uint32_t flags;
bool flagsPresent : 1; /**< Whether Pairing Type flags were present in Pair Setup M1. */
bool keepSetupInfo : 1; /**< Whether setup info should be kept on disconnect. */
} pairSetup;
/**
* IP specific attributes.
*/
struct {
/** Storage. */
HAPIPAccessoryServerStorage* _Nullable storage;
/** NULL-terminated array of bridged accessories for a bridge accessory. NULL otherwise. */
const HAPAccessory* _Nullable const* _Nullable bridgedAccessories;
/** IP specific accessory server state. */
HAPIPAccessoryServerState state;
/** Next IP specific accessory server state after state transition is completed. */
HAPIPAccessoryServerState nextState;
/** Flag indicating whether the HAP service is currently discoverable. */
bool isServiceDiscoverable;
/** The number of active sessions served by the accessory server. */
size_t numSessions;
/**
* Characteristic write request context.
*/
struct {
/** The session over which the request has been received. */
const HAPIPSession* _Nullable ipSession;
/** The characteristic whose value is to be written. */
const HAPCharacteristic* _Nullable characteristic;
/** The service that contains the characteristic. */
const HAPService* _Nullable service;
/** The accessory that provides the service. */
const HAPAccessory* _Nullable accessory;
} characteristicWriteRequestContext;
/** Timer that on expiry triggers a server state transition. */
HAPPlatformTimerRef stateTransitionTimer;
/** Timer that on expiry schedules pending event notifications. */
HAPPlatformTimerRef eventNotificationTimer;
/** Timer that on expiry runs the garbage task. */
HAPPlatformTimerRef garbageCollectionTimer;
/** Timer that on expiry schedules a maximum idle time check. */
HAPPlatformTimerRef maxIdleTimeTimer;
/** Currently registered Bonjour service. */
HAPIPServiceDiscoveryType discoverableService;
} ip;
/**
* BLE specific attributes.
*/
struct {
/**
* Storage.
*
* - Implementation note: For now, we use only one procedure.
*/
HAPBLEAccessoryServerStorage* _Nullable storage;
/**
* Connection information.
*/
struct {
/**
* Information about the currently written characteristic.
*/
struct {
/** Characteristic being written. */
const HAPCharacteristic* _Nullable characteristic;
/** The service that contains the characteristic. */
const HAPCharacteristic* _Nullable service;
/** The accessory that provides the service. */
const HAPAccessory* _Nullable accessory;
} write;
/** Connection handle of the connected controller, if applicable. */
HAPPlatformBLEPeripheralManagerConnectionHandle connectionHandle;
/** Whether a HomeKit controller is connected. */
bool connected : 1;
/** Whether the HAP-BLE procedure is attached. */
bool procedureAttached : 1;
} connection;
/** Timestamp for Least Recently Used scheme in Pair Resume session cache. */
uint32_t sessionCacheTimestamp;
/**
* Advertisement state.
*/
struct {
/** Preferred regular advertising interval. */
HAPBLEAdvertisingInterval interval;
/** Preferred duration of events in ms. */
uint16_t ev_duration;
HAPPlatformTimerRef timer; /**< Timer until advertisement parameters change. */
HAPPlatformTimerRef fast_timer; /**< Timer until fast initial advertising completes. */
bool fast_started : 1; /**< Whether the fast advertising has been started. */
bool connected : 1; /**< Whether a controller is connected. */
/**
* Broadcasted Events.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 7.4.2.2.2 Manufacturer Data
*/
struct {
/** Broadcast interval. */
HAPBLECharacteristicBroadcastInterval interval;
/**
* Characteristic instance ID, if broadcasted event active. 0 otherwise.
*
* - For Bluetooth LE, instance IDs cannot exceed UINT16_MAX.
*/
uint16_t iid;
/** Value. */
uint8_t value[8];
} broadcastedEvent;
} adv;
} ble;
/** Client context pointer. */
void* _Nullable context;
} HAPAccessoryServer;
HAP_STATIC_ASSERT(sizeof(HAPAccessoryServerRef) >= sizeof(HAPAccessoryServer), HAPAccessoryServer);
/**
* Gets client context for the accessory server.
*
* @param server Accessory server.
*
* @return Client context passed to HAPAccessoryServerCreate.
*/
HAP_RESULT_USE_CHECK
void* _Nullable HAPAccessoryServerGetClientContext(HAPAccessoryServerRef* server);
/**
* Schedules invocation of the accessory server's handleUpdatedState callback.
*
* @param server Accessory server.
*/
void HAPAccessoryServerDelegateScheduleHandleUpdatedState(HAPAccessoryServerRef* server);
/**
* Loads the accessory server LTSK. If none exists, it is generated.
*
* @param keyValueStore Key-value store.
* @param[out] ltsk Long-term secret key of the accessory server.
*/
void HAPAccessoryServerLoadLTSK(HAPPlatformKeyValueStoreRef keyValueStore, HAPAccessoryServerLongTermSecretKey* ltsk);
/**
* IP protocol version string.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 6.6.3 IP Protocol Version
*/
#define kHAPProtocolVersion_IP "1.1.0"
/**
* IP protocol version string (short).
*
* @see HomeKit Accessory Protocol Specification R14
* Section 6.6.3 IP Protocol Version
*/
#define kHAPShortProtocolVersion_IP "1.1"
/**
* BLE protocol version string.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 7.4.3.1 BLE Protocol Version Characteristic
*/
#define kHAPProtocolVersion_BLE "2.2.0"
/**
* Returns whether the accessory supports Apple Authentication Coprocessor.
*
* @param server Accessory server.
*
* @return true If the accessory supports Apple Authentication Coprocessor.
* @return false Otherwise.
*/
HAP_RESULT_USE_CHECK
bool HAPAccessoryServerSupportsMFiHWAuth(HAPAccessoryServerRef* server);
/**
* Returns the Pairing Feature flags.
*
* @param server Accessory server.
*
* @return Pairing feature flags
*/
HAP_RESULT_USE_CHECK
uint8_t HAPAccessoryServerGetPairingFeatureFlags(HAPAccessoryServerRef* server);
/**
* Returns the Status flags.
*
* @param server Accessory server.
*
* @return Status flags
*/
HAP_RESULT_USE_CHECK
uint8_t HAPAccessoryServerGetStatusFlags(HAPAccessoryServerRef* server);
/**
* Updates advertising data.
*
* @param server Accessory server.
*/
void HAPAccessoryServerUpdateAdvertisingData(HAPAccessoryServerRef* server);
/**
* If the last remaining admin controller pairing is removed, all pairings on the accessory must be removed.
*
* This must be called when:
* - the accessory server is started (to handle potential power failure during key-value store operations).
* - a pairing is removed.
*
* @param server Accessory server.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If persistent store access failed.
*
* @see HomeKit Accessory Protocol Specification R14
* Section 5.11 Remove Pairing
*/
HAP_RESULT_USE_CHECK
HAPError HAPAccessoryServerCleanupPairings(HAPAccessoryServerRef* server);
/**
* Get configuration number.
*
* @param keyValueStore Key-value store.
* @param[out] cn Configuration number.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If persistent store access failed.
*/
HAP_RESULT_USE_CHECK
HAPError HAPAccessoryServerGetCN(HAPPlatformKeyValueStoreRef keyValueStore, uint16_t* cn);
/**
* Increments configuration number.
*
* - IP: Must be called when an accessory, service or characteristic is added or removed from the accessory server.
*
* @param keyValueStore Key-value store.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If persistent store access failed.
*/
HAP_RESULT_USE_CHECK
HAPError HAPAccessoryServerIncrementCN(HAPPlatformKeyValueStoreRef keyValueStore);
/**
* Resets HomeKit state after a firmware update has occurred.
*
* - Prior to calling, make sure that the accessory server is not running.
*
* @param server Accessory server.
*
* @return kHAPError_None If successful.
* @return kHAPError_Unknown If persistent store access failed.
*/
HAP_RESULT_USE_CHECK
HAPError HAPHandleFirmwareUpdate(HAPAccessoryServerRef* server);
/**
* Informs the application that a controller subscribed to updates of characteristic value.
*
* @param server Accessory server.
* @param session The session over which the subscription state was changed.
* @param characteristic The characteristic whose value has changed.
* @param service The service that contains the characteristic.
* @param accessory The accessory that provides the service.
*/
void HAPAccessoryServerHandleSubscribe(
HAPAccessoryServerRef* server,
HAPSessionRef* session,
const HAPCharacteristic* characteristic,
const HAPService* service,
const HAPAccessory* accessory);
/**
* Informs the application that a controller unsubscribed from updates of characteristic value.
*
* @param server Accessory server.
* @param session The session over which the subscription state was changed.
* @param characteristic The characteristic whose value has changed.
* @param service The service that contains the characteristic.
* @param accessory The accessory that provides the service.
*/
void HAPAccessoryServerHandleUnsubscribe(
HAPAccessoryServerRef* server,
HAPSessionRef* session,
const HAPCharacteristic* characteristic,
const HAPService* service,
const HAPAccessory* accessory);
/**
* Returns whether a service is supported in the context of a given accessory server and over a
* given transport type.
*
* - Certain services are only applicable to certain types of accessory server configurations or
* certain types of transports.
*
* @param server Accessory server.
* @param transportType Transport type.
* @param service Service.
*
* @return true If the service is supported.
* @return false Otherwise.
*/
HAP_RESULT_USE_CHECK
bool HAPAccessoryServerSupportsService(
HAPAccessoryServerRef* server,
HAPTransportType transportType,
const HAPService* service);
/**
* Index of a service with a given type within an attribute database.
*
* - This is a zero-based index specific to the service type.
*
* - This is useful to compress a (HAPAccessory, HAPService) tuple.
* Use HAPAccessoryServerGetServiceFromServiceTypeIndex to fetch the (HAPAccessory, HAPService) tuple.
* There can be at most 150 accessories with 100 services each. 150 * 100 = 15000 -> UInt16.
*/
typedef uint16_t HAPServiceTypeIndex;
/**
* Gets the number of service instances with a given type within an attribute database.
*
* @param server Accessory server.
* @param serviceType The type of the service.
*
* @return Number of services with a given type within the accessory server's attribute database.
*/
HAP_RESULT_USE_CHECK
size_t HAPAccessoryServerGetNumServiceInstances(HAPAccessoryServerRef* server, const HAPUUID* serviceType);
/**
* Gets the index of a service for later lookup.
*
* @param server Accessory server.
* @param service Service.
* @param accessory The accessory that provides the service.
*
* @return Index of service within the accessory server's attribute database.
*/
HAP_RESULT_USE_CHECK
HAPServiceTypeIndex HAPAccessoryServerGetServiceTypeIndex(
HAPAccessoryServerRef* server,
const HAPService* service,
const HAPAccessory* accessory);
/**
* Gets a service by type and index.
*
* @param server Accessory server.
* @param serviceType The type of the service.
* @param serviceTypeIndex Index of the service as received through HAPAccessoryServerGetServiceTypeIndex.
* @param[out] service Service.
* @param[out] accessory The accessory that provides the service.
*/
void HAPAccessoryServerGetServiceFromServiceTypeIndex(
HAPAccessoryServerRef* server,
const HAPUUID* serviceType,
HAPServiceTypeIndex serviceTypeIndex,
const HAPService* _Nonnull* _Nonnull service,
const HAPAccessory* _Nonnull* _Nonnull accessory);
/**
* Callback that should be invoked for each HAP session.
*
* @param context Context.
* @param server Accessory server.
* @param session Session.
* @param[in,out] shouldContinue True if enumeration shall continue, False otherwise. Is set to true on input.
*/
typedef void (*HAPAccessoryServerEnumerateSessionsCallback)(
void* _Nullable context,
HAPAccessoryServerRef* server,
HAPSessionRef* session,
bool* shouldContinue);
/**
* Enumerates all connected HAP sessions associated with an accessory server.
*
* @param server Accessory server.
* @param callback Function to call on each configured HAP session.
* @param context Context that is passed to the callback.
*/
void HAPAccessoryServerEnumerateConnectedSessions(
HAPAccessoryServerRef* server,
HAPAccessoryServerEnumerateSessionsCallback callback,
void* _Nullable context);
/**
* Searches for an IP session corresponding to a given HAP session and returns the session index.
*
* @param server Accessory server.
* @param session The session to search for.
*
* @return The index of the IP session.
*/
HAP_RESULT_USE_CHECK
size_t HAPAccessoryServerGetIPSessionIndex(const HAPAccessoryServerRef* server, const HAPSessionRef* session);
#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
#ifdef __cplusplus
}
#endif
#endif