From b6d46327f47f90fdf44314718bbb5ae8a98a4980 Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Tue, 9 Jul 2024 05:58:38 +0800 Subject: [PATCH] [otci] add network management API to otci (#10482) This commit adds high level Thread network management API 'create_dataset()', 'join()', 'leave()' and 'wait_for()' to otci. --- tools/otci/otci/otci.py | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/otci/otci/otci.py b/tools/otci/otci/otci.py index eb01ba3cb..e8e54c049 100644 --- a/tools/otci/otci/otci.py +++ b/tools/otci/otci/otci.py @@ -1790,6 +1790,11 @@ class OTCI(object): self.execute_command(cmd) + def get_dataset_tlvs_bytes(self) -> bytes: + """Gets bytes of the Operational Dataset TLVs""" + hexstr = self.__parse_str(self.execute_command('dataset tlvs')) + return self.__hex_to_bytes(hexstr) + def dataset_set_buffer(self, active_timestamp: Optional[int] = None, channel: Optional[int] = None, @@ -2543,6 +2548,55 @@ class OTCI(object): return True + # + # Network management utilities + # + def create_dataset(self, + active_timestamp: Optional[int] = None, + channel: Optional[int] = None, + channel_mask: Optional[int] = None, + extpanid: Optional[str] = None, + mesh_local_prefix: Optional[str] = None, + network_key: Optional[str] = None, + network_name: Optional[str] = None, + panid: Optional[int] = None, + pskc: Optional[str] = None, + security_policy: Optional[tuple] = None, + pending_timestamp: Optional[int] = None) -> bytes: + """Creates a new Operational Dataset with given parameters.""" + self.dataset_clear_buffer() + self.dataset_init_buffer() + self.dataset_set_buffer(active_timestamp, channel, channel_mask, extpanid, mesh_local_prefix, network_key, + network_name, panid, pskc, security_policy, pending_timestamp) + return self.get_dataset_tlvs_bytes() + + def join(self, dataset: bytes) -> None: + """Joins to a Thread network with given Active Operational Dataset.""" + self.set_dataset_bytes('active', dataset) + self.ifconfig_up() + self.thread_start() + + def leave(self) -> None: + """Leaves from the Thread network.""" + self.thread_stop() + self.ifconfig_down() + + def wait_for(self, command: str, expect_line: Optional[Union[str, Pattern, Collection[Any]]], timeout: float = 60): + """Wait for the expected output by periodically executing the given command.""" + success = False + + while timeout > 0: + output = self.execute_command(command) + if any(match_line(line, expect_line) for line in output): + success = True + break + + self.__otcmd.wait(1) + timeout -= 1 + + if not success: + raise ExpectLineTimeoutError(expect_line) + # # Other TODOs #