@geastack/host 0.1.27 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/audio_runtime.cpp +131 -29
- package/bluetooth.cpp +22 -0
- package/host/audio.cpp +132 -4
- package/host/ble.cpp +44 -0
- package/host/bluetooth_central_apple.mm +412 -0
- package/host/crypto.cpp +318 -0
- package/host/crypto_apple.mm +69 -0
- package/host/fetch.cpp +47 -2
- package/host/files.cpp +194 -0
- package/host/files_apple.mm +25 -0
- package/host/image.cpp +17 -6
- package/host/media.cpp +8 -7
- package/host/menu.cpp +49 -0
- package/host/menu_apple.mm +249 -0
- package/host/menu_bar.cpp +31 -0
- package/host/menu_bar_apple.mm +220 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- package/host/tile_loader.cpp +5 -3
- package/host/websocket.cpp +29 -2
- package/host/websocket_apple.mm +30 -9
- package/host/websocket_curl.cpp +15 -4
- package/include/gea/embedded-host.h +5 -0
- package/include/host/audio.h +12 -0
- package/include/host/backends.h +25 -0
- package/include/host/crypto.h +46 -0
- package/include/host/files.h +42 -0
- package/include/host/menu.h +93 -0
- package/include/host/menu_bar.h +48 -0
- package/include/host/navigator.h +67 -1
- package/include/host/opus.h +35 -0
- package/include/host/websocket.h +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// CoreBluetooth backend for the generic GATT central (navigator.bluetooth.central,
|
|
3
|
+
// BluetoothCentralDriver in ble.h) on macOS and iOS. Everything runs on the main
|
|
4
|
+
// queue, where the Gea frame loop polls it: CoreBluetooth's delegate callbacks
|
|
5
|
+
// fill the scan list, the link state and the per-characteristic notification
|
|
6
|
+
// queues, and the app drains them each frame.
|
|
7
|
+
//
|
|
8
|
+
// The CBCentralManager is created on first use, so only apps that touch the
|
|
9
|
+
// central ask for Bluetooth access. Such apps need NSBluetoothAlwaysUsageDescription
|
|
10
|
+
// in their Info.plist (macos.json `infoPlist`), or the system terminates them.
|
|
11
|
+
#if defined(__APPLE__)
|
|
12
|
+
|
|
13
|
+
#import <CoreBluetooth/CoreBluetooth.h>
|
|
14
|
+
#import <Foundation/Foundation.h>
|
|
15
|
+
|
|
16
|
+
#include <map>
|
|
17
|
+
#include <set>
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <vector>
|
|
20
|
+
|
|
21
|
+
#include "ble.h"
|
|
22
|
+
|
|
23
|
+
__asm__(".linker_option \"-framework\", \"CoreBluetooth\"");
|
|
24
|
+
|
|
25
|
+
namespace {
|
|
26
|
+
|
|
27
|
+
struct ScanEntry {
|
|
28
|
+
std::string id;
|
|
29
|
+
std::string name;
|
|
30
|
+
std::string services;
|
|
31
|
+
int rssi = 0;
|
|
32
|
+
std::vector<std::uint8_t> manufacturer;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
std::string utf8(NSString *text) { return text ? std::string(text.UTF8String ?: "") : std::string(); }
|
|
36
|
+
|
|
37
|
+
NSString *nsString(const char *text) { return [NSString stringWithUTF8String:text ? text : ""] ?: @""; }
|
|
38
|
+
|
|
39
|
+
// "SERVICE/CHARACTERISTIC" in CoreBluetooth's canonical spelling, so "180f",
|
|
40
|
+
// "180F" and the full 128-bit form of a 16-bit UUID all name the same slot.
|
|
41
|
+
std::string slotKey(CBUUID *service, CBUUID *characteristic) {
|
|
42
|
+
return utf8(service.UUIDString) + "/" + utf8(characteristic.UUIDString);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
CBUUID *uuidOf(const char *text) {
|
|
46
|
+
NSString *value = [nsString(text) stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
|
|
47
|
+
if (value.length == 0) return nil;
|
|
48
|
+
@try {
|
|
49
|
+
return [CBUUID UUIDWithString:value];
|
|
50
|
+
} @catch (NSException *) {
|
|
51
|
+
return nil;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
NSArray<CBUUID *> *uuidsOf(const char *csv) {
|
|
56
|
+
NSMutableArray<CBUUID *> *uuids = [NSMutableArray array];
|
|
57
|
+
for (NSString *part in [nsString(csv) componentsSeparatedByString:@","]) {
|
|
58
|
+
CBUUID *uuid = uuidOf(part.UTF8String);
|
|
59
|
+
if (uuid) [uuids addObject:uuid];
|
|
60
|
+
}
|
|
61
|
+
return uuids;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
} // namespace
|
|
65
|
+
|
|
66
|
+
@interface GeaBluetoothCentral : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>
|
|
67
|
+
@end
|
|
68
|
+
|
|
69
|
+
@implementation GeaBluetoothCentral {
|
|
70
|
+
@public
|
|
71
|
+
CBCentralManager *_manager;
|
|
72
|
+
NSMutableDictionary<NSString *, CBPeripheral *> *_peripherals;
|
|
73
|
+
std::vector<ScanEntry> _scan;
|
|
74
|
+
std::map<std::string, std::size_t> _scanIndex;
|
|
75
|
+
NSArray<CBUUID *> *_scanServices;
|
|
76
|
+
bool _wantsScan;
|
|
77
|
+
|
|
78
|
+
CBPeripheral *_peripheral;
|
|
79
|
+
NSArray<CBUUID *> *_linkServices;
|
|
80
|
+
std::size_t _pendingServices;
|
|
81
|
+
std::string _link;
|
|
82
|
+
std::string _linkError;
|
|
83
|
+
std::map<std::string, CBCharacteristic *> _characteristics;
|
|
84
|
+
std::map<std::string, std::vector<std::uint8_t>> _received;
|
|
85
|
+
std::set<std::string> _subscribed;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
- (instancetype)init {
|
|
89
|
+
if ((self = [super init])) {
|
|
90
|
+
_peripherals = [NSMutableDictionary dictionary];
|
|
91
|
+
_wantsScan = false;
|
|
92
|
+
_pendingServices = 0;
|
|
93
|
+
_link = "idle";
|
|
94
|
+
_manager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
|
|
95
|
+
}
|
|
96
|
+
return self;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
- (const char *)state {
|
|
100
|
+
switch (_manager.state) {
|
|
101
|
+
case CBManagerStatePoweredOn: return "on";
|
|
102
|
+
case CBManagerStatePoweredOff: return "off";
|
|
103
|
+
case CBManagerStateUnauthorized: return "unauthorized";
|
|
104
|
+
case CBManagerStateUnsupported: return "unsupported";
|
|
105
|
+
case CBManagerStateResetting: return "resetting";
|
|
106
|
+
default: return "unknown";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
- (void)resumeScan {
|
|
111
|
+
if (!_wantsScan || _manager.state != CBManagerStatePoweredOn) return;
|
|
112
|
+
[_manager scanForPeripheralsWithServices:_scanServices.count ? _scanServices : nil
|
|
113
|
+
options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
- (void)startScan:(const char *)servicesCsv {
|
|
117
|
+
_scanServices = uuidsOf(servicesCsv);
|
|
118
|
+
_wantsScan = true;
|
|
119
|
+
[self resumeScan];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
- (void)stopScan {
|
|
123
|
+
_wantsScan = false;
|
|
124
|
+
if (_manager.state == CBManagerStatePoweredOn) [_manager stopScan];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
- (void)clearScan {
|
|
128
|
+
_scan.clear();
|
|
129
|
+
_scanIndex.clear();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
|
|
133
|
+
if (central.state == CBManagerStatePoweredOn) {
|
|
134
|
+
[self resumeScan];
|
|
135
|
+
} else if (_peripheral) {
|
|
136
|
+
[self closed:@"bluetooth is off"];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
- (void)centralManager:(CBCentralManager *)central
|
|
141
|
+
didDiscoverPeripheral:(CBPeripheral *)peripheral
|
|
142
|
+
advertisementData:(NSDictionary<NSString *, id> *)data
|
|
143
|
+
RSSI:(NSNumber *)rssi {
|
|
144
|
+
NSString *identifier = peripheral.identifier.UUIDString;
|
|
145
|
+
_peripherals[identifier] = peripheral;
|
|
146
|
+
const std::string id = utf8(identifier);
|
|
147
|
+
auto found = _scanIndex.find(id);
|
|
148
|
+
if (found == _scanIndex.end()) {
|
|
149
|
+
if (_scan.size() >= 256) return;
|
|
150
|
+
found = _scanIndex.emplace(id, _scan.size()).first;
|
|
151
|
+
_scan.push_back(ScanEntry{id});
|
|
152
|
+
}
|
|
153
|
+
ScanEntry &entry = _scan[found->second];
|
|
154
|
+
NSString *name = data[CBAdvertisementDataLocalNameKey] ?: peripheral.name;
|
|
155
|
+
if (name.length) entry.name = utf8(name);
|
|
156
|
+
if (rssi && rssi.intValue != 127) entry.rssi = rssi.intValue;
|
|
157
|
+
NSArray<CBUUID *> *services = data[CBAdvertisementDataServiceUUIDsKey];
|
|
158
|
+
if (services.count) {
|
|
159
|
+
std::string joined;
|
|
160
|
+
for (CBUUID *service in services) {
|
|
161
|
+
if (!joined.empty()) joined += ",";
|
|
162
|
+
joined += utf8(service.UUIDString);
|
|
163
|
+
}
|
|
164
|
+
entry.services = joined;
|
|
165
|
+
}
|
|
166
|
+
NSData *manufacturer = data[CBAdvertisementDataManufacturerDataKey];
|
|
167
|
+
if (manufacturer.length) {
|
|
168
|
+
const auto *bytes = static_cast<const std::uint8_t *>(manufacturer.bytes);
|
|
169
|
+
entry.manufacturer.assign(bytes, bytes + manufacturer.length);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
- (void)connect:(const char *)identifier services:(const char *)servicesCsv {
|
|
174
|
+
if (_peripheral) {
|
|
175
|
+
_peripheral.delegate = nil;
|
|
176
|
+
[_manager cancelPeripheralConnection:_peripheral];
|
|
177
|
+
_peripheral = nil;
|
|
178
|
+
}
|
|
179
|
+
_characteristics.clear();
|
|
180
|
+
_received.clear();
|
|
181
|
+
_subscribed.clear();
|
|
182
|
+
_linkError.clear();
|
|
183
|
+
|
|
184
|
+
NSString *key = nsString(identifier);
|
|
185
|
+
CBPeripheral *peripheral = _peripherals[key];
|
|
186
|
+
if (!peripheral) {
|
|
187
|
+
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:key];
|
|
188
|
+
peripheral = uuid ? [_manager retrievePeripheralsWithIdentifiers:@[ uuid ]].firstObject : nil;
|
|
189
|
+
}
|
|
190
|
+
if (!peripheral) {
|
|
191
|
+
_link = "closed";
|
|
192
|
+
_linkError = "unknown peripheral";
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
_peripheral = peripheral;
|
|
196
|
+
_linkServices = uuidsOf(servicesCsv);
|
|
197
|
+
_link = "connecting";
|
|
198
|
+
peripheral.delegate = self;
|
|
199
|
+
[_manager connectPeripheral:peripheral options:nil];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
- (void)closed:(NSString *)reason {
|
|
203
|
+
_peripheral.delegate = nil;
|
|
204
|
+
_peripheral = nil;
|
|
205
|
+
_characteristics.clear();
|
|
206
|
+
_subscribed.clear();
|
|
207
|
+
_link = "closed";
|
|
208
|
+
_linkError = utf8(reason ?: @"disconnected");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
- (void)disconnect {
|
|
212
|
+
if (_peripheral) [_manager cancelPeripheralConnection:_peripheral];
|
|
213
|
+
if (_peripheral || _link != "idle") [self closed:@"disconnected"];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
|
|
217
|
+
if (peripheral != _peripheral) return;
|
|
218
|
+
_link = "discovering";
|
|
219
|
+
[peripheral discoverServices:_linkServices.count ? _linkServices : nil];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
- (void)centralManager:(CBCentralManager *)central
|
|
223
|
+
didFailToConnectPeripheral:(CBPeripheral *)peripheral
|
|
224
|
+
error:(NSError *)error {
|
|
225
|
+
if (peripheral == _peripheral) [self closed:error.localizedDescription ?: @"connection failed"];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
- (void)centralManager:(CBCentralManager *)central
|
|
229
|
+
didDisconnectPeripheral:(CBPeripheral *)peripheral
|
|
230
|
+
error:(NSError *)error {
|
|
231
|
+
if (peripheral == _peripheral) [self closed:error.localizedDescription ?: @"disconnected"];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
|
|
235
|
+
if (peripheral != _peripheral) return;
|
|
236
|
+
if (error) {
|
|
237
|
+
[_manager cancelPeripheralConnection:peripheral];
|
|
238
|
+
[self closed:error.localizedDescription];
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
for (CBUUID *wanted in _linkServices) {
|
|
242
|
+
BOOL present = NO;
|
|
243
|
+
for (CBService *service in peripheral.services) present = present || [service.UUID isEqual:wanted];
|
|
244
|
+
if (!present) {
|
|
245
|
+
[_manager cancelPeripheralConnection:peripheral];
|
|
246
|
+
[self closed:[@"service not found: " stringByAppendingString:wanted.UUIDString]];
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
_pendingServices = peripheral.services.count;
|
|
251
|
+
if (_pendingServices == 0) {
|
|
252
|
+
_link = "ready";
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
for (CBService *service in peripheral.services) [peripheral discoverCharacteristics:nil forService:service];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
- (void)peripheral:(CBPeripheral *)peripheral
|
|
259
|
+
didDiscoverCharacteristicsForService:(CBService *)service
|
|
260
|
+
error:(NSError *)error {
|
|
261
|
+
if (peripheral != _peripheral) return;
|
|
262
|
+
for (CBCharacteristic *characteristic in service.characteristics) {
|
|
263
|
+
_characteristics[slotKey(service.UUID, characteristic.UUID)] = characteristic;
|
|
264
|
+
}
|
|
265
|
+
if (_pendingServices > 0 && --_pendingServices == 0) _link = "ready";
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
- (CBCharacteristic *)characteristic:(const char *)service of:(const char *)characteristic {
|
|
269
|
+
CBUUID *serviceID = uuidOf(service);
|
|
270
|
+
CBUUID *characteristicID = uuidOf(characteristic);
|
|
271
|
+
if (!serviceID || !characteristicID) return nil;
|
|
272
|
+
auto found = _characteristics.find(slotKey(serviceID, characteristicID));
|
|
273
|
+
return found == _characteristics.end() ? nil : found->second;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
- (void)subscribe:(const char *)service characteristic:(const char *)characteristic {
|
|
277
|
+
CBCharacteristic *target = [self characteristic:service of:characteristic];
|
|
278
|
+
if (target && _peripheral) [_peripheral setNotifyValue:YES forCharacteristic:target];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
- (bool)subscribed:(const char *)service characteristic:(const char *)characteristic {
|
|
282
|
+
CBCharacteristic *target = [self characteristic:service of:characteristic];
|
|
283
|
+
return target && _subscribed.count(slotKey(target.service.UUID, target.UUID)) > 0;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
- (void)peripheral:(CBPeripheral *)peripheral
|
|
287
|
+
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
|
|
288
|
+
error:(NSError *)error {
|
|
289
|
+
if (peripheral != _peripheral) return;
|
|
290
|
+
const std::string key = slotKey(characteristic.service.UUID, characteristic.UUID);
|
|
291
|
+
if (error || !characteristic.isNotifying) {
|
|
292
|
+
_subscribed.erase(key);
|
|
293
|
+
if (error) _linkError = utf8(error.localizedDescription);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
_subscribed.insert(key);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
- (void)peripheral:(CBPeripheral *)peripheral
|
|
300
|
+
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
|
|
301
|
+
error:(NSError *)error {
|
|
302
|
+
if (peripheral != _peripheral || error || !characteristic.value.length) return;
|
|
303
|
+
auto &queue = _received[slotKey(characteristic.service.UUID, characteristic.UUID)];
|
|
304
|
+
const auto *bytes = static_cast<const std::uint8_t *>(characteristic.value.bytes);
|
|
305
|
+
// Bounded: an app that never drains a chatty characteristic loses the oldest bytes.
|
|
306
|
+
constexpr std::size_t limit = 4 * 1024 * 1024;
|
|
307
|
+
queue.insert(queue.end(), bytes, bytes + characteristic.value.length);
|
|
308
|
+
if (queue.size() > limit) queue.erase(queue.begin(), queue.begin() + (queue.size() - limit));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
- (bool)write:(const char *)service
|
|
312
|
+
characteristic:(const char *)characteristic
|
|
313
|
+
bytes:(const std::uint8_t *)bytes
|
|
314
|
+
length:(int)length {
|
|
315
|
+
CBCharacteristic *target = [self characteristic:service of:characteristic];
|
|
316
|
+
if (!target || _link != "ready" || length < 0) return false;
|
|
317
|
+
const BOOL quiet = (target.properties & CBCharacteristicPropertyWriteWithoutResponse) != 0;
|
|
318
|
+
const CBCharacteristicWriteType type = quiet ? CBCharacteristicWriteWithoutResponse : CBCharacteristicWriteWithResponse;
|
|
319
|
+
const NSUInteger chunk = MAX((NSUInteger)20, [_peripheral maximumWriteValueLengthForType:type]);
|
|
320
|
+
const NSUInteger total = static_cast<NSUInteger>(length);
|
|
321
|
+
for (NSUInteger offset = 0; offset < total; offset += chunk) {
|
|
322
|
+
NSData *part = [NSData dataWithBytes:bytes + offset length:MIN(chunk, total - offset)];
|
|
323
|
+
[_peripheral writeValue:part forCharacteristic:target type:type];
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
- (std::vector<std::uint8_t>)read:(const char *)service characteristic:(const char *)characteristic {
|
|
329
|
+
CBUUID *serviceID = uuidOf(service);
|
|
330
|
+
CBUUID *characteristicID = uuidOf(characteristic);
|
|
331
|
+
if (!serviceID || !characteristicID) return {};
|
|
332
|
+
auto found = _received.find(slotKey(serviceID, characteristicID));
|
|
333
|
+
if (found == _received.end()) return {};
|
|
334
|
+
std::vector<std::uint8_t> bytes = std::move(found->second);
|
|
335
|
+
found->second.clear();
|
|
336
|
+
return bytes;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
- (void)requestRead:(const char *)service characteristic:(const char *)characteristic {
|
|
340
|
+
CBCharacteristic *target = [self characteristic:service of:characteristic];
|
|
341
|
+
if (target && _peripheral) [_peripheral readValueForCharacteristic:target];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
@end
|
|
345
|
+
|
|
346
|
+
namespace gea::framework::bluetooth {
|
|
347
|
+
namespace {
|
|
348
|
+
|
|
349
|
+
class AppleBluetoothCentralDriver final : public BluetoothCentralDriver {
|
|
350
|
+
public:
|
|
351
|
+
const char *state() override { return [central() state]; }
|
|
352
|
+
void startScan(const char *servicesCsv) override { [central() startScan:servicesCsv]; }
|
|
353
|
+
void stopScan() override {
|
|
354
|
+
if (instance_) [instance_ stopScan];
|
|
355
|
+
}
|
|
356
|
+
bool scanning() override { return instance_ && instance_->_wantsScan; }
|
|
357
|
+
int scanCount() override { return instance_ ? static_cast<int>(instance_->_scan.size()) : 0; }
|
|
358
|
+
const char *scanIdAt(int index) override { return entry(index) ? entry(index)->id.c_str() : ""; }
|
|
359
|
+
const char *scanNameAt(int index) override { return entry(index) ? entry(index)->name.c_str() : ""; }
|
|
360
|
+
int scanRssiAt(int index) override { return entry(index) ? entry(index)->rssi : 0; }
|
|
361
|
+
const char *scanServicesAt(int index) override { return entry(index) ? entry(index)->services.c_str() : ""; }
|
|
362
|
+
std::vector<std::uint8_t> scanManufacturerDataAt(int index) override {
|
|
363
|
+
return entry(index) ? entry(index)->manufacturer : std::vector<std::uint8_t>{};
|
|
364
|
+
}
|
|
365
|
+
void clearScan() override {
|
|
366
|
+
if (instance_) [instance_ clearScan];
|
|
367
|
+
}
|
|
368
|
+
void connect(const char *id, const char *servicesCsv) override { [central() connect:id services:servicesCsv]; }
|
|
369
|
+
const char *link() override { return instance_ ? instance_->_link.c_str() : "idle"; }
|
|
370
|
+
const char *linkError() override { return instance_ ? instance_->_linkError.c_str() : ""; }
|
|
371
|
+
void subscribe(const char *service, const char *characteristic) override {
|
|
372
|
+
if (instance_) [instance_ subscribe:service characteristic:characteristic];
|
|
373
|
+
}
|
|
374
|
+
bool subscribed(const char *service, const char *characteristic) override {
|
|
375
|
+
return instance_ && [instance_ subscribed:service characteristic:characteristic];
|
|
376
|
+
}
|
|
377
|
+
bool write(const char *service, const char *characteristic, const std::uint8_t *bytes, int length) override {
|
|
378
|
+
return instance_ && [instance_ write:service characteristic:characteristic bytes:bytes length:length];
|
|
379
|
+
}
|
|
380
|
+
std::vector<std::uint8_t> read(const char *service, const char *characteristic) override {
|
|
381
|
+
return instance_ ? [instance_ read:service characteristic:characteristic] : std::vector<std::uint8_t>{};
|
|
382
|
+
}
|
|
383
|
+
void requestRead(const char *service, const char *characteristic) override {
|
|
384
|
+
if (instance_) [instance_ requestRead:service characteristic:characteristic];
|
|
385
|
+
}
|
|
386
|
+
void disconnect() override {
|
|
387
|
+
if (instance_) [instance_ disconnect];
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
private:
|
|
391
|
+
GeaBluetoothCentral *central() {
|
|
392
|
+
if (!instance_) instance_ = [[GeaBluetoothCentral alloc] init];
|
|
393
|
+
return instance_;
|
|
394
|
+
}
|
|
395
|
+
const ScanEntry *entry(int index) const {
|
|
396
|
+
if (!instance_ || index < 0 || static_cast<std::size_t>(index) >= instance_->_scan.size()) return nullptr;
|
|
397
|
+
return &instance_->_scan[static_cast<std::size_t>(index)];
|
|
398
|
+
}
|
|
399
|
+
GeaBluetoothCentral *instance_ = nil;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
AppleBluetoothCentralDriver &appleCentral() {
|
|
403
|
+
static AppleBluetoothCentralDriver driver;
|
|
404
|
+
return driver;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
[[maybe_unused]] const bool installed = (BluetoothCentralDevice::setDriver(&appleCentral()), true);
|
|
408
|
+
|
|
409
|
+
} // namespace
|
|
410
|
+
} // namespace gea::framework::bluetooth
|
|
411
|
+
|
|
412
|
+
#endif
|