-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAttributePathExpandIterator.cpp
193 lines (172 loc) · 8.11 KB
/
AttributePathExpandIterator.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
*
* Copyright (c) 2021 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.
*/
/**
* @file
* This file defines read handler for a CHIP Interaction Data model
*
*/
#include <app/AttributePathExpandIterator.h>
#include <app-common/zap-generated/att-storage.h>
#include <app/ClusterInfo.h>
#include <app/ConcreteAttributePath.h>
#include <app/EventManagement.h>
#include <app/InteractionModelDelegate.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPTLVDebug.hpp>
#include <lib/support/CodeUtils.h>
#include <lib/support/DLLUtil.h>
#include <lib/support/logging/CHIPLogging.h>
using namespace chip;
// TODO: Here we use forward declaration for these symbols used, there should be some reorganize for code in app/util so they can be
// used with generated files or some mock files.
typedef uint8_t EmberAfClusterMask;
#define CLUSTER_MASK_SERVER (0x40)
extern uint16_t emberAfEndpointCount(void);
extern uint16_t emberAfIndexFromEndpoint(EndpointId endpoint);
extern uint8_t emberAfClusterCount(EndpointId endpoint, bool server);
extern uint16_t emberAfGetServerAttributeCount(chip::EndpointId endpoint, chip::ClusterId cluster);
extern uint16_t emberAfGetServerAttributeIndexByAttributeId(chip::EndpointId endpoint, chip::ClusterId cluster,
chip::AttributeId attributeId);
extern chip::EndpointId emberAfEndpointFromIndex(uint16_t index);
extern Optional<ClusterId> emberAfGetNthClusterId(chip::EndpointId endpoint, uint8_t n, bool server);
extern Optional<AttributeId> emberAfGetServerAttributeIdByIndex(chip::EndpointId endpoint, chip::ClusterId cluster,
uint16_t attributeIndex);
extern uint8_t emberAfClusterIndex(EndpointId endpoint, ClusterId clusterId, EmberAfClusterMask mask);
namespace chip {
namespace app {
void AttributePathExpandIterator::Reset(ClusterInfo * aClusterInfo)
{
mpClusterInfo = aClusterInfo;
// Reset iterator state
mEndpointIndex = UINT16_MAX;
mClusterIndex = UINT8_MAX;
mAttributeIndex = UINT16_MAX;
Proceed();
}
void AttributePathExpandIterator::PrepareEndpointIndexRange(const ClusterInfo & aClusterInfo)
{
if (aClusterInfo.HasWildcardEndpointId())
{
mBeginEndpointIndex = 0;
mEndEndpointIndex = emberAfEndpointCount();
}
else
{
mBeginEndpointIndex = emberAfIndexFromEndpoint(aClusterInfo.mEndpointId);
// If the given cluster id does not exist on the given endpoint, it will return uint16(0xFFFF), then endEndpointIndex
// will be 0, means we should iterate a null endppint set (skip it).
mEndEndpointIndex = static_cast<uint16_t>(mBeginEndpointIndex + 1);
}
}
void AttributePathExpandIterator::PrepareClusterIndexRange(const ClusterInfo & aClusterInfo, EndpointId aEndpointId)
{
if (aClusterInfo.HasWildcardClusterId())
{
mBeginClusterIndex = 0;
mEndClusterIndex = emberAfClusterCount(aEndpointId, true /* server */);
}
else
{
mBeginClusterIndex = emberAfClusterIndex(aEndpointId, aClusterInfo.mClusterId, CLUSTER_MASK_SERVER);
// If the given cluster id does not exist on the given endpoint, it will return uint8(0xFF), then endClusterIndex
// will be 0, means we should i
mEndClusterIndex = static_cast<uint8_t>(mBeginClusterIndex + 1);
}
}
void AttributePathExpandIterator::PrepareAttributeIndexRange(const ClusterInfo & aClusterInfo, EndpointId aEndpointId,
ClusterId aClusterId)
{
if (aClusterInfo.HasWildcardAttributeId())
{
mBeginAttributeIndex = 0;
mEndAttributeIndex = emberAfGetServerAttributeCount(aEndpointId, aClusterId);
}
else
{
mBeginAttributeIndex = emberAfGetServerAttributeIndexByAttributeId(aEndpointId, aClusterId, aClusterInfo.mFieldId);
// If the given attribute id does not exist on the given endpoint, it will return uint16(0xFFFF), then endAttributeIndex
// will be 0
mEndAttributeIndex = static_cast<uint16_t>(mBeginAttributeIndex + 1);
}
}
bool AttributePathExpandIterator::Proceed()
{
for (; mpClusterInfo != nullptr; (mpClusterInfo = mpClusterInfo->mpNext, mEndpointIndex = UINT16_MAX))
{
// Special case: If this is a concrete path, we just return its value as-is.
if (!mpClusterInfo->HasWildcard())
{
mOutputPath.mEndpointId = mpClusterInfo->mEndpointId;
mOutputPath.mClusterId = mpClusterInfo->mClusterId;
mOutputPath.mAttributeId = mpClusterInfo->mFieldId;
// Prepare for next iteration
(mpClusterInfo = mpClusterInfo->mpNext, mEndpointIndex = UINT16_MAX);
return true;
}
if (mEndpointIndex == UINT16_MAX)
{
PrepareEndpointIndexRange(*mpClusterInfo);
// If we have not started iterating over the endpoints yet.
mEndpointIndex = mBeginEndpointIndex;
mClusterIndex = UINT8_MAX;
}
for (; mEndpointIndex < mEndEndpointIndex; (mEndpointIndex++, mClusterIndex = UINT8_MAX, mAttributeIndex = UINT16_MAX))
{
EndpointId endpointId = emberAfEndpointFromIndex(mEndpointIndex);
if (mClusterIndex == UINT8_MAX)
{
PrepareClusterIndexRange(*mpClusterInfo, endpointId);
// If we have not started iterating over the clusters yet.
mClusterIndex = mBeginClusterIndex;
mAttributeIndex = UINT16_MAX;
}
for (; mClusterIndex < mEndClusterIndex; (mClusterIndex++, mAttributeIndex = UINT16_MAX))
{
// emberAfGetNthClusterId must return a valid cluster id here since we have verified the mClusterIndex does
// not exceed the endAttributeIndex.
ClusterId clusterId = emberAfGetNthClusterId(endpointId, mClusterIndex, true /* server */).Value();
if (mAttributeIndex == UINT16_MAX)
{
PrepareAttributeIndexRange(*mpClusterInfo, endpointId, clusterId);
// If we have not started iterating over the attributes yet.
mAttributeIndex = mBeginAttributeIndex;
}
if (mAttributeIndex < mEndAttributeIndex)
{
// GetServerAttributeIdByIdex must return a valid attribute here since we have verified the mAttributeindex does
// not exceed the endAttributeIndex.
mOutputPath.mAttributeId = emberAfGetServerAttributeIdByIndex(endpointId, clusterId, mAttributeIndex).Value();
mOutputPath.mClusterId = clusterId;
mOutputPath.mEndpointId = endpointId;
mAttributeIndex++;
// We found a valid attribute path, now return and increase the attribute index for next iteration.
// Return true will skip the increment of mClusterIndex, mEndpointIndex and mpClusterInfo.
return true;
}
// We have exhausted all attributes of this cluster, continue iterating over attributes of next cluster.
}
// We have exhausted all clusters of this endpoint, continue iterating over clusters of next endpoint.
}
// We have exhausted all endpoints in this cluster info, continue iterating over next cluster info item.
}
// Reset to default, invalid value.
mOutputPath = ConcreteAttributePath();
return false;
}
} // namespace app
} // namespace chip