# Path: ledgerblue/Dongle.py
# class Dongle(object):
# 	__metaclass__ = ABCMeta
# 
# 	@abstractmethod
# 	def exchange(self, apdu, timeout=TIMEOUT):
# 		pass
# 
# 	@abstractmethod
# 	def apduMaxDataSize(self):
# 		pass
# 
# 	@abstractmethod
# 	def close(self):
# 		pass
# 
# 	def setWaitImpl(self, waitImpl):
# 		self.waitImpl = waitImpl
# 
# class DongleWait(object):
# 	__metaclass__ = ABCMeta
# 
# 	@abstractmethod
# 	def waitFirstResponse(self, timeout):
# 		pass
# 
# Path: ledgerblue/commException.py
# class CommException(Exception):
# 
# 	def __init__(self, message, sw=0x6f00, data=None):
# 		self.message = message
# 		self.sw = sw
# 		self.data = data
# 
# 	def __str__(self):
# 		buf = "Exception : " + self.message
# 		return buf


import os
import traceback
import time
import hid
from .Dongle import Dongle, DongleWait
from u2flib_host.device import U2FDevice
from u2flib_host.yubicommon.compat import byte2int, int2byte
from u2flib_host.constants import INS_SIGN
from u2flib_host import exc
from hashlib import sha256
from .commException import CommException
                raise U2FHIDError(byte2int(resp[7]))

        if not resp:
            raise exc.DeviceError("Invalid response from device!")

        data_len = (byte2int(resp[5]) << 8) + byte2int(resp[6])
        data = resp[7:min(7 + data_len, HID_RPT_SIZE)]
        data_len -= len(data)

        seq = 0
        while data_len > 0:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:4] != cid:
                raise exc.DeviceError("Wrong CID from device!")
            if resp[4] != (seq & 0x7f):
                raise exc.DeviceError("Wrong SEQ from device! {} != {}".format(resp[4], seq))
            seq += 1
            new_data = resp[5:min(5 + data_len, HID_RPT_SIZE)]
            data_len -= len(new_data)
            data += new_data
        return data

    def call(self, cmd, data=b''):
        if isinstance(data, int):
            data = int2byte(data)

        self._send_req(self.cid, cmd, data)
        return self._read_resp(self.cid, cmd)


# Path: ledgerblue/Dongle.py
# class Dongle(object):
# 	__metaclass__ = ABCMeta
# 
# 	@abstractmethod
# 	def exchange(self, apdu, timeout=TIMEOUT):
# 		pass
# 
# 	@abstractmethod
# 	def apduMaxDataSize(self):
# 		pass
# 
# 	@abstractmethod
# 	def close(self):
# 		pass
# 
# 	def setWaitImpl(self, waitImpl):
# 		self.waitImpl = waitImpl
# 
# class DongleWait(object):
# 	__metaclass__ = ABCMeta
# 
# 	@abstractmethod
# 	def waitFirstResponse(self, timeout):
# 		pass
# 
# Path: ledgerblue/commException.py
# class CommException(Exception):
# 
# 	def __init__(self, message, sw=0x6f00, data=None):
# 		self.message = message
# 		self.sw = sw
# 		self.data = data
# 
# 	def __str__(self):
# 		buf = "Exception : " + self.message
# 		return buf


# Path: ledgerblue/commU2F.py
import os
import traceback
import time
import hid
from .Dongle import Dongle, DongleWait
from u2flib_host.device import U2FDevice
from u2flib_host.yubicommon.compat import byte2int, int2byte
from u2flib_host.constants import INS_SIGN
from u2flib_host import exc
from hashlib import sha256
from .commException import CommException

                raise U2FHIDError(byte2int(resp[7]))

        if not resp:
            raise exc.DeviceError("Invalid response from device!")

        data_len = (byte2int(resp[5]) << 8) + byte2int(resp[6])
        data = resp[7:min(7 + data_len, HID_RPT_SIZE)]
        data_len -= len(data)

        seq = 0
        while data_len > 0:
            resp_vals = _read_timeout(self.handle, HID_RPT_SIZE)
            resp = b''.join(int2byte(v) for v in resp_vals)
            if resp[:4] != cid:
                raise exc.DeviceError("Wrong CID from device!")
            if resp[4] != (seq & 0x7f):
                raise exc.DeviceError("Wrong SEQ from device! {} != {}".format(resp[4], seq))
            seq += 1
            new_data = resp[5:min(5 + data_len, HID_RPT_SIZE)]
            data_len -= len(new_data)
            data += new_data
        return data

    def call(self, cmd, data=b''):
        if isinstance(data, int):
            data = int2byte(data)

        self._send_req(self.cid, cmd, data)
        return self._read_resp(self.cid, cmd)


class U2FTunnelDongle(Dongle, DongleWait):