From 0efb7979965276c770a76bbed91c41620299e7a2 Mon Sep 17 00:00:00 2001 From: aeddi Date: Wed, 26 Jun 2019 17:43:06 +0200 Subject: [PATCH] feat(ios): improve os logging on iOS BLE --- .../network/protocol/ble/driver/BertyDevice.m | 30 +++++++++---------- .../protocol/ble/driver/BleInterface.h | 3 ++ .../protocol/ble/driver/BleInterface.m | 6 ++-- core/network/protocol/ble/driver/BleManager.m | 27 +++++++++-------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/core/network/protocol/ble/driver/BertyDevice.m b/core/network/protocol/ble/driver/BertyDevice.m index a825e0143f..b6de6090b4 100644 --- a/core/network/protocol/ble/driver/BertyDevice.m +++ b/core/network/protocol/ble/driver/BertyDevice.m @@ -83,7 +83,7 @@ - (instancetype)initWithPeripheral:(CBPeripheral *)peripheral - (void)handleMa:(NSData *)maData { NSString *remoteMa = [NSString stringWithUTF8String:[maData bytes]]; - os_log(OS_LOG_DEFAULT, "handlePeerID() device %@ with current Ma %@, new Ma %@", [self.peripheral.identifier UUIDString], self.remoteMa, remoteMa); + os_log(OS_LOG_BLE, "handlePeerID() device %@ with current Ma %@, new Ma %@", [self.peripheral.identifier UUIDString], self.remoteMa, remoteMa); self.remoteMa = [NSString stringWithUTF8String:[maData bytes]]; self.maRecv = TRUE; [self checkAndHandleFoundPeer]; @@ -91,7 +91,7 @@ - (void)handleMa:(NSData *)maData { - (void)handlePeerID:(NSData *)peerIDData { NSString *remotePeerID = [NSString stringWithUTF8String:[peerIDData bytes]]; - os_log(OS_LOG_DEFAULT, "handlePeerID() device %@ with current peerID %@, new peerID %@", [self.peripheral.identifier UUIDString], self.remotePeerID, remotePeerID); + os_log(OS_LOG_BLE, "handlePeerID() device %@ with current peerID %@, new peerID %@", [self.peripheral.identifier UUIDString], self.remotePeerID, remotePeerID); self.remotePeerID = remotePeerID; self.peerIDRecv = TRUE; [self checkAndHandleFoundPeer]; @@ -100,9 +100,9 @@ - (void)handlePeerID:(NSData *)peerIDData { - (void)checkAndHandleFoundPeer { if (self.maSend == TRUE && self.peerIDSend == TRUE && self.maRecv == TRUE && self.peerIDRecv == TRUE) { - os_log(OS_LOG_DEFAULT, "checkAndHandleFoundPeer() handling found peer %@", [self.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "checkAndHandleFoundPeer() handling found peer %@", [self.peripheral.identifier UUIDString]); if (!handlePeerFound([self.remotePeerID UTF8String], [self.remoteMa UTF8String])) { - os_log_error(OS_LOG_DEFAULT, "checkAndHandleFoundPeer() failed: golang can't handle new peer %@", [self.peripheral.identifier UUIDString]); + os_log_error(OS_LOG_BLE, "checkAndHandleFoundPeer() failed: golang can't handle new peer %@", [self.peripheral.identifier UUIDString]); //TODO: Disconnect device } } @@ -113,26 +113,26 @@ - (void)handshake { [self connectWithOptions:nil withBlock:^(BertyDevice* device, NSError *error){ if (error) { - os_log_error(OS_LOG_DEFAULT, "handshake() device %@ connection failed %@", [device.peripheral.identifier UUIDString], error); + os_log_error(OS_LOG_BLE, "handshake() device %@ connection failed %@", [device.peripheral.identifier UUIDString], error); return; } - os_log(OS_LOG_DEFAULT, "handshake() device %@ connection succeed", [device.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "handshake() device %@ connection succeed", [device.peripheral.identifier UUIDString]); [self discoverServices:@[self.manager.serviceUUID] withBlock:^(NSArray *services, NSError *error) { if (error) { - os_log_error(OS_LOG_DEFAULT, "handshake() device %@ discover service failed %@", [device.peripheral.identifier UUIDString], error); + os_log_error(OS_LOG_BLE, "handshake() device %@ discover service failed %@", [device.peripheral.identifier UUIDString], error); return; } - os_log(OS_LOG_DEFAULT, "handshake() device %@ service discover succeed", [device.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "handshake() device %@ service discover succeed", [device.peripheral.identifier UUIDString]); CBService *service = getService(services, [self.manager.serviceUUID UUIDString]); if (service == nil) { return; } [self discoverCharacteristics:@[self.manager.maUUID, self.manager.peerUUID, self.manager.writerUUID, self.manager.closerUUID,] forService:service withBlock:^(NSArray *chars, NSError *error) { if (error) { - os_log_error(OS_LOG_DEFAULT, "handshake() device %@ discover characteristic failed %@", [device.peripheral.identifier UUIDString], error); + os_log_error(OS_LOG_BLE, "handshake() device %@ discover characteristic failed %@", [device.peripheral.identifier UUIDString], error); return; } - os_log(OS_LOG_DEFAULT, "handshake() device %@ discover characteristic succeed", [device.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "handshake() device %@ discover characteristic succeed", [device.peripheral.identifier UUIDString]); for (CBCharacteristic *chr in chars) { if ([chr.UUID isEqual:self.manager.maUUID]) { self.ma = chr; @@ -145,17 +145,17 @@ - (void)handshake { [self writeToCharacteristic:[[self.manager.ma dataUsingEncoding:NSUTF8StringEncoding] mutableCopy] forCharacteristic:self.ma withEOD:TRUE andBlock:^(NSError *error) { if (error) { - os_log_error(OS_LOG_DEFAULT, "handshake() device %@ write Ma failed %@", [device.peripheral.identifier UUIDString], error); + os_log_error(OS_LOG_BLE, "handshake() device %@ write Ma failed %@", [device.peripheral.identifier UUIDString], error); return; } - os_log(OS_LOG_DEFAULT, "handshake() device %@ write Ma succeed", [device.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "handshake() device %@ write Ma succeed", [device.peripheral.identifier UUIDString]); self.maSend = TRUE; [self writeToCharacteristic:[[self.manager.peerID dataUsingEncoding:NSUTF8StringEncoding] mutableCopy] forCharacteristic:self.peerID withEOD:TRUE andBlock:^(NSError *error) { if (error) { - os_log_error(OS_LOG_DEFAULT, "handshake() device %@ write peerID failed %@", [device.peripheral.identifier UUIDString], error); + os_log_error(OS_LOG_BLE, "handshake() device %@ write peerID failed %@", [device.peripheral.identifier UUIDString], error); return; } - os_log(OS_LOG_DEFAULT, "handshake() device %@ write peerID succeed", [device.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "handshake() device %@ write peerID succeed", [device.peripheral.identifier UUIDString]); self.peerIDSend = TRUE; [self checkAndHandleFoundPeer]; @@ -172,7 +172,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray #import +#import #import #ifndef BleInterface_h #define BleInterface_h @class BleManager; +extern os_log_t OS_LOG_BLE; + unsigned short StartBleDriver(char *ma, char *peerID); unsigned short StopBleDriver(void); unsigned short DialDevice(char *ma); diff --git a/core/network/protocol/ble/driver/BleInterface.m b/core/network/protocol/ble/driver/BleInterface.m index 45b41433f7..869383e4cd 100644 --- a/core/network/protocol/ble/driver/BleInterface.m +++ b/core/network/protocol/ble/driver/BleInterface.m @@ -12,11 +12,12 @@ #import "BertyDevice.h" static BleManager *manager = nil; +os_log_t OS_LOG_BLE = nil; BleManager* getManager(void) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - os_log(OS_LOG_DEFAULT, "getManager() initialize!"); + os_log(OS_LOG_BLE, "getManager() initialize!"); manager = [[BleManager alloc] initScannerAndAdvertiser]; }); return manager; @@ -24,6 +25,7 @@ // TODO: Check if init failed unsigned short StartBleDriver(char *ma, char *peerID) { + OS_LOG_BLE = os_log_create("chat.berty.io.core.network.protocol.ble.driver", "CoreModule"); [getManager() setMa:[NSString stringWithUTF8String:ma]]; [getManager() setPeerID:[NSString stringWithUTF8String:peerID]]; [getManager() startScanning]; @@ -61,7 +63,7 @@ unsigned short SendToDevice(char *ma, NSData *data) { return 1; } - os_log_error(OS_LOG_DEFAULT, "writeNSData() no device found can't write"); + os_log_error(OS_LOG_BLE, "writeNSData() no device found can't write"); return 0; } diff --git a/core/network/protocol/ble/driver/BleManager.m b/core/network/protocol/ble/driver/BleManager.m index 6ba9b9af1f..e9d5be6f52 100644 --- a/core/network/protocol/ble/driver/BleManager.m +++ b/core/network/protocol/ble/driver/BleManager.m @@ -10,6 +10,7 @@ #import "BleManager.h" #import "BertyDevice.h" #import +#import "BleInterface.h" @implementation BleManager @@ -27,7 +28,7 @@ @implementation BleManager // TODO: No need to check error on this? - (instancetype __nonnull) initScannerAndAdvertiser { - os_log(OS_LOG_DEFAULT, "peripheralManager: initScannerAndAdvertiser"); + os_log(OS_LOG_BLE, "peripheralManager: initScannerAndAdvertiser"); self = [super init]; if (self) { @@ -55,7 +56,7 @@ - (instancetype __nonnull) initScannerAndAdvertiser { } - (void)initService { - os_log(OS_LOG_DEFAULT, "peripheralManager: initService"); + os_log(OS_LOG_BLE, "peripheralManager: initService"); self.serviceUUID = [CBUUID UUIDWithString:SERVICE_UUID]; self.maUUID = [CBUUID UUIDWithString:MA_UUID]; self.peerUUID = [CBUUID UUIDWithString:PEER_ID_UUID]; @@ -111,7 +112,7 @@ - (void)startAdvertising { - (void)addService { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - os_log(OS_LOG_DEFAULT, "peripheralManager: AddService: %@", [self.serviceUUID UUIDString]); + os_log(OS_LOG_BLE, "peripheralManager: AddService: %@", [self.serviceUUID UUIDString]); [self.statusCount await]; [self.pManager addService:self.bertyService]; }); @@ -119,9 +120,9 @@ - (void)addService { - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error { if (error) { - os_log(OS_LOG_DEFAULT, "didAddService() error: %@", [error localizedFailureReason]); + os_log(OS_LOG_BLE, "didAddService() error: %@", [error localizedFailureReason]); } - os_log(OS_LOG_DEFAULT, "peripheralManager: didAddService: %@", [service.UUID UUIDString]); + os_log(OS_LOG_BLE, "peripheralManager: didAddService: %@", [service.UUID UUIDString]); } #pragma mark - BertyDevice dict helper @@ -147,7 +148,7 @@ - (BertyDevice *)findPeripheral:(CBPeripheral *)peripheral { - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { _BERTY_ON_M_THREAD(^{ - os_log(OS_LOG_DEFAULT, "didConnectPeripheral() %@", [peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "didConnectPeripheral() %@", [peripheral.identifier UUIDString]); BertyDevice *d = [self findPeripheral:peripheral]; [d handleConnect:nil]; }); @@ -156,7 +157,7 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { BertyDevice *d = [self findPeripheral:peripheral]; [d handleConnect:error]; - os_log(OS_LOG_DEFAULT, "didFailToConnectPeripheral() %@", [peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "didFailToConnectPeripheral() %@", [peripheral.identifier UUIDString]); } - (void)centralManager:(CBCentralManager *)central @@ -169,14 +170,14 @@ - (void)centralManager:(CBCentralManager *)central @synchronized (self.bDevices) { [self.bDevices addObject:nDevice]; } - os_log(OS_LOG_DEFAULT, "didDiscoverPeripheral() device %@ added to BleManager.bDevices", [nDevice.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "didDiscoverPeripheral() device %@ added to BleManager.bDevices", [nDevice.peripheral.identifier UUIDString]); [nDevice handshake]; } }); } - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { - os_log(OS_LOG_DEFAULT, "didDisconnectPeripheral() for device %@ with error %@", [peripheral.identifier UUIDString], error); + os_log(OS_LOG_BLE, "didDisconnectPeripheral() for device %@ with error %@", [peripheral.identifier UUIDString], error); BertyDevice *nDevice = [self findPeripheral:peripheral]; @synchronized (self.bDevices) { [self.bDevices removeObject:nDevice]; @@ -222,7 +223,7 @@ - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)periphera break; } } - os_log(OS_LOG_DEFAULT, "peripheralManagerDidUpdateState: %@", stateString); + os_log(OS_LOG_BLE, "peripheralManagerDidUpdateState: %@", stateString); } - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central { @@ -254,7 +255,7 @@ - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central { } } - os_log(OS_LOG_DEFAULT, "centralManagerDidUpdateState: %@", stateString); + os_log(OS_LOG_BLE, "centralManagerDidUpdateState: %@", stateString); } - (BertyDevice *)findPeripheralFromIdentifier:(NSUUID *__nonnull)identifier { @@ -292,7 +293,7 @@ - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteReque // check if we hold a remote device of this type BertyDevice *remote = [self findPeripheralFromIdentifier:request.central.identifier]; if (remote == nil) { - os_log(OS_LOG_DEFAULT, "didReceiveWriteRequests() failed peer unknown"); + os_log(OS_LOG_BLE, "didReceiveWriteRequests() failed peer unknown"); // TODO: Add error HERE [peripheral respondToRequest:request withResult:CBATTErrorInsufficientAuthorization]; return; @@ -305,7 +306,7 @@ - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteReque // check if final data was received // NSLog(@"request ACTUALDATA=%@ VAL=%@ UUID=%@ P=%p", data, request.value, request.characteristic.UUID, data); if ([request.characteristic.UUID isEqual:self.writerUUID]) { - os_log(OS_LOG_DEFAULT, "didReceiveWriteRequests() writer called for device %@", [remote.peripheral.identifier UUIDString]); + os_log(OS_LOG_BLE, "didReceiveWriteRequests() writer called for device %@", [remote.peripheral.identifier UUIDString]); void(^handler)(NSData *) = [remote.characteristicHandlers objectForKey:[request.characteristic.UUID UUIDString]]; unsigned char zeroByte = 0; NSMutableData *tmpData = [NSMutableData dataWithData:request.value];