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_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
GetObjectData(std::shared_ptr<MtpOperationContext> & context)25 GetObjectData::GetObjectData(std::shared_ptr<MtpOperationContext> &context)
26 : PayloadData(context)
27 {
28 }
29
GetObjectData()30 GetObjectData::GetObjectData()
31 {
32 }
33
~GetObjectData()34 GetObjectData::~GetObjectData()
35 {
36 }
37
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)38 int GetObjectData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
39 {
40 if ((context_ == nullptr) || (!MtpStorageManager::GetInstance()->HasStorage())) {
41 MEDIA_ERR_LOG("GetObjectData::parser null or storage");
42 return MTP_INVALID_OBJECTHANDLE_CODE;
43 }
44
45 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
46 if (parameterCount < PARSER_PARAM_SUM) {
47 MEDIA_ERR_LOG("GetObjectData::parser paramCount=%{public}u, needCount=%{public}d",
48 parameterCount, PARSER_PARAM_SUM);
49 return MTP_INVALID_PARAMETER_CODE;
50 }
51
52 size_t offset = MTP_CONTAINER_HEADER_SIZE;
53 context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
54 context_->offset = 0;
55 context_->length = 0;
56 return MTP_SUCCESS;
57 }
58
Maker(std::vector<uint8_t> & outBuffer)59 int GetObjectData::Maker(std::vector<uint8_t> &outBuffer)
60 {
61 return MTP_SUCCESS;
62 }
63
CalculateSize()64 uint32_t GetObjectData::CalculateSize()
65 {
66 return 0;
67 }
68
SetResult(uint32_t result)69 bool GetObjectData::SetResult(uint32_t result)
70 {
71 if (hasSetResult_) {
72 return false;
73 }
74
75 hasSetResult_ = true;
76 result_ = result;
77 return true;
78 }
79 } // namespace Media
80 } // namespace OHOS