-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPBLEAccessoryServer.c
221 lines (193 loc) · 9.75 KB
/
HAPBLEAccessoryServer.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
// 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 = "BLEAccessoryServer" };
static void Create(HAPAccessoryServerRef* server_, const HAPAccessoryServerOptions* options) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(options);
HAPPrecondition(server->platform.ble.blePeripheralManager);
// Initialize BLE storage.
HAPPrecondition(options->ble.accessoryServerStorage);
HAPBLEAccessoryServerStorage* storage = options->ble.accessoryServerStorage;
HAPPrecondition(storage->gattTableElements);
HAPPrecondition(storage->sessionCacheElements);
HAPPrecondition(storage->numSessionCacheElements >= kHAPBLESessionCache_MinElements);
HAPPrecondition(storage->session);
HAPPrecondition(storage->procedures);
HAPPrecondition(storage->numProcedures >= 1);
HAPPrecondition(storage->procedureBuffer.bytes);
HAPPrecondition(storage->procedureBuffer.numBytes >= 1);
HAPRawBufferZero(storage->gattTableElements, storage->numGATTTableElements * sizeof *storage->gattTableElements);
HAPRawBufferZero(
storage->sessionCacheElements, storage->numSessionCacheElements * sizeof *storage->sessionCacheElements);
HAPRawBufferZero(storage->session, sizeof *storage->session);
HAPRawBufferZero(storage->procedures, storage->numProcedures * sizeof *storage->procedures);
HAPRawBufferZero(storage->procedureBuffer.bytes, storage->procedureBuffer.numBytes);
server->ble.storage = storage;
// Copy advertising configuration.
HAPPrecondition(options->ble.preferredAdvertisingInterval >= kHAPBLEAdvertisingInterval_Minimum);
HAPPrecondition(options->ble.preferredAdvertisingInterval <= kHAPBLEAdvertisingInterval_Maximum);
HAPPrecondition(options->ble.preferredNotificationDuration >= kHAPBLENotification_MinDuration);
server->ble.adv.interval = options->ble.preferredAdvertisingInterval;
server->ble.adv.ev_duration = options->ble.preferredNotificationDuration;
}
static void ValidateAccessory(const HAPAccessory* accessory) {
HAPPrecondition(accessory);
if (accessory->services) {
for (size_t i = 0; accessory->services[i]; i++) {
const HAPService* service = accessory->services[i];
HAPPrecondition(service->iid <= UINT16_MAX);
if (service->characteristics) {
for (size_t j = 0; service->characteristics[j]; j++) {
const HAPBaseCharacteristic* characteristic = service->characteristics[j];
HAPPrecondition(characteristic->iid <= UINT16_MAX);
}
}
}
}
}
static void PrepareStart(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPBLEAccessoryServerStorage* storage = HAPNonnull(server->ble.storage);
HAPRawBufferZero(storage->gattTableElements, storage->numGATTTableElements * sizeof *storage->gattTableElements);
HAPRawBufferZero(
storage->sessionCacheElements, storage->numSessionCacheElements * sizeof *storage->sessionCacheElements);
HAPRawBufferZero(storage->session, sizeof *storage->session);
HAPRawBufferZero(storage->procedures, storage->numProcedures * sizeof *storage->procedures);
HAPRawBufferZero(storage->procedureBuffer.bytes, storage->procedureBuffer.numBytes);
}
static void Start(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPError err;
HAPAssert(server->platform.ble.blePeripheralManager);
HAPPlatformBLEPeripheralManager* blePeripheralManager = server->platform.ble.blePeripheralManager;
// Set BD_ADDR.
HAPMACAddress deviceAddress;
err = HAPMACAddressGetRandomStaticBLEDeviceAddress(
server_,
/* bleInterface: */ NULL,
&deviceAddress);
if (err) {
HAPAssert(err == kHAPError_Unknown);
HAPFatalError();
}
HAPAssert(sizeof deviceAddress == sizeof(HAPPlatformBLEPeripheralManagerDeviceAddress));
HAPLogBufferInfo(&logObject, deviceAddress.bytes, sizeof deviceAddress.bytes, "BD_ADDR");
HAPPlatformBLEPeripheralManagerDeviceAddress bdAddr;
HAPRawBufferZero(&bdAddr, sizeof bdAddr);
bdAddr.bytes[0] = deviceAddress.bytes[5];
bdAddr.bytes[1] = deviceAddress.bytes[4];
bdAddr.bytes[2] = deviceAddress.bytes[3];
bdAddr.bytes[3] = deviceAddress.bytes[2];
bdAddr.bytes[4] = deviceAddress.bytes[1];
bdAddr.bytes[5] = deviceAddress.bytes[0];
HAPPlatformBLEPeripheralManagerSetDeviceAddress(blePeripheralManager, &bdAddr);
// Set GAP device name.
const HAPAccessory* primaryAccessory = HAPNonnull(server->primaryAccessory);
HAPAssert(primaryAccessory->name);
HAPAssert(HAPStringGetNumBytes(primaryAccessory->name) <= 64);
HAPPlatformBLEPeripheralManagerSetDeviceName(blePeripheralManager, primaryAccessory->name);
// Register GATT db.
HAPBLEPeripheralManagerRegister(server_);
}
static void TryStop(HAPAccessoryServerRef* server_, bool* didStop) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPPrecondition(server->platform.ble.blePeripheralManager);
HAPPlatformBLEPeripheralManagerRef blePeripheralManager = server->platform.ble.blePeripheralManager;
HAPPrecondition(didStop);
*didStop = false;
// Close all connections.
if (server->ble.connection.connected) {
HAPSession* session = (HAPSession*) server->ble.storage->session;
if (HAPBLESessionIsSafeToDisconnect(&session->_.ble)) {
HAPLogInfo(&logObject, "Disconnecting BLE connection - Server is shutting down.");
HAPPlatformBLEPeripheralManagerCancelCentralConnection(
blePeripheralManager, server->ble.connection.connectionHandle);
} else {
HAPLogInfo(&logObject, "Waiting for pending BLE data to be written.");
}
HAPLogInfo(&logObject, "Delaying shutdown. Waiting for BLE connection to terminate.");
return;
}
// Stop listening.
HAPPlatformBLEPeripheralManagerRemoveAllServices(blePeripheralManager);
HAPPlatformBLEPeripheralManagerSetDelegate(blePeripheralManager, NULL);
*didStop = true;
}
static void UpdateAdvertisingData(HAPAccessoryServerRef* server_) {
HAPPrecondition(server_);
HAPAccessoryServer* server = (HAPAccessoryServer*) server_;
HAPError err;
HAPAssert(server->platform.ble.blePeripheralManager);
HAPPlatformBLEPeripheralManagerRef blePeripheralManager = server->platform.ble.blePeripheralManager;
if (server->state == kHAPAccessoryServerState_Running) {
// Fetch advertisement parameters.
bool isActive;
uint16_t advertisingInterval;
uint8_t advertisingBytes[/* Maximum Bluetooth 4 limit: */ 31];
size_t numAdvertisingBytes;
uint8_t scanResponseBytes[/* Maximum Bluetooth 4 limit: */ 31];
size_t numScanResponseBytes;
err = HAPBLEAccessoryServerGetAdvertisingParameters(
server_,
&isActive,
&advertisingInterval,
advertisingBytes,
sizeof advertisingBytes,
&numAdvertisingBytes,
scanResponseBytes,
sizeof scanResponseBytes,
&numScanResponseBytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
HAPFatalError();
}
// Update advertisement.
if (isActive) {
HAPAssert(advertisingInterval);
HAPPlatformBLEPeripheralManagerStartAdvertising(
blePeripheralManager,
advertisingInterval,
advertisingBytes,
numAdvertisingBytes,
numScanResponseBytes ? scanResponseBytes : NULL,
numScanResponseBytes);
// Mark advertisement started.
HAPBLEAccessoryServerDidStartAdvertising(server_);
} else {
HAPPlatformBLEPeripheralManagerStopAdvertising(blePeripheralManager);
}
} else {
HAPLogInfo(&logObject, "Stopping advertisement - Server is shutting down.");
HAPPlatformBLEPeripheralManagerStopAdvertising(blePeripheralManager);
}
}
const HAPBLEAccessoryServerTransport kHAPAccessoryServerTransport_BLE = {
.create = Create,
.validateAccessory = ValidateAccessory,
.prepareStart = PrepareStart,
.start = Start,
.tryStop = TryStop,
.didRaiseEvent = HAPBLEAccessoryServerDidRaiseEvent,
.updateAdvertisingData = UpdateAdvertisingData,
.getGSN = HAPBLEAccessoryServerGetGSN,
.broadcast = { .expireKey = HAPBLEAccessoryServerBroadcastExpireKey },
.peripheralManager = { .release = HAPBLEPeripheralManagerRelease,
.handleSessionAccept = HAPBLEPeripheralManagerHandleSessionAccept,
.handleSessionInvalidate = HAPBLEPeripheralManagerHandleSessionInvalidate },
.sessionCache = { .fetch = HAPPairingBLESessionCacheFetch,
.save = HAPPairingBLESessionCacheSave,
.invalidateEntriesForPairing = HAPPairingBLESessionCacheInvalidateEntriesForPairing },
.session = { .create = HAPBLESessionCreate,
.release = HAPBLESessionRelease,
.invalidate = HAPBLESessionInvalidate,
.didStartPairingProcedure = HAPBLESessionDidStartPairingProcedure,
.didCompletePairingProcedure = HAPBLESessionDidCompletePairingProcedure }
};