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