|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2022 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 <app-common/zap-generated/attributes/Accessors.h> |
| 20 | +#include <app/clusters/switch-server/switch-server.h> |
| 21 | +#include <app/server/Server.h> |
| 22 | +#include <app/util/att-storage.h> |
| 23 | +#include <platform/PlatformManager.h> |
| 24 | +#include "stubs.h" |
| 25 | + |
| 26 | +using namespace chip; |
| 27 | +using namespace chip::app; |
| 28 | +using namespace chip::app::Clusters; |
| 29 | +using namespace chip::app::Clusters::Switch; |
| 30 | +using namespace chip::DeviceLayer; |
| 31 | + |
| 32 | +class SwitchEventHandler |
| 33 | +{ |
| 34 | +public: |
| 35 | + SwitchEventHandler(EndpointId endpoint):mEndpointId(endpoint){}; |
| 36 | + |
| 37 | + /** |
| 38 | + * Should be called when the latching switch is moved to a new position. |
| 39 | + */ |
| 40 | + void OnSwitchLatchedHandler(uint8_t newPosition); |
| 41 | + |
| 42 | + /** |
| 43 | + * Should be called when the momentary switch starts to be pressed. |
| 44 | + */ |
| 45 | + void OnSwitchInitialPressedHandler(uint8_t newPosition); |
| 46 | + |
| 47 | + /** |
| 48 | + * Should be called when the momentary switch has been pressed for a "long" time. |
| 49 | + */ |
| 50 | + void OnSwitchLongPressedHandler(uint8_t newPosition); |
| 51 | + |
| 52 | + /** |
| 53 | + * Should be called when the momentary switch has been released. |
| 54 | + */ |
| 55 | + void OnSwitchShortReleasedHandler(uint8_t previousPosition); |
| 56 | + |
| 57 | + /** |
| 58 | + * Should be called when the momentary switch has been released after having been pressed for a long time. |
| 59 | + */ |
| 60 | + void OnSwitchLongReleasedHandler(uint8_t previousPosition); |
| 61 | + |
| 62 | + /** |
| 63 | + * Should be called to indicate how many times the momentary switch has been pressed in a multi-press |
| 64 | + * sequence, during that sequence. |
| 65 | + */ |
| 66 | + void OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count); |
| 67 | + |
| 68 | + /** |
| 69 | + * Should be called to indicate how many times the momentary switch has been pressed in a multi-press |
| 70 | + * sequence, after it has been detected that the sequence has ended. |
| 71 | + */ |
| 72 | + void OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count); |
| 73 | + |
| 74 | +private: |
| 75 | + EndpointId mEndpointId; |
| 76 | +}; |
| 77 | + |
| 78 | + |
| 79 | +void SwitchEventHandler::OnSwitchLatchedHandler(uint8_t newPosition) |
| 80 | +{ |
| 81 | + ChipLogDetail(NotSpecified, "The latching switch is moved to a new position:%d", newPosition); |
| 82 | + |
| 83 | + Clusters::SwitchServer::Instance().OnSwitchLatch(mEndpointId, newPosition); |
| 84 | +} |
| 85 | + |
| 86 | +void SwitchEventHandler::OnSwitchInitialPressedHandler(uint8_t newPosition) |
| 87 | +{ |
| 88 | + ChipLogDetail(NotSpecified, "The new position when the momentary switch starts to be pressed:%d", newPosition); |
| 89 | + |
| 90 | + Clusters::SwitchServer::Instance().OnInitialPress(mEndpointId, newPosition); |
| 91 | +} |
| 92 | + |
| 93 | +void SwitchEventHandler::OnSwitchLongPressedHandler(uint8_t newPosition) |
| 94 | +{ |
| 95 | + ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed for a long time:%d", newPosition); |
| 96 | + |
| 97 | + Clusters::SwitchServer::Instance().OnLongPress(mEndpointId, newPosition); |
| 98 | +} |
| 99 | + |
| 100 | +void SwitchEventHandler::OnSwitchShortReleasedHandler(uint8_t previousPosition) |
| 101 | +{ |
| 102 | + ChipLogDetail(NotSpecified, "The the previous value of the CurrentPosition when the momentary switch has been released:%d", |
| 103 | + previousPosition); |
| 104 | + |
| 105 | + Clusters::SwitchServer::Instance().OnShortRelease(mEndpointId, previousPosition); |
| 106 | +} |
| 107 | + |
| 108 | +void SwitchEventHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition) |
| 109 | +{ |
| 110 | + ChipLogDetail(NotSpecified, |
| 111 | + "The the previous value of the CurrentPosition when the momentary switch has been released after having been " |
| 112 | + "pressed for a long time:%d", |
| 113 | + previousPosition); |
| 114 | + |
| 115 | + Clusters::SwitchServer::Instance().OnLongRelease(mEndpointId, previousPosition); |
| 116 | +} |
| 117 | + |
| 118 | +void SwitchEventHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count) |
| 119 | +{ |
| 120 | + ChipLogDetail(NotSpecified, "The new position when the momentary switch has been pressed in a multi-press sequence:%d", |
| 121 | + newPosition); |
| 122 | + ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count); |
| 123 | + |
| 124 | + Clusters::SwitchServer::Instance().OnMultiPressOngoing(mEndpointId, newPosition, count); |
| 125 | +} |
| 126 | + |
| 127 | +void SwitchEventHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count) |
| 128 | +{ |
| 129 | + ChipLogDetail(NotSpecified, "The previous position when the momentary switch has been pressed in a multi-press sequence:%d", |
| 130 | + previousPosition); |
| 131 | + ChipLogDetail(NotSpecified, "%d times the momentary switch has been pressed", count); |
| 132 | + |
| 133 | + Clusters::SwitchServer::Instance().OnMultiPressComplete(mEndpointId, previousPosition, count); |
| 134 | +} |
| 135 | + |
| 136 | +static std::map<int, SwitchEventHandler *> gSwitchEventHandlers{}; |
| 137 | + |
| 138 | + |
| 139 | +class SwitchAttributeDelegate : public AttributeDelegate |
| 140 | +{ |
| 141 | +public: |
| 142 | + SwitchAttributeDelegate (ClusterId clusterId) : AttributeDelegate(clusterId) {} |
| 143 | + |
| 144 | + void PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) override; |
| 145 | + |
| 146 | +private: |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +SwitchEventHandler * GetSwitchEventHandler(EndpointId endpointId) |
| 151 | +{ |
| 152 | + if (gSwitchEventHandlers.find(endpointId) == gSwitchEventHandlers.end()) { |
| 153 | + return nullptr; |
| 154 | + } |
| 155 | + |
| 156 | + return gSwitchEventHandlers[endpointId]; |
| 157 | +} |
| 158 | + |
| 159 | +void SwitchAttributeDelegate::PostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, uint8_t * value) |
| 160 | +{ |
| 161 | +printf("\033[41m %s, %d \033[0m \n", __func__, __LINE__); |
| 162 | + ChipLogProgress(Zcl, "SwitchAttributeDelegate::PostAttributeChangeCallback Endpoint: %d, Cluster: " ChipLogFormatMEI ", Type: %u, length %u", attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), type, size); |
| 163 | + |
| 164 | + switch (attributePath.mAttributeId) { |
| 165 | + case Attributes::CurrentPosition::Id: { |
| 166 | + SwitchEventHandler *eventHandler = GetSwitchEventHandler(attributePath.mEndpointId); |
| 167 | + uint8_t newPosition = *value; |
| 168 | + |
| 169 | + if (eventHandler) { |
| 170 | + eventHandler->OnSwitchLatchedHandler(newPosition); |
| 171 | + } |
| 172 | + } |
| 173 | + break; |
| 174 | + default: |
| 175 | + break; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +void emberAfSwitchClusterInitCallback(EndpointId endpoint) |
| 180 | +{ |
| 181 | + ChipLogProgress(Zcl, "Chef: emberAfSwitchClusterInitCallback"); |
| 182 | + gSwitchEventHandlers[endpoint] = new SwitchEventHandler(endpoint); |
| 183 | + RegisterApplicationAttributeDelegate(Switch::Id, new SwitchAttributeDelegate(Switch::Id)); |
| 184 | +printf("\033[44m %s, %d, Switch::ID=%u \033[0m \n", __func__, __LINE__, Switch::Id); |
| 185 | +} |
| 186 | + |
| 187 | + |
0 commit comments