                 }
             }
                 
-            catch { log?("Error waiting for new connection: \(error)") }
+            catch { log("Error waiting for new connection: \(error)") }
         }
         
-        log?("Stopped GATT Server")
-    }
-    
-    @inline(__always)
+        log("Stopped GATT Server")
+    }
+    
     private func newConnectionID() -> UInt {
-        
         lastConnectionID += 1
-        
         return lastConnectionID
     }
     
-    private func disconnect(_ connection: UInt, error: Error) {
-        
+    private func disconnect(_ connection: UInt, error: Error) async {
         // remove from peripheral, release and close socket
-        connectionsQueue.sync { [unowned self] in
-            self.connections[connection] = nil
-        }
-        
+        self.connections[connection] = nil
         // enable LE advertising
-        do { try controller.enableLowEnergyAdvertising() }
+        do { try await hostController.enableLowEnergyAdvertising() }
         catch HCIError.commandDisallowed { /* ignore */ }
-        catch { log?("Could not enable advertising. \(error)") }
-    }
-    
-    @inline(__always)
-    private func writeDatabase <T> (_ block: (inout GATTDatabase) -> (T)) -> T {
-        
-        return databaseQueue.sync { block(&self.database) }
-    }
-    
-    @inline(__always)
-    private func readConnection(_ block: () -> ()) {
-        
-        return readQueue.sync(execute: block)
+        catch { log("Could not enable advertising. \(error)") }
     }
 }
 

//
//  Created by Alsey Coleman Miller on 7/18/18.
//
/*
#if canImport(BluetoothGATT) && canImport(BluetoothHCI)
import Foundation
import Dispatch
@_exported import Bluetooth
@_exported import BluetoothGATT
@_exported import BluetoothHCI

@available(macOS 10.12, iOS 10.0, *)
public final class GATTPeripheral <HostController: BluetoothHostControllerInterface, L2CAPSocket: L2CAPSocketProtocol>: PeripheralManager {
    
    // MARK: - Properties
    
    public var log: ((String) -> ())?
    
    public let options: GATTPeripheralOptions
    
    public let controller: HostController
    
    public var willRead: ((GATTReadRequest<Central>) -> ATTError?)?
    
    public var willWrite: ((GATTWriteRequest<Central>) -> ATTError?)?
    
    public var didWrite: ((GATTWriteConfirmation<Central>) -> ())?
    
    public var newConnection: (() throws -> (socket: L2CAPSocket, central: Central))?
    
    public var activeConnections: [Central] {
        
        return connectionsQueue.sync { [unowned self] in
            self.connections.values.map { $0.central }
        }
    }
    
    // MARK: - Private Properties
    
    internal lazy var databaseQueue: DispatchQueue = DispatchQueue(label: "\(self) Database Queue")
    
    internal lazy var readQueue: DispatchQueue = DispatchQueue(label: "\(self) Read Queue")
    
    internal lazy var connectionsQueue: DispatchQueue = DispatchQueue(label: "\(self) Connections Queue")
    
    internal private(set) var database = GATTDatabase()
    
    internal private(set) var isServerRunning = false
    
    internal private(set) var connections = [UInt: GATTServerConnection<L2CAPSocket>]()
    
    private var serverThread: Thread?
    
    private var lastConnectionID: UInt = 0
    
    // MARK: - Initialization
    
    public init(controller: HostController,
                options: GATTPeripheralOptions = GATTPeripheralOptions()) {
        
        self.controller = controller
        self.options = options
    }
    
    // MARK: - Methods
    
    public func start() throws {
        
        guard isServerRunning == false else { return }
        
        // enable advertising
        do { try controller.enableLowEnergyAdvertising() }
        catch HCIError.commandDisallowed { /* ignore */ }
        
        /*
         let serverSocket = try L2CAPSocket.lowEnergyServer(controllerAddress: controller.address,
         isRandom: false,
         securityLevel: .low)
         */
        
        isServerRunning = true
        
        log?("Started GATT Server")
        
        let serverThread = Thread { [weak self] in self?.main() }
        
        self.serverThread = serverThread
        
        serverThread.start()
    }
    
    public func stop() {
        
        guard isServerRunning else { return }
        
        self.isServerRunning = false
        self.serverThread = nil
    }
    
    public func add(service: BluetoothGATT.GATTAttribute.Service) throws -> UInt16 {

}

#endif
