1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "header_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 using namespace std;
20 namespace OHOS {
21 namespace Media {
22 uint32_t HeaderData::sTransactionID_ = 0;
23
HeaderData(std::shared_ptr<MtpOperationContext> & context)24 HeaderData::HeaderData(std::shared_ptr<MtpOperationContext> &context)
25 {
26 context_ = context;
27 }
28
HeaderData(uint16_t containerType,uint16_t code,uint32_t transactionID)29 HeaderData::HeaderData(uint16_t containerType, uint16_t code, uint32_t transactionID)
30 : containerType_(containerType), code_(code), transactionID_(transactionID)
31 {
32 }
33
~HeaderData()34 HeaderData::~HeaderData()
35 {
36 }
37
Parser(vector<uint8_t> & buffer,uint32_t readSize)38 int HeaderData::Parser(vector<uint8_t> &buffer, uint32_t readSize)
39 {
40 if (readSize < PACKET_HEADER_LENGETH) {
41 MEDIA_ERR_LOG("readSize incorrect : < PACKET_HEADER_LENGETH!");
42 return MTP_ERROR_PACKET_INCORRECT;
43 }
44
45 int offset = 0;
46 containerLength_ = MtpPacketTool::GetUInt32(buffer[offset], buffer[offset + OFFSET_1],
47 buffer[offset + OFFSET_2], buffer[offset + OFFSET_3]);
48 containerType_ = MtpPacketTool::GetUInt16(buffer[offset + OFFSET_4], buffer[offset + OFFSET_5]);
49 code_ = MtpPacketTool::GetUInt16(buffer[offset + OFFSET_6], buffer[offset + OFFSET_7]);
50 transactionID_ = MtpPacketTool::GetUInt32(buffer[offset + OFFSET_8], buffer[offset + OFFSET_9],
51 buffer[offset + OFFSET_10], buffer[offset + OFFSET_11]);
52 if (context_ == nullptr) {
53 MEDIA_ERR_LOG("Parser error: MTP_ERROR_CONTEXT_IS_NULL");
54 return MTP_ERROR_CONTEXT_IS_NULL;
55 }
56
57 context_->operationCode = code_;
58 context_->transactionID = transactionID_;
59 HeaderData::sTransactionID_ = transactionID_;
60 MEDIA_DEBUG_LOG("Parser operationCode 0x%{public}x, transactionID %{public}d, containerType %{public}d",
61 context_->operationCode, context_->transactionID, containerType_);
62 if (containerType_ == CONTAINER_TYPE_2) {
63 context_->indata = true;
64 } else {
65 context_->indata = false;
66 }
67 return MTP_SUCCESS;
68 }
69
Maker(std::vector<uint8_t> & outBuffer)70 int HeaderData::Maker(std::vector<uint8_t> &outBuffer)
71 {
72 MtpPacketTool::PutUInt32(outBuffer, containerLength_);
73 MtpPacketTool::PutUInt16(outBuffer, containerType_);
74 MtpPacketTool::PutUInt16(outBuffer, code_);
75 MtpPacketTool::PutUInt32(outBuffer, transactionID_);
76 return MTP_SUCCESS;
77 }
78
GetCode() const79 uint16_t HeaderData::GetCode() const
80 {
81 return code_;
82 }
83
SetCode(uint16_t code)84 void HeaderData::SetCode(uint16_t code)
85 {
86 code_ = code;
87 }
88
GetContainerLength() const89 uint32_t HeaderData::GetContainerLength() const
90 {
91 return containerLength_;
92 }
93
SetContainerLength(uint32_t containerLength)94 void HeaderData::SetContainerLength(uint32_t containerLength)
95 {
96 containerLength_ = containerLength;
97 }
98
GetContainerType() const99 uint16_t HeaderData::GetContainerType() const
100 {
101 return containerType_;
102 }
103
SetContainerType(uint16_t containerType)104 void HeaderData::SetContainerType(uint16_t containerType)
105 {
106 containerType_ = containerType;
107 }
108
GetTransactionId() const109 uint32_t HeaderData::GetTransactionId() const
110 {
111 return transactionID_;
112 }
113
SetTransactionId(uint32_t transactionId)114 void HeaderData::SetTransactionId(uint32_t transactionId)
115 {
116 transactionID_ = transactionId;
117 }
118
Reset()119 void HeaderData::Reset()
120 {
121 containerLength_ = 0;
122 containerType_ = 0;
123 code_ = 0;
124 transactionID_ = 0;
125 }
126 } // namespace Media
127 } // namespace OHOS