Skip to content

Commit

Permalink
RPC: Add tokenized log support to console (#16537)
Browse files Browse the repository at this point in the history
- Update Pigwee to 67506a1a448 to bring in console fix.
- Hide Python Repl when opened in raw mode.
- Add tokenized log support to console.
- Cleanup py setup so install is cleaner, and also add
  chip-console as an entrypoint to make it cleaner to run.
  • Loading branch information
rgoliver authored and pull[bot] committed Oct 9, 2023
1 parent 504b142 commit 92ce41c
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/ameba/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ to be On or Off.

- Launch the chip-rpc console after resetting Ameba board

$ python3 -m chip_rpc.console --device /dev/tty<port connected to USB-TTL adapter> -b 115200
$ chip-console --device /dev/tty<port connected to USB-TTL adapter> -b 115200

- Get and Set lighting directly using the RPC console

Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Build or install the [rpc console](../../common/pigweed/rpc_console/README.md)

Start the console

python -m chip_rpc.console --device /dev/ttyUSB0
chip-console --device /dev/ttyUSB0

From within the console you can then invoke rpcs:

Expand Down
1 change: 1 addition & 0 deletions examples/build_overrides/pigweed_environment.gni
2 changes: 1 addition & 1 deletion examples/common/pigweed/rpc_console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ files required for CHIP.

To start the console provide the path to the device, for example:

$ python -m chip_rpc.console --device /dev/ttyUSB0
$ chip-console --device /dev/ttyUSB0

An example RPC command:

Expand Down
15 changes: 6 additions & 9 deletions examples/common/pigweed/rpc_console/py/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ import("$dir_pw_build/python_dist.gni")
import("${chip_root}/examples/common/pigweed/pigweed_rpcs.gni")

pw_python_package("chip_rpc") {
generate_setup = {
metadata = {
name = "chip_rpc"
version = "0.0.1"
}
options = {
install_requires = [ "ipython" ]
}
}
setup = [
"pyproject.toml",
"setup.cfg",
"setup.py",
]
sources = [
"chip_rpc/__init__.py",
"chip_rpc/console.py",
"chip_rpc/plugins/device_toolbar.py",
"chip_rpc/plugins/helper_scripts.py",
Expand Down
38 changes: 32 additions & 6 deletions examples/common/pigweed/rpc_console/py/chip_rpc/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
rpcs - used to invoke RPCs
device - the serial device used for communication
client - the pw_rpc.Client
scripts - helper scripts for working with chip devices
protos - protocol buffer messages indexed by proto package
An example RPC command:
Expand All @@ -47,7 +46,7 @@
from concurrent.futures import ThreadPoolExecutor
import sys
import threading
from typing import Any, BinaryIO
from typing import Any, BinaryIO, Collection

from chip_rpc.plugins.device_toolbar import DeviceToolbar
from chip_rpc.plugins.helper_scripts import HelperScripts
Expand All @@ -59,6 +58,10 @@
from pw_rpc import callback_client
from pw_rpc.console_tools.console import ClientInfo, flattened_rpc_completions

from pw_tokenizer.database import LoadTokenDatabases
from pw_tokenizer.detokenize import Detokenizer, detokenize_base64
from pw_tokenizer import tokens

# Protos
from attributes_service import attributes_service_pb2
from button_service import button_service_pb2
Expand Down Expand Up @@ -112,6 +115,11 @@ def _parse_args():
'--raw_serial',
action="store_true",
help=('Use raw serial instead of HDLC/RPC'))
parser.add_argument("--token-databases",
metavar='elf_or_token_database',
nargs="+",
action=LoadTokenDatabases,
help="Path to tokenizer database csv file(s).")
group.add_argument('-s',
'--socket-addr',
type=str,
Expand Down Expand Up @@ -152,6 +160,7 @@ def _start_ipython_raw_terminal() -> None:

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()
Expand Down Expand Up @@ -236,7 +245,8 @@ def read(self, num_bytes: int = PW_RPC_MAX_PACKET_SIZE):


def write_to_output(data: bytes,
unused_output: BinaryIO = sys.stdout.buffer,):
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,
Expand Down Expand Up @@ -276,9 +286,17 @@ def write_to_output(data: bytes,
fields.update(match.groupdict())
if "level" in match.groupdict():
fields["level"] = LEVEL_MAPPING[fields["level"]]
if detokenizer:
_LOG.warn(fields["msg"])
if len(fields["msg"]) % 2:
# TODO the msg likely wrapped, trim for now
fields["msg"] = fields["msg"][:-1]
fields["msg"] = detokenizer.detokenize(
bytes.fromhex(fields["msg"]))
break

_DEVICE_LOG.log(fields["level"], fields["msg"], extra={'extra_metadata_fields': {
"time": fields["time"], "type": fields["type"], "mod": fields["mod"]}})
"timestamp": fields["time"], "type": fields["type"], "mod": fields["mod"]}})


def _read_raw_serial(read: Callable[[], bytes], output):
Expand All @@ -294,6 +312,7 @@ def _read_raw_serial(read: Callable[[], bytes], output):


def console(device: str, baudrate: int,
token_databases: Collection[tokens.Database],
socket_addr: str, output: Any, raw_serial: bool) -> int:
"""Starts an interactive RPC console for HDLC."""
# argparse.FileType doesn't correctly handle '-' for binary files.
Expand Down Expand Up @@ -323,15 +342,22 @@ def read(): return serial_device.read(8192)
default_stream_timeout_s=None,
)

detokenizer = Detokenizer(tokens.Database.merged(*token_databases),
show_errors=False) if token_databases else None

if raw_serial:
threading.Thread(target=_read_raw_serial,
daemon=True,
args=(read, write_to_output)).start()
args=(read,
lambda data: write_to_output(
data, output, detokenizer),
)).start()
_start_ipython_raw_terminal()
else:
_start_ipython_hdlc_terminal(
HdlcRpcClient(read, PROTOS, default_channels(write),
lambda data: write_to_output(data, output),
lambda data: write_to_output(
data, output, detokenizer),
client_impl=callback_client_impl)
)
return 0
Expand Down
16 changes: 16 additions & 0 deletions examples/common/pigweed/rpc_console/py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[build-system]
requires = ['setuptools', 'wheel']
build-backend = 'setuptools.build_meta'
29 changes: 29 additions & 0 deletions examples/common/pigweed/rpc_console/py/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[metadata]
name = chip_rpc
version = 0.0.1

[options]
packages = find:
zip_safe = False
install_requires =
pw_console
pw_hdlc
pw_protobuf_compiler
pw_rpc
pw_tokenizer

[options.entry_points]
console_scripts = chip-console = chip_rpc.console:main
17 changes: 17 additions & 0 deletions examples/common/pigweed/rpc_console/py/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import setuptools # type: ignore

setuptools.setup() # Package definition in setup.cfg
2 changes: 1 addition & 1 deletion examples/ipv6only-app/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Build or install the [rpc console](../../common/pigweed/rpc_console/README.md)

Start the console:

$ python -m chip_rpc.console --device /dev/ttyUSB0 -b 115200
$ chip-console --device /dev/ttyUSB0 -b 115200

An example flow of performing a scan, connecting, and getting the IPv6 address:

Expand Down
2 changes: 1 addition & 1 deletion examples/light-switch-app/efr32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ via 2002::2
- To use the chip-rpc console after it has been installed run:
`python3 -m chip_rpc.console --device /dev/tty.<SERIALDEVICE> -b 115200 -o /<YourFolder>/pw_log.out`
`chip-console --device /dev/tty.<SERIALDEVICE> -b 115200 -o /<YourFolder>/pw_log.out`
- Then you can simulate a button press or release using the following command
where : idx = 0 or 1 for Button PB0 or PB1 action = 0 for PRESSED, 1 for
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/efr32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ via 2002::2
`pip3 install out/debug/chip_rpc_console_wheels/*.whl`
- To use the chip-rpc console after it has been installed run:
`python3 -m chip_rpc.console --device /dev/tty.<SERIALDEVICE> -b 115200 -o /<YourFolder>/pw_log.out`
`chip-console --device /dev/tty.<SERIALDEVICE> -b 115200 -o /<YourFolder>/pw_log.out`
- Then you can simulate a button press or release using the following command
where : idx = 0 or 1 for Button PB0 or PB1 action = 0 for PRESSED, 1 for
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ To cross-compile this example on x64 host and run on **NXP i.MX 8M Mini**
`pip3 install out/debug/chip_rpc_console_wheels/*.whl`

- To use the chip-rpc console after it has been installed run:
`python3 -m chip_rpc.console -s localhost:33000 -o /<YourFolder>/pw_log.out`
`chip-console -s localhost:33000 -o /<YourFolder>/pw_log.out`

- Then you can Get and Set the light using the RPCs:
`rpcs.chip.rpc.Lighting.Get()`
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/mbed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ parameters as arguments:

Example:

python -m chip_rpc.console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out
chip-console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out

To control the lighting type the following command, where you define if 'on'
state is true or false:
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ Build or install the [rpc console](../../common/pigweed/rpc_console/README.md)

Start the console

$ python -m chip_rpc.console --device /dev/ttyUSB0
$ chip-console --device /dev/ttyUSB0

From within the console you can then invoke rpcs:

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Build or install the [rpc console](../../common/pigweed/rpc_console/README.md)

Start the console

python -m chip_rpc.console --device /dev/ttyUSB0
chip-console --device /dev/ttyUSB0

From within the console you can then invoke rpcs:

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/mbed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ parameters as arguments:

Example:

python -m chip_rpc.console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out
chip-console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out

To control the lock type the following command, where you define if 'on' state
is true or false:
Expand Down
2 changes: 1 addition & 1 deletion examples/pigweed-app/mbed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ parameters as arguments:

Example:

python -m chip_rpc.console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out
chip-console -d /dev/ttyUSB0 -b 115200 -o /tmp/pw_rpc.out

To send the echo message type the following command, where you define the
message content:
Expand Down
2 changes: 1 addition & 1 deletion third_party/pigweed/repo
Submodule repo updated 86 files
+1 −0 docs/faq.rst
+2 −2 pw_arduino_build/arduino.gni
+1 −1 pw_assert_tokenized/public/pw_assert_tokenized/check_tokenized.h
+39 −0 pw_build/bazel_internal/BUILD.bazel
+21 −0 pw_build/bazel_internal/BUILD.gn
+51 −0 pw_build/bazel_internal/linker_script.ld
+65 −1 pw_build/bazel_internal/pigweed_internal.bzl
+17 −0 pw_build/bazel_internal/test.cc
+27 −0 pw_build/docs.rst
+1 −1 pw_build/pigweed.bzl
+5 −2 pw_console/BUILD.gn
+7 −1 pw_console/docs.rst
+1,984 −0 pw_console/images/command_runner_main_menu.svg
+ pw_console/images/pw_system_boot.png
+ pw_console/images/python_completion.png
+2 −0 pw_console/py/BUILD.gn
+258 −0 pw_console/py/command_runner_test.py
+5 −5 pw_console/py/help_window_test.py
+3 −1 pw_console/py/log_view_test.py
+515 −0 pw_console/py/pw_console/command_runner.py
+147 −93 pw_console/py/pw_console/console_app.py
+78 −1 pw_console/py/pw_console/console_prefs.py
+226 −17 pw_console/py/pw_console/docs/user_guide.rst
+15 −2 pw_console/py/pw_console/embed.py
+57 −13 pw_console/py/pw_console/help_window.py
+98 −20 pw_console/py/pw_console/key_bindings.py
+38 −33 pw_console/py/pw_console/log_pane.py
+2 −3 pw_console/py/pw_console/log_pane_saveas_dialog.py
+37 −12 pw_console/py/pw_console/plugins/calc_pane.py
+3 −5 pw_console/py/pw_console/quit_dialog.py
+14 −10 pw_console/py/pw_console/repl_pane.py
+10 −12 pw_console/py/pw_console/search_toolbar.py
+26 −4 pw_console/py/pw_console/style.py
+13 −3 pw_console/py/pw_console/widgets/border.py
+10 −0 pw_console/py/pw_console/widgets/window_pane.py
+6 −4 pw_console/py/pw_console/widgets/window_pane_toolbar.py
+13 −0 pw_console/py/pw_console/window_list.py
+18 −19 pw_console/py/pw_console/window_manager.py
+12 −0 pw_console/py/pw_console/yaml_config_loader_mixin.py
+41 −41 pw_console/py/window_manager_test.py
+0 −8 pw_docgen/docs.rst
+0 −5 pw_docgen/py/setup.cfg
+8 −0 pw_env_setup/docs.rst
+5 −0 pw_env_setup/get_pw_env_setup.sh
+4 −4 pw_env_setup/py/pw_env_setup/cipd_setup/pigweed.json
+7 −1 pw_env_setup/py/pw_env_setup/cipd_setup/update.py
+14 −4 pw_env_setup/py/pw_env_setup/cipd_setup/wrapper.py
+0 −11 pw_env_setup/py/pw_env_setup/virtualenv_setup/constraint.list
+1 −1 pw_env_setup/pypi_common_setup.cfg
+1 −1 pw_fuzzer/fuzzer.bzl
+28 −0 pw_kvs/key_value_store_initialized_test.cc
+5 −1 pw_kvs/public/pw_kvs/key_value_store.h
+15 −2 pw_protobuf/BUILD.bazel
+9 −3 pw_protobuf/BUILD.gn
+13 −2 pw_protobuf/CMakeLists.txt
+639 −0 pw_protobuf/codegen_decoder_test.cc
+37 −2 pw_protobuf/codegen_encoder_test.cc
+59 −7 pw_protobuf/docs.rst
+25 −24 pw_protobuf/public/pw_protobuf/stream_decoder.h
+6 −0 pw_protobuf/pw_protobuf_test_protos/imported.proto
+4 −0 pw_protobuf/pw_protobuf_test_protos/importer.proto
+1 −0 pw_protobuf/pw_protobuf_test_protos/repeated.proto
+566 −60 pw_protobuf/py/pw_protobuf/codegen_pwpb.py
+6 −1 pw_protobuf/stream_decoder.cc
+6 −1 pw_protobuf_compiler/proto.gni
+13 −15 pw_snapshot/module_usage.rst
+4 −0 pw_sync_freertos/thread_notification.cc
+4 −0 pw_sync_freertos/timed_thread_notification.cc
+21 −8 pw_system/BUILD.bazel
+20 −16 pw_system/BUILD.gn
+85 −80 pw_system/CMakeLists.txt
+12 −0 pw_system/backend.gni
+57 −4 pw_system/docs.rst
+2 −3 pw_system/hdlc_rpc_server.cc
+2 −2 pw_system/init.cc
+2 −2 pw_system/log.cc
+4 −2 pw_system/public/pw_system/rpc_server.h
+1 −0 pw_system/system_target.gni
+3 −2 pw_toolchain/host_clang/BUILD.gn
+2 −2 pw_transfer/client_test.cc
+47 −14 pw_transfer/context.cc
+7 −1 pw_transfer/public/pw_transfer/internal/context.h
+20 −0 pw_transfer/py/pw_transfer/transfer.py
+94 −2 pw_transfer/transfer_test.cc
+30 −1 pw_transfer/ts/transfer.ts
+3 −1 pw_transfer/ts/transfer_test.ts

0 comments on commit 92ce41c

Please sign in to comment.