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_value_data.h"
16 #include <cinttypes>
17 #include "media_log.h"
18 #include "media_mtp_utils.h"
19 #include "mtp_packet_tools.h"
20 #include "mtp_storage_manager.h"
21 using namespace std;
22 namespace OHOS {
23 namespace Media {
24 static constexpr int32_t PARSER_PARAM_SUM = 2;
25 
GetObjectPropValueData(std::shared_ptr<MtpOperationContext> & context)26 GetObjectPropValueData::GetObjectPropValueData(std::shared_ptr<MtpOperationContext> &context)
27     : PayloadData(context)
28 {
29 }
30 
~GetObjectPropValueData()31 GetObjectPropValueData::~GetObjectPropValueData()
32 {
33 }
34 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)35 int GetObjectPropValueData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
36 {
37     if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
38         MEDIA_ERR_LOG("GetObjectPropValueData::parser null or storage");
39         return MTP_FAIL;
40     }
41 
42     int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
43     if (parameterCount < PARSER_PARAM_SUM) {
44         MEDIA_ERR_LOG("GetObjectPropValueData::parser paramCount=%{public}u, needCount=%{public}d",
45             parameterCount, PARSER_PARAM_SUM);
46         return MTP_INVALID_PARAMETER_CODE;
47     }
48 
49     size_t offset = MTP_CONTAINER_HEADER_SIZE;
50     context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
51     context_->property = MtpPacketTool::GetUInt32(buffer, offset);
52     return MTP_SUCCESS;
53 }
54 
Maker(std::vector<uint8_t> & outBuffer)55 int GetObjectPropValueData::Maker(std::vector<uint8_t> &outBuffer)
56 {
57     if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
58         MEDIA_ERR_LOG("GetObjectPropValueData::maker null or storage");
59         return MTP_FAIL;
60     }
61 
62     if (!hasSetPropValue_) {
63         MEDIA_ERR_LOG("GetObjectPropValueData::maker set");
64         return MTP_INVALID_OBJECTHANDLE_CODE;
65     }
66 
67     if ((type_ == MTP_TYPE_INT8_CODE) || (type_ == MTP_TYPE_UINT8_CODE)) {
68         MtpPacketTool::PutUInt8(outBuffer, int64Value_);
69     } else if ((type_ == MTP_TYPE_INT16_CODE) || (type_ == MTP_TYPE_UINT16_CODE)) {
70         MtpPacketTool::PutUInt16(outBuffer, int64Value_);
71     } else if ((type_ == MTP_TYPE_INT32_CODE) || (type_ == MTP_TYPE_UINT32_CODE)) {
72         MtpPacketTool::PutUInt32(outBuffer, int64Value_);
73     } else if ((type_ == MTP_TYPE_INT64_CODE) || (type_ == MTP_TYPE_UINT64_CODE)) {
74         MtpPacketTool::PutUInt64(outBuffer, int64Value_);
75     } else if ((type_ == MTP_TYPE_INT128_CODE) || (type_ == MTP_TYPE_UINT128_CODE)) {
76         MtpPacketTool::PutUInt128(outBuffer, int128Value_);
77     } else if (type_ == MTP_TYPE_STRING_CODE) {
78         MtpPacketTool::PutString(outBuffer, strValue_);
79     } else {
80         MEDIA_ERR_LOG("GetObjectPropValueData::maker unsupported type");
81         return MTP_INVALID_OBJECTPROP_FORMAT_CODE;
82     }
83 
84     return MTP_SUCCESS;
85 }
86 
CalculateSize()87 uint32_t GetObjectPropValueData::CalculateSize()
88 {
89     std::vector<uint8_t> tmpVar;
90     int res = Maker(tmpVar);
91     if (res != MTP_SUCCESS) {
92         return res;
93     }
94     return tmpVar.size();
95 }
96 
SetPropValue(int type,uint64_t int64Value,const uint128_t int128Value,const std::string & strValue)97 bool GetObjectPropValueData::SetPropValue(int type, uint64_t int64Value, const uint128_t int128Value,
98     const std::string &strValue)
99 {
100     if (hasSetPropValue_) {
101         return false;
102     }
103     hasSetPropValue_ = true;
104     type_ = type;
105     int64Value_ = int64Value;
106     if (int128Value != nullptr) {
107         int128Value_[OFFSET_0] = int128Value[OFFSET_0];
108         int128Value_[OFFSET_1] = int128Value[OFFSET_1];
109         int128Value_[OFFSET_2] = int128Value[OFFSET_2];
110         int128Value_[OFFSET_3] = int128Value[OFFSET_3];
111     }
112     strValue_ = strValue;
113     return true;
114 }
115 } // namespace Media
116 } // namespace OHOS