From 02ebc69a557b8975daf8c693a13fa4dff4768f20 Mon Sep 17 00:00:00 2001 From: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:20:11 -0500 Subject: [PATCH] Reuse BaseApplication factory reset sequence. more cleanup --- examples/platform/silabs/BaseApplication.cpp | 114 +++++++++--------- examples/platform/silabs/BaseApplication.h | 14 +-- examples/window-app/silabs/include/AppEvent.h | 15 --- .../window-app/silabs/include/WindowManager.h | 3 +- examples/window-app/silabs/src/AppTask.cpp | 2 +- .../window-app/silabs/src/WindowManager.cpp | 64 ++-------- 6 files changed, 74 insertions(+), 138 deletions(-) diff --git a/examples/platform/silabs/BaseApplication.cpp b/examples/platform/silabs/BaseApplication.cpp index 8afa9d8b5337fe..fcb6a76156c374 100644 --- a/examples/platform/silabs/BaseApplication.cpp +++ b/examples/platform/silabs/BaseApplication.cpp @@ -129,9 +129,6 @@ StaticQueue_t sAppEventQueueStruct; StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)]; StaticTask_t appTaskStruct; -BaseApplication::Function_t mFunction; -bool mFunctionTimerActive; - #ifdef DISPLAY_ENABLED SilabsLCD slLCD; #endif @@ -149,7 +146,8 @@ Identify gIdentify = { #endif // EMBER_AF_PLUGIN_IDENTIFY_SERVER } // namespace -bool BaseApplication::sIsProvisioned = false; +bool BaseApplication::sIsProvisioned = false; +bool BaseApplication::mIsFactoryResetTriggered = false; #ifdef DIC_ENABLE namespace { @@ -280,39 +278,16 @@ void BaseApplication::FunctionTimerEventHandler(TimerHandle_t xTimer) void BaseApplication::FunctionEventHandler(AppEvent * aEvent) { - if (aEvent->Type != AppEvent::kEventType_Timer) - { - return; - } - + VerifyOrReturn(aEvent->Type == AppEvent::kEventType_Timer); // If we reached here, the button was held past FACTORY_RESET_TRIGGER_TIMEOUT, - // initiate factory reset - if (mFunctionTimerActive && mFunction == kFunction_StartBleAdv) + if (!mIsFactoryResetTriggered) { - SILABS_LOG("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_CANCEL_WINDOW_TIMEOUT); - - // Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to - // cancel, if required. - StartFunctionTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT); - -#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1 - StartStatusLEDTimer(); -#endif // CHIP_CONFIG_ENABLE_ICD_SERVER - - mFunction = kFunction_FactoryReset; - -#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917))) - // Turn off all LEDs before starting blink to make sure blink is - // co-ordinated. - sStatusLED.Set(false); - sStatusLED.Blink(500); -#endif // ENABLE_WSTK_LEDS + StartFactoryResetSequence(); } - else if (mFunctionTimerActive && mFunction == kFunction_FactoryReset) + else { - // Actually trigger Factory Reset - mFunction = kFunction_NoneSelected; - + // The factory reset sequence was in motion. The cancellation window expired. + // Factory Reset the device now. #if CHIP_CONFIG_ENABLE_ICD_SERVER == 1 StopStatusLEDTimer(); #endif // CHIP_CONFIG_ENABLE_ICD_SERVER @@ -449,7 +424,7 @@ void BaseApplication::LightEventHandler() // the LEDs at an even rate of 100ms. // // Otherwise, blink the LED ON for a very short time. - if (mFunction != kFunction_FactoryReset) + if (!mIsFactoryResetTriggered) { ActivateStatusLedPatterns(); } @@ -469,20 +444,22 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent) // start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT if (aEvent->ButtonEvent.Action == static_cast(SilabsPlatform::ButtonAction::ButtonPressed)) { - if (!mFunctionTimerActive && mFunction == kFunction_NoneSelected) - { - StartFunctionTimer(FACTORY_RESET_TRIGGER_TIMEOUT); - mFunction = kFunction_StartBleAdv; - } + StartFunctionTimer(FACTORY_RESET_TRIGGER_TIMEOUT); } else { - // If the button was released before factory reset got initiated, open the commissioning window and start BLE advertissement - // in fast mode - if (mFunctionTimerActive && mFunction == kFunction_StartBleAdv) + if (mIsFactoryResetTriggered) + { + CancelFactoryResetSequence(); + } + else { + // The factory reset sequence was not initiated, + // Press and Release: + // - Open the commissioning window and start BLE advertissement in fast mode when not commissioned + // - Output qr code in logs + // - Cycle LCD screen CancelFunctionTimer(); - mFunction = kFunction_NoneSelected; OutputQrCode(false); #ifdef DISPLAY_ENABLED @@ -515,19 +492,6 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent) #endif // CHIP_CONFIG_ENABLE_ICD_SERVER } } - else if (mFunctionTimerActive && mFunction == kFunction_FactoryReset) - { - CancelFunctionTimer(); - -#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1 - StopStatusLEDTimer(); -#endif - - // Change the function to none selected since factory reset has been - // canceled. - mFunction = kFunction_NoneSelected; - SILABS_LOG("Factory Reset has been Canceled"); - } } } @@ -538,8 +502,6 @@ void BaseApplication::CancelFunctionTimer() SILABS_LOG("app timer stop() failed"); appError(APP_ERROR_STOP_TIMER_FAILED); } - - mFunctionTimerActive = false; } void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs) @@ -558,8 +520,42 @@ void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs) SILABS_LOG("app timer start() failed"); appError(APP_ERROR_START_TIMER_FAILED); } +} + +void BaseApplication::StartFactoryResetSequence() +{ + // Initiate the factory reset sequence + SILABS_LOG("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_CANCEL_WINDOW_TIMEOUT); - mFunctionTimerActive = true; + // Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to + // cancel, if required. + StartFunctionTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT); + + mIsFactoryResetTriggered = true; +#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1 + StartStatusLEDTimer(); +#endif // CHIP_CONFIG_ENABLE_ICD_SERVER + +#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917))) + // Turn off all LEDs before starting blink to make sure blink is + // co-ordinated. + sStatusLED.Set(false); + sStatusLED.Blink(500); +#endif // ENABLE_WSTK_LEDS +} + +void BaseApplication::CancelFactoryResetSequence() +{ + CancelFunctionTimer(); + +#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1 + StopStatusLEDTimer(); +#endif + if (mIsFactoryResetTriggered) + { + mIsFactoryResetTriggered = false; + SILABS_LOG("Factory Reset has been Canceled"); + } } void BaseApplication::StartStatusLEDTimer() diff --git a/examples/platform/silabs/BaseApplication.h b/examples/platform/silabs/BaseApplication.h index 2527a164e229f8..67a44ee4e2954e 100644 --- a/examples/platform/silabs/BaseApplication.h +++ b/examples/platform/silabs/BaseApplication.h @@ -71,6 +71,7 @@ class BaseApplication BaseApplication() = default; virtual ~BaseApplication(){}; static bool sIsProvisioned; + static bool mIsFactoryResetTriggered; /** * @brief Create AppTask task and Event Queue @@ -106,6 +107,9 @@ class BaseApplication static void StopStatusLEDTimer(void); static bool GetProvisionStatus(void); + static void StartFactoryResetSequence(void); + static void CancelFactoryResetSequence(void); + #ifdef EMBER_AF_PLUGIN_IDENTIFY_SERVER // Idenfiy server command callbacks. static void OnIdentifyStart(Identify * identify); @@ -114,16 +118,6 @@ class BaseApplication static void OnTriggerIdentifyEffect(Identify * identify); #endif - enum Function_t - { - kFunction_NoneSelected = 0, - kFunction_SoftwareUpdate = 0, - kFunction_StartBleAdv = 1, - kFunction_FactoryReset = 2, - - kFunction_Invalid - } Function; - protected: CHIP_ERROR Init(); diff --git a/examples/window-app/silabs/include/AppEvent.h b/examples/window-app/silabs/include/AppEvent.h index 6ed767d099b9b7..f343a105e10919 100644 --- a/examples/window-app/silabs/include/AppEvent.h +++ b/examples/window-app/silabs/include/AppEvent.h @@ -32,14 +32,6 @@ struct AppEvent { kEventType_Button = 0, kEventType_Timer, - kEventType_Light, - kEventType_Install, - kEventType_ButtonDown, - kEventType_ButtonUp, - - kEventType_None, - kEventType_Reset, - kEventType_ResetPressed, kEventType_ResetWarning, kEventType_ResetCanceled, // Button events @@ -54,13 +46,6 @@ struct AppEvent // Cover Attribute update events kEventType_AttributeChange, - - // Provisioning events - kEventType_ProvisionedStateChanged, - kEventType_ConnectivityStateChanged, - kEventType_BLEConnectionsChanged, - kEventType_WinkOff, - kEventType_WinkOn, }; uint16_t Type; diff --git a/examples/window-app/silabs/include/WindowManager.h b/examples/window-app/silabs/include/WindowManager.h index c9738a3bffade4..bed448d5895c61 100644 --- a/examples/window-app/silabs/include/WindowManager.h +++ b/examples/window-app/silabs/include/WindowManager.h @@ -116,7 +116,7 @@ class WindowManager void PostAttributeChange(chip::EndpointId endpoint, chip::AttributeId attributeId); static void ButtonEventHandler(uint8_t button, uint8_t btnAction); - void UpdateLEDs(); + void UpdateLED(); void UpdateLCD(); static void GeneralEventHandler(AppEvent * aEvent); @@ -130,7 +130,6 @@ class WindowManager static void OnLongPressTimeout(Timer & timer); Timer * mLongPressTimer = nullptr; - bool isWinking = false; bool mTiltMode = false; bool mUpPressed = false; bool mDownPressed = false; diff --git a/examples/window-app/silabs/src/AppTask.cpp b/examples/window-app/silabs/src/AppTask.cpp index 92c80531080acb..8021688a892385 100644 --- a/examples/window-app/silabs/src/AppTask.cpp +++ b/examples/window-app/silabs/src/AppTask.cpp @@ -126,7 +126,7 @@ void AppTask::AppTaskMain(void * pvParameter) SILABS_LOG("App Task started"); - WindowManager::sWindow.UpdateLEDs(); + WindowManager::sWindow.UpdateLED(); WindowManager::sWindow.UpdateLCD(); while (true) diff --git a/examples/window-app/silabs/src/WindowManager.cpp b/examples/window-app/silabs/src/WindowManager.cpp index c7f4c8b81e1cba..c69b45e6cc59bf 100644 --- a/examples/window-app/silabs/src/WindowManager.cpp +++ b/examples/window-app/silabs/src/WindowManager.cpp @@ -53,7 +53,6 @@ using namespace chip::app::Clusters::WindowCovering; using namespace chip; using namespace ::chip::DeviceLayer; using namespace ::chip::DeviceLayer::Silabs; -#define APP_STATE_LED 0 #define APP_ACTION_LED 1 #ifdef DIC_ENABLE @@ -78,12 +77,6 @@ AppEvent CreateNewEvent(AppEvent::AppEventTypes type) return aEvent; } -inline void OnTriggerEffectCompleted(chip::System::Layer * systemLayer, void * appState) -{ - AppEvent event = CreateNewEvent(AppEvent::kEventType_WinkOff); - AppTask::GetAppTask().PostEvent(&event); -} - void WindowManager::Timer::Start() { if (xTimerIsTimerActive(mHandler)) @@ -156,7 +149,7 @@ void WindowManager::DispatchEventAttributeChange(chip::EndpointId endpoint, chip opStatus = OperationalStatusGet(endpoint); OperationalStatusPrint(opStatus); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); - UpdateLEDs(); + UpdateLED(); break; /* RW Mode */ case Attributes::Mode::Id: @@ -183,7 +176,7 @@ void WindowManager::DispatchEventAttributeChange(chip::EndpointId endpoint, chip /* ============= Positions for Position Aware ============= */ case Attributes::CurrentPositionLiftPercent100ths::Id: case Attributes::CurrentPositionTiltPercent100ths::Id: - UpdateLEDs(); + UpdateLED(); UpdateLCD(); break; default: @@ -211,13 +204,7 @@ void WindowManager::HandleLongPress() else if (mUpPressed) { mUpSuppressed = true; - if (mResetWarning) - { - // Double long press button up: Reset now, you were warned! - event.Type = AppEvent::kEventType_Reset; - AppTask::GetAppTask().PostEvent(&event); - } - else + if (!mResetWarning) { // Long press button up: Reset warning! event.Type = AppEvent::kEventType_ResetWarning; @@ -645,16 +632,18 @@ void WindowManager::PostAttributeChange(chip::EndpointId endpoint, chip::Attribu AppTask::GetAppTask().PostEvent(&event); } -void WindowManager::UpdateLEDs() +void WindowManager::UpdateLED() { Cover & cover = GetCover(); if (mResetWarning) { + SILABS_LOG("---- UPDATE ACTION LED mResetWarning") mActionLED.Set(false); mActionLED.Blink(500); } else { + SILABS_LOG("******* UPDATE ACTION LED NORMAL") // Action LED NPercent100ths current; LimitStatus liftLimit = LimitStatus::Intermediate; @@ -671,22 +660,20 @@ void WindowManager::UpdateLEDs() if (OperationalState::Stall != cover.mLiftOpState) { - mActionLED.Blink(100); } else if (LimitStatus::IsUpOrOpen == liftLimit) { - + SILABS_LOG("---- UPDATE ACTION LED IsUpOrOpen") mActionLED.Set(true); } else if (LimitStatus::IsDownOrClose == liftLimit) { - + SILABS_LOG("---- UPDATE ACTION LED IsDownOrClose") mActionLED.Set(false); } else { - mActionLED.Blink(1000); } } @@ -742,24 +729,14 @@ void WindowManager::GeneralEventHandler(AppEvent * aEvent) { case AppEvent::kEventType_ResetWarning: window->mResetWarning = true; - if (window->mLongPressTimer) - { - window->mLongPressTimer->Start(); - } - SILABS_LOG("Factory Reset Triggered. Release button within %ums to cancel.", LONG_PRESS_TIMEOUT); - // Turn off all LEDs before starting blink to make sure blink is - // co-ordinated. - window->UpdateLEDs(); + AppTask::GetAppTask().StartFactoryResetSequence(); + window->UpdateLED(); break; case AppEvent::kEventType_ResetCanceled: window->mResetWarning = false; - SILABS_LOG("Factory Reset has been Canceled"); - window->UpdateLEDs(); - break; - - case AppEvent::kEventType_Reset: - AppTask::GetAppTask().ScheduleFactoryReset(); + AppTask::GetAppTask().CancelFactoryResetSequence(); + window->UpdateLED(); break; case AppEvent::kEventType_UpPressed: @@ -833,26 +810,11 @@ void WindowManager::GeneralEventHandler(AppEvent * aEvent) window->GetCover().UpdateTargetPosition(OperationalState::MovingDownOrClose, window->mTiltMode); } break; + case AppEvent::kEventType_AttributeChange: window->DispatchEventAttributeChange(aEvent->mEndpoint, aEvent->mAttributeId); break; - case AppEvent::kEventType_ProvisionedStateChanged: - window->UpdateLEDs(); - window->UpdateLCD(); - break; - - case AppEvent::kEventType_WinkOn: - case AppEvent::kEventType_WinkOff: - window->isWinking = (AppEvent::kEventType_WinkOn == aEvent->Type); - window->UpdateLEDs(); - break; - - case AppEvent::kEventType_ConnectivityStateChanged: - case AppEvent::kEventType_BLEConnectionsChanged: - window->UpdateLEDs(); - break; - #ifdef DISPLAY_ENABLED case AppEvent::kEventType_CoverTypeChange: window->UpdateLCD();