|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include <chef-operational-state-delegate-impl.h> |
| 18 | +#include <platform/CHIPDeviceLayer.h> |
| 19 | + |
| 20 | +using namespace chip; |
| 21 | +using namespace chip::app; |
| 22 | +using namespace chip::app::Clusters; |
| 23 | +using namespace chip::app::Clusters::OperationalState; |
| 24 | +using namespace chip::app::Clusters::RvcOperationalState; |
| 25 | + |
| 26 | +static void onOperationalStateTimerTick(System::Layer * systemLayer, void * data); |
| 27 | + |
| 28 | +DataModel::Nullable<uint32_t> GenericOperationalStateDelegateImpl::GetCountdownTime() |
| 29 | +{ |
| 30 | + if (mCountDownTime.IsNull()) |
| 31 | + return DataModel::NullNullable; |
| 32 | + |
| 33 | + return DataModel::MakeNullable((uint32_t) (mCountDownTime.Value() - mRunningTime)); |
| 34 | +} |
| 35 | + |
| 36 | +CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) |
| 37 | +{ |
| 38 | + if (index >= mOperationalStateList.size()) |
| 39 | + { |
| 40 | + return CHIP_ERROR_NOT_FOUND; |
| 41 | + } |
| 42 | + operationalState = mOperationalStateList[index]; |
| 43 | + return CHIP_NO_ERROR; |
| 44 | +} |
| 45 | + |
| 46 | +CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) |
| 47 | +{ |
| 48 | + if (index >= mOperationalPhaseList.size()) |
| 49 | + { |
| 50 | + return CHIP_ERROR_NOT_FOUND; |
| 51 | + } |
| 52 | + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); |
| 53 | +} |
| 54 | + |
| 55 | +void GenericOperationalStateDelegateImpl::HandlePauseStateCallback(GenericOperationalError & err) |
| 56 | +{ |
| 57 | + OperationalState::OperationalStateEnum state = |
| 58 | + static_cast<OperationalState::OperationalStateEnum>(GetInstance()->GetCurrentOperationalState()); |
| 59 | + |
| 60 | + if (state == OperationalState::OperationalStateEnum::kStopped || state == OperationalState::OperationalStateEnum::kError) |
| 61 | + { |
| 62 | + err.Set(to_underlying(OperationalState::ErrorStateEnum::kCommandInvalidInState)); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // placeholder implementation |
| 67 | + auto error = GetInstance()->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kPaused)); |
| 68 | + if (error == CHIP_NO_ERROR) |
| 69 | + { |
| 70 | + err.Set(to_underlying(ErrorStateEnum::kNoError)); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation)); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void GenericOperationalStateDelegateImpl::HandleResumeStateCallback(GenericOperationalError & err) |
| 79 | +{ |
| 80 | + OperationalState::OperationalStateEnum state = |
| 81 | + static_cast<OperationalState::OperationalStateEnum>(GetInstance()->GetCurrentOperationalState()); |
| 82 | + |
| 83 | + if (state == OperationalState::OperationalStateEnum::kStopped || state == OperationalState::OperationalStateEnum::kError) |
| 84 | + { |
| 85 | + err.Set(to_underlying(OperationalState::ErrorStateEnum::kCommandInvalidInState)); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // placeholder implementation |
| 90 | + auto error = GetInstance()->SetOperationalState(to_underlying(OperationalStateEnum::kRunning)); |
| 91 | + if (error == CHIP_NO_ERROR) |
| 92 | + { |
| 93 | + err.Set(to_underlying(ErrorStateEnum::kNoError)); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void GenericOperationalStateDelegateImpl::HandleStartStateCallback(GenericOperationalError & err) |
| 102 | +{ |
| 103 | + OperationalState::GenericOperationalError current_err(to_underlying(OperationalState::ErrorStateEnum::kNoError)); |
| 104 | + GetInstance()->GetCurrentOperationalError(current_err); |
| 105 | + |
| 106 | + if (current_err.errorStateID != to_underlying(OperationalState::ErrorStateEnum::kNoError)) |
| 107 | + { |
| 108 | + err.Set(to_underlying(OperationalState::ErrorStateEnum::kUnableToStartOrResume)); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // placeholder implementation |
| 113 | + auto error = GetInstance()->SetOperationalState(to_underlying(OperationalStateEnum::kRunning)); |
| 114 | + if (error == CHIP_NO_ERROR) |
| 115 | + { |
| 116 | + (void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds16(1), onOperationalStateTimerTick, this); |
| 117 | + err.Set(to_underlying(ErrorStateEnum::kNoError)); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +void GenericOperationalStateDelegateImpl::HandleStopStateCallback(GenericOperationalError & err) |
| 126 | +{ |
| 127 | + // placeholder implementation |
| 128 | + auto error = GetInstance()->SetOperationalState(to_underlying(OperationalStateEnum::kStopped)); |
| 129 | + if (error == CHIP_NO_ERROR) |
| 130 | + { |
| 131 | + (void) DeviceLayer::SystemLayer().CancelTimer(onOperationalStateTimerTick, this); |
| 132 | + |
| 133 | + OperationalState::GenericOperationalError current_err(to_underlying(OperationalState::ErrorStateEnum::kNoError)); |
| 134 | + GetInstance()->GetCurrentOperationalError(current_err); |
| 135 | + |
| 136 | + Optional<DataModel::Nullable<uint32_t>> totalTime((DataModel::Nullable<uint32_t>(mRunningTime + mPausedTime))); |
| 137 | + Optional<DataModel::Nullable<uint32_t>> pausedTime((DataModel::Nullable<uint32_t>(mPausedTime))); |
| 138 | + |
| 139 | + GetInstance()->OnOperationCompletionDetected(static_cast<uint8_t>(current_err.errorStateID), totalTime, pausedTime); |
| 140 | + |
| 141 | + mRunningTime = 0; |
| 142 | + mPausedTime = 0; |
| 143 | + err.Set(to_underlying(ErrorStateEnum::kNoError)); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + err.Set(to_underlying(ErrorStateEnum::kUnableToCompleteOperation)); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +static void onOperationalStateTimerTick(System::Layer * systemLayer, void * data) |
| 152 | +{ |
| 153 | + GenericOperationalStateDelegateImpl * delegate = reinterpret_cast<GenericOperationalStateDelegateImpl *>(data); |
| 154 | + |
| 155 | + OperationalState::Instance * instance = OperationalState::GetOperationalStateInstance(); |
| 156 | + OperationalState::OperationalStateEnum state = |
| 157 | + static_cast<OperationalState::OperationalStateEnum>(instance->GetCurrentOperationalState()); |
| 158 | + |
| 159 | + auto countdown_time = delegate->GetCountdownTime(); |
| 160 | + |
| 161 | + if (countdown_time.IsNull() || (!countdown_time.IsNull() && countdown_time.Value() > 0)) |
| 162 | + { |
| 163 | + if (state == OperationalState::OperationalStateEnum::kRunning) |
| 164 | + { |
| 165 | + delegate->mRunningTime++; |
| 166 | + } |
| 167 | + else if (state == OperationalState::OperationalStateEnum::kPaused) |
| 168 | + { |
| 169 | + delegate->mPausedTime++; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (state == OperationalState::OperationalStateEnum::kRunning || state == OperationalState::OperationalStateEnum::kPaused) |
| 174 | + { |
| 175 | + (void) DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds16(1), onOperationalStateTimerTick, delegate); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + (void) DeviceLayer::SystemLayer().CancelTimer(onOperationalStateTimerTick, delegate); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// Init Operational State cluster |
| 184 | + |
| 185 | +static OperationalState::Instance * gOperationalStateInstance = nullptr; |
| 186 | +static OperationalStateDelegate * gOperationalStateDelegate = nullptr; |
| 187 | + |
| 188 | +OperationalState::Instance * OperationalState::GetOperationalStateInstance() |
| 189 | +{ |
| 190 | + return gOperationalStateInstance; |
| 191 | +} |
| 192 | + |
| 193 | +void OperationalState::Shutdown() |
| 194 | +{ |
| 195 | + if (gOperationalStateInstance != nullptr) |
| 196 | + { |
| 197 | + delete gOperationalStateInstance; |
| 198 | + gOperationalStateInstance = nullptr; |
| 199 | + } |
| 200 | + if (gOperationalStateDelegate != nullptr) |
| 201 | + { |
| 202 | + delete gOperationalStateDelegate; |
| 203 | + gOperationalStateDelegate = nullptr; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +void emberAfOperationalStateClusterInitCallback(chip::EndpointId endpointId) |
| 208 | +{ |
| 209 | + VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1. |
| 210 | + VerifyOrDie(gOperationalStateInstance == nullptr && gOperationalStateDelegate == nullptr); |
| 211 | + |
| 212 | + gOperationalStateDelegate = new OperationalStateDelegate; |
| 213 | + EndpointId operationalStateEndpoint = 0x01; |
| 214 | + gOperationalStateInstance = new OperationalState::Instance(gOperationalStateDelegate, operationalStateEndpoint); |
| 215 | + |
| 216 | + gOperationalStateInstance->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kStopped)); |
| 217 | + |
| 218 | + gOperationalStateInstance->Init(); |
| 219 | +} |
0 commit comments