|
| 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 "chef-window-covering.h" |
| 20 | +#include "app/clusters/window-covering-server/window-covering-server.h" |
| 21 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 22 | +#include <app/reporting/reporting.h> |
| 23 | +#include <app/util/attribute-storage.h> |
| 24 | +#include <app/util/config.h> |
| 25 | +#include <app/util/endpoint-config-api.h> |
| 26 | +#include <lib/support/logging/CHIPLogging.h> |
| 27 | + |
| 28 | +using namespace chip; |
| 29 | +using namespace chip::app::Clusters; |
| 30 | +using chip::Protocols::InteractionModel::Status; |
| 31 | + |
| 32 | +constexpr size_t kWindowCoveringDelegateTableSize = MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT; |
| 33 | +static_assert(kWindowCoveringDelegateTableSize <= kEmberInvalidEndpointIndex, "WindowCovering Delegate table size error"); |
| 34 | + |
| 35 | +std::unique_ptr<WindowCovering::ChefDelegate> gDelegateTable[kWindowCoveringDelegateTableSize]; |
| 36 | + |
| 37 | +WindowCovering::ChefDelegate * GetDelegate(EndpointId endpoint) |
| 38 | +{ |
| 39 | + uint16_t ep = |
| 40 | + emberAfGetClusterServerEndpointIndex(endpoint, WindowCovering::Id, MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT); |
| 41 | + return (ep >= kWindowCoveringDelegateTableSize ? nullptr : gDelegateTable[ep].get()); |
| 42 | +} |
| 43 | + |
| 44 | +void InitChefWindowCoveringCluster() |
| 45 | +{ |
| 46 | + const uint16_t endpointCount = emberAfEndpointCount(); |
| 47 | + |
| 48 | + for (uint16_t endpointIndex = 0; endpointIndex < endpointCount; endpointIndex++) |
| 49 | + { |
| 50 | + EndpointId endpointId = emberAfEndpointFromIndex(endpointIndex); |
| 51 | + if (endpointId == kInvalidEndpointId) |
| 52 | + { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if endpoint has WindowCovering cluster enabled |
| 57 | + uint16_t epIndex = emberAfGetClusterServerEndpointIndex(endpointId, WindowCovering::Id, |
| 58 | + MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT); |
| 59 | + if (epIndex >= kWindowCoveringDelegateTableSize) |
| 60 | + continue; |
| 61 | + |
| 62 | + // Skip if delegate was already initialized. |
| 63 | + if (gDelegateTable[epIndex]) |
| 64 | + continue; |
| 65 | + |
| 66 | + gDelegateTable[epIndex] = std::make_unique<WindowCovering::ChefDelegate>(); |
| 67 | + gDelegateTable[epIndex]->SetEndpoint(endpointId); |
| 68 | + WindowCovering::SetDefaultDelegate(endpointId, gDelegateTable[epIndex].get()); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +CHIP_ERROR WindowCovering::ChefDelegate::HandleMovement(WindowCoveringType type) |
| 73 | +{ |
| 74 | + Status status; |
| 75 | + app::DataModel::Nullable<Percent100ths> current; |
| 76 | + |
| 77 | + if (type == WindowCoveringType::Lift) |
| 78 | + { |
| 79 | + status = WindowCovering::Attributes::TargetPositionLiftPercent100ths::Get(mEndpoint, current); |
| 80 | + if (status != Status::Success) |
| 81 | + { |
| 82 | + ChipLogError(DeviceLayer, "HandleMovement: Failed to get TargetPositionLiftPercent100ths with error code %d", |
| 83 | + to_underlying(status)); |
| 84 | + return CHIP_ERROR_READ_FAILED; |
| 85 | + } |
| 86 | + |
| 87 | + // Instant update. No transition for now. |
| 88 | + status = WindowCovering::Attributes::CurrentPositionLiftPercent100ths::Set(mEndpoint, current); |
| 89 | + if (status != Status::Success) |
| 90 | + { |
| 91 | + ChipLogError(DeviceLayer, "HandleMovement: Failed to set CurrentPositionLiftPercent100ths with error code %d", |
| 92 | + to_underlying(status)); |
| 93 | + return CHIP_ERROR_WRITE_FAILED; |
| 94 | + } |
| 95 | + |
| 96 | + MatterReportingAttributeChangeCallback(mEndpoint, WindowCovering::Id, |
| 97 | + WindowCovering::Attributes::CurrentPositionLiftPercent100ths::Id); |
| 98 | + |
| 99 | + return CHIP_NO_ERROR; |
| 100 | + } |
| 101 | + else if (type == WindowCoveringType::Tilt) |
| 102 | + { |
| 103 | + status = WindowCovering::Attributes::TargetPositionTiltPercent100ths::Get(mEndpoint, current); |
| 104 | + if (status != Status::Success) |
| 105 | + { |
| 106 | + ChipLogError(DeviceLayer, "HandleMovement: Failed to get TargetPositionTiltPercent100ths - %d", to_underlying(status)); |
| 107 | + return CHIP_ERROR_READ_FAILED; |
| 108 | + } |
| 109 | + |
| 110 | + // Instant update. No transition for now. |
| 111 | + status = WindowCovering::Attributes::CurrentPositionTiltPercent100ths::Set(mEndpoint, current); |
| 112 | + if (status != Status::Success) |
| 113 | + { |
| 114 | + ChipLogError(DeviceLayer, "HandleMovement: Failed to set CurrentPositionTiltPercent100ths - %d", to_underlying(status)); |
| 115 | + return CHIP_ERROR_WRITE_FAILED; |
| 116 | + } |
| 117 | + |
| 118 | + MatterReportingAttributeChangeCallback(mEndpoint, WindowCovering::Id, |
| 119 | + WindowCovering::Attributes::CurrentPositionTiltPercent100ths::Id); |
| 120 | + |
| 121 | + return CHIP_NO_ERROR; |
| 122 | + } |
| 123 | + return CHIP_NO_ERROR; |
| 124 | +} |
| 125 | + |
| 126 | +CHIP_ERROR WindowCovering::ChefDelegate::HandleStopMotion() |
| 127 | +{ |
| 128 | + return CHIP_NO_ERROR; |
| 129 | +} |
0 commit comments