Skip to content

Commit

Permalink
feat(ios): add logger + refacto logs
Browse files Browse the repository at this point in the history
Signed-off-by: D4ryl00 <d4ryl00@gmail.com>
  • Loading branch information
D4ryl00 committed Dec 15, 2021
1 parent 840b642 commit b091582
Show file tree
Hide file tree
Showing 14 changed files with 566 additions and 324 deletions.
18 changes: 10 additions & 8 deletions go/pkg/ble-driver/BertyDevice_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#import "BleManager_darwin.h"
#import "ConnectedPeer.h"
#import "CircularQueue.h"
#import "BleQueue.h"
#import "Logger.h"
#import "CountDownLatch_darwin.h"

#ifndef BertyDevice_h
#define BertyDevice_h
@class BertyDevice;
@class ConnectedPeer;
NS_ASSUME_NONNULL_BEGIN

@class BleManager;

typedef void (^BertyDeviceConnectCallbackBlockType)(BertyDevice * __nullable, NSError * __nullable);
typedef void (^BertyDeviceServiceCallbackBlockType)(NSArray * __nullable, NSError * __nullable);
Expand All @@ -26,6 +27,7 @@ typedef void (^BertyDeviceWriteCallbackBlockType)(NSError * __nullable);

@interface BertyDevice : NSObject <CBPeripheralDelegate, NSStreamDelegate>

@property (nonatomic, strong, nonnull) Logger *logger;
@property (nonatomic, strong, nonnull) NSString *name;
@property (nonatomic, strong, nonnull) NSDictionary *serviceDict;
@property (nonatomic, strong, nullable) CBPeripheral *peripheral;
Expand All @@ -48,8 +50,8 @@ typedef void (^BertyDeviceWriteCallbackBlockType)(NSError * __nullable);
@property (nonatomic, strong, nonnull) CircularQueue *dataCache;
@property (readwrite) BOOL isDisconnecting;

- (instancetype __nullable)initWithIdentifier:(NSString *__nonnull)identifier central:(BleManager *__nonnull)manager asClient:(BOOL)client;
- (instancetype __nullable)initWithPeripheral:(CBPeripheral *__nonnull)peripheral central:(BleManager *__nonnull)manager withName:(NSString *__nonnull)name;
- (instancetype __nullable)initWithIdentifier:(NSString *__nonnull)identifier logger:(Logger *__nonnull)logger central:(BleManager *__nonnull)manager asClient:(BOOL)client;
- (instancetype __nullable)initWithPeripheral:(CBPeripheral *__nonnull)peripheral logger:(Logger *__nonnull)logger central:(BleManager *__nonnull)manager withName:(NSString *__nonnull)name;
- (void)closeBertyDevice;
- (BOOL)writeToCharacteristic:(NSData *__nonnull)data forCharacteristic:(CBCharacteristic *__nonnull)characteristic withEOD:(BOOL)eod;
- (void)handshake;
Expand Down Expand Up @@ -80,4 +82,4 @@ API_AVAILABLE(ios(11.0))
- (BOOL)l2capWrite:(NSData *__nonnull)data;

@end
#endif
NS_ASSUME_NONNULL_END
284 changes: 150 additions & 134 deletions go/pkg/ble-driver/BertyDevice_darwin.m

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions go/pkg/ble-driver/BleInterface_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
#import <os/log.h>
#import <signal.h>

#import "BleManager_darwin.h"
#import "Logger.h"

#ifndef BleInterface_h
#define BleInterface_h
@class BleManager;

#define LOCAL_DOMAIN "tech.berty.bty.BLE"
extern os_log_t OS_LOG_BLE;

void BLEStart(char *localPID);
void BLEStop(void);
Expand All @@ -26,5 +25,7 @@ void BLECloseConnWithPeer(char *remotePID);
int BLEBridgeHandleFoundPeer(NSString *remotePID);
void BLEBridgeHandleLostPeer(NSString *remotePID);
void BLEBridgeReceiveFromPeer(NSString *remotePID, NSData *payload);
void BLEBridgeLog(enum level level, NSString *message);
void BLEUseExternalLogger(void);

#endif /* BleInterface_h */
58 changes: 40 additions & 18 deletions go/pkg/ble-driver/BleInterface_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,50 @@
// Copyright © 2018 sacha. All rights reserved.
//

#import <os/log.h>
#import "BleInterface_darwin.h"
#import "BertyDevice_darwin.h"
#import "ConnectedPeer.h"

// This functions are Go functions so they aren't defined here
extern int BLEHandleFoundPeer(char *);
extern void BLEHandleLostPeer(char *);
extern void BLEReceiveFromPeer(char *, void *, unsigned long);
extern void BLELog(enum level level, const char *message);

os_log_t OS_LOG_BLE = nil;
static BleManager *manager = nil;
BOOL useExternalLogger = FALSE;

void handleException(NSException* exception) {
os_log_error(OS_LOG_BLE, "🔴 Unhandled exception %{public}@", exception);
NSLog(@"Unhandled exception %@", exception);
}

BleManager* getManager(void) {
static BleManager *manager = nil;

@synchronized([BleManager class])
{
if(!manager) {
os_log(OS_LOG_BLE, "BleManager: initialization");
manager = [[BleManager alloc] initScannerAndAdvertiser];
NSLog(@"BleManager: initialization");
manager = [[BleManager alloc] initDriver:useExternalLogger];
}
}
return manager;
}

void releaseManager(void) {
@synchronized([BleManager class])
{
if(manager) {
NSLog(@"releaseManager");
[manager release];
}
}
}

#pragma mark - incoming API functions

void BLEStart(char *localPID) {
OS_LOG_BLE = os_log_create(LOCAL_DOMAIN, "protocol");
os_log_debug(OS_LOG_BLE, "🟢 BLEStart()");
NSLog(@"BLEStart called");
@autoreleasepool {
NSString *localPIDString = [NSString stringWithUTF8String:localPID];
[getManager() setLocalPID:localPIDString];
[getManager().logger i:@"BLEStart: pid=%@", [getManager().logger SensitiveNSObject:localPIDString]];
[getManager() setID:[localPIDString substringWithRange:NSMakeRange([localPIDString length] - 4, 4)]];
[getManager() startScanning];
[getManager() startAdvertising];
Expand All @@ -51,10 +60,11 @@ void BLEStart(char *localPID) {

// TODO: Implement this, check if error
void BLEStop(void) {
os_log_debug(OS_LOG_BLE, "🟢 BLEStop()");
[getManager().logger i:@"BLEStop"];
[getManager() stopScanning];
[getManager() stopAdvertising];
[getManager() closeAllConnections];
releaseManager();
}

int BLESendToPeer(char *remotePID, void *payload, int length) {
Expand All @@ -65,12 +75,12 @@ int BLESendToPeer(char *remotePID, void *payload, int length) {

BertyDevice *bDevice = [getManager() findPeripheralFromPID:cPID];
if (bDevice == nil) {
os_log_error(OS_LOG_BLE, "BLESendToPeer error: no device found");
[getManager().logger e:@"BLESendToPeer error: no device found"];
return 0;
}

if (bDevice.peer == nil) {
os_log_error(OS_LOG_BLE, "BLESendToPeer error: peer object not found");
[getManager().logger e:@"BLESendToPeer error: peer object not found"];
return 0;
}

Expand All @@ -82,7 +92,7 @@ int BLESendToPeer(char *remotePID, void *payload, int length) {
} else if ([bDevice.peer isServerReady]) {
status = [getManager() writeAndNotify:bDevice data:cPayload];
} else {
os_log_error(OS_LOG_BLE, "BLESendToPeer error: device not connected");
[getManager().logger e:@"BLESendToPeer error: device not connected"];
}
}

Expand All @@ -101,15 +111,22 @@ int BLEDialPeer(char *remotePID) {

// TODO: Implement this
void BLECloseConnWithPeer(char *remotePID) {
os_log_error(OS_LOG_BLE, "🟢 BLECloseConnWithPeer()");
[getManager().logger i:@"BLECloseConnWithPeer called: remotePID=%@", [getManager().logger SensitiveString:remotePID]];
BertyDevice *bDevice = [getManager() findPeripheralFromPID:[NSString stringWithUTF8String:remotePID]];
if (bDevice != nil) {
[getManager() disconnect:bDevice];
}
}


// Use BLEBridgeLog to write logs to the external logger
void BLEUseExternalLogger(void) {
useExternalLogger = TRUE;
}

#pragma mark - outgoing API functions

int BLEBridgeHandleFoundPeer(NSString *remotePID) {
os_log_debug(OS_LOG_BLE, "BLEBridgeHandleFoundPeer called");
char *cPID = (char *)[remotePID UTF8String];
if (BLEHandleFoundPeer(cPID)) {
return (1);
Expand All @@ -118,7 +135,6 @@ int BLEBridgeHandleFoundPeer(NSString *remotePID) {
}

void BLEBridgeHandleLostPeer(NSString *remotePID) {
os_log_debug(OS_LOG_BLE, "BLEBridgeHandleLostPeer called");
char *cPID = (char *)[remotePID UTF8String];
BLEHandleLostPeer(cPID);
}
Expand All @@ -129,3 +145,9 @@ void BLEBridgeReceiveFromPeer(NSString *remotePID, NSData *payload) {
int length = (int)[payload length];
BLEReceiveFromPeer(cPID, cPayload, length);
}

// Write logs to the external logger
void BLEBridgeLog(enum level level, NSString *message) {
char *cMessage = (char *)[message UTF8String];
BLELog(level, cMessage);
}
16 changes: 10 additions & 6 deletions go/pkg/ble-driver/BleManager_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "CountDownLatch_darwin.h"

#import "BleInterface_darwin.h"
#import "BertyDevice_darwin.h"
#import "PeerManager.h"
#import "CountDownLatch_darwin.h"
#import "WriteDataCache.h"

#ifndef BleManager_h
#define BleManager_h
#define LOCAL_DOMAIN "tech.berty.bty"

@class BertyDevice;
NS_ASSUME_NONNULL_BEGIN

@interface BleManager : NSObject <CBPeripheralManagerDelegate, CBCentralManagerDelegate>

Expand All @@ -25,6 +27,8 @@
+ (NSString *__nonnull)NSDataToHex:(NSData *__nonnull)data;
+ (void) printLongLog:(NSString *__nonnull)message;

@property (nonatomic, strong, nullable) Logger *logger;
@property (nonatomic, strong, nonnull) PeerManager *peerManager;
@property (readwrite) BOOL pmEnable;
@property (readwrite) BOOL cmEnable;
@property (readwrite) int psm;
Expand All @@ -48,7 +52,7 @@
@property (nonatomic, strong, nullable) CountDownLatch *writerLactch;
@property (readwrite) BOOL writeStatus;

- (instancetype __nonnull) initScannerAndAdvertiser;
- (instancetype __nonnull) initDriver:(BOOL)useExternalLogger;
- (void)addService;
- (void)startScanning;
- (void)toggleScanner:(NSTimer *__nonnull)timer;
Expand All @@ -63,4 +67,4 @@

@end

#endif
NS_ASSUME_NONNULL_END

0 comments on commit b091582

Please sign in to comment.