Skip to content

Commit

Permalink
[SMCO] Add timer for test
Browse files Browse the repository at this point in the history
  • Loading branch information
hare-siterwell committed Jul 5, 2023
1 parent b2c2761 commit fd80945
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 70 deletions.
44 changes: 37 additions & 7 deletions examples/smoke-co-alarm-app/silabs/include/SmokeCoAlarmManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,51 @@ class SmokeCoAlarmManager
public:
using ExpressedStateEnum = chip::app::Clusters::SmokeCoAlarm::ExpressedStateEnum;

enum TriggeredEvent_t
{
kTriggeredEvent_SmokeAlarm = 0xffffffff00000090,
kTriggeredEvent_SmokeAlarmClear = 0xffffffff000000a0,
kTriggeredEvent_COAlarm = 0xffffffff00000091,
kTriggeredEvent_COAlarmClear = 0xffffffff000000a1,
kTriggeredEvent_BatteryAlert = 0xffffffff00000095,
kTriggeredEvent_BatteryAlertClear = 0xffffffff000000a5,
kTriggeredEvent_HardwareFaultAlert = 0xffffffff00000093,
kTriggeredEvent_HardwareFaultAlertClear = 0xffffffff000000a3,
kTriggeredEvent_EndofServiceAlert = 0xffffffff0000009a,
kTriggeredEvent_EndofServiceAlertClear = 0xffffffff000000aa,
kTriggeredEvent_DeviceMute = 0xffffffff0000009b,
kTriggeredEvent_DeviceMuteClear = 0xffffffff000000ab,
kTriggeredEvent_InterconnectSmokeAlarm = 0xffffffff00000092,
kTriggeredEvent_InterconnectSmokeAlarmClear = 0xffffffff000000a2,
kTriggeredEvent_InterconnectCOAlarm = 0xffffffff00000094,
kTriggeredEvent_InterconnectCOAlarmClear = 0xffffffff000000a4,
kTriggeredEvent_ContaminationStateHigh = 0xffffffff00000096,
kTriggeredEvent_ContaminationStateLow = 0xffffffff00000097,
kTriggeredEvent_ContaminationStateClear = 0xffffffff000000a6,
kTriggeredEvent_SensitivityLevelHigh = 0xffffffff00000098,
kTriggeredEvent_SensitivityLevelLow = 0xffffffff00000099,
kTriggeredEvent_SensitivityLevelClear = 0xffffffff000000a8,
} TriggeredEvent;

CHIP_ERROR Init();

/**
* @brief Execute the self-test process and attribute changes
*
*/
bool StartSelfTesting();
bool OnSelfTesting();

/**
* @brief Execute the self-test process manually
*
*/
bool ManualSelfTesting();

/**
* @brief Execute the HandleEventTrigger process
*
*/
bool StartHandleEventTrigger(uint64_t eventTrigger);
bool OnEventTriggerHandle(uint64_t eventTrigger);

/**
* @brief Updates the expressed state with new value
Expand All @@ -72,16 +104,14 @@ class SmokeCoAlarmManager
*/
int mExpressedStateMask = 1;

bool mSelfTesting;
bool mHandleEventTrigger;
uint64_t mEventTrigger;
bool mEndSelfTesting;

void CancelTimer(void);
void StartTimer(uint32_t aTimeoutMs);

static void TimerEventHandler(TimerHandle_t xTimer);
static void SelfTestingTimerEventHandler(AppEvent * aEvent);
static void EventTriggerTimerEventHandler(AppEvent * aEvent);
static void SelfTestingEventHandler(AppEvent * aEvent);
static void EndSelfTestingEventHandler(AppEvent * aEvent);

static SmokeCoAlarmManager sAlarm;
};
Expand Down
6 changes: 5 additions & 1 deletion examples/smoke-co-alarm-app/silabs/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ void AppTask::AppTaskMain(void * pvParameter)

void AppTask::ButtonActionEventHandler(AppEvent * aEvent)
{
SILABS_LOG("Button pressed!");
bool success = AlarmMgr().ManualSelfTesting();
if (!success)
{
SILABS_LOG("Manual self-test failed");
}
}

void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
Expand Down
140 changes: 80 additions & 60 deletions examples/smoke-co-alarm-app/silabs/src/SmokeCoAlarmManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ TimerHandle_t sAlarmTimer;
CHIP_ERROR SmokeCoAlarmManager::Init()
{
mExpressedStatePriority = {
ExpressedStateEnum::kEndOfService, ExpressedStateEnum::kSmokeAlarm, ExpressedStateEnum::kCOAlarm,
ExpressedStateEnum::kTesting, ExpressedStateEnum::kInterconnectSmoke, ExpressedStateEnum::kInterconnectCO,
ExpressedStateEnum::kTesting, ExpressedStateEnum::kEndOfService, ExpressedStateEnum::kSmokeAlarm,
ExpressedStateEnum::kCOAlarm, ExpressedStateEnum::kInterconnectSmoke, ExpressedStateEnum::kInterconnectCO,
ExpressedStateEnum::kHardwareFault, ExpressedStateEnum::kBatteryAlert, ExpressedStateEnum::kNormal
};

Expand Down Expand Up @@ -98,6 +98,8 @@ CHIP_ERROR SmokeCoAlarmManager::Init()
}
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

mEndSelfTesting = false;

return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -139,210 +141,228 @@ void SmokeCoAlarmManager::TimerEventHandler(TimerHandle_t xTimer)
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = alarm;
if (alarm->mSelfTesting)
{
event.Handler = SelfTestingTimerEventHandler;
}
else if (alarm->mHandleEventTrigger)
if (alarm->mEndSelfTesting)
{
event.Handler = EventTriggerTimerEventHandler;
event.Handler = EndSelfTestingEventHandler;
}
AppTask::GetAppTask().PostEvent(&event);
}

void SmokeCoAlarmManager::SelfTestingTimerEventHandler(AppEvent * aEvent)
void SmokeCoAlarmManager::SelfTestingEventHandler(AppEvent * aEvent)
{
chip::DeviceLayer::PlatformMgr().LockChipStack();

bool success = SmokeCoAlarmServer::Instance().SetTestInProgress(1, true);
if (success)
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

if (!success)
{
SILABS_LOG("Start self-testing!");
SILABS_LOG("Start self-testing failed");
return;
}

SILABS_LOG("Start self-testing!");

AlarmMgr().mEndSelfTesting = true;
AlarmMgr().StartTimer(10000); // Self-test simulation in progress
}

void SmokeCoAlarmManager::EndSelfTestingEventHandler(AppEvent * aEvent)
{
AlarmMgr().mEndSelfTesting = false;

chip::DeviceLayer::PlatformMgr().LockChipStack();
bool success = SmokeCoAlarmServer::Instance().SetTestInProgress(1, false);
if (success)
{
AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kTesting, false);
SILABS_LOG("End self-testing!");
}
chip::DeviceLayer::PlatformMgr().UnlockChipStack();
}

SmokeCoAlarmServer::Instance().SetTestInProgress(1, false);
AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kTesting, false);
bool SmokeCoAlarmManager::OnSelfTesting()
{
AppEvent event;
event.Handler = SelfTestingEventHandler;
AppTask::GetAppTask().PostEvent(&event);

chip::DeviceLayer::PlatformMgr().UnlockChipStack();
return true;
}

void SmokeCoAlarmManager::EventTriggerTimerEventHandler(AppEvent * aEvent)
bool SmokeCoAlarmManager::ManualSelfTesting()
{
SmokeCoAlarmManager * alarm = static_cast<SmokeCoAlarmManager *>(aEvent->TimerEvent.Context);
bool success = true;
bool success = false;

chip::DeviceLayer::PlatformMgr().LockChipStack();
if ((mExpressedStateMask & 0b110010110) == 0)
{
chip::DeviceLayer::PlatformMgr().LockChipStack();
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kTesting, true);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

if (success)
{
success = OnSelfTesting();
}
}

return success;
}

bool SmokeCoAlarmManager::OnEventTriggerHandle(uint64_t eventTrigger)
{
bool success = false;

switch (alarm->mEventTrigger)
switch (eventTrigger)
{
case 0xffffffff00000090:
case kTriggeredEvent_SmokeAlarm:
success = SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kWarning);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, true);
}
break;

case 0xffffffff000000a0:
case kTriggeredEvent_SmokeAlarmClear:
success = SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, false);
}
break;

case 0xffffffff00000091:
case kTriggeredEvent_COAlarm:
success = SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kWarning);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, true);
}
break;

case 0xffffffff000000a1:
case kTriggeredEvent_COAlarmClear:
success = SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, false);
}
break;

case 0xffffffff00000095:
case kTriggeredEvent_BatteryAlert:
success = SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kWarning);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, true);
}
break;

case 0xffffffff000000a5:
case kTriggeredEvent_BatteryAlertClear:
success = SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, false);
}
break;

case 0xffffffff00000093:
case kTriggeredEvent_HardwareFaultAlert:
success = SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, true);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, true);
}
break;

case 0xffffffff000000a3:
case kTriggeredEvent_HardwareFaultAlertClear:
success = SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, false);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, false);
}
break;

case 0xffffffff0000009a:
case kTriggeredEvent_EndofServiceAlert:
success = SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kExpired);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, true);
}
break;

case 0xffffffff000000aa:
case kTriggeredEvent_EndofServiceAlertClear:
success = SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, false);
}
break;

case 0xffffffff0000009b:
case kTriggeredEvent_DeviceMute:
success = SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kMuted);
break;

case 0xffffffff000000ab:
case kTriggeredEvent_DeviceMuteClear:
success = SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kNotMuted);
break;

case 0xffffffff00000092:
case kTriggeredEvent_InterconnectSmokeAlarm:
success = SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kWarning);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, true);
}
break;

case 0xffffffff000000a2:
case kTriggeredEvent_InterconnectSmokeAlarmClear:
success = SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, false);
}
break;

case 0xffffffff00000094:
case kTriggeredEvent_InterconnectCOAlarm:
success = SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kWarning);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, true);
}
break;

case 0xffffffff000000a4:
case kTriggeredEvent_InterconnectCOAlarmClear:
success = SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kNormal);
if (success)
{
success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, false);
}
break;

case 0xffffffff00000096:
case kTriggeredEvent_ContaminationStateHigh:
success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kWarning);
break;

case 0xffffffff00000097:
case kTriggeredEvent_ContaminationStateLow:
success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kLow);
break;

case 0xffffffff000000a6:
case kTriggeredEvent_ContaminationStateClear:
success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kNormal);
break;

case 0xffffffff00000098:
case kTriggeredEvent_SensitivityLevelHigh:
success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kHigh);
break;

case 0xffffffff00000099:
case kTriggeredEvent_SensitivityLevelLow:
success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kLow);
break;

case 0xffffffff000000a8:
case kTriggeredEvent_SensitivityLevelClear:
success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kStandard);
break;

default:
break;
}

chip::DeviceLayer::PlatformMgr().UnlockChipStack();
}

bool SmokeCoAlarmManager::StartSelfTesting()
{
AlarmMgr().mSelfTesting = true;
AlarmMgr().StartTimer(10);

return true;
}

bool SmokeCoAlarmManager::StartHandleEventTrigger(uint64_t eventTrigger)
{
AlarmMgr().mHandleEventTrigger = true;
AlarmMgr().mEventTrigger = eventTrigger;
AlarmMgr().StartTimer(1000);

return true;
return success;
}

bool SmokeCoAlarmManager::SetExpressedState(EndpointId endpointId, ExpressedStateEnum expressedState, bool isSet)
Expand Down
Loading

0 comments on commit fd80945

Please sign in to comment.