diff --git a/examples/lighting-app/nrf5/BUILD.gn b/examples/lighting-app/nrf5/BUILD.gn index 026c4a3b4e406f..ebb3a78ffc24c7 100644 --- a/examples/lighting-app/nrf5/BUILD.gn +++ b/examples/lighting-app/nrf5/BUILD.gn @@ -57,6 +57,7 @@ nrf5_sdk("sdk") { "MBEDTLS_X509_CREATE_C", "MBEDTLS_X509_CSR_WRITE_C", "OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE", + "OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT=1", ] } @@ -88,7 +89,7 @@ nrf5_executable("lighting_app") { ] public_deps = [ - "${chip_root}/examples/shell:chip_shell_collection", + "${chip_root}/examples/shell/shell_common:chip_shell_collection", ] deps = [ diff --git a/examples/lighting-app/nrf5/main/AppTask.cpp b/examples/lighting-app/nrf5/main/AppTask.cpp index 22f2c6d9a71737..5f35cfb741edbe 100644 --- a/examples/lighting-app/nrf5/main/AppTask.cpp +++ b/examples/lighting-app/nrf5/main/AppTask.cpp @@ -45,13 +45,16 @@ #include #include +#include "attribute-storage.h" +#include "gen/cluster-id.h" + APP_TIMER_DEF(sFunctionTimer); namespace { constexpr int kFactoryResetTriggerTimeout = 3000; constexpr int kFactoryResetCancelWindowTimeout = 3000; -constexpr size_t kAppTaskStackSize = 1536; +constexpr size_t kAppTaskStackSize = 4096; constexpr int kAppTaskPriority = 2; constexpr int kAppEventQueueSize = 10; constexpr int kExampleVenderID = 0xabcd; @@ -224,6 +227,8 @@ void AppTask::AppTaskMain(void * pvParameter) SetDeviceName("LightingDemo._chip._udp.local."); + GetAppTask().UpdateClusterState(); + while (true) { BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10)); @@ -287,6 +292,7 @@ void AppTask::AppTaskMain(void * pvParameter) sStatusLED.Animate(); sUnusedLED.Animate(); sUnusedLED_1.Animate(); + GetAppTask().UpdateClusterState(); uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic(); uint64_t nextChangeTimeUS = mLastChangeTimeUS + 5 * 1000 * 1000UL; @@ -527,6 +533,7 @@ void AppTask::PostEvent(const AppEvent * aEvent) void AppTask::DispatchEvent(AppEvent * aEvent) { + if (aEvent->Handler) { aEvent->Handler(aEvent); @@ -536,3 +543,16 @@ void AppTask::DispatchEvent(AppEvent * aEvent) NRF_LOG_INFO("Event received with no handler. Dropping event."); } } + +void AppTask::UpdateClusterState(void) +{ + uint8_t newValue = LightingMgr().IsTurnedOn(); + + // write the new on/off value + EmberAfStatus status = emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, + (uint8_t *) &newValue, ZCL_BOOLEAN_ATTRIBUTE_TYPE); + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + NRF_LOG_INFO("ERR: updating on/off %x", status); + } +} diff --git a/examples/lighting-app/nrf5/main/LightingCLI.cpp b/examples/lighting-app/nrf5/main/LightingCLI.cpp index 88c626f260069f..2db396eeb58b9a 100644 --- a/examples/lighting-app/nrf5/main/LightingCLI.cpp +++ b/examples/lighting-app/nrf5/main/LightingCLI.cpp @@ -30,17 +30,135 @@ #include #include #include +#include #include #include +#include +#include +#include + using namespace ::chip; +using namespace ::chip::DeviceLayer; using namespace ::chip::Shell; namespace { -const size_t kShellTaskStackSize = 1536; -const int kShellTaskPriority = 2; +enum CLIEvents +{ + kCLIEvent_LightOn = 0, + kCLIEvent_LightOff, + kCLIEvent_LightToggle, + + kCLIEvent_FactoryReset, +}; + +void CLIAppEventHandler(AppEvent * event) +{ + switch (event->CLIEvent.Event) + { + case kCLIEvent_LightOn: + LightingMgr().InitiateAction(LightingManager::ON_ACTION); + break; + case kCLIEvent_LightOff: + LightingMgr().InitiateAction(LightingManager::OFF_ACTION); + break; + case kCLIEvent_LightToggle: { + LightingManager::Action_t action; + if (LightingMgr().IsTurnedOn()) + { + action = LightingManager::OFF_ACTION; + } + else + { + action = LightingManager::ON_ACTION; + } + LightingMgr().InitiateAction(action); + break; + } + case kCLIEvent_FactoryReset: + ConfigurationMgr().InitiateFactoryReset(); + return; + default: + ChipLogError(Shell, "Unknown event received: %d", event->CLIEvent.Event); + return; + } +} + +int cmd_light(int argc, char ** argv) +{ + int end_point = 1; + AppEvent event; + event.Type = AppEvent::kEventType_CLI; + event.Handler = CLIAppEventHandler; + + VerifyOrExit(argc > 0 && argc <= 2, streamer_printf(streamer_get(), "light on/off/toggle [endpoint=1]\n\r")); + + if (strcmp(argv[0], "on") == 0) + { + event.CLIEvent.Event = kCLIEvent_LightOn; + } + else if (strcmp(argv[0], "off") == 0) + { + event.CLIEvent.Event = kCLIEvent_LightOff; + } + else if (strcmp(argv[0], "toggle") == 0) + { + event.CLIEvent.Event = kCLIEvent_LightToggle; + } + + if (argc == 2) + { + sscanf(argv[1], "%d", &end_point); + } + + GetAppTask().PostEvent(&event); +exit: + streamer_printf(streamer_get(), "\n\r"); + return 0; +} + +int cmd_app(int argc, char ** argv) +{ + int end_point = 1; + AppEvent event; + event.Type = AppEvent::kEventType_CLI; + event.Handler = CLIAppEventHandler; + + VerifyOrExit(argc > 0, streamer_printf(streamer_get(), "app subcommand|help\n\r")); + + if (strcmp(argv[0], "factoryrst") == 0) + { + event.CLIEvent.Event = kCLIEvent_FactoryReset; + } + else + { + streamer_printf(streamer_get(), "app factoryrst : Do factory reset\n\r"); + } + + GetAppTask().PostEvent(&event); +exit: + streamer_printf(streamer_get(), "\n\r"); + return 0; +} + +shell_command_t cmd_lightingcli[] = { + { &cmd_light, "light", "Lighting control" }, + { &cmd_app, "app", "App utilities" }, +}; + +void cmd_app_init(void) +{ + shell_register(cmd_lightingcli, ArraySize(cmd_lightingcli)); +} + +} // namespace + +namespace { + +const size_t kShellTaskStackSize = 2048; +const int kShellTaskPriority = 1; TaskHandle_t sShellTaskHandle; void LightingCLIMain(void * pvParameter) @@ -57,10 +175,12 @@ void LightingCLIMain(void * pvParameter) ChipLogDetail(Shell, "Initializing CHIP shell", rc); cmd_misc_init(); - cmd_base64_init(); + cmd_app_init(); cmd_btp_init(); cmd_otcli_init(); + ChipLogDetail(Shell, "Run CHIP shell Task", rc); + shell_task(NULL); } diff --git a/examples/lighting-app/nrf5/main/include/AppEvent.h b/examples/lighting-app/nrf5/main/include/AppEvent.h index 8a85b075641b64..5ebcbfae0f32e3 100644 --- a/examples/lighting-app/nrf5/main/include/AppEvent.h +++ b/examples/lighting-app/nrf5/main/include/AppEvent.h @@ -30,6 +30,7 @@ struct AppEvent kEventType_Timer, kEventType_Lighting, kEventType_Install, + kEventType_CLI, }; uint16_t Type; @@ -49,6 +50,10 @@ struct AppEvent { uint8_t Action; } LightingEvent; + struct + { + uint8_t Event; + } CLIEvent; }; EventHandler Handler; diff --git a/examples/lighting-app/nrf5/main/include/AppTask.h b/examples/lighting-app/nrf5/main/include/AppTask.h index ac49fbb42cd645..db088d74e292d6 100644 --- a/examples/lighting-app/nrf5/main/include/AppTask.h +++ b/examples/lighting-app/nrf5/main/include/AppTask.h @@ -49,6 +49,7 @@ class AppTask void CancelTimer(void); void DispatchEvent(AppEvent * event); + void UpdateClusterState(void); static void FunctionTimerEventHandler(AppEvent * aEvent); static void FunctionHandler(AppEvent * aEvent); diff --git a/examples/platform/nrf528xx/app/chipinit.cpp b/examples/platform/nrf528xx/app/chipinit.cpp index 5114837cb0f086..7eff16fe1e7ba2 100644 --- a/examples/platform/nrf528xx/app/chipinit.cpp +++ b/examples/platform/nrf528xx/app/chipinit.cpp @@ -87,13 +87,12 @@ ret_code_t ChipInit() #if CHIP_ENABLE_OPENTHREAD NRF_LOG_INFO("Initializing OpenThread stack"); - mbedtls_platform_set_calloc_free(ot_calloc, ot_free); - nrf_cc310_platform_abort_init(); - nrf_cc310_platform_mutex_init(); - mbedtls_platform_setup(NULL); - otHeapSetCAllocFree(ot_calloc, ot_free); + // mbedtls_platform_set_calloc_free(ot_calloc, ot_free); + + // otHeapSetCAllocFree(ot_calloc, ot_free); otSysInit(0, NULL); + NRF_LOG_INFO("Initializing OpenThread stack"); // Configure multiprotocol to work with BLE. { diff --git a/examples/platform/nrf528xx/app/project_include/OpenThreadConfig.h b/examples/platform/nrf528xx/app/project_include/OpenThreadConfig.h index fee4dac11c2b5a..a025a5f5fd44f8 100644 --- a/examples/platform/nrf528xx/app/project_include/OpenThreadConfig.h +++ b/examples/platform/nrf528xx/app/project_include/OpenThreadConfig.h @@ -45,7 +45,7 @@ // Use smaller maximum interval to speed up reattaching. #define OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MAXIMUM_INTERVAL (60 * 10 * 1000) // default 1200000 ms -#define OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE 1 +#define OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE 0 #define OPENTHREAD_CONFIG_JOINER_ENABLE 1 #define OPENTHREAD_CONFIG_NCP_UART_ENABLE 1 #define OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE 1 diff --git a/examples/shell/BUILD.gn b/examples/shell/BUILD.gn index db9783d546cf56..19c787776f939a 100644 --- a/examples/shell/BUILD.gn +++ b/examples/shell/BUILD.gn @@ -16,39 +16,6 @@ import("//build_overrides/chip.gni") import("${chip_root}/src/platform/device.gni") import("//build_overrides/openthread.gni") -config("chip_shell_collection_config") { - include_dirs = [ - ".", - "./include", - "${chip_root}/src/lib/shell", - ] - - defines = [ - "ENABLE_CHIP_SHELL", - "OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE", - ] -} - -static_library("chip_shell_collection") { - sources = [ - "cmd_base64.cpp", - "cmd_btp.cpp", - "cmd_device.cpp", - "cmd_misc.cpp", - "cmd_otcli.cpp", - ] - - public_deps = [ - "${chip_root}/src/lib", - "${chip_root}/src/lib/shell", - "${chip_root}/src/setup_payload", - "${openthread_root}:libopenthread-cli-ftd", - "${openthread_root}:libopenthread-ftd", - ] - - public_configs = [ ":chip_shell_collection_config" ] -} - if (chip_device_platform == "nrf5") { } else { @@ -62,7 +29,7 @@ if (chip_device_platform == "nrf5") { ] public_deps = [ - ":chip_shell_collection", + "shell_common:chip_shell_collection", "${chip_root}/src/lib/shell", "${chip_root}/src/platform", ] diff --git a/examples/shell/shell_common/BUILD.gn b/examples/shell/shell_common/BUILD.gn new file mode 100644 index 00000000000000..6ec0507b7d0df3 --- /dev/null +++ b/examples/shell/shell_common/BUILD.gn @@ -0,0 +1,50 @@ +# 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. + +import("//build_overrides/chip.gni") +import("${chip_root}/src/platform/device.gni") +import("//build_overrides/openthread.gni") + +config("chip_shell_collection_config") { + include_dirs = [ + ".", + "./include", + "${chip_root}/src/lib/shell", + ] + + defines = [ + "ENABLE_CHIP_SHELL", + "OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE", + ] +} + +static_library("chip_shell_collection") { + sources = [ + "cmd_base64.cpp", + "cmd_btp.cpp", + "cmd_device.cpp", + "cmd_misc.cpp", + "cmd_otcli.cpp", + ] + + public_deps = [ + "${chip_root}/src/lib", + "${chip_root}/src/lib/shell", + "${chip_root}/src/setup_payload", + "${openthread_root}:libopenthread-cli-ftd", + "${openthread_root}:libopenthread-ftd", + ] + + public_configs = [ ":chip_shell_collection_config" ] +} diff --git a/examples/shell/cmd_base64.cpp b/examples/shell/shell_common/cmd_base64.cpp similarity index 100% rename from examples/shell/cmd_base64.cpp rename to examples/shell/shell_common/cmd_base64.cpp diff --git a/examples/shell/cmd_btp.cpp b/examples/shell/shell_common/cmd_btp.cpp similarity index 100% rename from examples/shell/cmd_btp.cpp rename to examples/shell/shell_common/cmd_btp.cpp diff --git a/examples/shell/cmd_device.cpp b/examples/shell/shell_common/cmd_device.cpp similarity index 100% rename from examples/shell/cmd_device.cpp rename to examples/shell/shell_common/cmd_device.cpp diff --git a/examples/shell/cmd_misc.cpp b/examples/shell/shell_common/cmd_misc.cpp similarity index 100% rename from examples/shell/cmd_misc.cpp rename to examples/shell/shell_common/cmd_misc.cpp diff --git a/examples/shell/cmd_otcli.cpp b/examples/shell/shell_common/cmd_otcli.cpp similarity index 100% rename from examples/shell/cmd_otcli.cpp rename to examples/shell/shell_common/cmd_otcli.cpp diff --git a/examples/shell/include/ChipShellCollection.h b/examples/shell/shell_common/include/ChipShellCollection.h similarity index 100% rename from examples/shell/include/ChipShellCollection.h rename to examples/shell/shell_common/include/ChipShellCollection.h diff --git a/src/include/platform/internal/GenericPlatformManagerImpl.ipp b/src/include/platform/internal/GenericPlatformManagerImpl.ipp index 78137b7e0e36e7..539226a4045cb2 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl.ipp +++ b/src/include/platform/internal/GenericPlatformManagerImpl.ipp @@ -164,6 +164,7 @@ template void GenericPlatformManagerImpl::_ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg) { ChipDeviceEvent event; + event.Type = DeviceEventType::kCallWorkFunct; event.CallWorkFunct.WorkFunct = workFunct; event.CallWorkFunct.Arg = arg; @@ -191,6 +192,7 @@ void GenericPlatformManagerImpl::_DispatchEvent(const ChipDeviceEvent case DeviceEventType::kCallWorkFunct: // If the event is a "call work function" event, call the specified function. + event->CallWorkFunct.WorkFunct(event->CallWorkFunct.Arg); break; @@ -238,6 +240,7 @@ void GenericPlatformManagerImpl::DispatchEventToSystemLayer(const Chi template void GenericPlatformManagerImpl::DispatchEventToDeviceLayer(const ChipDeviceEvent * event) { + // Dispatch the event to all the components in the Device Layer. #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE BLEMgr().OnPlatformEvent(event); diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.ipp b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.ipp index 8534b981ada2d7..64be6b104e8fbe 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.ipp +++ b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.ipp @@ -93,8 +93,10 @@ void GenericPlatformManagerImpl_FreeRTOS::_UnlockChipStack(void) template void GenericPlatformManagerImpl_FreeRTOS::_PostEvent(const ChipDeviceEvent * event) { + if (mChipEventQueue != NULL) { + if (!xQueueSend(mChipEventQueue, event, 1)) { ChipLogError(DeviceLayer, "Failed to post event to CHIP Platform event queue"); @@ -172,6 +174,7 @@ void GenericPlatformManagerImpl_FreeRTOS::_RunEventLoop(void) // dispatching them until the queue is empty. while (eventReceived == pdTRUE) { + Impl()->DispatchEvent(&event); eventReceived = xQueueReceive(mChipEventQueue, &event, 0); diff --git a/src/lib/shell/shell.cpp b/src/lib/shell/shell.cpp index f49b257473d555..8d2192917c6e77 100644 --- a/src/lib/shell/shell.cpp +++ b/src/lib/shell/shell.cpp @@ -50,6 +50,7 @@ int shell_line_read(char * buffer, size_t max) int read = 0; bool done = false; char * inptr = buffer; + ChipLogError(Shell, "%s %d\n", __FUNCTION__, __LINE__); // Read in characters until we get a new line or we hit our max size. while (((inptr - buffer) < (int) max) && !done) diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.ipp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.ipp index e0f09ec4c00f60..d2322beed46da6 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.ipp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.ipp @@ -785,7 +785,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::DoInit(otInstanc VerifyOrExit(otInst != NULL, err = MapOpenThreadError(OT_ERROR_FAILED)); } -#if !defined(__ZEPHYR__) && OPENTHREAD_CONFIG_CLI_TRANSPORT == OT_CLI_TRANSPORT_UART +#if !defined(__ZEPHYR__) && !defined(ENABLE_CHIP_SHELL) otCliUartInit(otInst); #endif diff --git a/src/platform/nRF5/ConfigurationManagerImpl.cpp b/src/platform/nRF5/ConfigurationManagerImpl.cpp index 84d18117106a13..506ea83f567b87 100644 --- a/src/platform/nRF5/ConfigurationManagerImpl.cpp +++ b/src/platform/nRF5/ConfigurationManagerImpl.cpp @@ -93,6 +93,7 @@ bool ConfigurationManagerImpl::_CanFactoryReset() void ConfigurationManagerImpl::_InitiateFactoryReset() { + ChipLogError(DeviceLayer, "_InitiateFactoryReset"); PlatformMgr().ScheduleWork(DoFactoryReset); }