• 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/set_device_prop_value_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 #include "mtp_operation_utils.h"
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
23 static constexpr int32_t PARSER_PARAM_SUM = 1;
24 
SetDevicePropValueData(std::shared_ptr<MtpOperationContext> & context)25 SetDevicePropValueData::SetDevicePropValueData(std::shared_ptr<MtpOperationContext> &context)
26     : PayloadData(context)
27 {
28 }
29 
SetDevicePropValueData()30 SetDevicePropValueData::SetDevicePropValueData()
31 {
32 }
33 
~SetDevicePropValueData()34 SetDevicePropValueData::~SetDevicePropValueData()
35 {
36 }
37 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)38 int SetDevicePropValueData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
39 {
40     if (context_ == nullptr) {
41         MEDIA_ERR_LOG("SetDevicePropValueData::parser null");
42         return MTP_ERROR_INVALID_OBJECTHANDLE;
43     }
44 
45     int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
46     if (parameterCount < PARSER_PARAM_SUM) {
47         MEDIA_ERR_LOG("SetDevicePropValueData::parser paramCount=%{public}u, needCount=%{public}d",
48             parameterCount, PARSER_PARAM_SUM);
49         return MTP_ERROR_PACKET_INCORRECT;
50     }
51     size_t offset = MTP_CONTAINER_HEADER_SIZE;
52     if (!context_->indata) {
53         context_->property = MtpPacketTool::GetUInt32(buffer, offset);
54     } else {
55         PaserPropValue(buffer, offset, context_->property);
56     }
57     return MTP_SUCCESS;
58 }
59 
Maker(std::vector<uint8_t> & outBuffer)60 int SetDevicePropValueData::Maker(std::vector<uint8_t> &outBuffer)
61 {
62     return MTP_SUCCESS;
63 }
64 
CalculateSize()65 uint32_t SetDevicePropValueData::CalculateSize()
66 {
67     std::vector<uint8_t> tmpVar;
68     MtpPacketTool::PutInt16(tmpVar, MTP_OK_CODE);
69     int res = Maker(tmpVar);
70     if (res != MTP_SUCCESS) {
71         return res;
72     }
73     return tmpVar.size();
74 }
75 
PaserPropValue(const std::vector<uint8_t> & buffer,size_t & offset,uint32_t propertyCode)76 void SetDevicePropValueData::PaserPropValue(const std::vector<uint8_t> &buffer, size_t &offset, uint32_t propertyCode)
77 {
78     string value = MtpPacketTool::GetString(buffer, offset);
79 
80     switch (propertyCode) {
81         case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME_CODE:
82             if (!MtpOperationUtils::SetPropertyInner("persist.device.name", value)) {
83                 MEDIA_ERR_LOG("PaserPropValue SetPropertyInner fail");
84             }
85             break;
86         case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER_CODE:
87             // This function will be completed later
88             break;
89         case MTP_DEVICE_PROPERTY_SESSION_INITIATOR_VERSION_INFO_CODE:
90             // This function will be completed later
91             break;
92         default:
93             MEDIA_INFO_LOG("property do not find");
94             break;
95     }
96 }
97 } // namespace Media
98 } // namespace OHOS