forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral-diagnostics-server.cpp
529 lines (444 loc) · 19.5 KB
/
general-diagnostics-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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
*
* Copyright (c) 2021 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 "general-diagnostics-server.h"
#include <stdint.h>
#include <string.h>
#include <app/util/config.h>
#include <app-common/zap-generated/attributes/Accessors.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/AttributeAccessInterface.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandler.h>
#include <app/CommandHandlerInterface.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/EventLogging.h>
#include <app/reporting/reporting.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <lib/support/ScopedBuffer.h>
#include <platform/ConnectivityManager.h>
#include <platform/DiagnosticDataProvider.h>
#include <zap-generated/gen_config.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::GeneralDiagnostics;
using namespace chip::app::Clusters::GeneralDiagnostics::Attributes;
using namespace chip::DeviceLayer;
using chip::DeviceLayer::ConnectivityMgr;
using chip::DeviceLayer::DiagnosticDataProvider;
using chip::DeviceLayer::GetDiagnosticDataProvider;
using chip::Protocols::InteractionModel::Status;
namespace {
constexpr uint8_t kCurrentClusterRevision = 2;
bool IsTestEventTriggerEnabled()
{
auto * triggerDelegate = chip::Server::GetInstance().GetTestEventTriggerDelegate();
if (triggerDelegate == nullptr)
{
return false;
}
uint8_t zeroByteSpanData[TestEventTriggerDelegate::kEnableKeyLength] = { 0 };
if (triggerDelegate->DoesEnableKeyMatch(ByteSpan(zeroByteSpanData)))
{
return false;
}
return true;
}
bool IsByteSpanAllZeros(const ByteSpan & byteSpan)
{
for (unsigned char it : byteSpan)
{
if (it != 0)
{
return false;
}
}
return true;
}
void ReportAttributeOnAllEndpoints(AttributeId attribute)
{
for (auto endpoint : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
MatterReportingAttributeChangeCallback(endpoint, GeneralDiagnostics::Id, attribute);
}
}
TestEventTriggerDelegate * GetTriggerDelegateOnMatchingKey(ByteSpan enableKey)
{
if (enableKey.size() != TestEventTriggerDelegate::kEnableKeyLength)
{
return nullptr;
}
if (IsByteSpanAllZeros(enableKey))
{
return nullptr;
}
auto * triggerDelegate = chip::Server::GetInstance().GetTestEventTriggerDelegate();
if (triggerDelegate == nullptr || !triggerDelegate->DoesEnableKeyMatch(enableKey))
{
return nullptr;
}
return triggerDelegate;
}
class GeneralDiagnosticsGlobalInstance : public AttributeAccessInterface,
public CommandHandlerInterface,
public DeviceLayer::ConnectivityManagerDelegate
{
public:
// Register for the GeneralDiagnostics cluster on all endpoints.
GeneralDiagnosticsGlobalInstance() :
AttributeAccessInterface(Optional<EndpointId>::Missing(), GeneralDiagnostics::Id),
CommandHandlerInterface(Optional<EndpointId>::Missing(), GeneralDiagnostics::Id)
{}
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
void InvokeCommand(HandlerContext & handlerContext) override;
private:
template <typename T>
CHIP_ERROR ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), AttributeValueEncoder & aEncoder);
template <typename T>
CHIP_ERROR ReadListIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &), AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadNetworkInterfaces(AttributeValueEncoder & aEncoder);
void HandleTestEventTrigger(HandlerContext & ctx, const Commands::TestEventTrigger::DecodableType & commandData);
void HandleTimeSnapshot(HandlerContext & ctx, const Commands::TimeSnapshot::DecodableType & commandData);
#ifdef GENERAL_DIAGNOSTICS_ENABLE_PAYLOAD_TEST_REQUEST_CMD
void HandlePayloadTestRequest(HandlerContext & ctx, const Commands::PayloadTestRequest::DecodableType & commandData);
#endif
// Gets called when any network interface on the Node is updated.
void OnNetworkInfoChanged() override
{
ChipLogDetail(Zcl, "GeneralDiagnosticsDelegate: OnNetworkInfoChanged");
ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::NetworkInterfaces::Id);
}
};
CHIP_ERROR GeneralDiagnosticsGlobalInstance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
if (aPath.mClusterId != GeneralDiagnostics::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}
switch (aPath.mAttributeId)
{
case NetworkInterfaces::Id: {
return ReadNetworkInterfaces(aEncoder);
}
case ActiveHardwareFaults::Id: {
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveHardwareFaults, aEncoder);
}
case ActiveRadioFaults::Id: {
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveRadioFaults, aEncoder);
}
case ActiveNetworkFaults::Id: {
return ReadListIfSupported(&DiagnosticDataProvider::GetActiveNetworkFaults, aEncoder);
}
case RebootCount::Id: {
return ReadIfSupported(&DiagnosticDataProvider::GetRebootCount, aEncoder);
}
case UpTime::Id: {
System::Clock::Seconds64 system_time_seconds =
std::chrono::duration_cast<System::Clock::Seconds64>(Server::GetInstance().TimeSinceInit());
return aEncoder.Encode(static_cast<uint64_t>(system_time_seconds.count()));
}
case TotalOperationalHours::Id: {
return ReadIfSupported(&DiagnosticDataProvider::GetTotalOperationalHours, aEncoder);
}
case BootReason::Id: {
return ReadIfSupported(&DiagnosticDataProvider::GetBootReason, aEncoder);
}
case TestEventTriggersEnabled::Id: {
bool isTestEventTriggersEnabled = IsTestEventTriggerEnabled();
return aEncoder.Encode(isTestEventTriggersEnabled);
}
// Note: Attribute ID 0x0009 was removed (#30002).
case FeatureMap::Id: {
uint32_t features = 0;
#if CHIP_CONFIG_MAX_PATHS_PER_INVOKE > 1
features |= to_underlying(Clusters::GeneralDiagnostics::Feature::kDataModelTest);
#endif // CHIP_CONFIG_MAX_PATHS_PER_INVOKE > 1
return aEncoder.Encode(features);
}
case ClusterRevision::Id: {
return aEncoder.Encode(kCurrentClusterRevision);
}
}
return CHIP_NO_ERROR;
}
void GeneralDiagnosticsGlobalInstance::InvokeCommand(HandlerContext & handlerContext)
{
switch (handlerContext.mRequestPath.mCommandId)
{
case Commands::TestEventTrigger::Id:
CommandHandlerInterface::HandleCommand<Commands::TestEventTrigger::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleTestEventTrigger(ctx, commandData); });
break;
case Commands::TimeSnapshot::Id:
CommandHandlerInterface::HandleCommand<Commands::TimeSnapshot::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleTimeSnapshot(ctx, commandData); });
break;
#ifdef GENERAL_DIAGNOSTICS_ENABLE_PAYLOAD_TEST_REQUEST_CMD
case Commands::PayloadTestRequest::Id:
CommandHandlerInterface::HandleCommand<Commands::PayloadTestRequest::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandlePayloadTestRequest(ctx, commandData); });
break;
#endif
}
}
template <typename T>
CHIP_ERROR GeneralDiagnosticsGlobalInstance::ReadIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &),
AttributeValueEncoder & aEncoder)
{
T data;
CHIP_ERROR err = (GetDiagnosticDataProvider().*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = {};
}
else if (err != CHIP_NO_ERROR)
{
return err;
}
return aEncoder.Encode(data);
}
template <typename T>
CHIP_ERROR GeneralDiagnosticsGlobalInstance::ReadListIfSupported(CHIP_ERROR (DiagnosticDataProvider::*getter)(T &),
AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err = CHIP_NO_ERROR;
T faultList;
if ((GetDiagnosticDataProvider().*getter)(faultList) == CHIP_NO_ERROR)
{
err = aEncoder.EncodeList([&faultList](const auto & encoder) -> CHIP_ERROR {
for (auto fault : faultList)
{
ReturnErrorOnFailure(encoder.Encode(fault));
}
return CHIP_NO_ERROR;
});
}
else
{
err = aEncoder.EncodeEmptyList();
}
return err;
}
CHIP_ERROR GeneralDiagnosticsGlobalInstance::ReadNetworkInterfaces(AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err = CHIP_NO_ERROR;
DeviceLayer::NetworkInterface * netifs;
if (DeviceLayer::GetDiagnosticDataProvider().GetNetworkInterfaces(&netifs) == CHIP_NO_ERROR)
{
err = aEncoder.EncodeList([&netifs](const auto & encoder) -> CHIP_ERROR {
for (DeviceLayer::NetworkInterface * ifp = netifs; ifp != nullptr; ifp = ifp->Next)
{
ReturnErrorOnFailure(encoder.Encode(*ifp));
}
return CHIP_NO_ERROR;
});
DeviceLayer::GetDiagnosticDataProvider().ReleaseNetworkInterfaces(netifs);
}
else
{
err = aEncoder.EncodeEmptyList();
}
return err;
}
void GeneralDiagnosticsGlobalInstance::HandleTestEventTrigger(HandlerContext & ctx,
const Commands::TestEventTrigger::DecodableType & commandData)
{
auto * triggerDelegate = GetTriggerDelegateOnMatchingKey(commandData.enableKey);
if (triggerDelegate == nullptr)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
CHIP_ERROR handleEventTriggerResult = triggerDelegate->HandleEventTriggers(commandData.eventTrigger);
// When HandleEventTrigger fails, we simply convert any error to INVALID_COMMAND
ctx.mCommandHandler.AddStatus(ctx.mRequestPath,
(handleEventTriggerResult != CHIP_NO_ERROR) ? Status::InvalidCommand : Status::Success);
}
void GeneralDiagnosticsGlobalInstance::HandleTimeSnapshot(HandlerContext & ctx,
const Commands::TimeSnapshot::DecodableType & commandData)
{
ChipLogError(Zcl, "Received TimeSnapshot command!");
Commands::TimeSnapshotResponse::Type response;
System::Clock::Microseconds64 posix_time_us{ 0 };
// Only consider real time if time sync cluster is actually enabled. Avoids
// likelihood of frequently reporting unsynced time.
#ifdef ZCL_USING_TIME_SYNCHRONIZATION_CLUSTER_SERVER
CHIP_ERROR posix_time_err = System::SystemClock().GetClock_RealTime(posix_time_us);
if (posix_time_err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "Failed to get POSIX real time: %" CHIP_ERROR_FORMAT, posix_time_err.Format());
posix_time_us = System::Clock::Microseconds64{ 0 };
}
#endif // ZCL_USING_TIME_SYNCHRONIZATION_CLUSTER_SERVER
System::Clock::Milliseconds64 system_time_ms =
std::chrono::duration_cast<System::Clock::Milliseconds64>(Server::GetInstance().TimeSinceInit());
response.systemTimeMs = static_cast<uint64_t>(system_time_ms.count());
if (posix_time_us.count() != 0)
{
response.posixTimeMs.SetNonNull(
static_cast<uint64_t>(std::chrono::duration_cast<System::Clock::Milliseconds64>(posix_time_us).count()));
}
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
}
#ifdef GENERAL_DIAGNOSTICS_ENABLE_PAYLOAD_TEST_REQUEST_CMD
void GeneralDiagnosticsGlobalInstance::HandlePayloadTestRequest(HandlerContext & ctx,
const Commands::PayloadTestRequest::DecodableType & commandData)
{
// Max allowed is 2048.
if (commandData.count > 2048)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
// Ensure Test Event triggers are enabled and key matches.
auto * triggerDelegate = GetTriggerDelegateOnMatchingKey(commandData.enableKey);
if (triggerDelegate == nullptr)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
Commands::PayloadTestResponse::Type response;
Platform::ScopedMemoryBufferWithSize<uint8_t> payload;
if (!payload.Calloc(commandData.count))
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ResourceExhausted);
return;
}
memset(payload.Get(), commandData.value, payload.AllocatedSize());
response.payload = ByteSpan{ payload.Get(), payload.AllocatedSize() };
if (ctx.mCommandHandler.AddResponseData(ctx.mRequestPath, response) != CHIP_NO_ERROR)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ResourceExhausted);
}
}
#endif // GENERAL_DIAGNOSTICS_ENABLE_PAYLOAD_TEST_REQUEST_CMD
GeneralDiagnosticsGlobalInstance gGeneralDiagnosticsInstance;
} // anonymous namespace
namespace chip {
namespace app {
namespace Clusters {
GeneralDiagnosticsServer GeneralDiagnosticsServer::instance;
/**********************************************************
* GeneralDiagnosticsServer Implementation
*********************************************************/
GeneralDiagnosticsServer & GeneralDiagnosticsServer::Instance()
{
return instance;
}
// Gets called when the device has been rebooted.
void GeneralDiagnosticsServer::OnDeviceReboot(BootReasonEnum bootReason)
{
ChipLogDetail(Zcl, "GeneralDiagnostics: OnDeviceReboot");
ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::BootReason::Id);
// GeneralDiagnostics cluster should exist only for endpoint 0.
if (emberAfContainsServer(0, GeneralDiagnostics::Id))
{
Events::BootReason::Type event{ bootReason };
EventNumber eventNumber;
CHIP_ERROR err = LogEvent(event, 0, eventNumber);
if (CHIP_NO_ERROR != err)
{
ChipLogError(Zcl, "GeneralDiagnostics: Failed to record BootReason event: %" CHIP_ERROR_FORMAT, err.Format());
}
}
}
// Get called when the Node detects a hardware fault has been raised.
void GeneralDiagnosticsServer::OnHardwareFaultsDetect(const GeneralFaults<kMaxHardwareFaults> & previous,
const GeneralFaults<kMaxHardwareFaults> & current)
{
ChipLogDetail(Zcl, "GeneralDiagnostics: OnHardwareFaultsDetect");
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveHardwareFaults::Id);
// Record HardwareFault event
EventNumber eventNumber;
DataModel::List<const HardwareFaultEnum> currentList(reinterpret_cast<const HardwareFaultEnum *>(current.data()),
current.size());
DataModel::List<const HardwareFaultEnum> previousList(reinterpret_cast<const HardwareFaultEnum *>(previous.data()),
previous.size());
Events::HardwareFaultChange::Type event{ currentList, previousList };
if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "GeneralDiagnostics: Failed to record HardwareFault event");
}
}
}
// Get called when the Node detects a radio fault has been raised.
void GeneralDiagnosticsServer::OnRadioFaultsDetect(const GeneralFaults<kMaxRadioFaults> & previous,
const GeneralFaults<kMaxRadioFaults> & current)
{
ChipLogDetail(Zcl, "GeneralDiagnostics: OnRadioFaultsDetect");
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveRadioFaults::Id);
// Record RadioFault event
EventNumber eventNumber;
DataModel::List<const RadioFaultEnum> currentList(reinterpret_cast<const RadioFaultEnum *>(current.data()), current.size());
DataModel::List<const RadioFaultEnum> previousList(reinterpret_cast<const RadioFaultEnum *>(previous.data()),
previous.size());
Events::RadioFaultChange::Type event{ currentList, previousList };
if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "GeneralDiagnostics: Failed to record RadioFault event");
}
}
}
// Get called when the Node detects a network fault has been raised.
void GeneralDiagnosticsServer::OnNetworkFaultsDetect(const GeneralFaults<kMaxNetworkFaults> & previous,
const GeneralFaults<kMaxNetworkFaults> & current)
{
ChipLogDetail(Zcl, "GeneralDiagnostics: OnNetworkFaultsDetect");
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveNetworkFaults::Id);
// Record NetworkFault event
EventNumber eventNumber;
DataModel::List<const NetworkFaultEnum> currentList(reinterpret_cast<const NetworkFaultEnum *>(current.data()),
current.size());
DataModel::List<const NetworkFaultEnum> previousList(reinterpret_cast<const NetworkFaultEnum *>(previous.data()),
previous.size());
Events::NetworkFaultChange::Type event{ currentList, previousList };
if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "GeneralDiagnostics: Failed to record NetworkFault event");
}
}
}
} // namespace Clusters
} // namespace app
} // namespace chip
void MatterGeneralDiagnosticsPluginServerInitCallback()
{
BootReasonEnum bootReason;
AttributeAccessInterfaceRegistry::Instance().Register(&gGeneralDiagnosticsInstance);
CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(&gGeneralDiagnosticsInstance);
ConnectivityMgr().SetDelegate(&gGeneralDiagnosticsInstance);
if (GetDiagnosticDataProvider().GetBootReason(bootReason) == CHIP_NO_ERROR)
{
GeneralDiagnosticsServer::Instance().OnDeviceReboot(bootReason);
}
}