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_object_handles_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 using namespace std;
20 namespace OHOS {
21 namespace Media {
22 static constexpr int32_t PARSER_PARAM_SUM = 3;
23 
GetObjectHandlesData(std::shared_ptr<MtpOperationContext> & context)24 GetObjectHandlesData::GetObjectHandlesData(std::shared_ptr<MtpOperationContext> &context)
25     : PayloadData(context)
26 {
27 }
28 
GetObjectHandlesData()29 GetObjectHandlesData::GetObjectHandlesData()
30 {
31 }
32 
~GetObjectHandlesData()33 GetObjectHandlesData::~GetObjectHandlesData()
34 {
35 }
36 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)37 int GetObjectHandlesData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
38 {
39     if (context_ == nullptr) {
40         MEDIA_ERR_LOG("GetObjectHandlesData::parser context_ == nullptr");
41         return MTP_ERROR_CONTEXT_IS_NULL;
42     }
43 
44     int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
45     if (parameterCount < PARSER_PARAM_SUM) {
46         MEDIA_ERR_LOG("GetObjectHandlesData::parser paramCount=%{public}u, needCount=%{public}d",
47             parameterCount, PARSER_PARAM_SUM);
48         return MTP_ERROR_PACKET_INCORRECT;
49     }
50 
51     size_t offset = MTP_CONTAINER_HEADER_SIZE;
52     context_->storageID = MtpPacketTool::GetUInt32(buffer, offset);
53     context_->format = MtpPacketTool::GetUInt32(buffer, offset);
54     context_->parent = MtpPacketTool::GetUInt32(buffer, offset);
55     return MTP_SUCCESS;
56 }
57 
Maker(std::vector<uint8_t> & outBuffer)58 int GetObjectHandlesData::Maker(std::vector<uint8_t> &outBuffer)
59 {
60     auto handles = GetObjectHandles();
61     if (handles == nullptr) {
62         MEDIA_ERR_LOG("GetObjectHandlesData::maker handles");
63         return MTP_ERROR_INVALID_OBJECTHANDLE;
64     }
65 
66     MtpPacketTool::PutAUInt32(outBuffer, handles->data(), handles->size());
67     return MTP_SUCCESS;
68 }
69 
CalculateSize()70 uint32_t GetObjectHandlesData::CalculateSize()
71 {
72     std::vector<uint8_t> tmpVar;
73     int res = Maker(tmpVar);
74     if (res != MTP_SUCCESS) {
75         return res;
76     }
77 
78     return tmpVar.size();
79 }
80 
SetObjectHandles(std::shared_ptr<UInt32List> & objectHandles)81 bool GetObjectHandlesData::SetObjectHandles(std::shared_ptr<UInt32List> &objectHandles)
82 {
83     if (hasSetObjectHandles_) {
84         return false;
85     }
86 
87     hasSetObjectHandles_ = true;
88     objectHandles_ = objectHandles;
89     return true;
90 }
91 
GetObjectHandles()92 std::shared_ptr<UInt32List> GetObjectHandlesData::GetObjectHandles()
93 {
94     if (!hasSetObjectHandles_) {
95         return nullptr;
96     }
97 
98     return objectHandles_;
99 }
100 } // namespace Media
101 } // namespace OHOS