 //  Created by keyang on 10/21/15.
 //  Copyright © 2015 com.keyang. All rights reserved.
 //
+/*
+ *
+ *
+ */
 
 import Foundation
+import CoreBluetooth
 
 /// Notification
 public let CuspStateDidChangeNotification = "CuspStateDidChangeNotification"
 
-/// main operation queue identifier (主并发队列ID)
+/// main operation queue identifier
 private let QIDMain = "com.keyang.cusp.mainConcurrentQ"
 
 /// request operation serial queue identifier

/// session operation serial queue identifier
private let QIDSesOp = "com.keyang.cusp.sessionOperationQ"

/// Bluetooth Low Energy library in swift (使用swift编写的BLE通讯框架)
public class Cusp: NSObject {

	/// Singleton (单例)
	public class var central: Cusp {
		struct Static {
			static let instance: Cusp = Cusp()

// MARK: - Custom CBPeripheral subclass registeration
extension Cusp {

	internal func sessionFor(peripheral: Peripheral?) -> CommunicatingSession? {
		if peripheral == nil { return nil }

		var tgtSession: CommunicatingSession?

		dispatch_sync(self.sesOpQ) { () -> Void in
			for session in self.sessions {
				if session.peripheral == peripheral {
					tgtSession = session
					break
				}
			}
		}

		return tgtSession
	}

	/**
	retrieve specific Peripheral object that core matches

	- parameter core: CBPeripheral object

	- returns: Peripheral object or nil if not found
	*/
	internal func peripheralFor(core: CBPeripheral) -> Peripheral? {
		for p in discoveredPeripherals {
			if p.core == core {
				return p
			}
		}
		return nil
	}
}


