# Path: trezorlib/debuglink.py
# class DebugLink:
#     def __init__(self, transport, auto_interact=True):
#         self.transport = transport
#         self.allow_interactions = auto_interact
# 
#     def open(self):
#         self.transport.begin_session()
# 
#     def close(self):
#         self.transport.end_session()
# 
#     def _call(self, msg, nowait=False):
#         self.transport.write(msg)
#         if nowait:
#             return None
#         ret = self.transport.read()
#         return ret
# 
#     def state(self):
#         return self._call(proto.DebugLinkGetState())
# 
#     def read_pin(self):
#         state = self.state()
#         return state.pin, state.matrix
# 
#     def read_pin_encoded(self):
#         return self.encode_pin(*self.read_pin())
# 
#     def encode_pin(self, pin, matrix=None):
#         """Transform correct PIN according to the displayed matrix."""
#         if matrix is None:
#             _, matrix = self.read_pin()
#         return "".join([str(matrix.index(p) + 1) for p in pin])
# 
#     def read_layout(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.layout
# 
#     def read_mnemonic_secret(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.mnemonic_secret
# 
#     def read_recovery_word(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return (obj.recovery_fake_word, obj.recovery_word_pos)
# 
#     def read_reset_word(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_word
# 
#     def read_reset_word_pos(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_word_pos
# 
#     def read_reset_entropy(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_entropy
# 
#     def read_passphrase_protection(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.passphrase_protection
# 
#     def input(self, word=None, button=None, swipe=None):
#         if not self.allow_interactions:
#             return
#         decision = proto.DebugLinkDecision()
#         if button is not None:
#             decision.yes_no = button
#         elif word is not None:
#             decision.input = word
#         elif swipe is not None:
#             decision.up_down = swipe
#         else:
#             raise ValueError("You need to provide input data.")
#         self._call(decision, nowait=True)
# 
#     def press_button(self, yes_no):
#         self._call(proto.DebugLinkDecision(yes_no=yes_no), nowait=True)
# 
#     def press_yes(self):
#         self.input(button=True)
# 
#     def press_no(self):
#         self.input(button=False)
# 
#     def swipe_up(self):
#         self.input(swipe=True)
# 
#     def swipe_down(self):
#         self.input(swipe=False)
# 
#     def stop(self):
#         self._call(proto.DebugLinkStop(), nowait=True)
# 
#     @expect(proto.DebugLinkMemory, field="memory")
#     def memory_read(self, address, length):
#         return self._call(proto.DebugLinkMemoryRead(address=address, length=length))
# 
#     def memory_write(self, address, memory, flash=False):
#         self._call(
#             proto.DebugLinkMemoryWrite(address=address, memory=memory, flash=flash),
#             nowait=True,
#         )
# 
#     def flash_erase(self, sector):
#         self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)


from trezorlib.debuglink import DebugLink
from trezorlib.transport import enumerate_devices
import sys
#!/usr/bin/env python3


def find_debug():
    for device in enumerate_devices():
        try:
            debug_transport = device.find_debug()

# Path: trezorlib/debuglink.py
# class DebugLink:
#     def __init__(self, transport, auto_interact=True):
#         self.transport = transport
#         self.allow_interactions = auto_interact
# 
#     def open(self):
#         self.transport.begin_session()
# 
#     def close(self):
#         self.transport.end_session()
# 
#     def _call(self, msg, nowait=False):
#         self.transport.write(msg)
#         if nowait:
#             return None
#         ret = self.transport.read()
#         return ret
# 
#     def state(self):
#         return self._call(proto.DebugLinkGetState())
# 
#     def read_pin(self):
#         state = self.state()
#         return state.pin, state.matrix
# 
#     def read_pin_encoded(self):
#         return self.encode_pin(*self.read_pin())
# 
#     def encode_pin(self, pin, matrix=None):
#         """Transform correct PIN according to the displayed matrix."""
#         if matrix is None:
#             _, matrix = self.read_pin()
#         return "".join([str(matrix.index(p) + 1) for p in pin])
# 
#     def read_layout(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.layout
# 
#     def read_mnemonic_secret(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.mnemonic_secret
# 
#     def read_recovery_word(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return (obj.recovery_fake_word, obj.recovery_word_pos)
# 
#     def read_reset_word(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_word
# 
#     def read_reset_word_pos(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_word_pos
# 
#     def read_reset_entropy(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.reset_entropy
# 
#     def read_passphrase_protection(self):
#         obj = self._call(proto.DebugLinkGetState())
#         return obj.passphrase_protection
# 
#     def input(self, word=None, button=None, swipe=None):
#         if not self.allow_interactions:
#             return
#         decision = proto.DebugLinkDecision()
#         if button is not None:
#             decision.yes_no = button
#         elif word is not None:
#             decision.input = word
#         elif swipe is not None:
#             decision.up_down = swipe
#         else:
#             raise ValueError("You need to provide input data.")
#         self._call(decision, nowait=True)
# 
#     def press_button(self, yes_no):
#         self._call(proto.DebugLinkDecision(yes_no=yes_no), nowait=True)
# 
#     def press_yes(self):
#         self.input(button=True)
# 
#     def press_no(self):
#         self.input(button=False)
# 
#     def swipe_up(self):
#         self.input(swipe=True)
# 
#     def swipe_down(self):
#         self.input(swipe=False)
# 
#     def stop(self):
#         self._call(proto.DebugLinkStop(), nowait=True)
# 
#     @expect(proto.DebugLinkMemory, field="memory")
#     def memory_read(self, address, length):
#         return self._call(proto.DebugLinkMemoryRead(address=address, length=length))
# 
#     def memory_write(self, address, memory, flash=False):
#         self._call(
#             proto.DebugLinkMemoryWrite(address=address, memory=memory, flash=flash),
#             nowait=True,
#         )
# 
#     def flash_erase(self, sector):
#         self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)


# Path: tools/mem_write.py
from trezorlib.debuglink import DebugLink
from trezorlib.transport import enumerate_devices
import sys

#!/usr/bin/env python3


def find_debug():
    for device in enumerate_devices():
        try:
            debug_transport = device.find_debug()

            debug = DebugLink(debug_transport, auto_interact=False)