Skip to content

Commit

Permalink
[shell] Print command result in Zephyr platform
Browse files Browse the repository at this point in the history
1. Add streamer_print_result() function for printing either:
   - "Done" or
   - "Error: <error_string_or_code>"
   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 <[email protected]>
  • Loading branch information
Damian-Nordic committed Aug 11, 2023
1 parent c57aec6 commit 5c436f8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
18 changes: 2 additions & 16 deletions src/lib/shell/MainLoopDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/lib/shell/MainLoopZephyr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@
#include <lib/core/CHIPError.h>
#include <lib/shell/Engine.h>
#include <lib/shell/streamer_zephyr.h>
#include <platform/CHIPDeviceLayer.h>

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()
Expand Down
13 changes: 13 additions & 0 deletions src/lib/shell/streamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "streamer.h"

#include <lib/support/CodeUtils.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/logging/Constants.h>
#include <limits.h>
Expand Down Expand Up @@ -87,5 +88,17 @@ void streamer_print_hex(streamer_t * self, const uint8_t * bytes, int len)
}
}

void streamer_print_result(streamer_t * self, CHIP_ERROR error)
{
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
3 changes: 3 additions & 0 deletions src/lib/shell/streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#pragma once

#include <lib/core/CHIPError.h>

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c436f8

Please sign in to comment.