From 1fae6753c87d8445acf7fab8a768f2fff9745365 Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Tue, 23 Oct 2018 11:24:36 +0200 Subject: [PATCH] feat(ios): change dispatch at retrieve info and init Signed-off-by: Sacha Froment --- core/network/ble/BertyDevice.h | 19 +++-- core/network/ble/BertyDevice.m | 46 +----------- core/network/ble/ble.m | 124 ++++++++++++++++++++++----------- 3 files changed, 94 insertions(+), 95 deletions(-) diff --git a/core/network/ble/BertyDevice.h b/core/network/ble/BertyDevice.h index 1ea26ad505..b4646ccb26 100644 --- a/core/network/ble/BertyDevice.h +++ b/core/network/ble/BertyDevice.h @@ -15,23 +15,20 @@ @interface BertyDevice : NSObject @property (nonatomic, readwrite, strong) NSMutableArray *toSend; -@property (nonatomic, readonly, strong) NSString *peerID; -@property (nonatomic, readonly, strong) NSString *ma; +@property (nonatomic, readwrite, strong) NSString *peerID; +@property (nonatomic, readwrite, strong) NSString *ma; @property (nonatomic, readwrite, assign) BOOL isWaiting; @property (nonatomic, readwrite, strong) CBPeripheral *peripheral; -@property (nonatomic, readwrite, strong) dispatch_semaphore_t acceptSema; -@property (nonatomic, readwrite, strong) dispatch_semaphore_t maSema; -@property (nonatomic, readwrite, strong) dispatch_semaphore_t peerIDSema; -@property (nonatomic, readwrite, strong) dispatch_semaphore_t readerSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t connSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t svcSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t acceptSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t maSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t peerIDSema; +@property (atomic, readwrite, strong) dispatch_semaphore_t writerSema; - (instancetype)initWithPeripheral:(CBPeripheral *)peripheral; -- (void)setPeerID:(NSString*)p; -- (void)setMa:(NSString*)a; - (void)write:(NSData *)data; - (void)checkAndWrite; -- (void)waitDeviceRdy; -- (void)releaseAcceptSema; -- (void)releaseWriterSema; - (void)popToSend; @end diff --git a/core/network/ble/BertyDevice.m b/core/network/ble/BertyDevice.m index 8cdd9f8124..cf8e946d7b 100644 --- a/core/network/ble/BertyDevice.m +++ b/core/network/ble/BertyDevice.m @@ -17,14 +17,12 @@ - (instancetype)initWithPeripheral:(CBPeripheral *)peripheral { self.peripheral = peripheral; self.toSend = [[NSMutableArray alloc]init]; self.isWaiting = NO; + self.connSema = dispatch_semaphore_create(0); + self.svcSema = dispatch_semaphore_create(0); self.acceptSema = dispatch_semaphore_create(0); self.maSema = dispatch_semaphore_create(0); self.peerIDSema = dispatch_semaphore_create(0); - self.readerSema = dispatch_semaphore_create(0); - NSLog(@"INIT PERIPHERAL %@", [peripheral.identifier UUIDString]); - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [self waitDeviceRdy]; - }); + self.writerSema = dispatch_semaphore_create(0); return self; } @@ -93,43 +91,5 @@ - (void)checkAndWrite { } } -- (void)waitDeviceRdy { - dispatch_semaphore_wait(self.maSema, DISPATCH_TIME_FOREVER); - dispatch_semaphore_wait(self.peerIDSema, DISPATCH_TIME_FOREVER); - dispatch_semaphore_wait(self.readerSema, DISPATCH_TIME_FOREVER); - dispatch_semaphore_wait(self.acceptSema, DISPATCH_TIME_FOREVER); - AddToPeerStore([self.peerID UTF8String], [self.ma UTF8String]); - NSLog(@"Device REALLY rdy"); -} - -- (void)releaseAcceptSema { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSLog(@"unlock accept peer"); - dispatch_semaphore_signal(self.acceptSema); - }); -} - -- (void)releaseWriterSema { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSLog(@"unlock read peer"); - dispatch_semaphore_signal(self.readerSema); - }); -} - -- (void)setPeerID:(NSString*)p { - _peerID = p; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSLog(@"unlock peer"); - dispatch_semaphore_signal(self.peerIDSema); - }); -} - -- (void)setMa:(NSString*)a { - _ma = a; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSLog(@"unlock ma"); - dispatch_semaphore_signal(self.maSema); - }); -} @end diff --git a/core/network/ble/ble.m b/core/network/ble/ble.m index 4c7a23b961..de913513e4 100644 --- a/core/network/ble/ble.m +++ b/core/network/ble/ble.m @@ -99,12 +99,78 @@ - (instancetype)initWithMa:(NSString *)ma AndPeerID:(NSString *)peerID { self.peripheralManager.delegate = self; } - NSLog(@"init finished"); - [self startAdvertising]; - [self startDiscover]; + NSLog(@"init finished %@", [self.centralManager retrieveConnectedPeripheralsWithServices:@[self.serviceUUID]]); + for (CBPeripheral *peripheral in [self.centralManager retrieveConnectedPeripheralsWithServices:@[self.serviceUUID]]) { + [self newDevice:peripheral]; + } + return self; } +- (void)checkDiscoverBertyDeviceCharacteristic:(BertyDevice *)device { + for (CBCharacteristic *characteristic in [self getSvcForPeripheral:device.peripheral].characteristics) { + if ([characteristic.UUID isEqual:self.maUUID] || [characteristic.UUID isEqual:self.peerUUID]) { + [device.peripheral readValueForCharacteristic:characteristic]; + } else if ([characteristic.UUID isEqual:self.acceptUUID]) { + dispatch_semaphore_signal(device.acceptSema); + } else if ([characteristic.UUID isEqual:self.writerUUID]) { + dispatch_semaphore_signal(device.writerSema); + } + } +} + +- (void)checkUpdateValueCharacteristic:(CBCharacteristic *)charact ForDevice:(BertyDevice *)device { + if ([charact.UUID isEqual:self.maUUID]) { + device.ma = [[NSString alloc] initWithData:charact.value encoding:NSUTF8StringEncoding]; + dispatch_semaphore_signal(device.maSema); + } else if ([charact.UUID isEqual:self.peerUUID]) { + device.peerID = [[NSString alloc] initWithData:charact.value encoding:NSUTF8StringEncoding]; + dispatch_semaphore_signal(device.peerIDSema); + } +} + +- (BertyDevice *)newDevice:(CBPeripheral *)peripheral { + BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; + [peripheral setDelegate:self]; + + if (device == nil) { + device = [[BertyDevice alloc] initWithPeripheral:peripheral]; + @synchronized (self.bertyDevices) { + [self.bertyDevices setObject:device forKey:[peripheral.identifier UUIDString]]; + } + } + + [self.centralManager connectPeripheral:device.peripheral options:nil]; + + // once device we have the device init dispact a block that will wait for the connection + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + dispatch_semaphore_wait(device.connSema, DISPATCH_TIME_FOREVER); + [peripheral discoverServices:@[self.serviceUUID]]; + dispatch_semaphore_wait(device.svcSema, DISPATCH_TIME_FOREVER); + + // once service is disco asked for charact retrieve + [device.peripheral discoverCharacteristics:@[self.peerUUID, self.maUUID, self.writerUUID, self.acceptUUID] + forService:[self getSvcForPeripheral:device.peripheral]]; + dispatch_group_t group = dispatch_group_create(); + dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + dispatch_semaphore_wait(device.writerSema, DISPATCH_TIME_FOREVER); + }); + dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + dispatch_semaphore_wait(device.acceptSema, DISPATCH_TIME_FOREVER); + }); + dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + dispatch_semaphore_wait(device.maSema, DISPATCH_TIME_FOREVER); + }); + dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + dispatch_semaphore_wait(device.peerIDSema, DISPATCH_TIME_FOREVER); + }); + dispatch_group_wait(group, DISPATCH_TIME_FOREVER); + NSLog(@"READY device %@", peripheral); + }); + + return device; +} + - (CBCharacteristic *)getWriterForPeripheral:(CBPeripheral *)peripheral { return [self characteristicWithUUID:self.writerUUID forServiceUUID:self.serviceUUID inPeripheral:peripheral]; } @@ -121,6 +187,10 @@ - (CBCharacteristic *)getMaForPeripheral:(CBPeripheral *)peripheral { return [self characteristicWithUUID:self.maUUID forServiceUUID:self.serviceUUID inPeripheral:peripheral]; } +- (CBService *)getSvcForPeripheral:(CBPeripheral *)peripheral { + return [self serviceWithUUID:self.serviceUUID forPeripheral:peripheral]; +} + - (int)dialPeer:(NSString *)peerID { BertyDevice *device = nil; while (device == nil) { @@ -189,13 +259,15 @@ - (CBService *)serviceWithUUID:(CBUUID *)serviceUUID forPeripheral:(CBPeripheral return service; } } + return nil; } - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"didConnectPeriheral: %@", [peripheral.identifier UUIDString]); - [peripheral discoverServices:@[self.serviceUUID]]; + BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; + dispatch_semaphore_signal(device.connSema); } - (void)centralManager:(CBCentralManager *)central @@ -218,12 +290,9 @@ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { - if (![self.discoveredDevice objectForKey:peripheral.identifier]) { + if (![self getDeviceFromPeripheral:peripheral]) { NSLog(@"didDiscoverPeripheral: %@", [peripheral.identifier UUIDString]); - [peripheral setDelegate:self]; - [self.bertyDevices setObject:[[BertyDevice alloc]initWithPeripheral:peripheral] forKey:[peripheral.identifier UUIDString]]; - [self.centralManager connectPeripheral:peripheral options:nil]; - [self.discoveredDevice setObject:peripheral forKey:peripheral.identifier]; + [self newDevice:peripheral]; } } @@ -254,13 +323,8 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central { - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"didDiscoverServices %@", [peripheral.identifier UUIDString]); - for (CBService* service in peripheral.services) { - if ([service.UUID isEqual:self.serviceUUID]) { - NSLog(@"try discovering charact"); - [peripheral discoverCharacteristics:@[self.peerUUID, self.writerUUID, self.maUUID, self.acceptUUID] forService:service]; - return ; - } - } + BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; + dispatch_semaphore_signal(device.svcSema); } - (void)peripheral:(CBPeripheral *)peripheral @@ -276,25 +340,7 @@ - (void)peripheral:(CBPeripheral *)peripheral NSLog(@"error discovering characteristic %@ %@", [error localizedFailureReason], [error localizedDescription]); return ; } - for (CBCharacteristic *characteristic in service.characteristics) { - if ([characteristic.UUID isEqual:self.maUUID]) { - NSLog(@"Try reading ma"); - [peripheral readValueForCharacteristic:characteristic]; - NSLog(@"readed ma"); - } else if ([characteristic.UUID isEqual:self.writerUUID]) { - BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; - NSLog(@"release writer sema"); - [device releaseWriterSema]; - } else if ([characteristic.UUID isEqual:self.peerUUID]) { - NSLog(@"Try reading peer"); - [peripheral readValueForCharacteristic:characteristic]; - NSLog(@"readed peer"); - } else if ([characteristic.UUID isEqual:self.acceptUUID]) { - BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; - NSLog(@"release sema"); - [device releaseAcceptSema]; - } - } + [self checkDiscoverBertyDeviceCharacteristic:[self getDeviceFromPeripheral:peripheral]]; } - (void)peripheral:(CBPeripheral *)peripheral @@ -309,13 +355,9 @@ - (void)peripheral:(CBPeripheral *)peripheral - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { - NSLog(@"getting value"); + NSLog(@"didUpdateValueForCharacteristic %@ %@", [peripheral.identifier], [characteristic.UUID]); BertyDevice *device = [self getDeviceFromPeripheral:peripheral]; - if ([characteristic.UUID isEqual:self.maUUID] && device != nil) { - [device setMa:[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]]; - } else if ([characteristic.UUID isEqual:self.peerUUID] && device != nil) { - [device setPeerID:[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]]; - } + [self checkUpdateValueCharacteristic:characteristic ForDevice:device]; if (error) { NSLog(@"error: %@", [error localizedDescription]);