-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPIPAccessoryServer.h
341 lines (271 loc) · 9.6 KB
/
HAPIPAccessoryServer.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
// 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_IP_ACCESSORY_SERVER_H
#define HAP_IP_ACCESSORY_SERVER_H
#ifdef __cplusplus
extern "C" {
#endif
#include "HAP+Internal.h"
#if __has_feature(nullability)
#pragma clang assume_nonnull begin
#endif
typedef struct {
void (*init)(HAPAccessoryServerRef* p_srv);
HAP_RESULT_USE_CHECK
HAPError (*deinit)(HAPAccessoryServerRef* p_srv);
HAP_RESULT_USE_CHECK
HAPAccessoryServerState (*get_state)(HAPAccessoryServerRef* p_srv);
void (*start)(HAPAccessoryServerRef* p_srv);
HAP_RESULT_USE_CHECK
HAPError (*stop)(HAPAccessoryServerRef* p_srv);
HAP_RESULT_USE_CHECK
HAPError (*raise_event)(
HAPAccessoryServerRef* server,
const HAPCharacteristic* characteristic,
const HAPService* service,
const HAPAccessory* accessory);
HAP_RESULT_USE_CHECK
HAPError (*raise_event_on_session)(
HAPAccessoryServerRef* server,
const HAPCharacteristic* characteristic,
const HAPService* service,
const HAPAccessory* accessory,
const HAPSessionRef* session);
} HAPAccessoryServerServerEngine;
extern const HAPAccessoryServerServerEngine HAPIPAccessoryServerServerEngine;
struct HAPIPAccessoryServerTransport {
void (*create)(HAPAccessoryServerRef* server, const HAPAccessoryServerOptions* options);
void (*prepareStart)(HAPAccessoryServerRef* server);
void (*willStart)(HAPAccessoryServerRef* server);
void (*prepareStop)(HAPAccessoryServerRef* server);
struct {
void (*invalidateDependentIPState)(HAPAccessoryServerRef* server_, HAPSessionRef* session);
} session;
struct {
void (*install)(void);
void (*uninstall)(void);
const HAPAccessoryServerServerEngine* _Nullable (*_Nonnull get)(void);
} serverEngine;
};
/**
* Session type.
*/
HAP_ENUM_BEGIN(uint8_t, HAPIPSecuritySessionType) { /** HAP session. */
kHAPIPSecuritySessionType_HAP = 1,
/** MFiSAP session. */
kHAPIPSecuritySessionType_MFiSAP
} HAP_ENUM_END(uint8_t, HAPIPSecuritySessionType);
/**
* Session.
*/
typedef struct {
/** Session type. */
HAPIPSecuritySessionType type;
/** Whether or not the session is open. */
bool isOpen : 1;
/** Whether or not a security session has been established. */
bool isSecured : 1;
/**
* Whether or not the /config message has been received.
*
* - This sends FIN after the next response, and restarts the IP server after receiving FIN from controller.
*/
bool receivedConfig : 1;
/** Session. */
union {
/** HAP session. */
HAPSessionRef hap;
/** MFi SAP session. */
struct {
/** AES master context. */
HAP_aes_ctr_ctx aesMasterContext;
/** Whether or not the /configured message has been received. */
bool receivedConfigured : 1;
} mfiSAP;
} _;
} HAPIPSecuritySession;
//----------------------------------------------------------------------------------------------------------------------
/**
* Accessory server session state.
*/
HAP_ENUM_BEGIN(uint8_t, HAPIPSessionState) { /** Accessory server session is idle. */
kHAPIPSessionState_Idle,
/** Accessory server session is reading. */
kHAPIPSessionState_Reading,
/** Accessory server session is writing. */
kHAPIPSessionState_Writing
} HAP_ENUM_END(uint8_t, HAPIPSessionState);
/**
* HTTP/1.1 Content Type.
*/
HAP_ENUM_BEGIN(uint8_t, HAPIPAccessoryServerContentType) { /** Unknown HTTP/1.1 content type. */
kHAPIPAccessoryServerContentType_Unknown,
/** application/hap+json. */
kHAPIPAccessoryServerContentType_Application_HAPJSON,
/** application/octet-stream. */
kHAPIPAccessoryServerContentType_Application_OctetStream,
/** application/pairing+tlv8. */
kHAPIPAccessoryServerContentType_Application_PairingTLV8
} HAP_ENUM_END(uint8_t, HAPIPAccessoryServerContentType);
/**
* IP specific event notification state.
*/
typedef struct {
/** Accessory instance ID. */
uint64_t aid;
/** Characteristic instance ID. */
uint64_t iid;
/** Flag indicating whether an event has been raised for the given characteristic in the given accessory. */
bool flag;
} HAPIPEventNotification;
HAP_STATIC_ASSERT(sizeof(HAPIPEventNotificationRef) >= sizeof(HAPIPEventNotification), event_notification);
/**
* IP specific accessory server session descriptor.
*/
typedef struct {
/** Accessory server serving this session. */
HAPAccessoryServerRef* _Nullable server;
/** TCP stream. */
HAPPlatformTCPStreamRef tcpStream;
/** Flag indicating whether the TCP stream is open. */
bool tcpStreamIsOpen;
/** IP session state. */
HAPIPSessionState state;
/** Time stamp of last activity on this session. */
HAPTime stamp;
/** Security session. */
HAPIPSecuritySession securitySession;
/** Inbound buffer. */
HAPIPByteBuffer inboundBuffer;
/** Marked inbound buffer position indicating the position until which the buffer has been decrypted. */
size_t inboundBufferMark;
/** Outbound buffer. */
HAPIPByteBuffer outboundBuffer;
/**
* Marked outbound buffer position indicating the position until which the buffer has not yet been encrypted
* (starting from outboundBuffer->limit).
*/
size_t outboundBufferMark;
/** HTTP reader. */
struct util_http_reader httpReader;
/** Current position of the HTTP reader in the inbound buffer. */
size_t httpReaderPosition;
/** Flag indication whether an error has been encountered while parsing a HTTP message. */
bool httpParserError;
/**
* HTTP/1.1 Method.
*/
struct {
/**
* Pointer to the HTTP/1.1 method in the inbound buffer.
*/
char* _Nullable bytes;
/**
* Length of the HTTP/1.1 method in the inbound buffer.
*/
size_t numBytes;
} httpMethod;
/**
* HTTP/1.1 URI.
*/
struct {
/**
* Pointer to the HTTP/1.1 URI in the inbound buffer.
*/
char* _Nullable bytes;
/**
* Length of the HTTP/1.1 URI in the inbound buffer.
*/
size_t numBytes;
} httpURI;
/**
* HTTP/1.1 Header Field Name.
*/
struct {
/**
* Pointer to the current HTTP/1.1 header field name in the inbound buffer.
*/
char* _Nullable bytes;
/**
* Length of the current HTTP/1.1 header field name in the inbound buffer.
*/
size_t numBytes;
} httpHeaderFieldName;
/**
* HTTP/1.1 Header Field Value.
*/
struct {
/**
* Pointer to the current HTTP/1.1 header value in the inbound buffer.
*/
char* _Nullable bytes;
/**
* Length of the current HTTP/1.1 header value in the inbound buffer.
*/
size_t numBytes;
} httpHeaderFieldValue;
/**
* HTTP/1.1 Content Length.
*/
struct {
/**
* Flag indicating whether a HTTP/1.1 content length is defined.
*/
bool isDefined;
/**
* HTTP/1.1 content length.
*/
size_t value;
} httpContentLength;
/**
* HTTP/1.1 Content Type.
*/
HAPIPAccessoryServerContentType httpContentType;
/**
* Array of event notification contexts on this session.
*/
HAPIPEventNotificationRef* _Nullable eventNotifications;
/**
* The maximum number of events this session can handle.
*/
size_t maxEventNotifications;
/**
* The number of subscribed events on this session.
*/
size_t numEventNotifications;
/**
* The number of raised events on this session.
*/
size_t numEventNotificationFlags;
/**
* Time stamp of last event notification on this session.
*/
HAPTime eventNotificationStamp;
/**
* Time when the request expires. 0 if no timed write in progress.
*/
HAPTime timedWriteExpirationTime;
/**
* PID of timed write. Must match "pid" of next PUT /characteristics.
*/
uint64_t timedWritePID;
/**
* Serialization context for incremental accessory attribute database serialization.
*/
HAPIPAccessorySerializationContext accessorySerializationContext;
/**
* Flag indicating whether incremental serialization of accessory attribute database is in progress.
*/
bool accessorySerializationIsInProgress;
} HAPIPSessionDescriptor;
HAP_STATIC_ASSERT(sizeof(HAPIPSessionDescriptorRef) >= sizeof(HAPIPSessionDescriptor), HAPIPSessionDescriptor);
#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
#ifdef __cplusplus
}
#endif
#endif