# Path: btchip/ledgerWrapper.py
# def wrapCommandAPDU(channel, command, packetSize):
# 	if packetSize < 3:
# 		raise BTChipException("Can't handle Ledger framing with less than 3 bytes for the report")
# 	sequenceIdx = 0		
# 	offset = 0
# 	result = struct.pack(">HBHH", channel, 0x05, sequenceIdx, len(command))
# 	sequenceIdx = sequenceIdx + 1
# 	if len(command) > packetSize - 7:
# 		blockSize = packetSize - 7
# 	else:
# 		blockSize = len(command)
# 	result += command[offset : offset + blockSize]
# 	offset = offset + blockSize
# 	while offset != len(command):
# 		result += struct.pack(">HBH", channel, 0x05, sequenceIdx)
# 		sequenceIdx = sequenceIdx + 1
# 		if (len(command) - offset) > packetSize - 5:
# 			blockSize = packetSize - 5
# 		else:
# 			blockSize = len(command) - offset
# 		result += command[offset : offset + blockSize]
# 		offset = offset + blockSize
# 	while (len(result) % packetSize) != 0:
# 		result += b"\x00"
# 	return bytearray(result)
# 
# def unwrapResponseAPDU(channel, data, packetSize):
# 	sequenceIdx = 0		
# 	offset = 0
# 	if ((data is None) or (len(data) < 7 + 5)):
# 		return None
# 	if struct.unpack(">H", data[offset : offset + 2])[0] != channel:
# 		raise BTChipException("Invalid channel")
# 	offset += 2
# 	if data[offset] != 0x05:
# 		raise BTChipException("Invalid tag")
# 	offset += 1
# 	if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx:
# 		raise BTChipException("Invalid sequence")
# 	offset += 2
# 	responseLength = struct.unpack(">H", data[offset : offset + 2])[0]
# 	offset += 2
# 	if len(data) < 7 + responseLength:
# 		return None
# 	if responseLength > packetSize - 7:
# 		blockSize = packetSize - 7
# 	else:
# 		blockSize = responseLength
# 	result = data[offset : offset + blockSize]
# 	offset += blockSize
# 	while (len(result) != responseLength):
# 		sequenceIdx = sequenceIdx + 1
# 		if (offset == len(data)):
# 			return None
# 		if struct.unpack(">H", data[offset : offset + 2])[0] != channel:
# 			raise BTChipException("Invalid channel")
# 		offset += 2
# 		if data[offset] != 0x05:
# 			raise BTChipException("Invalid tag")
# 		offset += 1
# 		if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx:
# 			raise BTChipException("Invalid sequence")
# 		offset += 2
# 		if (responseLength - len(result)) > packetSize - 5:
# 			blockSize = packetSize - 5
# 		else:
# 			blockSize = responseLength - len(result)
# 		result += data[offset : offset + blockSize]
# 		offset += blockSize
# 	return bytearray(result)


from abc import ABCMeta, abstractmethod
from .btchipException import *
from .ledgerWrapper import wrapCommandAPDU, unwrapResponseAPDU
from binascii import hexlify
	from smartcard.Exceptions import NoCardException
	from smartcard.System import readers
	from smartcard.util import toHexString, toBytes
import time
import os
import struct
import socket
	import hid
	def waitFirstResponse(self, timeout):
		pass

class Dongle(object):
	__metaclass__ = ABCMeta

	@abstractmethod
	def exchange(self, apdu, timeout=20000):
		pass

	@abstractmethod
	def close(self):
		pass

	def setWaitImpl(self, waitImpl):
		self.waitImpl = waitImpl

class HIDDongleHIDAPI(Dongle, DongleWait):

	def __init__(self, device, ledger=False, debug=False):
		self.device = device
		self.ledger = ledger		
		self.debug = debug
		self.waitImpl = self
		self.opened = True

	def exchange(self, apdu, timeout=20000):
		if self.debug:
			print("=> %s" % hexlify(apdu))
		if self.ledger:

# Path: btchip/ledgerWrapper.py
# def wrapCommandAPDU(channel, command, packetSize):
# 	if packetSize < 3:
# 		raise BTChipException("Can't handle Ledger framing with less than 3 bytes for the report")
# 	sequenceIdx = 0		
# 	offset = 0
# 	result = struct.pack(">HBHH", channel, 0x05, sequenceIdx, len(command))
# 	sequenceIdx = sequenceIdx + 1
# 	if len(command) > packetSize - 7:
# 		blockSize = packetSize - 7
# 	else:
# 		blockSize = len(command)
# 	result += command[offset : offset + blockSize]
# 	offset = offset + blockSize
# 	while offset != len(command):
# 		result += struct.pack(">HBH", channel, 0x05, sequenceIdx)
# 		sequenceIdx = sequenceIdx + 1
# 		if (len(command) - offset) > packetSize - 5:
# 			blockSize = packetSize - 5
# 		else:
# 			blockSize = len(command) - offset
# 		result += command[offset : offset + blockSize]
# 		offset = offset + blockSize
# 	while (len(result) % packetSize) != 0:
# 		result += b"\x00"
# 	return bytearray(result)
# 
# def unwrapResponseAPDU(channel, data, packetSize):
# 	sequenceIdx = 0		
# 	offset = 0
# 	if ((data is None) or (len(data) < 7 + 5)):
# 		return None
# 	if struct.unpack(">H", data[offset : offset + 2])[0] != channel:
# 		raise BTChipException("Invalid channel")
# 	offset += 2
# 	if data[offset] != 0x05:
# 		raise BTChipException("Invalid tag")
# 	offset += 1
# 	if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx:
# 		raise BTChipException("Invalid sequence")
# 	offset += 2
# 	responseLength = struct.unpack(">H", data[offset : offset + 2])[0]
# 	offset += 2
# 	if len(data) < 7 + responseLength:
# 		return None
# 	if responseLength > packetSize - 7:
# 		blockSize = packetSize - 7
# 	else:
# 		blockSize = responseLength
# 	result = data[offset : offset + blockSize]
# 	offset += blockSize
# 	while (len(result) != responseLength):
# 		sequenceIdx = sequenceIdx + 1
# 		if (offset == len(data)):
# 			return None
# 		if struct.unpack(">H", data[offset : offset + 2])[0] != channel:
# 			raise BTChipException("Invalid channel")
# 		offset += 2
# 		if data[offset] != 0x05:
# 			raise BTChipException("Invalid tag")
# 		offset += 1
# 		if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx:
# 			raise BTChipException("Invalid sequence")
# 		offset += 2
# 		if (responseLength - len(result)) > packetSize - 5:
# 			blockSize = packetSize - 5
# 		else:
# 			blockSize = responseLength - len(result)
# 		result += data[offset : offset + blockSize]
# 		offset += blockSize
# 	return bytearray(result)


# Path: btchip/btchipComm.py
from abc import ABCMeta, abstractmethod
from .btchipException import *
from .ledgerWrapper import wrapCommandAPDU, unwrapResponseAPDU
from binascii import hexlify
	from smartcard.Exceptions import NoCardException
	from smartcard.System import readers
	from smartcard.util import toHexString, toBytes
import time
import os
import struct
import socket
	import hid

	def waitFirstResponse(self, timeout):
		pass

class Dongle(object):
	__metaclass__ = ABCMeta

	@abstractmethod
	def exchange(self, apdu, timeout=20000):
		pass

	@abstractmethod
	def close(self):
		pass

	def setWaitImpl(self, waitImpl):
		self.waitImpl = waitImpl

class HIDDongleHIDAPI(Dongle, DongleWait):

	def __init__(self, device, ledger=False, debug=False):
		self.device = device
		self.ledger = ledger		
		self.debug = debug
		self.waitImpl = self
		self.opened = True

	def exchange(self, apdu, timeout=20000):
		if self.debug:
			print("=> %s" % hexlify(apdu))
		if self.ledger:

			apdu = wrapCommandAPDU(0x0101, apdu, 64)		