Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[esp32] Added CHIP esp32 shell for testing purposes #6253

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ if(NOT CHIP_ROOT)
get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH)
endif()

set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs)
if (CONFIG_ENABLE_CHIP_SHELL)
list(APPEND CHIP_REQURIE_COMPONENTS console)
endif()

idf_component_register(SRCS chip.c chip.cpp
PRIV_REQUIRES freertos lwip bt mdns mbedtls fatfs)
PRIV_REQUIRES ${CHIP_REQURIE_COMPONENTS})

# Prepare initial args file (lacking compile flags)
# This will be saved as args.gn.in
Expand All @@ -57,6 +62,10 @@ if (NOT CONFIG_USE_MINIMAL_MDNS)
chip_gn_arg_append("chip_mdns" "\"platform\"")
endif()

if (CONFIG_ENABLE_CHIP_SHELL)
chip_gn_arg_append("chip_build_shell_lib" "true")
endif()


set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")
Expand Down Expand Up @@ -151,6 +160,11 @@ if(CONFIG_BT_ENABLED)
list(APPEND chip_libraries $<TARGET_FILE:${bt_lib}> -lbtdm_app)
endif()

if (CONFIG_ENABLE_CHIP_SHELL)
idf_component_get_property(console_lib console COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${console_lib}>)
endif()

if(NOT CONFIG_USE_MINIMAL_MDNS)
idf_component_get_property(mdns_lib mdns COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
Expand Down
6 changes: 6 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ menu "CHIP Core"
The CHIP library is shipped with a minimal mDNS implementation,
enable this config to use it rather than the mDNS library in IDF.

config ENABLE_CHIP_SHELL
bool "Use the CHIP shell library"
default n
help
Link the application against CHIP interactive shell.

# TODO: add log level selection

endmenu # "General Options"
Expand Down
5 changes: 5 additions & 0 deletions examples/shell/esp32/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.vscode

/build/
/sdkconfig
/sdkconfig.old
28 changes: 28 additions & 0 deletions examples/shell/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright (c) 2021 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set(EXTRA_COMPONENT_DIRS
${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components
)

project(chip-shell)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)
27 changes: 27 additions & 0 deletions examples/shell/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2021 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

set(CHIP_SHELL_DIR "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/shell")


idf_component_register(SRCS main.cpp
"${CHIP_SHELL_DIR}/shell_common/cmd_ping.cpp"
PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src"
"${CHIP_SHELL_DIR}/shell_common/include"
PRIV_REQUIRES chip nvs_flash bt console)
76 changes: 76 additions & 0 deletions examples/shell/esp32/main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* 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 <ChipShellCollection.h>
#include <lib/shell/shell_core.h>
#include <lib/support/CHIPPlatformMemory.h>
#include <platform/CHIPDeviceLayer.h>

static void chip_shell_task(void * args)
{
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);
if (line == NULL || strlen(line) == 0)
{
continue;
}
linenoiseHistoryAdd(line);
int ret;
esp_console_run(line, &ret);
if (ret)
{
char errorStr[160];
bool errorStrFound = chip::FormatCHIPError(errorStr, sizeof(errorStr), ret);
if (!errorStrFound)
{
errorStr[0] = 0;
}
printf("Error: %s\n", errorStr);
}
else
{
printf("Done\n");
}

linenoiseFree(line);
}
}

extern "C" void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
xTaskCreate(&chip_shell_task, "chip_shell", 8192, NULL, 5, NULL);
}
6 changes: 6 additions & 0 deletions examples/shell/esp32/partitions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
# Factory partition size about 1.9MB
factory, app, factory, , 1945K,
40 changes: 40 additions & 0 deletions examples/shell/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2021 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Default to 921600 baud when flashing and monitoring device
CONFIG_ESPTOOLPY_BAUD_921600B=y
CONFIG_ESPTOOLPY_BAUD=921600
CONFIG_ESPTOOLPY_COMPRESSED=y
CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y
CONFIG_ESPTOOLPY_MONITOR_BAUD=115200

#enable BT
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y

#enable lwip ipv6 autoconfig
CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"

# Vendor and product id
CONFIG_DEVICE_VENDOR_ID=0x235A
CONFIG_DEVICE_PRODUCT_ID=0x4541
CONFIG_ENABLE_CHIP_SHELL=y
1 change: 1 addition & 0 deletions examples/shell/esp32/third_party/connectedhomeip
5 changes: 3 additions & 2 deletions examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using namespace chip;
using namespace Shell;
using namespace Logging;
using chip::Inet::IPAddress;

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
constexpr size_t kMaxTcpActiveConnectionCount = 4;
Expand Down Expand Up @@ -143,7 +144,7 @@ TransportMgr<Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>

Messaging::ExchangeManager gExchangeManager;
SecureSessionMgr gSessionManager;
Inet::IPAddress gDestAddr;
IPAddress gDestAddr;

bool EchoIntervalExpired(void)
{
Expand Down Expand Up @@ -259,7 +260,7 @@ void StartPinging(streamer_t * stream, char * destination)
Transport::AdminPairingInfo * adminInfo = nullptr;
uint32_t maxEchoCount = 0;

if (!Inet::IPAddress::FromString(destination, gDestAddr))
if (!IPAddress::FromString(destination, gDestAddr))
{
streamer_printf(stream, "Invalid Echo Server IP address: %s\n", destination);
ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import("//build_overrides/chip.gni")
import("${chip_root}/src/platform/device.gni")

declare_args() {
chip_build_shell_lib = false
}

config("includes") {
include_dirs = [ "." ]
}
Expand All @@ -37,6 +41,10 @@ static_library("lib") {
"${chip_root}/src/transport",
]

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

if (chip_device_platform != "none") {
public_deps += [ "${chip_root}/src/app/server" ]
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import("//build_overrides/chip.gni")

import("${chip_root}/src/lib/core/core.gni")
import("${chip_root}/src/platform/device.gni")

static_library("shell") {
output_name = "libCHIPShell"
Expand All @@ -33,6 +34,10 @@ static_library("shell") {
sources += [ "streamer_stdio.cpp" ]
}

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

if (current_os == "zephyr") {
sources += [ "streamer_zephyr.cpp" ]
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/shell/shell_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class Shell
unsigned _commandSetSize[CHIP_SHELL_MAX_MODULES];
unsigned _commandSetCount;

public:
Shell() {}

/** Return the root singleton for the Shell command hierarchy. */
static Shell & Root() { return theShellRoot; }

/**
* Registers a set of defaults commands (help) for all Shell and sub-Shell instances.
*
Expand All @@ -110,12 +116,6 @@ class Shell
*/
void RegisterDefaultCommands();

public:
Shell() {}

/** Return the root singleton for the Shell command hierarchy. */
static Shell & Root() { return theShellRoot; }

/**
* Execution callback for a shell command.
*
Expand Down
Loading