• 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 "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     MtpPacketTool::Dump(buffer, offset);
50 
51     context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
52     context_->format = MtpPacketTool::GetUInt32(buffer, offset);
53     context_->property = MtpPacketTool::GetUInt32(buffer, offset);
54     context_->groupCode = MtpPacketTool::GetUInt32(buffer, offset);
55     context_->depth = MtpPacketTool::GetUInt32(buffer, offset);
56     return MTP_SUCCESS;
57 }
58 
Maker(std::vector<uint8_t> & outBuffer)59 int GetObjectPropListData::Maker(std::vector<uint8_t> &outBuffer)
60 {
61     if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
62         MEDIA_ERR_LOG("GetObjectPropListData::maker null or storage");
63         return MTP_FAIL;
64     }
65     if (!hasSetProps_) {
66         MEDIA_ERR_LOG("GetObjectPropListData::maker set");
67         return MTP_INVALID_OBJECTHANDLE_CODE;
68     }
69     size_t count = (props_ == nullptr) ? 0 : props_->size();
70 
71     MtpPacketTool::PutUInt32(outBuffer, count);
72     for (size_t i = 0; i < count; i++) {
73         Property &prop = (*props_)[i];
74         WriteProperty(outBuffer, prop);
75     }
76 
77     MtpPacketTool::Dump(outBuffer);
78     return MTP_SUCCESS;
79 }
80 
CalculateSize()81 uint32_t GetObjectPropListData::CalculateSize()
82 {
83     std::vector<uint8_t> tmpVar;
84     int res = Maker(tmpVar);
85     if (res != MTP_SUCCESS) {
86         return res;
87     }
88     return tmpVar.size();
89 }
90 
SetProps(std::shared_ptr<std::vector<Property>> & props)91 bool GetObjectPropListData::SetProps(std::shared_ptr<std::vector<Property>> &props)
92 {
93     if (hasSetProps_) {
94         return false;
95     }
96     hasSetProps_ = true;
97     props_ = props;
98     return true;
99 }
100 
WriteProperty(std::vector<uint8_t> & outBuffer,const Property & prop)101 void GetObjectPropListData::WriteProperty(std::vector<uint8_t> &outBuffer, const Property &prop)
102 {
103     MtpPacketTool::PutUInt32(outBuffer, prop.handle_);
104     MtpPacketTool::PutUInt16(outBuffer, prop.code_);
105     MtpPacketTool::PutUInt16(outBuffer, prop.type_);
106 
107     if (prop.currentValue == nullptr) {
108         MEDIA_ERR_LOG("GetObjectPropListData::WriteProperty bad value");
109         return;
110     }
111 
112     if (prop.type_ == MTP_TYPE_STRING_CODE) {
113         WritePropertyStrValue(outBuffer, prop);
114         return;
115     }
116 
117     WritePropertyIntValue(outBuffer, prop);
118 }
119 
WritePropertyStrValue(std::vector<uint8_t> & outBuffer,const Property & prop)120 void GetObjectPropListData::WritePropertyStrValue(std::vector<uint8_t> &outBuffer, const Property &prop)
121 {
122     auto &value = prop.currentValue;
123     if (prop.type_ == MTP_TYPE_STRING_CODE) {
124         if (value->str_ == nullptr) {
125             MtpPacketTool::PutUInt8(outBuffer, 0);
126         } else {
127             MtpPacketTool::PutString(outBuffer, *(value->str_));
128         }
129     }
130 }
131 
WritePropertyIntValue(std::vector<uint8_t> & outBuffer,const Property & prop)132 void GetObjectPropListData::WritePropertyIntValue(std::vector<uint8_t> &outBuffer, const Property &prop)
133 {
134     auto &value = prop.currentValue;
135     switch (prop.type_) {
136         case MTP_TYPE_INT8_CODE:
137             MtpPacketTool::PutInt8(outBuffer, value->bin_.i8);
138             break;
139         case MTP_TYPE_UINT8_CODE:
140             MtpPacketTool::PutUInt8(outBuffer, value->bin_.ui8);
141             break;
142         case MTP_TYPE_INT16_CODE:
143             MtpPacketTool::PutInt16(outBuffer, value->bin_.i16);
144             break;
145         case MTP_TYPE_UINT16_CODE:
146             MtpPacketTool::PutUInt16(outBuffer, value->bin_.ui16);
147             break;
148         case MTP_TYPE_INT32_CODE:
149             MtpPacketTool::PutInt32(outBuffer, value->bin_.i32);
150             break;
151         case MTP_TYPE_UINT32_CODE:
152             MtpPacketTool::PutUInt32(outBuffer, value->bin_.ui32);
153             break;
154         case MTP_TYPE_INT64_CODE:
155             MtpPacketTool::PutInt64(outBuffer, value->bin_.i64);
156             break;
157         case MTP_TYPE_UINT64_CODE:
158             MtpPacketTool::PutUInt64(outBuffer, value->bin_.ui64);
159             break;
160         case MTP_TYPE_INT128_CODE:
161             MtpPacketTool::PutInt128(outBuffer, value->bin_.i128);
162             break;
163         case MTP_TYPE_UINT128_CODE:
164             MtpPacketTool::PutUInt128(outBuffer, value->bin_.ui128);
165             break;
166         default:
167             MEDIA_ERR_LOG("GetObjectPropListData::writeProperty bad or unsupported data type %{public}u", prop.type_);
168             break;
169     }
170 }
171 } // namespace Media
172 } // namespace OHOS