# 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
		while(offset != len(tmp)):
			data = tmp[offset:offset + 64]
			data = bytearray([0]) + data
			self.device.write(data)
			offset += 64
		dataLength = 0
		dataStart = 2		
		result = self.waitImpl.waitFirstResponse(timeout)
		if not self.ledger:
			if result[0] == 0x61: # 61xx : data available
				self.device.set_nonblocking(False)
				dataLength = result[1]
				dataLength += 2
				if dataLength > 62:
					remaining = dataLength - 62
					while(remaining != 0):
						if remaining > 64:
							blockLength = 64
						else:
							blockLength = remaining
						result.extend(bytearray(self.device.read(65))[0:blockLength])
						remaining -= blockLength
				swOffset = dataLength
				dataLength -= 2
				self.device.set_nonblocking(True)
			else:
				swOffset = 0
		else:
			self.device.set_nonblocking(False)
			while True:

# 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

		while(offset != len(tmp)):
			data = tmp[offset:offset + 64]
			data = bytearray([0]) + data
			self.device.write(data)
			offset += 64
		dataLength = 0
		dataStart = 2		
		result = self.waitImpl.waitFirstResponse(timeout)
		if not self.ledger:
			if result[0] == 0x61: # 61xx : data available
				self.device.set_nonblocking(False)
				dataLength = result[1]
				dataLength += 2
				if dataLength > 62:
					remaining = dataLength - 62
					while(remaining != 0):
						if remaining > 64:
							blockLength = 64
						else:
							blockLength = remaining
						result.extend(bytearray(self.device.read(65))[0:blockLength])
						remaining -= blockLength
				swOffset = dataLength
				dataLength -= 2
				self.device.set_nonblocking(True)
			else:
				swOffset = 0
		else:
			self.device.set_nonblocking(False)
			while True:

				response = unwrapResponseAPDU(0x0101, result, 64)