• 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_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     MEDIA_INFO_LOG("WriteValue   value type %{public}d", type);
78     switch (type) {
79         case MTP_TYPE_INT8_CODE:
80         case MTP_TYPE_AINT8_CODE:
81             MtpPacketTool::PutUInt8(buffer, static_cast<uint8_t>(value.bin_.i8));
82             break;
83         case MTP_TYPE_UINT8_CODE:
84         case MTP_TYPE_AUINT8_CODE:
85             MtpPacketTool::PutUInt8(buffer, value.bin_.ui8);
86             break;
87         case MTP_TYPE_INT16_CODE:
88         case MTP_TYPE_AINT16_CODE:
89             MtpPacketTool::PutUInt16(buffer, static_cast<uint16_t>(value.bin_.i16));
90             break;
91         case MTP_TYPE_UINT16_CODE:
92         case MTP_TYPE_AUINT16_CODE:
93             MtpPacketTool::PutUInt16(buffer, value.bin_.ui16);
94             break;
95         case MTP_TYPE_INT32_CODE:
96         case MTP_TYPE_AINT32_CODE:
97             MtpPacketTool::PutUInt32(buffer, static_cast<uint32_t>(value.bin_.i32));
98             break;
99         case MTP_TYPE_UINT32_CODE:
100         case MTP_TYPE_AUINT32_CODE:
101             MtpPacketTool::PutUInt32(buffer, value.bin_.ui32);
102             break;
103         case MTP_TYPE_STRING_CODE:
104             MEDIA_INFO_LOG("value type MTP_TYPE_STRING_CODE");
105             if (value.str_ == nullptr) {
106                 MtpPacketTool::PutUInt8(buffer, 0);
107             } else {
108                 MtpPacketTool::PutString(buffer, *(value.str_));
109             }
110             break;
111         default: {
112             MEDIA_ERR_LOG("value type not find");
113             return MTP_ERROR_DEVICEPROP_NOT_SUPPORTED;
114         }
115     }
116     return MTP_SUCCESS;
117 }
118 } // namespace Media
119 } // namespace OHOS