// // CentralController.swift // CovidSafe // // Copyright © 2020 Australian Government. All rights reserved. // import Foundation import CoreData import CoreBluetooth import UIKit struct CentralWriteData: Codable { var modelC: String // phone model of central var rssi: Double var txPower: Double? var msg: String // tempID var org: String var v: Int } class CentralController: NSObject { enum CentralError: Error { case centralAlreadyOn case centralAlreadyOff } var centralDidUpdateStateCallback: ((CBManagerState) -> Void)? var characteristicDidReadValue: ((EncounterRecord) -> Void)? private let restoreIdentifierKey = "com.joelkek.tracer.central" private var central: CBCentralManager? private var recoveredPeripherals: [CBPeripheral] = [] private var queue: DispatchQueue // This dict is to keep track of discovered android devices, so that i do not connect to the same android device multiple times within the same BluetraceConfig.CentralScanInterval private var discoveredAndroidPeriManufacturerToUUIDMap = [Data: UUID]() // This dict has 2 purpose // 1. To store all the EncounterRecord, because the RSSI and TxPower is gotten at the didDiscoverPeripheral delegate, but the characterstic value is gotten at didUpdateValueForCharacteristic delegate // 2. Use to check for duplicated iphones peripheral being discovered, so that i dont connect to the same iphone again in the same scan window private var scannedPeripherals = [UUID: (peripheral: CBPeripheral, encounter: EncounterRecord)]() // stores the peripherals encountered within one scan interval var timerForScanning: Timer? public init(queue: DispatchQueue) { self.queue = queue super.init() } func turnOn() { DLog("CC requested to be turnOn") guard central == nil else { return } central = CBCentralManager(delegate: self, queue: self.queue, options: [CBCentralManagerOptionRestoreIdentifierKey: restoreIdentifierKey, CBCentralManagerOptionShowPowerAlertKey: 1]) } func turnOff() { DLog("CC turnOff") guard central != nil else { return } central?.stopScan() central = nil } public func getState() -> CBManagerState? { return central?.state } public func getDiscoveredPeripheralsCount() -> Int { let COUNT_NOT_FOUND = -1 guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return COUNT_NOT_FOUND } let managedContext = appDelegate.persistentContainer.viewContext let fetchRequest = NSFetchRequest(entityName: "Encounter") let sortByDate = NSSortDescriptor(key: "timestamp", ascending: false) fetchRequest.sortDescriptors = [sortByDate] let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedContext, sectionNameKeyPath: nil, cacheName: nil) do { try fetchedResultsController.performFetch() return fetchedResultsController.fetchedObjects?.count ?? COUNT_NOT_FOUND } catch let error as NSError { print("Could not perform fetch. \(error), \(error.userInfo)") return COUNT_NOT_FOUND } } } extension CentralController: CBCentralManagerDelegate { func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) { } func centralManagerDidUpdateState(_ central: CBCentralManager) { centralDidUpdateStateCallback?(central.state) switch central.state { case .poweredOn: DispatchQueue.main.async { self.timerForScanning = Timer.scheduledTimer(withTimeInterval: TimeInterval(BluetraceConfig.CentralScanInterval), repeats: true) { _ in DLog("CC Starting a scan") Encounter.timestamp(for: .scanningStarted) // for all peripherals that are not disconnected, disconnect them self.scannedPeripherals.forEach { (scannedPeri) in central.cancelPeripheralConnection(scannedPeri.value.peripheral) } // clear all peripherals, such that a new scan window can take place self.scannedPeripherals = [UUID: (CBPeripheral, EncounterRecord)]() self.discoveredAndroidPeriManufacturerToUUIDMap = [Data: UUID]() central.scanForPeripherals(withServices: [BluetraceConfig.BluetoothServiceID]) DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(BluetraceConfig.CentralScanDuration)) { DLog("CC Stopping a scan") central.stopScan() Encounter.timestamp(for: .scanningStopped) } } self.timerForScanning?.fire() } default: timerForScanning?.invalidate() } } func handlePeripheralOfUncertainStatus(_ peripheral: CBPeripheral) { // If not connected to Peripheral, attempt connection and exit if peripheral.state != .connected { DLog("CC handlePeripheralOfUncertainStatus not connected") central?.connect(peripheral) return } // If don't know about Peripheral's services, discover services and exit if peripheral.services == nil { DLog("CC handlePeripheralOfUncertainStatus unknown services") peripheral.discoverServices([BluetraceConfig.BluetoothServiceID]) return } // If Peripheral's services don't contain targetID, disconnect and remove, then exit. // If it does contain targetID, discover characteristics for service guard let service = peripheral.services?.first(where: { $0.uuid == BluetraceConfig.BluetoothServiceID }) else { DLog("CC handlePeripheralOfUncertainStatus no matching Services") central?.cancelPeripheralConnection(peripheral) return } DLog("CC handlePeripheralOfUncertainStatus discoverCharacteristics") peripheral.discoverCharacteristics([BluetraceConfig.BluetoothServiceID], for: service) // If Peripheral's service's characteristics don't contain targetID, disconnect and remove, then exit. // If it does contain targetID, read value for characteristic guard let characteristic = service.characteristics?.first(where: { $0.uuid == BluetraceConfig.BluetoothServiceID}) else { DLog("CC handlePeripheralOfUncertainStatus no matching Characteristics") central?.cancelPeripheralConnection(peripheral) return } DLog("CC handlePeripheralOfUncertainStatus readValue") peripheral.readValue(for: characteristic) return } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { let debugLogs = ["CentralState": BluetraceUtils.centralStateToString(central.state), "peripheral": peripheral, "advertisments": advertisementData as AnyObject] as AnyObject DLog("\(debugLogs)") // iphones will "mask" the peripheral's identifier for android devices, resulting in the same android device being discovered multiple times with different peripheral identifier. Hence Android is using use CBAdvertisementDataServiceDataKey data for identifying an android pheripheral if let manuData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data { let androidIdentifierData = manuData.subdata(in: 2..