forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchef-window-covering.cpp
129 lines (110 loc) · 5.1 KB
/
chef-window-covering.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "chef-window-covering.h"
#include "app/clusters/window-covering-server/window-covering-server.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <app/util/config.h>
#include <app/util/endpoint-config-api.h>
#include <lib/support/logging/CHIPLogging.h>
using namespace chip;
using namespace chip::app::Clusters;
using chip::Protocols::InteractionModel::Status;
constexpr size_t kWindowCoveringDelegateTableSize =
MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
static_assert(kWindowCoveringDelegateTableSize <= kEmberInvalidEndpointIndex, "WindowCovering Delegate table size error");
std::unique_ptr<WindowCovering::ChefDelegate> gDelegateTable[kWindowCoveringDelegateTableSize];
WindowCovering::ChefDelegate * GetDelegate(EndpointId endpoint)
{
uint16_t ep =
emberAfGetClusterServerEndpointIndex(endpoint, WindowCovering::Id, MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT);
return (ep >= kWindowCoveringDelegateTableSize ? nullptr : gDelegateTable[ep].get());
}
void InitChefWindowCoveringCluster()
{
const uint16_t endpointCount = emberAfEndpointCount();
for (uint16_t endpointIndex = 0; endpointIndex < endpointCount; endpointIndex++)
{
// Get endpoint ID from index.
EndpointId endpointId = emberAfEndpointFromIndex(endpointIndex);
if (endpointId == kInvalidEndpointId)
{
continue;
}
// Check if endpoint has WindowCovering cluster enabled
uint16_t epIndex = emberAfGetClusterServerEndpointIndex(endpointId, WindowCovering::Id,
MATTER_DM_WINDOW_COVERING_CLUSTER_SERVER_ENDPOINT_COUNT);
if (epIndex >= kWindowCoveringDelegateTableSize)
continue;
// Skip if delegate was already initialized.
if (gDelegateTable[epIndex])
continue;
gDelegateTable[epIndex] = std::make_unique<WindowCovering::ChefDelegate>();
gDelegateTable[epIndex]->SetEndpoint(endpointId);
WindowCovering::SetDefaultDelegate(endpointId, gDelegateTable[epIndex].get());
}
}
CHIP_ERROR WindowCovering::ChefDelegate::HandleMovement(WindowCoveringType type)
{
Status status;
app::DataModel::Nullable<Percent100ths> current;
if (type == WindowCoveringType::Lift)
{
status = WindowCovering::Attributes::TargetPositionLiftPercent100ths::Get(mEndpoint, current);
if (status != Status::Success)
{
ChipLogError(DeviceLayer, "HandleMovement: Failed to get TargetPositionLiftPercent100ths - %d", to_underlying(status));
return CHIP_ERROR_READ_FAILED;
}
// Instant update. No transition for now.
status = WindowCovering::Attributes::CurrentPositionLiftPercent100ths::Set(mEndpoint, current);
if (status != Status::Success)
{
ChipLogError(DeviceLayer, "HandleMovement: Failed to set CurrentPositionLiftPercent100ths - %d", to_underlying(status));
return CHIP_ERROR_WRITE_FAILED;
}
MatterReportingAttributeChangeCallback(mEndpoint, WindowCovering::Id,
WindowCovering::Attributes::CurrentPositionLiftPercent100ths::Id);
return CHIP_NO_ERROR;
}
else if (type == WindowCoveringType::Tilt)
{
status = WindowCovering::Attributes::TargetPositionTiltPercent100ths::Get(mEndpoint, current);
if (status != Status::Success)
{
ChipLogError(DeviceLayer, "HandleMovement: Failed to get TargetPositionTiltPercent100ths - %d", to_underlying(status));
return CHIP_ERROR_READ_FAILED;
}
// Instant update. No transition for now.
status = WindowCovering::Attributes::CurrentPositionTiltPercent100ths::Set(mEndpoint, current);
if (status != Status::Success)
{
ChipLogError(DeviceLayer, "HandleMovement: Failed to set CurrentPositionTiltPercent100ths - %d", to_underlying(status));
return CHIP_ERROR_WRITE_FAILED;
}
MatterReportingAttributeChangeCallback(mEndpoint, WindowCovering::Id,
WindowCovering::Attributes::CurrentPositionTiltPercent100ths::Id);
return CHIP_NO_ERROR;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR WindowCovering::ChefDelegate::HandleStopMotion()
{
return CHIP_NO_ERROR;
}