26
26
#include " app/CommandHandler.h"
27
27
#include " app/CommandHandlerInterface.h"
28
28
#include " app/InteractionModelEngine.h"
29
+ #include " app/MessageDef/StatusIB.h"
29
30
#include " app/clusters/general-commissioning-server/general-commissioning-server.h"
30
31
#include " app/data-model/Nullable.h"
31
32
#include " app/server/Server.h"
@@ -46,25 +47,25 @@ using Protocols::InteractionModel::Status;
46
47
47
48
static bool CheckOverCASESession (CommandHandlerInterface::HandlerContext & ctx)
48
49
{
49
- chip:: Messaging::ExchangeContext * exchangeCtx = ctx.mCommandHandler .GetExchangeContext ();
50
+ Messaging::ExchangeContext * exchangeCtx = ctx.mCommandHandler .GetExchangeContext ();
50
51
if (!exchangeCtx || !exchangeCtx->HasSessionHandle () || !exchangeCtx->GetSessionHandle ()->IsSecureSession () ||
51
52
exchangeCtx->GetSessionHandle ()->AsSecureSession ()->GetSecureSessionType () != Transport::SecureSession::Type::kCASE )
52
53
{
53
54
ChipLogError (Zcl, " This command MUST be over a valid CASE session" );
54
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel:: Status::UnsupportedAccess);
55
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::UnsupportedAccess);
55
56
return false ;
56
57
}
57
58
return true ;
58
59
}
59
60
60
61
static bool CheckFailSafeArmed (CommandHandlerInterface::HandlerContext & ctx)
61
62
{
62
- auto & failSafeContext = chip:: Server::GetInstance ().GetFailSafeContext ();
63
+ auto & failSafeContext = Server::GetInstance ().GetFailSafeContext ();
63
64
if (failSafeContext.IsFailSafeArmed (ctx.mCommandHandler .GetAccessingFabricIndex ()))
64
65
{
65
66
return true ;
66
67
}
67
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel:: Status::FailsafeRequired);
68
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::FailsafeRequired);
68
69
return false ;
69
70
}
70
71
@@ -73,64 +74,42 @@ static bool CheckDelegate(CommandHandlerInterface::HandlerContext & ctx, Delegat
73
74
if (!delegate)
74
75
{
75
76
ChipLogError (Zcl, " Thread Border Router Management server not initialized" );
76
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel:: Status::InvalidInState);
77
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::InvalidInState);
77
78
}
78
79
return delegate;
79
80
}
80
81
81
- void ServerInstance::HandleGetActiveDatasetRequest (HandlerContext & ctx,
82
- const Commands::GetActiveDatasetRequest::DecodableType & req)
83
- {
84
- VerifyOrReturn (CheckOverCASESession (ctx));
85
- VerifyOrReturn (CheckDelegate (ctx, mDelegate ));
86
-
87
- Commands::DatasetResponse::Type response;
88
- Thread::OperationalDataset activeDataset;
89
- CHIP_ERROR err = mDelegate ->GetActiveDataset (activeDataset);
90
- if (err == CHIP_ERROR_NOT_FOUND)
91
- {
92
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::NotFound);
93
- }
94
- else if (err != CHIP_NO_ERROR)
95
- {
96
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::Failure);
97
- }
98
- ReturnOnFailure (err);
99
- response.dataset = activeDataset.AsByteSpan ();
100
- ctx.mCommandHandler .AddResponse (ctx.mRequestPath , response);
101
- }
102
-
103
- void ServerInstance::HandleGetPendingDatasetRequest (HandlerContext & ctx,
104
- const Commands::GetPendingDatasetRequest::DecodableType & req)
82
+ void ServerInstance::HandleGetDatasetRequest (HandlerContext & ctx, Delegate::DatasetType type)
105
83
{
106
84
VerifyOrReturn (CheckOverCASESession (ctx));
107
85
VerifyOrReturn (CheckDelegate (ctx, mDelegate ));
108
86
109
87
Commands::DatasetResponse::Type response;
110
- Thread::OperationalDataset pendingDataset ;
111
- CHIP_ERROR err = mDelegate ->GetPendingDataset (pendingDataset );
112
- if (err == CHIP_ERROR_NOT_FOUND )
88
+ Thread::OperationalDataset dataset ;
89
+ CHIP_ERROR err = mDelegate ->GetDataset (dataset, type );
90
+ if (err != CHIP_NO_ERROR )
113
91
{
114
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::NotFound);
115
- }
116
- else if (err != CHIP_NO_ERROR)
117
- {
118
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::Failure);
92
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , StatusIB (err).mStatus );
93
+ return ;
119
94
}
120
- ReturnOnFailure (err);
121
- response.dataset = pendingDataset.AsByteSpan ();
95
+ response.dataset = dataset.AsByteSpan ();
122
96
ctx.mCommandHandler .AddResponse (ctx.mRequestPath , response);
123
97
}
124
98
125
99
void ServerInstance::HandleSetActiveDatasetRequest (HandlerContext & ctx,
126
100
const Commands::SetActiveDatasetRequest::DecodableType & req)
127
101
{
102
+ // The SetActiveDatasetRequest command SHALL be FailSafeArmed. Upon receiving this command, the Thread BR will set its
103
+ // active dataset. If the dataset is set successfully, OnActivateDatasetComplete will be called with CHIP_NO_ERROR, prompting
104
+ // the Thread BR to respond with a success status and disarm the FailSafe timer. If an error occurs while setting the active
105
+ // dataset, the Thread BR should respond with a failure status. In this case, when the FailSafe timer expires, the active
106
+ // dataset set by this command will be reverted. If the FailSafe timer expires before the Thread BR responds, the Thread BR will
107
+ // respond with a timeout status and the active dataset should also be reverted.
128
108
VerifyOrReturn (CheckFailSafeArmed (ctx));
129
109
VerifyOrReturn (CheckDelegate (ctx, mDelegate ));
130
110
if (mAsyncCommandHandle .Get () != nullptr )
131
111
{
132
112
ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::Busy);
133
- ctx.SetCommandHandled ();
134
113
return ;
135
114
}
136
115
@@ -145,7 +124,7 @@ void ServerInstance::HandleSetActiveDatasetRequest(HandlerContext & ctx,
145
124
return ;
146
125
}
147
126
148
- if (mDelegate ->GetActiveDataset (currentActiveDataset) == CHIP_NO_ERROR &&
127
+ if (mDelegate ->GetDataset (currentActiveDataset, Delegate::DatasetType:: kActive ) == CHIP_NO_ERROR &&
149
128
currentActiveDataset.GetActiveTimestamp (currentActiveDatasetTimestamp) == CHIP_NO_ERROR)
150
129
{
151
130
// If this command is invoked when the ActiveDatasetTimestamp attribute is not null, the command SHALL
@@ -172,7 +151,7 @@ void ServerInstance::HandleSetPendingDatasetRequest(HandlerContext & ctx,
172
151
bool panChangeSupported;
173
152
if (mDelegate ->GetPanChangeSupported (panChangeSupported) != CHIP_NO_ERROR || !panChangeSupported)
174
153
{
175
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel:: Status::UnsupportedCommand);
154
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::UnsupportedCommand);
176
155
return ;
177
156
}
178
157
Thread::OperationalDataset pendingDataset;
@@ -183,14 +162,8 @@ void ServerInstance::HandleSetPendingDatasetRequest(HandlerContext & ctx,
183
162
ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Status::InvalidCommand);
184
163
return ;
185
164
}
186
- if (mDelegate ->SetPendingDataset (pendingDataset) != CHIP_NO_ERROR)
187
- {
188
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel::Status::Failure);
189
- }
190
- else
191
- {
192
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , Protocols::InteractionModel::Status::Success);
193
- }
165
+ CHIP_ERROR err = mDelegate ->SetPendingDataset (pendingDataset);
166
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , StatusIB (err).mStatus );
194
167
}
195
168
196
169
void ServerInstance::InvokeCommand (HandlerContext & ctx)
@@ -235,15 +208,15 @@ CHIP_ERROR ServerInstance::ReadBorderRouterName(AttributeValueEncoder & aEncoder
235
208
char borderRouterName[kBorderRouterNameMaxLength ];
236
209
MutableCharSpan borderRouterNameSpan (borderRouterName);
237
210
ReturnErrorOnFailure (mDelegate ->GetBorderRouterName (borderRouterNameSpan));
238
- return aEncoder.Encode (chip::CharSpan (borderRouterName, borderRouterNameSpan. size ()) );
211
+ return aEncoder.Encode (borderRouterNameSpan);
239
212
}
240
213
241
214
CHIP_ERROR ServerInstance::ReadBorderAgentID (AttributeValueEncoder & aEncoder)
242
215
{
243
216
uint8_t borderAgentId[kBorderAgentIdLength ];
244
217
MutableByteSpan borderAgentIdSpan (borderAgentId);
245
218
ReturnErrorOnFailure (mDelegate ->GetBorderAgentId (borderAgentIdSpan));
246
- return aEncoder.Encode (chip::ByteSpan (borderAgentId) );
219
+ return aEncoder.Encode (borderAgentIdSpan );
247
220
}
248
221
249
222
CHIP_ERROR ServerInstance::ReadThreadVersion (AttributeValueEncoder & aEncoder)
@@ -264,12 +237,12 @@ CHIP_ERROR ServerInstance::ReadActiveDatasetTimestamp(AttributeValueEncoder & aE
264
237
{
265
238
Thread::OperationalDataset activeDataset;
266
239
uint64_t activeDatasetTimestamp;
267
- if (mDelegate ->GetActiveDataset (activeDataset) == CHIP_NO_ERROR &&
240
+ if (mDelegate ->GetDataset (activeDataset, Delegate::DatasetType:: kActive ) == CHIP_NO_ERROR &&
268
241
activeDataset.GetActiveTimestamp (activeDatasetTimestamp) == CHIP_NO_ERROR)
269
242
{
270
243
return aEncoder.Encode (DataModel::MakeNullable (activeDatasetTimestamp));
271
244
}
272
- return aEncoder.Encode (DataModel::Nullable< uint64_t >() );
245
+ return aEncoder.EncodeNull ( );
273
246
}
274
247
275
248
CHIP_ERROR ServerInstance::Read (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
@@ -330,29 +303,29 @@ void ServerInstance::OnActivateDatasetComplete(CHIP_ERROR error)
330
303
if (error == CHIP_NO_ERROR)
331
304
{
332
305
// The successful completion of the activation process SHALL disarm the fail-safe timer.
333
- auto & failSafeContext = chip:: Server::GetInstance ().GetFailSafeContext ();
306
+ auto & failSafeContext = Server::GetInstance ().GetFailSafeContext ();
334
307
failSafeContext.DisarmFailSafe ();
335
308
CommitSavedBreadcrumb ();
336
309
}
337
310
else
338
311
{
339
312
ChipLogError (Zcl, " Failed on activating the active dataset for Thread BR: %" CHIP_ERROR_FORMAT, error.Format ());
340
313
}
341
- commandHandle->AddStatus (mPath , error == CHIP_NO_ERROR ? Status::Success : Status::Failure );
314
+ commandHandle->AddStatus (mPath , StatusIB ( error). mStatus );
342
315
}
343
316
344
317
void ServerInstance::OnFailSafeTimerExpired ()
345
318
{
319
+ if (mDelegate )
320
+ {
321
+ mDelegate ->RevertActiveDataset ();
322
+ }
346
323
auto commandHandleRef = std::move (mAsyncCommandHandle );
347
324
auto commandHandle = commandHandleRef.Get ();
348
325
if (commandHandle == nullptr )
349
326
{
350
327
return ;
351
328
}
352
- if (mDelegate )
353
- {
354
- mDelegate ->RevertActiveDataset ();
355
- }
356
329
commandHandle->AddStatus (mPath , Status::Timeout);
357
330
}
358
331
0 commit comments