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 "payload_data/get_object_info_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 static constexpr int32_t PARSER_PARAM_SUM = 1;
23
GetObjectInfoData(std::shared_ptr<MtpOperationContext> & context)24 GetObjectInfoData::GetObjectInfoData(std::shared_ptr<MtpOperationContext> &context)
25 : PayloadData(context)
26 {
27 }
28
GetObjectInfoData()29 GetObjectInfoData::GetObjectInfoData()
30 {
31 }
32
~GetObjectInfoData()33 GetObjectInfoData::~GetObjectInfoData()
34 {
35 }
36
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)37 int GetObjectInfoData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
38 {
39 if (context_ == nullptr) {
40 MEDIA_ERR_LOG("GetObjectInfoData::parser null");
41 return MTP_ERROR_CONTEXT_IS_NULL;
42 }
43
44 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
45 if (parameterCount < PARSER_PARAM_SUM) {
46 MEDIA_ERR_LOG("GetObjectInfoData::parser paramCount=%{public}u, needCount=%{public}d",
47 parameterCount, PARSER_PARAM_SUM);
48 return MTP_ERROR_PACKET_INCORRECT;
49 }
50 size_t offset = MTP_CONTAINER_HEADER_SIZE;
51 context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
52 return MTP_SUCCESS;
53 }
54
Maker(std::vector<uint8_t> & outBuffer)55 int GetObjectInfoData::Maker(std::vector<uint8_t> &outBuffer)
56 {
57 auto objectInfo = GetObjectInfo();
58 if (objectInfo == nullptr) {
59 MEDIA_ERR_LOG("GetObjectInfoData::maker object info");
60 return MTP_INVALID_OBJECTHANDLE_CODE;
61 }
62
63 MtpPacketTool::PutUInt32(outBuffer, objectInfo->storageID);
64 MtpPacketTool::PutUInt16(outBuffer, objectInfo->format);
65 MtpPacketTool::PutUInt16(outBuffer, objectInfo->protectionStatus);
66 MtpPacketTool::PutUInt32(outBuffer, objectInfo->size);
67 MtpPacketTool::PutUInt16(outBuffer, objectInfo->thumbFormat);
68 MtpPacketTool::PutUInt32(outBuffer, objectInfo->thumbCompressedSize);
69 MtpPacketTool::PutUInt32(outBuffer, objectInfo->thumbPixelWidth);
70 MtpPacketTool::PutUInt32(outBuffer, objectInfo->thumbPixelHeight);
71 MtpPacketTool::PutUInt32(outBuffer, objectInfo->imagePixelWidth);
72 MtpPacketTool::PutUInt32(outBuffer, objectInfo->imagePixelHeight);
73 MtpPacketTool::PutUInt32(outBuffer, objectInfo->imagePixelDepth);
74 MtpPacketTool::PutUInt32(outBuffer, objectInfo->parent);
75 MtpPacketTool::PutUInt16(outBuffer, objectInfo->associationType);
76 MtpPacketTool::PutUInt32(outBuffer, objectInfo->associationDesc);
77 MtpPacketTool::PutUInt32(outBuffer, objectInfo->sequenceNumber);
78 MtpPacketTool::PutString(outBuffer, objectInfo->name);
79 MtpPacketTool::PutString(outBuffer, MtpPacketTool::FormatDateTime(objectInfo->dateCreated));
80 MtpPacketTool::PutString(outBuffer, MtpPacketTool::FormatDateTime(objectInfo->dateModified));
81 MtpPacketTool::PutUInt8(outBuffer, 0);
82 return MTP_SUCCESS;
83 }
84
CalculateSize()85 uint32_t GetObjectInfoData::CalculateSize()
86 {
87 std::vector<uint8_t> tmpVar;
88 int res = Maker(tmpVar);
89 if (res != MTP_SUCCESS) {
90 return res;
91 }
92 return tmpVar.size();
93 }
94
SetObjectInfo(std::shared_ptr<ObjectInfo> & objectInfo)95 bool GetObjectInfoData::SetObjectInfo(std::shared_ptr<ObjectInfo> &objectInfo)
96 {
97 if (hasSetObjectInfo_) {
98 return false;
99 }
100 hasSetObjectInfo_ = true;
101 objectInfo_ = objectInfo;
102 return true;
103 }
104
GetObjectInfo()105 std::shared_ptr<ObjectInfo> GetObjectInfoData::GetObjectInfo()
106 {
107 if (!hasSetObjectInfo_) {
108 return nullptr;
109 }
110 return objectInfo_;
111 }
112 } // namespace Media
113 } // namespace OHOS