console_reader.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright 2015-2021 Espressif Systems (Shanghai) CO LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import queue
  16. import time
  17. from serial.tools.miniterm import Console
  18. from .console_parser import ConsoleParser
  19. from .constants import CMD_STOP, TAG_CMD
  20. from .stoppable_thread import StoppableThread
  21. class ConsoleReader(StoppableThread):
  22. """ Read input keys from the console and push them to the queue,
  23. until stopped.
  24. """
  25. def __init__(self, console, event_queue, cmd_queue, parser, test_mode):
  26. # type: (Console, queue.Queue, queue.Queue, ConsoleParser, bool) -> None
  27. super(ConsoleReader, self).__init__()
  28. self.console = console
  29. self.event_queue = event_queue
  30. self.cmd_queue = cmd_queue
  31. self.parser = parser
  32. self.test_mode = test_mode
  33. def run(self):
  34. # type: () -> None
  35. self.console.setup()
  36. try:
  37. while self.alive:
  38. try:
  39. if os.name == 'nt':
  40. # Windows kludge: because the console.cancel() method doesn't
  41. # seem to work to unblock getkey() on the Windows implementation.
  42. #
  43. # So we only call getkey() if we know there's a key waiting for us.
  44. import msvcrt
  45. while not msvcrt.kbhit() and self.alive: # type: ignore
  46. time.sleep(0.1)
  47. if not self.alive:
  48. break
  49. elif self.test_mode:
  50. # In testing mode the stdin is connected to PTY but is not used for input anything. For PTY
  51. # the canceling by fcntl.ioctl isn't working and would hang in self.console.getkey().
  52. # Therefore, we avoid calling it.
  53. while self.alive:
  54. time.sleep(0.1)
  55. break
  56. c = self.console.getkey()
  57. except KeyboardInterrupt:
  58. c = '\x03'
  59. if c is not None:
  60. ret = self.parser.parse(c)
  61. if ret is not None:
  62. (tag, cmd) = ret
  63. # stop command should be executed last
  64. if tag == TAG_CMD and cmd != CMD_STOP:
  65. self.cmd_queue.put(ret)
  66. else:
  67. self.event_queue.put(ret)
  68. finally:
  69. self.console.cleanup()
  70. def _cancel(self):
  71. # type: () -> None
  72. if os.name == 'posix' and not self.test_mode:
  73. # this is the way cancel() is implemented in pyserial 3.3 or newer,
  74. # older pyserial (3.1+) has cancellation implemented via 'select',
  75. # which does not work when console sends an escape sequence response
  76. #
  77. # even older pyserial (<3.1) does not have this method
  78. #
  79. # on Windows there is a different (also hacky) fix, applied above.
  80. #
  81. # note that TIOCSTI is not implemented in WSL / bash-on-Windows.
  82. # TODO: introduce some workaround to make it work there.
  83. #
  84. # Note: This would throw exception in testing mode when the stdin is connected to PTY.
  85. import fcntl
  86. import termios
  87. fcntl.ioctl(self.console.fd, termios.TIOCSTI, b'\0')