|
| 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 <diagnostic-logs-provider-delegate-impl.h> |
| 20 | +#include <lib/support/SafeInt.h> |
| 21 | + |
| 22 | +using namespace chip; |
| 23 | +using namespace chip::app::Clusters::DiagnosticLogs; |
| 24 | + |
| 25 | +LogProvider LogProvider::sInstance; |
| 26 | + |
| 27 | +namespace { |
| 28 | +bool IsValidIntent(IntentEnum intent) |
| 29 | +{ |
| 30 | + return intent != IntentEnum::kUnknownEnumValue; |
| 31 | +} |
| 32 | + |
| 33 | +// end_user_support.log, network_diag.log, and crash.log files are embedded in the firmware |
| 34 | +extern const uint8_t endUserSupportLogStart[] asm("_binary_end_user_support_log_start"); |
| 35 | +extern const uint8_t endUserSupportLogEnd[] asm("_binary_end_user_support_log_end"); |
| 36 | + |
| 37 | +extern const uint8_t networkDiagnosticLogStart[] asm("_binary_network_diag_log_start"); |
| 38 | +extern const uint8_t networkDiagnosticLogEnd[] asm("_binary_network_diag_log_end"); |
| 39 | + |
| 40 | +extern const uint8_t crashLogStart[] asm("_binary_crash_log_start"); |
| 41 | +extern const uint8_t crashLogEnd[] asm("_binary_crash_log_end"); |
| 42 | +} // namespace |
| 43 | + |
| 44 | +LogProvider::~LogProvider() |
| 45 | +{ |
| 46 | + for (auto sessionSpan : mSessionSpanMap) |
| 47 | + { |
| 48 | + Platform::MemoryFree(sessionSpan.second); |
| 49 | + } |
| 50 | + mSessionSpanMap.clear(); |
| 51 | +} |
| 52 | + |
| 53 | +CHIP_ERROR LogProvider::GetLogForIntent(IntentEnum intent, MutableByteSpan & outBuffer, Optional<uint64_t> & outTimeStamp, |
| 54 | + Optional<uint64_t> & outTimeSinceBoot) |
| 55 | +{ |
| 56 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 57 | + LogSessionHandle sessionHandle = kInvalidLogSessionHandle; |
| 58 | + |
| 59 | + err = StartLogCollection(intent, sessionHandle, outTimeStamp, outTimeSinceBoot); |
| 60 | + VerifyOrReturnError(CHIP_NO_ERROR == err, err, outBuffer.reduce_size(0)); |
| 61 | + |
| 62 | + bool unusedOutIsEndOfLog; |
| 63 | + err = CollectLog(sessionHandle, outBuffer, unusedOutIsEndOfLog); |
| 64 | + VerifyOrReturnError(CHIP_NO_ERROR == err, err, outBuffer.reduce_size(0)); |
| 65 | + |
| 66 | + err = EndLogCollection(sessionHandle); |
| 67 | + VerifyOrReturnError(CHIP_NO_ERROR == err, err, outBuffer.reduce_size(0)); |
| 68 | + |
| 69 | + return CHIP_NO_ERROR; |
| 70 | +} |
| 71 | + |
| 72 | +const uint8_t * LogProvider::GetDataStartForIntent(IntentEnum intent) |
| 73 | +{ |
| 74 | + switch (intent) |
| 75 | + { |
| 76 | + case IntentEnum::kEndUserSupport: |
| 77 | + return &endUserSupportLogStart[0]; |
| 78 | + case IntentEnum::kNetworkDiag: |
| 79 | + return &networkDiagnosticLogStart[0]; |
| 80 | + case IntentEnum::kCrashLogs: |
| 81 | + return &crashLogStart[0]; |
| 82 | + default: |
| 83 | + return nullptr; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +size_t LogProvider::GetSizeForIntent(IntentEnum intent) |
| 88 | +{ |
| 89 | + switch (intent) |
| 90 | + { |
| 91 | + case IntentEnum::kEndUserSupport: |
| 92 | + return static_cast<size_t>(endUserSupportLogEnd - endUserSupportLogStart); |
| 93 | + case IntentEnum::kNetworkDiag: |
| 94 | + return static_cast<size_t>(networkDiagnosticLogEnd - networkDiagnosticLogStart); |
| 95 | + case IntentEnum::kCrashLogs: |
| 96 | + return static_cast<size_t>(crashLogEnd - crashLogStart); |
| 97 | + default: |
| 98 | + return 0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +CHIP_ERROR LogProvider::StartLogCollection(IntentEnum intent, LogSessionHandle & outHandle, Optional<uint64_t> & outTimeStamp, |
| 103 | + Optional<uint64_t> & outTimeSinceBoot) |
| 104 | +{ |
| 105 | + VerifyOrReturnValue(IsValidIntent(intent), CHIP_ERROR_INVALID_ARGUMENT); |
| 106 | + |
| 107 | + const uint8_t * dataStart = GetDataStartForIntent(intent); |
| 108 | + VerifyOrReturnError(dataStart, CHIP_ERROR_NOT_FOUND); |
| 109 | + |
| 110 | + size_t dataSize = GetSizeForIntent(intent); |
| 111 | + VerifyOrReturnError(dataSize, CHIP_ERROR_NOT_FOUND); |
| 112 | + |
| 113 | + ByteSpan * span = reinterpret_cast<ByteSpan *>(Platform::MemoryCalloc(1, sizeof(ByteSpan))); |
| 114 | + VerifyOrReturnValue(span, CHIP_ERROR_NO_MEMORY); |
| 115 | + |
| 116 | + *span = ByteSpan(dataStart, dataSize); |
| 117 | + |
| 118 | + mLogSessionHandle++; |
| 119 | + // If the session handle rolls over to UINT16_MAX which is invalid, reset to 0. |
| 120 | + VerifyOrDo(mLogSessionHandle != kInvalidLogSessionHandle, mLogSessionHandle = 0); |
| 121 | + |
| 122 | + outHandle = mLogSessionHandle; |
| 123 | + mSessionSpanMap[mLogSessionHandle] = span; |
| 124 | + return CHIP_NO_ERROR; |
| 125 | +} |
| 126 | + |
| 127 | +CHIP_ERROR LogProvider::EndLogCollection(LogSessionHandle sessionHandle) |
| 128 | +{ |
| 129 | + VerifyOrReturnValue(sessionHandle != kInvalidLogSessionHandle, CHIP_ERROR_INVALID_ARGUMENT); |
| 130 | + VerifyOrReturnValue(mSessionSpanMap.count(sessionHandle), CHIP_ERROR_INVALID_ARGUMENT); |
| 131 | + |
| 132 | + ByteSpan * span = mSessionSpanMap[sessionHandle]; |
| 133 | + mSessionSpanMap.erase(sessionHandle); |
| 134 | + |
| 135 | + Platform::MemoryFree(span); |
| 136 | + return CHIP_NO_ERROR; |
| 137 | +} |
| 138 | + |
| 139 | +CHIP_ERROR LogProvider::CollectLog(LogSessionHandle sessionHandle, MutableByteSpan & outBuffer, bool & outIsEndOfLog) |
| 140 | +{ |
| 141 | + VerifyOrReturnValue(sessionHandle != kInvalidLogSessionHandle, CHIP_ERROR_INVALID_ARGUMENT); |
| 142 | + VerifyOrReturnValue(mSessionSpanMap.count(sessionHandle), CHIP_ERROR_INVALID_ARGUMENT); |
| 143 | + |
| 144 | + ByteSpan * span = mSessionSpanMap[sessionHandle]; |
| 145 | + auto dataSize = span->size(); |
| 146 | + auto count = std::min(dataSize, outBuffer.size()); |
| 147 | + |
| 148 | + VerifyOrReturnError(CanCastTo<off_t>(count), CHIP_ERROR_INVALID_ARGUMENT, outBuffer.reduce_size(0)); |
| 149 | + |
| 150 | + ReturnErrorOnFailure(CopySpanToMutableSpan(ByteSpan(span->data(), count), outBuffer)); |
| 151 | + |
| 152 | + outIsEndOfLog = dataSize == count; |
| 153 | + |
| 154 | + if (!outIsEndOfLog) |
| 155 | + { |
| 156 | + // reduce the span after reading count bytes |
| 157 | + *span = span->SubSpan(count); |
| 158 | + } |
| 159 | + |
| 160 | + return CHIP_NO_ERROR; |
| 161 | +} |
0 commit comments