Skip to content

Commit

Permalink
fix(ble): fix stream close
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 29, 2018
1 parent b94eec7 commit 84be82b
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 192 deletions.
15 changes: 14 additions & 1 deletion core/network/ble/BertyDevice.h
Expand Up @@ -18,7 +18,20 @@
@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 (atomic, readwrite, assign) BOOL closedSend;
@property (atomic, readwrite, assign) BOOL closed;
@property (atomic, readwrite, assign) BOOL didRdySema;
@property (atomic, readwrite, strong) CBPeripheral *peripheral;
@property (atomic, readwrite, strong) CBService *svc;
@property (atomic, readwrite, strong) CBCharacteristic *writer;
@property (atomic, readwrite, strong) CBCharacteristic *isRdy;
@property (atomic, readwrite, strong) CBCharacteristic *closer;
@property (atomic, readwrite, strong) CBCharacteristic *accepter;
@property (atomic, readwrite, strong) dispatch_semaphore_t writeWaiter;
@property (atomic, readwrite, strong) dispatch_semaphore_t acceptWaiterSema;
@property (atomic, readwrite, strong) dispatch_semaphore_t closerWaiterSema;
@property (atomic, readwrite, strong) dispatch_semaphore_t closerSema;
@property (atomic, readwrite, strong) dispatch_semaphore_t isRdySema;
@property (atomic, readwrite, strong) dispatch_semaphore_t connSema;
@property (atomic, readwrite, strong) dispatch_semaphore_t svcSema;
@property (atomic, readwrite, strong) dispatch_semaphore_t acceptSema;
Expand Down
23 changes: 18 additions & 5 deletions core/network/ble/BertyDevice.m
Expand Up @@ -16,9 +16,17 @@ - (instancetype)initWithPeripheral:(CBPeripheral *)peripheral {
self = [super init];
self.peripheral = peripheral;
self.toSend = [[NSMutableArray alloc]init];
self.closed = NO;
self.isWaiting = NO;
self.closedSend = NO;
self.didRdySema = NO;
self.connSema = dispatch_semaphore_create(0);
self.closerWaiterSema = dispatch_semaphore_create(0);
self.acceptWaiterSema = dispatch_semaphore_create(0);
self.closerSema = dispatch_semaphore_create(0);
self.writeWaiter = dispatch_semaphore_create(0);
self.svcSema = dispatch_semaphore_create(0);
self.isRdySema = dispatch_semaphore_create(0);
self.acceptSema = dispatch_semaphore_create(0);
self.maSema = dispatch_semaphore_create(0);
self.peerIDSema = dispatch_semaphore_create(0);
Expand Down Expand Up @@ -63,9 +71,11 @@ - (void)write:(NSData *)data {
[self.toSend addObject:chunk];
} while (offset < length);
}

if (self.isWaiting == NO) {
self.isWaiting = YES;
[self.peripheral writeValue:self.toSend[0] forCharacteristic:[self getWriter] type:CBCharacteristicWriteWithResponse];
[self.peripheral writeValue:self.toSend[0] forCharacteristic:self.writer type:CBCharacteristicWriteWithResponse];
dispatch_semaphore_wait(self.writeWaiter, DISPATCH_TIME_FOREVER);
}
}

Expand All @@ -76,17 +86,20 @@ - (void)popToSend {
}
if ([self.toSend count] == 0) {
self.isWaiting = NO;
dispatch_semaphore_signal(self.writeWaiter);
}
}
}

- (void)checkAndWrite {
if (self.closed == YES && self.closedSend == NO) {
self.closedSend = YES;
[self.peripheral writeValue:[[NSData alloc] init] forCharacteristic:self.closer type:CBCharacteristicWriteWithResponse];
}
@synchronized (self.toSend) {
if ([self.toSend count] >= 1) {
@synchronized (self.toSend) {
NSData *data = self.toSend[0];
[self.peripheral writeValue:data forCharacteristic:[self getWriter] type:CBCharacteristicWriteWithResponse];
}
NSData *data = self.toSend[0];
[self.peripheral writeValue:data forCharacteristic:self.writer type:CBCharacteristicWriteWithResponse];
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions core/network/ble/ble.h
Expand Up @@ -22,38 +22,43 @@ int dialPeer(char *peerID);
char *readPeerID(char *peerID);
NSData *Bytes2NSData(void *bytes, int length);
void writeNSData(NSData *data, char *ma);
void closeConn(char *ma);
int isClosed(char *ma);

@interface BertyCentralManager : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate, CBPeripheralManagerDelegate>

@property (nonatomic, assign) BOOL serviceAdded;
@property (nonatomic, strong) NSMutableDictionary *discoveredDevice;
@property (nonatomic, strong) NSMutableDictionary *peripheralToPeerID;
@property (nonatomic, strong) NSMutableDictionary *peerIDToPeripheral;
@property (nonatomic, strong) NSMutableDictionary *bertyDevices;
@property (nonatomic, strong) NSMutableDictionary *acceptSemaphore;
@property (nonatomic, strong) NSMutableDictionary *oldDevices;
@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@property (nonatomic, strong) CBMutableService *bertyService;
@property (nonatomic, strong) CBMutableCharacteristic *acceptCharacteristic;
@property (nonatomic, strong) CBMutableCharacteristic *maCharacteristic;
@property (nonatomic, strong) CBMutableCharacteristic *peerIDCharacteristic;
@property (nonatomic, strong) CBMutableCharacteristic *writerCharacteristic;
@property (nonatomic, strong) CBMutableCharacteristic *isRdyCharacteristic;
@property (nonatomic, strong) CBMutableCharacteristic *closerCharacteristic;
@property (nonatomic, strong) NSString *ma;
@property (nonatomic, strong) NSString *peerID;
@property (nonatomic, strong) CBUUID *serviceUUID;
@property (nonatomic, strong) CBUUID *maUUID;
@property (nonatomic, strong) CBUUID *peerUUID;
@property (nonatomic, strong) CBUUID *dialUUID;
@property (nonatomic, strong) CBUUID *writerUUID;
@property (nonatomic, strong) CBUUID *isRdyUUID;
@property (nonatomic, strong) CBUUID *closerUUID;
@property (nonatomic, strong) CBUUID *acceptUUID;
@property (nonatomic, strong) NSMutableArray<NSData*>* toSend;
@property (atomic, readwrite, strong) dispatch_semaphore_t centralWaiter;

- (void)startAdvertising;
- (void)startDiscover;
- (instancetype)initWithMa:(NSString *)ma AndPeerID:(NSString *)peerID;
- (void)write:(NSData *)data forMa:(NSString *)ma;
- (char *)readPeerID:(NSString *)ma;
- (int)dialPeer:(NSString *)peerID;
- (void)close:(NSString *)ma;
- (int)isClosed:(NSString *)ma;

@end

Expand Down

0 comments on commit 84be82b

Please sign in to comment.