IDFApp.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright 2015-2017 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. """ IDF Test Applications """
  15. import subprocess
  16. import os
  17. import App
  18. class IDFApp(App.BaseApp):
  19. """
  20. Implements common esp-idf application behavior.
  21. idf applications should inherent from this class and overwrite method get_binary_path.
  22. """
  23. IDF_DOWNLOAD_CONFIG_FILE = "download.config"
  24. IDF_FLASH_ARGS_FILE = "flash_project_args"
  25. def __init__(self, app_path):
  26. super(IDFApp, self).__init__(app_path)
  27. self.idf_path = self.get_sdk_path()
  28. self.binary_path = self.get_binary_path(app_path)
  29. assert os.path.exists(self.binary_path)
  30. if self.IDF_DOWNLOAD_CONFIG_FILE not in os.listdir(self.binary_path):
  31. if self.IDF_FLASH_ARGS_FILE not in os.listdir(self.binary_path):
  32. msg = ("Neither {} nor {} exists. "
  33. "Try to run 'make print_flash_cmd | tail -n 1 > {}/{}' "
  34. "or 'idf.py build' "
  35. "for resolving the issue."
  36. "").format(self.IDF_DOWNLOAD_CONFIG_FILE, self.IDF_FLASH_ARGS_FILE,
  37. self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE)
  38. raise AssertionError(msg)
  39. self.esptool, self.partition_tool = self.get_tools()
  40. @classmethod
  41. def get_sdk_path(cls):
  42. idf_path = os.getenv("IDF_PATH")
  43. assert idf_path
  44. assert os.path.exists(idf_path)
  45. return idf_path
  46. @classmethod
  47. def get_tools(cls):
  48. idf_path = cls.get_sdk_path()
  49. # get esptool and partition tool for esp-idf
  50. esptool = os.path.join(idf_path, "components",
  51. "esptool_py", "esptool", "esptool.py")
  52. partition_tool = os.path.join(idf_path, "components",
  53. "partition_table", "gen_esp32part.py")
  54. assert os.path.exists(esptool) and os.path.exists(partition_tool)
  55. return esptool, partition_tool
  56. def get_binary_path(self, app_path):
  57. """
  58. get binary path according to input app_path.
  59. subclass must overwrite this method.
  60. :param app_path: path of application
  61. :return: abs app binary path
  62. """
  63. pass
  64. def process_arg(self, arg):
  65. """
  66. process args in download.config. convert to abs path for .bin args. strip spaces and CRLFs.
  67. """
  68. if ".bin" in arg:
  69. ret = os.path.join(self.binary_path, arg)
  70. else:
  71. ret = arg
  72. return ret.strip("\r\n ")
  73. def process_app_info(self):
  74. """
  75. get app download config and partition info from a specific app path
  76. :return: download config, partition info
  77. """
  78. if self.IDF_FLASH_ARGS_FILE in os.listdir(self.binary_path):
  79. with open(os.path.join(self.binary_path, self.IDF_FLASH_ARGS_FILE), "r") as f:
  80. configs = []
  81. for line in f:
  82. line = line.strip()
  83. if len(line) > 0:
  84. configs += line.split()
  85. else:
  86. with open(os.path.join(self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE), "r") as f:
  87. configs = f.read().split(" ")
  88. download_configs = ["--chip", "auto", "--before", "default_reset",
  89. "--after", "hard_reset", "write_flash", "-z"]
  90. download_configs += [self.process_arg(x) for x in configs]
  91. # handle partition table
  92. for partition_file in download_configs:
  93. if "partition" in partition_file:
  94. partition_file = os.path.join(self.binary_path, partition_file)
  95. break
  96. else:
  97. raise ValueError("No partition table found for IDF binary path: {}".format(self.binary_path))
  98. process = subprocess.Popen(["python", self.partition_tool, partition_file],
  99. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  100. raw_data = process.stdout.read()
  101. if isinstance(raw_data, bytes):
  102. raw_data = raw_data.decode()
  103. partition_table = dict()
  104. for line in raw_data.splitlines():
  105. if line[0] != "#":
  106. try:
  107. _name, _type, _subtype, _offset, _size, _flags = line.split(",")
  108. if _size[-1] == "K":
  109. _size = int(_size[:-1]) * 1024
  110. elif _size[-1] == "M":
  111. _size = int(_size[:-1]) * 1024 * 1024
  112. else:
  113. _size = int(_size)
  114. except ValueError:
  115. continue
  116. partition_table[_name] = {
  117. "type": _type,
  118. "subtype": _subtype,
  119. "offset": _offset,
  120. "size": _size,
  121. "flags": _flags
  122. }
  123. return download_configs, partition_table
  124. class Example(IDFApp):
  125. def get_binary_path(self, app_path):
  126. # build folder of example path
  127. path = os.path.join(self.idf_path, app_path, "build")
  128. if not os.path.exists(path):
  129. # search for CI build folders
  130. app = os.path.basename(app_path)
  131. example_path = os.path.join(self.idf_path, "build_examples", "example_builds")
  132. for dirpath, dirnames, files in os.walk(example_path):
  133. if dirnames:
  134. if dirnames[0] == app:
  135. path = os.path.join(example_path, dirpath, dirnames[0], "build")
  136. break
  137. else:
  138. raise OSError("Failed to find example binary")
  139. return path
  140. class UT(IDFApp):
  141. def get_binary_path(self, app_path):
  142. """
  143. :param app_path: app path or app config
  144. :return: binary path
  145. """
  146. if not app_path:
  147. app_path = "default"
  148. path = os.path.join(self.idf_path, app_path)
  149. if not os.path.exists(path):
  150. while True:
  151. # try to get by config
  152. if app_path == "default":
  153. # it's default config, we first try to get form build folder of unit-test-app
  154. path = os.path.join(self.idf_path, "tools", "unit-test-app", "build")
  155. if os.path.exists(path):
  156. # found, use bin in build path
  157. break
  158. # ``make ut-build-all-configs`` or ``make ut-build-CONFIG`` will copy binary to output folder
  159. path = os.path.join(self.idf_path, "tools", "unit-test-app", "output", app_path)
  160. if os.path.exists(path):
  161. break
  162. raise OSError("Failed to get unit-test-app binary path")
  163. return path
  164. class SSC(IDFApp):
  165. def get_binary_path(self, app_path):
  166. # TODO: to implement SSC get binary path
  167. return app_path
  168. class AT(IDFApp):
  169. def get_binary_path(self, app_path):
  170. # TODO: to implement AT get binary path
  171. return app_path