From d38f55bec167273f3d0f5be31c8f545987643b16 Mon Sep 17 00:00:00 2001 From: Martin Turon Date: Thu, 16 Dec 2021 19:21:43 -0800 Subject: [PATCH] [shell] Correct order of atexit and shutdown handling. --- examples/platform/efr32/matter_shell.cpp | 2 +- examples/shell/esp32/main/main.cpp | 4 ++++ examples/shell/mbed/main/main.cpp | 2 +- examples/shell/standalone/main.cpp | 4 ++-- src/lib/shell/Engine.cpp | 10 ++++++++++ src/lib/shell/Engine.h | 9 +++++++++ src/lib/shell/MainLoopDefault.cpp | 1 - src/lib/shell/MainLoopEFR32.cpp | 1 - src/lib/shell/MainLoopESP32.cpp | 4 ---- src/lib/shell/commands/Meta.cpp | 8 +++++++- 10 files changed, 34 insertions(+), 11 deletions(-) diff --git a/examples/platform/efr32/matter_shell.cpp b/examples/platform/efr32/matter_shell.cpp index 50b5af84947a91..d398a41a0c7757 100644 --- a/examples/platform/efr32/matter_shell.cpp +++ b/examples/platform/efr32/matter_shell.cpp @@ -71,7 +71,7 @@ void WaitForShellActivity() void startShellTask() { - int status = chip::Shell::streamer_init(chip::Shell::streamer_get()); + int status = chip::Shell::Engine::Root().Init(); assert(status == 0); // For now also register commands from shell_common (shell app). diff --git a/examples/shell/esp32/main/main.cpp b/examples/shell/esp32/main/main.cpp index 7f6879663c9080..1e990aad525e23 100644 --- a/examples/shell/esp32/main/main.cpp +++ b/examples/shell/esp32/main/main.cpp @@ -36,6 +36,10 @@ extern "C" void app_main(void) chip::Platform::MemoryInit(); chip::DeviceLayer::PlatformMgr().InitChipStack(); chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); + + int ret = Engine::Root().Init(); + assert(ret == 0); + cmd_ping_init(); xTaskCreate(&chip_shell_task, "chip_shell", 2048, NULL, 5, NULL); } diff --git a/examples/shell/mbed/main/main.cpp b/examples/shell/mbed/main/main.cpp index 7865283deda515..cc1b627ead8e8b 100644 --- a/examples/shell/mbed/main/main.cpp +++ b/examples/shell/mbed/main/main.cpp @@ -84,7 +84,7 @@ int main() } // Initialize the default streamer that was linked. - ret = streamer_init(streamer_get()); + ret = Engine::Root().Init(); if (ret) { ChipLogError(Shell, "Streamer initialization failed [%d]", ret); diff --git a/examples/shell/standalone/main.cpp b/examples/shell/standalone/main.cpp index 363f18caa8aaec..0335bbb6e497e0 100644 --- a/examples/shell/standalone/main.cpp +++ b/examples/shell/standalone/main.cpp @@ -38,8 +38,8 @@ int main() #if CHIP_DEVICE_CONFIG_ENABLE_WPA chip::DeviceLayer::ConnectivityManagerImpl().StartWiFiManagement(); #endif - // Initialize the default streamer that was linked. - const int rc = streamer_init(streamer_get()); + + const int rc = Engine::Root().Init(); if (rc != 0) { diff --git a/src/lib/shell/Engine.cpp b/src/lib/shell/Engine.cpp index 5c7b10bf4364c0..10bb2dcfa6723a 100644 --- a/src/lib/shell/Engine.cpp +++ b/src/lib/shell/Engine.cpp @@ -44,6 +44,16 @@ namespace Shell { Engine Engine::theEngineRoot; +int Engine::Init() +{ + // Initialize the default streamer that was linked. + int error = streamer_init(streamer_get()); + + Engine::Root().RegisterDefaultCommands(); + + return error; +} + void Engine::ForEachCommand(shell_command_iterator_t * on_command, void * arg) { for (unsigned i = 0; i < _commandSetCount; i++) diff --git a/src/lib/shell/Engine.h b/src/lib/shell/Engine.h index 7c6238857f3f2a..be65ed45c94dc0 100644 --- a/src/lib/shell/Engine.h +++ b/src/lib/shell/Engine.h @@ -152,6 +152,15 @@ class Engine */ void RunMainLoop(); + /** + * Initialize the Shell::Engine. + * + * Activates the linked streamer, registers default commands, and sets up exit handlers. + * + * @return 0 for success, otherwise failed. + */ + int Init(); + private: static void ProcessShellLineTask(intptr_t context); }; diff --git a/src/lib/shell/MainLoopDefault.cpp b/src/lib/shell/MainLoopDefault.cpp index 34e393c1c741f0..f077a7cab3b8d6 100644 --- a/src/lib/shell/MainLoopDefault.cpp +++ b/src/lib/shell/MainLoopDefault.cpp @@ -181,7 +181,6 @@ namespace Shell { void Engine::RunMainLoop() { - Engine::Root().RegisterDefaultCommands(); streamer_printf(streamer_get(), CHIP_SHELL_PROMPT); while (true) diff --git a/src/lib/shell/MainLoopEFR32.cpp b/src/lib/shell/MainLoopEFR32.cpp index 32f4a1cc2012e4..3b1f298d5cfb55 100644 --- a/src/lib/shell/MainLoopEFR32.cpp +++ b/src/lib/shell/MainLoopEFR32.cpp @@ -185,7 +185,6 @@ namespace Shell { void Engine::RunMainLoop() { - Engine::Root().RegisterDefaultCommands(); streamer_printf(streamer_get(), kShellPrompt); while (true) diff --git a/src/lib/shell/MainLoopESP32.cpp b/src/lib/shell/MainLoopESP32.cpp index 8351c62721bc16..aecca35774a07e 100644 --- a/src/lib/shell/MainLoopESP32.cpp +++ b/src/lib/shell/MainLoopESP32.cpp @@ -70,10 +70,6 @@ namespace Shell { void Engine::RunMainLoop() { - int ret = chip::Shell::streamer_init(chip::Shell::streamer_get()); - assert(ret == 0); - - Engine::Root().RegisterDefaultCommands(); while (true) { const char * prompt = LOG_COLOR_I "> " LOG_RESET_COLOR; diff --git a/src/lib/shell/commands/Meta.cpp b/src/lib/shell/commands/Meta.cpp index 67490cd22be64c..7bf188de763e43 100644 --- a/src/lib/shell/commands/Meta.cpp +++ b/src/lib/shell/commands/Meta.cpp @@ -40,11 +40,15 @@ namespace Shell { static CHIP_ERROR ExitHandler(int argc, char ** argv) { streamer_printf(streamer_get(), "Goodbye\r\n"); - chip::DeviceLayer::PlatformMgr().Shutdown(); exit(0); return CHIP_NO_ERROR; } +static void AtExitShell() +{ + chip::DeviceLayer::PlatformMgr().Shutdown(); +} + static CHIP_ERROR HelpHandler(int argc, char ** argv) { Engine::Root().ForEachCommand(PrintCommandHelp, nullptr); @@ -65,6 +69,8 @@ void RegisterMetaCommands() { &VersionHandler, "version", "Output the software version" }, }; + std::atexit(AtExitShell); + Engine::Root().RegisterCommands(sCmds, ArraySize(sCmds)); }