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