Skip to content

Commit

Permalink
[shell] enable shell as an optional module in apps (project-chip#7080)
Browse files Browse the repository at this point in the history
This PR allows users to build shell into the devices for debugging and
testing purpose.
  • Loading branch information
gjc13 authored May 27, 2021
1 parent 4262811 commit c8f286f
Show file tree
Hide file tree
Showing 29 changed files with 399 additions and 496 deletions.
7 changes: 7 additions & 0 deletions config/esp32/components/chip/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ COMPONENT_ADD_INCLUDEDIRS = project-config \
COMPONENT_ADD_LDFLAGS = -L$(OUTPUT_DIR)/lib/ \
-lCHIP

ifdef CONFIG_ENABLE_CHIP_SHELL
COMPONENT_ADD_LDFLAGS += -lCHIPShell
endif

ifdef CONFIG_ENABLE_PW_RPC
COMPONENT_ADD_LDFLAGS += -lPwRpc
endif
Expand Down Expand Up @@ -135,6 +139,9 @@ endif
echo "pw_sys_io_BACKEND = \"//third_party/connectedhomeip/examples/platform/esp32/pw_sys_io:pw_sys_io_esp32\"" >> $(OUTPUT_DIR)/args.gn ;\
echo "dir_pw_third_party_nanopb = \"//third_party/connectedhomeip/third_party/nanopb/repo\"" >>$(OUTPUT_DIR)/args.gn ;\
fi
if [[ "$(CONFIG_ENABLE_CHIP_SHELL)" = "y" ]]; then \
echo "chip_build_libshell = true" >> $(OUTPUT_DIR)/args.gn ;\
fi
if [[ "$(CONFIG_USE_MINIMAL_MDNS)" = "n" ]]; then \
echo "chip_mdns = platform" >> $(OUTPUT_DIR)/args.gn ;\
fi
Expand Down
12 changes: 12 additions & 0 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <app/server/Mdns.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <lib/shell/Engine.h>
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/ManualSetupPayloadGenerator.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
Expand Down Expand Up @@ -545,6 +546,13 @@ class AppCallbacks : public AppDelegate
void OnPairingWindowClosed() override { pairingWindowLED.Set(false); }
};

#if CONFIG_ENABLE_CHIP_SHELL
void ChipShellTask(void * args)
{
chip::Shell::Engine::Root().RunMainLoop();
}
#endif // CONFIG_ENABLE_CHIP_SHELL

} // namespace

extern "C" void app_main()
Expand Down Expand Up @@ -599,6 +607,10 @@ extern "C" void app_main()
AppCallbacks callbacks;
InitServer(&callbacks);

#if CONFIG_ENABLE_CHIP_SHELL
xTaskCreate(&ChipShellTask, "chip_shell", 2048, NULL, 5, NULL);
#endif

SetupPretendDevices();

std::string qrCodeText = createSetupPayload();
Expand Down
3 changes: 3 additions & 0 deletions examples/all-clusters-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ CONFIG_DEVICE_PRODUCT_ID=0x4541
# Main task needs a bit more stack than the default
# default is 3584, bump this up to 4k.
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096

#enable debug shell
CONFIG_ENABLE_CHIP_SHELL=y
9 changes: 9 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <core/CHIPError.h>
#include <lib/shell/Engine.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <support/CHIPMem.h>
Expand All @@ -40,6 +41,7 @@ using namespace chip;
using namespace chip::Inet;
using namespace chip::Transport;
using namespace chip::DeviceLayer;
using chip::Shell::Engine;

namespace {
void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
Expand Down Expand Up @@ -110,8 +112,15 @@ int ChipLinuxAppInit(int argc, char ** argv)

void ChipLinuxAppMainLoop()
{
#if CHIP_ENABLE_SHELL
std::thread shellThread([]() { Engine::Root().RunMainLoop(); });
#endif

// Init ZCL Data Model and CHIP App Server
InitServer();

chip::DeviceLayer::PlatformMgr().RunEventLoop();
#if CHIP_ENABLE_SHELL
shellThread.join();
#endif
}
13 changes: 12 additions & 1 deletion examples/platform/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ config("app-main-config") {
include_dirs = [ "." ]
}

declare_args() {
chip_enable_shell = false
}

source_set("app-main") {
sources = [
"AppMain.cpp",
Expand All @@ -27,14 +31,21 @@ source_set("app-main") {
"Options.h",
]

defines = []

if (chip_enable_pw_rpc) {
defines = [ "PW_RPC_ENABLED" ]
defines += [ "PW_RPC_ENABLED" ]
}

public_deps = [
"${chip_root}/src/app/server",
"${chip_root}/src/lib",
]

if (chip_enable_shell) {
defines += [ "CHIP_ENABLE_SHELL" ]
public_deps += [ "${chip_root}/src/lib/shell" ]
}

public_configs = [ ":app-main-config" ]
}
67 changes: 7 additions & 60 deletions examples/shell/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,27 @@
* limitations under the License.
*/

#include "esp_console.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "linenoise/linenoise.h"
#include "nvs_flash.h"
#include "support/CHIPMem.h"
#include "support/ErrorStr.h"

#include <ChipShellCollection.h>
#include <lib/shell/Engine.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPPlatformMemory.h>
#include <platform/CHIPDeviceLayer.h>

class ShellLineArgs
{
public:
ShellLineArgs(char * line, TaskHandle_t source_task) : m_line(line), m_source_task(source_task) {}
char * GetLine() { return m_line; }
void WaitShellProcessDone() { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); }
void NotifyShellProcessDone() { xTaskNotifyGive(m_source_task); }

private:
char * m_line;
TaskHandle_t m_source_task;
};
using chip::Shell::Engine;

static void process_shell_line(intptr_t context)
static void chip_shell_task(void * args)
{
ShellLineArgs * shellArgs = reinterpret_cast<ShellLineArgs *>(context);
int ret;
esp_console_run(shellArgs->GetLine(), &ret);
if (ret)
{
printf("Error: %s\r\n", chip::ErrorStr(ret));
}
else
{
printf("Done\r\n");
}

linenoiseFree(shellArgs->GetLine());
shellArgs->NotifyShellProcessDone();
Engine::Root().RunMainLoop();
}

static void chip_shell_task(void * args)
extern "C" void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
chip::Platform::MemoryInit();
chip::DeviceLayer::PlatformMgr().InitChipStack();
chip::DeviceLayer::PlatformMgr().StartEventLoopTask();
int ret = chip::Shell::streamer_init(chip::Shell::streamer_get());
assert(ret == 0);
cmd_ping_init();
while (true)
{
const char * prompt = LOG_COLOR_I "> " LOG_RESET_COLOR;
char * line = linenoise(prompt);
printf("\r\n");
if (line == NULL || strlen(line) == 0)
{
continue;
}
ShellLineArgs shellArgs(line, xTaskGetCurrentTaskHandle());
linenoiseHistoryAdd(line);
chip::DeviceLayer::PlatformMgr().ScheduleWork(process_shell_line, reinterpret_cast<intptr_t>(&shellArgs));
shellArgs.WaitShellProcessDone();
}
}

extern "C" void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
xTaskCreate(&chip_shell_task, "chip_shell", 4096, NULL, 5, NULL);
xTaskCreate(&chip_shell_task, "chip_shell", 2048, NULL, 5, NULL);
}
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ static shell_command_t cmds_misc[] = {

void cmd_misc_init()
{
shell_register(cmds_misc, ArraySize(cmds_misc));
Engine::Root().RegisterCommands(cmds_misc, ArraySize(cmds_misc));
}
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_otcli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using namespace chip::DeviceLayer;
using namespace chip::Logging;
using namespace chip::ArgParser;

static chip::Shell::Shell sShellOtcliSubcommands;
static chip::Shell::Engine sShellOtcliSubcommands;

int cmd_otcli_help_iterator(shell_command_t * command, void * arg)
{
Expand Down Expand Up @@ -172,6 +172,6 @@ void cmd_otcli_init()
#endif

// Register the root otcli command with the top-level shell.
shell_register(&cmds_otcli_root, 1);
Engine::Root().RegisterCommands(&cmds_otcli_root, 1);
#endif // CHIP_ENABLE_OPENTHREAD
}
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,5 @@ static shell_command_t cmds_ping[] = {

void cmd_ping_init()
{
shell_register(cmds_ping, ArraySize(cmds_ping));
Engine::Root().RegisterCommands(cmds_ping, ArraySize(cmds_ping));
}
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,5 @@ static shell_command_t cmds_send[] = {

void cmd_send_init()
{
shell_register(cmds_send, ArraySize(cmds_send));
Engine::Root().RegisterCommands(cmds_send, ArraySize(cmds_send));
}
2 changes: 1 addition & 1 deletion examples/shell/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ int main()
cmd_ping_init();
cmd_send_init();

shell_task(nullptr);
Engine::Root().RunMainLoop();
return 0;
}
9 changes: 7 additions & 2 deletions src/lib/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ static_library("shell") {
output_name = "libCHIPShell"
output_dir = "${root_out_dir}/lib"

sources = [ "Commands.cpp" ]
sources = []

if (chip_target_style == "unix") {
sources += [ "streamer_stdio.cpp" ]
}

if (chip_device_platform == "esp32") {
sources += [ "streamer_esp32.cpp" ]
sources += [
"MainLoopESP32.cpp",
"streamer_esp32.cpp",
]
} else {
sources += [ "MainLoopDefault.cpp" ]
}

if (current_os == "zephyr") {
Expand Down
41 changes: 0 additions & 41 deletions src/lib/shell/Commands.cpp

This file was deleted.

Loading

0 comments on commit c8f286f

Please sign in to comment.