 
         return messageData
     }
-
-
-
-
-    //MARK: Messages
-    func sendAuth(password: String) {
-        var requestMsgPayload = Particle_Ctrl_Mesh_AuthRequest()
-        requestMsgPayload.password = password
-
-        self.prepareRequestMessage(type: .Auth, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendGetDeviceId() {
-        let requestMsgPayload = Particle_Ctrl_GetDeviceIdRequest()
-
-        self.prepareRequestMessage(type: .GetDeviceId, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendSetClaimCode(claimCode: String) {
-        var requestMsgPayload = Particle_Ctrl_SetClaimCodeRequest()
-        requestMsgPayload.code = claimCode
-
-        self.prepareRequestMessage(type: .SetClaimCode, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendGetSerialNumber() {
-        var requestMsgPayload = Particle_Ctrl_GetSerialNumberRequest()
-
-        self.prepareRequestMessage(type: .GetSerialNumber, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendGetConnectionStatus() {
-        var requestMsgPayload = Particle_Ctrl_Cloud_GetConnectionStatusRequest()
-
-        self.prepareRequestMessage(type: .GetConnectionStatus, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendIsClaimed() {
-        let requestMsgPayload = Particle_Ctrl_IsClaimedRequest()
-
-        self.prepareRequestMessage(type: .IsClaimed, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendCreateNetwork(name: String, password: String) {
-        var requestMsgPayload = Particle_Ctrl_Mesh_CreateNetworkRequest()
-        requestMsgPayload.name = name
-        requestMsgPayload.password = password
-
-        self.prepareRequestMessage(type: .CreateNetwork, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendStartCommissioner() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_StartCommissionerRequest()
-
-        self.prepareRequestMessage(type: .StartCommissioner, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendStopCommissioner() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_StopCommissionerRequest()
-
-        self.prepareRequestMessage(type: .StopCommissioner, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendStarListening() {
-        let requestMsgPayload = Particle_Ctrl_StartListeningModeRequest();
-
-        self.prepareRequestMessage(type: .StartListening, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendStopListening() {
-        let requestMsgPayload = Particle_Ctrl_StopListeningModeRequest();
-
-        self.prepareRequestMessage(type: .StopListening, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendDeviceSetupDone() {
-        let requestMsgPayload = Particle_Ctrl_SetDeviceSetupDoneRequest();
-
-        self.prepareRequestMessage(type: .DeviceSetupDone, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendIsDeviceSetupDone() {
-        let requestMsgPayload = Particle_Ctrl_IsDeviceSetupDoneRequest();
-
-        self.prepareRequestMessage(type: .IsDeviceSetupDone, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendPrepareJoiner(networkInfo: Particle_Ctrl_Mesh_NetworkInfo) {
-        var requestMsgPayload = Particle_Ctrl_Mesh_PrepareJoinerRequest()
-        requestMsgPayload.network = networkInfo
-
-        self.prepareRequestMessage(type: .PrepareJoiner, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendAddJoiner(eui64: String, password: String) {
-        var requestMsgPayload = Particle_Ctrl_Mesh_AddJoinerRequest()
-        requestMsgPayload.eui64 = eui64
-        requestMsgPayload.password = password
-
-        self.prepareRequestMessage(type: .AddJoiner, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendRemoveJoiner(eui64: String) {
-        var requestMsgPayload = Particle_Ctrl_Mesh_RemoveJoinerRequest()
-        requestMsgPayload.eui64 = eui64
-
-        self.prepareRequestMessage(type: .RemoveJoiner, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendJoinNetwork() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_JoinNetworkRequest()
-
-        self.prepareRequestMessage(type: .JoinNetwork, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendLeaveNetwork() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_LeaveNetworkRequest()
-
-        self.prepareRequestMessage(type: .LeaveNetwork, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendGetNetworkInfo() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_GetNetworkInfoRequest()
-
-        self.prepareRequestMessage(type: .GetNetworkInfo, payload: self.serialize(message: requestMsgPayload))
-    }
-
-
-    func sendScanNetworks() {
-        let requestMsgPayload = Particle_Ctrl_Mesh_ScanNetworksRequest()
-
-        self.prepareRequestMessage(type: .ScanNetworks, payload: self.serialize(message: requestMsgPayload))
-    }
-
-    func sendGetInterfaceList() {
-        let requestMsgPayload = Particle_Ctrl_GetInterfaceListRequest()
-
-        self.prepareRequestMessage(type: .GetInterfaceList, payload: self.serialize(message: requestMsgPayload))
-    }
-
 
     //MARK: MeshSetupBluetoothConnectionDataDelegate
     func bluetoothConnectionDidReceiveData(sender: MeshSetupBluetoothConnection, data: Data) {

        }
    }

    private func prepareRequestMessage(type: ControlRequestMessageType, payload: Data) {
        if self.waitingForReply {
            fatalError("Trying to send message while transceiver is waiting for a reply")


typealias MeshSetupNetworkInfo = Particle_Ctrl_Mesh_NetworkInfo
typealias CloudConnectionStatus = Particle_Ctrl_Cloud_ConnectionStatus
typealias MeshSetupNetworkInterface = Particle_Ctrl_InterfaceEntry

class MeshSetupProtocolTransceiver: NSObject, MeshSetupBluetoothConnectionDataDelegate {

    private var bluetoothConnection: MeshSetupBluetoothConnection
    private var encryptionManager: MeshSetupEncryptionManager

    private var requestMessageId: UInt16 = 1

    private var waitingForReply: Bool = false
    private var rxBuffer: Data = Data()
    private var txBuffer: Data!

    private var onReplyCallback: ((ReplyMessage?) -> ())!

    private lazy var sendTimeoutWorker: DispatchWorkItem  = DispatchWorkItem() {
        [weak self] in

        NSLog("what is happening?")

        if let sSelf = self {
            sSelf.messageSendTimeout()
        }

    }


    
    required init(connection: MeshSetupBluetoothConnection) {
        self.bluetoothConnection = connection
        self.encryptionManager = MeshSetupEncryptionManager(derivedSecret: bluetoothConnection.derivedSecret!)

