blob: cfda0a6a711adafc011fc7b07294526dbf20010c [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jerry Zhange5aa05d2017-10-13 12:14:42 -070017#include <algorithm>
Jerry Zhange242f122017-10-16 14:54:08 -070018#include <android-base/logging.h>
Jerry Zhang487be612016-10-24 12:10:41 -070019#include <android-base/properties.h>
20#include <chrono>
Jerry Zhang487be612016-10-24 12:10:41 -070021#include <dirent.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040025#include <stdio.h>
26#include <stdlib.h>
27#include <sys/types.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040028#include <sys/stat.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040029#include <sys/stat.h>
caozhiyuan854cb172017-04-26 16:52:30 +080030#include <sys/time.h>
Mike Lockwoodc42aa122010-06-14 17:58:08 -070031
Mike Lockwooda881b442010-09-23 22:32:05 -040032#define LOG_TAG "MtpServer"
33
Mike Lockwood16864ba2010-05-11 17:16:59 -040034#include "MtpDebug.h"
Jerry Zhange5aa05d2017-10-13 12:14:42 -070035#include "IMtpDatabase.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070036#include "MtpDevHandle.h"
37#include "MtpFfsCompatHandle.h"
38#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070039#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040040#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040041#include "MtpServer.h"
42#include "MtpStorage.h"
43#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040044
Mike Lockwood7850ef92010-05-14 10:10:36 -040045namespace android {
46
Mike Lockwood16864ba2010-05-11 17:16:59 -040047static const MtpOperationCode kSupportedOperationCodes[] = {
48 MTP_OPERATION_GET_DEVICE_INFO,
49 MTP_OPERATION_OPEN_SESSION,
50 MTP_OPERATION_CLOSE_SESSION,
51 MTP_OPERATION_GET_STORAGE_IDS,
52 MTP_OPERATION_GET_STORAGE_INFO,
53 MTP_OPERATION_GET_NUM_OBJECTS,
54 MTP_OPERATION_GET_OBJECT_HANDLES,
55 MTP_OPERATION_GET_OBJECT_INFO,
56 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070057 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040058 MTP_OPERATION_DELETE_OBJECT,
59 MTP_OPERATION_SEND_OBJECT_INFO,
60 MTP_OPERATION_SEND_OBJECT,
61// MTP_OPERATION_INITIATE_CAPTURE,
62// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080063 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040064// MTP_OPERATION_SELF_TEST,
65// MTP_OPERATION_SET_OBJECT_PROTECTION,
66// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040067 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040068 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
69 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
70 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040071// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070072 MTP_OPERATION_MOVE_OBJECT,
73 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050074 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040075// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
76 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040077 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040078 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
79 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040080 MTP_OPERATION_GET_OBJECT_PROP_LIST,
81// MTP_OPERATION_SET_OBJECT_PROP_LIST,
82// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
83// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040084 MTP_OPERATION_GET_OBJECT_REFERENCES,
85 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040086// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070087 // Android extension for direct file IO
88 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
89 MTP_OPERATION_SEND_PARTIAL_OBJECT,
90 MTP_OPERATION_TRUNCATE_OBJECT,
91 MTP_OPERATION_BEGIN_EDIT_OBJECT,
92 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040093};
94
Mike Lockwood873871f2010-07-12 18:54:16 -040095static const MtpEventCode kSupportedEventCodes[] = {
96 MTP_EVENT_OBJECT_ADDED,
97 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050098 MTP_EVENT_STORE_ADDED,
99 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800100 MTP_EVENT_DEVICE_PROP_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400101};
102
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700103MtpServer::MtpServer(IMtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800104 const MtpString& deviceInfoManufacturer,
105 const MtpString& deviceInfoModel,
106 const MtpString& deviceInfoDeviceVersion,
107 const MtpString& deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700108 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400109 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800110 mDeviceInfoManufacturer(deviceInfoManufacturer),
111 mDeviceInfoModel(deviceInfoModel),
112 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
113 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400114 mSessionID(0),
115 mSessionOpen(false),
116 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400117 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800118 mSendObjectFileSize(0),
119 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400120{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400121}
122
123MtpServer::~MtpServer() {
124}
125
Jerry Zhang487be612016-10-24 12:10:41 -0700126IMtpHandle* MtpServer::sHandle = nullptr;
127
128int MtpServer::configure(bool usePtp) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700129 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700130 if (sHandle == nullptr) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700131 if (ffs_ok) {
132 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
133 sHandle = aio_compat ? new MtpFfsCompatHandle() : new MtpFfsHandle();
134 } else {
135 sHandle = new MtpDevHandle();
136 }
Jerry Zhang487be612016-10-24 12:10:41 -0700137 }
Jerry Zhangfbc8b062017-10-23 12:03:40 -0700138
139 int ret = sHandle->configure(usePtp);
140 if (ret) ALOGE("Failed to configure MTP driver!");
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700141 android::base::SetProperty("sys.usb.ffs.mtp.ready", "1");
Jerry Zhangfbc8b062017-10-23 12:03:40 -0700142 return ret;
Jerry Zhang487be612016-10-24 12:10:41 -0700143}
144
Mike Lockwooda8494402011-02-18 09:07:14 -0500145void MtpServer::addStorage(MtpStorage* storage) {
146 Mutex::Autolock autoLock(mMutex);
147
148 mStorages.push(storage);
149 sendStoreAdded(storage->getStorageID());
150}
151
152void MtpServer::removeStorage(MtpStorage* storage) {
153 Mutex::Autolock autoLock(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700154 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
155 if (iter != mStorages.end()) {
156 sendStoreRemoved(storage->getStorageID());
157 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500158 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400159}
160
161MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800162 if (id == 0)
163 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700164 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165 if (storage->getStorageID() == id)
166 return storage;
167 }
Jerry Zhang487be612016-10-24 12:10:41 -0700168 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169}
170
Mike Lockwooda8494402011-02-18 09:07:14 -0500171bool MtpServer::hasStorage(MtpStorageID id) {
172 if (id == 0 || id == 0xFFFFFFFF)
173 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700174 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500175}
176
Mike Lockwood16864ba2010-05-11 17:16:59 -0400177void MtpServer::run() {
Jerry Zhang487be612016-10-24 12:10:41 -0700178 if (!sHandle) {
179 ALOGE("MtpServer was never configured!");
180 return;
181 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400182
Jerry Zhang487be612016-10-24 12:10:41 -0700183 if (sHandle->start()) {
184 ALOGE("Failed to start usb driver!");
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -0800185 sHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700186 return;
187 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400188
189 while (1) {
Jerry Zhang487be612016-10-24 12:10:41 -0700190 int ret = mRequest.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400191 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700192 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400193 if (errno == ECANCELED) {
194 // return to top of loop and wait for next command
195 continue;
196 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400197 break;
198 }
199 MtpOperationCode operation = mRequest.getOperationCode();
200 MtpTransactionID transaction = mRequest.getTransactionID();
201
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400203 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400204 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400205 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
206 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
207 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400208 if (dataIn) {
Jerry Zhang487be612016-10-24 12:10:41 -0700209 int ret = mData.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400210 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000211 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400212 if (errno == ECANCELED) {
213 // return to top of loop and wait for next command
214 continue;
215 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400216 break;
217 }
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400219 } else {
220 mData.reset();
221 }
222
Mike Lockwood916076c2010-06-04 09:49:21 -0400223 if (handleRequest()) {
224 if (!dataIn && mData.hasData()) {
225 mData.setOperationCode(operation);
226 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("sending data:");
Jerry Zhang487be612016-10-24 12:10:41 -0700228 ret = mData.write(sHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400229 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000230 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400231 if (errno == ECANCELED) {
232 // return to top of loop and wait for next command
233 continue;
234 }
235 break;
236 }
237 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400238
Mike Lockwood916076c2010-06-04 09:49:21 -0400239 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang487be612016-10-24 12:10:41 -0700241 ret = mResponse.write(sHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800242 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400243 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000244 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800245 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400246 // return to top of loop and wait for next command
247 continue;
248 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400249 break;
250 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400251 } else {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400253 }
254 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400255
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700256 // commit any open edits
257 int count = mObjectEditList.size();
258 for (int i = 0; i < count; i++) {
259 ObjectEdit* edit = mObjectEditList[i];
260 commitEdit(edit);
261 delete edit;
262 }
263 mObjectEditList.clear();
264
Jerry Zhang487be612016-10-24 12:10:41 -0700265 sHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400266}
267
Mike Lockwood873871f2010-07-12 18:54:16 -0400268void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500270 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400271}
272
273void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500275 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
276}
277
278void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500280 sendEvent(MTP_EVENT_STORE_ADDED, id);
281}
282
283void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100284 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500285 sendEvent(MTP_EVENT_STORE_REMOVED, id);
286}
287
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800288void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
289 ALOGV("sendDevicePropertyChanged %d\n", property);
290 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
291}
292
Mike Lockwooda8494402011-02-18 09:07:14 -0500293void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400294 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500295 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400296 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500297 mEvent.setParameter(1, param1);
Jerry Zhang487be612016-10-24 12:10:41 -0700298 if (mEvent.write(sHandle))
299 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400300 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400301}
302
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700303void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
304 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700305 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700306 mObjectEditList.add(edit);
307}
308
309MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
310 int count = mObjectEditList.size();
311 for (int i = 0; i < count; i++) {
312 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700313 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700314 }
Jerry Zhang487be612016-10-24 12:10:41 -0700315 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700316}
317
318void MtpServer::removeEditObject(MtpObjectHandle handle) {
319 int count = mObjectEditList.size();
320 for (int i = 0; i < count; i++) {
321 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700322 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700323 delete edit;
324 mObjectEditList.removeAt(i);
325 return;
326 }
327 }
Steve Block29357bc2012-01-06 19:20:56 +0000328 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700329}
330
331void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700332 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700333}
334
335
Mike Lockwood916076c2010-06-04 09:49:21 -0400336bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500337 Mutex::Autolock autoLock(mMutex);
338
Mike Lockwood16864ba2010-05-11 17:16:59 -0400339 MtpOperationCode operation = mRequest.getOperationCode();
340 MtpResponseCode response;
341
342 mResponse.reset();
343
344 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400345 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700346 mSendObjectFormat = 0;
347 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400348 }
349
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700350 int containertype = mRequest.getContainerType();
351 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
352 ALOGE("wrong container type %d", containertype);
353 return false;
354 }
355
356 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
357
Mike Lockwood16864ba2010-05-11 17:16:59 -0400358 switch (operation) {
359 case MTP_OPERATION_GET_DEVICE_INFO:
360 response = doGetDeviceInfo();
361 break;
362 case MTP_OPERATION_OPEN_SESSION:
363 response = doOpenSession();
364 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800365 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400366 case MTP_OPERATION_CLOSE_SESSION:
367 response = doCloseSession();
368 break;
369 case MTP_OPERATION_GET_STORAGE_IDS:
370 response = doGetStorageIDs();
371 break;
372 case MTP_OPERATION_GET_STORAGE_INFO:
373 response = doGetStorageInfo();
374 break;
375 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
376 response = doGetObjectPropsSupported();
377 break;
378 case MTP_OPERATION_GET_OBJECT_HANDLES:
379 response = doGetObjectHandles();
380 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400381 case MTP_OPERATION_GET_NUM_OBJECTS:
382 response = doGetNumObjects();
383 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400384 case MTP_OPERATION_GET_OBJECT_REFERENCES:
385 response = doGetObjectReferences();
386 break;
387 case MTP_OPERATION_SET_OBJECT_REFERENCES:
388 response = doSetObjectReferences();
389 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400390 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
391 response = doGetObjectPropValue();
392 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400393 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
394 response = doSetObjectPropValue();
395 break;
396 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
397 response = doGetDevicePropValue();
398 break;
399 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
400 response = doSetDevicePropValue();
401 break;
402 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
403 response = doResetDevicePropValue();
404 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400405 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
406 response = doGetObjectPropList();
407 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400408 case MTP_OPERATION_GET_OBJECT_INFO:
409 response = doGetObjectInfo();
410 break;
411 case MTP_OPERATION_GET_OBJECT:
412 response = doGetObject();
413 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700414 case MTP_OPERATION_GET_THUMB:
415 response = doGetThumb();
416 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500417 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700418 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
419 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500420 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400421 case MTP_OPERATION_SEND_OBJECT_INFO:
422 response = doSendObjectInfo();
423 break;
424 case MTP_OPERATION_SEND_OBJECT:
425 response = doSendObject();
426 break;
427 case MTP_OPERATION_DELETE_OBJECT:
428 response = doDeleteObject();
429 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700430 case MTP_OPERATION_COPY_OBJECT:
431 response = doCopyObject();
432 break;
433 case MTP_OPERATION_MOVE_OBJECT:
434 response = doMoveObject();
435 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400436 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400437 response = doGetObjectPropDesc();
438 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400439 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
440 response = doGetDevicePropDesc();
441 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700442 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
443 response = doSendPartialObject();
444 break;
445 case MTP_OPERATION_TRUNCATE_OBJECT:
446 response = doTruncateObject();
447 break;
448 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
449 response = doBeginEditObject();
450 break;
451 case MTP_OPERATION_END_EDIT_OBJECT:
452 response = doEndEditObject();
453 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400454 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700455 ALOGE("got unsupported command %s (%x)",
456 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400457 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
458 break;
459 }
460
Mike Lockwood916076c2010-06-04 09:49:21 -0400461 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
462 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400463 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400464 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400465}
466
467MtpResponseCode MtpServer::doGetDeviceInfo() {
468 MtpStringBuffer string;
469
Mike Lockwood782aef12010-08-10 07:37:50 -0400470 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
471 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
472 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
473
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474 // fill in device info
475 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400476 if (mPtp) {
477 mData.putUInt32(0);
478 } else {
479 // MTP Vendor Extension ID
480 mData.putUInt32(6);
481 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400482 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400483 if (mPtp) {
484 // no extensions
485 string.set("");
486 } else {
487 // MTP extensions
488 string.set("microsoft.com: 1.0; android.com: 1.0;");
489 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400490 mData.putString(string); // MTP Extensions
491 mData.putUInt16(0); //Functional Mode
492 mData.putAUInt16(kSupportedOperationCodes,
493 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400494 mData.putAUInt16(kSupportedEventCodes,
495 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400496 mData.putAUInt16(deviceProperties); // Device Properties Supported
497 mData.putAUInt16(captureFormats); // Capture Formats
498 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500499
Alex Klyubin792298f2016-12-21 11:20:22 -0800500 mData.putString(mDeviceInfoManufacturer); // Manufacturer
501 mData.putString(mDeviceInfoModel); // Model
502 mData.putString(mDeviceInfoDeviceVersion); // Device Version
503 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400504
Mike Lockwood782aef12010-08-10 07:37:50 -0400505 delete playbackFormats;
506 delete captureFormats;
507 delete deviceProperties;
508
Mike Lockwood16864ba2010-05-11 17:16:59 -0400509 return MTP_RESPONSE_OK;
510}
511
512MtpResponseCode MtpServer::doOpenSession() {
513 if (mSessionOpen) {
514 mResponse.setParameter(1, mSessionID);
515 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
516 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800517 if (mRequest.getParameterCount() < 1)
518 return MTP_RESPONSE_INVALID_PARAMETER;
519
Mike Lockwood16864ba2010-05-11 17:16:59 -0400520 mSessionID = mRequest.getParameter(1);
521 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400522
Mike Lockwood16864ba2010-05-11 17:16:59 -0400523 return MTP_RESPONSE_OK;
524}
525
526MtpResponseCode MtpServer::doCloseSession() {
527 if (!mSessionOpen)
528 return MTP_RESPONSE_SESSION_NOT_OPEN;
529 mSessionID = 0;
530 mSessionOpen = false;
531 return MTP_RESPONSE_OK;
532}
533
534MtpResponseCode MtpServer::doGetStorageIDs() {
535 if (!mSessionOpen)
536 return MTP_RESPONSE_SESSION_NOT_OPEN;
537
538 int count = mStorages.size();
539 mData.putUInt32(count);
540 for (int i = 0; i < count; i++)
541 mData.putUInt32(mStorages[i]->getStorageID());
542
543 return MTP_RESPONSE_OK;
544}
545
546MtpResponseCode MtpServer::doGetStorageInfo() {
547 MtpStringBuffer string;
548
549 if (!mSessionOpen)
550 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800551 if (mRequest.getParameterCount() < 1)
552 return MTP_RESPONSE_INVALID_PARAMETER;
553
Mike Lockwood16864ba2010-05-11 17:16:59 -0400554 MtpStorageID id = mRequest.getParameter(1);
555 MtpStorage* storage = getStorage(id);
556 if (!storage)
557 return MTP_RESPONSE_INVALID_STORAGE_ID;
558
559 mData.putUInt16(storage->getType());
560 mData.putUInt16(storage->getFileSystemType());
561 mData.putUInt16(storage->getAccessCapability());
562 mData.putUInt64(storage->getMaxCapacity());
563 mData.putUInt64(storage->getFreeSpace());
564 mData.putUInt32(1024*1024*1024); // Free Space in Objects
565 string.set(storage->getDescription());
566 mData.putString(string);
567 mData.putEmptyString(); // Volume Identifier
568
569 return MTP_RESPONSE_OK;
570}
571
572MtpResponseCode MtpServer::doGetObjectPropsSupported() {
573 if (!mSessionOpen)
574 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800575 if (mRequest.getParameterCount() < 1)
576 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400577 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800578 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400579 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400580 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400581 return MTP_RESPONSE_OK;
582}
583
584MtpResponseCode MtpServer::doGetObjectHandles() {
585 if (!mSessionOpen)
586 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800587 if (mRequest.getParameterCount() < 3)
588 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400589 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400590 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400591 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400592 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500593
594 if (!hasStorage(storageID))
595 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400596
597 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700598 if (handles == NULL)
599 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400600 mData.putAUInt32(handles);
601 delete handles;
602 return MTP_RESPONSE_OK;
603}
604
Mike Lockwood343af4e2010-08-02 10:52:20 -0400605MtpResponseCode MtpServer::doGetNumObjects() {
606 if (!mSessionOpen)
607 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800608 if (mRequest.getParameterCount() < 3)
609 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400610 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
611 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
612 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400613 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500614 if (!hasStorage(storageID))
615 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400616
617 int count = mDatabase->getNumObjects(storageID, format, parent);
618 if (count >= 0) {
619 mResponse.setParameter(1, count);
620 return MTP_RESPONSE_OK;
621 } else {
622 mResponse.setParameter(1, 0);
623 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
624 }
625}
626
Mike Lockwood438344f2010-08-03 15:30:09 -0400627MtpResponseCode MtpServer::doGetObjectReferences() {
628 if (!mSessionOpen)
629 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500630 if (!hasStorage())
631 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800632 if (mRequest.getParameterCount() < 1)
633 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500634 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400635
636 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400637 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400638 if (handles) {
639 mData.putAUInt32(handles);
640 delete handles;
641 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400642 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400643 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400644 return MTP_RESPONSE_OK;
645}
646
647MtpResponseCode MtpServer::doSetObjectReferences() {
648 if (!mSessionOpen)
649 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500650 if (!hasStorage())
651 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800652 if (mRequest.getParameterCount() < 1)
653 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400654 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500655
Mike Lockwood438344f2010-08-03 15:30:09 -0400656 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800657 if (!references)
658 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400659 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
660 delete references;
661 return result;
662}
663
Mike Lockwood16864ba2010-05-11 17:16:59 -0400664MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500665 if (!hasStorage())
666 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800667 if (mRequest.getParameterCount() < 2)
668 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400669 MtpObjectHandle handle = mRequest.getParameter(1);
670 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400672 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400673
Mike Lockwood8277cec2010-08-10 15:20:35 -0400674 return mDatabase->getObjectPropertyValue(handle, property, mData);
675}
676
677MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500678 if (!hasStorage())
679 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800680 if (mRequest.getParameterCount() < 2)
681 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400682 MtpObjectHandle handle = mRequest.getParameter(1);
683 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400685 MtpDebug::getObjectPropCodeName(property));
686
687 return mDatabase->setObjectPropertyValue(handle, property, mData);
688}
689
690MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800691 if (mRequest.getParameterCount() < 1)
692 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400693 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400695 MtpDebug::getDevicePropCodeName(property));
696
697 return mDatabase->getDevicePropertyValue(property, mData);
698}
699
700MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800701 if (mRequest.getParameterCount() < 1)
702 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400703 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400705 MtpDebug::getDevicePropCodeName(property));
706
707 return mDatabase->setDevicePropertyValue(property, mData);
708}
709
710MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800711 if (mRequest.getParameterCount() < 1)
712 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400713 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100714 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400715 MtpDebug::getDevicePropCodeName(property));
716
717 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400718}
719
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400720MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500721 if (!hasStorage())
722 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800723 if (mRequest.getParameterCount() < 5)
724 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400725
726 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500727 // use uint32_t so we can support 0xFFFFFFFF
728 uint32_t format = mRequest.getParameter(2);
729 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400730 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500731 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400733 handle, MtpDebug::getFormatCodeName(format),
734 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
735
736 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
737}
738
Mike Lockwood16864ba2010-05-11 17:16:59 -0400739MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500740 if (!hasStorage())
741 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800742 if (mRequest.getParameterCount() < 1)
743 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400744 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700745 MtpObjectInfo info(handle);
746 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
747 if (result == MTP_RESPONSE_OK) {
748 char date[20];
749
750 mData.putUInt32(info.mStorageID);
751 mData.putUInt16(info.mFormat);
752 mData.putUInt16(info.mProtectionStatus);
753
754 // if object is being edited the database size may be out of date
755 uint32_t size = info.mCompressedSize;
756 ObjectEdit* edit = getEditObject(handle);
757 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700758 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700759 mData.putUInt32(size);
760
761 mData.putUInt16(info.mThumbFormat);
762 mData.putUInt32(info.mThumbCompressedSize);
763 mData.putUInt32(info.mThumbPixWidth);
764 mData.putUInt32(info.mThumbPixHeight);
765 mData.putUInt32(info.mImagePixWidth);
766 mData.putUInt32(info.mImagePixHeight);
767 mData.putUInt32(info.mImagePixDepth);
768 mData.putUInt32(info.mParent);
769 mData.putUInt16(info.mAssociationType);
770 mData.putUInt32(info.mAssociationDesc);
771 mData.putUInt32(info.mSequenceNumber);
772 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700773 formatDateTime(info.mDateCreated, date, sizeof(date));
774 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700775 formatDateTime(info.mDateModified, date, sizeof(date));
776 mData.putString(date); // date modified
777 mData.putEmptyString(); // keywords
778 }
779 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400780}
781
782MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500783 if (!hasStorage())
784 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800785 if (mRequest.getParameterCount() < 1)
786 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400787 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400788 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400789 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800790 MtpObjectFormat format;
791 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400792 if (result != MTP_RESPONSE_OK)
793 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400794
Jerry Zhang487be612016-10-24 12:10:41 -0700795 auto start = std::chrono::steady_clock::now();
796
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400797 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400798 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400799 mfr.fd = open(filePath, O_RDONLY);
800 if (mfr.fd < 0) {
801 return MTP_RESPONSE_GENERAL_ERROR;
802 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400803 mfr.offset = 0;
804 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400805 mfr.command = mRequest.getOperationCode();
806 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400807
808 // then transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700809 int ret = sHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800810 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700811 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800812 if (errno == ECANCELED) {
813 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
814 } else {
815 result = MTP_RESPONSE_GENERAL_ERROR;
816 }
817 } else {
818 result = MTP_RESPONSE_OK;
819 }
820
Jerry Zhang487be612016-10-24 12:10:41 -0700821 auto end = std::chrono::steady_clock::now();
822 std::chrono::duration<double> diff = end - start;
823 struct stat sstat;
824 fstat(mfr.fd, &sstat);
825 uint64_t finalsize = sstat.st_size;
826 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
827 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400828 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800829 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400830}
831
Mike Lockwood64000782011-04-24 18:40:17 -0700832MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800833 if (mRequest.getParameterCount() < 1)
834 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700835 MtpObjectHandle handle = mRequest.getParameter(1);
836 size_t thumbSize;
837 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
838 if (thumb) {
839 // send data
840 mData.setOperationCode(mRequest.getOperationCode());
841 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang487be612016-10-24 12:10:41 -0700842 mData.writeData(sHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700843 free(thumb);
844 return MTP_RESPONSE_OK;
845 } else {
846 return MTP_RESPONSE_GENERAL_ERROR;
847 }
848}
849
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700850MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500851 if (!hasStorage())
852 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500853 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700854 uint64_t offset;
855 uint32_t length;
856 offset = mRequest.getParameter(2);
857 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800858 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
859 if (mRequest.getParameterCount() < 4)
860 return MTP_RESPONSE_INVALID_PARAMETER;
861
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700862 // android extension with 64 bit offset
863 uint64_t offset2 = mRequest.getParameter(3);
864 offset = offset | (offset2 << 32);
865 length = mRequest.getParameter(4);
866 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800867 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
868 if (mRequest.getParameterCount() < 3)
869 return MTP_RESPONSE_INVALID_PARAMETER;
870
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700871 // standard GetPartialObject
872 length = mRequest.getParameter(3);
873 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500874 MtpString pathBuf;
875 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800876 MtpObjectFormat format;
877 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500878 if (result != MTP_RESPONSE_OK)
879 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700880 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500881 length = fileLength - offset;
882
883 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700884 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500885 mtp_file_range mfr;
886 mfr.fd = open(filePath, O_RDONLY);
887 if (mfr.fd < 0) {
888 return MTP_RESPONSE_GENERAL_ERROR;
889 }
890 mfr.offset = offset;
891 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400892 mfr.command = mRequest.getOperationCode();
893 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500894 mResponse.setParameter(1, length);
895
Mike Lockwoodef441d92011-07-14 21:00:02 -0400896 // transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700897 int ret = sHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100898 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800899 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500900 if (ret < 0) {
901 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800902 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500903 else
tao.pei07a9e542015-07-17 17:18:41 +0800904 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500905 }
tao.pei07a9e542015-07-17 17:18:41 +0800906 close(mfr.fd);
907 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500908}
909
Mike Lockwood16864ba2010-05-11 17:16:59 -0400910MtpResponseCode MtpServer::doSendObjectInfo() {
911 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800912 uint16_t temp16;
913 uint32_t temp32;
914
915 if (mRequest.getParameterCount() < 2)
916 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400917 MtpStorageID storageID = mRequest.getParameter(1);
918 MtpStorage* storage = getStorage(storageID);
919 MtpObjectHandle parent = mRequest.getParameter(2);
920 if (!storage)
921 return MTP_RESPONSE_INVALID_STORAGE_ID;
922
923 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400924 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400925 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400926 parent = 0;
927 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800928 int64_t length;
929 MtpObjectFormat format;
930 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400931 if (result != MTP_RESPONSE_OK)
932 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800933 if (format != MTP_FORMAT_ASSOCIATION)
934 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400935 }
936
937 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800938 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
939 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
940 MtpObjectFormat format = temp16;
941 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
942 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
943 mSendObjectFileSize = temp32;
944 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
945 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
946 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
947 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
948 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
949 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
950 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
951 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
952 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800953 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800954 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400955 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800956 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700957 if (name.getCharCount() == 0) {
958 ALOGE("empty name");
959 return MTP_RESPONSE_INVALID_PARAMETER;
960 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800961 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
962 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400963 // keywords follow
964
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400966 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400967 if (!parseDateTime(modified, modifiedTime))
968 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400969
970 if (path[path.size() - 1] != '/')
971 path += "/";
972 path += (const char *)name;
973
Mike Lockwood20c3be02010-12-12 12:17:43 -0800974 // check space first
975 if (mSendObjectFileSize > storage->getFreeSpace())
976 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400977 uint64_t maxFileSize = storage->getMaxFileSize();
978 // check storage max file size
979 if (maxFileSize != 0) {
980 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
981 // is >= 0xFFFFFFFF
982 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
983 return MTP_RESPONSE_OBJECT_TOO_LARGE;
984 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800985
Steve Blockb8a80522011-12-20 16:23:08 +0000986 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700987 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
988 parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400989 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400990 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400991 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400992
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700993 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -0700994 int ret = makeFolder((const char *)path);
995 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400996 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -0800997
998 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700999 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001000 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001001 mSendObjectFilePath = path;
1002 // save the handle for the SendObject call, which should follow
1003 mSendObjectHandle = handle;
1004 mSendObjectFormat = format;
1005 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001006
1007 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001008 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001009 mResponse.setParameter(3, handle);
1010
1011 return MTP_RESPONSE_OK;
1012}
1013
Jerry Zhang708b3e02017-09-26 17:53:39 -07001014MtpResponseCode MtpServer::doMoveObject() {
1015 if (!hasStorage())
1016 return MTP_RESPONSE_GENERAL_ERROR;
1017 if (mRequest.getParameterCount() < 3)
1018 return MTP_RESPONSE_INVALID_PARAMETER;
1019 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1020 MtpStorageID storageID = mRequest.getParameter(2);
1021 MtpStorage* storage = getStorage(storageID);
1022 MtpObjectHandle parent = mRequest.getParameter(3);
1023 if (!storage)
1024 return MTP_RESPONSE_INVALID_STORAGE_ID;
1025 MtpString path;
1026 MtpResponseCode result;
1027
1028 MtpString fromPath;
1029 int64_t fileLength;
1030 MtpObjectFormat format;
1031 MtpObjectInfo info(objectHandle);
1032 result = mDatabase->getObjectInfo(objectHandle, info);
1033 if (result != MTP_RESPONSE_OK)
1034 return result;
1035 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1036 if (result != MTP_RESPONSE_OK)
1037 return result;
1038
1039 // special case the root
1040 if (parent == 0) {
1041 path = storage->getPath();
1042 } else {
1043 int64_t parentLength;
1044 MtpObjectFormat parentFormat;
1045 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1046 if (result != MTP_RESPONSE_OK)
1047 return result;
1048 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1049 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1050 }
1051
1052 if (path[path.size() - 1] != '/')
1053 path += "/";
1054 path += info.mName;
1055
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001056 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1057 if (result != MTP_RESPONSE_OK)
1058 return result;
1059
Jerry Zhang708b3e02017-09-26 17:53:39 -07001060 if (info.mStorageID == storageID) {
1061 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
1062 if (rename(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001063 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001064 result = MTP_RESPONSE_GENERAL_ERROR;
1065 }
1066 } else {
1067 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001068 if (format == MTP_FORMAT_ASSOCIATION) {
1069 int ret = makeFolder((const char *)path);
1070 ret += copyRecursive(fromPath, path);
1071 if (ret) {
1072 result = MTP_RESPONSE_GENERAL_ERROR;
1073 } else {
1074 deletePath(fromPath);
1075 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001076 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001077 if (copyFile(fromPath, path)) {
1078 result = MTP_RESPONSE_GENERAL_ERROR;
1079 } else {
1080 deletePath(fromPath);
1081 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001082 }
1083 }
1084
1085 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001086 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1087 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001088
1089 return result;
1090}
1091
1092MtpResponseCode MtpServer::doCopyObject() {
1093 if (!hasStorage())
1094 return MTP_RESPONSE_GENERAL_ERROR;
1095 MtpResponseCode result = MTP_RESPONSE_OK;
1096 if (mRequest.getParameterCount() < 3)
1097 return MTP_RESPONSE_INVALID_PARAMETER;
1098 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1099 MtpStorageID storageID = mRequest.getParameter(2);
1100 MtpStorage* storage = getStorage(storageID);
1101 MtpObjectHandle parent = mRequest.getParameter(3);
1102 if (!storage)
1103 return MTP_RESPONSE_INVALID_STORAGE_ID;
1104 MtpString path;
1105
1106 MtpString fromPath;
1107 int64_t fileLength;
1108 MtpObjectFormat format;
1109 MtpObjectInfo info(objectHandle);
1110 result = mDatabase->getObjectInfo(objectHandle, info);
1111 if (result != MTP_RESPONSE_OK)
1112 return result;
1113 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1114 if (result != MTP_RESPONSE_OK)
1115 return result;
1116
1117 // special case the root
1118 if (parent == 0) {
1119 path = storage->getPath();
1120 } else {
1121 int64_t parentLength;
1122 MtpObjectFormat parentFormat;
1123 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1124 if (result != MTP_RESPONSE_OK)
1125 return result;
1126 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1127 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1128 }
1129
1130 // check space first
1131 if ((uint64_t) fileLength > storage->getFreeSpace())
1132 return MTP_RESPONSE_STORAGE_FULL;
1133
1134 if (path[path.size() - 1] != '/')
1135 path += "/";
1136 path += info.mName;
1137
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001138 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001139 if (handle == kInvalidObjectHandle) {
1140 return MTP_RESPONSE_GENERAL_ERROR;
1141 }
1142
1143 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001144 if (format == MTP_FORMAT_ASSOCIATION) {
1145 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001146 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001147 if (ret) {
1148 result = MTP_RESPONSE_GENERAL_ERROR;
1149 }
1150 } else {
1151 if (copyFile(fromPath, path)) {
1152 result = MTP_RESPONSE_GENERAL_ERROR;
1153 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001154 }
1155
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001156 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001157 mResponse.setParameter(1, handle);
1158 return result;
1159}
1160
Mike Lockwood16864ba2010-05-11 17:16:59 -04001161MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001162 if (!hasStorage())
1163 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001164 MtpResponseCode result = MTP_RESPONSE_OK;
1165 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001166 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001167 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001168 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001169
Jerry Zhang487be612016-10-24 12:10:41 -07001170 auto start = std::chrono::steady_clock::now();
1171
Mike Lockwood16864ba2010-05-11 17:16:59 -04001172 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001173 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001174 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1175 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001176 }
1177
Mike Lockwoodef441d92011-07-14 21:00:02 -04001178 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001179 ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001180 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1181 result = MTP_RESPONSE_GENERAL_ERROR;
1182 goto done;
1183 }
1184 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001185
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001186 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1187 if (initialData != 0)
1188 ALOGE("Expected folder size to be 0!");
1189 mSendObjectHandle = kInvalidObjectHandle;
1190 mSendObjectFormat = 0;
1191 mSendObjectModifiedTime = 0;
1192 return result;
1193 }
1194
Mike Lockwood16864ba2010-05-11 17:16:59 -04001195 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001196 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001197 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001198 result = MTP_RESPONSE_GENERAL_ERROR;
1199 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001200 }
Jerry Zhange242f122017-10-16 14:54:08 -07001201 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001202 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001203 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001204 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001205 umask(mask);
1206
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001207 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001208 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001209 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001210
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001211 if (ret < 0) {
1212 ALOGE("failed to write initial data");
1213 result = MTP_RESPONSE_GENERAL_ERROR;
1214 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001215 mfr.offset = initialData;
1216 if (mSendObjectFileSize == 0xFFFFFFFF) {
1217 // tell driver to read until it receives a short packet
1218 mfr.length = 0xFFFFFFFF;
1219 } else {
1220 mfr.length = mSendObjectFileSize - initialData;
1221 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001222
Jerry Zhang54107562017-05-15 11:54:19 -07001223 mfr.command = 0;
1224 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001225
Jerry Zhang54107562017-05-15 11:54:19 -07001226 // transfer the file
1227 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1228 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1229 if ((ret < 0) && (errno == ECANCELED)) {
1230 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001231 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001232 }
caozhiyuan854cb172017-04-26 16:52:30 +08001233
1234 if (mSendObjectModifiedTime) {
1235 struct timespec newTime[2];
1236 newTime[0].tv_nsec = UTIME_NOW;
1237 newTime[1].tv_sec = mSendObjectModifiedTime;
1238 newTime[1].tv_nsec = 0;
1239 if (futimens(mfr.fd, newTime) < 0) {
1240 ALOGW("changing modified time failed, %s", strerror(errno));
1241 }
1242 }
1243
Jerry Zhang487be612016-10-24 12:10:41 -07001244 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001245 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001246
Mike Lockwood916076c2010-06-04 09:49:21 -04001247 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001248 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001249 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001250 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001251 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001252 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001253 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001254 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001255
1256done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001257 // reset so we don't attempt to send the data back
1258 mData.reset();
1259
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001260 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001261 mSendObjectHandle = kInvalidObjectHandle;
1262 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001263 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001264
1265 auto end = std::chrono::steady_clock::now();
1266 std::chrono::duration<double> diff = end - start;
1267 uint64_t finalsize = sstat.st_size;
1268 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1269 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001270 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001271}
1272
1273MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001274 if (!hasStorage())
1275 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001276 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001277 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001278 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001279 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001280 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1281 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001282
1283 MtpString filePath;
1284 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001285 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001286 if (result != MTP_RESPONSE_OK)
1287 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001288
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001289 // Don't delete the actual files unless the database deletion is allowed
1290 result = mDatabase->beginDeleteObject(handle);
1291 if (result != MTP_RESPONSE_OK)
1292 return result;
1293
1294 bool success = deletePath((const char *)filePath);
1295
1296 mDatabase->endDeleteObject(handle, success);
1297 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001298}
1299
1300MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001301 if (mRequest.getParameterCount() < 2)
1302 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001303 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001304 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001305 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001306 MtpDebug::getFormatCodeName(format));
1307 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001308 if (!property)
1309 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001310 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001311 delete property;
1312 return MTP_RESPONSE_OK;
1313}
1314
1315MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001316 if (mRequest.getParameterCount() < 1)
1317 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001318 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001319 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001320 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1321 if (!property)
1322 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1323 property->write(mData);
1324 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001325 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001326}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001327
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001328MtpResponseCode MtpServer::doSendPartialObject() {
1329 if (!hasStorage())
1330 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001331 if (mRequest.getParameterCount() < 4)
1332 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001333 MtpObjectHandle handle = mRequest.getParameter(1);
1334 uint64_t offset = mRequest.getParameter(2);
1335 uint64_t offset2 = mRequest.getParameter(3);
1336 offset = offset | (offset2 << 32);
1337 uint32_t length = mRequest.getParameter(4);
1338
1339 ObjectEdit* edit = getEditObject(handle);
1340 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001341 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001342 return MTP_RESPONSE_GENERAL_ERROR;
1343 }
1344
1345 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001346 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001347 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1348 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001349 return MTP_RESPONSE_GENERAL_ERROR;
1350 }
1351
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001352 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001353 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001354
Mike Lockwoodef441d92011-07-14 21:00:02 -04001355 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001356 int ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001357 if (ret < MTP_CONTAINER_HEADER_SIZE)
1358 return MTP_RESPONSE_GENERAL_ERROR;
1359 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1360
1361 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001362 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001363 offset += initialData;
1364 length -= initialData;
1365 }
1366
tao.pei07a9e542015-07-17 17:18:41 +08001367 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001368 if (ret < 0) {
1369 ALOGE("failed to write initial data");
1370 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001371 mtp_file_range mfr;
1372 mfr.fd = edit->mFD;
1373 mfr.offset = offset;
1374 mfr.length = length;
1375 mfr.command = 0;
1376 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001377
Jerry Zhang54107562017-05-15 11:54:19 -07001378 // transfer the file
1379 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1380 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1381 if ((ret < 0) && (errno == ECANCELED)) {
1382 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001383 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001384 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001385 if (ret < 0) {
1386 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001387 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001388 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1389 else
1390 return MTP_RESPONSE_GENERAL_ERROR;
1391 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001392
1393 // reset so we don't attempt to send this back
1394 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001395 mResponse.setParameter(1, length);
1396 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001397 if (end > edit->mSize) {
1398 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001399 }
1400 return MTP_RESPONSE_OK;
1401}
1402
1403MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001404 if (mRequest.getParameterCount() < 3)
1405 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001406 MtpObjectHandle handle = mRequest.getParameter(1);
1407 ObjectEdit* edit = getEditObject(handle);
1408 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001409 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001410 return MTP_RESPONSE_GENERAL_ERROR;
1411 }
1412
1413 uint64_t offset = mRequest.getParameter(2);
1414 uint64_t offset2 = mRequest.getParameter(3);
1415 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001416 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001417 return MTP_RESPONSE_GENERAL_ERROR;
1418 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001419 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001420 return MTP_RESPONSE_OK;
1421 }
1422}
1423
1424MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001425 if (mRequest.getParameterCount() < 1)
1426 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001427 MtpObjectHandle handle = mRequest.getParameter(1);
1428 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001429 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001430 return MTP_RESPONSE_GENERAL_ERROR;
1431 }
1432
1433 MtpString path;
1434 int64_t fileLength;
1435 MtpObjectFormat format;
1436 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1437 if (result != MTP_RESPONSE_OK)
1438 return result;
1439
1440 int fd = open((const char *)path, O_RDWR | O_EXCL);
1441 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001442 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001443 return MTP_RESPONSE_GENERAL_ERROR;
1444 }
1445
1446 addEditObject(handle, path, fileLength, format, fd);
1447 return MTP_RESPONSE_OK;
1448}
1449
1450MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001451 if (mRequest.getParameterCount() < 1)
1452 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001453 MtpObjectHandle handle = mRequest.getParameter(1);
1454 ObjectEdit* edit = getEditObject(handle);
1455 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001456 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001457 return MTP_RESPONSE_GENERAL_ERROR;
1458 }
1459
1460 commitEdit(edit);
1461 removeEditObject(handle);
1462 return MTP_RESPONSE_OK;
1463}
1464
Mike Lockwood7850ef92010-05-14 10:10:36 -04001465} // namespace android