• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK_AND_RETURN_RET_LOG(readSize >= PACKET_HEADER_LENGETH, MTP_ERROR_PACKET_INCORRECT,
41         "readSize incorrect : < PACKET_HEADER_LENGETH!");
42     CHECK_AND_RETURN_RET_LOG(buffer.size() >= PACKET_HEADER_LENGETH, MTP_ERROR_PACKET_INCORRECT,
43         "buffer size incorrect : < PACKET_HEADER_LENGETH!");
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     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_ERROR_CONTEXT_IS_NULL,
53         "Parser error: MTP_ERROR_CONTEXT_IS_NULL");
54 
55     context_->operationCode = code_;
56     context_->transactionID = transactionID_;
57     HeaderData::sTransactionID_ = transactionID_;
58     MEDIA_DEBUG_LOG("Parser operationCode 0x%{public}x, transactionID %{public}d, containerType %{public}d",
59         context_->operationCode, context_->transactionID, containerType_);
60     if (containerType_ == CONTAINER_TYPE_2) {
61         context_->indata = true;
62     } else {
63         context_->indata = false;
64     }
65     return MTP_SUCCESS;
66 }
67 
Maker(std::vector<uint8_t> & outBuffer)68 int HeaderData::Maker(std::vector<uint8_t> &outBuffer)
69 {
70     MtpPacketTool::PutUInt32(outBuffer, containerLength_);
71     MtpPacketTool::PutUInt16(outBuffer, containerType_);
72     MtpPacketTool::PutUInt16(outBuffer, code_);
73     MtpPacketTool::PutUInt32(outBuffer, transactionID_);
74     return MTP_SUCCESS;
75 }
76 
GetCode() const77 uint16_t HeaderData::GetCode() const
78 {
79     return code_;
80 }
81 
SetCode(uint16_t code)82 void HeaderData::SetCode(uint16_t code)
83 {
84     code_ = code;
85 }
86 
GetContainerLength() const87 uint32_t HeaderData::GetContainerLength() const
88 {
89     return containerLength_;
90 }
91 
SetContainerLength(uint32_t containerLength)92 void HeaderData::SetContainerLength(uint32_t containerLength)
93 {
94     containerLength_ = containerLength;
95 }
96 
GetContainerType() const97 uint16_t HeaderData::GetContainerType() const
98 {
99     return containerType_;
100 }
101 
SetContainerType(uint16_t containerType)102 void HeaderData::SetContainerType(uint16_t containerType)
103 {
104     containerType_ = containerType;
105 }
106 
GetTransactionId() const107 uint32_t HeaderData::GetTransactionId() const
108 {
109     return transactionID_;
110 }
111 
SetTransactionId(uint32_t transactionId)112 void HeaderData::SetTransactionId(uint32_t transactionId)
113 {
114     transactionID_ = transactionId;
115 }
116 
Reset()117 void HeaderData::Reset()
118 {
119     containerLength_ = 0;
120     containerType_ = 0;
121     code_ = 0;
122     transactionID_ = 0;
123 }
124 } // namespace Media
125 } // namespace OHOS