Skip to content

Commit

Permalink
Reuse BaseApplication factory reset sequence. more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinez-silabs committed Nov 28, 2023
1 parent 553abd6 commit 02ebc69
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 138 deletions.
114 changes: 55 additions & 59 deletions examples/platform/silabs/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand All @@ -469,20 +444,22 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(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
Expand Down Expand Up @@ -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");
}
}
}

Expand All @@ -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)
Expand All @@ -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()
Expand Down
14 changes: 4 additions & 10 deletions examples/platform/silabs/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BaseApplication
BaseApplication() = default;
virtual ~BaseApplication(){};
static bool sIsProvisioned;
static bool mIsFactoryResetTriggered;

/**
* @brief Create AppTask task and Event Queue
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down
15 changes: 0 additions & 15 deletions examples/window-app/silabs/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions examples/window-app/silabs/include/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/window-app/silabs/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 02ebc69

Please sign in to comment.