From f3ff3487f89e8ee3f5625175de96762223165497 Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Thu, 10 Aug 2023 13:21:40 +0200 Subject: [PATCH] [shell] Print command result in Zephyr platform 1. Add streamer_print_result() function for printing either: - "Done" or - "Error: " after each shell command and use that in the default and Zephyr shell implementation. Before that Zephyr would not print the result at all (unlike other platforms). 2. Lock Matter stack before running a shell command on Zephyr platform. Signed-off-by: Damian Krolik --- src/lib/shell/MainLoopDefault.cpp | 18 ++---------------- src/lib/shell/MainLoopZephyr.cpp | 13 +++++++++++-- src/lib/shell/streamer.cpp | 15 +++++++++++++++ src/lib/shell/streamer.h | 3 +++ 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/lib/shell/MainLoopDefault.cpp b/src/lib/shell/MainLoopDefault.cpp index fba29a7238f161..85f892d7fdee56 100644 --- a/src/lib/shell/MainLoopDefault.cpp +++ b/src/lib/shell/MainLoopDefault.cpp @@ -163,22 +163,8 @@ void ProcessShellLine(intptr_t args) if (argc > 0) { - CHIP_ERROR retval = Engine::Root().ExecCommand(argc, argv); - - if (retval != CHIP_NO_ERROR) - { - char errorStr[160]; - bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval); - if (!errorStrFound) - { - errorStr[0] = 0; - } - streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr); - } - else - { - streamer_printf(streamer_get(), "Done\r\n", argv[0]); - } + CHIP_ERROR error = Engine::Root().ExecCommand(argc, argv); + streamer_print_result(streamer_get(), error); } MemoryFree(line); streamer_printf(streamer_get(), CHIP_SHELL_PROMPT); diff --git a/src/lib/shell/MainLoopZephyr.cpp b/src/lib/shell/MainLoopZephyr.cpp index d106bfa89fa13b..42bcbdf9332547 100644 --- a/src/lib/shell/MainLoopZephyr.cpp +++ b/src/lib/shell/MainLoopZephyr.cpp @@ -20,13 +20,22 @@ #include #include #include +#include using chip::Shell::Engine; static int cmd_matter(const struct shell * shell, size_t argc, char ** argv) { - chip::Shell::streamer_set_shell(shell); - return (Engine::Root().ExecCommand(argc - 1, argv + 1) == CHIP_NO_ERROR) ? 0 : -ENOEXEC; + CHIP_ERROR error; + + { + chip::DeviceLayer::StackLock lock; + chip::Shell::streamer_set_shell(shell); + error = Engine::Root().ExecCommand(argc - 1, argv + 1); + chip::Shell::streamer_print_result(chip::Shell::streamer_get(), error); + } + + return error == CHIP_NO_ERROR ? 0 : -ENOEXEC; } static int RegisterCommands() diff --git a/src/lib/shell/streamer.cpp b/src/lib/shell/streamer.cpp index e44352bc53f20c..73b8ffc788217f 100644 --- a/src/lib/shell/streamer.cpp +++ b/src/lib/shell/streamer.cpp @@ -23,6 +23,7 @@ #include "streamer.h" +#include #include #include #include @@ -87,5 +88,19 @@ void streamer_print_hex(streamer_t * self, const uint8_t * bytes, int len) } } +void streamer_print_result(streamer_t * self, CHIP_ERROR error) +{ + VerifyOrReturn(error != CHIP_ERROR_IN_PROGRESS); + + if (error != CHIP_NO_ERROR) + { + streamer_printf(self, "Error: %" CHIP_ERROR_FORMAT "\r\n", error.Format()); + } + else + { + streamer_printf(self, "Done\r\n"); + } +} + } // namespace Shell } // namespace chip diff --git a/src/lib/shell/streamer.h b/src/lib/shell/streamer.h index 700ed9aac72eae..c5dce91bbab244 100644 --- a/src/lib/shell/streamer.h +++ b/src/lib/shell/streamer.h @@ -23,6 +23,8 @@ #pragma once +#include + #include #include #include @@ -51,6 +53,7 @@ ssize_t streamer_write(streamer_t * self, const char * buf, size_t len); ssize_t streamer_vprintf(streamer_t * self, const char * fmt, va_list ap); ssize_t streamer_printf(streamer_t * self, const char * fmt, ...); void streamer_print_hex(streamer_t * self, const uint8_t * data, int len); +void streamer_print_result(streamer_t * self, CHIP_ERROR error); streamer_t * streamer_get(); } // namespace Shell