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 
57     context_->property = MtpPacketTool::GetUInt32(buffer, offset);
58     context_->format = MtpPacketTool::GetUInt32(buffer, offset);
59     return MTP_SUCCESS;
60 }
61 
Maker(std::vector<uint8_t> & outBuffer)62 int GetObjectPropDescData::Maker(std::vector<uint8_t> &outBuffer)
63 {
64     if (context_ == nullptr) {
65         MEDIA_ERR_LOG("GetObjectPropDescData::maker null");
66         return MTP_FAIL;
67     }
68 
69     std::shared_ptr<Property> prop = GetProp();
70     if (prop == nullptr) {
71         MEDIA_ERR_LOG("GetObjectPropDescData::maker prop");
72         return MTP_INVALID_OBJECTHANDLE_CODE;
73     }
74     prop->Write(outBuffer);
75     return MTP_SUCCESS;
76 }
77 
CalculateSize()78 uint32_t GetObjectPropDescData::CalculateSize()
79 {
80     std::vector<uint8_t> tmpVar;
81     int res = Maker(tmpVar);
82     if (res != MTP_SUCCESS) {
83         return res;
84     }
85     return tmpVar.size();
86 }
87 
GetProp()88 std::shared_ptr<Property> GetObjectPropDescData::GetProp()
89 {
90     std::shared_ptr<Property> prop = GetPropInt();
91     if (prop == nullptr) {
92         prop = GetPropStr();
93     }
94     if (prop == nullptr) {
95         prop = GetPropForm();
96     }
97     return prop;
98 }
99 
GetPropInt()100 std::shared_ptr<Property> GetObjectPropDescData::GetPropInt()
101 {
102     switch (context_->property) {
103         case MTP_PROPERTY_OBJECT_FORMAT_CODE: // use format as default value
104             return std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE, false, context_->format);
105         case MTP_PROPERTY_PROTECTION_STATUS_CODE:
106         case MTP_PROPERTY_TRACK_CODE:
107             return std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
108         case MTP_PROPERTY_STORAGE_ID_CODE:
109         case MTP_PROPERTY_PARENT_OBJECT_CODE:
110         case MTP_PROPERTY_DURATION_CODE:
111         case MTP_PROPERTY_AUDIO_WAVE_CODEC_CODE:
112             return std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
113         case MTP_PROPERTY_OBJECT_SIZE_CODE:
114             return std::make_shared<Property>(context_->property, MTP_TYPE_UINT64_CODE);
115         case MTP_PROPERTY_PERSISTENT_UID_CODE:
116             return std::make_shared<Property>(context_->property, MTP_TYPE_UINT128_CODE);
117     }
118     return nullptr;
119 }
120 
GetPropStr()121 std::shared_ptr<Property> GetObjectPropDescData::GetPropStr()
122 {
123     switch (context_->property) {
124         case MTP_PROPERTY_NAME_CODE:
125         case MTP_PROPERTY_DISPLAY_NAME_CODE:
126         case MTP_PROPERTY_ARTIST_CODE:
127         case MTP_PROPERTY_ALBUM_NAME_CODE:
128         case MTP_PROPERTY_ALBUM_ARTIST_CODE:
129         case MTP_PROPERTY_GENRE_CODE:
130         case MTP_PROPERTY_COMPOSER_CODE:
131         case MTP_PROPERTY_DESCRIPTION_CODE:
132             return std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE);
133         case MTP_PROPERTY_DATE_MODIFIED_CODE:
134         case MTP_PROPERTY_DATE_ADDED_CODE:
135         case MTP_PROPERTY_ORIGINAL_RELEASE_DATE_CODE: {
136             std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE);
137             prop->SetFormDateTime();
138             return prop;
139         }
140         case MTP_PROPERTY_OBJECT_FILE_NAME_CODE: // renaming files and folders
141             return std::make_shared<Property>(context_->property, MTP_TYPE_STRING_CODE, true);
142     }
143     return nullptr;
144 }
145 
GetPropForm()146 std::shared_ptr<Property> GetObjectPropDescData::GetPropForm()
147 {
148     switch (context_->property) {
149         case MTP_PROPERTY_BITRATE_TYPE_CODE: {
150             std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
151             prop->SetFormEnum(BITRATE_ENUM);
152             return prop;
153         }
154         case MTP_PROPERTY_AUDIO_BITRATE_CODE: {
155             std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
156             prop->SetFormRange(AUDIO_BITRATE_MIN, AUDIO_BITRATE_MAX, AUDIO_BITRATE_STEP);
157             return prop;
158         }
159         case MTP_PROPERTY_NUMBER_OF_CHANNELS_CODE: {
160             std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT16_CODE);
161             prop->SetFormEnum(CHANNEL_ENUM);
162             return prop;
163         }
164         case MTP_PROPERTY_SAMPLE_RATE_CODE: {
165             std::shared_ptr<Property> prop = std::make_shared<Property>(context_->property, MTP_TYPE_UINT32_CODE);
166             prop->SetFormRange(SAMPLE_RATE_MIN, SAMPLE_RATE_MAX, SAMPLE_RATE_STEP);
167             return prop;
168         }
169     }
170     return nullptr;
171 }
172 } // namespace Media
173 } // namespace OHOS