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_device_prop_value_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_operation_utils.h"
19 #include "mtp_packet_tools.h"
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
GetDevicePropValueData(std::shared_ptr<MtpOperationContext> & context)23 GetDevicePropValueData::GetDevicePropValueData(std::shared_ptr<MtpOperationContext> &context)
24 :PayloadData(context)
25 {
26 }
27
GetDevicePropValueData()28 GetDevicePropValueData::GetDevicePropValueData()
29 {
30 }
31
~GetDevicePropValueData()32 GetDevicePropValueData::~GetDevicePropValueData()
33 {
34 }
35
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)36 int GetDevicePropValueData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
37 {
38 if (context_ == nullptr) {
39 return MTP_ERROR_CONTEXT_IS_NULL;
40 }
41
42 size_t offset = MTP_CONTAINER_HEADER_SIZE;
43 context_->property = MtpPacketTool::GetUInt32(buffer, offset);
44 return MTP_SUCCESS;
45 }
46
Maker(std::vector<uint8_t> & outBuffer)47 int GetDevicePropValueData::Maker(std::vector<uint8_t> &outBuffer)
48 {
49 if (value_ == nullptr) {
50 MEDIA_ERR_LOG("value is NULL");
51 return MTP_ERROR_DEVICEPROP_NOT_SUPPORTED;
52 }
53 return WriteValue(outBuffer, type_, *value_);
54 }
55
CalculateSize()56 uint32_t GetDevicePropValueData::CalculateSize()
57 {
58 std::vector<uint8_t> tmpVar;
59
60 int res = Maker(tmpVar);
61 if (res != MTP_SUCCESS) {
62 return static_cast<uint32_t>(res);
63 }
64
65 uint32_t size = tmpVar.size();
66 return size;
67 }
68
SetValue(uint16_t type,std::shared_ptr<Property::Value> & value)69 void GetDevicePropValueData::SetValue(uint16_t type, std::shared_ptr<Property::Value> &value)
70 {
71 type_ = type;
72 value_ = value;
73 }
74
WriteValue(std::vector<uint8_t> & buffer,uint16_t type,const Property::Value & value)75 int GetDevicePropValueData::WriteValue(std::vector<uint8_t> &buffer, uint16_t type, const Property::Value &value)
76 {
77 switch (type) {
78 case MTP_TYPE_INT8_CODE:
79 case MTP_TYPE_AINT8_CODE:
80 MtpPacketTool::PutUInt8(buffer, static_cast<uint8_t>(value.bin_.i8));
81 break;
82 case MTP_TYPE_UINT8_CODE:
83 case MTP_TYPE_AUINT8_CODE:
84 MtpPacketTool::PutUInt8(buffer, value.bin_.ui8);
85 break;
86 case MTP_TYPE_INT16_CODE:
87 case MTP_TYPE_AINT16_CODE:
88 MtpPacketTool::PutUInt16(buffer, static_cast<uint16_t>(value.bin_.i16));
89 break;
90 case MTP_TYPE_UINT16_CODE:
91 case MTP_TYPE_AUINT16_CODE:
92 MtpPacketTool::PutUInt16(buffer, value.bin_.ui16);
93 break;
94 case MTP_TYPE_INT32_CODE:
95 case MTP_TYPE_AINT32_CODE:
96 MtpPacketTool::PutUInt32(buffer, static_cast<uint32_t>(value.bin_.i32));
97 break;
98 case MTP_TYPE_UINT32_CODE:
99 case MTP_TYPE_AUINT32_CODE:
100 MtpPacketTool::PutUInt32(buffer, value.bin_.ui32);
101 break;
102 default: {
103 MEDIA_ERR_LOG("value type not find");
104 return MTP_ERROR_DEVICEPROP_NOT_SUPPORTED;
105 }
106 }
107 return MTP_SUCCESS;
108 }
109 } // namespace Media
110 } // namespace OHOS