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