Skip to content

Commit

Permalink
feat(ios): improve os logging on iOS BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Jun 26, 2019
1 parent 0c1554e commit 0efb797
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
30 changes: 15 additions & 15 deletions core/network/protocol/ble/driver/BertyDevice.m
Expand Up @@ -83,15 +83,15 @@ - (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];
}

- (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];
Expand All @@ -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
}
}
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -172,7 +172,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBServi
if (service == nil) {
return;
}
os_log(OS_LOG_DEFAULT, "didModifyServices() with invalidated %@", invalidatedServices);
os_log(OS_LOG_BLE, "didModifyServices() with invalidated %@", invalidatedServices);
self.maSend = FALSE;
self.peerIDSend = FALSE;
self.maRecv = FALSE;
Expand Down
3 changes: 3 additions & 0 deletions core/network/protocol/ble/driver/BleInterface.h
Expand Up @@ -8,12 +8,15 @@

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <os/log.h>
#import <signal.h>

#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);
Expand Down
6 changes: 4 additions & 2 deletions core/network/protocol/ble/driver/BleInterface.m
Expand Up @@ -12,18 +12,20 @@
#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;
}

// 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];
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 14 additions & 13 deletions core/network/protocol/ble/driver/BleManager.m
Expand Up @@ -10,6 +10,7 @@
#import "BleManager.h"
#import "BertyDevice.h"
#import <os/log.h>
#import "BleInterface.h"

@implementation BleManager

Expand All @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -111,17 +112,17 @@ - (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];
});
}

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

0 comments on commit 0efb797

Please sign in to comment.