• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "MtpOperationUtils"
16 #include "mtp_operation_utils.h"
17 #include <fstream>
18 #include <cstdint>
19 #include <cinttypes>
20 #include <iremote_object.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include "application_context.h"
26 #include "ability_manager_client.h"
27 #ifdef HAS_BATTERY_MANAGER_PART
28 #include "battery_srv_client.h"
29 #endif
30 #include "directory_ex.h"
31 #include "iservice_registry.h"
32 #include "media_log.h"
33 #include "media_mtp_utils.h"
34 #include "mtp_dfx_reporter.h"
35 #include "mtp_manager.h"
36 #include "mtp_packet_tools.h"
37 #include "mtp_operation_context.h"
38 #include "mtp_ptp_proxy.h"
39 #include "mtp_storage_manager.h"
40 #include "mtp_store_observer.h"
41 #include "payload_data.h"
42 #include "payload_data/resp_common_data.h"
43 #include "payload_data/close_session_data.h"
44 #include "payload_data/copy_object_data.h"
45 #include "payload_data/delete_object_data.h"
46 #include "payload_data/get_device_info_data.h"
47 #include "payload_data/get_device_prop_desc_data.h"
48 #include "payload_data/get_device_prop_value_data.h"
49 #include "payload_data/get_num_objects_data.h"
50 #include "payload_data/get_object_data.h"
51 #include "payload_data/get_object_handles_data.h"
52 #include "payload_data/get_object_info_data.h"
53 #include "payload_data/get_object_prop_list_data.h"
54 #include "payload_data/get_object_prop_desc_data.h"
55 #include "payload_data/get_object_prop_value_data.h"
56 #include "payload_data/get_object_props_supported_data.h"
57 #include "payload_data/get_object_references_data.h"
58 #include "payload_data/get_partial_object_data.h"
59 #include "payload_data/get_storage_info_data.h"
60 #include "payload_data/get_storage_ids_data.h"
61 #include "payload_data/get_thumb_data.h"
62 #include "payload_data/move_object_data.h"
63 #include "payload_data/object_event_data.h"
64 #include "payload_data/open_session_data.h"
65 #include "payload_data/send_object_data.h"
66 #include "payload_data/send_object_info_data.h"
67 #include "payload_data/set_device_prop_value_data.h"
68 #include "payload_data/set_object_prop_value_data.h"
69 #include "payload_data/set_object_references_data.h"
70 #include "parameters.h"
71 #include "storage.h"
72 #include "system_ability_definition.h"
73 using namespace std;
74 namespace OHOS {
75 namespace Media {
76 #ifdef HAS_BATTERY_MANAGER_PART
77 static constexpr int MAX_BATTERY = 100;
78 static constexpr int ERROR_BATTERY = -1;
79 #endif
80 static constexpr int EMPTY_BATTERY = 0;
81 static constexpr int STORAGE_MANAGER_UID = 5003;
82 static constexpr int RECEVIE_OBJECT_CANCELLED = -20;
83 static constexpr int RECEVIE_OBJECT_FAILED = -17;
84 const std::string PUBLIC_DOC = "/storage/media/local/files/Docs";
85 
86 static constexpr uint32_t HEADER_LEN = 12;
87 static constexpr uint32_t READ_LEN = 1024;
88 static constexpr uint32_t SEND_OBJECT_FILE_MAX_SIZE = 0xFFFFFFFF;
89 constexpr int32_t PATH_TIMEVAL_MAX = 2;
90 static bool g_isDevicePropSet = false;
91 
MtpOperationUtils(const shared_ptr<MtpOperationContext> & context,bool isInit)92 MtpOperationUtils::MtpOperationUtils(const shared_ptr<MtpOperationContext> &context, bool isInit) : context_(context)
93 {
94     if (isInit) {
95         auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
96         CHECK_AND_RETURN_LOG(saManager != nullptr, "GetSystemAbilityManager failed, saManager is null");
97 
98         auto token = saManager->GetSystemAbility(STORAGE_MANAGER_UID);
99         MtpPtpProxy::GetInstance().Init(token, context);
100         g_isDevicePropSet = false;
101     }
102 }
103 
~MtpOperationUtils()104 MtpOperationUtils::~MtpOperationUtils()
105 {
106 }
107 
GetDeviceInfo(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)108 uint16_t MtpOperationUtils::GetDeviceInfo(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
109 {
110     if (containerType != DATA_CONTAINER_TYPE) {
111         data = make_shared<RespCommonData>();
112         return CheckErrorCode(errorCode);
113     }
114 
115     shared_ptr<GetDeviceInfoData> getDeviceInfoData = make_shared<GetDeviceInfoData>();
116     getDeviceInfoData->SetManufacturer(GetPropertyInner("const.product.manufacturer",
117         DEFAULT_PRODUCT_MANUFACTURER));
118     getDeviceInfoData->SetModel(GetPropertyInner("const.product.model", DEFAULT_PRODUCT_MODEL));
119     getDeviceInfoData->SetVersion(GetPropertyInner("const.product.software.version",
120         DEFAULT_PRODUCT_SOFTWARE_VERSION));
121     getDeviceInfoData->SetSerialNum(GetPropertyInner("ohos.boot.sn", "0"));
122     data = getDeviceInfoData;
123     errorCode = MTP_SUCCESS;
124     return MTP_OK_CODE;
125 }
126 
GetNumObjects(shared_ptr<PayloadData> & data)127 uint16_t MtpOperationUtils::GetNumObjects(shared_ptr<PayloadData> &data)
128 {
129     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
130         "GetNumObjects context_ is null");
131 
132     CHECK_AND_RETURN_RET(MtpStorageManager::GetInstance()->HasStorage(context_->storageID),
133         MTP_INVALID_STORAGEID_CODE);
134 
135     shared_ptr<GetNumObjectsData> getNumObjects = make_shared<GetNumObjectsData>();
136     data = getNumObjects;
137     return CheckErrorCode(MTP_SUCCESS);
138 }
139 
HasStorage(int & errorCode)140 uint16_t MtpOperationUtils::HasStorage(int &errorCode)
141 {
142     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_ERROR_CONTEXT_IS_NULL,
143         "GetObjectHandles context_ is null");
144     CHECK_AND_RETURN_RET_LOG(context_->sessionOpen != false, MTP_ERROR_SESSION_NOT_OPEN,
145         "GetObjectHandles session not open");
146     CHECK_AND_RETURN_RET_LOG(MtpStorageManager::GetInstance()->HasStorage(context_->storageID),
147         MTP_ERROR_INVALID_STORAGE_ID, "GetObjectHandles no this storage");
148     return MTP_SUCCESS;
149 }
150 
GetObjectHandles(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)151 uint16_t MtpOperationUtils::GetObjectHandles(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
152 {
153     if (containerType != DATA_CONTAINER_TYPE) {
154         data = make_shared<RespCommonData>();
155         return CheckErrorCode(errorCode);
156     }
157 
158     uint16_t ret = HasStorage(errorCode);
159     CHECK_AND_RETURN_RET(ret == MTP_SUCCESS, CheckErrorCode(ret));
160     // Determine whether the device is a Mac computer
161     // WIN/MAC parent is 0, Linux parent is 0xffffffff
162     // WIN set device prop, MAC/Linux do not set device prop
163     bool isMac = (context_->parent == 0 && !g_isDevicePropSet) ? true : false;
164     if (context_->parent == MTP_ALL_HANDLE_ID) {
165         context_->parent = 0;
166     }
167     shared_ptr<UInt32List> objectHandles = make_shared<UInt32List>();
168     errorCode = MtpPtpProxy::GetInstance().GetHandles(context_, objectHandles, isMac);
169     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS,
170         CheckErrorCode(errorCode), "GetObjectHandles GetHandles fail!");
171 
172     shared_ptr<GetObjectHandlesData> getObjectHandles = make_shared<GetObjectHandlesData>();
173     getObjectHandles->SetObjectHandles(objectHandles);
174     data = getObjectHandles;
175     errorCode = MTP_SUCCESS;
176     return CheckErrorCode(errorCode);
177 }
178 
GetObjectInfo(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)179 uint16_t MtpOperationUtils::GetObjectInfo(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
180 {
181     if (containerType != DATA_CONTAINER_TYPE) {
182         data = make_shared<RespCommonData>();
183         return CheckErrorCode(errorCode);
184     }
185 
186     uint16_t ret = HasStorage(errorCode);
187     CHECK_AND_RETURN_RET(errorCode == MTP_SUCCESS, CheckErrorCode(ret));
188 
189     shared_ptr<ObjectInfo> objectInfo = make_shared<ObjectInfo>(context_->handle);
190     errorCode = MtpPtpProxy::GetInstance().GetObjectInfo(context_, objectInfo);
191     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS,
192         CheckErrorCode(errorCode), "GetObjectHandles GetObjectInfo fail!");
193     shared_ptr<GetObjectInfoData> getObjectInfo = make_shared<GetObjectInfoData>();
194     getObjectInfo->SetObjectInfo(objectInfo);
195     data = getObjectInfo;
196     errorCode = MTP_SUCCESS;
197     return CheckErrorCode(errorCode);
198 }
199 
GetObjectPropDesc(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)200 uint16_t MtpOperationUtils::GetObjectPropDesc(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
201 {
202     if (containerType != DATA_CONTAINER_TYPE) {
203         data = make_shared<RespCommonData>();
204         return CheckErrorCode(errorCode);
205     }
206 
207     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
208         "GetObjectPropDesc context_ is null");
209 
210     data = make_shared<GetObjectPropDescData>(context_);
211     errorCode = MTP_SUCCESS;
212     return CheckErrorCode(errorCode);
213 }
214 
GetObjectPropValue(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)215 uint16_t MtpOperationUtils::GetObjectPropValue(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
216 {
217     if (containerType != DATA_CONTAINER_TYPE) {
218         data = make_shared<RespCommonData>();
219         return CheckErrorCode(errorCode);
220     }
221 
222     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
223         "GetObjectPropValue context_ is null");
224 
225     int type = MTP_TYPE_UNDEFINED_CODE;
226     // GetObjectPropValue may have 3 types of return value, using params to return in one function
227     uint64_t int64Value = 0;
228     uint128_t int128Value = {0};
229     string strValue;
230     errorCode = MtpPtpProxy::GetInstance().GetObjectPropValue(context_, int64Value, int128Value, strValue);
231     shared_ptr<GetObjectPropValueData> getObjectPropValue = make_shared<GetObjectPropValueData>(context_);
232     type = MtpPacketTool::GetObjectPropTypeByPropCode(context_->property);
233     getObjectPropValue->SetPropValue(type, int64Value, int128Value, strValue);
234     data = getObjectPropValue;
235     errorCode = MTP_SUCCESS;
236     return CheckErrorCode(errorCode);
237 }
238 
DoSetObjectPropValue(int & errorCode)239 void MtpOperationUtils::DoSetObjectPropValue(int &errorCode)
240 {
241     if (context_ == nullptr) {
242         MEDIA_ERR_LOG("SetObjectPropValue context_ is null");
243         errorCode = MTP_ERROR_CONTEXT_IS_NULL;
244         return;
245     }
246 
247     errorCode = MtpPtpProxy::GetInstance().SetObjectPropValue(context_);
248     SendEventPacket(context_->handle, MTP_EVENT_OBJECT_ADDED_CODE);
249 }
250 
SendEventPacket(uint32_t objectHandle,uint16_t eventCode)251 void MtpOperationUtils::SendEventPacket(uint32_t objectHandle, uint16_t eventCode)
252 {
253     CHECK_AND_RETURN_LOG(context_ != nullptr, "SendEventPacket context_ is null");
254 
255     EventMtp event;
256     event.length = MTP_CONTAINER_HEADER_SIZE + sizeof(objectHandle);
257     vector<uint8_t> outBuffer;
258     MtpPacketTool::PutUInt32(outBuffer, event.length);
259     MtpPacketTool::PutUInt16(outBuffer, EVENT_CONTAINER_TYPE);
260     MtpPacketTool::PutUInt16(outBuffer, eventCode);
261     MtpPacketTool::PutUInt32(outBuffer, context_->transactionID);
262     MtpPacketTool::PutUInt32(outBuffer, objectHandle);
263 
264     event.data = outBuffer;
265     CHECK_AND_RETURN_LOG(context_->mtpDriver != nullptr, "SendEventPacket mtpDriver is null");
266     auto startTime = std::chrono::high_resolution_clock::now();
267     int32_t result = context_->mtpDriver->WriteEvent(event);
268     auto endTime = std::chrono::high_resolution_clock::now();
269     std::chrono::duration<uint16_t, std::milli> duration =
270         std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
271     MtpDfxReporter::GetInstance().DoSendResponseResultDfxReporter(eventCode, result,
272         duration.count(), OperateMode::writemode);
273 }
274 
GetObjectPropList(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)275 uint16_t MtpOperationUtils::GetObjectPropList(shared_ptr<PayloadData> &data,
276     uint16_t containerType, int &errorCode)
277 {
278     if (containerType != DATA_CONTAINER_TYPE) {
279         data = make_shared<RespCommonData>();
280         return CheckErrorCode(errorCode);
281     }
282 
283     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
284         "GetObjectPropList context_ is null");
285 
286     shared_ptr<vector<Property>> props = make_shared<vector<Property>>();
287     errorCode = MtpPtpProxy::GetInstance().GetObjectPropList(context_, props);
288     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, CheckErrorCode(errorCode), "GetObjectPropList fail!");
289 
290     shared_ptr<GetObjectPropListData> getObjectPropList = make_shared<GetObjectPropListData>(context_);
291     getObjectPropList->SetProps(props);
292     data = getObjectPropList;
293     errorCode = MTP_SUCCESS;
294     return CheckErrorCode(errorCode);
295 }
296 
GetObjectReferences(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)297 uint16_t MtpOperationUtils::GetObjectReferences(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
298 {
299     if (containerType != DATA_CONTAINER_TYPE) {
300         data = make_shared<RespCommonData>();
301         return CheckErrorCode(errorCode);
302     }
303 
304     uint16_t ret = HasStorage(errorCode);
305     CHECK_AND_RETURN_RET(ret == MTP_SUCCESS, CheckErrorCode(ret));
306 
307     shared_ptr<UInt32List> objectHandles = nullptr;
308     shared_ptr<GetObjectReferencesData> getObjectReferences = make_shared<GetObjectReferencesData>(context_);
309     getObjectReferences->SetObjectHandles(objectHandles);
310     data = getObjectReferences;
311     errorCode = MTP_SUCCESS;
312     return CheckErrorCode(errorCode);
313 }
314 
SetObjectReferences(shared_ptr<PayloadData> & data)315 uint16_t MtpOperationUtils::SetObjectReferences(shared_ptr<PayloadData> &data)
316 {
317     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
318         "SetObjectReferences context_ is null");
319 
320     uint16_t result = MTP_INVALID_OBJECTPROP_FORMAT_CODE;
321     shared_ptr<SetObjectReferencesData> setObjectReferences = make_shared<SetObjectReferencesData>(context_);
322     setObjectReferences->SetResult(result);
323     data = setObjectReferences;
324     return CheckErrorCode(MTP_SUCCESS);
325 }
326 
GetObjectDataDeal()327 uint16_t MtpOperationUtils::GetObjectDataDeal()
328 {
329     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_ERROR_CONTEXT_IS_NULL, "GetObjectDataDeal context_ is null");
330 
331     int fd = 0;
332     int errorCode = 0;
333     if (MtpManager::GetInstance().IsMtpMode() && !MtpPtpProxy::GetInstance().IsMtpExistObject(context_)) {
334         SendEventPacket(context_->handle, MTP_EVENT_OBJECT_REMOVED_CODE);
335         return MTP_INVALID_OBJECTHANDLE_CODE;
336     }
337     errorCode = MtpPtpProxy::GetInstance().GetReadFd(context_, fd);
338     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, errorCode, "GetObjectDataDeal GetFd fail!");
339 
340     MtpFileRange object;
341     object.fd = fd;
342     struct stat sstat;
343     int result = fstat(object.fd, &sstat);
344     PreDealFd(result < 0, fd);
345     CHECK_AND_RETURN_RET_LOG(result == MTP_SUCCESS, MTP_ERROR_INCOMPLETE_TRANSFER,
346         "GetObjectDataDeal fstat error = %{public}d", errno);
347 
348     object.offset = static_cast<loff_t>(context_->offset);
349     if (context_->length == 0 || context_->length == MTP_ALL_HANDLE_ID) {
350         object.length = sstat.st_size;
351     } else {
352         if (context_->offset > static_cast<uint64_t>(sstat.st_size)) {
353             context_->length = 0;
354             MEDIA_WARN_LOG("GetObjectDataDeal offset is larger than file size, set length to 0");
355         } else if (context_->offset + context_->length > static_cast<uint64_t>(sstat.st_size)) {
356             context_->length = static_cast<uint32_t>(static_cast<uint64_t>(sstat.st_size) - context_->offset);
357         }
358         object.length = context_->length;
359     }
360     object.command = context_->operationCode;
361     object.transaction_id = context_->transactionID;
362     result = context_->mtpDriver->SendObj(object);
363     PreDealFd(result < 0, fd);
364     CHECK_AND_RETURN_RET_LOG(result >= 0, MTP_ERROR_INCOMPLETE_TRANSFER,
365         "GetObjectDataDeal SendObj error!");
366     int32_t ret = MtpPtpProxy::GetInstance().CloseReadFd(context_, fd);
367     CHECK_AND_RETURN_RET_LOG(ret == MTP_SUCCESS, E_ERR, "DealFd CloseFd fail!");
368     return MTP_SUCCESS;
369 }
370 
GetObject(shared_ptr<PayloadData> & data,int errorCode)371 uint16_t MtpOperationUtils::GetObject(shared_ptr<PayloadData> &data, int errorCode)
372 {
373     data = make_shared<GetObjectData>();
374     return CheckErrorCode(errorCode);
375 }
376 
ModifyObjectInfo()377 void MtpOperationUtils::ModifyObjectInfo()
378 {
379     CHECK_AND_RETURN_LOG(context_ != nullptr, "DoRecevieSendObject context_ is null");
380 
381     std::tm tmCreated = {};
382     std::tm tmModified = {};
383     std::istringstream created(context_->created);
384     std::istringstream modified(context_->modified);
385     created >> std::get_time(&tmCreated, "%Y%m%dT%H%M%S");
386     modified >> std::get_time(&tmModified, "%Y%m%dT%H%M%S");
387     bool cond = (created.fail() || modified.fail());
388     CHECK_AND_RETURN_LOG(!cond, "get_time failed");
389 
390     std::string path;
391     MtpPtpProxy::GetInstance().GetModifyObjectInfoPathById(context_->handle, path);
392 
393     struct timeval times[PATH_TIMEVAL_MAX] = { { 0, 0 }, { 0, 0 } };
394     times[0].tv_sec = mktime(&tmCreated);
395     times[1].tv_sec = mktime(&tmModified);
396     if (utimes(path.c_str(), times) != 0) {
397         MEDIA_WARN_LOG("utimes path:%{public}s failed", path.c_str());
398     }
399 }
400 
DoRecevieSendObject()401 int32_t MtpOperationUtils::DoRecevieSendObject()
402 {
403     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_INVALID_PARAMETER_CODE, "DoRecevieSendObject context_ is null");
404 
405     vector<uint8_t> dataBuffer;
406     uint32_t temp = READ_LEN;
407     int errorCode = context_->mtpDriver->Read(dataBuffer, temp);
408     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, errorCode, "DoRecevieSendObject Read error!");
409 
410     int fd = 0;
411     errorCode = MtpPtpProxy::GetInstance().GetWriteFd(context_, fd);
412     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, errorCode, "DoRecevieSendObject GetFd fail!");
413 
414     uint32_t initialData = dataBuffer.size() < HEADER_LEN  ? 0 : dataBuffer.size() - HEADER_LEN;
415     errorCode = write(fd, &dataBuffer[HEADER_LEN], initialData);
416     PreDealFd(errorCode < 0, fd);
417     CHECK_AND_RETURN_RET_LOG(errorCode >= 0, MTP_ERROR_RESPONSE_GENERAL,
418         "DoRecevieSendObject write error = %{public}d", errno);
419 
420     MtpFileRange object;
421     object.fd = fd;
422     object.offset = initialData;
423     if (context_->sendObjectFileSize == SEND_OBJECT_FILE_MAX_SIZE) {
424         // when file size is over 0xFFFFFFFF, driver will read until it receives a short packet
425         object.length = SEND_OBJECT_FILE_MAX_SIZE;
426     } else {
427         object.length = static_cast<int64_t>(context_->sendObjectFileSize) - static_cast<int64_t>(initialData);
428     }
429     errorCode = RecevieSendObject(object, fd);
430     CHECK_AND_RETURN_RET_LOG(errorCode != MTP_ERROR_TRANSFER_CANCELLED, MTP_ERROR_TRANSFER_CANCELLED,
431         "DoRecevieSendObject ReceiveObj Cancelled = %{public}d", MTP_ERROR_TRANSFER_CANCELLED);
432     CHECK_AND_RETURN_RET_LOG(errorCode != MTP_ERROR_TRANSFER_FAILED, MTP_ERROR_TRANSFER_FAILED,
433         "DoRecevieSendObject ReceiveObj Failed = %{public}d", MTP_ERROR_TRANSFER_FAILED);
434     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, MTP_ERROR_RESPONSE_GENERAL,
435         "DoRecevieSendObject ReceiveObj fail errorCode = %{public}d", errorCode);
436 
437     ModifyObjectInfo();
438     errorCode = fsync(fd);
439     PreDealFd(errorCode != MTP_SUCCESS, fd);
440     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, MTP_ERROR_RESPONSE_GENERAL,
441         "DoRecevieSendObject fsync eroor = %{public}d", errno);
442     struct stat sstat;
443     errorCode = fstat(fd, &sstat);
444     PreDealFd(errorCode != MTP_SUCCESS, fd);
445     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, MTP_ERROR_RESPONSE_GENERAL,
446         "DoRecevieSendObject fstat error = %{public}d", errno);
447 
448     errorCode = MtpPtpProxy::GetInstance().CloseWriteFd(context_, fd);
449     CHECK_AND_RETURN_RET_LOG(errorCode == MTP_SUCCESS, errorCode, "DoRecevieSendObject CloseFd fail!");
450 
451     return MTP_SUCCESS;
452 }
453 
RecevieSendObject(MtpFileRange & object,int fd)454 int32_t MtpOperationUtils::RecevieSendObject(MtpFileRange &object, int fd)
455 {
456     MEDIA_DEBUG_LOG("RecevieSendObject begin");
457     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_INVALID_PARAMETER_CODE, "DoRecevieSendObject context_ is null");
458     CHECK_AND_RETURN_RET_LOG(context_->mtpDriver != nullptr, MTP_INVALID_PARAMETER_CODE,
459         "DoRecevieSendObject context_->mtpDriver is null");
460 
461     int32_t errorCode = context_->mtpDriver->ReceiveObj(object);
462     bool cond = (errorCode != RECEVIE_OBJECT_CANCELLED && errorCode != RECEVIE_OBJECT_FAILED);
463     CHECK_AND_RETURN_RET(!cond, errorCode);
464 
465     PreDealFd(errorCode != MTP_SUCCESS, fd);
466     string filePath("");
467     if (MtpManager::GetInstance().IsMtpMode()) {
468         MtpPtpProxy::GetInstance().GetMtpPathById(context_->handle, filePath);
469         CHECK_AND_RETURN_RET_LOG(!filePath.empty(), MTP_ERROR_TRANSFER_CANCELLED,
470             "File path is invalid!");
471         int ret = unlink(filePath.c_str());
472         CHECK_AND_RETURN_RET_LOG(ret == 0, MTP_ERROR_TRANSFER_CANCELLED, "unlink file fail");
473     }
474     MtpPtpProxy::GetInstance().DeleteCanceledObject(filePath, context_->handle);
475     CHECK_AND_RETURN_RET(errorCode != RECEVIE_OBJECT_FAILED, MTP_ERROR_TRANSFER_FAILED);
476     return MTP_ERROR_TRANSFER_CANCELLED;
477 }
478 
PreDealFd(const bool deal,const int fd)479 void MtpOperationUtils::PreDealFd(const bool deal, const int fd)
480 {
481     if (!deal || fd <= 0) {
482         return;
483     }
484     int32_t ret = MtpPtpProxy::GetInstance().CloseReadFd(context_, fd);
485     CHECK_AND_PRINT_LOG(ret == MTP_SUCCESS, "DealFd CloseFd fail!");
486 }
487 
GetThumb(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)488 uint16_t MtpOperationUtils::GetThumb(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
489 {
490     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, MTP_INVALID_PARAMETER_CODE, "DoRecevieSendObject context_ is null");
491 
492     if (containerType != DATA_CONTAINER_TYPE) {
493         data = make_shared<RespCommonData>();
494         return CheckErrorCode(errorCode);
495     }
496 
497     uint16_t ret = HasStorage(errorCode);
498     CHECK_AND_RETURN_RET(ret == MTP_SUCCESS, CheckErrorCode(ret));
499 
500     shared_ptr<UInt8List> thumb = make_shared<UInt8List>();
501     errorCode = MtpPtpProxy::GetInstance().GetThumb(context_, thumb);
502     if (errorCode != MTP_SUCCESS) {
503         data = make_shared<RespCommonData>();
504         return CheckErrorCode(errorCode);
505     }
506 
507     shared_ptr<GetThumbData> getThumb = make_shared<GetThumbData>();
508     getThumb->SetThumb(thumb);
509     data = getThumb;
510     errorCode = MTP_SUCCESS;
511     return CheckErrorCode(errorCode);
512 }
513 
SendObjectInfo(shared_ptr<PayloadData> & data,int & errorCode)514 uint16_t MtpOperationUtils::SendObjectInfo(shared_ptr<PayloadData> &data, int &errorCode)
515 {
516     if (context_ == nullptr) {
517         MEDIA_ERR_LOG("MtpOperationUtils::SendObjectInfo param is null");
518         data = make_shared<RespCommonData>();
519         return CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL);
520     }
521 
522     auto manager = MtpStorageManager::GetInstance();
523     CHECK_AND_RETURN_RET_LOG(manager != nullptr, MTP_INVALID_PARAMETER_CODE, "MtpStorageManager instance is nullptr");
524 
525     // should reserve the space for the frame of the cmd 0x100d
526     if (context_->sendObjectFileSize + READ_LEN > manager->GetFreeSize()) {
527         data = make_shared<RespCommonData>();
528         MEDIA_DEBUG_LOG("SendObjectInfo run out of memory, sendObjectFileSize %{public}d",
529             context_->sendObjectFileSize);
530         MEDIA_DEBUG_LOG("SendObjectInfo run out of memory, FreeSpaceInBytes %{public}"
531             PRId64, manager->GetFreeSize());
532         return MTP_STORE_FULL_CODE;
533     }
534 
535     uint32_t storageID = 0;
536     uint32_t parent = 0;
537     uint32_t handle = 0;
538     errorCode = MtpPtpProxy::GetInstance().SendObjectInfo(context_, storageID, parent, handle);
539     if (errorCode != MTP_SUCCESS) {
540         MEDIA_ERR_LOG("MtpOperationUtils::SendObjectInfo fail!");
541         data = make_shared<RespCommonData>();
542         return CheckErrorCode(errorCode);
543     }
544     context_->handle = handle;
545     shared_ptr<SendObjectInfoData> sendObjectInfo = make_shared<SendObjectInfoData>();
546     sendObjectInfo->SetSetParam(storageID, parent, handle);
547     data = sendObjectInfo;
548     return CheckErrorCode(errorCode);
549 }
550 
GetRespCommonData(shared_ptr<PayloadData> & data,int errorCode)551 uint16_t MtpOperationUtils::GetRespCommonData(shared_ptr<PayloadData> &data, int errorCode)
552 {
553     data = make_shared<RespCommonData>();
554     return CheckErrorCode(errorCode);
555 }
556 
GetPartialObject(shared_ptr<PayloadData> & data)557 uint16_t MtpOperationUtils::GetPartialObject(shared_ptr<PayloadData> &data)
558 {
559     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
560         "GetPartialObject context_ is null");
561 
562     shared_ptr<GetPartialObjectData> getPartialObject = make_shared<GetPartialObjectData>(context_);
563     getPartialObject->SetLength(context_->length);
564     data = getPartialObject;
565     return MTP_OK_CODE;
566 }
567 
GetObjectPropsSupported(shared_ptr<PayloadData> & data)568 uint16_t MtpOperationUtils::GetObjectPropsSupported(shared_ptr<PayloadData> &data)
569 {
570     CHECK_AND_RETURN_RET_LOG(context_ != nullptr, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL),
571         "GetObjectPropsSupported context_ is null");
572 
573     data = make_shared<GetObjectPropsSupportedData>(context_);
574     return MTP_OK_CODE;
575 }
576 
DeleteObject(shared_ptr<PayloadData> & data,int & errorCode)577 uint16_t MtpOperationUtils::DeleteObject(shared_ptr<PayloadData> &data, int &errorCode)
578 {
579     if (context_ == nullptr) {
580         errorCode = MTP_ERROR_CONTEXT_IS_NULL;
581     } else {
582         MEDIA_ERR_LOG("MtpOperationUtils::DeleteObject format=%{public}u", context_->format);
583         errorCode = MtpPtpProxy::GetInstance().DeleteObject(context_);
584     }
585     data = make_shared<RespCommonData>();
586     return CheckErrorCode(errorCode);
587 }
588 
MoveObject(shared_ptr<PayloadData> & data,int & errorCode)589 uint16_t MtpOperationUtils::MoveObject(shared_ptr<PayloadData> &data, int &errorCode)
590 {
591     bool cond = (context_ == nullptr);
592     CHECK_AND_RETURN_RET_LOG(!cond, CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL), "param is null");
593 
594     uint32_t repeatHandle = 0;
595     errorCode = MtpPtpProxy::GetInstance().MoveObject(context_, repeatHandle);
596 
597     data = make_shared<RespCommonData>();
598     if (repeatHandle != 0) {
599         SendEventPacket(repeatHandle, MTP_EVENT_OBJECT_REMOVED_CODE);
600         MEDIA_INFO_LOG("MTP:Send Event MTP_EVENT_OBJECT_REMOVED_CODE,repeatHandle[%{public}d]", repeatHandle);
601     }
602     return CheckErrorCode(errorCode);
603 }
604 
CopyObject(shared_ptr<PayloadData> & data,int & errorCode)605 uint16_t MtpOperationUtils::CopyObject(shared_ptr<PayloadData> &data, int &errorCode)
606 {
607     if (context_ == nullptr) {
608         MEDIA_ERR_LOG("param is null");
609         data = make_shared<RespCommonData>();
610         return CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL);
611     }
612 
613     uint32_t objectHandle = 0;
614     uint32_t oldHandle = 0;
615     MtpPtpProxy::GetInstance().CopyObject(context_, objectHandle, oldHandle);
616 
617     shared_ptr<CopyObjectData> copyObject = make_shared<CopyObjectData>();
618     copyObject->SetObjectHandle(objectHandle);
619     data = copyObject;
620     if (oldHandle != 0) {
621         SendEventPacket(oldHandle, MTP_EVENT_OBJECT_REMOVED_CODE);
622         MEDIA_INFO_LOG("MTP:Send Event MTP_EVENT_OBJECT_REMOVED_CODE,oldHandle[%{public}d]", oldHandle);
623     }
624     return CheckErrorCode(errorCode);
625 }
626 
GetStorageIDs(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)627 uint16_t MtpOperationUtils::GetStorageIDs(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
628 {
629     if (containerType != DATA_CONTAINER_TYPE || context_ == nullptr) {
630         data = make_shared<RespCommonData>();
631         return CheckErrorCode(MTP_ERROR_CONTEXT_IS_NULL);
632     }
633 
634     CHECK_AND_RETURN_RET_LOG(context_->sessionOpen != false, CheckErrorCode(MTP_ERROR_SESSION_NOT_OPEN),
635         "session isn't open");
636     auto manager = MtpStorageManager::GetInstance();
637     CHECK_AND_RETURN_RET_LOG(manager != nullptr, MTP_PARAMETER_NOT_SUPPORTED_CODE,
638         "MtpStorageManager instance is nullptr");
639     if (MtpManager::GetInstance().IsMtpMode()) {
640         MtpStoreObserver::AttachContext(context_);
641         MtpPtpProxy::GetInstance().GetMtpStorageIds();
642     } else {
643         auto storage = make_shared<Storage>();
644         CHECK_AND_RETURN_RET_LOG(storage != nullptr, E_ERR, "storage is nullptr");
645         storage->SetStorageID(DEFAULT_STORAGE_ID);
646         storage->SetStorageType(MTP_STORAGE_FIXEDRAM);
647         storage->SetFilesystemType(MTP_FILESYSTEM_GENERICHIERARCHICAL);
648         storage->SetAccessCapability(MTP_ACCESS_READ_WRITE);
649         storage->SetMaxCapacity(manager->GetTotalSize(PUBLIC_DOC));
650         storage->SetFreeSpaceInBytes(manager->GetFreeSize(PUBLIC_DOC));
651         storage->SetFreeSpaceInObjects(0);
652         storage->SetStorageDescription(manager->GetStorageDescription(MTP_STORAGE_FIXEDRAM));
653         manager->AddStorage(storage);
654     }
655 
656     shared_ptr<GetStorageIdsData> getStorageIdsData = make_shared<GetStorageIdsData>();
657     getStorageIdsData->SetStorages(manager->GetStorages());
658     data = getStorageIdsData;
659     errorCode = MTP_SUCCESS;
660     return CheckErrorCode(errorCode);
661 }
662 
GetStorageInfo(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)663 uint16_t MtpOperationUtils::GetStorageInfo(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
664 {
665     if (containerType != DATA_CONTAINER_TYPE) {
666         data = make_shared<RespCommonData>();
667         return CheckErrorCode(errorCode);
668     }
669 
670     CHECK_AND_RETURN_RET_LOG(context_->sessionOpen != false, CheckErrorCode(MTP_ERROR_SESSION_NOT_OPEN),
671         "session isn't open error");
672 
673     shared_ptr<Storage> storage = MtpStorageManager::GetInstance()->GetStorage(context_->storageInfoID);
674     CHECK_AND_RETURN_RET_LOG(storage != nullptr, CheckErrorCode(MTP_ERROR_INVALID_STORAGE_ID),
675         "invalid storage id error");
676     shared_ptr<GetStorageInfoData> getStorageInfoData = make_shared<GetStorageInfoData>();
677     getStorageInfoData->SetStorage(storage);
678     data = getStorageInfoData;
679     errorCode = MTP_SUCCESS;
680     return MTP_OK_CODE;
681 }
682 
GetOpenSession(shared_ptr<PayloadData> & data,int errorCode)683 uint16_t MtpOperationUtils::GetOpenSession(shared_ptr<PayloadData> &data, int errorCode)
684 {
685     shared_ptr<OpenSessionData> openSessionData = make_shared<OpenSessionData>(context_);
686     uint16_t respCode = CheckErrorCode(errorCode);
687     if (respCode == MTP_SESSION_ALREADY_OPEN_CODE) {
688         openSessionData->SetSessionId(context_->sessionID);
689     }
690     if (respCode == MTP_OK_CODE) {
691         context_->sessionOpen = true;
692     }
693     data = openSessionData;
694     return respCode;
695 }
696 
CheckErrorCode(int errorCode)697 uint16_t MtpOperationUtils::CheckErrorCode(int errorCode)
698 {
699     switch (errorCode) {
700         case MTP_ERROR_PACKET_INCORRECT:
701             return MTP_INVALID_PARAMETER_CODE;
702         case MTP_ERROR_SESSION_ALREADY_OPEN:
703             return MTP_SESSION_ALREADY_OPEN_CODE;
704         case MTP_ERROR_NO_THIS_FILE:
705             return MTP_INVALID_OBJECTHANDLE_CODE;
706         case MTP_ERROR_INCOMPLETE_TRANSFER:
707             return MTP_INCOMPLETE_TRANSFER_CODE;
708         case MTP_ERROR_SESSION_NOT_OPEN:
709             return MTP_SESSION_NOT_OPEN_CODE;
710         case MTP_ERROR_INVALID_STORAGE_ID:
711             return MTP_INVALID_STORAGEID_CODE;
712         case MTP_ERROR_INVALID_OBJECTHANDLE:
713             return MTP_INVALID_OBJECTHANDLE_CODE;
714         case MTP_ERROR_DEVICEPROP_NOT_SUPPORTED:
715             return MTP_DEVICEPROP_NOT_SUPPORTED_CODE;
716         case MTP_ERROR_STORE_NOT_AVAILABLE:
717             return MTP_STORE_NOT_AVAILABLE_CODE;
718         case MTP_ERROR_INVALID_PARENTOBJECT:
719             return MTP_INVALID_PARENTOBJECT_CODE;
720         case MTP_ERROR_PARAMETER_NOT_SUPPORTED:
721             return MTP_PARAMETER_NOT_SUPPORTED_CODE;
722         case MTP_ERROR_INVALID_OBJECTPROP_VALUE:
723             return MTP_INVALID_OBJECTPROP_VALUE_CODE;
724         case MTP_ERROR_INVALID_OBJECTPROP_FORMAT:
725             return MTP_INVALID_OBJECTPROP_FORMAT_CODE;
726         case MTP_ERROR_INVALID_OBJECTPROPCODE:
727             return MTP_INVALID_OBJECTPROPCODE_CODE;
728         case MTP_ERROR_ACCESS_DENIED:
729             return MTP_ACCESS_DENIED_CODE;
730         case MTP_ERROR_SPECIFICATION_BY_GROUP_UNSUPPORTED:
731             return MTP_SPECIFICATION_BY_GROUP_UNSUPPORTED_CODE;
732         case MTP_ERROR_SPECIFICATION_BY_DEPTH_UNSUPPORTED:
733             return MTP_SPECIFICATION_BY_DEPTH_UNSUPPORTED_CODE;
734         case MTP_ERROR_TRANSFER_FAILED:
735             return MTP_STORE_FULL_CODE;
736         default:
737             return MTP_OK_CODE;
738     }
739 }
740 
GetCloseSession(shared_ptr<PayloadData> & data)741 uint16_t MtpOperationUtils::GetCloseSession(shared_ptr<PayloadData> &data)
742 {
743     data = make_shared<CloseSessionData>(context_);
744     return MTP_OK_CODE;
745 }
746 
GetPropDesc(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)747 uint16_t MtpOperationUtils::GetPropDesc(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
748 {
749     if (containerType != DATA_CONTAINER_TYPE) {
750         data = make_shared<RespCommonData>();
751         return CheckErrorCode(errorCode);
752     }
753 
754     shared_ptr<GetDevicePropDescData> devicePropDescData = make_shared<GetDevicePropDescData>();
755     shared_ptr<Property> property = nullptr;
756     switch (context_->property) {
757         case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER_CODE:
758             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_STR, true);
759             break;
760         case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME_CODE:
761             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_STR, true);
762             property->currentValue->str_ = make_shared<string>(GetPropertyInner("const.product.name",
763                 DEFAULT_PRODUCT_NAME));
764             break;
765         case MTP_DEVICE_PROPERTY_SESSION_INITIATOR_VERSION_INFO_CODE:
766             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_STR, true);
767             break;
768         case MTP_DEVICE_PROPERTY_IMAGE_SIZE_CODE:
769             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_STR, true);
770             break;
771         case MTP_DEVICE_PROPERTY_BATTERY_LEVEL_CODE:
772             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_UINT8);
773             property->currentValue->bin_.ui8 = (uint8_t)MtpOperationUtils::GetBatteryLevel();
774             property->SetFormRange(BATTERY_LEVEL_MIN, BATTERY_LEVEL_MAX, BATTERY_LEVEL_STEP);
775             break;
776         case MTP_DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE_CODE:
777             property = make_shared<Property>(context_->property, MTP_DEVICE_PROP_DESC_TYPE_UINT32);
778             break;
779         default:
780             MEDIA_INFO_LOG("property do not find");
781             break;
782     }
783 
784     devicePropDescData->SetProperty(property);
785     data = devicePropDescData;
786     errorCode = MTP_SUCCESS;
787     return MTP_OK_CODE;
788 }
789 
GetPropValue(shared_ptr<PayloadData> & data,uint16_t containerType,int & errorCode)790 uint16_t MtpOperationUtils::GetPropValue(shared_ptr<PayloadData> &data, uint16_t containerType, int &errorCode)
791 {
792     if (containerType != DATA_CONTAINER_TYPE) {
793         data = make_shared<RespCommonData>();
794         return CheckErrorCode(errorCode);
795     }
796 
797     shared_ptr<GetDevicePropValueData> devicePropValueData = make_shared<GetDevicePropValueData>();
798     shared_ptr<Property::Value> value = make_shared<Property::Value>();
799     uint16_t valueType = MTP_DEVICE_PROP_DESC_TYPE_UNDEFINED;
800     switch (context_->property) {
801         case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER_CODE:
802             valueType = MTP_DEVICE_PROP_DESC_TYPE_STR;
803             value->str_ = make_shared<string>("");
804             break;
805         case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME_CODE:
806             valueType = MTP_DEVICE_PROP_DESC_TYPE_STR;
807             value->str_ = make_shared<string>(GetPropertyInner("const.product.name", DEFAULT_PRODUCT_NAME));
808             break;
809         case MTP_DEVICE_PROPERTY_SESSION_INITIATOR_VERSION_INFO_CODE:
810             valueType = MTP_DEVICE_PROP_DESC_TYPE_STR;
811             value->str_ = make_shared<string>("");
812             break;
813         case MTP_DEVICE_PROPERTY_IMAGE_SIZE_CODE:
814             valueType = MTP_DEVICE_PROP_DESC_TYPE_STR;
815             value->str_ = make_shared<string>("");
816             break;
817         case MTP_DEVICE_PROPERTY_BATTERY_LEVEL_CODE:
818             valueType = MTP_DEVICE_PROP_DESC_TYPE_UINT8;
819             value->bin_.ui8 = (uint8_t)MtpOperationUtils::GetBatteryLevel();
820             break;
821         case MTP_DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE_CODE:
822             valueType = MTP_DEVICE_PROP_DESC_TYPE_UINT32;
823             value->bin_.ui32 = MTP_PERCEIVED_DEVICE_TYPE_GENERIC;
824             break;
825         default:
826             MEDIA_INFO_LOG("property do not find");
827             break;
828     }
829 
830     devicePropValueData->SetValue(valueType, value);
831     data = devicePropValueData;
832     errorCode = MTP_SUCCESS;
833     return MTP_OK_CODE;
834 }
835 
SetDevicePropValueResp(shared_ptr<PayloadData> & data)836 uint16_t MtpOperationUtils::SetDevicePropValueResp(shared_ptr<PayloadData> &data)
837 {
838     data = make_shared<RespCommonData>();
839     return MTP_OK_CODE;
840 }
841 
ResetDevicePropResp(shared_ptr<PayloadData> & data)842 uint16_t MtpOperationUtils::ResetDevicePropResp(shared_ptr<PayloadData> &data)
843 {
844     CHECK_AND_PRINT_LOG(SetPropertyInner("const.product.name", DEFAULT_PRODUCT_NAME), "SetPropertyInner fail");
845     data = make_shared<RespCommonData>();
846     return MTP_OK_CODE;
847 }
848 
ObjectEvent(shared_ptr<PayloadData> & data,const int32_t payload)849 uint16_t MtpOperationUtils::ObjectEvent(shared_ptr<PayloadData> &data, const int32_t payload)
850 {
851     std::shared_ptr<ObjectEventData> eventData = make_shared<ObjectEventData>();
852     eventData->SetPayload(payload);
853     data = eventData;
854     return MTP_OK_CODE;
855 }
856 
GetPathByHandle(const uint32_t & handle,string & path,string & realPath)857 uint16_t MtpOperationUtils::GetPathByHandle(const uint32_t &handle, string &path, string &realPath)
858 {
859     MtpPtpProxy::GetInstance().GetPathByHandle(handle, path, realPath);
860     return MTP_OK_CODE;
861 }
862 
GetHandleByPaths(string path,uint32_t & handle)863 int32_t MtpOperationUtils::GetHandleByPaths(string path, uint32_t &handle)
864 {
865     CHECK_AND_RETURN_RET(!path.empty(), MTP_UNDEFINED_CODE);
866     if (path.back() == '/') {
867         path.pop_back();
868     }
869     return MtpPtpProxy::GetInstance().GetIdByPath(path, handle);
870 }
871 
TryAddExternalStorage(const std::string & fsUuid,uint32_t & storageId)872 bool MtpOperationUtils::TryAddExternalStorage(const std::string &fsUuid, uint32_t &storageId)
873 {
874     CHECK_AND_RETURN_RET(!MtpManager::GetInstance().IsMtpMode(),
875         MtpPtpProxy::GetInstance().MtpTryAddExternalStorage(fsUuid, storageId));
876     return false;
877 }
878 
TryRemoveExternalStorage(const std::string & fsUuid,uint32_t & storageId)879 bool MtpOperationUtils::TryRemoveExternalStorage(const std::string &fsUuid, uint32_t &storageId)
880 {
881     CHECK_AND_RETURN_RET(!MtpManager::GetInstance().IsMtpMode(),
882         MtpPtpProxy::GetInstance().MtpTryRemoveExternalStorage(fsUuid, storageId));
883     return false;
884 }
885 
GetBatteryLevel()886 int32_t MtpOperationUtils::GetBatteryLevel()
887 {
888 #ifdef HAS_BATTERY_MANAGER_PART
889     auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance();
890     int32_t capacity = batterySrvClient.GetCapacity();
891     bool cond = (capacity > MAX_BATTERY || capacity < EMPTY_BATTERY);
892     CHECK_AND_RETURN_RET(!cond, ERROR_BATTERY);
893     return capacity;
894 #else
895     return EMPTY_BATTERY;
896 #endif
897 }
898 
GetPropertyInner(const std::string & property,const std::string & defValue)899 std::string MtpOperationUtils::GetPropertyInner(const std::string &property, const std::string &defValue)
900 {
901     return OHOS::system::GetParameter(property, defValue);
902 }
903 
SetPropertyInner(const std::string & property,const std::string & defValue)904 bool MtpOperationUtils::SetPropertyInner(const std::string &property, const std::string &defValue)
905 {
906     return OHOS::system::SetParameter(property, defValue);
907 }
908 
SetIsDevicePropSet()909 void MtpOperationUtils::SetIsDevicePropSet()
910 {
911     g_isDevicePropSet = true;
912 }
913 
914 } // namespace Media
915 } // namespace OHOS
916