forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecosystem-information-server.cpp
390 lines (337 loc) · 15.7 KB
/
ecosystem-information-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
390
/*
*
* Copyright (c) 2024 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 "ecosystem-information-server.h"
#include <app/AttributeAccessInterface.h>
#include <app/AttributeAccessInterfaceRegistry.h>
namespace chip {
namespace app {
namespace Clusters {
namespace EcosystemInformation {
namespace {
constexpr size_t kDeviceNameMaxSize = 64;
constexpr size_t kUniqueLocationIdMaxSize = 64;
constexpr size_t kUniqueLocationIdsListMaxSize = 64;
constexpr size_t kLocationDescriptorNameMaxSize = 128;
constexpr size_t kDeviceDirectoryMaxSize = 256;
constexpr size_t kLocationDirectoryMaxSize = 64;
class AttrAccess : public AttributeAccessInterface
{
public:
// Register for the EcosystemInformationCluster on all endpoints.
AttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), Clusters::EcosystemInformation::Id) {}
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
};
CHIP_ERROR AttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == Clusters::EcosystemInformation::Id);
switch (aPath.mAttributeId)
{
case Attributes::RemovedOn::Id:
return EcosystemInformationServer::Instance().EncodeRemovedOnAttribute(aPath.mEndpointId, aEncoder);
case Attributes::DeviceDirectory ::Id:
return EcosystemInformationServer::Instance().EncodeDeviceDirectoryAttribute(aPath.mEndpointId, aEncoder);
case Attributes::LocationDirectory ::Id:
return EcosystemInformationServer::Instance().EncodeLocationStructAttribute(aPath.mEndpointId, aEncoder);
default:
break;
}
return CHIP_NO_ERROR;
}
// WARNING: caller is expected to use the returned LocationDescriptorStruct::Type immediately. Caller must
// be certain that the provided aLocationDescriptor has not been destroyed, prior to using the return
// struct to encode.
// TODO(#33223) To improve safety we could make GetEncodableLocationDescriptorStruct a private
// memeber method where we explicitly delete member method for the parameter that matches
// (LocationDescriptorStruct && aLocationDescriptor).
Globals::Structs::LocationDescriptorStruct::Type
GetEncodableLocationDescriptorStruct(const LocationDescriptorStruct & aLocationDescriptor)
{
Globals::Structs::LocationDescriptorStruct::Type locationDescriptor;
// This would imply data is either not properly validated before being
// stored here or corruption has occurred.
VerifyOrDie(!aLocationDescriptor.mLocationName.empty());
locationDescriptor.locationName = CharSpan(aLocationDescriptor.mLocationName.c_str(), aLocationDescriptor.mLocationName.size());
if (aLocationDescriptor.mFloorNumber.has_value())
{
locationDescriptor.floorNumber.SetNonNull(aLocationDescriptor.mFloorNumber.value());
}
else
{
locationDescriptor.floorNumber.SetNull();
}
if (aLocationDescriptor.mAreaType.has_value())
{
locationDescriptor.areaType.SetNonNull(aLocationDescriptor.mAreaType.value());
}
else
{
locationDescriptor.areaType.SetNull();
}
return locationDescriptor;
}
} // namespace
EcosystemDeviceStruct::Builder & EcosystemDeviceStruct::Builder::SetDeviceName(std::string aDeviceName,
uint64_t aDeviceNameLastEditEpochUs)
{
VerifyOrDie(!mIsAlreadyBuilt);
mDeviceName = std::move(aDeviceName);
mDeviceNameLastEditEpochUs = aDeviceNameLastEditEpochUs;
return *this;
}
EcosystemDeviceStruct::Builder & EcosystemDeviceStruct::Builder::SetBrigedEndpoint(EndpointId aBridgedEndpoint)
{
VerifyOrDie(!mIsAlreadyBuilt);
mBridgedEndpoint = aBridgedEndpoint;
return *this;
}
EcosystemDeviceStruct::Builder & EcosystemDeviceStruct::Builder::SetOriginalEndpoint(EndpointId aOriginalEndpoint)
{
VerifyOrDie(!mIsAlreadyBuilt);
mOriginalEndpoint = aOriginalEndpoint;
return *this;
}
EcosystemDeviceStruct::Builder & EcosystemDeviceStruct::Builder::AddDeviceType(Structs::DeviceTypeStruct::Type aDeviceType)
{
VerifyOrDie(!mIsAlreadyBuilt);
mDeviceTypes.push_back(std::move(aDeviceType));
return *this;
}
EcosystemDeviceStruct::Builder & EcosystemDeviceStruct::Builder::AddUniqueLocationId(std::string aUniqueLocationId,
uint64_t aUniqueLocationIdsLastEditEpochUs)
{
VerifyOrDie(!mIsAlreadyBuilt);
mUniqueLocationIds.push_back(std::move(aUniqueLocationId));
mUniqueLocationIdsLastEditEpochUs = aUniqueLocationIdsLastEditEpochUs;
return *this;
}
std::unique_ptr<EcosystemDeviceStruct> EcosystemDeviceStruct::Builder::Build()
{
VerifyOrReturnValue(!mIsAlreadyBuilt, nullptr, ChipLogError(Zcl, "Build() already called"));
VerifyOrReturnValue(mDeviceName.size() <= kDeviceNameMaxSize, nullptr, ChipLogError(Zcl, "Device name too large"));
VerifyOrReturnValue(mOriginalEndpoint != kInvalidEndpointId, nullptr, ChipLogError(Zcl, "Invalid original endpoint"));
VerifyOrReturnValue(!mDeviceTypes.empty(), nullptr, ChipLogError(Zcl, "No device types added"));
VerifyOrReturnValue(mUniqueLocationIds.size() <= kUniqueLocationIdsListMaxSize, nullptr,
ChipLogError(Zcl, "Too many location ids"));
for (auto & locationId : mUniqueLocationIds)
{
VerifyOrReturnValue(locationId.size() <= kUniqueLocationIdMaxSize, nullptr, ChipLogError(Zcl, "Location id too long"));
}
// std::make_unique does not have access to private constructor we workaround with using new
std::unique_ptr<EcosystemDeviceStruct> ret{ new EcosystemDeviceStruct(
std::move(mDeviceName), mDeviceNameLastEditEpochUs, mBridgedEndpoint, mOriginalEndpoint, std::move(mDeviceTypes),
std::move(mUniqueLocationIds), mUniqueLocationIdsLastEditEpochUs) };
mIsAlreadyBuilt = true;
return ret;
}
CHIP_ERROR EcosystemDeviceStruct::Encode(const AttributeValueEncoder::ListEncodeHelper & aEncoder, const FabricIndex & aFabricIndex)
{
Structs::EcosystemDeviceStruct::Type deviceStruct;
if (!mDeviceName.empty())
{
deviceStruct.deviceName.SetValue(CharSpan(mDeviceName.c_str(), mDeviceName.size()));
// When there is a device name we also include mDeviceNameLastEditEpochUs
deviceStruct.deviceNameLastEdit.SetValue(mDeviceNameLastEditEpochUs);
}
deviceStruct.bridgedEndpoint = mBridgedEndpoint;
deviceStruct.originalEndpoint = mOriginalEndpoint;
deviceStruct.deviceTypes = DataModel::List<const Structs::DeviceTypeStruct::Type>(mDeviceTypes.data(), mDeviceTypes.size());
std::vector<CharSpan> locationIds;
locationIds.reserve(mUniqueLocationIds.size());
for (auto & id : mUniqueLocationIds)
{
locationIds.push_back(CharSpan(id.c_str(), id.size()));
}
deviceStruct.uniqueLocationIDs = DataModel::List<CharSpan>(locationIds.data(), locationIds.size());
deviceStruct.uniqueLocationIDsLastEdit = mUniqueLocationIdsLastEditEpochUs;
// TODO(#33223) this is a hack, use mFabricIndex when it exists.
deviceStruct.SetFabricIndex(aFabricIndex);
return aEncoder.Encode(deviceStruct);
}
EcosystemLocationStruct::Builder & EcosystemLocationStruct::Builder::SetLocationName(std::string aLocationName)
{
VerifyOrDie(!mIsAlreadyBuilt);
mLocationDescriptor.mLocationName = std::move(aLocationName);
return *this;
}
EcosystemLocationStruct::Builder & EcosystemLocationStruct::Builder::SetFloorNumber(std::optional<int16_t> aFloorNumber)
{
VerifyOrDie(!mIsAlreadyBuilt);
mLocationDescriptor.mFloorNumber = aFloorNumber;
return *this;
}
EcosystemLocationStruct::Builder &
EcosystemLocationStruct::Builder::SetAreaTypeTag(std::optional<Globals::AreaTypeTag> aAreaTypeTag)
{
VerifyOrDie(!mIsAlreadyBuilt);
mLocationDescriptor.mAreaType = aAreaTypeTag;
return *this;
}
EcosystemLocationStruct::Builder &
EcosystemLocationStruct::Builder::SetLocationDescriptorLastEdit(uint64_t aLocationDescriptorLastEditEpochUs)
{
VerifyOrDie(!mIsAlreadyBuilt);
mLocationDescriptorLastEditEpochUs = aLocationDescriptorLastEditEpochUs;
return *this;
}
std::unique_ptr<EcosystemLocationStruct> EcosystemLocationStruct::Builder::Build()
{
VerifyOrReturnValue(!mIsAlreadyBuilt, nullptr, ChipLogError(Zcl, "Build() already called"));
VerifyOrReturnValue(!mLocationDescriptor.mLocationName.empty(), nullptr, ChipLogError(Zcl, "Must Provided Location Name"));
VerifyOrReturnValue(mLocationDescriptor.mLocationName.size() <= kLocationDescriptorNameMaxSize, nullptr,
ChipLogError(Zcl, "Must Location Name must be less than 64 bytes"));
// std::make_unique does not have access to private constructor we workaround with using new
std::unique_ptr<EcosystemLocationStruct> ret{ new EcosystemLocationStruct(std::move(mLocationDescriptor),
mLocationDescriptorLastEditEpochUs) };
mIsAlreadyBuilt = true;
return ret;
}
CHIP_ERROR EcosystemLocationStruct::Encode(const AttributeValueEncoder::ListEncodeHelper & aEncoder,
const std::string & aUniqueLocationId, const FabricIndex & aFabricIndex)
{
Structs::EcosystemLocationStruct::Type locationStruct;
VerifyOrDie(!aUniqueLocationId.empty());
locationStruct.uniqueLocationID = CharSpan(aUniqueLocationId.c_str(), aUniqueLocationId.size());
locationStruct.locationDescriptor = GetEncodableLocationDescriptorStruct(mLocationDescriptor);
locationStruct.locationDescriptorLastEdit = mLocationDescriptorLastEditEpochUs;
// TODO(#33223) this is a hack, use mFabricIndex when it exists.
locationStruct.SetFabricIndex(aFabricIndex);
return aEncoder.Encode(locationStruct);
}
EcosystemInformationServer EcosystemInformationServer::mInstance;
EcosystemInformationServer & EcosystemInformationServer::Instance()
{
return mInstance;
}
CHIP_ERROR EcosystemInformationServer::AddEcosystemInformationClusterToEndpoint(EndpointId aEndpoint)
{
VerifyOrReturnError((aEndpoint != kRootEndpointId && aEndpoint != kInvalidEndpointId), CHIP_ERROR_INVALID_ARGUMENT);
auto it = mDevicesMap.find(aEndpoint);
// We expect that the device has not been previously added.
VerifyOrReturnError((it == mDevicesMap.end()), CHIP_ERROR_INCORRECT_STATE);
// This create an empty DeviceInfo in mDevicesMap.
mDevicesMap[aEndpoint];
return CHIP_NO_ERROR;
}
CHIP_ERROR EcosystemInformationServer::AddDeviceInfo(EndpointId aEndpoint, std::unique_ptr<EcosystemDeviceStruct> aDevice)
{
VerifyOrReturnError(aDevice, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError((aEndpoint != kRootEndpointId && aEndpoint != kInvalidEndpointId), CHIP_ERROR_INVALID_ARGUMENT);
auto & deviceInfo = mDevicesMap[aEndpoint];
VerifyOrReturnError((deviceInfo.mDeviceDirectory.size() < kDeviceDirectoryMaxSize), CHIP_ERROR_NO_MEMORY);
deviceInfo.mDeviceDirectory.push_back(std::move(aDevice));
return CHIP_NO_ERROR;
}
CHIP_ERROR EcosystemInformationServer::AddLocationInfo(EndpointId aEndpoint, const std::string & aLocationId,
std::unique_ptr<EcosystemLocationStruct> aLocation)
{
VerifyOrReturnError(aLocation, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError((aEndpoint != kRootEndpointId && aEndpoint != kInvalidEndpointId), CHIP_ERROR_INVALID_ARGUMENT);
auto & deviceInfo = mDevicesMap[aEndpoint];
VerifyOrReturnError((deviceInfo.mLocationDirectory.find(aLocationId) == deviceInfo.mLocationDirectory.end()),
CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError((deviceInfo.mLocationDirectory.size() < kLocationDirectoryMaxSize), CHIP_ERROR_NO_MEMORY);
deviceInfo.mLocationDirectory[aLocationId] = std::move(aLocation);
return CHIP_NO_ERROR;
}
CHIP_ERROR EcosystemInformationServer::RemoveDevice(EndpointId aEndpoint, uint64_t aEpochUs)
{
auto it = mDevicesMap.find(aEndpoint);
VerifyOrReturnError((it != mDevicesMap.end()), CHIP_ERROR_INVALID_ARGUMENT);
auto & deviceInfo = it->second;
deviceInfo.mRemovedOn.SetValue(aEpochUs);
return CHIP_NO_ERROR;
}
CHIP_ERROR EcosystemInformationServer::EncodeRemovedOnAttribute(EndpointId aEndpoint, AttributeValueEncoder & aEncoder)
{
auto it = mDevicesMap.find(aEndpoint);
if (it == mDevicesMap.end())
{
// We are always going to be given a valid endpoint. If the endpoint
// doesn't exist in our map that indicate that the cluster was not
// added on this endpoint, hence UnsupportedCluster.
return CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
}
auto & deviceInfo = it->second;
if (!deviceInfo.mRemovedOn.HasValue())
{
aEncoder.EncodeNull();
return CHIP_NO_ERROR;
}
aEncoder.Encode(deviceInfo.mRemovedOn.Value());
return CHIP_NO_ERROR;
}
CHIP_ERROR EcosystemInformationServer::EncodeDeviceDirectoryAttribute(EndpointId aEndpoint, AttributeValueEncoder & aEncoder)
{
auto it = mDevicesMap.find(aEndpoint);
if (it == mDevicesMap.end())
{
// We are always going to be given a valid endpoint. If the endpoint
// doesn't exist in our map that indicate that the cluster was not
// added on this endpoint, hence UnsupportedCluster.
return CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
}
auto & deviceInfo = it->second;
if (deviceInfo.mDeviceDirectory.empty() || deviceInfo.mRemovedOn.HasValue())
{
return aEncoder.EncodeEmptyList();
}
FabricIndex fabricIndex = aEncoder.AccessingFabricIndex();
return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
for (auto & device : deviceInfo.mDeviceDirectory)
{
ReturnErrorOnFailure(device->Encode(encoder, fabricIndex));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR EcosystemInformationServer::EncodeLocationStructAttribute(EndpointId aEndpoint, AttributeValueEncoder & aEncoder)
{
auto it = mDevicesMap.find(aEndpoint);
if (it == mDevicesMap.end())
{
// We are always going to be given a valid endpoint. If the endpoint
// doesn't exist in our map that indicate that the cluster was not
// added on this endpoint, hence UnsupportedCluster.
return CHIP_IM_GLOBAL_STATUS(UnsupportedCluster);
}
auto & deviceInfo = it->second;
if (deviceInfo.mLocationDirectory.empty() || deviceInfo.mRemovedOn.HasValue())
{
return aEncoder.EncodeEmptyList();
}
FabricIndex fabricIndex = aEncoder.AccessingFabricIndex();
return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
for (auto & [id, device] : deviceInfo.mLocationDirectory)
{
ReturnErrorOnFailure(device->Encode(encoder, id, fabricIndex));
}
return CHIP_NO_ERROR;
});
return CHIP_NO_ERROR;
}
} // namespace EcosystemInformation
} // namespace Clusters
} // namespace app
} // namespace chip
// -----------------------------------------------------------------------------
// Plugin initialization
chip::app::Clusters::EcosystemInformation::AttrAccess gAttrAccess;
void MatterEcosystemInformationPluginServerInitCallback()
{
registerAttributeAccessOverride(&gAttrAccess);
}