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_desc_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 = 2;
23 static const std::vector<int> CHANNEL_ENUM = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
24 static const std::vector<int> BITRATE_ENUM = { 1, 2, };
25 static constexpr int AUDIO_BITRATE_MIN = 1;
26 static constexpr int AUDIO_BITRATE_MAX = 1536000;
27 static constexpr int AUDIO_BITRATE_STEP = 1;
28 static constexpr int SAMPLE_RATE_MIN = 8000;
29 static constexpr int SAMPLE_RATE_MAX = 48000;
30 static constexpr int SAMPLE_RATE_STEP = 1;
31
GetObjectPropDescData(std::shared_ptr<MtpOperationContext> & context)32 GetObjectPropDescData::GetObjectPropDescData(std::shared_ptr<MtpOperationContext> &context)
33 : PayloadData(context)
34 {
35 }
36
~GetObjectPropDescData()37 GetObjectPropDescData::~GetObjectPropDescData()
38 {
39 }
40
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)41 int GetObjectPropDescData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
42 {
43 if (context_ == nullptr) {
44 MEDIA_ERR_LOG("GetObjectPropDescData::parser null");
45 return MTP_FAIL;
46 }
47
48 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
49 if (parameterCount < PARSER_PARAM_SUM) {
50 MEDIA_ERR_LOG("GetObjectPropDescData::parser paramCount=%{public}u, needCount=%{public}d",
51 parameterCount, PARSER_PARAM_SUM);
52 return MTP_INVALID_PARAMETER_CODE;
53 }
54
55 size_t offset = MTP_CONTAINER_HEADER_SIZE;
56 MtpPacketTool::Dump(buffer, offset);
57
58 context_->property = MtpPacketTool::GetUInt32(buffer, offset);
59 context_->format = MtpPacketTool::GetUInt32(buffer, offset);
60 return MTP_SUCCESS;
61 }
62
Maker(std::vector<uint8_t> & outBuffer)63 int GetObjectPropDescData::Maker(std::vector<uint8_t> &outBuffer)
64 {
65 if (context_ == nullptr) {
66 MEDIA_ERR_LOG("GetObjectPropDescData::maker null");
67 return MTP_FAIL;
68 }
69
70 std::shared_ptr<Property> prop = GetProp();
71 if (prop == nullptr) {
72 MEDIA_ERR_LOG("GetObjectPropDescData::maker prop");
73 return MTP_INVALID_OBJECTHANDLE_CODE;
74 }
75 prop->Write(outBuffer);
76 MtpPacketTool::Dump(outBuffer);
77 return MTP_SUCCESS;
78 }
79
CalculateSize()80 uint32_t GetObjectPropDescData::CalculateSize()
81 {
82 std::vector<uint8_t> tmpVar;
83 int res = Maker(tmpVar);
84 if (res != MTP_SUCCESS) {
85 return res;
86 }
87 return tmpVar.size();
88 }
89
GetProp()90 std::shared_ptr<Property> GetObjectPropDescData::GetProp()
91 {
92 std::shared_ptr<Property> prop = GetPropInt();
93 if (prop == nullptr) {
94 prop = GetPropStr();
95 }
96 if (prop == nullptr) {
97 prop = GetPropForm();
98 }
99 return prop;
100 }
101
GetPropInt()102 std::shared_ptr<Property> GetObjectPropDescData::GetPropInt()
103 {
104 switch (context_->property) {
105 case MTP_PROPERTY_OBJECT_FORMAT_CODE: // use format as default value
106 return std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE, false, context_->format);
107 case MTP_PROPERTY_PROTECTION_STATUS_CODE:
108 case MTP_PROPERTY_TRACK_CODE:
109 return std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
110 case MTP_PROPERTY_STORAGE_ID_CODE:
111 case MTP_PROPERTY_PARENT_OBJECT_CODE:
112 case MTP_PROPERTY_DURATION_CODE:
113 case MTP_PROPERTY_AUDIO_WAVE_CODEC_CODE:
114 return std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
115 case MTP_PROPERTY_OBJECT_SIZE_CODE:
116 return std::make_shared<Property>(context_->property, MTP_TYPE_UINT64_CODE);
117 case MTP_PROPERTY_PERSISTENT_UID_CODE:
118 return std::make_shared<Property>(context_->property, MTP_TYPE_UINT128_CODE);
119 }
120 return nullptr;
121 }
122
GetPropStr()123 std::shared_ptr<Property> GetObjectPropDescData::GetPropStr()
124 {
125 switch (context_->property) {
126 case MTP_PROPERTY_NAME_CODE:
127 case MTP_PROPERTY_DISPLAY_NAME_CODE:
128 case MTP_PROPERTY_ARTIST_CODE:
129 case MTP_PROPERTY_ALBUM_NAME_CODE:
130 case MTP_PROPERTY_ALBUM_ARTIST_CODE:
131 case MTP_PROPERTY_GENRE_CODE:
132 case MTP_PROPERTY_COMPOSER_CODE:
133 case MTP_PROPERTY_DESCRIPTION_CODE:
134 return std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE);
135 case MTP_PROPERTY_DATE_MODIFIED_CODE:
136 case MTP_PROPERTY_DATE_ADDED_CODE:
137 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE_CODE: {
138 std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE);
139 prop->SetFormDateTime();
140 return prop;
141 }
142 case MTP_PROPERTY_OBJECT_FILE_NAME_CODE: // renaming files and folders
143 return std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE, true);
144 }
145 return nullptr;
146 }
147
GetPropForm()148 std::shared_ptr<Property> GetObjectPropDescData::GetPropForm()
149 {
150 switch (context_->property) {
151 case MTP_PROPERTY_BITRATE_TYPE_CODE: {
152 std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
153 prop->SetFormEnum(BITRATE_ENUM);
154 return prop;
155 }
156 case MTP_PROPERTY_AUDIO_BITRATE_CODE: {
157 std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
158 prop->SetFormRange(AUDIO_BITRATE_MIN, AUDIO_BITRATE_MAX, AUDIO_BITRATE_STEP);
159 return prop;
160 }
161 case MTP_PROPERTY_NUMBER_OF_CHANNELS_CODE: {
162 std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
163 prop->SetFormEnum(CHANNEL_ENUM);
164 return prop;
165 }
166 case MTP_PROPERTY_SAMPLE_RATE_CODE: {
167 std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
168 prop->SetFormRange(SAMPLE_RATE_MIN, SAMPLE_RATE_MAX, SAMPLE_RATE_STEP);
169 return prop;
170 }
171 }
172 return nullptr;
173 }
174 } // namespace Media
175 } // namespace OHOS