forked from tinygo-org/cbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btdlg.m
444 lines (388 loc) · 12.3 KB
/
btdlg.m
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
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "bt.h"
@implementation BTDlg
- (id)
init
{
self = [super init];
if (self == nil) {
return nil;
}
return self;
}
/**
* Called when the central manager successfully connects to a peripheral.
*/
- (void)
centralManager:(CBCentralManager *)cm
didConnectPeripheral:(CBPeripheral *)prph
{
prph.delegate = self;
BTCentralManagerDidConnectPeripheral(cm, prph);
}
/**
* Called when a connection to a preipheral is terminated.
*/
- (void)
centralManager:(CBCentralManager *)cm
didDisconnectPeripheral:(CBPeripheral *)prph
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTCentralManagerDidDisconnectPeripheral(cm, prph, &err);
}
/**
* Called when the central manager fails to connect to a peripheral.
*/
- (void)
centralManager:(CBCentralManager *)cm
didFailToConnectPeripheral:(CBPeripheral *)prph
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTCentralManagerDidFailToConnectPeripheral(cm, prph, &err);
}
// macOS 10.15+
#if 0
- (void)
centralManager:(CBCentralManager *)cm
connectionEventDidOccur:(CBConnectionEvent)event
forPeripheral:(CBPeripheral *)prph
{
BTCentralManagerConnectionEventDidOccur(cm, event, prph);
}
#endif
/**
* Called when the central manager discovers a peripheral while scanning for
* devices.
*/
- (void)
centralManager:(CBCentralManager *)cm
didDiscoverPeripheral:(CBPeripheral *)prph
advertisementData:(NSDictionary *)advData
RSSI:(NSNumber *)RSSI
{
struct adv_fields af = {0};
af.name = dict_string(advData, CBAdvertisementDataLocalNameKey);
af.mfg_data = dict_bytes(advData, CBAdvertisementDataManufacturerDataKey);
af.pwr_lvl = dict_int(advData, CBAdvertisementDataTxPowerLevelKey, ADV_FIELDS_PWR_LVL_NONE);
af.connectable = dict_int(advData, CBAdvertisementDataIsConnectable, ADV_FIELDS_CONNECTABLE_NONE);
const NSArray *arr = [advData objectForKey:CBAdvertisementDataServiceUUIDsKey];
const char *svc_uuids[[arr count]];
for (int i = 0; i < [arr count]; i++) {
const CBUUID *uuid = [arr objectAtIndex:i];
svc_uuids[i] = [[uuid UUIDString] UTF8String];
}
af.svc_uuids = (struct string_arr) {
.strings = svc_uuids,
.count = [arr count],
};
const NSDictionary *dict = [advData objectForKey:CBAdvertisementDataServiceDataKey];
const NSArray *keys = [dict allKeys];
const char *svc_data_uuids[[keys count]];
struct byte_arr svc_data_values[[keys count]];
for (int i = 0; i < [keys count]; i++) {
const CBUUID *uuid = [keys objectAtIndex:i];
svc_data_uuids[i] = [[uuid UUIDString] UTF8String];
const NSData *data = [dict objectForKey:uuid];
svc_data_values[i].data = [data bytes];
svc_data_values[i].length = [data length];
}
af.svc_data_uuids = (struct string_arr) {
.strings = svc_data_uuids,
.count = [keys count],
};
af.svc_data_values = svc_data_values;
prph.delegate = self;
[prph retain];
BTCentralManagerDidDiscoverPeripheral(cm, prph, &af, [RSSI intValue]);
}
/**
* Called whenever the central manager's state is updated.
*/
- (void)
centralManagerDidUpdateState:(CBCentralManager *)cm
{
BTCentralManagerDidUpdateState(cm);
}
/**
* Called when the central manager is about to restore its state (as requested
* by the application).
*/
- (void)
centralManager:(CBCentralManager *)cm
willRestoreState:(NSDictionary<NSString *,id> *)dict
{
struct cmgr_restore_opts opts = {0};
const NSArray *prphs = [dict objectForKey:CBCentralManagerRestoredStatePeripheralsKey];
opts.prphs = nsarray_to_obj_arr(prphs);
const NSArray *uuids = [dict objectForKey:CBCentralManagerRestoredStateScanServicesKey];
opts.scan_svcs = cbuuids_to_strs(uuids);
struct scan_opts scan_opts = {0};
const NSDictionary *scan_dict = [dict objectForKey:CBCentralManagerRestoredStateScanOptionsKey];
if (scan_dict != nil && [scan_dict count] > 0) {
opts.scan_opts = &scan_opts;
NSNumber *dups = [scan_dict objectForKey:CBCentralManagerScanOptionAllowDuplicatesKey];
if (dups != nil && [dups boolValue]) {
opts.scan_opts->allow_dups = true;
}
NSArray *sol_uuids = [scan_dict objectForKey:CBCentralManagerScanOptionSolicitedServiceUUIDsKey];
opts.scan_opts->sol_svc_uuids = cbuuids_to_strs(sol_uuids);
}
BTCentralManagerWillRestoreState(cm, &opts);
free(scan_opts.sol_svc_uuids.strings);
free(opts.scan_svcs.strings);
free(opts.prphs.objs);
}
/**
* Called when the central manager successfully discovers services on a
* peripheral.
*/
- (void) peripheral:(CBPeripheral *)prph
didDiscoverServices:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidDiscoverServices(prph, &err);
}
/**
* Called when the central manager successfully discovers included services of
* a peripheral's primary service.
*/
- (void) peripheral:(CBPeripheral *)prph
didDiscoverIncludedServicesForService:(CBService *)svc
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidDiscoverIncludedServices(prph, svc, &err);
}
/**
* Called when the central manager successfully discovers characteristics of a
* peripheral's service.
*/
- (void)
peripheral:(CBPeripheral *)prph
didDiscoverCharacteristicsForService:(CBService *)svc
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidDiscoverCharacteristics(prph, svc, &err);
}
/**
* Called when the central manager successfully discovers descriptors of a
* peripheral's characteristic.
*/
- (void)
peripheral:(CBPeripheral *)prph
didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)chr
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidDiscoverDescriptors(prph, chr, &err);
}
/**
* Called when a connected peripheral communicates a characteristic's value
* (via read response, notification, or indication).
*/
- (void)
peripheral:(CBPeripheral *)prph
didUpdateValueForCharacteristic:(CBCharacteristic *)chr
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidUpdateValueForCharacteristic(prph, chr, &err);
}
/**
* Called when a connected peripheral communicates a descriptors's value
* (via read response).
*/
- (void)
peripheral:(CBPeripheral *)prph
didUpdateValueForDescriptor:(CBDescriptor *)dsc
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidUpdateValueForDescriptor(prph, dsc, &err);
}
/**
* Called when a connected peripheral responds to a Characteristic Write
* Request.
*/
- (void)
peripheral:(CBPeripheral *)prph
didWriteValueForCharacteristic:(CBCharacteristic *)chr
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidWriteValueForCharacteristic(prph, chr, &err);
}
/**
* Called when a connected peripheral responds to a Descriptor Write Request.
*/
- (void)
peripheral:(CBPeripheral *)prph
didWriteValueForDescriptor:(CBDescriptor *)dsc
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidWriteValueForDescriptor(prph, dsc, &err);
}
/**
* Called when a previously unsuccessful Write Without Response request can be
* retried.
*/
- (void)
peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)prph
{
BTPeripheralIsReadyToSendWriteWithoutResponse(prph);
}
/**
* Called when we (un)subscribe to a connected peripheral's characteristic.
*/
- (void)
peripheral:(CBPeripheral *)prph
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)chr
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidUpdateNotificationState(prph, chr, &err);
}
/**
* Called when we read a connected peripheral's RSSI.
*/
- (void)
peripheral:(CBPeripheral *)prph
didReadRSSI:(NSNumber *)RSSI
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralDidReadRSSI(prph, [RSSI intValue], &err);
}
/**
* Called when a connected peripheral changes its name.
*/
- (void)
peripheralDidUpdateName:(CBPeripheral *)prph
{
BTPeripheralDidUpdateName(prph);
}
/**
* Called when a connected peripheral changes its set of services.
*/
- (void)
peripheral:(CBPeripheral *)prph
didModifyServices:(NSArray<CBService *> *)invSvcs
{
struct obj_arr oa = nsarray_to_obj_arr(invSvcs);
BTPeripheralDidModifyServices(prph, &oa);
free(oa.objs);
}
/**
* Called whenever the peripheral manager's state is updated.
*/
- (void)
peripheralManagerDidUpdateState:(CBPeripheralManager *)pm
{
BTPeripheralManagerDidUpdateState(pm);
}
/**
* Called when the peripheral manager is about to restore its state (as
* requested by the application).
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
willRestoreState:(NSDictionary<NSString *,id> *)dict
{
struct pmgr_restore_opts opts = {0};
const NSArray *svcs = [dict objectForKey:CBPeripheralManagerRestoredStateServicesKey];
opts.svcs = nsarray_to_obj_arr(svcs);
struct adv_data adv_data = {0};
const NSDictionary *ad_dict = [dict objectForKey:CBPeripheralManagerRestoredStateAdvertisementDataKey];
if (ad_dict != nil && [dict count] > 0) {
opts.adv_data = &adv_data;
NSString *nss = [ad_dict objectForKey:CBAdvertisementDataLocalNameKey];
if (nss != nil) {
adv_data.name = [nss UTF8String];
}
NSArray *nsa = [ad_dict objectForKey:CBAdvertisementDataServiceUUIDsKey];
if (nsa != nil) {
adv_data.svc_uuids = cbuuids_to_strs(nsa);
}
}
BTPeripheralManagerWillRestoreState(pmgr, &opts);
free(adv_data.svc_uuids.strings);
free(opts.svcs.objs);
}
/**
* Called when an attempt to register a service has completed.
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
didAddService:(CBService *)svc
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralManagerDidAddService(pmgr, svc, &err);
}
/**
* Called when we start advertising or fail to advertise.
*/
- (void)
peripheralManagerDidStartAdvertising:(CBPeripheralManager *)pmgr
error:(NSError *)nserr
{
struct bt_error err = nserror_to_bt_error(nserr);
BTPeripheralManagerDidStartAdvertising(pmgr, &err);
}
/**
* Called when a connected central has subscribed to one of our
* characteristics.
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
central:(CBCentral *)cent
didSubscribeToCharacteristic:(CBCharacteristic *)chr
{
BTPeripheralManagerCentralDidSubscribe(pmgr, cent, chr);
}
/**
* Called when a connected central has unsubscribed from one of our
* characteristics.
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
central:(CBCentral *)cent
didUnsubscribeFromCharacteristic:(CBCharacteristic *)chr
{
BTPeripheralManagerCentralDidUnsubscribe(pmgr, cent, chr);
}
/**
* Called when a previous unsuccessful attempt to send notifications can be
* retried.
*/
- (void)
peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)pmgr
{
BTPeripheralManagerIsReadyToUpdateSubscribers(pmgr);
}
/**
* Called when a connected cerntral has sent us a read request.
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
didReceiveReadRequest:(CBATTRequest *)req
{
BTPeripheralManagerDidReceiveReadRequest(pmgr, req);
}
/**
* Called when connected cerntrals have sent us write requests.
*/
- (void)
peripheralManager:(CBPeripheralManager *)pmgr
didReceiveWriteRequests:(NSArray *)reqs
{
struct obj_arr oa = nsarray_to_obj_arr(reqs);
BTPeripheralManagerDidReceiveWriteRequests(pmgr, &oa);
free(oa.objs);
}
@end