38
38
#include " platform/CHIPDeviceEvent.h"
39
39
#include " platform/PlatformManager.h"
40
40
#include " protocols/interaction_model/StatusCode.h"
41
+ #include < optional>
41
42
42
43
namespace chip {
43
44
namespace app {
@@ -46,21 +47,24 @@ namespace ThreadBorderRouterManagement {
46
47
47
48
using Protocols::InteractionModel::Status;
48
49
49
- static bool IsCommandOverCASESession (CommandHandlerInterface::HandlerContext & ctx)
50
+ bool ServerInstance:: IsCommandOverCASESession (CommandHandlerInterface::HandlerContext & ctx)
50
51
{
52
+ #if CONFIG_BUILD_FOR_HOST_UNIT_TEST
53
+ if (mSkipCASESessionCheck )
54
+ {
55
+ return true ;
56
+ }
57
+ #endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
51
58
Messaging::ExchangeContext * exchangeCtx = ctx.mCommandHandler .GetExchangeContext ();
52
59
return exchangeCtx && exchangeCtx->HasSessionHandle () && exchangeCtx->GetSessionHandle ()->IsSecureSession () &&
53
60
exchangeCtx->GetSessionHandle ()->AsSecureSession ()->GetSecureSessionType () == Transport::SecureSession::Type::kCASE ;
54
61
}
55
62
56
- Status ServerInstance::HandleGetDatasetRequest (bool isOverCASESession , Delegate::DatasetType type,
63
+ Status ServerInstance::HandleGetDatasetRequest (CommandHandlerInterface::HandlerContext & ctx , Delegate::DatasetType type,
57
64
Thread::OperationalDataset & dataset)
58
65
{
59
66
VerifyOrDie (mDelegate );
60
- if (!isOverCASESession)
61
- {
62
- return Status::UnsupportedAccess;
63
- }
67
+ VerifyOrReturnValue (IsCommandOverCASESession (ctx), Status::UnsupportedAccess);
64
68
65
69
CHIP_ERROR err = mDelegate ->GetDataset (dataset, type);
66
70
if (err != CHIP_NO_ERROR)
@@ -70,7 +74,7 @@ Status ServerInstance::HandleGetDatasetRequest(bool isOverCASESession, Delegate:
70
74
return Status::Success;
71
75
}
72
76
73
- Status ServerInstance::HandleSetActiveDatasetRequest (CommandHandler * commandHandler ,
77
+ Status ServerInstance::HandleSetActiveDatasetRequest (CommandHandlerInterface::HandlerContext & ctx ,
74
78
const Commands::SetActiveDatasetRequest::DecodableType & req)
75
79
{
76
80
// The SetActiveDatasetRequest command SHALL be FailSafeArmed. Upon receiving this command, the Thread BR will set its
@@ -80,7 +84,8 @@ Status ServerInstance::HandleSetActiveDatasetRequest(CommandHandler * commandHan
80
84
// reverted. If the FailSafe timer expires before the Thread BR responds, the Thread BR will respond with a timeout status and
81
85
// the active dataset should also be reverted.
82
86
VerifyOrDie (mDelegate );
83
- VerifyOrReturnValue (mFailsafeContext .IsFailSafeArmed (commandHandler->GetAccessingFabricIndex ()), Status::FailsafeRequired);
87
+ VerifyOrReturnValue (IsCommandOverCASESession (ctx), Status::UnsupportedAccess);
88
+ VerifyOrReturnValue (mFailsafeContext .IsFailSafeArmed (ctx.mCommandHandler .GetAccessingFabricIndex ()), Status::FailsafeRequired);
84
89
85
90
Thread::OperationalDataset activeDataset;
86
91
Thread::OperationalDataset currentActiveDataset;
@@ -101,17 +106,19 @@ Status ServerInstance::HandleSetActiveDatasetRequest(CommandHandler * commandHan
101
106
{
102
107
return Status::Busy;
103
108
}
104
- commandHandler-> FlushAcksRightAwayOnSlowCommand ();
105
- mAsyncCommandHandle = CommandHandler::Handle (commandHandler );
109
+ ctx. mCommandHandler . FlushAcksRightAwayOnSlowCommand ();
110
+ mAsyncCommandHandle = CommandHandler::Handle (&ctx. mCommandHandler );
106
111
mBreadcrumb = req.breadcrumb ;
107
112
mSetActiveDatasetSequenceNumber ++;
108
113
mDelegate ->SetActiveDataset (activeDataset, mSetActiveDatasetSequenceNumber , this );
109
114
return Status::Success;
110
115
}
111
116
112
- Status ServerInstance::HandleSetPendingDatasetRequest (const Commands::SetPendingDatasetRequest::DecodableType & req)
117
+ Status ServerInstance::HandleSetPendingDatasetRequest (CommandHandlerInterface::HandlerContext & ctx,
118
+ const Commands::SetPendingDatasetRequest::DecodableType & req)
113
119
{
114
120
VerifyOrDie (mDelegate );
121
+ VerifyOrReturnValue (IsCommandOverCASESession (ctx), Status::UnsupportedAccess);
115
122
if (!mDelegate ->GetPanChangeSupported ())
116
123
{
117
124
return Status::UnsupportedCommand;
@@ -143,21 +150,21 @@ void ServerInstance::InvokeCommand(HandlerContext & ctxt)
143
150
case Commands::GetActiveDatasetRequest::Id:
144
151
HandleCommand<Commands::GetActiveDatasetRequest::DecodableType>(ctxt, [this ](HandlerContext & ctx, const auto & req) {
145
152
Thread::OperationalDataset dataset;
146
- Status status = HandleGetActiveDatasetRequest (IsCommandOverCASESession ( ctx) , dataset);
153
+ Status status = HandleGetActiveDatasetRequest (ctx, dataset);
147
154
AddDatasetResponse (ctx, status, dataset);
148
155
});
149
156
break ;
150
157
case Commands::GetPendingDatasetRequest::Id:
151
158
HandleCommand<Commands::GetPendingDatasetRequest::DecodableType>(ctxt, [this ](HandlerContext & ctx, const auto & req) {
152
159
Thread::OperationalDataset dataset;
153
- Status status = HandleGetPendingDatasetRequest (IsCommandOverCASESession ( ctx) , dataset);
160
+ Status status = HandleGetPendingDatasetRequest (ctx, dataset);
154
161
AddDatasetResponse (ctx, status, dataset);
155
162
});
156
163
break ;
157
164
case Commands::SetActiveDatasetRequest::Id:
158
165
HandleCommand<Commands::SetActiveDatasetRequest::DecodableType>(ctxt, [this ](HandlerContext & ctx, const auto & req) {
159
166
mPath = ctx.mRequestPath ;
160
- Status status = HandleSetActiveDatasetRequest (& ctx. mCommandHandler , req);
167
+ Status status = HandleSetActiveDatasetRequest (ctx, req);
161
168
if (status != Status::Success)
162
169
{
163
170
// If status is not Success, we should immediately report the status. Otherwise the async work will report the
@@ -168,7 +175,8 @@ void ServerInstance::InvokeCommand(HandlerContext & ctxt)
168
175
break ;
169
176
case Commands::SetPendingDatasetRequest::Id:
170
177
HandleCommand<Commands::SetPendingDatasetRequest::DecodableType>(ctxt, [this ](HandlerContext & ctx, const auto & req) {
171
- ctx.mCommandHandler .AddStatus (ctx.mRequestPath , HandleSetPendingDatasetRequest (req));
178
+ Status status = HandleSetPendingDatasetRequest (ctx, req);
179
+ ctx.mCommandHandler .AddStatus (ctx.mRequestPath , status);
172
180
});
173
181
break ;
174
182
default :
@@ -199,16 +207,28 @@ CHIP_ERROR ServerInstance::ReadBorderAgentID(MutableByteSpan & outBorderAgentId)
199
207
return CHIP_NO_ERROR;
200
208
}
201
209
202
- Optional <uint64_t > ServerInstance::ReadActiveDatasetTimestamp ()
210
+ std::optional <uint64_t > ServerInstance::ReadActiveDatasetTimestamp ()
203
211
{
204
212
uint64_t activeDatasetTimestampValue = 0 ;
205
213
Thread::OperationalDataset activeDataset;
206
214
if ((mDelegate ->GetDataset (activeDataset, Delegate::DatasetType::kActive ) == CHIP_NO_ERROR) &&
207
215
(activeDataset.GetActiveTimestamp (activeDatasetTimestampValue) == CHIP_NO_ERROR))
208
216
{
209
- return MakeOptional (activeDatasetTimestampValue);
217
+ return std::make_optional (activeDatasetTimestampValue);
218
+ }
219
+ return std::nullopt;
220
+ }
221
+
222
+ std::optional<uint64_t > ServerInstance::ReadPendingDatasetTimestamp ()
223
+ {
224
+ uint64_t pendingDatasetTimestampValue = 0 ;
225
+ Thread::OperationalDataset pendingDataset;
226
+ if ((mDelegate ->GetDataset (pendingDataset, Delegate::DatasetType::kPending ) == CHIP_NO_ERROR) &&
227
+ (pendingDataset.GetActiveTimestamp (pendingDatasetTimestampValue) == CHIP_NO_ERROR))
228
+ {
229
+ return std::make_optional (pendingDatasetTimestampValue);
210
230
}
211
- return NullOptional ;
231
+ return std::nullopt ;
212
232
}
213
233
214
234
CHIP_ERROR ServerInstance::Read (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
@@ -259,9 +279,13 @@ CHIP_ERROR ServerInstance::Read(const ConcreteReadAttributePath & aPath, Attribu
259
279
break ;
260
280
}
261
281
case Attributes::ActiveDatasetTimestamp::Id: {
262
- Optional<uint64_t > activeDatasetTimestamp = ReadActiveDatasetTimestamp ();
263
- status = activeDatasetTimestamp.HasValue () ? aEncoder.Encode (DataModel::MakeNullable (activeDatasetTimestamp.Value ()))
264
- : aEncoder.EncodeNull ();
282
+ std::optional<uint64_t > activeDatasetTimestamp = ReadActiveDatasetTimestamp ();
283
+ status = activeDatasetTimestamp.has_value () ? aEncoder.Encode (activeDatasetTimestamp.value ()) : aEncoder.EncodeNull ();
284
+ break ;
285
+ }
286
+ case Attributes::PendingDatasetTimestamp::Id: {
287
+ std::optional<uint64_t > pendingDatasetTimestamp = ReadPendingDatasetTimestamp ();
288
+ status = pendingDatasetTimestamp.has_value () ? aEncoder.Encode (pendingDatasetTimestamp.value ()) : aEncoder.EncodeNull ();
265
289
break ;
266
290
}
267
291
default :
0 commit comments