Skip to content

Commit

Permalink
Added logging and updated CBM version
Browse files Browse the repository at this point in the history
  • Loading branch information
DrAma999 committed Aug 7, 2020
1 parent 0650e7a commit f253851
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 44 deletions.
8 changes: 7 additions & 1 deletion LittleBlueTooth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
658A75C224BB74FC00F874EF /* CoreBluetoothTypeAliases.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_28 /* CoreBluetoothTypeAliases.swift */; };
658A75C524BB765C00F874EF /* LittleBlueTooth.h in Headers */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* LittleBlueTooth.h */; settings = {ATTRIBUTES = (Public, ); }; };
6590D38224D0030000BEE864 /* WriteWithoutResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6590D38124D0030000BEE864 /* WriteWithoutResponse.swift */; };
65A6B35024DD29000068FE1C /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65A6B34F24DD29000068FE1C /* Loggable.swift */; };
65A6B35124DD29070068FE1C /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65A6B34F24DD29000068FE1C /* Loggable.swift */; };
65AD4BD424D437B700C0CBE6 /* LittleBlueTooth.h in Headers */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* LittleBlueTooth.h */; settings = {ATTRIBUTES = (Public, ); }; };
65AD4BD624D437B700C0CBE6 /* LittleBlueToothError.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* LittleBlueToothError.swift */; };
65AD4BD724D437B700C0CBE6 /* AdvertisingData.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_15 /* AdvertisingData.swift */; };
Expand Down Expand Up @@ -89,6 +91,7 @@
655330DC24BF159D007D299B /* CentralRestorer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CentralRestorer.swift; sourceTree = "<group>"; };
655330DE24BF5446007D299B /* StateRestoration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateRestoration.swift; sourceTree = "<group>"; };
6590D38124D0030000BEE864 /* WriteWithoutResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteWithoutResponse.swift; sourceTree = "<group>"; };
65A6B34F24DD29000068FE1C /* Loggable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Loggable.swift; sourceTree = "<group>"; };
65AD4BEB24D437B700C0CBE6 /* LittleBlueToothForTest.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LittleBlueToothForTest.framework; sourceTree = BUILT_PRODUCTS_DIR; };
65AD4BEC24D437B700C0CBE6 /* LittleBlueTooth copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "LittleBlueTooth copy-Info.plist"; path = "/Users/Andrea/Documents/GitHub/LittleBlueTooth/LittleBlueTooth copy-Info.plist"; sourceTree = "<absolute>"; };
65BA2D0E24CDDE99008B4BD7 /* LittleBluetoothConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LittleBluetoothConfiguration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -185,6 +188,7 @@
OBJ_18 /* PeripheralDiscovery.swift */,
655330DC24BF159D007D299B /* CentralRestorer.swift */,
65BA2D0E24CDDE99008B4BD7 /* LittleBluetoothConfiguration.swift */,
65A6B34F24DD29000068FE1C /* Loggable.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -439,6 +443,7 @@
65AD4BD924D437B700C0CBE6 /* Peripheral.swift in Sources */,
65AD4BDA24D437B700C0CBE6 /* PeripheralDiscovery.swift in Sources */,
65AD4BDB24D437B700C0CBE6 /* CentralRestorer.swift in Sources */,
65A6B35124DD29070068FE1C /* Loggable.swift in Sources */,
65AD4BDC24D437B700C0CBE6 /* CBCentralManagerDelegateProxy.swift in Sources */,
65AD4BDD24D437B700C0CBE6 /* CBPeripheralProxy.swift in Sources */,
65AD4BDE24D437B700C0CBE6 /* ReplaySubject.swift in Sources */,
Expand All @@ -461,6 +466,7 @@
OBJ_55 /* Peripheral.swift in Sources */,
OBJ_56 /* PeripheralDiscovery.swift in Sources */,
655330DD24BF159D007D299B /* CentralRestorer.swift in Sources */,
65A6B35024DD29000068FE1C /* Loggable.swift in Sources */,
OBJ_57 /* CBCentralManagerDelegateProxy.swift in Sources */,
OBJ_58 /* CBPeripheralProxy.swift in Sources */,
OBJ_59 /* ReplaySubject.swift in Sources */,
Expand Down Expand Up @@ -1119,7 +1125,7 @@
repositoryURL = "https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock.git";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 0.9.0;
minimumVersion = 0.11.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(name: "CoreBluetoothMock",
url: "https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock.git",
.upToNextMinor(from: "0.9.0")),
.upToNextMinor(from: "0.11.0")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public struct LittleBluetoothConfiguration {
/// Handler used to manage state restoration. `Restored` object will contain the restored information
/// could be a peripheral, a scan or nothing
public var restoreHandler: ((Restored) -> Void)?
/// Enable logging, log is made using os_log and it exposes some information even in release configuration
public var isLogEnabled = false

public init() {}
}
26 changes: 26 additions & 0 deletions Sources/LittleBlueTooth/Classes/Model/Loggable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Loggable.swift
// LittleBlueTooth
//
// Created by Andrea Finollo on 07/08/2020.
//

import Foundation
import os.log

protocol Loggable {
var isLogEnabled: Bool {get set}
func log(_ message: StaticString, log: OSLog, type: OSLogType, arg: CVarArg...)
}


extension Loggable {
func log(_ message: StaticString, log: OSLog, type: OSLogType, arg: CVarArg...) {
#if !TEST
guard isLogEnabled else {
return
}
os_log(type, log: log, message, arg)
#endif
}
}
3 changes: 3 additions & 0 deletions Sources/LittleBlueTooth/Classes/Model/Peripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class Peripheral: Identifiable {
}
set {
_isLogEnabled = newValue
peripheralProxy.isLogEnabled = newValue
}
}

Expand Down Expand Up @@ -342,3 +343,5 @@ extension Peripheral: CustomDebugStringConvertible {
"""
}
}

extension Peripheral: Loggable {}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public enum BluetoothState {
self = .poweredOff
case .poweredOn:
self = .poweredOn
#if !TEST
@unknown default:
fatalError()
#endif
}
}
}
Expand Down Expand Up @@ -80,20 +82,29 @@ class CBCentralManagerDelegateProxy: NSObject {

extension CBCentralManagerDelegateProxy: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// os_log("CBCMD DidUpdateState %{public}d", log: OSLog.LittleBT_Log_General, type: .debug, central.state.rawValue)
log("CBCMD DidUpdateState %{public}d",
log: OSLog.LittleBT_Log_CentralManager,
type: .debug,
arg: central.state.rawValue)
_centralStatePublisher.send(BluetoothState(central.state))
}

/// Scan
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// os_log("CBCMD DidDiscover %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, peripheral.description)
log("CBCMD DidDiscover %{public}@",
log: OSLog.LittleBT_Log_CentralManager,
type: .debug,
arg: peripheral.description)
let peripheraldiscovery = PeripheralDiscovery(peripheral, advertisement: advertisementData, rssi: RSSI)
centralDiscoveriesPublisher.send(peripheraldiscovery)
}

/// Monitoring connection
func centralManager(_ central: CBCentralManager, didConnect: CBPeripheral) {
// os_log("CBCMD DidConnect %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, didConnect.description)
log("CBCMD DidConnect %{public}@",
log: OSLog.LittleBT_Log_CentralManager,
type: .debug,
arg: didConnect.description)
if isAutoconnectionActive {
isAutoconnectionActive = false
let event = ConnectionEvent.autoConnected(didConnect)
Expand All @@ -105,9 +116,11 @@ extension CBCentralManagerDelegateProxy: CBCentralManagerDelegate {
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral: CBPeripheral, error: Error?) {
// #if !TEST
// os_log("CBCMD DidDisconnect %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, didDisconnectPeripheral.description, error?.localizedDescription ?? "")
// #endif
log("CBCMD DidDisconnect %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_CentralManager,
type: .debug,
arg: didDisconnectPeripheral.description,
error?.localizedDescription ?? "")
isAutoconnectionActive = false
var lttlError: LittleBluetoothError?
if let error = error {
Expand All @@ -128,7 +141,10 @@ extension CBCentralManagerDelegateProxy: CBCentralManagerDelegate {
}

func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
// os_log("CBCMD WillRestoreState %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, dict.description)
log("CBCMD WillRestoreState %{public}@",
log: OSLog.LittleBT_Log_CentralManager,
type: .debug,
arg: dict.description)
_willRestoreStatePublisher.send(CentralRestorer(centralManager: central, restoredInfo: dict))
}

Expand All @@ -137,3 +153,5 @@ extension CBCentralManagerDelegateProxy: CBCentralManagerDelegate {
#endif

}

extension CBCentralManagerDelegateProxy: Loggable {}
51 changes: 42 additions & 9 deletions Sources/LittleBlueTooth/Classes/Proxies/CBPeripheralProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ class CBPeripheralDelegateProxy: NSObject {
extension CBPeripheralDelegateProxy: CBPeripheralDelegate {

func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral){
// os_log("CBPD ReadyToSendWRiteWOResp", log: OSLog.LittleBT_Log_General, type: .debug)
log("CBPD ReadyToSendWRiteWOResp",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug)
peripheralIsReadyToSendWriteWithoutResponse.send()
}

func peripheralDidUpdateName(_ peripheral: CBPeripheral) {
// os_log("CBPD DidUpdateName %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, peripheral.name ?? "na")
log("CBPD DidUpdateName %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: peripheral.name ?? "na")
peripheralChangesPublisher.send(.name(peripheral.name))
}

func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]){
// os_log("CBPD DidModifyServices %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, invalidatedServices.description)
log("CBPD DidModifyServices %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: invalidatedServices.description)
peripheralChangesPublisher.send(.invalidatedServices(invalidatedServices))
}

Expand All @@ -65,7 +73,10 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?){
// os_log("CBPD DidDiscoverServices, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, error?.localizedDescription ?? "")
log("CBPD DidDiscoverServices, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: error?.localizedDescription ?? "")
if let error = error {
peripheralDiscoveredServicesPublisher.send((nil,.serviceNotFound(error)))
} else {
Expand All @@ -74,7 +85,11 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverIncludedServicesFor service: CBService, error: Error?) {
// os_log("CBPD DidDiscoverIncludedServices %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, service.description, error?.localizedDescription ?? "")
log("CBPD DidDiscoverIncludedServices %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: service.description,
error?.localizedDescription ?? "")
if let error = error {
peripheralDiscoveredIncludedServicesPublisher.send((service, error))
} else {
Expand All @@ -83,7 +98,11 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?){
// os_log("CBPD DidDiscoverCharacteristic %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, service.description, error?.localizedDescription ?? "")
log("CBPD DidDiscoverCharacteristic %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: service.description,
error?.localizedDescription ?? "")
if let error = error {
peripheralDiscoveredCharacteristicsForServicePublisher.send((service, .characteristicNotFound(error)))
} else {
Expand All @@ -92,7 +111,11 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?){
// os_log("CBPD DidUpdateValue %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, characteristic.description, error?.localizedDescription ?? "")
log("CBPD DidUpdateValue %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: characteristic.description,
error?.localizedDescription ?? "")
if let error = error {
peripheralUpdatedValueForCharacteristicPublisher.send((characteristic, .couldNotReadFromCharacteristic(characteristic: characteristic.uuid, error: error)))
} else {
Expand All @@ -105,7 +128,11 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
// os_log("CBPD DidWriteValue %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, characteristic.description, error?.localizedDescription ?? "")
log("CBPD DidWriteValue %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: characteristic.description,
error?.localizedDescription ?? "")
if let error = error {
peripheralWrittenValueForCharacteristicPublisher.send((characteristic, .couldNotWriteFromCharacteristic(characteristic: characteristic.uuid, error: error)))
} else {
Expand All @@ -114,7 +141,11 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?){
// os_log("CBPD DidUpdateNotifState %{public}@, Error %{public}@", log: OSLog.LittleBT_Log_General, type: .debug, characteristic.description, error?.localizedDescription ?? "")
log("CBPD DidUpdateNotifState %{public}@, Error %{public}@",
log: OSLog.LittleBT_Log_Peripheral,
type: .debug,
arg: characteristic.description,
error?.localizedDescription ?? "")
if let error = error {
peripheralUpdatedNotificationStateForCharacteristicPublisher.send((characteristic, .couldNotUpdateListenState(characteristic: characteristic.uuid, error: error)))
} else {
Expand All @@ -127,3 +158,5 @@ extension CBPeripheralDelegateProxy: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?){}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?){}
}

extension CBPeripheralDelegateProxy: Loggable {}
Loading

0 comments on commit f253851

Please sign in to comment.