diff --git a/examples/common/pigweed/rpc_console/py/BUILD.gn b/examples/common/pigweed/rpc_console/py/BUILD.gn index d250129dca13c5..5c74b7b2599544 100644 --- a/examples/common/pigweed/rpc_console/py/BUILD.gn +++ b/examples/common/pigweed/rpc_console/py/BUILD.gn @@ -37,6 +37,7 @@ pw_python_package("chip_rpc") { "$dir_pw_log_tokenized/py", "$dir_pw_protobuf_compiler/py", "$dir_pw_rpc/py", + "$dir_pw_system/py", "$dir_pw_tokenizer/py", "${chip_root}/examples/common/pigweed:attributes_service.python", "${chip_root}/examples/common/pigweed:button_service.python", diff --git a/examples/common/pigweed/rpc_console/py/chip_rpc/console.py b/examples/common/pigweed/rpc_console/py/chip_rpc/console.py index 7880d579fccf52..6d2b06fd73208f 100644 --- a/examples/common/pigweed/rpc_console/py/chip_rpc/console.py +++ b/examples/common/pigweed/rpc_console/py/chip_rpc/console.py @@ -29,35 +29,20 @@ rpcs - used to invoke RPCs device - the serial device used for communication - client - the pw_rpc.Client + client - the HDLC rpc client protos - protocol buffer messages indexed by proto package An example RPC command: - rpcs.chip.rpc.DeviceCommon.GetDeviceInfo() + rpcs.chip.rpc.Device.GetDeviceInfo() + device.rpcs.chip.rpc.Device.GetDeviceInfo() """ - import argparse -import logging -import re import sys -import threading -from collections import namedtuple -from concurrent.futures import ThreadPoolExecutor -from inspect import cleandoc -from typing import Any, BinaryIO, Callable, Collection +from pathlib import Path +from typing import Any, Collection -import pw_cli.log -from chip_rpc.plugins.device_toolbar import DeviceToolbar -from chip_rpc.plugins.helper_scripts import HelperScripts -from pw_console import PwConsoleEmbed, socket_client -from pw_console.__main__ import create_temp_log_file -from pw_console.pyserial_wrapper import SerialWithLogging -from pw_hdlc.rpc import HdlcRpcClient, SelectableReader, SerialReader, default_channels -from pw_rpc import callback_client -from pw_rpc.console_tools.console import ClientInfo, flattened_rpc_completions -from pw_tokenizer import tokens -from pw_tokenizer.database import LoadTokenDatabases -from pw_tokenizer.detokenize import Detokenizer +import pw_system.console +from pw_hdlc import rpc # Protos # isort: off @@ -72,24 +57,6 @@ from thread_service import thread_service_pb2 from wifi_service import wifi_service_pb2 -_LOG = logging.getLogger(__name__) -_DEVICE_LOG = logging.getLogger('rpc_device') - -PW_RPC_MAX_PACKET_SIZE = 256 -SOCKET_SERVER = 'localhost' -SOCKET_PORT = 33000 - -PROTOS = [attributes_service_pb2, - button_service_pb2, - descriptor_service_pb2, - device_service_pb2, - echo_pb2, - lighting_service_pb2, - locking_service_pb2, - ot_cli_service_pb2, - thread_service_pb2, - wifi_service_pb2] - def _parse_args(): """Parses and returns the command line arguments.""" @@ -116,7 +83,7 @@ def _parse_args(): parser.add_argument("--token-databases", metavar='elf_or_token_database', nargs="+", - action=LoadTokenDatabases, + type=Path, help="Path to tokenizer database csv file(s).") group.add_argument('-s', '--socket-addr', @@ -126,227 +93,55 @@ def _parse_args(): return parser.parse_args() -def _start_ipython_raw_terminal() -> None: - """Starts an interactive IPython terminal with preset variables. This raw - terminal does not use HDLC and provides no RPC functionality, this is - just a serial log viewer.""" - local_variables = dict( - LOG=_DEVICE_LOG, - ) - - welcome_message = cleandoc(""" - Welcome to the CHIP Console! - - This has been started in raw serial mode, - and all RPC functionality is disabled. - - Press F1 for help. - """) - - interactive_console = PwConsoleEmbed( - global_vars=local_variables, - local_vars=None, - loggers={ - 'Device Logs': [_DEVICE_LOG], - 'Host Logs': [logging.getLogger()], - 'Serial Debug': [logging.getLogger('pw_console.serial_debug_logger')], - }, - repl_startup_message=welcome_message, - help_text=__doc__, - app_title="CHIP Console", - ) - - interactive_console.hide_windows('Host Logs') - interactive_console.hide_windows('Serial Debug') - interactive_console.hide_windows('Python Repl') - - # Setup Python logger propagation - interactive_console.setup_python_logging() - # Don't send device logs to the root logger. - _DEVICE_LOG.propagate = False - interactive_console.embed() - - -def _start_ipython_hdlc_terminal(client: HdlcRpcClient) -> None: - """Starts an interactive IPython terminal with preset variables.""" - local_variables = dict( - client=client, - channel_client=client.client.channel(1), - rpcs=client.client.channel(1).rpcs, - scripts=HelperScripts(client.client.channel(1).rpcs), - protos=client.protos.packages, - # Include the active pane logger for creating logs in the repl. - LOG=_DEVICE_LOG, - ) - - client_info = ClientInfo('channel_client', - client.client.channel(1).rpcs, client.client) - completions = flattened_rpc_completions([client_info]) - - welcome_message = cleandoc(""" - Welcome to the CHIP RPC Console! - - Press F1 for help. - Example commands: - - rpcs.chip.rpc.Device.GetDeviceInfo() - - LOG.warning('Message appears console log window.') - """) - - interactive_console = PwConsoleEmbed( - global_vars=local_variables, - local_vars=None, - loggers={ - 'Device Logs': [_DEVICE_LOG], - 'Host Logs': [logging.getLogger()], - 'Serial Debug': [logging.getLogger('pw_console.serial_debug_logger')], - }, - repl_startup_message=welcome_message, - help_text=__doc__, - app_title="CHIP Console", - ) - - interactive_console.add_sentence_completer(completions) - interactive_console.add_bottom_toolbar( - DeviceToolbar(client.client.channel(1).rpcs)) - - interactive_console.hide_windows('Host Logs') - interactive_console.hide_windows('Serial Debug') - - # Setup Python logger propagation - interactive_console.setup_python_logging() - # Don't send device logs to the root logger. - _DEVICE_LOG.propagate = False - with client: - interactive_console.embed() - - -def write_to_output(data: bytes, - unused_output: BinaryIO = sys.stdout.buffer, - detokenizer=None): - log_line = data - RegexStruct = namedtuple('RegexStruct', 'platform type regex match_num') - LEVEL_MAPPING = {"I": logging.INFO, "W": logging.WARNING, "P": logging.INFO, - "E": logging.ERROR, "F": logging.FATAL, "V": logging.DEBUG, "D": logging.DEBUG, - "": logging.INFO, "": logging.DEBUG, "": logging.ERROR, - "": logging.INFO, "": logging.WARNING, - "": logging.ERROR, "": logging.DEBUG, - "ERR": logging.ERROR, "DBG": logging.DEBUG, "INF": logging.INFO} - - ESP_CHIP_REGEX = r"(?P[IWEFV]) \((?P