|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "ButtonEventsSimulator.h" |
| 20 | + |
| 21 | +#include <utility> |
| 22 | +#include <functional> |
| 23 | +#include <inttypes.h> |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 27 | +#include <app-common/zap-generated/cluster-objects.h> |
| 28 | +#include <app/EventLogging.h> |
| 29 | +#include <lib/core/CHIPError.h> |
| 30 | +#include <lib/core/DataModelTypes.h> |
| 31 | +#include <lib/support/CodeUtils.h> |
| 32 | +#include <lib/support/logging/CHIPLogging.h> |
| 33 | +#include <platform/CHIPDeviceLayer.h> |
| 34 | +#include <system/SystemLayer.h> |
| 35 | +#include <system/SystemClock.h> |
| 36 | + |
| 37 | +namespace chip { |
| 38 | +namespace app { |
| 39 | + |
| 40 | +namespace { |
| 41 | + |
| 42 | +void SetButtonPosition(EndpointId endpointId, uint8_t position) |
| 43 | +{ |
| 44 | + Clusters::Switch::Attributes::CurrentPosition::Set(endpointId, position); |
| 45 | +} |
| 46 | + |
| 47 | +void EmitInitialPress(EndpointId endpointId, uint8_t newPosition) |
| 48 | +{ |
| 49 | + Clusters::Switch::Events::InitialPress::Type event{newPosition}; |
| 50 | + EventNumber eventNumber = 0; |
| 51 | + |
| 52 | + CHIP_ERROR err = LogEvent(event, endpointId, eventNumber); |
| 53 | + if (err != CHIP_NO_ERROR) |
| 54 | + { |
| 55 | + ChipLogError(NotSpecified, "Failed to log InitialPress event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + ChipLogProgress(NotSpecified, "Logged InitialPress(%" PRIu8 ") with ID %" PRIu64 " on Endpoint %" PRIu16, |
| 60 | + newPosition, eventNumber, endpointId); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void EmitLongPress(EndpointId endpointId, uint8_t newPosition) |
| 65 | +{ |
| 66 | + Clusters::Switch::Events::LongPress::Type event{newPosition}; |
| 67 | + EventNumber eventNumber = 0; |
| 68 | + |
| 69 | + CHIP_ERROR err = LogEvent(event, endpointId, eventNumber); |
| 70 | + if (err != CHIP_NO_ERROR) |
| 71 | + { |
| 72 | + ChipLogError(NotSpecified, "Failed to log LongPress event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + ChipLogProgress(NotSpecified, "Logged LongPress(%" PRIu8 ") with ID %" PRIu64 " on Endpoint %" PRIu16, |
| 77 | + newPosition, eventNumber, endpointId); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void EmitLongRelease(EndpointId endpointId, uint8_t previousPosition) |
| 82 | +{ |
| 83 | + Clusters::Switch::Events::LongRelease::Type event{}; |
| 84 | + event.previousPosition = previousPosition; |
| 85 | + EventNumber eventNumber = 0; |
| 86 | + |
| 87 | + CHIP_ERROR err = LogEvent(event, endpointId, eventNumber); |
| 88 | + if (err != CHIP_NO_ERROR) |
| 89 | + { |
| 90 | + ChipLogError(NotSpecified, "Failed to log LongRelease event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + ChipLogProgress(NotSpecified, "Logged LongRelease with ID %" PRIu64 " on Endpoint %" PRIu16, |
| 95 | + eventNumber, endpointId); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void EmitMultiPressComplete(EndpointId endpointId, uint8_t previousPosition, uint8_t count) |
| 100 | +{ |
| 101 | + Clusters::Switch::Events::MultiPressComplete::Type event{}; |
| 102 | + event.previousPosition = previousPosition; |
| 103 | + event.totalNumberOfPressesCounted = count; |
| 104 | + EventNumber eventNumber = 0; |
| 105 | + |
| 106 | + CHIP_ERROR err = LogEvent(event, endpointId, eventNumber); |
| 107 | + if (err != CHIP_NO_ERROR) |
| 108 | + { |
| 109 | + ChipLogError(NotSpecified, "Failed to log MultiPressComplete event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + ChipLogProgress(NotSpecified, "Logged MultiPressComplete(count=%" PRIu8 ") with ID %" PRIu64 " on Endpoint %" PRIu16, |
| 114 | + count, eventNumber, endpointId); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace |
| 119 | + |
| 120 | +void ButtonEventsSimulator::OnTimerDone(System::Layer * layer, void * appState) |
| 121 | +{ |
| 122 | + ButtonEventsSimulator *that = reinterpret_cast<ButtonEventsSimulator*>(appState); |
| 123 | + that->Next(); |
| 124 | +} |
| 125 | + |
| 126 | +bool ButtonEventsSimulator::Execute(DoneCallback && doneCallback) |
| 127 | +{ |
| 128 | + VerifyOrReturnValue(mIdleButtonId != mPressedButtonId, false); |
| 129 | + |
| 130 | + switch(mMode) |
| 131 | + { |
| 132 | + case Mode::kModeLongPress: |
| 133 | + VerifyOrReturnValue(mLongPressDurationMillis > mLongPressDelayMillis, false); |
| 134 | + SetState(State::kEmitStartOfLongPress); |
| 135 | + break; |
| 136 | + case Mode::kModeMultiPress: |
| 137 | + VerifyOrReturnValue(mMultiPressPressedTimeMillis.count() > 0, false); |
| 138 | + VerifyOrReturnValue(mMultiPressReleasedTimeMillis.count() > 0, false); |
| 139 | + VerifyOrReturnValue(mMultiPressNumPresses > 0, false); |
| 140 | + SetState(State::kEmitStartOfMultiPress); |
| 141 | + break; |
| 142 | + default: |
| 143 | + return false; |
| 144 | + } |
| 145 | + mDoneCallback = std::move(doneCallback); |
| 146 | + Next(); |
| 147 | + return true; |
| 148 | +} |
| 149 | + |
| 150 | +void ButtonEventsSimulator::SetState(ButtonEventsSimulator::State newState) |
| 151 | +{ |
| 152 | + ButtonEventsSimulator::State oldState = mState; |
| 153 | + if (oldState != newState) |
| 154 | + { |
| 155 | + ChipLogProgress(NotSpecified, "ButtonEventsSimulator state change %u -> %u", static_cast<unsigned>(oldState), static_cast<unsigned>(newState)); |
| 156 | + } |
| 157 | + |
| 158 | + mState = newState; |
| 159 | +} |
| 160 | + |
| 161 | +void ButtonEventsSimulator::StartTimer(System::Clock::Timeout duration) |
| 162 | +{ |
| 163 | + chip::DeviceLayer::SystemLayer().StartTimer(duration, &ButtonEventsSimulator::OnTimerDone, this); |
| 164 | +} |
| 165 | + |
| 166 | +void ButtonEventsSimulator::Next() |
| 167 | +{ |
| 168 | + switch (mState) { |
| 169 | + case ButtonEventsSimulator::State::kIdle: { |
| 170 | + ChipLogError(NotSpecified, "Found idle state where not expected!"); |
| 171 | + break; |
| 172 | + } |
| 173 | + case ButtonEventsSimulator::State::kEmitStartOfLongPress: { |
| 174 | + SetButtonPosition(mEndpointId, mPressedButtonId); |
| 175 | + EmitInitialPress(mEndpointId, mPressedButtonId); |
| 176 | + SetState(ButtonEventsSimulator::State::kEmitLongPress); |
| 177 | + StartTimer(mLongPressDelayMillis); |
| 178 | + break; |
| 179 | + } |
| 180 | + case ButtonEventsSimulator::State::kEmitLongPress: { |
| 181 | + EmitLongPress(mEndpointId, mPressedButtonId); |
| 182 | + SetState(ButtonEventsSimulator::State::kEmitLongRelease); |
| 183 | + StartTimer(mLongPressDurationMillis - mLongPressDelayMillis); |
| 184 | + break; |
| 185 | + } |
| 186 | + case ButtonEventsSimulator::State::kEmitLongRelease: { |
| 187 | + SetButtonPosition(mEndpointId, mIdleButtonId); |
| 188 | + EmitLongRelease(mEndpointId, mPressedButtonId); |
| 189 | + SetState(ButtonEventsSimulator::State::kIdle); |
| 190 | + mDoneCallback(); |
| 191 | + break; |
| 192 | + } |
| 193 | + case ButtonEventsSimulator::State::kEmitStartOfMultiPress: { |
| 194 | + EmitInitialPress(mEndpointId, mPressedButtonId); |
| 195 | + StartTimer(mMultiPressNumPresses * (mMultiPressPressedTimeMillis + mMultiPressReleasedTimeMillis)); |
| 196 | + SetState(ButtonEventsSimulator::State::kEmitEndOfMultiPress); |
| 197 | + break; |
| 198 | + } |
| 199 | + case ButtonEventsSimulator::State::kEmitEndOfMultiPress: { |
| 200 | + EmitMultiPressComplete(mEndpointId, mPressedButtonId, mMultiPressNumPresses); |
| 201 | + SetState(ButtonEventsSimulator::State::kIdle); |
| 202 | + mDoneCallback(); |
| 203 | + break; |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +} // namespace app |
| 209 | +} // namespace chip |
0 commit comments