-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
PeripheralBaseMacOS.mm
689 lines (552 loc) · 26.5 KB
/
PeripheralBaseMacOS.mm
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#import "PeripheralBaseMacOS.h"
#import "CharacteristicBuilder.h"
#import "DescriptorBuilder.h"
#import "LoggingInternal.h"
#import "ServiceBuilder.h"
#import "Utils.h"
#import <simpleble/Exceptions.h>
#import <optional>
#define WAIT_UNTIL_FALSE(obj, var) \
do { \
BOOL _tmpVar = YES; \
while (_tmpVar) { \
[NSThread sleepForTimeInterval:0.01]; \
@synchronized(obj) { \
_tmpVar = (var); \
} \
} \
} while (0)
// --------------------------------------------------
@interface BleTask : NSObject
@property(strong, atomic) NSError* error;
@property(atomic) BOOL pending;
@end
@implementation BleTask
- (instancetype)init {
self = [super init];
if (self) {
_pending = NO;
}
return self;
}
@end
@interface DescriptorExtras : NSObject
@property(strong) BleTask* task;
@end
@implementation DescriptorExtras
- (instancetype)init {
self = [super init];
if (self) {
_task = [[BleTask alloc] init];
}
return self;
}
@end
@interface CharacteristicExtras : NSObject {
@public
std::function<void(SimpleBLE::ByteArray)> valueChangedCallback;
}
@property(strong) BleTask* task;
@property(strong) NSData* value;
@property(strong) NSMutableDictionary<NSString*, DescriptorExtras*>* descriptorExtras;
@end
@implementation CharacteristicExtras
- (instancetype)init {
self = [super init];
if (self) {
_task = [[BleTask alloc] init];
_descriptorExtras = [[NSMutableDictionary alloc] init];
}
return self;
}
@end
// --------------------------------------------------
@interface PeripheralBaseMacOS () {}
@property(strong) CBPeripheral* peripheral;
@property(strong) CBCentralManager* centralManager;
@property(strong, atomic) BleTask* task;
@property(strong, atomic) NSError* disconnectionError;
@property(strong) NSMutableDictionary<NSString*, CharacteristicExtras*>* characteristicExtras;
- (CBService*)findService:(NSString*)uuid;
- (CBCharacteristic*)findCharacteristic:(NSString*)uuid service:(CBService*)service;
- (std::pair<CBService*, CBCharacteristic*>)findServiceAndCharacteristic:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid;
- (void)throwBasedOnError:(NSError*)error withFormat:(NSString*)format, ...;
@end
@implementation PeripheralBaseMacOS
- (instancetype)init:(CBPeripheral*)peripheral centralManager:(CBCentralManager*)centralManager {
self = [super init];
if (self) {
// NOTE: It's important to make a copy of the peripheral and central objects into
// a strong property to prevent them from being deallocated by ARC or the garbage collector.
_peripheral = [peripheral copy];
_centralManager = centralManager;
_peripheral.delegate = self;
_characteristicExtras = [[NSMutableDictionary alloc] init];
_task = [[BleTask alloc] init];
}
return self;
}
- (void*)underlying {
return (__bridge void*)self.peripheral;
}
- (NSString*)identifier {
if (self.peripheral.name != nil) {
return [self.peripheral.name copy];
} else {
return @"";
}
}
- (NSString*)address {
return [self.peripheral.identifier UUIDString];
}
- (uint16_t)mtu {
return [self.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithoutResponse];
}
- (void)connect {
@synchronized(_task) {
// --- Connect to the peripheral ---
@synchronized(self) {
_task.error = nil;
_task.pending = YES;
[self.centralManager connectPeripheral:self.peripheral options:@{}]; // TODO: Do we need to pass any options?
}
WAIT_UNTIL_FALSE(self, _task.pending);
if (self.peripheral.state != CBPeripheralStateConnected || _task.error != nil) {
[self throwBasedOnError:_task.error withFormat:@"Peripheral Connection"];
}
// --- Discover services and characteristics ---
@synchronized(self) {
_task.error = nil;
_task.pending = YES;
[self.peripheral discoverServices:nil];
}
WAIT_UNTIL_FALSE(self, _task.pending);
if (self.peripheral.state != CBPeripheralStateConnected) {
[self throwBasedOnError:_disconnectionError withFormat:@"Service Discovery"];
}
if (self.peripheral.services == nil || _task.error != nil) {
[self throwBasedOnError:_task.error withFormat:@"Service Discovery"];
}
// For each service found, discover characteristics.
for (CBService* service in self.peripheral.services) {
@synchronized(self) {
_task.error = nil;
_task.pending = YES;
[self.peripheral discoverCharacteristics:nil forService:service];
}
WAIT_UNTIL_FALSE(self, _task.pending);
if (self.peripheral.state != CBPeripheralStateConnected) {
[self throwBasedOnError:_disconnectionError withFormat:@"Characteristic Discovery for service %@", service.UUID];
}
if (service.characteristics == nil || _task.error != nil) {
[self throwBasedOnError:_task.error withFormat:@"Characteristic Discovery for service %@", service.UUID];
}
// For each characteristic, create the associated extra properties and discover descriptors.
for (CBCharacteristic* characteristic in service.characteristics) {
CharacteristicExtras* characteristicExtras = [[CharacteristicExtras alloc] init];
@synchronized(self) {
_task.error = nil;
_task.pending = YES;
[self.peripheral discoverDescriptorsForCharacteristic:characteristic];
}
WAIT_UNTIL_FALSE(self, _task.pending);
if (self.peripheral.state != CBPeripheralStateConnected) {
[self throwBasedOnError:_disconnectionError
withFormat:@"Descriptor Discovery for characteristic %@", characteristic.UUID];
}
if (characteristic.descriptors == nil || _task.error != nil) {
[self throwBasedOnError:_task.error withFormat:@"Descriptor Discovery for characteristic %@", characteristic.UUID];
}
// For each descriptor, create the associated extra properties.
for (CBDescriptor* descriptor in characteristic.descriptors) {
@synchronized(self) {
[characteristicExtras.descriptorExtras setObject:[[DescriptorExtras alloc] init]
forKey:[[descriptor.UUID UUIDString] lowercaseString]];
}
}
@synchronized(self) {
[self.characteristicExtras setObject:characteristicExtras forKey:[[characteristic.UUID UUIDString] lowercaseString]];
}
}
}
}
}
- (void)disconnect {
@synchronized(_task) {
@synchronized(self) {
_task.error = nil;
_task.pending = YES;
[self.centralManager cancelPeripheralConnection:self.peripheral];
}
WAIT_UNTIL_FALSE(self, _task.pending);
if (self.peripheral.state != CBPeripheralStateDisconnected) {
[self throwBasedOnError:_task.error withFormat:@"Peripheral Disconnection"];
}
}
}
- (bool)isConnected {
return self.peripheral.state == CBPeripheralStateConnected;
}
- (std::vector<SimpleBLE::Service>)getServices {
std::vector<SimpleBLE::Service> service_list;
for (CBService* service in self.peripheral.services) {
// Build the list of characteristics for the service.
std::vector<SimpleBLE::Characteristic> characteristic_list;
for (CBCharacteristic* characteristic in service.characteristics) {
// Build the list of descriptors for the characteristic.
std::vector<SimpleBLE::Descriptor> descriptor_list;
for (CBDescriptor* descriptor in characteristic.descriptors) {
descriptor_list.push_back(SimpleBLE::DescriptorBuilder(uuidToSimpleBLE(descriptor.UUID)));
}
bool can_read = (characteristic.properties & CBCharacteristicPropertyRead) != 0;
bool can_write_request = (characteristic.properties & CBCharacteristicPropertyWrite) != 0;
bool can_write_command = (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) != 0;
bool can_notify = (characteristic.properties & CBCharacteristicPropertyNotify) != 0;
bool can_indicate = (characteristic.properties & CBCharacteristicPropertyIndicate) != 0;
characteristic_list.push_back(SimpleBLE::CharacteristicBuilder(uuidToSimpleBLE(characteristic.UUID), descriptor_list, can_read,
can_write_request, can_write_command, can_notify, can_indicate));
}
service_list.push_back(SimpleBLE::ServiceBuilder(uuidToSimpleBLE(service.UUID), characteristic_list));
}
return service_list;
}
- (SimpleBLE::ByteArray)read:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
// Check that the characteristic supports this feature.
if ((characteristic.properties & CBCharacteristicPropertyRead) == 0) {
NSLog(@"Characteristic does not support read.");
throw SimpleBLE::Exception::OperationNotSupported();
}
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
BleTask* task = characteristicExtras.task;
@synchronized(task) {
if (characteristic.isNotifying) {
// If the characteristic is already notifying, we'll just wait for the next notification.
@synchronized(self) {
characteristicExtras.value = nil;
}
WAIT_UNTIL_FALSE(self, (characteristicExtras.value == nil));
return SimpleBLE::ByteArray((const char*)characteristicExtras.value.bytes, characteristicExtras.value.length);
} else {
// Otherwise, we'll trigger a value read and wait for the response.
@synchronized(self) {
task.error = nil;
task.pending = YES;
[self.peripheral readValueForCharacteristic:characteristic];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (task.error != nil) {
[self throwBasedOnError:task.error withFormat:@"Characteristic %@ Read", characteristic.UUID];
}
return SimpleBLE::ByteArray((const char*)characteristic.value.bytes, characteristic.value.length);
}
}
}
- (void)writeRequest:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid payload:(NSData*)payload {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
// Check that the characteristic supports this feature.
if ((characteristic.properties & CBCharacteristicPropertyWrite) == 0) {
NSLog(@"Characteristic does not support write with response.");
throw SimpleBLE::Exception::OperationNotSupported();
}
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
BleTask* task = characteristicExtras.task;
@synchronized(task) {
@synchronized(self) {
task.error = nil;
task.pending = YES;
[self.peripheral writeValue:payload forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (task.error != nil) {
[self throwBasedOnError:task.error withFormat:@"Characteristic %@ Write Request", characteristic.UUID];
}
}
}
- (void)writeCommand:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid payload:(NSData*)payload {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
// Check that the characteristic supports this feature.
if ((characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) == 0) {
NSLog(@"Characteristic does not support write without response.");
throw SimpleBLE::Exception::OperationNotSupported();
}
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
BleTask* task = characteristicExtras.task;
@synchronized(task) {
// NOTE: This write is unacknowledged.
@synchronized(self) {
[self.peripheral writeValue:payload forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}
}
}
- (void)notify:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid
callback:(std::function<void(SimpleBLE::ByteArray)>)callback {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
BleTask* task = characteristicExtras.task;
@synchronized(task) {
@synchronized(self) {
task.error = nil;
task.pending = YES;
characteristicExtras->valueChangedCallback = callback;
[self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (!characteristic.isNotifying || task.error != nil) {
[self throwBasedOnError:task.error withFormat:@"Characteristic %@ Notify/Indicate", characteristic.UUID];
}
}
}
- (void)indicate:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid
callback:(std::function<void(SimpleBLE::ByteArray)>)callback {
[self notify:service_uuid characteristic_uuid:characteristic_uuid callback:callback];
}
- (void)unsubscribe:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
BleTask* task = characteristicExtras.task;
@synchronized(task) {
@synchronized(self) {
task.error = nil;
task.pending = YES;
characteristicExtras->valueChangedCallback = nil;
[self.peripheral setNotifyValue:NO forCharacteristic:characteristic];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (characteristic.isNotifying || task.error != nil) {
[self throwBasedOnError:task.error withFormat:@"Characteristic %@ Unsubscribe", characteristic.UUID];
}
}
}
- (SimpleBLE::ByteArray)read:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid
descriptor_uuid:(NSString*)descriptor_uuid {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
CBDescriptor* descriptor = [self findDescriptor:descriptor_uuid characteristic:characteristic];
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:descriptor_uuid];
BleTask* task = descriptorExtras.task;
@synchronized(task) {
@synchronized(self) {
task.error = nil;
task.pending = YES;
[self.peripheral readValueForDescriptor:descriptor];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (task.error != nil) {
[self throwBasedOnError:task.error withFormat:@"Descriptor %@ Read", descriptor.UUID];
}
const char* bytes = (const char*)[descriptor.value bytes];
return SimpleBLE::ByteArray(bytes, [descriptor.value length]);
}
}
- (void)write:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid
descriptor_uuid:(NSString*)descriptor_uuid
payload:(NSData*)payload {
std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
characteristic_uuid:characteristic_uuid];
CBCharacteristic* characteristic = serviceAndCharacteristic.second;
CBDescriptor* descriptor = [self findDescriptor:descriptor_uuid characteristic:characteristic];
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:descriptor_uuid];
BleTask* task = descriptorExtras.task;
@synchronized(task) {
@synchronized(self) {
task.error = nil;
task.pending = YES;
[self.peripheral writeValue:payload forDescriptor:descriptor];
}
WAIT_UNTIL_FALSE(self, task.pending);
if (task.error) {
[self throwBasedOnError:task.error withFormat:@"Descriptor %@ Write", descriptor.UUID];
}
}
}
#pragma mark - Auxiliary methods
- (CBService*)findService:(NSString*)uuid {
CBUUID* service_uuid = [CBUUID UUIDWithString:uuid];
for (CBService* service in self.peripheral.services) {
if ([service.UUID isEqual:service_uuid]) {
return service;
}
}
throw SimpleBLE::Exception::ServiceNotFound([uuid UTF8String]);
}
- (CBCharacteristic*)findCharacteristic:(NSString*)uuid service:(CBService*)service {
CBUUID* characteristic_uuid = [CBUUID UUIDWithString:uuid];
if (service == nil) {
throw SimpleBLE::Exception::BaseException("Invalid service parameter.");
}
for (CBCharacteristic* characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:characteristic_uuid]) {
return characteristic;
}
}
throw SimpleBLE::Exception::CharacteristicNotFound([uuid UTF8String]);
}
- (CBDescriptor*)findDescriptor:(NSString*)uuid characteristic:(CBCharacteristic*)characteristic {
CBUUID* descriptor_uuid = [CBUUID UUIDWithString:uuid];
for (CBDescriptor* descriptor in characteristic.descriptors) {
if ([descriptor.UUID isEqual:descriptor_uuid]) {
return descriptor;
}
}
throw SimpleBLE::Exception::DescriptorNotFound([uuid UTF8String]);
}
- (std::pair<CBService*, CBCharacteristic*>)findServiceAndCharacteristic:(NSString*)service_uuid
characteristic_uuid:(NSString*)characteristic_uuid {
CBService* service = [self findService:service_uuid];
CBCharacteristic* characteristic = [self findCharacteristic:characteristic_uuid service:service];
return std::pair<CBService*, CBCharacteristic*>(service, characteristic);
}
- (void)throwBasedOnError:(NSError*)error withFormat:(NSString*)format, ... {
va_list argList;
va_start(argList, format);
NSString* formattedString = [[NSString alloc] initWithFormat:format arguments:argList];
va_end(argList);
if (error == nil) {
NSString* exceptionMessage = [NSString stringWithFormat:@"%@ failed", formattedString];
NSLog(@"%@", exceptionMessage);
throw SimpleBLE::Exception::OperationFailed([exceptionMessage UTF8String]);
} else {
NSString* errorMessage = [error localizedDescription];
NSString* exceptionMessage = [NSString stringWithFormat:@"%@ failed: %@", formattedString, errorMessage];
NSLog(@"%@", exceptionMessage);
throw SimpleBLE::Exception::OperationFailed([exceptionMessage UTF8String]);
}
}
#pragma mark - CBCentralManagerDelegate
- (void)delegateDidConnect {
@synchronized(self) {
_task.pending = NO;
}
}
- (void)delegateDidFailToConnect:(NSError*)error {
@synchronized(self) {
_task.error = error;
_task.pending = NO;
}
}
- (void)delegateDidDisconnect:(NSError*)error {
@synchronized(self) {
self->_disconnectionError = error;
_task.pending = NO;
for (CharacteristicExtras* characteristicExtras in self.characteristicExtras.allValues) {
characteristicExtras.task.pending = NO;
for (DescriptorExtras* descriptorExtras in characteristicExtras.descriptorExtras.allValues) {
descriptorExtras.task.pending = NO;
}
}
}
}
#pragma mark - CBPeripheralDelegate
- (void)peripheral:(CBPeripheral*)peripheral didModifyServices:(NSArray<CBService*>*)invalidatedServices {
// NOTE: Whenever this method is called, any pending operations are cancelled. In addition to that,
// the provided list of services does NOT include any characteristics or descriptors, so need to
// clear pending flags for those as well.
@synchronized(self) {
_task.pending = NO;
for (CharacteristicExtras* characteristicExtras in self.characteristicExtras.allValues) {
characteristicExtras.task.pending = NO;
for (DescriptorExtras* descriptorExtras in characteristicExtras.descriptorExtras.allValues) {
descriptorExtras.task.pending = NO;
}
}
}
}
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error {
@synchronized(self) {
_task.error = error;
_task.pending = NO;
}
}
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(CBService*)service error:(NSError*)error {
@synchronized(self) {
_task.error = error;
_task.pending = NO;
}
}
- (void)peripheral:(CBPeripheral*)peripheral
didDiscoverDescriptorsForCharacteristic:(CBCharacteristic*)characteristic
error:(NSError*)error {
@synchronized(self) {
_task.error = error;
_task.pending = NO;
}
}
- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:[[characteristic.UUID UUIDString] lowercaseString]];
if (characteristic.isNotifying) {
// If the characteristic is notifying, just save the value and trigger the callback.
@synchronized(self) {
characteristicExtras.value = characteristic.value;
}
if (characteristicExtras->valueChangedCallback != nil) {
characteristicExtras->valueChangedCallback(
SimpleBLE::ByteArray((const char*)characteristic.value.bytes, characteristic.value.length));
}
} else {
// If the characteristic is not notifying, then this is a response to a read request.
BleTask* task = characteristicExtras.task;
@synchronized(self) {
task.error = error;
task.pending = NO;
}
}
}
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:[[characteristic.UUID UUIDString] lowercaseString]];
BleTask* task = characteristicExtras.task;
@synchronized(self) {
task.error = error;
task.pending = NO;
}
}
- (void)peripheral:(CBPeripheral*)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic*)characteristic
error:(NSError*)error {
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:[[characteristic.UUID UUIDString] lowercaseString]];
BleTask* task = characteristicExtras.task;
@synchronized(self) {
task.error = error;
task.pending = NO;
}
}
- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral*)peripheral {
NSLog(@"Peripheral ready to send: %@", peripheral);
}
- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:[[descriptor.characteristic.UUID UUIDString] lowercaseString]];
DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:[[descriptor.UUID UUIDString] lowercaseString]];
BleTask* task = descriptorExtras.task;
@synchronized(self) {
task.error = error;
task.pending = NO;
}
}
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:[[descriptor.characteristic.UUID UUIDString] lowercaseString]];
DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:[[descriptor.UUID UUIDString] lowercaseString]];
BleTask* task = descriptorExtras.task;
@synchronized(self) {
task.error = error;
task.pending = NO;
}
}
@end