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 31from ble.ble_stream_secure import BleStreamSecure 32 33from abc import ABC, abstractmethod 34 35 36class CommandResult(ABC): 37 38 def __init__(self, value=None): 39 self.value = value 40 41 @abstractmethod 42 def pretty_print(self): 43 pass 44 45 46class Command(ABC): 47 48 def __init__(self): 49 self._subcommands = {} 50 51 async def execute(self, args, context) -> CommandResult: 52 if len(args) > 0 and args[0] in self._subcommands.keys(): 53 return await self.execute_subcommand(args, context) 54 55 return await self.execute_default(args, context) 56 57 async def execute_subcommand(self, args, context) -> CommandResult: 58 return await self._subcommands[args[0]].execute(args[1:], context) 59 60 @abstractmethod 61 async def execute_default(self, args, context): 62 pass 63 64 @abstractmethod 65 def get_help_string(self) -> str: 66 pass 67 68 def print_help(self, indent=0): 69 indent_width = 4 70 indentation = ' ' * indent_width * indent 71 print(f'{indentation}{self.get_help_string()}') 72 73 if 'help' in self._subcommands.keys(): 74 print(f'{indentation}"help" command available.') 75 elif len(self._subcommands) != 0: 76 print(f'{indentation}Subcommands:') 77 for name, sc in self._subcommands.items(): 78 print(f'{indentation}{" " * indent_width}{name}\t- ', end='') 79 sc.print_help() 80 81 82class CommandResultTLV(CommandResult): 83 84 def pretty_print(self): 85 tlv: TLV = self.value 86 tlv_type = TcatTLVType.from_value(tlv.type) 87 print('Result: TLV:') 88 if tlv_type is not None: 89 print(f'\tTYPE:\t{TcatTLVType.from_value(tlv.type).name}') 90 else: 91 print(f'\tTYPE:\tunknown: {hex(tlv.type)} ({tlv.type})') 92 print(f'\tLEN:\t{len(tlv.value)}') 93 if tlv_type == TcatTLVType.APPLICATION: 94 print(f'\tVALUE:\t{tlv.value.decode("ascii")}') 95 else: 96 print(f'\tVALUE:\t0x{tlv.value.hex()}') 97 98 99class CommandResultNone(CommandResult): 100 101 def pretty_print(self): 102 pass 103