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 <= ArraySize(mFileDesignator), CHIP_ERROR_INVALID_STRING_LENGTH);
37 :
38 0 : mTransfer = transferSession;
39 0 : mFileDesignatorLen = static_cast<uint8_t>(fileDesignatorLength);
40 0 : memcpy(mFileDesignator, fileDesignator, fileDesignatorLength);
41 0 : return CHIP_NO_ERROR;
42 : }
43 :
44 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Accept()
45 : {
46 0 : ReturnErrorOnFailure(EnsureState());
47 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);
48 :
49 0 : TransferSession::TransferAcceptData acceptData;
50 0 : acceptData.ControlMode = TransferControlFlags::kSenderDrive;
51 0 : acceptData.MaxBlockSize = mTransfer->GetTransferBlockSize();
52 0 : acceptData.StartOffset = mTransfer->GetStartOffset();
53 0 : acceptData.Length = mTransfer->GetTransferLength();
54 :
55 0 : return mTransfer->AcceptTransfer(acceptData);
56 : }
57 :
58 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Reject(CHIP_ERROR error)
59 : {
60 0 : ReturnErrorOnFailure(EnsureState());
61 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);
62 :
63 0 : auto statusCode = GetBdxStatusCodeFromChipError(error);
64 0 : return mTransfer->AbortTransfer(statusCode);
65 : }
66 :
67 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::Continue()
68 : {
69 0 : ReturnErrorOnFailure(EnsureState());
70 0 : VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);
71 :
72 0 : return mTransfer->PrepareBlockAck();
73 : }
74 :
75 0 : void BDXTransferProxyDiagnosticLog::Reset()
76 : {
77 0 : ReturnOnFailure(EnsureState());
78 0 : VerifyOrReturn(nullptr != mTransfer);
79 :
80 0 : memset(mFileDesignator, 0, sizeof(mFileDesignator));
81 0 : mFileDesignatorLen = 0;
82 0 : mTransfer = nullptr;
83 0 : mFabricIndex = kUndefinedFabricIndex;
84 0 : mPeerNodeId = kUndefinedNodeId;
85 : }
86 :
87 0 : CHIP_ERROR BDXTransferProxyDiagnosticLog::EnsureState() const
88 : {
89 0 : assertChipStackLockedByCurrentThread();
90 0 : VerifyOrReturnError(kUndefinedFabricIndex != mFabricIndex, CHIP_ERROR_INCORRECT_STATE);
91 0 : VerifyOrReturnError(kUndefinedNodeId != mPeerNodeId, CHIP_ERROR_INCORRECT_STATE);
92 :
93 0 : return CHIP_NO_ERROR;
94 : }
95 :
96 : } // namespace bdx
97 : } // namespace chip
|