You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
12 KiB

  1. """Acquire debugging information from usb hid devices
  2. cli implementation of https://www.pjrc.com/teensy/hid_listen.html
  3. """
  4. from pathlib import Path
  5. from threading import Thread
  6. from time import sleep, strftime
  7. import hid
  8. import usb.core
  9. from milc import cli
  10. LOG_COLOR = {
  11. 'next': 0,
  12. 'colors': [
  13. '{fg_blue}',
  14. '{fg_cyan}',
  15. '{fg_green}',
  16. '{fg_magenta}',
  17. '{fg_red}',
  18. '{fg_yellow}',
  19. ],
  20. }
  21. KNOWN_BOOTLOADERS = {
  22. # VID , PID
  23. ('03EB', '2FEF'): 'atmel-dfu: ATmega16U2',
  24. ('03EB', '2FF0'): 'atmel-dfu: ATmega32U2',
  25. ('03EB', '2FF3'): 'atmel-dfu: ATmega16U4',
  26. ('03EB', '2FF4'): 'atmel-dfu: ATmega32U4',
  27. ('03EB', '2FF9'): 'atmel-dfu: AT90USB64',
  28. ('03EB', '2FFA'): 'atmel-dfu: AT90USB162',
  29. ('03EB', '2FFB'): 'atmel-dfu: AT90USB128',
  30. ('03EB', '6124'): 'Microchip SAM-BA',
  31. ('0483', 'DF11'): 'stm32-dfu: STM32 BOOTLOADER',
  32. ('16C0', '05DC'): 'USBasp: USBaspLoader',
  33. ('16C0', '05DF'): 'bootloadHID: HIDBoot',
  34. ('16C0', '0478'): 'halfkay: Teensy Halfkay',
  35. ('1B4F', '9203'): 'caterina: Pro Micro 3.3V',
  36. ('1B4F', '9205'): 'caterina: Pro Micro 5V',
  37. ('1B4F', '9207'): 'caterina: LilyPadUSB',
  38. ('1C11', 'B007'): 'kiibohd: Kiibohd DFU Bootloader',
  39. ('1EAF', '0003'): 'stm32duino: Maple 003',
  40. ('1FFB', '0101'): 'caterina: Polou A-Star 32U4 Bootloader',
  41. ('2341', '0036'): 'caterina: Arduino Leonardo',
  42. ('2341', '0037'): 'caterina: Arduino Micro',
  43. ('239A', '000C'): 'caterina: Adafruit Feather 32U4',
  44. ('239A', '000D'): 'caterina: Adafruit ItsyBitsy 32U4 3v',
  45. ('239A', '000E'): 'caterina: Adafruit ItsyBitsy 32U4 5v',
  46. ('239A', '000E'): 'caterina: Adafruit ItsyBitsy 32U4 5v',
  47. ('2A03', '0036'): 'caterina: Arduino Leonardo',
  48. ('2A03', '0037'): 'caterina: Arduino Micro',
  49. ('314B', '0106'): 'apm32-dfu: APM32 DFU ISP Mode'
  50. }
  51. class MonitorDevice(object):
  52. def __init__(self, hid_device, numeric):
  53. self.hid_device = hid_device
  54. self.numeric = numeric
  55. self.device = hid.Device(path=hid_device['path'])
  56. self.current_line = ''
  57. cli.log.info('Console Connected: %(color)s%(manufacturer_string)s %(product_string)s{style_reset_all} (%(color)s%(vendor_id)04X:%(product_id)04X:%(index)d{style_reset_all})', hid_device)
  58. def read(self, size, encoding='ascii', timeout=1):
  59. """Read size bytes from the device.
  60. """
  61. return self.device.read(size, timeout).decode(encoding)
  62. def read_line(self):
  63. """Read from the device's console until we get a \n.
  64. """
  65. while '\n' not in self.current_line:
  66. self.current_line += self.read(32).replace('\x00', '')
  67. lines = self.current_line.split('\n', 1)
  68. self.current_line = lines[1]
  69. return lines[0]
  70. def run_forever(self):
  71. while True:
  72. try:
  73. message = {**self.hid_device, 'text': self.read_line()}
  74. identifier = (int2hex(message['vendor_id']), int2hex(message['product_id'])) if self.numeric else (message['manufacturer_string'], message['product_string'])
  75. message['identifier'] = ':'.join(identifier)
  76. message['ts'] = '{style_dim}{fg_green}%s{style_reset_all} ' % (strftime(cli.config.general.datetime_fmt),) if cli.args.timestamp else ''
  77. cli.echo('%(ts)s%(color)s%(identifier)s:%(index)d{style_reset_all}: %(text)s' % message)
  78. except hid.HIDException:
  79. break
  80. class FindDevices(object):
  81. def __init__(self, vid, pid, index, numeric):
  82. self.vid = vid
  83. self.pid = pid
  84. self.index = index
  85. self.numeric = numeric
  86. def run_forever(self):
  87. """Process messages from our queue in a loop.
  88. """
  89. live_devices = {}
  90. live_bootloaders = {}
  91. while True:
  92. try:
  93. for device in list(live_devices):
  94. if not live_devices[device]['thread'].is_alive():
  95. cli.log.info('Console Disconnected: %(color)s%(manufacturer_string)s %(product_string)s{style_reset_all} (%(color)s%(vendor_id)04X:%(product_id)04X:%(index)d{style_reset_all})', live_devices[device])
  96. del live_devices[device]
  97. for device in self.find_devices():
  98. if device['path'] not in live_devices:
  99. device['color'] = LOG_COLOR['colors'][LOG_COLOR['next']]
  100. LOG_COLOR['next'] = (LOG_COLOR['next'] + 1) % len(LOG_COLOR['colors'])
  101. live_devices[device['path']] = device
  102. try:
  103. monitor = MonitorDevice(device, self.numeric)
  104. device['thread'] = Thread(target=monitor.run_forever, daemon=True)
  105. device['thread'].start()
  106. except Exception as e:
  107. device['e'] = e
  108. device['e_name'] = e.__class__.__name__
  109. cli.log.error("Could not connect to %(color)s%(manufacturer_string)s %(product_string)s{style_reset_all} (%(color)s:%(vendor_id)04X:%(product_id)04X:%(index)d): %(e_name)s: %(e)s", device)
  110. if cli.config.general.verbose:
  111. cli.log.exception(e)
  112. del live_devices[device['path']]
  113. if cli.args.bootloaders:
  114. for device in self.find_bootloaders():
  115. if device.address in live_bootloaders:
  116. live_bootloaders[device.address]._qmk_found = True
  117. else:
  118. name = KNOWN_BOOTLOADERS[(int2hex(device.idVendor), int2hex(device.idProduct))]
  119. cli.log.info('Bootloader Connected: {style_bright}{fg_magenta}%s', name)
  120. device._qmk_found = True
  121. live_bootloaders[device.address] = device
  122. for device in list(live_bootloaders):
  123. if live_bootloaders[device]._qmk_found:
  124. live_bootloaders[device]._qmk_found = False
  125. else:
  126. name = KNOWN_BOOTLOADERS[(int2hex(live_bootloaders[device].idVendor), int2hex(live_bootloaders[device].idProduct))]
  127. cli.log.info('Bootloader Disconnected: {style_bright}{fg_magenta}%s', name)
  128. del live_bootloaders[device]
  129. sleep(.1)
  130. except KeyboardInterrupt:
  131. break
  132. def is_bootloader(self, hid_device):
  133. """Returns true if the device in question matches a known bootloader vid/pid.
  134. """
  135. return (int2hex(hid_device.idVendor), int2hex(hid_device.idProduct)) in KNOWN_BOOTLOADERS
  136. def is_console_hid(self, hid_device):
  137. """Returns true when the usage page indicates it's a teensy-style console.
  138. """
  139. return hid_device['usage_page'] == 0xFF31 and hid_device['usage'] == 0x0074
  140. def is_filtered_device(self, hid_device):
  141. """Returns True if the device should be included in the list of available consoles.
  142. """
  143. return int2hex(hid_device['vendor_id']) == self.vid and int2hex(hid_device['product_id']) == self.pid
  144. def find_devices_by_report(self, hid_devices):
  145. """Returns a list of available teensy-style consoles by doing a brute-force search.
  146. Some versions of linux don't report usage and usage_page. In that case we fallback to reading the report (possibly inaccurately) ourselves.
  147. """
  148. devices = []
  149. for device in hid_devices:
  150. path = device['path'].decode('utf-8')
  151. if path.startswith('/dev/hidraw'):
  152. number = path[11:]
  153. report = Path(f'/sys/class/hidraw/hidraw{number}/device/report_descriptor')
  154. if report.exists():
  155. rp = report.read_bytes()
  156. if rp[1] == 0x31 and rp[3] == 0x09:
  157. devices.append(device)
  158. return devices
  159. def find_bootloaders(self):
  160. """Returns a list of available bootloader devices.
  161. """
  162. return list(filter(self.is_bootloader, usb.core.find(find_all=True)))
  163. def find_devices(self):
  164. """Returns a list of available teensy-style consoles.
  165. """
  166. hid_devices = hid.enumerate()
  167. devices = list(filter(self.is_console_hid, hid_devices))
  168. if not devices:
  169. devices = self.find_devices_by_report(hid_devices)
  170. if self.vid and self.pid:
  171. devices = list(filter(self.is_filtered_device, devices))
  172. # Add index numbers
  173. device_index = {}
  174. for device in devices:
  175. id = ':'.join((int2hex(device['vendor_id']), int2hex(device['product_id'])))
  176. if id not in device_index:
  177. device_index[id] = 0
  178. device_index[id] += 1
  179. device['index'] = device_index[id]
  180. return devices
  181. def int2hex(number):
  182. """Returns a string representation of the number as hex.
  183. """
  184. return "%04X" % number
  185. def list_devices(device_finder):
  186. """Show the user a nicely formatted list of devices.
  187. """
  188. devices = device_finder.find_devices()
  189. if devices:
  190. cli.log.info('Available devices:')
  191. for dev in devices:
  192. color = LOG_COLOR['colors'][LOG_COLOR['next']]
  193. LOG_COLOR['next'] = (LOG_COLOR['next'] + 1) % len(LOG_COLOR['colors'])
  194. cli.log.info("\t%s%s:%s:%d{style_reset_all}\t%s %s", color, int2hex(dev['vendor_id']), int2hex(dev['product_id']), dev['index'], dev['manufacturer_string'], dev['product_string'])
  195. if cli.args.bootloaders:
  196. bootloaders = device_finder.find_bootloaders()
  197. if bootloaders:
  198. cli.log.info('Available Bootloaders:')
  199. for dev in bootloaders:
  200. cli.log.info("\t%s:%s\t%s", int2hex(dev.idVendor), int2hex(dev.idProduct), KNOWN_BOOTLOADERS[(int2hex(dev.idVendor), int2hex(dev.idProduct))])
  201. @cli.argument('--bootloaders', arg_only=True, default=True, action='store_boolean', help='displaying bootloaders.')
  202. @cli.argument('-d', '--device', help='Device to select - uses format <pid>:<vid>[:<index>].')
  203. @cli.argument('-l', '--list', arg_only=True, action='store_true', help='List available hid_listen devices.')
  204. @cli.argument('-n', '--numeric', arg_only=True, action='store_true', help='Show VID/PID instead of names.')
  205. @cli.argument('-t', '--timestamp', arg_only=True, action='store_true', help='Print the timestamp for received messages as well.')
  206. @cli.argument('-w', '--wait', type=int, default=1, help="How many seconds to wait between checks (Default: 1)")
  207. @cli.subcommand('Acquire debugging information from usb hid devices.', hidden=False if cli.config.user.developer else True)
  208. def console(cli):
  209. """Acquire debugging information from usb hid devices
  210. """
  211. vid = None
  212. pid = None
  213. index = 1
  214. if cli.config.console.device:
  215. device = cli.config.console.device.split(':')
  216. if len(device) == 2:
  217. vid, pid = device
  218. elif len(device) == 3:
  219. vid, pid, index = device
  220. if not index.isdigit():
  221. cli.log.error('Device index must be a number! Got "%s" instead.', index)
  222. exit(1)
  223. index = int(index)
  224. if index < 1:
  225. cli.log.error('Device index must be greater than 0! Got %s', index)
  226. exit(1)
  227. else:
  228. cli.log.error('Invalid format for device, expected "<pid>:<vid>[:<index>]" but got "%s".', cli.config.console.device)
  229. cli.print_help()
  230. exit(1)
  231. vid = vid.upper()
  232. pid = pid.upper()
  233. device_finder = FindDevices(vid, pid, index, cli.args.numeric)
  234. if cli.args.list:
  235. return list_devices(device_finder)
  236. print('Looking for devices...', flush=True)
  237. device_finder.run_forever()