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""" 28import readline 29import shlex 30from argparse import ArgumentParser 31from ble.ble_stream_secure import BleStreamSecure 32from cli.base_commands import (DisconnectCommand, HelpCommand, HelloCommand, CommissionCommand, DecommissionCommand, 33 ExtractDatasetCommand, GetCommissionerCertificate, GetDeviceIdCommand, GetPskdHash, 34 GetExtPanIDCommand, GetNetworkNameCommand, GetProvisioningUrlCommand, PingCommand, 35 GetRandomNumberChallenge, ThreadStateCommand, ScanCommand, PresentHash) 36from .tlv_commands import TlvCommand 37from cli.dataset_commands import (DatasetCommand) 38from dataset.dataset import ThreadDataset 39from typing import Optional 40 41 42class CLI: 43 44 def __init__(self, 45 dataset: ThreadDataset, 46 cmd_args: Optional[ArgumentParser] = None, 47 ble_sstream: Optional[BleStreamSecure] = None): 48 self._commands = { 49 'help': HelpCommand(), 50 'hello': HelloCommand(), 51 'commission': CommissionCommand(), 52 'decommission': DecommissionCommand(), 53 'disconnect': DisconnectCommand(), 54 'device_id': GetDeviceIdCommand(), 55 'ext_panid': GetExtPanIDCommand(), 56 'provisioning_url': GetProvisioningUrlCommand(), 57 'network_name': GetNetworkNameCommand(), 58 'ping': PingCommand(), 59 'dataset': DatasetCommand(), 60 'get_dataset': ExtractDatasetCommand(), 61 'thread': ThreadStateCommand(), 62 'scan': ScanCommand(), 63 'random_challenge': GetRandomNumberChallenge(), 64 'present_hash': PresentHash(), 65 'peer_pskd_hash': GetPskdHash(), 66 'tlv': TlvCommand(), 67 'get_comm_cert': GetCommissionerCertificate(), 68 } 69 self._context = { 70 'ble_sstream': ble_sstream, 71 'dataset': dataset, 72 'commands': self._commands, 73 'cmd_args': cmd_args 74 } 75 readline.set_completer(self.completer) 76 readline.parse_and_bind('tab: complete') 77 78 def completer(self, text, state): 79 command_pool = self._commands.keys() 80 full_line = readline.get_line_buffer().lstrip() 81 words = full_line.split() 82 83 should_suggest_subcommands = len(words) > 1 or (len(words) == 1 and full_line[-1].isspace()) 84 if should_suggest_subcommands: 85 if words[0] not in self._commands.keys(): 86 return None 87 88 current_command = self._commands[words[0]] 89 if full_line[-1].isspace(): 90 subcommands = words[1:] 91 else: 92 subcommands = words[1:-1] 93 for nextarg in subcommands: 94 if nextarg in current_command._subcommands.keys(): 95 current_command = current_command._subcommands[nextarg] 96 else: 97 return None 98 99 if len(current_command._subcommands) == 0: 100 return None 101 102 command_pool = current_command._subcommands.keys() 103 104 options = [c for c in command_pool if c.startswith(text)] 105 if state < len(options): 106 return options[state] 107 else: 108 return None 109 110 async def evaluate_input(self, user_input): 111 # do not parse empty commands 112 if not user_input.strip(): 113 return 114 115 command_parts = shlex.split(user_input) 116 command = command_parts[0] 117 args = command_parts[1:] 118 119 if command not in self._commands.keys(): 120 raise Exception('Invalid command: {}'.format(command)) 121 122 return await self._commands[command].execute(args, self._context) 123