diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index 4f1ed485c156b4..86e629d4d54c68 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -191,6 +191,7 @@ chip_gn_arg_string("zephyr_ar" ${CMAKE_AR}) chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) +chip_gn_arg_bool ("chip_logging" CONFIG_LOG) chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) chip_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT) @@ -199,6 +200,10 @@ chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQU chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) +chip_gn_arg_bool ("chip_error_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 1) +chip_gn_arg_bool ("chip_progress_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 3) +chip_gn_arg_bool ("chip_detail_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 4) +chip_gn_arg_bool ("chip_automation_logging" FALSE) if (CONFIG_CHIP_FACTORY_DATA) chip_gn_arg_bool ("chip_use_transitional_commissionable_data_provider" "false") diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index 894986b2bf69ec..80d8770e8ed7e3 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -149,3 +149,16 @@ config CHIP_LOG_SIZE_OPTIMIZATION information that is too detailed to be used in most cases. You can find full configuration enabled by this option in the platform/telink/CHIPPlatformConfig.h file. + +config CHIP_BUTTON_MANAGER_IRQ_MODE + bool "Use GPIO in an IRQ mode instead of polling the GPIO" + default n + help + Use GPIO in an IRQ mode to avoid button polling loop and extend the battery lifetime by waking up by GPIO event. + GPIO events are working only with GPIO IRQ. This option changes button matrix configuration. + +config CHIP_ENABLE_APPLICATION_STATUS_LED + bool "Enable application status LED" + default y + help + Enable application status LED. diff --git a/examples/all-clusters-app/telink/include/AppTask.h b/examples/all-clusters-app/telink/include/AppTask.h index 69e5d61669752b..88cc4d8418d177 100644 --- a/examples/all-clusters-app/telink/include/AppTask.h +++ b/examples/all-clusters-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,13 +19,19 @@ #pragma once #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include #include +#if CONFIG_CHIP_FACTORY_DATA +#include +#endif + #include struct k_timer; @@ -41,14 +47,18 @@ class AppTask private: friend AppTask & GetAppTask(void); + CHIP_ERROR Init(void); static void ActionIdentifyStateUpdateHandler(k_timer * timer); void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(void); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void FactoryResetButtonEventHandler(void); static void StartThreadButtonEventHandler(void); static void StartBleAdvButtonEventHandler(void); @@ -61,7 +71,6 @@ class AppTask static void FactoryResetHandler(AppEvent * aEvent); static void StartThreadHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void InitButtons(void); @@ -70,6 +79,10 @@ class AppTask static AppTask sAppTask; PWMDevice mPwmIdentifyLed; + +#if CONFIG_CHIP_FACTORY_DATA + chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; +#endif }; inline AppTask & GetAppTask(void) diff --git a/examples/all-clusters-app/telink/prj.conf b/examples/all-clusters-app/telink/prj.conf index be2598cb6ade52..867ef065f12fe0 100644 --- a/examples/all-clusters-app/telink/prj.conf +++ b/examples/all-clusters-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=y CONFIG_OPENTHREAD_FTD=n +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -53,4 +56,19 @@ CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING="2022" CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y # CHIP shell -CONFIG_CHIP_LIB_SHELL=n \ No newline at end of file +CONFIG_CHIP_LIB_SHELL=n + +# Disable factory data support. +CONFIG_CHIP_FACTORY_DATA=n +CONFIG_CHIP_FACTORY_DATA_BUILD=n +CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n diff --git a/examples/all-clusters-app/telink/src/AppTask.cpp b/examples/all-clusters-app/telink/src/AppTask.cpp index 41abf953c3300b..cffc3cdb6c4426 100644 --- a/examples/all-clusters-app/telink/src/AppTask.cpp +++ b/examples/all-clusters-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,16 +22,14 @@ #include "AppEvent.h" #include "ButtonManager.h" #include "binding-handler.h" -#include -#include - -#include #include "ThreadUtil.h" +#include #include +#include +#include #include - #include #include @@ -67,7 +65,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -84,20 +83,28 @@ constexpr uint32_t kIdentifyBreatheRateMs = 1000; const struct pwm_dt_spec sPwmIdentifySpecGreenLed = LIGHTING_PWM_SPEC_IDENTIFY_GREEN; +#if CONFIG_CHIP_FACTORY_DATA +// NOTE! This key is for test/certification only and should not be available in production devices! +uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; +#endif + K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif Button sFactoryResetButton; Button sThreadStartButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -135,12 +142,14 @@ CHIP_ERROR AppTask::Init(void) { LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); - // Initialize status LED + // Initialize LEDs +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -148,11 +157,6 @@ CHIP_ERROR AppTask::Init(void) k_timer_init(&sFactoryResetTimer, &AppTask::FactoryResetTimerTimeoutCallback, nullptr); k_timer_user_data_set(&sFactoryResetTimer, this); - // Init ZCL Data Model and start server - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - // Initialize PWM Identify led CHIP_ERROR err = sAppTask.mPwmIdentifyLed.Init(&sPwmIdentifySpecGreenLed, kDefaultMinLevel, kDefaultMaxLevel, kDefaultMaxLevel); if (err != CHIP_NO_ERROR) @@ -163,10 +167,28 @@ CHIP_ERROR AppTask::Init(void) sAppTask.mPwmIdentifyLed.SetCallbacks(nullptr, nullptr, ActionIdentifyStateUpdateHandler); - // Initialize device attestation config + // Initialize CHIP server +#if CONFIG_CHIP_FACTORY_DATA + ReturnErrorOnFailure(mFactoryDataProvider.Init()); + SetDeviceInstanceInfoProvider(&mFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&mFactoryDataProvider); + SetCommissionableDataProvider(&mFactoryDataProvider); + // Read EnableKey from the factory data. + MutableByteSpan enableKey(sTestEventTriggerEnableKey); + err = mFactoryDataProvider.GetEnableKey(enableKey); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("mFactoryDataProvider.GetEnableKey() failed. Could not delegate a test event trigger"); + memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey)); + } +#else SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif - gExampleDeviceInfoProvider.SetStorageDelegate(&chip::Server::GetInstance().GetPersistentStorage()); + static CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); + gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage()); chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider); #if CONFIG_CHIP_OTA_REQUESTOR @@ -226,13 +248,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -298,15 +315,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -369,6 +391,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -404,6 +427,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -411,13 +435,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -485,16 +513,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sThreadStartButton); diff --git a/examples/all-clusters-app/telink/src/main.cpp b/examples/all-clusters-app/telink/src/main.cpp index 4061e239ee8fbc..dc8216db43d274 100644 --- a/examples/all-clusters-app/telink/src/main.cpp +++ b/examples/all-clusters-app/telink/src/main.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -61,7 +61,9 @@ int main(void) goto exit; } -#ifdef CONFIG_OPENTHREAD_MTD +#ifdef CONFIG_OPENTHREAD_MTD_SED + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); +#elif CONFIG_OPENTHREAD_MTD err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); #else err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); diff --git a/examples/all-clusters-minimal-app/telink/include/AppConfig.h b/examples/all-clusters-minimal-app/telink/include/AppConfig.h index 27d2cc072c03fc..1e0b2ae8e72ec0 100644 --- a/examples/all-clusters-minimal-app/telink/include/AppConfig.h +++ b/examples/all-clusters-minimal-app/telink/include/AppConfig.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,5 @@ #define BUTTON_PIN_2 0 // LEDs config -// System led config -#define SYSTEM_STATE_LED_PORT DEVICE_DT_GET(DT_NODELABEL(gpiob)) -#define SYSTEM_STATE_LED_PIN 7 +#define LEDS_PORT DEVICE_DT_GET(DT_NODELABEL(gpiob)) +#define SYSTEM_STATE_LED 7 diff --git a/examples/all-clusters-minimal-app/telink/include/AppTask.h b/examples/all-clusters-minimal-app/telink/include/AppTask.h index 7d0168759055d0..596f0836d2ea2c 100644 --- a/examples/all-clusters-minimal-app/telink/include/AppTask.h +++ b/examples/all-clusters-minimal-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,9 @@ #pragma once #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include @@ -43,8 +45,11 @@ class AppTask void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void FactoryResetButtonEventHandler(void); static void StartBleAdvButtonEventHandler(void); @@ -55,7 +60,6 @@ class AppTask static void FactoryResetTimerEventHandler(AppEvent * aEvent); static void FactoryResetHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void InitButtons(void); diff --git a/examples/all-clusters-minimal-app/telink/prj.conf b/examples/all-clusters-minimal-app/telink/prj.conf index 0d56373b56dffe..2c563df99dad1b 100644 --- a/examples/all-clusters-minimal-app/telink/prj.conf +++ b/examples/all-clusters-minimal-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -50,4 +50,10 @@ CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING="2022" CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y # CHIP shell -CONFIG_CHIP_LIB_SHELL=n \ No newline at end of file +CONFIG_CHIP_LIB_SHELL=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y diff --git a/examples/all-clusters-minimal-app/telink/src/AppTask.cpp b/examples/all-clusters-minimal-app/telink/src/AppTask.cpp index 1577259b94feba..0dc0bb1a320a0d 100644 --- a/examples/all-clusters-minimal-app/telink/src/AppTask.cpp +++ b/examples/all-clusters-minimal-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +22,12 @@ #include "AppEvent.h" #include "ButtonManager.h" #include "binding-handler.h" -#include -#include #include "ThreadUtil.h" +#include +#include #include - #include #include @@ -60,24 +59,27 @@ SHELL_CMD_REGISTER(telink, &sub_telink, "Telink commands", NULL); LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; -constexpr int kAppEventQueueSize = 10; -constexpr uint8_t kButtonPushEvent = 1; -constexpr uint8_t kButtonReleaseEvent = 0; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; +constexpr int kAppEventQueueSize = 10; +constexpr uint8_t kButtonPushEvent = 1; +constexpr uint8_t kButtonReleaseEvent = 0; K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif Button sFactoryResetButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; } // namespace @@ -103,16 +105,18 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; CHIP_ERROR AppTask::Init() { - CHIP_ERROR ret; + CHIP_ERROR err; LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); // Initialize status LED - LEDWidget::InitGpio(SYSTEM_STATE_LED_PORT); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); - sStatusLED.Init(SYSTEM_STATE_LED_PIN); + sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -140,11 +144,11 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); // Configure Bindings - ret = InitBindingHandlers(); - if (ret != CHIP_NO_ERROR) + err = InitBindingHandlers(); + if (err != CHIP_NO_ERROR) { LOG_ERR("InitBindingHandlers fail"); - return ret; + return err; } PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); @@ -154,18 +158,18 @@ CHIP_ERROR AppTask::Init() // between the main and the CHIP threads. PlatformMgr().AddEventHandler(ChipEventHandler, 0); - ret = ConnectivityMgr().SetBLEDeviceName("TelinkMinApp"); - if (ret != CHIP_NO_ERROR) + err = ConnectivityMgr().SetBLEDeviceName("TelinkMinApp"); + if (err != CHIP_NO_ERROR) { LOG_ERR("SetBLEDeviceName fail"); - return ret; + return err; } - ret = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(new AppFabricTableDelegate); - if (ret != CHIP_NO_ERROR) + err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(new AppFabricTableDelegate); + if (err != CHIP_NO_ERROR) { LOG_ERR("AppFabricTableDelegate fail"); - return ret; + return err; } return CHIP_NO_ERROR; @@ -185,13 +189,8 @@ CHIP_ERROR AppTask::StartApp() while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -207,15 +206,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -252,6 +256,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -287,6 +292,7 @@ void AppTask::UpdateStatusLED() sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -294,13 +300,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -359,15 +369,19 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sBleAdvStartButton); diff --git a/examples/all-clusters-minimal-app/telink/src/main.cpp b/examples/all-clusters-minimal-app/telink/src/main.cpp index 4061e239ee8fbc..dc8216db43d274 100644 --- a/examples/all-clusters-minimal-app/telink/src/main.cpp +++ b/examples/all-clusters-minimal-app/telink/src/main.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -61,7 +61,9 @@ int main(void) goto exit; } -#ifdef CONFIG_OPENTHREAD_MTD +#ifdef CONFIG_OPENTHREAD_MTD_SED + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); +#elif CONFIG_OPENTHREAD_MTD err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); #else err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); diff --git a/examples/contact-sensor-app/telink/boards/tlsr9518adk80d.overlay b/examples/contact-sensor-app/telink/boards/tlsr9518adk80d.overlay new file mode 100755 index 00000000000000..6e5b59a7c5a8f0 --- /dev/null +++ b/examples/contact-sensor-app/telink/boards/tlsr9518adk80d.overlay @@ -0,0 +1,3 @@ +&cpu0 { + clock-frequency = <32000000>; +}; \ No newline at end of file diff --git a/examples/contact-sensor-app/telink/include/AppTask.h b/examples/contact-sensor-app/telink/include/AppTask.h index 0cf75d63696004..8c022cd9a585ea 100644 --- a/examples/contact-sensor-app/telink/include/AppTask.h +++ b/examples/contact-sensor-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,9 @@ #include "AppEvent.h" #include "ContactSensorManager.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include @@ -56,6 +58,7 @@ class AppTask private: friend AppTask & GetAppTask(void); + CHIP_ERROR Init(void); static void ActionIdentifyStateUpdateHandler(k_timer * timer); @@ -67,8 +70,11 @@ class AppTask static void UpdateClusterStateInternal(intptr_t arg); static void UpdateDeviceStateInternal(intptr_t arg); - static void UpdateStatusLED(void); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void FactoryResetButtonEventHandler(void); static void StartBleAdvButtonEventHandler(void); static void ToggleContactStateButtonEventHandler(void); @@ -81,7 +87,6 @@ class AppTask static void FactoryResetHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); static void ContactActionEventHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void InitButtons(void); diff --git a/examples/contact-sensor-app/telink/prj.conf b/examples/contact-sensor-app/telink/prj.conf index 3c582145b38a29..8ad82c35fab3a8 100755 --- a/examples/contact-sensor-app/telink/prj.conf +++ b/examples/contact-sensor-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=y CONFIG_OPENTHREAD_FTD=n +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -58,4 +61,14 @@ CONFIG_CHIP_LIB_SHELL=n # Disable factory data support. CONFIG_CHIP_FACTORY_DATA=n CONFIG_CHIP_FACTORY_DATA_BUILD=n -CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n \ No newline at end of file +CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n diff --git a/examples/contact-sensor-app/telink/src/AppTask.cpp b/examples/contact-sensor-app/telink/src/AppTask.cpp index 375b649928b5a1..fc62aa79e2b4eb 100644 --- a/examples/contact-sensor-app/telink/src/AppTask.cpp +++ b/examples/contact-sensor-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -49,7 +49,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -74,19 +75,21 @@ uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; LEDWidget sContactSensorLED; +#endif Button sFactoryResetButton; Button sToggleContactStateButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -122,7 +125,8 @@ CHIP_ERROR AppTask::Init(void) { LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); - // Initialize status LED + // Initialize LEDs +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); @@ -131,6 +135,7 @@ CHIP_ERROR AppTask::Init(void) sContactSensorLED.Init(CONTACT_STATE_LED); sContactSensorLED.Set(ContactSensorMgr().IsContactClosed()); +#endif UpdateDeviceState(); @@ -216,12 +221,16 @@ void AppTask::OnStateChanged(ContactSensorManager::State aState) if (ContactSensorManager::State::kContactClosed == aState) { LOG_INF("Contact state changed to CLOSED"); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED sContactSensorLED.Set(true); +#endif } else if (ContactSensorManager::State::kContactOpened == aState) { LOG_INF("Contact state changed to OPEN"); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED sContactSensorLED.Set(false); +#endif } if (sAppTask.IsSyncClusterToButtonAction()) @@ -244,13 +253,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -337,15 +341,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -432,6 +441,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -467,6 +477,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -474,13 +485,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -553,16 +568,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sToggleContactStateButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, false, ToggleContactStateButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sToggleContactStateButton.Configure(BUTTON_PORT, BUTTON_PIN_2, ToggleContactStateButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sToggleContactStateButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, ToggleContactStateButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sToggleContactStateButton); @@ -582,5 +602,7 @@ void AppTask::UpdateDeviceStateInternal(intptr_t arg) (void) app::Clusters::BooleanState::Attributes::StateValue::Get(1, &stateValueAttrValue); ChipLogProgress(NotSpecified, "emberAfReadAttribute : %d", stateValueAttrValue); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED sContactSensorLED.Set(stateValueAttrValue); +#endif } diff --git a/examples/light-switch-app/telink/README.md b/examples/light-switch-app/telink/README.md index a150047a5ec4d0..865f482b9f093b 100755 --- a/examples/light-switch-app/telink/README.md +++ b/examples/light-switch-app/telink/README.md @@ -159,7 +159,7 @@ To perform the unicast binding process, complete the following steps: 2. Add a binding table to the Light Switch binding cluster: ```bash - $ ./chip-tool binding write binding '[{"fabricIndex": 1, "node": , "endpoint": 1, "cluster": 6}, {"fabricIndex": 1, "node": , "endpoint": 1, "cluster": 8}]' 1 + $ ./chip-tool binding write binding '[{"fabricIndex": 1, "node": , "endpoint": 1, "cluster": 6}]' 1 ``` In this command: diff --git a/examples/light-switch-app/telink/boards/tlsr9518adk80d.overlay b/examples/light-switch-app/telink/boards/tlsr9518adk80d.overlay new file mode 100644 index 00000000000000..6e5b59a7c5a8f0 --- /dev/null +++ b/examples/light-switch-app/telink/boards/tlsr9518adk80d.overlay @@ -0,0 +1,3 @@ +&cpu0 { + clock-frequency = <32000000>; +}; \ No newline at end of file diff --git a/examples/light-switch-app/telink/include/AppConfig.h b/examples/light-switch-app/telink/include/AppConfig.h index 688d848ea520ae..d41e4c557b4598 100755 --- a/examples/light-switch-app/telink/include/AppConfig.h +++ b/examples/light-switch-app/telink/include/AppConfig.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +18,7 @@ #pragma once -// ---- Lighting Example App Config ---- +// ---- Light Switch Example App Config ---- // Buttons config #define BUTTON_PORT DEVICE_DT_GET(DT_NODELABEL(gpioc)) diff --git a/examples/light-switch-app/telink/include/AppTask.h b/examples/light-switch-app/telink/include/AppTask.h index 8226505fcc2ccc..953b677e083cde 100644 --- a/examples/light-switch-app/telink/include/AppTask.h +++ b/examples/light-switch-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,9 @@ #pragma once #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include @@ -65,8 +67,11 @@ class AppTask void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void SwitchActionButtonEventHandler(void); static void FactoryResetButtonEventHandler(void); static void StartThreadButtonEventHandler(void); @@ -81,7 +86,6 @@ class AppTask static void StartThreadHandler(AppEvent * aEvent); static void SwitchActionEventHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void InitButtons(void); @@ -92,7 +96,6 @@ class AppTask PWMDevice mPwmIdentifyLed; #if CONFIG_CHIP_FACTORY_DATA - // chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; #endif }; diff --git a/examples/light-switch-app/telink/prj.conf b/examples/light-switch-app/telink/prj.conf index 757920cafb7165..a6054d82342638 100755 --- a/examples/light-switch-app/telink/prj.conf +++ b/examples/light-switch-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=y CONFIG_OPENTHREAD_FTD=n +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -58,4 +61,14 @@ CONFIG_CHIP_LIB_SHELL=n # Disable factory data support. CONFIG_CHIP_FACTORY_DATA=n CONFIG_CHIP_FACTORY_DATA_BUILD=n -CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n \ No newline at end of file +CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n diff --git a/examples/light-switch-app/telink/src/AppTask.cpp b/examples/light-switch-app/telink/src/AppTask.cpp index 9cf419a884e5c5..ffa763dfe5f7b1 100644 --- a/examples/light-switch-app/telink/src/AppTask.cpp +++ b/examples/light-switch-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -69,7 +69,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -94,19 +95,21 @@ uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif Button sFactoryResetButton; Button sSwitchButton; Button sThreadStartButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -143,11 +146,13 @@ CHIP_ERROR AppTask::Init(void) LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); // Initialize status LED +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -184,9 +189,7 @@ CHIP_ERROR AppTask::Init(void) #endif static CommonCaseDeviceServerInitParams initParams; - // static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) }; (void) initParams.InitializeStaticResourcesBeforeServerInit(); - // initParams.testEventTriggerDelegate = &testEventTriggerDelegate; ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage()); chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider); @@ -243,13 +246,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -337,15 +335,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -408,6 +411,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -443,6 +447,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -450,13 +455,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -536,17 +545,23 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sSwitchButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, false, SwitchActionButtonEventHandler); - sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sSwitchButton.Configure(BUTTON_PORT, BUTTON_PIN_2, SwitchActionButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sSwitchButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, SwitchActionButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sSwitchButton); diff --git a/examples/lighting-app/telink/include/AppTask.h b/examples/lighting-app/telink/include/AppTask.h index f0a175a4d9f9c1..dbfb0bef4fc63d 100644 --- a/examples/lighting-app/telink/include/AppTask.h +++ b/examples/lighting-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,9 @@ #include "AppConfig.h" #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include @@ -70,8 +72,11 @@ class AppTask void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(void); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void LightingActionButtonEventHandler(void); static void FactoryResetButtonEventHandler(void); static void StartThreadButtonEventHandler(void); @@ -86,7 +91,6 @@ class AppTask static void StartThreadHandler(AppEvent * aEvent); static void LightingActionEventHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void ButtonEventHandler(ButtonId_t btnId, bool btnPressed); @@ -103,7 +107,6 @@ class AppTask PWMDevice mPwmIdentifyLed; #if CONFIG_CHIP_FACTORY_DATA - // chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; #endif }; diff --git a/examples/lighting-app/telink/prj.conf b/examples/lighting-app/telink/prj.conf index f05cfc94f7cdce..8e02f9a2fa6692 100644 --- a/examples/lighting-app/telink/prj.conf +++ b/examples/lighting-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Project CHIP Authors +# Copyright (c) 2021-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=n CONFIG_OPENTHREAD_FTD=y +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -58,4 +61,18 @@ CONFIG_CHIP_LIB_SHELL=n # Disable factory data support. CONFIG_CHIP_FACTORY_DATA=n CONFIG_CHIP_FACTORY_DATA_BUILD=n -CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n \ No newline at end of file +CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n + +# Custom RF power values +CONFIG_B91_BLE_CTRL_RF_POWER_P9P11DBM=y +CONFIG_IEEE802154_B91_CUSTOM_RF_POWER=9 diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index 41a3cfcc57f373..47332a53748037 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -70,7 +70,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -100,8 +101,11 @@ uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif #if USE_RGB_PWM uint8_t sBrightness; PWMDevice::Action_t sColorAction = PWMDevice::INVALID_ACTION; @@ -115,11 +119,10 @@ Button sLightingButton; Button sThreadStartButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -156,12 +159,14 @@ CHIP_ERROR AppTask::Init(void) LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); // Initialize LEDs +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -227,9 +232,7 @@ CHIP_ERROR AppTask::Init(void) #endif static CommonCaseDeviceServerInitParams initParams; - // static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) }; (void) initParams.InitializeStaticResourcesBeforeServerInit(); - // initParams.testEventTriggerDelegate = &testEventTriggerDelegate; ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage()); @@ -278,13 +281,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -399,15 +397,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -470,6 +473,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -505,6 +509,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -512,13 +517,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -660,9 +669,8 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::ButtonEventHandler(ButtonId_t btnId, bool btnPressed) @@ -691,10 +699,17 @@ void AppTask::ButtonEventHandler(ButtonId_t btnId, bool btnPressed) void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sLightingButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, false, LightingActionButtonEventHandler); - sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sLightingButton.Configure(BUTTON_PORT, BUTTON_PIN_2, LightingActionButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sLightingButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, LightingActionButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sLightingButton); diff --git a/examples/lighting-app/telink/src/main.cpp b/examples/lighting-app/telink/src/main.cpp index ca6d59cbc9eae8..12abb1de76ec74 100644 --- a/examples/lighting-app/telink/src/main.cpp +++ b/examples/lighting-app/telink/src/main.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2021-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -69,7 +69,9 @@ int main(void) goto exit; } -#ifdef CONFIG_OPENTHREAD_MTD +#ifdef CONFIG_OPENTHREAD_MTD_SED + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); +#elif CONFIG_OPENTHREAD_MTD err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); #else err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); diff --git a/examples/ota-requestor-app/telink/include/AppTask.h b/examples/ota-requestor-app/telink/include/AppTask.h index d7111cc5f6ca9d..1a0c2aa4698962 100755 --- a/examples/ota-requestor-app/telink/include/AppTask.h +++ b/examples/ota-requestor-app/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,9 @@ #pragma once #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include @@ -59,8 +61,11 @@ class AppTask void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(void); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void FactoryResetButtonEventHandler(void); static void StartThreadButtonEventHandler(void); static void StartBleAdvButtonEventHandler(void); @@ -73,7 +78,6 @@ class AppTask static void FactoryResetHandler(AppEvent * aEvent); static void StartThreadHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void InitButtons(void); diff --git a/examples/ota-requestor-app/telink/prj.conf b/examples/ota-requestor-app/telink/prj.conf index e787d9ffeed55b..a5946d1a0f3fc4 100755 --- a/examples/ota-requestor-app/telink/prj.conf +++ b/examples/ota-requestor-app/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=y CONFIG_OPENTHREAD_FTD=n +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -52,4 +55,14 @@ CONFIG_CHIP_DEVICE_SOFTWARE_VERSION_STRING="2022" CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y # CHIP shell -CONFIG_CHIP_LIB_SHELL=n \ No newline at end of file +CONFIG_CHIP_LIB_SHELL=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n diff --git a/examples/ota-requestor-app/telink/src/AppTask.cpp b/examples/ota-requestor-app/telink/src/AppTask.cpp index 1480a01e591e35..8cc0cea6e3992d 100644 --- a/examples/ota-requestor-app/telink/src/AppTask.cpp +++ b/examples/ota-requestor-app/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,22 +21,18 @@ #include "AppConfig.h" #include "AppEvent.h" #include "ButtonManager.h" -#include -#include - -#include #include "ThreadUtil.h" +#include #include +#include +#include #include - #include #include - -#include - #include +#include #include #include #include @@ -76,7 +72,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -95,18 +92,20 @@ const struct pwm_dt_spec sPwmIdentifySpecGreenLed = LIGHTING_PWM_SPEC_IDENTIFY_G K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif Button sFactoryResetButton; Button sThreadStartButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -144,12 +143,14 @@ CHIP_ERROR AppTask::Init(void) { LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); - // Initialize status LED + // Initialize LEDs +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -226,13 +227,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -298,15 +294,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -369,6 +370,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -404,6 +406,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -411,13 +414,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -489,16 +496,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sThreadStartButton); diff --git a/examples/ota-requestor-app/telink/src/main.cpp b/examples/ota-requestor-app/telink/src/main.cpp index 4061e239ee8fbc..dc8216db43d274 100644 --- a/examples/ota-requestor-app/telink/src/main.cpp +++ b/examples/ota-requestor-app/telink/src/main.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -61,7 +61,9 @@ int main(void) goto exit; } -#ifdef CONFIG_OPENTHREAD_MTD +#ifdef CONFIG_OPENTHREAD_MTD_SED + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); +#elif CONFIG_OPENTHREAD_MTD err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); #else err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); diff --git a/examples/platform/telink/util/include/ButtonManager.h b/examples/platform/telink/util/include/ButtonManager.h index c728d59a5ea48d..d78837477e1918 100644 --- a/examples/platform/telink/util/include/ButtonManager.h +++ b/examples/platform/telink/util/include/ButtonManager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2021-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,8 +26,10 @@ class Button { public: - void Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, bool intBothLevel, void (*callback)(void)); + void Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, void (*callback)(void)); + void Configure(const struct device * port, gpio_pin_t inPin, void (*callback)(void)); void Poll(Button * previous); + void PollIRQ(void); void SetCallback(void (*callback)(void)); private: @@ -38,7 +40,6 @@ class Button gpio_pin_t mOutPin; gpio_pin_t mInPin; int mPreviousState = STATE_LOW; - bool mIntBothLevel = false; void (*mCallback)(void) = NULL; }; @@ -47,6 +48,7 @@ class ButtonManager public: void Init(void); void Poll(void); + void PollIRQ(void); void AddButton(Button & button); void SetCallback(unsigned int index, void (*callback)(void)); diff --git a/examples/platform/telink/util/src/ButtonManager.cpp b/examples/platform/telink/util/src/ButtonManager.cpp index 21decd38b54298..4c964ec1b60de4 100644 --- a/examples/platform/telink/util/src/ButtonManager.cpp +++ b/examples/platform/telink/util/src/ButtonManager.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2021-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -29,21 +30,49 @@ LOG_MODULE_REGISTER(ButtonManager); ButtonManager ButtonManager::sInstance; -void Button::Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, bool intBothLevel, void (*callback)(void)) +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE +static struct gpio_callback button_cb_data; +void button_pressed(const struct device * dev, struct gpio_callback * cb, uint32_t pins); +#endif + +void Button::Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, void (*callback)(void)) { __ASSERT(device_is_ready(port), "%s is not ready\n", port->name); - mPort = port; - mOutPin = outPin; - mInPin = inPin; - mIntBothLevel = intBothLevel; - mCallback = callback; + mPort = port; + mOutPin = outPin; + mInPin = inPin; + mCallback = callback; } int Button::Init(void) { int ret = 0; +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + ret = gpio_pin_configure(mPort, mInPin, GPIO_INPUT | GPIO_PULL_UP); + if (ret < 0) + { + LOG_ERR("Config in pin err: %d", ret); + return ret; + } + + ret = gpio_pin_interrupt_configure(mPort, mInPin, GPIO_INT_EDGE_FALLING); + if (ret < 0) + { + LOG_ERR("Config irq pin err: %d", ret); + return ret; + } + + gpio_init_callback(&button_cb_data, button_pressed, mInPin); + ret = gpio_add_callback(mPort, &button_cb_data); + if (ret < 0) + { + LOG_ERR("Config gpio_init_callback err: %d", ret); + return ret; + } +#else + ret = gpio_pin_configure(mPort, mOutPin, GPIO_OUTPUT_ACTIVE); if (ret < 0) { @@ -57,6 +86,7 @@ int Button::Init(void) LOG_ERR("Config in pin err: %d", ret); return ret; } +#endif return ret; } @@ -92,7 +122,7 @@ void Button::Poll(Button * previous) ret = gpio_pin_get(mPort, mInPin); assert(ret >= 0); - if ((mIntBothLevel && ret != mPreviousState) || (!mIntBothLevel && ret == STATE_HIGH && ret != mPreviousState)) + if (ret == STATE_HIGH && ret != mPreviousState) { if (mCallback != NULL) { @@ -148,4 +178,44 @@ void ButtonEntry(void * param1, void * param2, void * param3) } } +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE +void button_pressed(const struct device * dev, struct gpio_callback * cb, uint32_t pins) +{ + ButtonManager & sInstance = ButtonManagerInst(); + sInstance.PollIRQ(); +} + +void ButtonManager::PollIRQ(void) +{ + for (unsigned int i = 0; i < mButtons.size(); i++) + { + mButtons[i].PollIRQ(); + } +} + +void Button::PollIRQ() +{ + int ret = gpio_pin_get(mPort, mInPin); + if (ret == STATE_LOW) + { + if (mCallback != NULL) + { + mCallback(); + } + } +} + +void Button::Configure(const struct device * port, gpio_pin_t inPin, void (*callback)(void)) +{ + __ASSERT(device_is_ready(port), "%s is not ready\n", port->name); + + mPort = port; + mInPin = inPin; + mCallback = callback; + + Init(); +} + +#else K_THREAD_DEFINE(buttonThread, 512, ButtonEntry, NULL, NULL, NULL, K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1), 0, 0); +#endif diff --git a/examples/thermostat/telink/boards/tlsr9518adk80d.overlay b/examples/thermostat/telink/boards/tlsr9518adk80d.overlay new file mode 100755 index 00000000000000..6e5b59a7c5a8f0 --- /dev/null +++ b/examples/thermostat/telink/boards/tlsr9518adk80d.overlay @@ -0,0 +1,3 @@ +&cpu0 { + clock-frequency = <32000000>; +}; \ No newline at end of file diff --git a/examples/thermostat/telink/include/AppTask.h b/examples/thermostat/telink/include/AppTask.h index 2f75c4f308133b..facc5e7fb63d2e 100755 --- a/examples/thermostat/telink/include/AppTask.h +++ b/examples/thermostat/telink/include/AppTask.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,9 @@ #pragma once #include "AppEvent.h" +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED #include "LEDWidget.h" +#endif #include "PWMDevice.h" #include "SensorManager.h" #include "TemperatureManager.h" @@ -56,8 +58,11 @@ class AppTask void DispatchEvent(AppEvent * event); - static void UpdateStatusLED(void); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED + static void UpdateLedStateEventHandler(AppEvent * aEvent); static void LEDStateUpdateHandler(LEDWidget * ledWidget); + static void UpdateStatusLED(); +#endif static void FactoryResetButtonEventHandler(void); static void StartThreadButtonEventHandler(void); static void StartBleAdvButtonEventHandler(void); @@ -70,7 +75,6 @@ class AppTask static void FactoryResetHandler(AppEvent * aEvent); static void StartThreadHandler(AppEvent * aEvent); static void StartBleAdvHandler(AppEvent * aEvent); - static void UpdateLedStateEventHandler(AppEvent * aEvent); static void UpdateIdentifyStateEventHandler(AppEvent * aEvent); static void InitButtons(void); @@ -81,7 +85,6 @@ class AppTask PWMDevice mPwmIdentifyLed; #if CONFIG_CHIP_FACTORY_DATA - // chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; chip::DeviceLayer::FactoryDataProvider mFactoryDataProvider; #endif }; diff --git a/examples/thermostat/telink/prj.conf b/examples/thermostat/telink/prj.conf index 26765a55c99d10..3fe18aefdc638a 100755 --- a/examples/thermostat/telink/prj.conf +++ b/examples/thermostat/telink/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 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. @@ -27,6 +27,9 @@ CONFIG_PWM=y # OpenThread configs CONFIG_OPENTHREAD_MTD=y CONFIG_OPENTHREAD_FTD=n +CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT=n +CONFIG_CHIP_SED_IDLE_INTERVAL=200 +CONFIG_CHIP_THREAD_SSED=n # Default OpenThread network settings CONFIG_OPENTHREAD_PANID=4660 @@ -58,4 +61,14 @@ CONFIG_CHIP_LIB_SHELL=n # Disable factory data support. CONFIG_CHIP_FACTORY_DATA=n CONFIG_CHIP_FACTORY_DATA_BUILD=n -CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n \ No newline at end of file +CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n + +# Enable Button IRQ mode. The poling mode is used by default. +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n + +# Disable Status LED. +CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED=y + +# Enable Power Management +CONFIG_PM=n +CONFIG_PM_DEVICE=n diff --git a/examples/thermostat/telink/src/AppTask.cpp b/examples/thermostat/telink/src/AppTask.cpp index 6c4d026329c422..d7ab453dffce17 100644 --- a/examples/thermostat/telink/src/AppTask.cpp +++ b/examples/thermostat/telink/src/AppTask.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -68,7 +68,8 @@ using namespace ::chip::Credentials; using namespace ::chip::DeviceLayer; namespace { -constexpr int kFactoryResetTriggerTimeout = 2000; +constexpr int kFactoryResetCalcTimeout = 3000; +constexpr int kFactoryResetTriggerCntr = 3; constexpr int kAppEventQueueSize = 10; constexpr uint8_t kButtonPushEvent = 1; constexpr uint8_t kButtonReleaseEvent = 0; @@ -93,18 +94,20 @@ uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); k_timer sFactoryResetTimer; +uint8_t sFactoryResetCntr = 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget sStatusLED; +#endif Button sFactoryResetButton; Button sThreadStartButton; Button sBleAdvStartButton; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; -bool sIsFactoryResetTimerActive = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -140,12 +143,14 @@ CHIP_ERROR AppTask::Init(void) { LOG_INF("SW Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); - // Initialize status LED + // Initialize LEDs +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED LEDWidget::InitGpio(LEDS_PORT); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); sStatusLED.Init(SYSTEM_STATE_LED); UpdateStatusLED(); +#endif InitButtons(); @@ -182,9 +187,7 @@ CHIP_ERROR AppTask::Init(void) #endif static CommonCaseDeviceServerInitParams initParams; - // static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) }; (void) initParams.InitializeStaticResourcesBeforeServerInit(); - // initParams.testEventTriggerDelegate = &testEventTriggerDelegate; ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage()); @@ -246,13 +249,8 @@ CHIP_ERROR AppTask::StartApp(void) while (true) { - int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); - - while (!ret) - { - DispatchEvent(&event); - ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); - } + k_msgq_get(&sAppEventQueue, &event, K_FOREVER); + DispatchEvent(&event); } } @@ -324,15 +322,20 @@ void AppTask::FactoryResetButtonEventHandler(void) void AppTask::FactoryResetHandler(AppEvent * aEvent) { - if (!sIsFactoryResetTimerActive) + if (sFactoryResetCntr == 0) { - k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetTriggerTimeout), K_NO_WAIT); - sIsFactoryResetTimerActive = true; + k_timer_start(&sFactoryResetTimer, K_MSEC(kFactoryResetCalcTimeout), K_NO_WAIT); } - else + + sFactoryResetCntr++; + LOG_INF("Factory Reset Trigger Counter: %d/%d", sFactoryResetCntr, kFactoryResetTriggerCntr); + + if (sFactoryResetCntr == kFactoryResetTriggerCntr) { k_timer_stop(&sFactoryResetTimer); - sIsFactoryResetTimerActive = false; + sFactoryResetCntr = 0; + + chip::Server::GetInstance().ScheduleFactoryReset(); } } @@ -395,6 +398,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent) } } +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED void AppTask::UpdateLedStateEventHandler(AppEvent * aEvent) { if (aEvent->Type == AppEvent::kEventType_UpdateLedState) @@ -430,6 +434,7 @@ void AppTask::UpdateStatusLED(void) sStatusLED.Blink(50, 950); } } +#endif void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { @@ -437,13 +442,17 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */ { case DeviceEventType::kCHIPoBLEAdvertisingChange: sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadStateChange: sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); +#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED UpdateStatusLED(); +#endif break; case DeviceEventType::kThreadConnectivityChange: #if CONFIG_CHIP_OTA_REQUESTOR @@ -513,16 +522,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent) return; } - sIsFactoryResetTimerActive = false; - LOG_INF("FactoryResetHandler"); - chip::Server::GetInstance().ScheduleFactoryReset(); + sFactoryResetCntr = 0; + LOG_INF("Factory Reset Trigger Counter is cleared"); } void AppTask::InitButtons(void) { - sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true, FactoryResetButtonEventHandler); - sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false, StartThreadButtonEventHandler); - sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false, StartBleAdvButtonEventHandler); +#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler); +#else + sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); + sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler); + sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); +#endif ButtonManagerInst().AddButton(sFactoryResetButton); ButtonManagerInst().AddButton(sThreadStartButton); diff --git a/src/platform/telink/CHIPDevicePlatformConfig.h b/src/platform/telink/CHIPDevicePlatformConfig.h index 28c93f1d4e6ee9..f4d25b7ce5dfd9 100644 --- a/src/platform/telink/CHIPDevicePlatformConfig.h +++ b/src/platform/telink/CHIPDevicePlatformConfig.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2022-2023 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. @@ -215,3 +215,20 @@ #ifdef CONFIG_CHIP_EXTENDED_DISCOVERY #define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1 #endif // CONFIG_CHIP_EXTENDED_DISCOVERY + +#ifdef CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT +#define CHIP_DEVICE_CONFIG_ENABLE_SED 1 +#define CHIP_DEVICE_CONFIG_THREAD_SSED CONFIG_CHIP_THREAD_SSED +#endif // CONFIG_CHIP_ENABLE_SLEEPY_END_DEVICE_SUPPORT + +#ifndef CHIP_DEVICE_CONFIG_SED_IDLE_INTERVAL +#ifdef CONFIG_CHIP_SED_IDLE_INTERVAL +#define CHIP_DEVICE_CONFIG_SED_IDLE_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_CHIP_SED_IDLE_INTERVAL) +#endif // CONFIG_CHIP_SED_IDLE_INTERVAL +#endif // CHIP_DEVICE_CONFIG_SED_IDLE_INTERVAL + +#ifndef CHIP_DEVICE_CONFIG_SED_ACTIVE_INTERVAL +#ifdef CONFIG_CHIP_SED_ACTIVE_INTERVAL +#define CHIP_DEVICE_CONFIG_SED_ACTIVE_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_CHIP_SED_ACTIVE_INTERVAL) +#endif // CONFIG_CHIP_SED_ACTIVE_INTERVAL +#endif // CHIP_DEVICE_CONFIG_SED_ACTIVE_INTERVAL