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_prop_list_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 #include "mtp_storage_manager.h"
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
23 static constexpr int32_t PARSER_PARAM_SUM = 5;
24
GetObjectPropListData(std::shared_ptr<MtpOperationContext> & context)25 GetObjectPropListData::GetObjectPropListData(std::shared_ptr<MtpOperationContext> &context)
26 : PayloadData(context)
27 {
28 }
29
~GetObjectPropListData()30 GetObjectPropListData::~GetObjectPropListData()
31 {
32 }
33
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)34 int GetObjectPropListData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
35 {
36 if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
37 MEDIA_ERR_LOG("GetObjectPropListData::parser null or storage");
38 return MTP_FAIL;
39 }
40
41 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
42 if (parameterCount < PARSER_PARAM_SUM) {
43 MEDIA_ERR_LOG("GetObjectPropListData::parser paramCount=%{public}u, needCount=%{public}d",
44 parameterCount, PARSER_PARAM_SUM);
45 return MTP_INVALID_PARAMETER_CODE;
46 }
47
48 size_t offset = MTP_CONTAINER_HEADER_SIZE;
49
50 context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
51 context_->format = MtpPacketTool::GetUInt32(buffer, offset);
52 context_->property = MtpPacketTool::GetUInt32(buffer, offset);
53 context_->groupCode = MtpPacketTool::GetUInt32(buffer, offset);
54 context_->depth = MtpPacketTool::GetUInt32(buffer, offset);
55 return MTP_SUCCESS;
56 }
57
Maker(std::vector<uint8_t> & outBuffer)58 int GetObjectPropListData::Maker(std::vector<uint8_t> &outBuffer)
59 {
60 if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
61 MEDIA_ERR_LOG("GetObjectPropListData::maker null or storage");
62 return MTP_FAIL;
63 }
64 if (!hasSetProps_) {
65 MEDIA_ERR_LOG("GetObjectPropListData::maker set");
66 return MTP_INVALID_OBJECTHANDLE_CODE;
67 }
68 size_t count = (props_ == nullptr) ? 0 : props_->size();
69
70 MtpPacketTool::PutUInt32(outBuffer, count);
71 for (size_t i = 0; i < count; i++) {
72 Property &prop = (*props_)[i];
73 WriteProperty(outBuffer, prop);
74 }
75
76 return MTP_SUCCESS;
77 }
78
CalculateSize()79 uint32_t GetObjectPropListData::CalculateSize()
80 {
81 std::vector<uint8_t> tmpVar;
82 int res = Maker(tmpVar);
83 if (res != MTP_SUCCESS) {
84 return res;
85 }
86 return tmpVar.size();
87 }
88
SetProps(std::shared_ptr<std::vector<Property>> & props)89 bool GetObjectPropListData::SetProps(std::shared_ptr<std::vector<Property>> &props)
90 {
91 if (hasSetProps_) {
92 return false;
93 }
94 hasSetProps_ = true;
95 props_ = props;
96 return true;
97 }
98
WriteProperty(std::vector<uint8_t> & outBuffer,const Property & prop)99 void GetObjectPropListData::WriteProperty(std::vector<uint8_t> &outBuffer, const Property &prop)
100 {
101 MtpPacketTool::PutUInt32(outBuffer, prop.handle_);
102 MtpPacketTool::PutUInt16(outBuffer, prop.code_);
103 MtpPacketTool::PutUInt16(outBuffer, prop.type_);
104
105 if (prop.currentValue == nullptr) {
106 MEDIA_ERR_LOG("GetObjectPropListData::WriteProperty bad value");
107 return;
108 }
109
110 if (prop.type_ == MTP_TYPE_STRING_CODE) {
111 WritePropertyStrValue(outBuffer, prop);
112 return;
113 }
114
115 WritePropertyIntValue(outBuffer, prop);
116 }
117
WritePropertyStrValue(std::vector<uint8_t> & outBuffer,const Property & prop)118 void GetObjectPropListData::WritePropertyStrValue(std::vector<uint8_t> &outBuffer, const Property &prop)
119 {
120 auto &value = prop.currentValue;
121 if (prop.type_ == MTP_TYPE_STRING_CODE) {
122 if (value->str_ == nullptr) {
123 MtpPacketTool::PutUInt8(outBuffer, 0);
124 } else {
125 MtpPacketTool::PutString(outBuffer, *(value->str_));
126 }
127 }
128 }
129
WritePropertyIntValue(std::vector<uint8_t> & outBuffer,const Property & prop)130 void GetObjectPropListData::WritePropertyIntValue(std::vector<uint8_t> &outBuffer, const Property &prop)
131 {
132 auto &value = prop.currentValue;
133 switch (prop.type_) {
134 case MTP_TYPE_INT8_CODE:
135 MtpPacketTool::PutInt8(outBuffer, value->bin_.i8);
136 break;
137 case MTP_TYPE_UINT8_CODE:
138 MtpPacketTool::PutUInt8(outBuffer, value->bin_.ui8);
139 break;
140 case MTP_TYPE_INT16_CODE:
141 MtpPacketTool::PutInt16(outBuffer, value->bin_.i16);
142 break;
143 case MTP_TYPE_UINT16_CODE:
144 MtpPacketTool::PutUInt16(outBuffer, value->bin_.ui16);
145 break;
146 case MTP_TYPE_INT32_CODE:
147 MtpPacketTool::PutInt32(outBuffer, value->bin_.i32);
148 break;
149 case MTP_TYPE_UINT32_CODE:
150 MtpPacketTool::PutUInt32(outBuffer, value->bin_.ui32);
151 break;
152 case MTP_TYPE_INT64_CODE:
153 MtpPacketTool::PutInt64(outBuffer, value->bin_.i64);
154 break;
155 case MTP_TYPE_UINT64_CODE:
156 MtpPacketTool::PutUInt64(outBuffer, value->bin_.ui64);
157 break;
158 case MTP_TYPE_INT128_CODE:
159 MtpPacketTool::PutInt128(outBuffer, value->bin_.i128);
160 break;
161 case MTP_TYPE_UINT128_CODE:
162 MtpPacketTool::PutUInt128(outBuffer, value->bin_.ui128);
163 break;
164 default:
165 MEDIA_ERR_LOG("GetObjectPropListData::writeProperty bad or unsupported data type %{public}u", prop.type_);
166 break;
167 }
168 }
169 } // namespace Media
170 } // namespace OHOS