forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicDispatcher.cpp
351 lines (299 loc) · 11 KB
/
DynamicDispatcher.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
*
* Copyright (c) 2022-2023 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 <access/SubjectDescriptor.h>
#include <app-common/zap-generated/callback.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app-common/zap-generated/ids/Commands.h>
#include <app/CommandHandler.h>
#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>
#include <app/GlobalAttributes.h>
#include <app/MessageDef/AttributeReportIBs.h>
#include <app/MessageDef/StatusIB.h>
#include <app/WriteHandler.h>
#include <app/data-model/Decode.h>
#include <app/util/attribute-storage.h>
#include <app/util/attribute-table.h>
#include <app/util/endpoint-config-api.h>
#include <cstddef>
#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/Optional.h>
#include <lib/core/TLV.h>
#include <protocols/interaction_model/Constants.h>
/**
* This file defines the APIs needed to handle interaction model dispatch.
* These are the APIs normally defined generated ember code,
* however we want a different implementation of these
* to enable more dynamic behavior, since not all framework consumers will be
* implementing the same server clusters.
*/
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
namespace {
// TODO: Maybe consider making this configurable? See also
// AccessControl.cpp.
constexpr EndpointId kSupportedEndpoint = 0;
DataVersion gMockDataVersion = 0;
} // anonymous namespace
namespace chip {
namespace app {
using Access::SubjectDescriptor;
using Protocols::InteractionModel::Status;
void DispatchSingleClusterCommand(const ConcreteCommandPath & aPath, TLV::TLVReader & aReader, CommandHandler * aCommandObj)
{
// This command passed ServerClusterCommandExists so we know it's one of our
// supported commands.
using namespace OtaSoftwareUpdateProvider::Commands;
bool wasHandled = false;
CHIP_ERROR err = CHIP_NO_ERROR;
switch (aPath.mCommandId)
{
case QueryImage::Id: {
QueryImage::DecodableType commandData;
err = DataModel::Decode(aReader, commandData);
if (err == CHIP_NO_ERROR)
{
wasHandled = emberAfOtaSoftwareUpdateProviderClusterQueryImageCallback(aCommandObj, aPath, commandData);
}
break;
}
case ApplyUpdateRequest::Id: {
ApplyUpdateRequest::DecodableType commandData;
err = DataModel::Decode(aReader, commandData);
if (err == CHIP_NO_ERROR)
{
wasHandled = emberAfOtaSoftwareUpdateProviderClusterApplyUpdateRequestCallback(aCommandObj, aPath, commandData);
}
break;
}
case NotifyUpdateApplied::Id: {
NotifyUpdateApplied::DecodableType commandData;
err = DataModel::Decode(aReader, commandData);
if (err == CHIP_NO_ERROR)
{
wasHandled = emberAfOtaSoftwareUpdateProviderClusterNotifyUpdateAppliedCallback(aCommandObj, aPath, commandData);
}
break;
}
default:
break;
}
if (CHIP_NO_ERROR != err || !wasHandled)
{
aCommandObj->AddStatus(aPath, Status::InvalidCommand);
}
}
} // namespace app
} // namespace chip
/**
* Called by the OTA provider cluster server to determine an index
* into its array.
*/
uint16_t emberAfGetClusterServerEndpointIndex(EndpointId endpoint, ClusterId cluster, uint16_t fixedClusterServerEndpointCount)
{
if (endpoint == kSupportedEndpoint && cluster == OtaSoftwareUpdateProvider::Id)
{
return 0;
}
return UINT16_MAX;
}
/**
* Methods used by AttributePathExpandIterator, which need to exist
* because it is part of libCHIP. For AttributePathExpandIterator
* purposes, for now, we just pretend like we have just our one
* endpoint, the OTA Provider cluster, and no attributes (because we
* would be erroring out from them anyway).
*/
uint16_t emberAfGetServerAttributeCount(EndpointId endpoint, ClusterId cluster)
{
return 0;
}
uint16_t emberAfEndpointCount(void)
{
return 1;
}
uint16_t emberAfIndexFromEndpoint(EndpointId endpoint)
{
if (endpoint == kSupportedEndpoint)
{
return 0;
}
return UINT16_MAX;
}
EndpointId emberAfEndpointFromIndex(uint16_t index)
{
// Index must be valid here, so 0.
return kSupportedEndpoint;
}
Optional<ClusterId> emberAfGetNthClusterId(EndpointId endpoint, uint8_t n, bool server)
{
if (endpoint == kSupportedEndpoint && n == 0 && server)
{
return MakeOptional(OtaSoftwareUpdateProvider::Id);
}
return NullOptional;
}
uint16_t emberAfGetServerAttributeIndexByAttributeId(EndpointId endpoint, ClusterId cluster, AttributeId attributeId)
{
return UINT16_MAX;
}
bool emberAfContainsAttribute(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId)
{
return false;
}
uint8_t emberAfClusterCount(EndpointId endpoint, bool server)
{
if (endpoint == kSupportedEndpoint && server)
{
return 1;
}
return 0;
}
uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * type, bool server)
{
const EmberAfClusterMask cluster_mask = server ? MATTER_CLUSTER_BITMAP_SERVER : MATTER_CLUSTER_BITMAP_CLIENT;
return static_cast<uint8_t>(std::count_if(type->cluster, type->cluster + type->clusterCount,
[=](const EmberAfCluster & cluster) { return (cluster.mask & cluster_mask) != 0; }));
}
Optional<AttributeId> emberAfGetServerAttributeIdByIndex(EndpointId endpoint, ClusterId cluster, uint16_t attributeIndex)
{
return NullOptional;
}
uint8_t emberAfClusterIndex(EndpointId endpoint, ClusterId clusterId, EmberAfClusterMask mask)
{
if (endpoint == kSupportedEndpoint && clusterId == OtaSoftwareUpdateProvider::Id && (mask & MATTER_CLUSTER_BITMAP_SERVER))
{
return 0;
}
return UINT8_MAX;
}
bool emberAfEndpointIndexIsEnabled(uint16_t index)
{
return index == 0;
}
namespace {
const CommandId acceptedCommands[] = { Clusters::OtaSoftwareUpdateProvider::Commands::QueryImage::Id,
Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateRequest::Id,
Clusters::OtaSoftwareUpdateProvider::Commands::NotifyUpdateApplied::Id, kInvalidCommandId };
const CommandId generatedCommands[] = { Clusters::OtaSoftwareUpdateProvider::Commands::QueryImageResponse::Id,
Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateResponse::Id, kInvalidCommandId };
const EmberAfCluster otaProviderCluster{
.clusterId = Clusters::OtaSoftwareUpdateProvider::Id,
.attributes = nullptr,
.attributeCount = 0,
.clusterSize = 0,
.mask = MATTER_CLUSTER_BITMAP_SERVER,
.functions = nullptr,
.acceptedCommandList = acceptedCommands,
.generatedCommandList = generatedCommands,
.eventList = nullptr,
.eventCount = 0,
};
const EmberAfEndpointType otaProviderEndpoint{ .cluster = &otaProviderCluster, .clusterCount = 1, .endpointSize = 0 };
} // namespace
const EmberAfEndpointType * emberAfFindEndpointType(EndpointId endpoint)
{
if (endpoint == kSupportedEndpoint)
{
return &otaProviderEndpoint;
}
return nullptr;
}
const EmberAfCluster * emberAfFindServerCluster(EndpointId endpoint, ClusterId cluster)
{
if (endpoint == kSupportedEndpoint && cluster == Clusters::OtaSoftwareUpdateProvider::Id)
{
return &otaProviderCluster;
}
return nullptr;
}
unsigned emberAfMetadataStructureGeneration()
{
// DynamicDispatcher at this point hardcodes a single OTA provider cluster.
// The structure does not change over time, so the current version stays at 0.
return 0;
}
Protocols::InteractionModel::Status emberAfWriteAttribute(const ConcreteAttributePath & path, const EmberAfWriteDataInput & input)
{
return Protocols::InteractionModel::Status::UnsupportedAttribute;
}
Protocols::InteractionModel::Status emAfReadOrWriteAttribute(const EmberAfAttributeSearchRecord * attRecord,
const EmberAfAttributeMetadata ** metadata, uint8_t * buffer,
uint16_t readLength, bool write)
{
return Protocols::InteractionModel::Status::UnsupportedAttribute;
}
namespace chip {
namespace app {
EndpointComposition GetCompositionForEndpointIndex(uint16_t endpointIndex)
{
return EndpointComposition::kFullFamily;
}
} // namespace app
} // namespace chip
EndpointId emberAfParentEndpointFromIndex(uint16_t index)
{
return kInvalidEndpointId;
}
CHIP_ERROR GetSemanticTagForEndpointAtIndex(EndpointId endpoint, size_t index,
Clusters::Descriptor::Structs::SemanticTagStruct::Type & tag)
{
return CHIP_ERROR_NOT_FOUND;
}
void emberAfAttributeChanged(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId,
AttributesChangedListener * listener)
{
gMockDataVersion++;
listener->MarkDirty(AttributePathParams(endpoint, clusterId, attributeId));
}
void emberAfEndpointChanged(EndpointId endpoint, AttributesChangedListener * listener)
{
listener->MarkDirty(AttributePathParams(endpoint));
}
DataVersion * emberAfDataVersionStorage(const ConcreteClusterPath & aConcreteClusterPath)
{
return &gMockDataVersion;
}
Protocols::InteractionModel::Status emAfWriteAttributeExternal(const ConcreteAttributePath & path,
const EmberAfWriteDataInput & input)
{
return Protocols::InteractionModel::Status::UnsupportedAttribute;
}
Span<const EmberAfDeviceType> emberAfDeviceTypeListFromEndpointIndex(unsigned endpointIndex, CHIP_ERROR & err)
{
err = CHIP_ERROR_NOT_IMPLEMENTED;
return Span<const EmberAfDeviceType>();
}
const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endpointType, ClusterId clusterId,
EmberAfClusterMask mask, uint8_t * index)
{
if ((endpointType == &otaProviderEndpoint) && (clusterId == Clusters::OtaSoftwareUpdateProvider::Id))
{
return &otaProviderCluster;
}
return nullptr;
}
const EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId)
{
// no known attributes even for OTA
return nullptr;
}