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_object_references_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 #include "mtp_storage_manager.h"
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
23 static constexpr int32_t PARSER_PARAM_SUM = 1;
24 
SetObjectReferencesData(std::shared_ptr<MtpOperationContext> & context)25 SetObjectReferencesData::SetObjectReferencesData(std::shared_ptr<MtpOperationContext> &context)
26     : PayloadData(context)
27 {
28 }
29 
~SetObjectReferencesData()30 SetObjectReferencesData::~SetObjectReferencesData()
31 {
32 }
33 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)34 int SetObjectReferencesData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
35 {
36     if ((context_ == nullptr) || (!context_->sessionOpen)) {
37         MEDIA_ERR_LOG("SetObjectReferencesData::parser null or session");
38         return MTP_SESSION_NOT_OPEN_CODE;
39     }
40 
41     if (!MtpStorageManager::GetInstance()->HasStorage()) {
42         MEDIA_ERR_LOG("SetObjectReferencesData::parser storage");
43         return MTP_INVALID_OBJECTHANDLE_CODE;
44     }
45 
46     int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
47     if (parameterCount < PARSER_PARAM_SUM) {
48         MEDIA_ERR_LOG("SetObjectReferencesData::parser paramCount=%{public}u, needCount=%{public}d",
49             parameterCount, PARSER_PARAM_SUM);
50         return MTP_INVALID_PARAMETER_CODE;
51     }
52 
53     size_t offset = MTP_CONTAINER_HEADER_SIZE;
54     context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
55     context_->handles = MtpPacketTool::GetAUInt32(buffer, offset);
56     return MTP_SUCCESS;
57 }
58 
Maker(std::vector<uint8_t> & outBuffer)59 int SetObjectReferencesData::Maker(std::vector<uint8_t> &outBuffer)
60 {
61     if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
62         MEDIA_ERR_LOG("SetObjectReferencesData::parser null or storage");
63         return MTP_FAIL;
64     }
65 
66     if (!hasSetResult_) {
67         MEDIA_ERR_LOG("SetObjectReferencesData::parser set");
68         return MTP_INVALID_OBJECTHANDLE_CODE;
69     }
70     return MTP_SUCCESS;
71 }
72 
CalculateSize()73 uint32_t SetObjectReferencesData::CalculateSize()
74 {
75     std::vector<uint8_t> tmpVar;
76     int res = Maker(tmpVar);
77     if (res != MTP_SUCCESS) {
78         return res;
79     }
80     return tmpVar.size();
81 }
82 
SetResult(uint16_t result)83 bool SetObjectReferencesData::SetResult(uint16_t result)
84 {
85     if (hasSetResult_) {
86         return false;
87     }
88     hasSetResult_ = true;
89     result_ = result;
90     return true;
91 }
92 } // namespace Media
93 } // namespace OHOS