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 
16 #include "payload_data/open_session_data.h"
17 #include "media_log.h"
18 #include "media_mtp_utils.h"
19 #include "mtp_constants.h"
20 #include "mtp_packet_tools.h"
21 using namespace std;
22 
23 namespace OHOS {
24 namespace Media {
OpenSessionData(shared_ptr<MtpOperationContext> & context)25 OpenSessionData::OpenSessionData(shared_ptr<MtpOperationContext> &context)
26     :PayloadData(context)
27 {
28 }
29 
~OpenSessionData()30 OpenSessionData::~OpenSessionData()
31 {
32 }
33 
Parser(const vector<uint8_t> & buffer,int32_t readSize)34 int OpenSessionData::Parser(const vector<uint8_t> &buffer, int32_t readSize)
35 {
36     if (context_ == nullptr) {
37         return MTP_FAIL;
38     }
39     if ((readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE < 1) { // param num < 1
40         return MTP_FAIL;
41     }
42     size_t offset = MTP_CONTAINER_HEADER_SIZE;
43     context_->tempSessionID = MtpPacketTool::GetUInt32(buffer, offset);
44     return MTP_SUCCESS;
45 }
46 
Maker(vector<uint8_t> & outBuffer)47 int OpenSessionData::Maker(vector<uint8_t> &outBuffer)
48 {
49     if (sessionID_ > 0) {
50         MtpPacketTool::PutUInt32(outBuffer, sessionID_);
51     }
52     return MTP_SUCCESS;
53 }
54 
CalculateSize()55 uint32_t OpenSessionData::CalculateSize()
56 {
57     std::vector<uint8_t> tmpVar;
58     if (sessionID_ > 0) {
59         MtpPacketTool::PutUInt32(tmpVar, sessionID_);
60     }
61     return tmpVar.size();
62 }
63 
SetSessionId(uint32_t sessionID)64 void OpenSessionData::SetSessionId(uint32_t sessionID)
65 {
66     sessionID_ = sessionID;
67 }
68 } // namespace Media
69 } // namespace OHOS