forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread-border-router-management-server.cpp
390 lines (363 loc) · 15.1 KB
/
thread-border-router-management-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
*
* 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 "thread-border-router-management-server.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/AttributeAccessInterfaceRegistry.h"
#include "app/AttributeValueEncoder.h"
#include "app/CommandHandler.h"
#include "app/CommandHandlerInterface.h"
#include "app/CommandHandlerInterfaceRegistry.h"
#include "app/InteractionModelEngine.h"
#include "app/MessageDef/StatusIB.h"
#include "app/clusters/general-commissioning-server/general-commissioning-server.h"
#include "app/data-model/Nullable.h"
#include "lib/core/CHIPError.h"
#include "lib/core/Optional.h"
#include "lib/support/CodeUtils.h"
#include "lib/support/Span.h"
#include "lib/support/ThreadOperationalDataset.h"
#include "platform/CHIPDeviceEvent.h"
#include "platform/PlatformManager.h"
#include "protocols/interaction_model/StatusCode.h"
#include <optional>
namespace chip {
namespace app {
namespace Clusters {
namespace ThreadBorderRouterManagement {
using Protocols::InteractionModel::Status;
bool ServerInstance::IsCommandOverCASESession(CommandHandlerInterface::HandlerContext & ctx)
{
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
if (mSkipCASESessionCheck)
{
return true;
}
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
Messaging::ExchangeContext * exchangeCtx = ctx.mCommandHandler.GetExchangeContext();
return exchangeCtx && exchangeCtx->HasSessionHandle() && exchangeCtx->GetSessionHandle()->IsSecureSession() &&
exchangeCtx->GetSessionHandle()->AsSecureSession()->GetSecureSessionType() == Transport::SecureSession::Type::kCASE;
}
Status ServerInstance::HandleGetDatasetRequest(CommandHandlerInterface::HandlerContext & ctx, Delegate::DatasetType type,
Thread::OperationalDataset & dataset)
{
VerifyOrDie(mDelegate);
VerifyOrReturnValue(IsCommandOverCASESession(ctx), Status::UnsupportedAccess);
CHIP_ERROR err = mDelegate->GetDataset(dataset, type);
if (err != CHIP_NO_ERROR)
{
return err == CHIP_IM_GLOBAL_STATUS(NotFound) ? StatusIB(err).mStatus : Status::Failure;
}
return Status::Success;
}
Status ServerInstance::HandleSetActiveDatasetRequest(CommandHandlerInterface::HandlerContext & ctx,
const Commands::SetActiveDatasetRequest::DecodableType & req)
{
// The SetActiveDatasetRequest command SHALL be FailSafeArmed. Upon receiving this command, the Thread BR will set its
// active dataset. If the dataset is set successfully, OnActivateDatasetComplete will be called with CHIP_NO_ERROR, prompting
// the Thread BR to respond with a success status. If an error occurs while setting the active dataset, the Thread BR should
// respond with a failure status. In this case, when the FailSafe timer expires, the active dataset set by this command will be
// reverted. If the FailSafe timer expires before the Thread BR responds, the Thread BR will respond with a timeout status and
// the active dataset should also be reverted.
VerifyOrDie(mDelegate);
VerifyOrReturnValue(IsCommandOverCASESession(ctx), Status::UnsupportedAccess);
VerifyOrReturnValue(mFailsafeContext.IsFailSafeArmed(ctx.mCommandHandler.GetAccessingFabricIndex()), Status::FailsafeRequired);
Thread::OperationalDataset activeDataset;
Thread::OperationalDataset currentActiveDataset;
uint64_t currentActiveDatasetTimestamp = 0;
// If any of the parameters in the ActiveDataset is invalid, the command SHALL fail with a status code
// of INVALID_COMMAND.
VerifyOrReturnValue(activeDataset.Init(req.activeDataset) == CHIP_NO_ERROR, Status::InvalidCommand);
// If this command is invoked when the ActiveDatasetTimestamp attribute is not null, the command SHALL
// fail with a status code of INVALID_IN_STATE.
if ((mDelegate->GetDataset(currentActiveDataset, Delegate::DatasetType::kActive) == CHIP_NO_ERROR) &&
(currentActiveDataset.GetActiveTimestamp(currentActiveDatasetTimestamp) == CHIP_NO_ERROR))
{
return Status::InvalidInState;
}
// If there is a back end command process, return status BUSY.
if (mAsyncCommandHandle.Get())
{
return Status::Busy;
}
ctx.mCommandHandler.FlushAcksRightAwayOnSlowCommand();
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
mBreadcrumb = req.breadcrumb;
mSetActiveDatasetSequenceNumber++;
mDelegate->SetActiveDataset(activeDataset, mSetActiveDatasetSequenceNumber, this);
return Status::Success;
}
Status ServerInstance::HandleSetPendingDatasetRequest(CommandHandlerInterface::HandlerContext & ctx,
const Commands::SetPendingDatasetRequest::DecodableType & req)
{
VerifyOrDie(mDelegate);
VerifyOrReturnValue(IsCommandOverCASESession(ctx), Status::UnsupportedAccess);
if (!mDelegate->GetPanChangeSupported())
{
return Status::UnsupportedCommand;
}
Thread::OperationalDataset pendingDataset;
// If any of the parameters in the PendingDataset is invalid, the command SHALL fail with a status code
// of INVALID_COMMAND.
ReturnErrorCodeIf(pendingDataset.Init(req.pendingDataset) != CHIP_NO_ERROR, Status::InvalidCommand);
CHIP_ERROR err = mDelegate->SetPendingDataset(pendingDataset);
return StatusIB(err).mStatus;
}
void AddDatasetResponse(CommandHandlerInterface::HandlerContext & ctx, Status status, const Thread::OperationalDataset & dataset)
{
if (status != Status::Success)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
return;
}
Commands::DatasetResponse::Type response;
response.dataset = dataset.AsByteSpan();
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
}
void ServerInstance::InvokeCommand(HandlerContext & ctxt)
{
switch (ctxt.mRequestPath.mCommandId)
{
case Commands::GetActiveDatasetRequest::Id:
HandleCommand<Commands::GetActiveDatasetRequest::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
Thread::OperationalDataset dataset;
Status status = HandleGetActiveDatasetRequest(ctx, dataset);
AddDatasetResponse(ctx, status, dataset);
});
break;
case Commands::GetPendingDatasetRequest::Id:
HandleCommand<Commands::GetPendingDatasetRequest::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
Thread::OperationalDataset dataset;
Status status = HandleGetPendingDatasetRequest(ctx, dataset);
AddDatasetResponse(ctx, status, dataset);
});
break;
case Commands::SetActiveDatasetRequest::Id:
HandleCommand<Commands::SetActiveDatasetRequest::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
mPath = ctx.mRequestPath;
Status status = HandleSetActiveDatasetRequest(ctx, req);
if (status != Status::Success)
{
// If status is not Success, we should immediately report the status. Otherwise the async work will report the
// status to the client.
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
});
break;
case Commands::SetPendingDatasetRequest::Id:
HandleCommand<Commands::SetPendingDatasetRequest::DecodableType>(ctxt, [this](HandlerContext & ctx, const auto & req) {
Status status = HandleSetPendingDatasetRequest(ctx, req);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
});
break;
default:
break;
}
}
void ServerInstance::ReadFeatureMap(BitFlags<Feature> & outFeatureMap)
{
if (mDelegate->GetPanChangeSupported())
{
outFeatureMap.Set(Feature::kPANChange);
}
}
CHIP_ERROR ServerInstance::ReadBorderRouterName(MutableCharSpan & outBorderRouterName)
{
mDelegate->GetBorderRouterName(outBorderRouterName);
VerifyOrReturnValue(outBorderRouterName.size() <= kBorderRouterNameMaxLength, CHIP_IM_GLOBAL_STATUS(Failure));
return CHIP_NO_ERROR;
}
CHIP_ERROR ServerInstance::ReadBorderAgentID(MutableByteSpan & outBorderAgentId)
{
VerifyOrReturnValue((mDelegate->GetBorderAgentId(outBorderAgentId) == CHIP_NO_ERROR) &&
(outBorderAgentId.size() == kBorderAgentIdLength),
CHIP_IM_GLOBAL_STATUS(Failure));
return CHIP_NO_ERROR;
}
std::optional<uint64_t> ServerInstance::ReadActiveDatasetTimestamp()
{
uint64_t activeDatasetTimestampValue = 0;
Thread::OperationalDataset activeDataset;
if ((mDelegate->GetDataset(activeDataset, Delegate::DatasetType::kActive) == CHIP_NO_ERROR) &&
(activeDataset.GetActiveTimestamp(activeDatasetTimestampValue) == CHIP_NO_ERROR))
{
return std::make_optional(activeDatasetTimestampValue);
}
return std::nullopt;
}
std::optional<uint64_t> ServerInstance::ReadPendingDatasetTimestamp()
{
uint64_t pendingDatasetTimestampValue = 0;
Thread::OperationalDataset pendingDataset;
if ((mDelegate->GetDataset(pendingDataset, Delegate::DatasetType::kPending) == CHIP_NO_ERROR) &&
(pendingDataset.GetActiveTimestamp(pendingDatasetTimestampValue) == CHIP_NO_ERROR))
{
return std::make_optional(pendingDatasetTimestampValue);
}
return std::nullopt;
}
CHIP_ERROR ServerInstance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
if (aPath.mClusterId != ThreadBorderRouterManagement::Id)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
VerifyOrDie(mDelegate);
CHIP_ERROR status = CHIP_NO_ERROR;
switch (aPath.mAttributeId)
{
case Globals::Attributes::FeatureMap::Id: {
BitFlags<Feature> featureMap;
ReadFeatureMap(featureMap);
status = aEncoder.Encode(featureMap);
break;
}
case Attributes::BorderRouterName::Id: {
char borderRouterNameBuf[kBorderRouterNameMaxLength] = { 0 };
MutableCharSpan borderRouterName(borderRouterNameBuf);
status = ReadBorderRouterName(borderRouterName);
// If there are any internal errors, the status will be returned and the client will get an error report.
if (status == CHIP_NO_ERROR)
{
status = aEncoder.Encode(borderRouterName);
}
break;
}
case Attributes::BorderAgentID::Id: {
uint8_t borderAgentIDBuf[kBorderAgentIdLength] = { 0 };
MutableByteSpan borderAgentID(borderAgentIDBuf);
status = ReadBorderAgentID(borderAgentID);
if (status == CHIP_NO_ERROR)
{
status = aEncoder.Encode(borderAgentID);
}
break;
}
case Attributes::ThreadVersion::Id: {
uint16_t threadVersion = mDelegate->GetThreadVersion();
status = aEncoder.Encode(threadVersion);
break;
}
case Attributes::InterfaceEnabled::Id: {
bool interfaceEnabled = mDelegate->GetInterfaceEnabled();
status = aEncoder.Encode(interfaceEnabled);
break;
}
case Attributes::ActiveDatasetTimestamp::Id: {
std::optional<uint64_t> activeDatasetTimestamp = ReadActiveDatasetTimestamp();
status = activeDatasetTimestamp.has_value() ? aEncoder.Encode(activeDatasetTimestamp.value()) : aEncoder.EncodeNull();
break;
}
case Attributes::PendingDatasetTimestamp::Id: {
std::optional<uint64_t> pendingDatasetTimestamp = ReadPendingDatasetTimestamp();
status = pendingDatasetTimestamp.has_value() ? aEncoder.Encode(pendingDatasetTimestamp.value()) : aEncoder.EncodeNull();
break;
}
default:
break;
}
if (status == CHIP_ERROR_NO_MEMORY)
{
// If the status returned by Encode function is CHIP_ERROR_NO_MEMORY, map it to IM status ResourceExhausted
return CHIP_IM_GLOBAL_STATUS(ResourceExhausted);
}
if (status != CHIP_NO_ERROR)
{
return CHIP_IM_GLOBAL_STATUS(Failure);
}
return CHIP_NO_ERROR;
}
void ServerInstance::CommitSavedBreadcrumb()
{
if (mBreadcrumb.HasValue())
{
GeneralCommissioning::SetBreadcrumb(mBreadcrumb.Value());
}
mBreadcrumb.ClearValue();
}
void ServerInstance::OnActivateDatasetComplete(uint32_t sequenceNum, CHIP_ERROR error)
{
auto commandHandleRef = std::move(mAsyncCommandHandle);
auto commandHandle = commandHandleRef.Get();
if (commandHandle == nullptr)
{
return;
}
if (mSetActiveDatasetSequenceNumber != sequenceNum)
{
// Previous SetActiveDatasetRequest was handled.
return;
}
if (error == CHIP_NO_ERROR)
{
// TODO: SPEC Issue #10022
CommitSavedBreadcrumb();
}
else
{
ChipLogError(Zcl, "Failed on activating the active dataset for Thread BR: %" CHIP_ERROR_FORMAT, error.Format());
}
commandHandle->AddStatus(mPath, StatusIB(error).mStatus);
}
void ServerInstance::ReportAttributeChanged(AttributeId attributeId)
{
MatterReportingAttributeChangeCallback(mServerEndpointId, Id, attributeId);
}
void ServerInstance::OnFailSafeTimerExpired()
{
if (mDelegate)
{
mDelegate->RevertActiveDataset();
}
auto commandHandleRef = std::move(mAsyncCommandHandle);
auto commandHandle = commandHandleRef.Get();
if (commandHandle == nullptr)
{
return;
}
commandHandle->AddStatus(mPath, Status::Timeout);
}
void ServerInstance::OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
ServerInstance * _this = reinterpret_cast<ServerInstance *>(arg);
if (event->Type == DeviceLayer::DeviceEventType::kFailSafeTimerExpired)
{
_this->OnFailSafeTimerExpired();
}
else if (event->Type == DeviceLayer::DeviceEventType::kCommissioningComplete)
{
_this->mDelegate->CommitActiveDataset();
}
}
CHIP_ERROR ServerInstance::Init()
{
ReturnErrorCodeIf(!mDelegate, CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::RegisterCommandHandler(this));
VerifyOrReturnError(registerAttributeAccessOverride(this), CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(OnPlatformEventHandler, reinterpret_cast<intptr_t>(this)));
return mDelegate->Init(this);
}
} // namespace ThreadBorderRouterManagement
} // namespace Clusters
} // namespace app
} // namespace chip
void MatterThreadBorderRouterManagementPluginServerInitCallback()
{
// Nothing to do, the server init routine will be done in Instance::Init()
}