Skip to content

Commit

Permalink
[shell] Correct order of atexit and shutdown handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
turon committed Dec 17, 2021
1 parent 01c86bb commit d38f55b
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/platform/efr32/matter_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions examples/shell/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/shell/mbed/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/shell/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/lib/shell/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
9 changes: 9 additions & 0 deletions src/lib/shell/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
1 change: 0 additions & 1 deletion src/lib/shell/MainLoopDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ namespace Shell {

void Engine::RunMainLoop()
{
Engine::Root().RegisterDefaultCommands();
streamer_printf(streamer_get(), CHIP_SHELL_PROMPT);

while (true)
Expand Down
1 change: 0 additions & 1 deletion src/lib/shell/MainLoopEFR32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ namespace Shell {

void Engine::RunMainLoop()
{
Engine::Root().RegisterDefaultCommands();
streamer_printf(streamer_get(), kShellPrompt);

while (true)
Expand Down
4 changes: 0 additions & 4 deletions src/lib/shell/MainLoopESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/shell/commands/Meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,6 +69,8 @@ void RegisterMetaCommands()
{ &VersionHandler, "version", "Output the software version" },
};

std::atexit(AtExitShell);

Engine::Root().RegisterCommands(sCmds, ArraySize(sCmds));
}

Expand Down

0 comments on commit d38f55b

Please sign in to comment.