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