forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups-server.cpp
389 lines (335 loc) · 15.7 KB
/
groups-server.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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 "groups-server.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/CommandHandler.h>
#include <app/reporting/reporting.h>
#include <app/util/config.h>
#include <credentials/GroupDataProvider.h>
#include <inttypes.h>
#include <lib/support/CodeUtils.h>
#include <tracing/macros.h>
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
#include <app/clusters/scenes-server/scenes-server.h>
#endif // MATTER_DM_PLUGIN_SCENES_MANAGEMENT
using namespace chip;
using namespace app::Clusters;
using namespace app::Clusters::Groups;
using namespace chip::Credentials;
using Protocols::InteractionModel::Status;
// Is the device identifying?
static bool emberAfIsDeviceIdentifying(EndpointId endpoint)
{
#ifdef ZCL_USING_IDENTIFY_CLUSTER_SERVER
uint16_t identifyTime;
Status status = app::Clusters::Identify::Attributes::IdentifyTime::Get(endpoint, &identifyTime);
return (status == Status::Success && 0 < identifyTime);
#else
return false;
#endif
}
/**
* @brief Checks if group-endpoint association exist for the given fabric
*/
static bool GroupExists(FabricIndex fabricIndex, EndpointId endpointId, GroupId groupId)
{
GroupDataProvider * provider = GetGroupDataProvider();
VerifyOrReturnError(nullptr != provider, false);
return provider->HasEndpoint(fabricIndex, groupId, endpointId);
}
/**
* @brief Checks if there are key set associated with the given GroupId
*/
static bool KeyExists(FabricIndex fabricIndex, GroupId groupId)
{
GroupDataProvider * provider = GetGroupDataProvider();
VerifyOrReturnError(nullptr != provider, false);
GroupDataProvider::GroupKey entry;
auto it = provider->IterateGroupKeys(fabricIndex);
bool found = false;
while (!found && it->Next(entry))
{
if (entry.group_id == groupId)
{
GroupDataProvider::KeySet keys;
found = (CHIP_NO_ERROR == provider->GetKeySet(fabricIndex, entry.keyset_id, keys));
}
}
it->Release();
return found;
}
static Status GroupAdd(FabricIndex fabricIndex, EndpointId endpointId, GroupId groupId, const CharSpan & groupName)
{
VerifyOrReturnError(IsValidGroupId(groupId), Status::ConstraintError);
VerifyOrReturnError(groupName.size() <= GroupDataProvider::GroupInfo::kGroupNameMax, Status::ConstraintError);
GroupDataProvider * provider = GetGroupDataProvider();
VerifyOrReturnError(nullptr != provider, Status::NotFound);
VerifyOrReturnError(KeyExists(fabricIndex, groupId), Status::UnsupportedAccess);
// Add a new entry to the GroupTable
CHIP_ERROR err = provider->SetGroupInfo(fabricIndex, GroupDataProvider::GroupInfo(groupId, groupName));
if (CHIP_NO_ERROR == err)
{
err = provider->AddEndpoint(fabricIndex, groupId, endpointId);
}
if (CHIP_NO_ERROR == err)
{
MatterReportingAttributeChangeCallback(kRootEndpointId, GroupKeyManagement::Id,
GroupKeyManagement::Attributes::GroupTable::Id);
return Status::Success;
}
ChipLogDetail(Zcl, "ERR: Failed to add mapping (end:%d, group:0x%x), err:%" CHIP_ERROR_FORMAT, endpointId, groupId,
err.Format());
return Status::ResourceExhausted;
}
static Status GroupRemove(FabricIndex fabricIndex, EndpointId endpointId, GroupId groupId)
{
VerifyOrReturnError(IsValidGroupId(groupId), Status::ConstraintError);
VerifyOrReturnError(GroupExists(fabricIndex, endpointId, groupId), Status::NotFound);
GroupDataProvider * provider = GetGroupDataProvider();
VerifyOrReturnError(nullptr != provider, Status::NotFound);
CHIP_ERROR err = provider->RemoveEndpoint(fabricIndex, groupId, endpointId);
if (CHIP_NO_ERROR == err)
{
MatterReportingAttributeChangeCallback(kRootEndpointId, GroupKeyManagement::Id,
GroupKeyManagement::Attributes::GroupTable::Id);
return Status::Success;
}
ChipLogDetail(Zcl, "ERR: Failed to remove mapping (end:%d, group:0x%x), err:%" CHIP_ERROR_FORMAT, endpointId, groupId,
err.Format());
return Status::NotFound;
}
void emberAfGroupsClusterServerInitCallback(EndpointId endpointId)
{
// According to spec, highest bit (Group Names) MUST match feature bit 0 (Group Names)
Status status = Attributes::NameSupport::Set(endpointId, NameSupportBitmap::kGroupNames);
if (status != Status::Success)
{
ChipLogDetail(Zcl, "ERR: writing NameSupport %x", to_underlying(status));
}
status = Attributes::FeatureMap::Set(endpointId, static_cast<uint32_t>(Feature::kGroupNames));
if (status != Status::Success)
{
ChipLogDetail(Zcl, "ERR: writing group feature map %x", to_underlying(status));
}
}
bool emberAfGroupsClusterAddGroupCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::AddGroup::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("AddGroup", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
Groups::Commands::AddGroupResponse::Type response;
response.groupID = commandData.groupID;
response.status = to_underlying(GroupAdd(fabricIndex, commandPath.mEndpointId, commandData.groupID, commandData.groupName));
commandObj->AddResponse(commandPath, response);
return true;
}
bool emberAfGroupsClusterViewGroupCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::ViewGroup::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("ViewGroup", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
auto groupId = commandData.groupID;
GroupDataProvider * provider = GetGroupDataProvider();
GroupDataProvider::GroupInfo info;
Groups::Commands::ViewGroupResponse::Type response;
CHIP_ERROR err = CHIP_NO_ERROR;
Status status = Status::NotFound;
VerifyOrExit(IsValidGroupId(groupId), status = Status::ConstraintError);
VerifyOrExit(nullptr != provider, status = Status::Failure);
VerifyOrExit(provider->HasEndpoint(fabricIndex, groupId, commandPath.mEndpointId), status = Status::NotFound);
err = provider->GetGroupInfo(fabricIndex, groupId, info);
VerifyOrExit(CHIP_NO_ERROR == err, status = Status::NotFound);
response.groupName = CharSpan(info.name, strnlen(info.name, GroupDataProvider::GroupInfo::kGroupNameMax));
status = Status::Success;
exit:
response.groupID = groupId;
response.status = to_underlying(status);
commandObj->AddResponse(commandPath, response);
return true;
}
struct GroupMembershipResponse
{
// A null capacity means that it is unknown if any further groups MAY be added.
const chip::app::DataModel::Nullable<uint8_t> kCapacityUnknown;
// Use GetCommandId instead of commandId directly to avoid naming conflict with CommandIdentification in ExecutionOfACommand
static constexpr CommandId GetCommandId() { return Commands::GetGroupMembershipResponse::Id; }
static constexpr ClusterId GetClusterId() { return Groups::Id; }
GroupMembershipResponse(const Commands::GetGroupMembership::DecodableType & data, chip::EndpointId endpoint,
GroupDataProvider::EndpointIterator * iter) :
mCommandData(data),
mEndpoint(endpoint), mIterator(iter)
{}
const Commands::GetGroupMembership::DecodableType & mCommandData;
chip::EndpointId mEndpoint = kInvalidEndpointId;
GroupDataProvider::EndpointIterator * mIterator = nullptr;
CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag) const
{
TLV::TLVType outer;
ReturnErrorOnFailure(writer.StartContainer(tag, TLV::kTLVType_Structure, outer));
ReturnErrorOnFailure(app::DataModel::Encode(
writer, TLV::ContextTag(Commands::GetGroupMembershipResponse::Fields::kCapacity), kCapacityUnknown));
{
TLV::TLVType type;
ReturnErrorOnFailure(writer.StartContainer(TLV::ContextTag(Commands::GetGroupMembershipResponse::Fields::kGroupList),
TLV::kTLVType_Array, type));
{
GroupDataProvider::GroupEndpoint mapping;
size_t requestedCount = 0;
ReturnErrorOnFailure(mCommandData.groupList.ComputeSize(&requestedCount));
if (0 == requestedCount)
{
// 1.3.6.3.1. If the GroupList field is empty, the entity SHALL respond with all group identifiers of which the
// entity is a member.
while (mIterator && mIterator->Next(mapping))
{
if (mapping.endpoint_id == mEndpoint)
{
ReturnErrorOnFailure(app::DataModel::Encode(writer, TLV::AnonymousTag(), mapping.group_id));
ChipLogDetail(Zcl, " 0x%02x", mapping.group_id);
}
}
}
else
{
while (mIterator && mIterator->Next(mapping))
{
auto iter = mCommandData.groupList.begin();
while (iter.Next())
{
if (mapping.endpoint_id == mEndpoint && mapping.group_id == iter.GetValue())
{
ReturnErrorOnFailure(app::DataModel::Encode(writer, TLV::AnonymousTag(), mapping.group_id));
ChipLogDetail(Zcl, " 0x%02x", mapping.group_id);
break;
}
}
ReturnErrorOnFailure(iter.GetStatus());
}
}
ChipLogDetail(Zcl, "]");
}
ReturnErrorOnFailure(writer.EndContainer(type));
}
ReturnErrorOnFailure(writer.EndContainer(outer));
return CHIP_NO_ERROR;
}
};
bool emberAfGroupsClusterGetGroupMembershipCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::GetGroupMembership::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("GetGroupMembership", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
auto * provider = GetGroupDataProvider();
Status status = Status::Failure;
VerifyOrExit(nullptr != provider, status = Status::Failure);
{
GroupDataProvider::EndpointIterator * iter = nullptr;
iter = provider->IterateEndpoints(fabricIndex);
VerifyOrExit(nullptr != iter, status = Status::Failure);
commandObj->AddResponse(commandPath, GroupMembershipResponse(commandData, commandPath.mEndpointId, iter));
iter->Release();
status = Status::Success;
}
exit:
if (Status::Success != status)
{
ChipLogDetail(Zcl, "GroupsCluster: GetGroupMembership failed: failed: 0x%x", to_underlying(status));
commandObj->AddStatus(commandPath, status);
}
return true;
}
bool emberAfGroupsClusterRemoveGroupCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::RemoveGroup::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("RemoveGroup", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
Groups::Commands::RemoveGroupResponse::Type response;
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
// If a group is removed the scenes associated with that group SHOULD be removed.
ScenesManagement::ScenesServer::Instance().GroupWillBeRemoved(fabricIndex, commandPath.mEndpointId, commandData.groupID);
#endif
response.groupID = commandData.groupID;
response.status = to_underlying(GroupRemove(fabricIndex, commandPath.mEndpointId, commandData.groupID));
commandObj->AddResponse(commandPath, response);
return true;
}
bool emberAfGroupsClusterRemoveAllGroupsCallback(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::RemoveAllGroups::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("RemoveAllGroups", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
auto * provider = GetGroupDataProvider();
Status status = Status::Failure;
VerifyOrExit(nullptr != provider, status = Status::Failure);
#ifdef MATTER_DM_PLUGIN_SCENES_MANAGEMENT
{
GroupDataProvider::EndpointIterator * iter = provider->IterateEndpoints(fabricIndex);
GroupDataProvider::GroupEndpoint mapping;
VerifyOrExit(nullptr != iter, status = Status::Failure);
while (iter->Next(mapping))
{
if (commandPath.mEndpointId == mapping.endpoint_id)
{
ScenesManagement::ScenesServer::Instance().GroupWillBeRemoved(fabricIndex, mapping.endpoint_id, mapping.group_id);
}
}
iter->Release();
ScenesManagement::ScenesServer::Instance().GroupWillBeRemoved(fabricIndex, commandPath.mEndpointId,
ScenesManagement::ScenesServer::kGlobalSceneGroupId);
}
#endif
provider->RemoveEndpoint(fabricIndex, commandPath.mEndpointId);
status = Status::Success;
MatterReportingAttributeChangeCallback(kRootEndpointId, GroupKeyManagement::Id, GroupKeyManagement::Attributes::GroupTable::Id);
exit:
commandObj->AddStatus(commandPath, status);
if (Status::Success != status)
{
ChipLogDetail(Zcl, "GroupsCluster: RemoveAllGroups failed: 0x%x", to_underlying(status));
}
return true;
}
bool emberAfGroupsClusterAddGroupIfIdentifyingCallback(app::CommandHandler * commandObj,
const app::ConcreteCommandPath & commandPath,
const Commands::AddGroupIfIdentifying::DecodableType & commandData)
{
MATTER_TRACE_SCOPE("AddGroupIfIdentifying", "Groups");
auto fabricIndex = commandObj->GetAccessingFabricIndex();
auto groupId = commandData.groupID;
auto groupName = commandData.groupName;
auto endpointId = commandPath.mEndpointId;
Status status;
if (!emberAfIsDeviceIdentifying(endpointId))
{
// If not identifying, ignore add group -> success; not a failure.
status = Status::Success;
}
else
{
status = GroupAdd(fabricIndex, endpointId, groupId, groupName);
}
commandObj->AddStatus(commandPath, status);
return true;
}
bool emberAfGroupsClusterEndpointInGroupCallback(chip::FabricIndex fabricIndex, EndpointId endpointId, GroupId groupId)
{
return GroupExists(fabricIndex, endpointId, groupId);
}
void emberAfPluginGroupsServerSetGroupNameCallback(EndpointId endpoint, GroupId groupId, const CharSpan & groupName) {}
void MatterGroupsPluginServerInitCallback() {}