|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "GenericEventManagementTestEventTriggerHandler.h" |
| 20 | + |
| 21 | +#include <app/clusters/general-diagnostics-server/general-diagnostics-server.h> |
| 22 | +#include <platform/GeneralFaults.h> |
| 23 | + |
| 24 | +namespace chip { |
| 25 | +namespace app { |
| 26 | + |
| 27 | +using namespace chip::DeviceLayer; |
| 28 | +using namespace chip::app::Clusters; |
| 29 | + |
| 30 | +CHIP_ERROR GenericEventManagementTestEventTriggerHandler::HandleFillUpEventLoggingBufferEventTriger() |
| 31 | +{ |
| 32 | + /* Create a fake hardware fault list. */ |
| 33 | + GeneralFaults<kMaxHardwareFaults> hardwareFaults; |
| 34 | + for (uint8_t hardwareFault = to_underlying(GeneralDiagnostics::HardwareFaultEnum::kUnspecified); |
| 35 | + hardwareFault < kMaxHardwareFaults; hardwareFault++) |
| 36 | + { |
| 37 | + hardwareFaults.add(hardwareFault); |
| 38 | + } |
| 39 | + |
| 40 | + /* Fill up the critical logging buffer by 10 hardware faults. */ |
| 41 | + constexpr uint8_t kHardwareFaultCountForCriticalBuffer = 10; |
| 42 | + for (uint8_t i = 0; i < kHardwareFaultCountForCriticalBuffer; i++) |
| 43 | + { |
| 44 | + GeneralDiagnosticsServer::Instance().OnHardwareFaultsDetect(hardwareFaults, hardwareFaults); |
| 45 | + } |
| 46 | + |
| 47 | + /* Fill up the info logging buffer. */ |
| 48 | + FillUpEventLoggingBufferWithFakeSoftwareFault(CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE); |
| 49 | + |
| 50 | + /* Fill up the debug logging buffer. */ |
| 51 | + FillUpEventLoggingBufferWithFakeSoftwareFault(CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE); |
| 52 | + |
| 53 | + return CHIP_NO_ERROR; |
| 54 | +} |
| 55 | + |
| 56 | +void GenericEventManagementTestEventTriggerHandler::FillUpEventLoggingBufferWithFakeSoftwareFault(size_t bufferSize) |
| 57 | +{ |
| 58 | + /* Create a fake fault message. */ |
| 59 | + constexpr size_t kEncodingOverhead = 0x40; |
| 60 | + const size_t recordSize = bufferSize - kEncodingOverhead; |
| 61 | + char * recordBuffer = static_cast<char *>(Platform::MemoryAlloc(recordSize)); |
| 62 | + VerifyOrReturn(recordBuffer != nullptr); |
| 63 | + std::unique_ptr<char, decltype(&Platform::MemoryFree)> recordString(recordBuffer, &Platform::MemoryFree); |
| 64 | + memset(recordString.get(), 0x55, recordSize); |
| 65 | + recordString.get()[recordSize - 1] = '\0'; |
| 66 | + |
| 67 | + /* Fill up the logging buffer by a software fault. */ |
| 68 | + TriggerSoftwareFaultEvent(recordString.get()); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace app |
| 72 | +} // namespace chip |
0 commit comments