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_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
GetObjectReferencesData(std::shared_ptr<MtpOperationContext> & context)25 GetObjectReferencesData::GetObjectReferencesData(std::shared_ptr<MtpOperationContext> &context)
26 : PayloadData(context)
27 {
28 }
29
~GetObjectReferencesData()30 GetObjectReferencesData::~GetObjectReferencesData()
31 {
32 }
33
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)34 int GetObjectReferencesData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
35 {
36 if (context_ == nullptr) {
37 MEDIA_ERR_LOG("GetObjectReferencesData::parser context_ is null");
38 return MTP_ERROR_SESSION_NOT_OPEN;
39 }
40
41 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
42 if (parameterCount < PARSER_PARAM_SUM) {
43 MEDIA_ERR_LOG("GetObjectReferencesData::parser paramCount=%{public}u, needCount=%{public}d",
44 parameterCount, PARSER_PARAM_SUM);
45 return MTP_ERROR_PACKET_INCORRECT;
46 }
47
48 size_t offset = MTP_CONTAINER_HEADER_SIZE;
49 context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
50 return MTP_SUCCESS;
51 }
52
Maker(std::vector<uint8_t> & outBuffer)53 int GetObjectReferencesData::Maker(std::vector<uint8_t> &outBuffer)
54 {
55 if ((context_ == nullptr) || (!context_->sessionOpen)) {
56 MEDIA_ERR_LOG("GetObjectReferencesData::maker null or session");
57 return MTP_SESSION_NOT_OPEN_CODE;
58 }
59
60 if (!MtpStorageManager::GetInstance()->HasStorage()) {
61 MEDIA_ERR_LOG("GetObjectReferencesData::maker storage");
62 return MTP_INVALID_OBJECTHANDLE_CODE;
63 }
64
65 auto handles = GetObjectHandles();
66 if (handles == nullptr) {
67 MtpPacketTool::PutUInt32(outBuffer, 0);
68 } else {
69 MtpPacketTool::PutAUInt32(outBuffer, handles->data(), handles->size());
70 }
71 return MTP_SUCCESS;
72 }
73
CalculateSize()74 uint32_t GetObjectReferencesData::CalculateSize()
75 {
76 std::vector<uint8_t> tmpVar;
77 int res = Maker(tmpVar);
78 if (res != MTP_SUCCESS) {
79 return res;
80 }
81 return tmpVar.size();
82 }
83
SetObjectHandles(std::shared_ptr<UInt32List> & objectHandles)84 bool GetObjectReferencesData::SetObjectHandles(std::shared_ptr<UInt32List> &objectHandles)
85 {
86 if (hasSetObjectHandles_) {
87 return false;
88 }
89 hasSetObjectHandles_ = true;
90 objectHandles_ = objectHandles;
91 return true;
92 }
93
GetObjectHandles()94 std::shared_ptr<UInt32List> GetObjectReferencesData::GetObjectHandles()
95 {
96 if (!hasSetObjectHandles_) {
97 return nullptr;
98 }
99 return objectHandles_;
100 }
101 } // namespace Media
102 } // namespace OHOS