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_num_objects_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 = 3;
24
GetNumObjectsData(std::shared_ptr<MtpOperationContext> & context)25 GetNumObjectsData::GetNumObjectsData(std::shared_ptr<MtpOperationContext> &context)
26 : PayloadData(context)
27 {
28 }
29
GetNumObjectsData()30 GetNumObjectsData::GetNumObjectsData()
31 {
32 }
33
~GetNumObjectsData()34 GetNumObjectsData::~GetNumObjectsData()
35 {
36 }
37
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)38 int GetNumObjectsData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
39 {
40 if (context_ == nullptr) {
41 MEDIA_ERR_LOG("context_ is null");
42 return MTP_ERROR_CONTEXT_IS_NULL;
43 }
44
45 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
46 if (parameterCount < PARSER_PARAM_SUM) {
47 MEDIA_ERR_LOG("paramCount=%{public}u, needCount=%{public}d", parameterCount, PARSER_PARAM_SUM);
48 return MTP_INVALID_PARAMETER_CODE;
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
56 if (!MtpStorageManager::GetInstance()->HasStorage(context_->storageID)) {
57 MEDIA_ERR_LOG("no match storage");
58 return MTP_ERROR_INVALID_STORAGE_ID;
59 }
60 return MTP_SUCCESS;
61 }
62
Maker(std::vector<uint8_t> & outBuffer)63 int GetNumObjectsData::Maker(std::vector<uint8_t> &outBuffer)
64 {
65 auto count = GetNum();
66 if (count >= 0) {
67 MtpPacketTool::PutUInt32(outBuffer, count);
68 return MTP_OK_CODE;
69 } else {
70 MtpPacketTool::PutUInt32(outBuffer, 0);
71 return MTP_INVALID_OBJECTHANDLE_CODE;
72 }
73 return MTP_SUCCESS;
74 }
75
CalculateSize()76 uint32_t GetNumObjectsData::CalculateSize()
77 {
78 std::vector<uint8_t> tmpVar;
79
80 int res = Maker(tmpVar);
81 if (res != MTP_SUCCESS) {
82 return res;
83 }
84
85 uint32_t size = tmpVar.size();
86 return size;
87 }
88
SetNum(int num)89 bool GetNumObjectsData::SetNum(int num)
90 {
91 if (hasSetNum_) {
92 return false;
93 }
94
95 hasSetNum_ = true;
96 num_ = num;
97 return true;
98 }
99
GetNum()100 int GetNumObjectsData::GetNum()
101 {
102 if (!hasSetNum_) {
103 return -1;
104 }
105
106 return num_;
107 }
108 } // namespace Media
109 } // namespace OHOS