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_storage_info_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 {
GetStorageInfoData(shared_ptr<MtpOperationContext> & context)22 GetStorageInfoData::GetStorageInfoData(shared_ptr<MtpOperationContext>& context)
23     : PayloadData(context)
24 {
25 }
26 
GetStorageInfoData()27 GetStorageInfoData::GetStorageInfoData()
28 {
29 }
30 
~GetStorageInfoData()31 GetStorageInfoData::~GetStorageInfoData()
32 {
33 }
34 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)35 int GetStorageInfoData::Parser(const std::vector<uint8_t>& buffer, int32_t readSize)
36 {
37     if (context_ == nullptr) {
38         return MTP_ERROR_CONTEXT_IS_NULL;
39     }
40     if ((readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE < 1) { // param num < 1
41         return MTP_ERROR_PACKET_INCORRECT;
42     }
43     size_t offset = MTP_CONTAINER_HEADER_SIZE;
44     context_->storageInfoID = MtpPacketTool::GetUInt32(buffer, offset);
45     return MTP_SUCCESS;
46 }
47 
Maker(std::vector<uint8_t> & outBuffer)48 int GetStorageInfoData::Maker(std::vector<uint8_t>& outBuffer)
49 {
50     if (storage_ != nullptr) {
51         MEDIA_INFO_LOG("storageID_ = %{public}d", storage_->GetStorageID());
52         MtpPacketTool::PutUInt16(outBuffer, storage_->GetStorageType());
53         MtpPacketTool::PutUInt16(outBuffer, storage_->GetFilesystemType());
54         MtpPacketTool::PutUInt16(outBuffer, storage_->GetAccessCapability());
55         MtpPacketTool::PutUInt64(outBuffer, storage_->GetMaxCapacity());
56         MtpPacketTool::PutUInt64(outBuffer, storage_->GetFreeSpaceInBytes());
57         MtpPacketTool::PutUInt32(outBuffer, storage_->GetFreeSpaceInObjects());
58         MtpPacketTool::PutString(outBuffer, storage_->GetStorageDescription());
59         MtpPacketTool::PutString(outBuffer, storage_->GetVolumeIdentifier());
60     }
61     return MTP_SUCCESS;
62 }
63 
CalculateSize()64 uint32_t GetStorageInfoData::CalculateSize()
65 {
66     std::vector<uint8_t> tmpVar;
67     int res = Maker(tmpVar);
68     if (res != MTP_SUCCESS) {
69         return res;
70     }
71     return tmpVar.size();
72 }
73 
SetStorage(const std::shared_ptr<Storage> & storage)74 void GetStorageInfoData::SetStorage(const std::shared_ptr<Storage> &storage)
75 {
76     storage_ = storage;
77 }
78 } // namespace Media
79 } // namespace OHOS