From 3748480ff629243b49b8eb745d0b922ab9fb39f9 Mon Sep 17 00:00:00 2001 From: EricZijian_Siter Date: Sat, 15 Jul 2023 16:43:46 +0800 Subject: [PATCH] Update Smoke CO Alarm Implementation (#27955) * Add event field to Smoke CO Alarm implementation * Fix INVALID_COMMAND * Simplify the switch-case --------- Co-authored-by: Hare --- .../silabs/src/SmokeCoAlarmManager.cpp | 141 ++++++------------ .../smoke-co-alarm-server.cpp | 10 +- 2 files changed, 50 insertions(+), 101 deletions(-) diff --git a/examples/smoke-co-alarm-app/silabs/src/SmokeCoAlarmManager.cpp b/examples/smoke-co-alarm-app/silabs/src/SmokeCoAlarmManager.cpp index d6ee7535a036fb..77919b896d5126 100644 --- a/examples/smoke-co-alarm-app/silabs/src/SmokeCoAlarmManager.cpp +++ b/examples/smoke-co-alarm-app/silabs/src/SmokeCoAlarmManager.cpp @@ -210,183 +210,132 @@ bool SmokeCoAlarmManager::ManualSelfTesting() bool SmokeCoAlarmManager::OnEventTriggerHandle(uint64_t eventTrigger) { - bool success = false; + bool isValidCommand = true; switch (eventTrigger) { case kTriggeredEvent_WarningSmokeAlarm: - success = SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kWarning); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kWarning), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, true); break; case kTriggeredEvent_CriticalSmokeAlarm: - success = SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kCritical); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kCritical), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, true); break; case kTriggeredEvent_SmokeAlarmClear: - success = SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetSmokeState(1, AlarmStateEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kSmokeAlarm, false); break; case kTriggeredEvent_WarningCOAlarm: - success = SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kWarning); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kWarning), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, true); break; case kTriggeredEvent_CriticalCOAlarm: - success = SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kCritical); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kCritical), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, true); break; case kTriggeredEvent_COAlarmClear: - success = SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetCOState(1, AlarmStateEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kCOAlarm, false); break; case kTriggeredEvent_WarningBatteryAlert: - success = SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kWarning); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kWarning), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, true); break; case kTriggeredEvent_CriticalBatteryAlert: - success = SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kCritical); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kCritical), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, true); break; case kTriggeredEvent_BatteryAlertClear: - success = SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetBatteryAlert(1, AlarmStateEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kBatteryAlert, false); break; case kTriggeredEvent_HardwareFaultAlert: - success = SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, true); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, true), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, true); break; case kTriggeredEvent_HardwareFaultAlertClear: - success = SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, false); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetHardwareFaultAlert(1, false), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kHardwareFault, false); break; case kTriggeredEvent_EndofServiceAlert: - success = SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kExpired); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kExpired), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, true); break; case kTriggeredEvent_EndofServiceAlertClear: - success = SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetEndOfServiceAlert(1, EndOfServiceEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kEndOfService, false); break; case kTriggeredEvent_DeviceMute: - success = SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kMuted); + SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kMuted); break; case kTriggeredEvent_DeviceMuteClear: - success = SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kNotMuted); + SmokeCoAlarmServer::Instance().SetDeviceMuted(1, MuteStateEnum::kNotMuted); break; case kTriggeredEvent_InterconnectSmokeAlarm: - success = SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kWarning); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kWarning), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, true); break; case kTriggeredEvent_InterconnectSmokeAlarmClear: - success = SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetInterconnectSmokeAlarm(1, AlarmStateEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectSmoke, false); break; case kTriggeredEvent_InterconnectCOAlarm: - success = SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kWarning); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, true); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kWarning), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, true); break; case kTriggeredEvent_InterconnectCOAlarmClear: - success = SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kNormal); - if (success) - { - success = AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, false); - } + VerifyOrReturnValue(SmokeCoAlarmServer::Instance().SetInterconnectCOAlarm(1, AlarmStateEnum::kNormal), true); + AlarmMgr().SetExpressedState(1, ExpressedStateEnum::kInterconnectCO, false); break; case kTriggeredEvent_ContaminationStateHigh: - success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kWarning); + SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kWarning); break; case kTriggeredEvent_ContaminationStateLow: - success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kLow); + SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kLow); break; case kTriggeredEvent_ContaminationStateClear: - success = SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kNormal); + SmokeCoAlarmServer::Instance().SetContaminationState(1, ContaminationStateEnum::kNormal); break; case kTriggeredEvent_SensitivityLevelHigh: - success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kHigh); + SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kHigh); break; case kTriggeredEvent_SensitivityLevelLow: - success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kLow); + SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kLow); break; case kTriggeredEvent_SensitivityLevelClear: - success = SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kStandard); + SmokeCoAlarmServer::Instance().SetSensitivityLevel(1, SensitivityEnum::kStandard); break; default: - break; + isValidCommand = false; } - return success; + return isValidCommand; } bool SmokeCoAlarmManager::SetExpressedState(EndpointId endpointId, ExpressedStateEnum expressedState, bool isSet) diff --git a/src/app/clusters/smoke-co-alarm-server/smoke-co-alarm-server.cpp b/src/app/clusters/smoke-co-alarm-server/smoke-co-alarm-server.cpp index 403955d33e41d5..aa1d6fe59e0b28 100644 --- a/src/app/clusters/smoke-co-alarm-server/smoke-co-alarm-server.cpp +++ b/src/app/clusters/smoke-co-alarm-server/smoke-co-alarm-server.cpp @@ -83,7 +83,7 @@ bool SmokeCoAlarmServer::SetSmokeState(EndpointId endpointId, AlarmStateEnum new if (success && (newSmokeState == AlarmStateEnum::kWarning || newSmokeState == AlarmStateEnum::kCritical)) { - Events::SmokeAlarm::Type event{}; + Events::SmokeAlarm::Type event{ newSmokeState }; SendEvent(endpointId, event); } @@ -107,7 +107,7 @@ bool SmokeCoAlarmServer::SetCOState(EndpointId endpointId, AlarmStateEnum newCOS if (success && (newCOState == AlarmStateEnum::kWarning || newCOState == AlarmStateEnum::kCritical)) { - Events::COAlarm::Type event{}; + Events::COAlarm::Type event{ newCOState }; SendEvent(endpointId, event); } @@ -131,7 +131,7 @@ bool SmokeCoAlarmServer::SetBatteryAlert(EndpointId endpointId, AlarmStateEnum n if (success && (newBatteryAlert == AlarmStateEnum::kWarning || newBatteryAlert == AlarmStateEnum::kCritical)) { - Events::LowBattery::Type event{}; + Events::LowBattery::Type event{ newBatteryAlert }; SendEvent(endpointId, event); } @@ -297,7 +297,7 @@ bool SmokeCoAlarmServer::SetInterconnectSmokeAlarm(EndpointId endpointId, AlarmS if (success && (newInterconnectSmokeAlarm == AlarmStateEnum::kWarning || newInterconnectSmokeAlarm == AlarmStateEnum::kCritical)) { - Events::InterconnectSmokeAlarm::Type event{}; + Events::InterconnectSmokeAlarm::Type event{ newInterconnectSmokeAlarm }; SendEvent(endpointId, event); } @@ -323,7 +323,7 @@ bool SmokeCoAlarmServer::SetInterconnectCOAlarm(EndpointId endpointId, AlarmStat if (success && (newInterconnectCOAlarm == AlarmStateEnum::kWarning || newInterconnectCOAlarm == AlarmStateEnum::kCritical)) { - Events::InterconnectCOAlarm::Type event{}; + Events::InterconnectCOAlarm::Type event{ newInterconnectCOAlarm }; SendEvent(endpointId, event); }