IDFApp.py 6.5 KB

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