Skip to content

Commit

Permalink
feat(ios): change dispatch at retrieve info and init
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Oct 23, 2018
1 parent 98cc090 commit 1fae675
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 95 deletions.
19 changes: 8 additions & 11 deletions core/network/ble/BertyDevice.h
Expand Up @@ -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
Expand Down
46 changes: 3 additions & 43 deletions core/network/ble/BertyDevice.m
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
124 changes: 83 additions & 41 deletions core/network/ble/ble.m
Expand Up @@ -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];
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -218,12 +290,9 @@ - (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary<NSString *,id> *)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];
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]);
Expand Down

0 comments on commit 1fae675

Please sign in to comment.