diff --git a/examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp b/examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp index 55336ea76030dd..4558cf3cb65ac6 100644 --- a/examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp +++ b/examples/temperature-measurement-app/esp32/main/diagnostic-logs-provider-delegate-impl.cpp @@ -291,6 +291,26 @@ CHIP_ERROR LogProvider::EndLogCollection(LogSessionHandle sessionHandle) return CHIP_NO_ERROR; } +CHIP_ERROR LogProvider::EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error) +{ + if (error != CHIP_NO_ERROR) + { + // Handle the error + ChipLogError(DeviceLayer, "End log collection reason: %s", ErrorStr(error)); + } + VerifyOrReturnValue(sessionHandle != kInvalidLogSessionHandle, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnValue(mSessionContextMap.count(sessionHandle), CHIP_ERROR_INVALID_ARGUMENT); + + LogContext * context = mSessionContextMap[sessionHandle]; + VerifyOrReturnError(context, CHIP_ERROR_INCORRECT_STATE); + + CleanupLogContextForIntent(context); + Platform::MemoryFree(context); + mSessionContextMap.erase(sessionHandle); + + return error; +} + CHIP_ERROR LogProvider::CollectLog(LogSessionHandle sessionHandle, MutableByteSpan & outBuffer, bool & outIsEndOfLog) { VerifyOrReturnValue(sessionHandle != kInvalidLogSessionHandle, CHIP_ERROR_INVALID_ARGUMENT); diff --git a/examples/temperature-measurement-app/esp32/main/include/diagnostic-logs-provider-delegate-impl.h b/examples/temperature-measurement-app/esp32/main/include/diagnostic-logs-provider-delegate-impl.h index 3431a54adc86a8..708f8bf017d858 100644 --- a/examples/temperature-measurement-app/esp32/main/include/diagnostic-logs-provider-delegate-impl.h +++ b/examples/temperature-measurement-app/esp32/main/include/diagnostic-logs-provider-delegate-impl.h @@ -45,6 +45,7 @@ class LogProvider : public DiagnosticLogsProviderDelegate CHIP_ERROR StartLogCollection(IntentEnum intent, LogSessionHandle & outHandle, Optional & outTimeStamp, Optional & outTimeSinceBoot) override; CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle) override; + CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error) override; CHIP_ERROR CollectLog(LogSessionHandle sessionHandle, MutableByteSpan & outBuffer, bool & outIsEndOfLog) override; size_t GetSizeForIntent(IntentEnum intent) override; CHIP_ERROR GetLogForIntent(IntentEnum intent, MutableByteSpan & outBuffer, Optional & outTimeStamp, diff --git a/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.cpp b/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.cpp index 6119652990679f..83c43e3919215e 100644 --- a/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.cpp +++ b/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.cpp @@ -162,7 +162,7 @@ void BDXDiagnosticLogsProvider::OnMsgToSend(TransferSession::OutputEvent & event auto err = mBDXTransferExchangeCtx->SendMessage(msgTypeData.ProtocolId, msgTypeData.MessageType, std::move(event.MsgData), sendFlags); - VerifyOrDo(CHIP_NO_ERROR == err, Reset()); + VerifyOrDo(CHIP_NO_ERROR == err, Reset(err)); } void BDXDiagnosticLogsProvider::OnAcceptReceived() @@ -213,7 +213,7 @@ void BDXDiagnosticLogsProvider::OnAckEOFReceived() { ChipLogProgress(BDX, "Diagnostic logs transfer: Success"); - Reset(); + Reset(CHIP_NO_ERROR); } void BDXDiagnosticLogsProvider::OnStatusReceived(TransferSession::OutputEvent & event) @@ -223,21 +223,21 @@ void BDXDiagnosticLogsProvider::OnStatusReceived(TransferSession::OutputEvent & // If a failure StatusReport is received in response to the SendInit message, the Node SHALL send a RetrieveLogsResponse command // with a Status of Denied. VerifyOrDo(mIsAcceptReceived, SendCommandResponse(StatusEnum::kDenied)); - Reset(); + Reset(CHIP_ERROR_INCORRECT_STATE); } void BDXDiagnosticLogsProvider::OnInternalError() { ChipLogError(BDX, "Internal Error"); VerifyOrDo(mIsAcceptReceived, SendCommandResponse(StatusEnum::kDenied)); - Reset(); + Reset(CHIP_ERROR_INTERNAL); } void BDXDiagnosticLogsProvider::OnTimeout() { ChipLogError(BDX, "Timeout"); VerifyOrDo(mIsAcceptReceived, SendCommandResponse(StatusEnum::kDenied)); - Reset(); + Reset(CHIP_ERROR_TIMEOUT); } void BDXDiagnosticLogsProvider::SendCommandResponse(StatusEnum status) @@ -264,7 +264,7 @@ void BDXDiagnosticLogsProvider::SendCommandResponse(StatusEnum status) commandHandle->AddResponse(mRequestPath, response); } -void BDXDiagnosticLogsProvider::Reset() +void BDXDiagnosticLogsProvider::Reset(CHIP_ERROR error) { assertChipStackLockedByCurrentThread(); @@ -279,7 +279,7 @@ void BDXDiagnosticLogsProvider::Reset() if (mDelegate != nullptr) { - mDelegate->EndLogCollection(mLogSessionHandle); + mDelegate->EndLogCollection(mLogSessionHandle, error); mDelegate = nullptr; } diff --git a/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.h b/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.h index 6122998eae986c..a80b04a3d61c5c 100644 --- a/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.h +++ b/src/app/clusters/diagnostic-logs-server/BDXDiagnosticLogsProvider.h @@ -76,8 +76,10 @@ class BDXDiagnosticLogsProvider : public bdx::Initiator /** * This method is called to reset state. It resets the transfer, cleans up the * exchange and ends log collection. + * @param[in] error A CHIP_ERROR value indicating the reason for resetting the state. + * It is permissible to pass CHIP_NO_ERROR to indicate normal termination. */ - void Reset(); + void Reset(CHIP_ERROR error); Messaging::ExchangeContext * mBDXTransferExchangeCtx; DiagnosticLogsProviderDelegate * mDelegate; diff --git a/src/app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h b/src/app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h index dc511fdacadeaa..f2d3a19732e2f3 100644 --- a/src/app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h +++ b/src/app/clusters/diagnostic-logs-server/DiagnosticLogsProviderDelegate.h @@ -63,6 +63,27 @@ class DiagnosticLogsProviderDelegate */ virtual CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle) = 0; + /** + * Called to end log collection for the log session identified by sessionHandle. + * This must be called if StartLogCollection happens successfully and a valid sessionHandle has been + * returned from StartLogCollection. + * + * This overload of EndLogCollection provides additional context through the error parameter, which + * can be used to indicate the reason for ending the log collection. + * + * @param[in] sessionHandle The unique handle for this log session returned from a call to StartLogCollection. + * @param[in] error A CHIP_ERROR value indicating the reason for ending the log collection. + * It is permissible to pass CHIP_NO_ERROR to indicate normal termination. + * @note Derived classes can override this method to handle additional termination logic specific to their + * implementation. If not overridden, this default implementation calls EndLogCollection with only + * sessionHandle. + * + */ + virtual CHIP_ERROR EndLogCollection(LogSessionHandle sessionHandle, CHIP_ERROR error) + { + return EndLogCollection(sessionHandle); + } + /** * Called to get the next chunk for the log session identified by sessionHandle. * The outBuffer is resized to the actual size of data that was successfully read from the file.