Skip to content

Commit

Permalink
[Mbed] Add shell example (#10452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ATmobica authored and pull[bot] committed Aug 24, 2023
1 parent 8f3ad60 commit 1665548
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 12 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/examples-mbed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ concurrency:
jobs:
mbedos:
name: Mbed OS examples building
timeout-minutes: 60
timeout-minutes: 70

env:
BUILD_TYPE: mbedos
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@
"lock-app",
"lighting-app",
"pigweed-app",
"all-clusters-app"
"all-clusters-app",
"shell"
],
"default": "lock-app"
},
Expand Down
3 changes: 2 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@
"lock-app",
"lighting-app",
"pigweed-app",
"all-clusters-app"
"all-clusters-app",
"shell"
],
"default": "lock-app"
},
Expand Down
2 changes: 2 additions & 0 deletions examples/shell/mbed/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config/
build-*/
58 changes: 58 additions & 0 deletions examples/shell/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions examples/shell/mbed/config.in
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions examples/shell/mbed/main/include/CHIPProjectConfig.h
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions examples/shell/mbed/main/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <ChipShellCollection.h>
#include <lib/core/CHIPCore.h>
#include <lib/shell/Engine.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/mbed/Logging.h>

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;
}
31 changes: 31 additions & 0 deletions examples/shell/mbed/mbed_app.json
Original file line number Diff line number Diff line change
@@ -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\""
}
}
}
2 changes: 1 addition & 1 deletion scripts/examples/mbed_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mbed/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class GapEventHandler : private mbed::NonCopyable<GapEventHandler>, 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;
Expand Down
7 changes: 0 additions & 7 deletions src/platform/mbed/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
#include <platform/internal/GenericConfigurationManagerImpl.h>
#include <platform/mbed/MbedConfig.h>

// Forward declare test method that run the tests.
int cmd_device_test_config(int argc, char ** argv);

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -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;
};

/**
Expand Down
10 changes: 10 additions & 0 deletions src/platform/mbed/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 1665548

Please sign in to comment.