|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <app/clusters/software-diagnostics-server/SoftwareDiagnosticsTestEventTriggerHandler.h> |
| 19 | +#include <app/clusters/software-diagnostics-server/software-diagnostics-server.h> |
| 20 | +#include <platform/CHIPDeviceLayer.h> |
| 21 | +#include <platform/DiagnosticDataProvider.h> |
| 22 | + |
| 23 | +using namespace chip; |
| 24 | +using namespace chip::app; |
| 25 | +using namespace chip::DeviceLayer; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +void SetTestEventTrigger_SoftwareFaultOccurred() |
| 30 | +{ |
| 31 | + Clusters::SoftwareDiagnostics::Events::SoftwareFault::Type softwareFault; |
| 32 | + char threadName[kMaxThreadNameLength + 1]; |
| 33 | + |
| 34 | + softwareFault.id = static_cast<uint64_t>(getpid()); |
| 35 | + Platform::CopyString(threadName, std::to_string(softwareFault.id).c_str()); |
| 36 | + |
| 37 | + softwareFault.name.SetValue(CharSpan::fromCharString(threadName)); |
| 38 | + |
| 39 | + std::time_t result = std::time(nullptr); |
| 40 | + // Using size of 50 as it is double the expected 25 characters "Www Mmm dd hh:mm:ss yyyy\n". |
| 41 | + char timeChar[50]; |
| 42 | + if (std::strftime(timeChar, sizeof(timeChar), "%c", std::localtime(&result))) |
| 43 | + { |
| 44 | + softwareFault.faultRecording.SetValue(ByteSpan(Uint8::from_const_char(timeChar), strlen(timeChar))); |
| 45 | + } |
| 46 | + |
| 47 | + Clusters::SoftwareDiagnosticsServer::Instance().OnSoftwareFaultDetect(softwareFault); |
| 48 | +} |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +bool HandleSoftwareDiagnosticsTestEventTrigger(uint64_t eventTrigger) |
| 53 | +{ |
| 54 | + SoftwareDiagnosticsTrigger trigger = static_cast<SoftwareDiagnosticsTrigger>(eventTrigger); |
| 55 | + |
| 56 | + switch (trigger) |
| 57 | + { |
| 58 | + case SoftwareDiagnosticsTrigger::kSoftwareFault: |
| 59 | + ChipLogProgress(Support, "[Software-Diagnostics-Test-Event] => Software Fault occurred"); |
| 60 | + SetTestEventTrigger_SoftwareFaultOccurred(); |
| 61 | + break; |
| 62 | + default: |
| 63 | + |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | +} |
0 commit comments