idf_monitor_wrapper.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2019 Espressif Systems (Shanghai) PTE 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. from __future__ import unicode_literals
  15. import argparse
  16. import sys
  17. import threading
  18. import time
  19. from io import open
  20. import serial
  21. try:
  22. import idf_monitor
  23. except ImportError:
  24. sys.path.append('..')
  25. import idf_monitor
  26. def monitor_serial_reader_state(serial_reader, file_to_create):
  27. """
  28. The purpose of this wrapper is to monitor the serial reader state of idf_monitor.py. file_to_create is created
  29. after the serial reader thread has been started. The existence of this file will indicate to
  30. run_test_idf_monitor.py that idf_monitor.py is ready to process inputs.
  31. """
  32. while not serial_reader.serial.is_open or not serial_reader.alive:
  33. time.sleep(1)
  34. with open(file_to_create, 'w', encoding='utf-8'):
  35. pass
  36. def main():
  37. parser = argparse.ArgumentParser('Wrapper for idf_monitor used for testing')
  38. parser.add_argument('--port')
  39. parser.add_argument('--print_filter')
  40. parser.add_argument('--serial_alive_file')
  41. parser.add_argument('--toolchain-prefix')
  42. parser.add_argument('--decode-panic', default='disable')
  43. parser.add_argument('--target', default=None)
  44. parser.add_argument('--elf-file')
  45. args = parser.parse_args()
  46. serial_instance = serial.serial_for_url(args.port, 115200, do_not_open=True)
  47. monitor = idf_monitor.Monitor(serial_instance, args.elf_file, args.print_filter, 'make',
  48. toolchain_prefix=args.toolchain_prefix, eol='CR',
  49. decode_panic=args.decode_panic, target=args.target)
  50. sys.stderr.write('Monitor instance has been created.\n')
  51. monitor_thread = threading.Thread(target=monitor_serial_reader_state,
  52. args=(monitor.serial_reader, args.serial_alive_file))
  53. monitor_thread.start()
  54. sys.stderr.write('Monitoring thread has been created.\n')
  55. monitor.main_loop()
  56. sys.stderr.write('Monitor loop exited.\n')
  57. monitor_thread.join(60)
  58. sys.stderr.write('Monitoring thread joined.\n')
  59. if __name__ == '__main__':
  60. main()