idf_monitor_wrapper.py 2.3 KB

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