2017-10-10 02:44:55 +00:00
|
|
|
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
""" IDF Test Applications """
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import App
|
2020-04-02 15:50:28 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IDFApp(App.BaseApp):
|
|
|
|
"""
|
|
|
|
Implements common esp-idf application behavior.
|
|
|
|
idf applications should inherent from this class and overwrite method get_binary_path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
IDF_DOWNLOAD_CONFIG_FILE = "download.config"
|
2018-12-05 00:13:33 +00:00
|
|
|
IDF_FLASH_ARGS_FILE = "flasher_args.json"
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
def __init__(self, app_path):
|
|
|
|
super(IDFApp, self).__init__(app_path)
|
|
|
|
self.idf_path = self.get_sdk_path()
|
|
|
|
self.binary_path = self.get_binary_path(app_path)
|
2019-03-18 04:16:24 +00:00
|
|
|
self.elf_file = self._get_elf_file_path(self.binary_path)
|
2017-10-10 02:44:55 +00:00
|
|
|
assert os.path.exists(self.binary_path)
|
2018-09-12 11:30:36 +00:00
|
|
|
if self.IDF_DOWNLOAD_CONFIG_FILE not in os.listdir(self.binary_path):
|
|
|
|
if self.IDF_FLASH_ARGS_FILE not in os.listdir(self.binary_path):
|
|
|
|
msg = ("Neither {} nor {} exists. "
|
|
|
|
"Try to run 'make print_flash_cmd | tail -n 1 > {}/{}' "
|
|
|
|
"or 'idf.py build' "
|
|
|
|
"for resolving the issue."
|
|
|
|
"").format(self.IDF_DOWNLOAD_CONFIG_FILE, self.IDF_FLASH_ARGS_FILE,
|
2018-12-04 12:46:48 +00:00
|
|
|
self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE)
|
2018-09-12 11:30:36 +00:00
|
|
|
raise AssertionError(msg)
|
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
self.flash_files, self.flash_settings = self._parse_flash_download_config()
|
2018-12-04 12:46:48 +00:00
|
|
|
self.partition_table = self._parse_partition_table()
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_sdk_path(cls):
|
|
|
|
idf_path = os.getenv("IDF_PATH")
|
|
|
|
assert idf_path
|
|
|
|
assert os.path.exists(idf_path)
|
|
|
|
return idf_path
|
|
|
|
|
2018-12-07 14:15:34 +00:00
|
|
|
def _get_sdkconfig_paths(self):
|
|
|
|
"""
|
|
|
|
returns list of possible paths where sdkconfig could be found
|
|
|
|
|
|
|
|
Note: could be overwritten by a derived class to provide other locations or order
|
|
|
|
"""
|
|
|
|
return [os.path.join(self.binary_path, "sdkconfig"), os.path.join(self.binary_path, "..", "sdkconfig")]
|
|
|
|
|
|
|
|
def get_sdkconfig(self):
|
|
|
|
"""
|
|
|
|
reads sdkconfig and returns a dictionary with all configuredvariables
|
|
|
|
|
|
|
|
:param sdkconfig_file: location of sdkconfig
|
|
|
|
:raise: AssertionError: if sdkconfig file does not exist in defined paths
|
|
|
|
"""
|
|
|
|
d = {}
|
|
|
|
sdkconfig_file = None
|
|
|
|
for i in self._get_sdkconfig_paths():
|
|
|
|
if os.path.exists(i):
|
|
|
|
sdkconfig_file = i
|
|
|
|
break
|
|
|
|
assert sdkconfig_file is not None
|
|
|
|
with open(sdkconfig_file) as f:
|
|
|
|
for line in f:
|
|
|
|
configs = line.split('=')
|
|
|
|
if len(configs) == 2:
|
|
|
|
d[configs[0]] = configs[1]
|
|
|
|
return d
|
|
|
|
|
2017-10-10 02:44:55 +00:00
|
|
|
def get_binary_path(self, app_path):
|
|
|
|
"""
|
|
|
|
get binary path according to input app_path.
|
|
|
|
|
|
|
|
subclass must overwrite this method.
|
|
|
|
|
|
|
|
:param app_path: path of application
|
|
|
|
:return: abs app binary path
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2019-03-18 04:16:24 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_elf_file_path(binary_path):
|
|
|
|
ret = ""
|
|
|
|
file_names = os.listdir(binary_path)
|
|
|
|
for fn in file_names:
|
|
|
|
if os.path.splitext(fn)[1] == ".elf":
|
|
|
|
ret = os.path.join(binary_path, fn)
|
|
|
|
return ret
|
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
def _parse_flash_download_config(self):
|
2017-10-10 02:44:55 +00:00
|
|
|
"""
|
2018-12-05 00:13:33 +00:00
|
|
|
Parse flash download config from build metadata files
|
2017-10-10 02:44:55 +00:00
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
Sets self.flash_files, self.flash_settings
|
|
|
|
|
|
|
|
(Called from constructor)
|
2017-10-10 02:44:55 +00:00
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
Returns (flash_files, flash_settings)
|
2017-10-10 02:44:55 +00:00
|
|
|
"""
|
2018-09-12 11:30:36 +00:00
|
|
|
|
|
|
|
if self.IDF_FLASH_ARGS_FILE in os.listdir(self.binary_path):
|
2018-12-05 00:13:33 +00:00
|
|
|
# CMake version using build metadata file
|
2018-09-12 11:30:36 +00:00
|
|
|
with open(os.path.join(self.binary_path, self.IDF_FLASH_ARGS_FILE), "r") as f:
|
2018-12-05 00:13:33 +00:00
|
|
|
args = json.load(f)
|
2018-12-04 12:46:48 +00:00
|
|
|
flash_files = [(offs,file) for (offs,file) in args["flash_files"].items() if offs != ""]
|
2018-12-05 00:13:33 +00:00
|
|
|
flash_settings = args["flash_settings"]
|
2018-09-12 11:30:36 +00:00
|
|
|
else:
|
2018-12-05 00:13:33 +00:00
|
|
|
# GNU Make version uses download.config arguments file
|
2018-09-12 11:30:36 +00:00
|
|
|
with open(os.path.join(self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE), "r") as f:
|
2018-12-05 00:13:33 +00:00
|
|
|
args = f.readlines()[-1].split(" ")
|
|
|
|
flash_files = []
|
|
|
|
flash_settings = {}
|
|
|
|
for idx in range(0, len(args), 2): # process arguments in pairs
|
|
|
|
if args[idx].startswith("--"):
|
|
|
|
# strip the -- from the command line argument
|
2018-12-04 12:46:48 +00:00
|
|
|
flash_settings[args[idx][2:]] = args[idx + 1]
|
2018-12-05 00:13:33 +00:00
|
|
|
else:
|
|
|
|
# offs, filename
|
2018-12-04 12:46:48 +00:00
|
|
|
flash_files.append((args[idx], args[idx + 1]))
|
2017-10-10 02:44:55 +00:00
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
# make file offsets into integers, make paths absolute
|
2018-12-04 12:46:48 +00:00
|
|
|
flash_files = [(int(offs, 0), os.path.join(self.binary_path, path.strip())) for (offs, path) in flash_files]
|
2017-10-10 02:44:55 +00:00
|
|
|
|
2018-12-05 00:13:33 +00:00
|
|
|
return (flash_files, flash_settings)
|
|
|
|
|
|
|
|
def _parse_partition_table(self):
|
|
|
|
"""
|
|
|
|
Parse partition table contents based on app binaries
|
|
|
|
|
|
|
|
Returns partition_table data
|
|
|
|
|
|
|
|
(Called from constructor)
|
|
|
|
"""
|
|
|
|
partition_tool = os.path.join(self.idf_path,
|
|
|
|
"components",
|
|
|
|
"partition_table",
|
|
|
|
"gen_esp32part.py")
|
|
|
|
assert os.path.exists(partition_tool)
|
|
|
|
|
2020-04-02 15:50:28 +00:00
|
|
|
errors = []
|
|
|
|
# self.flash_files is sorted based on offset in order to have a consistent result with different versions of
|
|
|
|
# Python
|
|
|
|
for (_, path) in sorted(self.flash_files, key=lambda elem: elem[0]):
|
|
|
|
if 'partition' in os.path.split(path)[1]:
|
2018-12-05 00:13:33 +00:00
|
|
|
partition_file = os.path.join(self.binary_path, path)
|
2020-04-02 15:50:28 +00:00
|
|
|
|
|
|
|
process = subprocess.Popen([sys.executable, partition_tool, partition_file],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(raw_data, raw_error) = process.communicate()
|
|
|
|
if isinstance(raw_error, bytes):
|
|
|
|
raw_error = raw_error.decode()
|
|
|
|
if 'Traceback' in raw_error:
|
|
|
|
# Some exception occured. It is possible that we've tried the wrong binary file.
|
|
|
|
errors.append((path, raw_error))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(raw_data, bytes):
|
|
|
|
raw_data = raw_data.decode()
|
2017-10-10 02:44:55 +00:00
|
|
|
break
|
|
|
|
else:
|
2020-04-02 15:50:28 +00:00
|
|
|
traceback_msg = os.linesep.join(['{} {}:{}{}'.format(partition_tool,
|
|
|
|
p,
|
|
|
|
os.linesep,
|
|
|
|
msg) for p, msg in errors])
|
|
|
|
raise ValueError("No partition table found for IDF binary path: {}{}{}".format(self.binary_path,
|
|
|
|
os.linesep,
|
|
|
|
traceback_msg))
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
partition_table = dict()
|
|
|
|
for line in raw_data.splitlines():
|
|
|
|
if line[0] != "#":
|
|
|
|
try:
|
|
|
|
_name, _type, _subtype, _offset, _size, _flags = line.split(",")
|
|
|
|
if _size[-1] == "K":
|
|
|
|
_size = int(_size[:-1]) * 1024
|
|
|
|
elif _size[-1] == "M":
|
|
|
|
_size = int(_size[:-1]) * 1024 * 1024
|
|
|
|
else:
|
|
|
|
_size = int(_size)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
partition_table[_name] = {
|
|
|
|
"type": _type,
|
|
|
|
"subtype": _subtype,
|
|
|
|
"offset": _offset,
|
|
|
|
"size": _size,
|
|
|
|
"flags": _flags
|
|
|
|
}
|
2018-12-05 00:13:33 +00:00
|
|
|
|
|
|
|
return partition_table
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Example(IDFApp):
|
2018-12-07 14:15:34 +00:00
|
|
|
def _get_sdkconfig_paths(self):
|
|
|
|
"""
|
|
|
|
overrides the parent method to provide exact path of sdkconfig for example tests
|
|
|
|
"""
|
|
|
|
return [os.path.join(self.binary_path, "..", "sdkconfig")]
|
|
|
|
|
2017-10-10 02:44:55 +00:00
|
|
|
def get_binary_path(self, app_path):
|
|
|
|
# build folder of example path
|
|
|
|
path = os.path.join(self.idf_path, app_path, "build")
|
|
|
|
if not os.path.exists(path):
|
|
|
|
# search for CI build folders
|
|
|
|
app = os.path.basename(app_path)
|
|
|
|
example_path = os.path.join(self.idf_path, "build_examples", "example_builds")
|
|
|
|
for dirpath, dirnames, files in os.walk(example_path):
|
|
|
|
if dirnames:
|
|
|
|
if dirnames[0] == app:
|
|
|
|
path = os.path.join(example_path, dirpath, dirnames[0], "build")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise OSError("Failed to find example binary")
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
class UT(IDFApp):
|
|
|
|
def get_binary_path(self, app_path):
|
2018-01-31 10:59:10 +00:00
|
|
|
"""
|
|
|
|
:param app_path: app path or app config
|
|
|
|
:return: binary path
|
|
|
|
"""
|
|
|
|
if not app_path:
|
|
|
|
app_path = "default"
|
|
|
|
|
|
|
|
path = os.path.join(self.idf_path, app_path)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
while True:
|
|
|
|
# try to get by config
|
|
|
|
if app_path == "default":
|
|
|
|
# it's default config, we first try to get form build folder of unit-test-app
|
|
|
|
path = os.path.join(self.idf_path, "tools", "unit-test-app", "build")
|
|
|
|
if os.path.exists(path):
|
|
|
|
# found, use bin in build path
|
|
|
|
break
|
|
|
|
# ``make ut-build-all-configs`` or ``make ut-build-CONFIG`` will copy binary to output folder
|
|
|
|
path = os.path.join(self.idf_path, "tools", "unit-test-app", "output", app_path)
|
|
|
|
if os.path.exists(path):
|
|
|
|
break
|
|
|
|
raise OSError("Failed to get unit-test-app binary path")
|
2017-10-10 02:44:55 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
class SSC(IDFApp):
|
|
|
|
def get_binary_path(self, app_path):
|
|
|
|
# TODO: to implement SSC get binary path
|
|
|
|
return app_path
|
|
|
|
|
|
|
|
|
|
|
|
class AT(IDFApp):
|
|
|
|
def get_binary_path(self, app_path):
|
|
|
|
# TODO: to implement AT get binary path
|
|
|
|
return app_path
|