|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 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 | + |
| 18 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 19 | +#include <app-common/zap-generated/cluster-objects.h> |
| 20 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 21 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 22 | +#include <app/AttributeAccessInterface.h> |
| 23 | +#include <app/util/attribute-storage.h> |
| 24 | +#include <lib/support/CodeUtils.h> |
| 25 | +#include <lib/support/logging/CHIPLogging.h> |
| 26 | + |
| 27 | +using namespace chip; |
| 28 | +using namespace chip::app; |
| 29 | +using namespace chip::app::Clusters; |
| 30 | +using namespace chip::app::Clusters::FanControl::Attributes; |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +/* |
| 35 | + * TODO: This is a stop-gap solution to allow the existing fan control cluster tests to run after changes to |
| 36 | + * the cluster objects for TE1. This should be removed once #6496 is resolved as it will likley result in a |
| 37 | + * FanControl delegate added to the SDK. |
| 38 | + * |
| 39 | + * FYI... The previous implementation of the FanControl cluster set the speedCurrent/percentCurrent when it received |
| 40 | + * speedSetting/percentSetting. The new implementation of the FanControl cluster does not do this as this should |
| 41 | + * really be done by the application. |
| 42 | + */ |
| 43 | + |
| 44 | +class FanAttrAccess : public AttributeAccessInterface |
| 45 | +{ |
| 46 | +public: |
| 47 | + // Register for the FanControl cluster on all endpoints. |
| 48 | + FanAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), FanControl::Id) {} |
| 49 | + |
| 50 | + CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override; |
| 51 | + |
| 52 | +private: |
| 53 | + static constexpr uint16_t ClusterRevision = 1; |
| 54 | + |
| 55 | + CHIP_ERROR ReadPercentCurrent(EndpointId endpoint, AttributeValueEncoder & aEncoder); |
| 56 | + CHIP_ERROR ReadSpeedCurrent(EndpointId endpoint, AttributeValueEncoder & aEncoder); |
| 57 | +}; |
| 58 | + |
| 59 | +CHIP_ERROR FanAttrAccess::ReadPercentCurrent(EndpointId endpoint, AttributeValueEncoder & aEncoder) |
| 60 | +{ |
| 61 | + // Return PercentSetting attribute value for now |
| 62 | + DataModel::Nullable<uint8_t> percentSetting; |
| 63 | + PercentSetting::Get(endpoint, percentSetting); |
| 64 | + if (percentSetting.HasValidValue()) |
| 65 | + { |
| 66 | + return aEncoder.Encode(percentSetting.Value()); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + return aEncoder.Encode(0); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +CHIP_ERROR FanAttrAccess::ReadSpeedCurrent(EndpointId endpoint, AttributeValueEncoder & aEncoder) |
| 75 | +{ |
| 76 | + // Return SpeedCurrent attribute value for now |
| 77 | + DataModel::Nullable<uint8_t> speedSetting; |
| 78 | + SpeedSetting::Get(endpoint, speedSetting); |
| 79 | + if (speedSetting.HasValidValue()) |
| 80 | + { |
| 81 | + return aEncoder.Encode(speedSetting.Value()); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + return aEncoder.Encode(0); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +FanAttrAccess gAttrAccess; |
| 90 | + |
| 91 | +CHIP_ERROR FanAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 92 | +{ |
| 93 | + VerifyOrDie(aPath.mClusterId == FanControl::Id); |
| 94 | + |
| 95 | + switch (aPath.mAttributeId) |
| 96 | + { |
| 97 | + case SpeedCurrent::Id: |
| 98 | + return ReadSpeedCurrent(aPath.mEndpointId, aEncoder); |
| 99 | + case PercentCurrent::Id: |
| 100 | + return ReadPercentCurrent(aPath.mEndpointId, aEncoder); |
| 101 | + default: |
| 102 | + break; |
| 103 | + } |
| 104 | + return CHIP_NO_ERROR; |
| 105 | +} |
| 106 | +} // anonymous namespace |
| 107 | + |
| 108 | +void emberAfFanControlClusterInitCallback(EndpointId endpoint) |
| 109 | +{ |
| 110 | + uint32_t featureMap = 0; |
| 111 | + |
| 112 | + featureMap |= to_underlying(FanControl::FanControlFeature::kMultiSpeed); |
| 113 | + featureMap |= to_underlying(FanControl::FanControlFeature::kAuto); |
| 114 | + |
| 115 | + FeatureMap::Set(endpoint, featureMap); |
| 116 | + |
| 117 | + registerAttributeAccessOverride(&gAttrAccess); |
| 118 | +} |
0 commit comments