Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2023 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 "BdxTransferProxyDiagnosticLog.h" 20 : 21 : #include <lib/core/CHIPSafeCasts.h> 22 : #include <lib/support/CHIPMemString.h> 23 : #include <platform/LockTracker.h> 24 : 25 : namespace chip { 26 : namespace bdx { 27 : 28 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Init(TransferSession * transferSession) 29 : { 30 0 : VerifyOrReturnError(nullptr != transferSession, CHIP_ERROR_INVALID_ARGUMENT); 31 0 : VerifyOrReturnError(nullptr == mTransfer, CHIP_ERROR_INCORRECT_STATE); 32 : 33 0 : uint16_t fileDesignatorLength = 0; 34 0 : auto fileDesignator = transferSession->GetFileDesignator(fileDesignatorLength); 35 : 36 0 : VerifyOrReturnError(fileDesignatorLength > 0, CHIP_ERROR_INVALID_STRING_LENGTH); 37 0 : VerifyOrReturnError(fileDesignatorLength <= ArraySize(mFileDesignator), CHIP_ERROR_INVALID_STRING_LENGTH); 38 : 39 0 : mTransfer = transferSession; 40 0 : mFileDesignatorLen = static_cast<uint8_t>(fileDesignatorLength); 41 0 : memcpy(mFileDesignator, fileDesignator, fileDesignatorLength); 42 0 : return CHIP_NO_ERROR; 43 : } 44 : 45 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Accept() 46 : { 47 0 : ReturnErrorOnFailure(EnsureState()); 48 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE); 49 : 50 0 : TransferSession::TransferAcceptData acceptData; 51 0 : acceptData.ControlMode = TransferControlFlags::kSenderDrive; 52 0 : acceptData.MaxBlockSize = mTransfer->GetTransferBlockSize(); 53 0 : acceptData.StartOffset = mTransfer->GetStartOffset(); 54 0 : acceptData.Length = mTransfer->GetTransferLength(); 55 : 56 0 : return mTransfer->AcceptTransfer(acceptData); 57 : } 58 : 59 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Reject(CHIP_ERROR error) 60 : { 61 0 : ReturnErrorOnFailure(EnsureState()); 62 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE); 63 : 64 0 : auto statusCode = GetBdxStatusCodeFromChipError(error); 65 0 : return mTransfer->AbortTransfer(statusCode); 66 : } 67 : 68 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Continue() 69 : { 70 0 : ReturnErrorOnFailure(EnsureState()); 71 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE); 72 : 73 0 : return mTransfer->PrepareBlockAck(); 74 : } 75 : 76 0 : void BDXTransferProxyDiagnosticLog::Reset() 77 : { 78 0 : ReturnOnFailure(EnsureState()); 79 0 : VerifyOrReturn(nullptr != mTransfer); 80 : 81 0 : memset(mFileDesignator, 0, sizeof(mFileDesignator)); 82 0 : mFileDesignatorLen = 0; 83 0 : mTransfer = nullptr; 84 0 : mFabricIndex = kUndefinedFabricIndex; 85 0 : mPeerNodeId = kUndefinedNodeId; 86 : } 87 : 88 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::EnsureState() const 89 : { 90 0 : assertChipStackLockedByCurrentThread(); 91 0 : VerifyOrReturnError(kUndefinedFabricIndex != mFabricIndex, CHIP_ERROR_INCORRECT_STATE); 92 0 : VerifyOrReturnError(kUndefinedNodeId != mPeerNodeId, CHIP_ERROR_INCORRECT_STATE); 93 : 94 0 : return CHIP_NO_ERROR; 95 : } 96 : 97 : } // namespace bdx 98 : } // namespace chip