• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2  Copyright (c) 2024, The OpenThread Authors.
3  All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  1. Redistributions of source code must retain the above copyright
8     notice, this list of conditions and the following disclaimer.
9  2. Redistributions in binary form must reproduce the above copyright
10     notice, this list of conditions and the following disclaimer in the
11     documentation and/or other materials provided with the distribution.
12  3. Neither the name of the copyright holder nor the
13     names of its contributors may be used to endorse or promote products
14     derived from this software without specific prior written permission.
15
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  POSSIBILITY OF SUCH DAMAGE.
27"""
28
29from tlv.tlv import TLV
30from tlv.tcat_tlv import TcatTLVType
31
32from abc import ABC, abstractmethod
33
34
35class CommandResult(ABC):
36
37    def __init__(self, value=None):
38        self.value = value
39
40    @abstractmethod
41    def pretty_print(self):
42        pass
43
44
45class Command(ABC):
46
47    def __init__(self):
48        self._subcommands = {}
49
50    async def execute(self, args, context) -> CommandResult:
51        if len(args) > 0 and args[0] in self._subcommands.keys():
52            return await self.execute_subcommand(args, context)
53
54        return await self.execute_default(args, context)
55
56    async def execute_subcommand(self, args, context) -> CommandResult:
57        return await self._subcommands[args[0]].execute(args[1:], context)
58
59    @abstractmethod
60    async def execute_default(self, args, context) -> CommandResult:
61        pass
62
63    @abstractmethod
64    def get_help_string(self) -> str:
65        pass
66
67    def print_help(self, indent=0):
68        indent_width = 4
69        indentation = ' ' * indent_width * indent
70        print(f'{indentation}{self.get_help_string()}')
71
72        if 'help' in self._subcommands.keys():
73            print(f'{indentation}"help" command available.')
74        elif len(self._subcommands) != 0:
75            print(f'{indentation}Subcommands:')
76            for name, sc in self._subcommands.items():
77                print(f'{indentation}{" " * indent_width}{name}\t- ', end='')
78                sc.print_help()
79
80
81class CommandResultTLV(CommandResult):
82
83    def pretty_print(self):
84        tlv: TLV = self.value
85        tlv_type = TcatTLVType.from_value(tlv.type)
86        print('Result: TLV:')
87        if tlv_type is not None:
88            print(f'\tTYPE:\t{TcatTLVType.from_value(tlv.type).name}')
89        else:
90            print(f'\tTYPE:\tunknown: {hex(tlv.type)} ({tlv.type})')
91        print(f'\tLEN:\t{len(tlv.value)}')
92        if tlv_type == TcatTLVType.APPLICATION:
93            print(f'\tVALUE:\t{tlv.value.decode("ascii")}')
94        else:
95            print(f'\tVALUE:\t0x{tlv.value.hex()}')
96
97
98class CommandResultNone(CommandResult):
99
100    def pretty_print(self):
101        pass
102