From c3310ca870363f12bed35ee45549c35716f94887 Mon Sep 17 00:00:00 2001 From: Artur Tynecki <77382963+ATmobica@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:40:18 +0200 Subject: [PATCH] [Mbed] Add shell example (#10452) * Add Mbed OS shell example implementation Add shell to mbed_example script, launch.json, tasks.json and build workflow Add sleep function to arch.c ConfiguraitonManagerImple cleanup * Add shell example to Mbed build workflow Fix server command * Fix network socket client commands - partial * Fix close IOCTL socket for Mbe implementation * Improve Mbed network client and example commands * Improve message buffer service and error logs * Fix BLE advertising stop * Add CHIP_DEVICE_CONFIG_ENABLE_TEST_DEVICE_IDENTITY flag to shell config * Improve echo server logs Mbed commands cleanup * Restore log filter * Fix handle DNS resolve callback checking error * Update app logging * Changes for Mbed OS update * Improve examples startup logging * Fix after platform clock changes * Fix MBED_PATH value Fix mbed shell commands Changes restyle * Remove mbed-specific shell command from shell app * Fix Mbed shell example build --- .github/workflows/examples-mbed.yaml | 11 +- .vscode/launch.json | 3 +- .vscode/tasks.json | 3 +- examples/shell/mbed/.gitignore | 2 + examples/shell/mbed/CMakeLists.txt | 58 ++++++++++ examples/shell/mbed/config.in | 6 + .../mbed/main/include/CHIPProjectConfig.h | 45 ++++++++ examples/shell/mbed/main/main.cpp | 106 ++++++++++++++++++ examples/shell/mbed/mbed_app.json | 31 +++++ scripts/examples/mbed_example.sh | 2 +- src/inet/InetInterface.cpp | 3 + src/platform/mbed/BLEManagerImpl.cpp | 2 +- src/platform/mbed/ConfigurationManagerImpl.h | 7 -- src/platform/mbed/arch.c | 10 ++ 14 files changed, 277 insertions(+), 12 deletions(-) create mode 100644 examples/shell/mbed/.gitignore create mode 100644 examples/shell/mbed/CMakeLists.txt create mode 100644 examples/shell/mbed/config.in create mode 100644 examples/shell/mbed/main/include/CHIPProjectConfig.h create mode 100644 examples/shell/mbed/main/main.cpp create mode 100644 examples/shell/mbed/mbed_app.json diff --git a/.github/workflows/examples-mbed.yaml b/.github/workflows/examples-mbed.yaml index 6719d9797f0b9f..f97feefde8e01e 100644 --- a/.github/workflows/examples-mbed.yaml +++ b/.github/workflows/examples-mbed.yaml @@ -26,7 +26,7 @@ concurrency: jobs: mbedos: name: Mbed OS examples building - timeout-minutes: 60 + timeout-minutes: 70 env: BUILD_TYPE: mbedos @@ -98,6 +98,15 @@ jobs: mbed $APP_TARGET+$APP_PROFILE all-clusters-app \ examples/all-clusters-app/mbed/build-CY8CPROTO_062_4343W/release/chip-mbed-all-clusters-app-example.elf \ /tmp/bloat_reports/ + + - name: Build shell example + timeout-minutes: 10 + run: | + scripts/examples/mbed_example.sh -a=shell -b=$APP_TARGET -p=$APP_PROFILE + .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ + mbed $APP_TARGET+$APP_PROFILE shell \ + examples/shell/mbed/build-CY8CPROTO_062_4343W/release/chip-mbed-shell-example.elf \ + /tmp/bloat_reports/ - name: Build unit tests timeout-minutes: 10 diff --git a/.vscode/launch.json b/.vscode/launch.json index ac398fc811a801..147053871024d8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -314,7 +314,8 @@ "lock-app", "lighting-app", "pigweed-app", - "all-clusters-app" + "all-clusters-app", + "shell" ], "default": "lock-app" }, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f8bdee88c8a4cc..8baf5533a94d4c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -188,7 +188,8 @@ "lock-app", "lighting-app", "pigweed-app", - "all-clusters-app" + "all-clusters-app", + "shell" ], "default": "lock-app" }, diff --git a/examples/shell/mbed/.gitignore b/examples/shell/mbed/.gitignore new file mode 100644 index 00000000000000..58f6e14fd6f5b2 --- /dev/null +++ b/examples/shell/mbed/.gitignore @@ -0,0 +1,2 @@ +config/ +build-*/ diff --git a/examples/shell/mbed/CMakeLists.txt b/examples/shell/mbed/CMakeLists.txt new file mode 100644 index 00000000000000..17ec6390124cd6 --- /dev/null +++ b/examples/shell/mbed/CMakeLists.txt @@ -0,0 +1,58 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.19.0) + +get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH) +get_filename_component(APP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/.. REALPATH) +get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/config.in + ${CMAKE_CURRENT_BINARY_DIR}/chip_build/config + @ONLY +) + +set(MBED_PATH $ENV{MBED_OS_PATH} CACHE INTERNAL "") +set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") +set(APP_TARGET chip-mbed-shell-example) + +include(${MBED_PATH}/tools/cmake/app.cmake) + +project(${APP_TARGET}) + +add_subdirectory(${MBED_PATH} ./mbed_build) +add_subdirectory($ENV{MBED_OS_POSIX_SOCKET_PATH} ./mbed_os_posix_socket_build) + +add_executable(${APP_TARGET}) + +add_subdirectory(${CHIP_ROOT}/config/mbed ./chip_build) + +mbed_configure_app_target(${APP_TARGET}) + +target_include_directories(${APP_TARGET} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/main/include + ${APP_ROOT}/shell_common/include + ${GEN_DIR}/app-common) + +target_sources(${APP_TARGET} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/main/main.cpp + ${APP_ROOT}/shell_common/cmd_misc.cpp + ${APP_ROOT}/shell_common/cmd_otcli.cpp + ${APP_ROOT}/shell_common/cmd_ping.cpp + ${APP_ROOT}/shell_common/cmd_send.cpp + ${APP_ROOT}/shell_common/globals.cpp +) + +target_link_libraries(${APP_TARGET} mbed-os-posix-socket mbed-os mbed-ble mbed-events mbed-netsocket mbed-storage mbed-storage-kv-global-api mbed-mbedtls mbed-emac chip) + +if(MBED_TARGET STREQUAL "CY8CPROTO_062_4343W") + target_link_libraries(${APP_TARGET} mbed-cy-psoc6-common-network) +endif() + +mbed_set_post_build(${APP_TARGET}) + +option(VERBOSE_BUILD "Have a verbose build process") +if(VERBOSE_BUILD) + set(CMAKE_VERBOSE_MAKEFILE ON) +endif() diff --git a/examples/shell/mbed/config.in b/examples/shell/mbed/config.in new file mode 100644 index 00000000000000..33fc7acf3c4a8f --- /dev/null +++ b/examples/shell/mbed/config.in @@ -0,0 +1,6 @@ +CONFIG_CHIP_BUILD_TESTS=n +CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS=y +CONFIG_CHIP_PROJECT_CONFIG=main/include/CHIPProjectConfig.h +CONFIG_CHIP_BYPASS_RENDEZVOUS=n +CONFIG_CHIP_LIB_SHELL=y +CONFIG_MBED_BSD_SOCKET_TRACE=n diff --git a/examples/shell/mbed/main/include/CHIPProjectConfig.h b/examples/shell/mbed/main/include/CHIPProjectConfig.h new file mode 100644 index 00000000000000..e46d15d1042277 --- /dev/null +++ b/examples/shell/mbed/main/include/CHIPProjectConfig.h @@ -0,0 +1,45 @@ +/* + * + * 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. + */ + +/** + * @file + * This is a place to put application or project-specific overrides + * to the default configuration values for general CHIP features. + * + */ + +#pragma once + +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 1 +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 + +#define CHIP_SHELL_MAX_MODULES 30 + +// Use a default pairing code if one hasn't been provisioned in flash. +#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE 20202021 +#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR 0xF00 + +/** + * CHIP_DEVICE_CONFIG_ENABLE_TEST_DEVICE_IDENTITY + * + * Enables the use of a hard-coded default Chip device id and credentials if no device id + * is found in Chip NV storage. + * + * This option is for testing only and should be disabled in production releases. + */ +#define CHIP_DEVICE_CONFIG_ENABLE_TEST_DEVICE_IDENTITY 34 diff --git a/examples/shell/mbed/main/main.cpp b/examples/shell/mbed/main/main.cpp new file mode 100644 index 00000000000000..7865283deda515 --- /dev/null +++ b/examples/shell/mbed/main/main.cpp @@ -0,0 +1,106 @@ +/* + * + * Copyright (c) 2020 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 "mbedtls/platform.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace ::chip; +using namespace ::chip::Shell; +using namespace ::chip::DeviceLayer; +using namespace ::chip::Logging::Platform; + +int main() +{ + int ret = 0; + CHIP_ERROR err = CHIP_NO_ERROR; + + mbed_logging_init(); + + ChipLogProgress(NotSpecified, "Mbed shell example application start"); + + ret = mbedtls_platform_setup(NULL); + if (ret) + { + ChipLogError(Shell, "Mbed TLS platform initialization failed [%d]", ret); + goto exit; + } + + err = chip::Platform::MemoryInit(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Shell, "Memory initalization failed: %s", err.AsString()); + ret = EXIT_FAILURE; + goto exit; + } + + err = PlatformMgr().InitChipStack(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Shell, "Chip stack initalization failed: %s", err.AsString()); + ret = EXIT_FAILURE; + goto exit; + } + +#ifdef MBED_CONF_APP_BLE_DEVICE_NAME + err = ConnectivityMgr().SetBLEDeviceName(MBED_CONF_APP_BLE_DEVICE_NAME); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Shell, "Set BLE device name failed: %s", err.AsString()); + ret = EXIT_FAILURE; + goto exit; + } +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_WPA + ConnectivityManagerImpl().StartWiFiManagement(); +#endif + + err = PlatformMgr().StartEventLoopTask(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Shell, "Chip stack start failed: %s", err.AsString()); + ret = EXIT_FAILURE; + goto exit; + } + + // Initialize the default streamer that was linked. + ret = streamer_init(streamer_get()); + if (ret) + { + ChipLogError(Shell, "Streamer initialization failed [%d]", ret); + goto exit; + } + + cmd_misc_init(); + cmd_otcli_init(); + cmd_ping_init(); + cmd_send_init(); + + ChipLogProgress(NotSpecified, "Mbed shell example application run"); + + Engine::Root().RunMainLoop(); + +exit: + ChipLogProgress(NotSpecified, "Exited with code %d", ret); + return ret; +} diff --git a/examples/shell/mbed/mbed_app.json b/examples/shell/mbed/mbed_app.json new file mode 100644 index 00000000000000..1a6b662db9abcf --- /dev/null +++ b/examples/shell/mbed/mbed_app.json @@ -0,0 +1,31 @@ +{ + "macros": ["MBEDTLS_USER_CONFIG_FILE=\"chip_mbedtls_config.h\""], + "target_overrides": { + "*": { + "platform.stdio-baud-rate": 115200, + "lwip.ipv6-enabled": true, + "lwip.raw-socket-enabled": true, + "nsapi.default-wifi-security": "WPA_WPA2", + "nsapi.default-wifi-ssid": "\"YOUR_SSID\"", + "nsapi.default-wifi-password": "\"YOUR_PASSWORD\"", + "mbed-trace.max-level": "TRACE_LEVEL_DEBUG", + "mbed-trace.enable": true, + "target.printf_lib": "std" + }, + "CY8CPROTO_062_4343W": { + "target.network-default-interface-type": "WIFI", + "target.macros_add": [ + "MXCRYPTO_DISABLED", + "NL_ASSERT_LOG=NL_ASSERT_LOG_DEFAULT", + "NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG", + "WHD_PRINT_DISABLE" + ] + } + }, + "config": { + "ble-device-name": { + "help": "Name used for BLE advertising.", + "value": "\"MBED-shell\"" + } + } +} diff --git a/scripts/examples/mbed_example.sh b/scripts/examples/mbed_example.sh index caf93e2ce2b636..ba66762b6b8f5c 100755 --- a/scripts/examples/mbed_example.sh +++ b/scripts/examples/mbed_example.sh @@ -22,7 +22,7 @@ cd "$CHIP_ROOT"/examples SUPPORTED_TOOLCHAIN=(GCC_ARM ARM) SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W) -SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app) +SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app shell) SUPPORTED_PROFILES=(release develop debug) SUPPORTED_COMMAND=(build flash build-flash) diff --git a/src/inet/InetInterface.cpp b/src/inet/InetInterface.cpp index 0cff70e50b425d..f2bc434be8a524 100644 --- a/src/inet/InetInterface.cpp +++ b/src/inet/InetInterface.cpp @@ -747,6 +747,9 @@ CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & i interface = InterfaceId(currentId); return CHIP_NO_ERROR; } +#if __MBED__ + CloseIOCTLSocket(); +#endif } interface = InterfaceId::Null(); return INET_ERROR_UNKNOWN_INTERFACE; diff --git a/src/platform/mbed/BLEManagerImpl.cpp b/src/platform/mbed/BLEManagerImpl.cpp index 94cd31f5d499e4..ab8a34477746ce 100644 --- a/src/platform/mbed/BLEManagerImpl.cpp +++ b/src/platform/mbed/BLEManagerImpl.cpp @@ -202,7 +202,7 @@ class GapEventHandler : private mbed::NonCopyable, public ble:: ChipLogDetail(DeviceLayer, "GAP %s", __FUNCTION__); BLEManagerImpl & ble_manager = BLEMgrImpl(); - ble_manager.mFlags.Set(ble_manager.kFlag_Advertising); + ble_manager.mFlags.Clear(ble_manager.kFlag_Advertising); // Post a CHIPoBLEAdvertisingChange(Stopped) event. ChipDeviceEvent chip_event; diff --git a/src/platform/mbed/ConfigurationManagerImpl.h b/src/platform/mbed/ConfigurationManagerImpl.h index 13d8239b73f8ea..ca60444f3c0f5f 100644 --- a/src/platform/mbed/ConfigurationManagerImpl.h +++ b/src/platform/mbed/ConfigurationManagerImpl.h @@ -26,9 +26,6 @@ #include #include -// Forward declare test method that run the tests. -int cmd_device_test_config(int argc, char ** argv); - namespace chip { namespace DeviceLayer { @@ -66,10 +63,6 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); - - // ===== Members for internal use by the following friends (testing in shell application) - friend int ::cmd_device_test_config(int argc, char ** argv); - using MbedConfig::RunConfigUnitTest; }; /** diff --git a/src/platform/mbed/arch.c b/src/platform/mbed/arch.c index 51e1e4b17b5b4a..844d22476034ae 100644 --- a/src/platform/mbed/arch.c +++ b/src/platform/mbed/arch.c @@ -89,3 +89,13 @@ void usleep(unsigned int usec) wait_us((int) us); } } + +void sleep(unsigned int sec) +{ + unsigned int ms = (sec * 1000); + + if (ms) + { + thread_sleep_for(ms); + } +}