IDFApp.py 7.8 KB

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