blob: e4ac8b0015e5b4e08153cb5c1e67ecd18263eb5a [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 Zhang63dac452017-12-06 15:19:36 -080036#include "MtpDescriptors.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070037#include "MtpDevHandle.h"
38#include "MtpFfsCompatHandle.h"
39#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070040#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040041#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040042#include "MtpServer.h"
43#include "MtpStorage.h"
44#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040045
Mike Lockwood7850ef92010-05-14 10:10:36 -040046namespace android {
47
Mike Lockwood16864ba2010-05-11 17:16:59 -040048static const MtpOperationCode kSupportedOperationCodes[] = {
49 MTP_OPERATION_GET_DEVICE_INFO,
50 MTP_OPERATION_OPEN_SESSION,
51 MTP_OPERATION_CLOSE_SESSION,
52 MTP_OPERATION_GET_STORAGE_IDS,
53 MTP_OPERATION_GET_STORAGE_INFO,
54 MTP_OPERATION_GET_NUM_OBJECTS,
55 MTP_OPERATION_GET_OBJECT_HANDLES,
56 MTP_OPERATION_GET_OBJECT_INFO,
57 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070058 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040059 MTP_OPERATION_DELETE_OBJECT,
60 MTP_OPERATION_SEND_OBJECT_INFO,
61 MTP_OPERATION_SEND_OBJECT,
62// MTP_OPERATION_INITIATE_CAPTURE,
63// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080064 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040065// MTP_OPERATION_SELF_TEST,
66// MTP_OPERATION_SET_OBJECT_PROTECTION,
67// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040068 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040069 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
70 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
71 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040072// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070073 MTP_OPERATION_MOVE_OBJECT,
74 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050075 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040076// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
77 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040078 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040079 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
80 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040081 MTP_OPERATION_GET_OBJECT_PROP_LIST,
82// MTP_OPERATION_SET_OBJECT_PROP_LIST,
83// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
84// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040085 MTP_OPERATION_GET_OBJECT_REFERENCES,
86 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040087// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070088 // Android extension for direct file IO
89 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
90 MTP_OPERATION_SEND_PARTIAL_OBJECT,
91 MTP_OPERATION_TRUNCATE_OBJECT,
92 MTP_OPERATION_BEGIN_EDIT_OBJECT,
93 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040094};
95
Mike Lockwood873871f2010-07-12 18:54:16 -040096static const MtpEventCode kSupportedEventCodes[] = {
97 MTP_EVENT_OBJECT_ADDED,
98 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050099 MTP_EVENT_STORE_ADDED,
100 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800101 MTP_EVENT_DEVICE_PROP_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400102};
103
Jerry Zhang63dac452017-12-06 15:19:36 -0800104MtpServer::MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800105 const MtpString& deviceInfoManufacturer,
106 const MtpString& deviceInfoModel,
107 const MtpString& deviceInfoDeviceVersion,
108 const MtpString& deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700109 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400110 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800111 mDeviceInfoManufacturer(deviceInfoManufacturer),
112 mDeviceInfoModel(deviceInfoModel),
113 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
114 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400115 mSessionID(0),
116 mSessionOpen(false),
117 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400118 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800119 mSendObjectFileSize(0),
120 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400121{
Jerry Zhang63dac452017-12-06 15:19:36 -0800122 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
123 if (ffs_ok) {
124 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
125 mHandle = aio_compat ? new MtpFfsCompatHandle(controlFd) : new MtpFfsHandle(controlFd);
126 } else {
127 mHandle = new MtpDevHandle();
128 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400129}
130
131MtpServer::~MtpServer() {
132}
133
Mike Lockwooda8494402011-02-18 09:07:14 -0500134void MtpServer::addStorage(MtpStorage* storage) {
135 Mutex::Autolock autoLock(mMutex);
136
137 mStorages.push(storage);
138 sendStoreAdded(storage->getStorageID());
139}
140
141void MtpServer::removeStorage(MtpStorage* storage) {
142 Mutex::Autolock autoLock(mMutex);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700143 auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
144 if (iter != mStorages.end()) {
145 sendStoreRemoved(storage->getStorageID());
146 mStorages.erase(iter);
Mike Lockwooda8494402011-02-18 09:07:14 -0500147 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400148}
149
150MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800151 if (id == 0)
152 return mStorages[0];
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700153 for (MtpStorage *storage : mStorages) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400154 if (storage->getStorageID() == id)
155 return storage;
156 }
Jerry Zhang487be612016-10-24 12:10:41 -0700157 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400158}
159
Mike Lockwooda8494402011-02-18 09:07:14 -0500160bool MtpServer::hasStorage(MtpStorageID id) {
161 if (id == 0 || id == 0xFFFFFFFF)
162 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700163 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500164}
165
Mike Lockwood16864ba2010-05-11 17:16:59 -0400166void MtpServer::run() {
Jerry Zhang63dac452017-12-06 15:19:36 -0800167 if (mHandle->start(mPtp)) {
Jerry Zhang487be612016-10-24 12:10:41 -0700168 ALOGE("Failed to start usb driver!");
Jerry Zhang63dac452017-12-06 15:19:36 -0800169 mHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700170 return;
171 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172
173 while (1) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800174 int ret = mRequest.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400175 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700176 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400177 if (errno == ECANCELED) {
178 // return to top of loop and wait for next command
179 continue;
180 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400181 break;
182 }
183 MtpOperationCode operation = mRequest.getOperationCode();
184 MtpTransactionID transaction = mRequest.getTransactionID();
185
Steve Block3856b092011-10-20 11:56:00 +0100186 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400187 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400188 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400189 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
190 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
191 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400192 if (dataIn) {
Jerry Zhang63dac452017-12-06 15:19:36 -0800193 int ret = mData.read(mHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400194 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000195 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400196 if (errno == ECANCELED) {
197 // return to top of loop and wait for next command
198 continue;
199 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400200 break;
201 }
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400203 } else {
204 mData.reset();
205 }
206
Mike Lockwood916076c2010-06-04 09:49:21 -0400207 if (handleRequest()) {
208 if (!dataIn && mData.hasData()) {
209 mData.setOperationCode(operation);
210 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100211 ALOGV("sending data:");
Jerry Zhang63dac452017-12-06 15:19:36 -0800212 ret = mData.write(mHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400213 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000214 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400215 if (errno == ECANCELED) {
216 // return to top of loop and wait for next command
217 continue;
218 }
219 break;
220 }
221 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400222
Mike Lockwood916076c2010-06-04 09:49:21 -0400223 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang63dac452017-12-06 15:19:36 -0800225 ret = mResponse.write(mHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800226 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400227 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000228 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800229 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400230 // return to top of loop and wait for next command
231 continue;
232 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400233 break;
234 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400235 } else {
Steve Block3856b092011-10-20 11:56:00 +0100236 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400237 }
238 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400239
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700240 // commit any open edits
241 int count = mObjectEditList.size();
242 for (int i = 0; i < count; i++) {
243 ObjectEdit* edit = mObjectEditList[i];
244 commitEdit(edit);
245 delete edit;
246 }
247 mObjectEditList.clear();
248
Jerry Zhang63dac452017-12-06 15:19:36 -0800249 mHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400250}
251
Mike Lockwood873871f2010-07-12 18:54:16 -0400252void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100253 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500254 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400255}
256
257void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500259 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
260}
261
262void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500264 sendEvent(MTP_EVENT_STORE_ADDED, id);
265}
266
267void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500269 sendEvent(MTP_EVENT_STORE_REMOVED, id);
270}
271
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800272void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
273 ALOGV("sendDevicePropertyChanged %d\n", property);
274 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
275}
276
Mike Lockwooda8494402011-02-18 09:07:14 -0500277void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400278 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500279 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400280 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500281 mEvent.setParameter(1, param1);
Jerry Zhang63dac452017-12-06 15:19:36 -0800282 if (mEvent.write(mHandle))
Jerry Zhang487be612016-10-24 12:10:41 -0700283 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400284 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400285}
286
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700287void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
288 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700289 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700290 mObjectEditList.add(edit);
291}
292
293MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
294 int count = mObjectEditList.size();
295 for (int i = 0; i < count; i++) {
296 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700297 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700298 }
Jerry Zhang487be612016-10-24 12:10:41 -0700299 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700300}
301
302void MtpServer::removeEditObject(MtpObjectHandle handle) {
303 int count = mObjectEditList.size();
304 for (int i = 0; i < count; i++) {
305 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700306 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700307 delete edit;
308 mObjectEditList.removeAt(i);
309 return;
310 }
311 }
Steve Block29357bc2012-01-06 19:20:56 +0000312 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700313}
314
315void MtpServer::commitEdit(ObjectEdit* edit) {
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700316 mDatabase->rescanFile((const char *)edit->mPath, edit->mHandle, edit->mFormat);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700317}
318
319
Mike Lockwood916076c2010-06-04 09:49:21 -0400320bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500321 Mutex::Autolock autoLock(mMutex);
322
Mike Lockwood16864ba2010-05-11 17:16:59 -0400323 MtpOperationCode operation = mRequest.getOperationCode();
324 MtpResponseCode response;
325
326 mResponse.reset();
327
328 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400329 mSendObjectHandle = kInvalidObjectHandle;
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700330 mSendObjectFormat = 0;
331 mSendObjectModifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400332 }
333
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700334 int containertype = mRequest.getContainerType();
335 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
336 ALOGE("wrong container type %d", containertype);
337 return false;
338 }
339
340 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
341
Mike Lockwood16864ba2010-05-11 17:16:59 -0400342 switch (operation) {
343 case MTP_OPERATION_GET_DEVICE_INFO:
344 response = doGetDeviceInfo();
345 break;
346 case MTP_OPERATION_OPEN_SESSION:
347 response = doOpenSession();
348 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800349 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400350 case MTP_OPERATION_CLOSE_SESSION:
351 response = doCloseSession();
352 break;
353 case MTP_OPERATION_GET_STORAGE_IDS:
354 response = doGetStorageIDs();
355 break;
356 case MTP_OPERATION_GET_STORAGE_INFO:
357 response = doGetStorageInfo();
358 break;
359 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
360 response = doGetObjectPropsSupported();
361 break;
362 case MTP_OPERATION_GET_OBJECT_HANDLES:
363 response = doGetObjectHandles();
364 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400365 case MTP_OPERATION_GET_NUM_OBJECTS:
366 response = doGetNumObjects();
367 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400368 case MTP_OPERATION_GET_OBJECT_REFERENCES:
369 response = doGetObjectReferences();
370 break;
371 case MTP_OPERATION_SET_OBJECT_REFERENCES:
372 response = doSetObjectReferences();
373 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400374 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
375 response = doGetObjectPropValue();
376 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400377 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
378 response = doSetObjectPropValue();
379 break;
380 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
381 response = doGetDevicePropValue();
382 break;
383 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
384 response = doSetDevicePropValue();
385 break;
386 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
387 response = doResetDevicePropValue();
388 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400389 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
390 response = doGetObjectPropList();
391 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400392 case MTP_OPERATION_GET_OBJECT_INFO:
393 response = doGetObjectInfo();
394 break;
395 case MTP_OPERATION_GET_OBJECT:
396 response = doGetObject();
397 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700398 case MTP_OPERATION_GET_THUMB:
399 response = doGetThumb();
400 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500401 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700402 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
403 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500404 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400405 case MTP_OPERATION_SEND_OBJECT_INFO:
406 response = doSendObjectInfo();
407 break;
408 case MTP_OPERATION_SEND_OBJECT:
409 response = doSendObject();
410 break;
411 case MTP_OPERATION_DELETE_OBJECT:
412 response = doDeleteObject();
413 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700414 case MTP_OPERATION_COPY_OBJECT:
415 response = doCopyObject();
416 break;
417 case MTP_OPERATION_MOVE_OBJECT:
418 response = doMoveObject();
419 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400420 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400421 response = doGetObjectPropDesc();
422 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400423 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
424 response = doGetDevicePropDesc();
425 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700426 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
427 response = doSendPartialObject();
428 break;
429 case MTP_OPERATION_TRUNCATE_OBJECT:
430 response = doTruncateObject();
431 break;
432 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
433 response = doBeginEditObject();
434 break;
435 case MTP_OPERATION_END_EDIT_OBJECT:
436 response = doEndEditObject();
437 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400438 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700439 ALOGE("got unsupported command %s (%x)",
440 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400441 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
442 break;
443 }
444
Mike Lockwood916076c2010-06-04 09:49:21 -0400445 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
446 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400447 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400448 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400449}
450
451MtpResponseCode MtpServer::doGetDeviceInfo() {
452 MtpStringBuffer string;
453
Mike Lockwood782aef12010-08-10 07:37:50 -0400454 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
455 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
456 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
457
Mike Lockwood16864ba2010-05-11 17:16:59 -0400458 // fill in device info
459 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400460 if (mPtp) {
461 mData.putUInt32(0);
462 } else {
463 // MTP Vendor Extension ID
464 mData.putUInt32(6);
465 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400466 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400467 if (mPtp) {
468 // no extensions
469 string.set("");
470 } else {
471 // MTP extensions
472 string.set("microsoft.com: 1.0; android.com: 1.0;");
473 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474 mData.putString(string); // MTP Extensions
475 mData.putUInt16(0); //Functional Mode
476 mData.putAUInt16(kSupportedOperationCodes,
477 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400478 mData.putAUInt16(kSupportedEventCodes,
479 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400480 mData.putAUInt16(deviceProperties); // Device Properties Supported
481 mData.putAUInt16(captureFormats); // Capture Formats
482 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500483
Alex Klyubin792298f2016-12-21 11:20:22 -0800484 mData.putString(mDeviceInfoManufacturer); // Manufacturer
485 mData.putString(mDeviceInfoModel); // Model
486 mData.putString(mDeviceInfoDeviceVersion); // Device Version
487 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400488
Mike Lockwood782aef12010-08-10 07:37:50 -0400489 delete playbackFormats;
490 delete captureFormats;
491 delete deviceProperties;
492
Mike Lockwood16864ba2010-05-11 17:16:59 -0400493 return MTP_RESPONSE_OK;
494}
495
496MtpResponseCode MtpServer::doOpenSession() {
497 if (mSessionOpen) {
498 mResponse.setParameter(1, mSessionID);
499 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
500 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800501 if (mRequest.getParameterCount() < 1)
502 return MTP_RESPONSE_INVALID_PARAMETER;
503
Mike Lockwood16864ba2010-05-11 17:16:59 -0400504 mSessionID = mRequest.getParameter(1);
505 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400506
Mike Lockwood16864ba2010-05-11 17:16:59 -0400507 return MTP_RESPONSE_OK;
508}
509
510MtpResponseCode MtpServer::doCloseSession() {
511 if (!mSessionOpen)
512 return MTP_RESPONSE_SESSION_NOT_OPEN;
513 mSessionID = 0;
514 mSessionOpen = false;
515 return MTP_RESPONSE_OK;
516}
517
518MtpResponseCode MtpServer::doGetStorageIDs() {
519 if (!mSessionOpen)
520 return MTP_RESPONSE_SESSION_NOT_OPEN;
521
522 int count = mStorages.size();
523 mData.putUInt32(count);
524 for (int i = 0; i < count; i++)
525 mData.putUInt32(mStorages[i]->getStorageID());
526
527 return MTP_RESPONSE_OK;
528}
529
530MtpResponseCode MtpServer::doGetStorageInfo() {
531 MtpStringBuffer string;
532
533 if (!mSessionOpen)
534 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800535 if (mRequest.getParameterCount() < 1)
536 return MTP_RESPONSE_INVALID_PARAMETER;
537
Mike Lockwood16864ba2010-05-11 17:16:59 -0400538 MtpStorageID id = mRequest.getParameter(1);
539 MtpStorage* storage = getStorage(id);
540 if (!storage)
541 return MTP_RESPONSE_INVALID_STORAGE_ID;
542
543 mData.putUInt16(storage->getType());
544 mData.putUInt16(storage->getFileSystemType());
545 mData.putUInt16(storage->getAccessCapability());
546 mData.putUInt64(storage->getMaxCapacity());
547 mData.putUInt64(storage->getFreeSpace());
548 mData.putUInt32(1024*1024*1024); // Free Space in Objects
549 string.set(storage->getDescription());
550 mData.putString(string);
551 mData.putEmptyString(); // Volume Identifier
552
553 return MTP_RESPONSE_OK;
554}
555
556MtpResponseCode MtpServer::doGetObjectPropsSupported() {
557 if (!mSessionOpen)
558 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800559 if (mRequest.getParameterCount() < 1)
560 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400561 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800562 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400563 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400564 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400565 return MTP_RESPONSE_OK;
566}
567
568MtpResponseCode MtpServer::doGetObjectHandles() {
569 if (!mSessionOpen)
570 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800571 if (mRequest.getParameterCount() < 3)
572 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400573 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400574 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400575 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400576 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500577
578 if (!hasStorage(storageID))
579 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400580
581 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700582 if (handles == NULL)
583 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400584 mData.putAUInt32(handles);
585 delete handles;
586 return MTP_RESPONSE_OK;
587}
588
Mike Lockwood343af4e2010-08-02 10:52:20 -0400589MtpResponseCode MtpServer::doGetNumObjects() {
590 if (!mSessionOpen)
591 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800592 if (mRequest.getParameterCount() < 3)
593 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400594 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
595 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
596 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400597 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500598 if (!hasStorage(storageID))
599 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400600
601 int count = mDatabase->getNumObjects(storageID, format, parent);
602 if (count >= 0) {
603 mResponse.setParameter(1, count);
604 return MTP_RESPONSE_OK;
605 } else {
606 mResponse.setParameter(1, 0);
607 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
608 }
609}
610
Mike Lockwood438344f2010-08-03 15:30:09 -0400611MtpResponseCode MtpServer::doGetObjectReferences() {
612 if (!mSessionOpen)
613 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500614 if (!hasStorage())
615 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800616 if (mRequest.getParameterCount() < 1)
617 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500618 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400619
620 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400621 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400622 if (handles) {
623 mData.putAUInt32(handles);
624 delete handles;
625 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400626 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400627 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400628 return MTP_RESPONSE_OK;
629}
630
631MtpResponseCode MtpServer::doSetObjectReferences() {
632 if (!mSessionOpen)
633 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500634 if (!hasStorage())
635 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800636 if (mRequest.getParameterCount() < 1)
637 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400638 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500639
Mike Lockwood438344f2010-08-03 15:30:09 -0400640 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800641 if (!references)
642 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400643 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
644 delete references;
645 return result;
646}
647
Mike Lockwood16864ba2010-05-11 17:16:59 -0400648MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500649 if (!hasStorage())
650 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800651 if (mRequest.getParameterCount() < 2)
652 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400653 MtpObjectHandle handle = mRequest.getParameter(1);
654 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100655 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400656 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400657
Mike Lockwood8277cec2010-08-10 15:20:35 -0400658 return mDatabase->getObjectPropertyValue(handle, property, mData);
659}
660
661MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500662 if (!hasStorage())
663 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800664 if (mRequest.getParameterCount() < 2)
665 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400666 MtpObjectHandle handle = mRequest.getParameter(1);
667 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400669 MtpDebug::getObjectPropCodeName(property));
670
671 return mDatabase->setObjectPropertyValue(handle, property, mData);
672}
673
674MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800675 if (mRequest.getParameterCount() < 1)
676 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400677 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400679 MtpDebug::getDevicePropCodeName(property));
680
681 return mDatabase->getDevicePropertyValue(property, mData);
682}
683
684MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800685 if (mRequest.getParameterCount() < 1)
686 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400687 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400689 MtpDebug::getDevicePropCodeName(property));
690
691 return mDatabase->setDevicePropertyValue(property, mData);
692}
693
694MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800695 if (mRequest.getParameterCount() < 1)
696 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400697 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400699 MtpDebug::getDevicePropCodeName(property));
700
701 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400702}
703
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400704MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500705 if (!hasStorage())
706 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800707 if (mRequest.getParameterCount() < 5)
708 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400709
710 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500711 // use uint32_t so we can support 0xFFFFFFFF
712 uint32_t format = mRequest.getParameter(2);
713 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400714 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500715 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100716 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400717 handle, MtpDebug::getFormatCodeName(format),
718 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
719
720 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
721}
722
Mike Lockwood16864ba2010-05-11 17:16:59 -0400723MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500724 if (!hasStorage())
725 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800726 if (mRequest.getParameterCount() < 1)
727 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400728 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700729 MtpObjectInfo info(handle);
730 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
731 if (result == MTP_RESPONSE_OK) {
732 char date[20];
733
734 mData.putUInt32(info.mStorageID);
735 mData.putUInt16(info.mFormat);
736 mData.putUInt16(info.mProtectionStatus);
737
738 // if object is being edited the database size may be out of date
739 uint32_t size = info.mCompressedSize;
740 ObjectEdit* edit = getEditObject(handle);
741 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700742 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700743 mData.putUInt32(size);
744
745 mData.putUInt16(info.mThumbFormat);
746 mData.putUInt32(info.mThumbCompressedSize);
747 mData.putUInt32(info.mThumbPixWidth);
748 mData.putUInt32(info.mThumbPixHeight);
749 mData.putUInt32(info.mImagePixWidth);
750 mData.putUInt32(info.mImagePixHeight);
751 mData.putUInt32(info.mImagePixDepth);
752 mData.putUInt32(info.mParent);
753 mData.putUInt16(info.mAssociationType);
754 mData.putUInt32(info.mAssociationDesc);
755 mData.putUInt32(info.mSequenceNumber);
756 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700757 formatDateTime(info.mDateCreated, date, sizeof(date));
758 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700759 formatDateTime(info.mDateModified, date, sizeof(date));
760 mData.putString(date); // date modified
761 mData.putEmptyString(); // keywords
762 }
763 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400764}
765
766MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500767 if (!hasStorage())
768 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800769 if (mRequest.getParameterCount() < 1)
770 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400771 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400772 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400773 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800774 MtpObjectFormat format;
775 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400776 if (result != MTP_RESPONSE_OK)
777 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400778
Jerry Zhang487be612016-10-24 12:10:41 -0700779 auto start = std::chrono::steady_clock::now();
780
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400781 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400782 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400783 mfr.fd = open(filePath, O_RDONLY);
784 if (mfr.fd < 0) {
785 return MTP_RESPONSE_GENERAL_ERROR;
786 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400787 mfr.offset = 0;
788 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400789 mfr.command = mRequest.getOperationCode();
790 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400791
792 // then transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800793 int ret = mHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800794 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700795 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800796 if (errno == ECANCELED) {
797 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
798 } else {
799 result = MTP_RESPONSE_GENERAL_ERROR;
800 }
801 } else {
802 result = MTP_RESPONSE_OK;
803 }
804
Jerry Zhang487be612016-10-24 12:10:41 -0700805 auto end = std::chrono::steady_clock::now();
806 std::chrono::duration<double> diff = end - start;
807 struct stat sstat;
808 fstat(mfr.fd, &sstat);
809 uint64_t finalsize = sstat.st_size;
810 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
811 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400812 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800813 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400814}
815
Mike Lockwood64000782011-04-24 18:40:17 -0700816MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800817 if (mRequest.getParameterCount() < 1)
818 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700819 MtpObjectHandle handle = mRequest.getParameter(1);
820 size_t thumbSize;
821 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
822 if (thumb) {
823 // send data
824 mData.setOperationCode(mRequest.getOperationCode());
825 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang63dac452017-12-06 15:19:36 -0800826 mData.writeData(mHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700827 free(thumb);
828 return MTP_RESPONSE_OK;
829 } else {
830 return MTP_RESPONSE_GENERAL_ERROR;
831 }
832}
833
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700834MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500835 if (!hasStorage())
836 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500837 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700838 uint64_t offset;
839 uint32_t length;
840 offset = mRequest.getParameter(2);
841 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800842 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
843 if (mRequest.getParameterCount() < 4)
844 return MTP_RESPONSE_INVALID_PARAMETER;
845
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700846 // android extension with 64 bit offset
847 uint64_t offset2 = mRequest.getParameter(3);
848 offset = offset | (offset2 << 32);
849 length = mRequest.getParameter(4);
850 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800851 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
852 if (mRequest.getParameterCount() < 3)
853 return MTP_RESPONSE_INVALID_PARAMETER;
854
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700855 // standard GetPartialObject
856 length = mRequest.getParameter(3);
857 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500858 MtpString pathBuf;
859 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800860 MtpObjectFormat format;
861 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500862 if (result != MTP_RESPONSE_OK)
863 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700864 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500865 length = fileLength - offset;
866
867 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700868 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500869 mtp_file_range mfr;
870 mfr.fd = open(filePath, O_RDONLY);
871 if (mfr.fd < 0) {
872 return MTP_RESPONSE_GENERAL_ERROR;
873 }
874 mfr.offset = offset;
875 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400876 mfr.command = mRequest.getOperationCode();
877 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500878 mResponse.setParameter(1, length);
879
Mike Lockwoodef441d92011-07-14 21:00:02 -0400880 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -0800881 int ret = mHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800883 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500884 if (ret < 0) {
885 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800886 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500887 else
tao.pei07a9e542015-07-17 17:18:41 +0800888 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500889 }
tao.pei07a9e542015-07-17 17:18:41 +0800890 close(mfr.fd);
891 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500892}
893
Mike Lockwood16864ba2010-05-11 17:16:59 -0400894MtpResponseCode MtpServer::doSendObjectInfo() {
895 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800896 uint16_t temp16;
897 uint32_t temp32;
898
899 if (mRequest.getParameterCount() < 2)
900 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400901 MtpStorageID storageID = mRequest.getParameter(1);
902 MtpStorage* storage = getStorage(storageID);
903 MtpObjectHandle parent = mRequest.getParameter(2);
904 if (!storage)
905 return MTP_RESPONSE_INVALID_STORAGE_ID;
906
907 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400908 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400909 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400910 parent = 0;
911 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800912 int64_t length;
913 MtpObjectFormat format;
914 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400915 if (result != MTP_RESPONSE_OK)
916 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800917 if (format != MTP_FORMAT_ASSOCIATION)
918 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400919 }
920
921 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800922 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
923 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
924 MtpObjectFormat format = temp16;
925 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
926 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
927 mSendObjectFileSize = temp32;
928 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
929 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
930 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
931 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
932 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
933 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
934 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
935 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
936 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800937 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800938 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400939 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800940 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700941 if (name.getCharCount() == 0) {
942 ALOGE("empty name");
943 return MTP_RESPONSE_INVALID_PARAMETER;
944 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800945 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
946 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400947 // keywords follow
948
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400950 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400951 if (!parseDateTime(modified, modifiedTime))
952 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400953
954 if (path[path.size() - 1] != '/')
955 path += "/";
956 path += (const char *)name;
957
Mike Lockwood20c3be02010-12-12 12:17:43 -0800958 // check space first
959 if (mSendObjectFileSize > storage->getFreeSpace())
960 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400961 uint64_t maxFileSize = storage->getMaxFileSize();
962 // check storage max file size
963 if (maxFileSize != 0) {
964 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
965 // is >= 0xFFFFFFFF
966 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
967 return MTP_RESPONSE_OBJECT_TOO_LARGE;
968 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800969
Steve Blockb8a80522011-12-20 16:23:08 +0000970 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700971 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path, format,
972 parent, storageID);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400973 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400974 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400975 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400976
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700977 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -0700978 int ret = makeFolder((const char *)path);
979 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400980 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -0800981
982 // SendObject does not get sent for directories, so call endSendObject here instead
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700983 mDatabase->endSendObject(handle, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400984 }
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700985 mSendObjectFilePath = path;
986 // save the handle for the SendObject call, which should follow
987 mSendObjectHandle = handle;
988 mSendObjectFormat = format;
989 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400990
991 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400992 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400993 mResponse.setParameter(3, handle);
994
995 return MTP_RESPONSE_OK;
996}
997
Jerry Zhang708b3e02017-09-26 17:53:39 -0700998MtpResponseCode MtpServer::doMoveObject() {
999 if (!hasStorage())
1000 return MTP_RESPONSE_GENERAL_ERROR;
1001 if (mRequest.getParameterCount() < 3)
1002 return MTP_RESPONSE_INVALID_PARAMETER;
1003 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1004 MtpStorageID storageID = mRequest.getParameter(2);
1005 MtpStorage* storage = getStorage(storageID);
1006 MtpObjectHandle parent = mRequest.getParameter(3);
1007 if (!storage)
1008 return MTP_RESPONSE_INVALID_STORAGE_ID;
1009 MtpString path;
1010 MtpResponseCode result;
1011
1012 MtpString fromPath;
1013 int64_t fileLength;
1014 MtpObjectFormat format;
1015 MtpObjectInfo info(objectHandle);
1016 result = mDatabase->getObjectInfo(objectHandle, info);
1017 if (result != MTP_RESPONSE_OK)
1018 return result;
1019 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1020 if (result != MTP_RESPONSE_OK)
1021 return result;
1022
1023 // special case the root
1024 if (parent == 0) {
1025 path = storage->getPath();
1026 } else {
1027 int64_t parentLength;
1028 MtpObjectFormat parentFormat;
1029 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1030 if (result != MTP_RESPONSE_OK)
1031 return result;
1032 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1033 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1034 }
1035
1036 if (path[path.size() - 1] != '/')
1037 path += "/";
1038 path += info.mName;
1039
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001040 result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
1041 if (result != MTP_RESPONSE_OK)
1042 return result;
1043
Jerry Zhang708b3e02017-09-26 17:53:39 -07001044 if (info.mStorageID == storageID) {
1045 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
1046 if (rename(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001047 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001048 result = MTP_RESPONSE_GENERAL_ERROR;
1049 }
1050 } else {
1051 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001052 if (format == MTP_FORMAT_ASSOCIATION) {
1053 int ret = makeFolder((const char *)path);
1054 ret += copyRecursive(fromPath, path);
1055 if (ret) {
1056 result = MTP_RESPONSE_GENERAL_ERROR;
1057 } else {
1058 deletePath(fromPath);
1059 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001060 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001061 if (copyFile(fromPath, path)) {
1062 result = MTP_RESPONSE_GENERAL_ERROR;
1063 } else {
1064 deletePath(fromPath);
1065 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001066 }
1067 }
1068
1069 // If the move failed, undo the database change
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001070 mDatabase->endMoveObject(info.mParent, parent, info.mStorageID, storageID, objectHandle,
1071 result == MTP_RESPONSE_OK);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001072
1073 return result;
1074}
1075
1076MtpResponseCode MtpServer::doCopyObject() {
1077 if (!hasStorage())
1078 return MTP_RESPONSE_GENERAL_ERROR;
1079 MtpResponseCode result = MTP_RESPONSE_OK;
1080 if (mRequest.getParameterCount() < 3)
1081 return MTP_RESPONSE_INVALID_PARAMETER;
1082 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1083 MtpStorageID storageID = mRequest.getParameter(2);
1084 MtpStorage* storage = getStorage(storageID);
1085 MtpObjectHandle parent = mRequest.getParameter(3);
1086 if (!storage)
1087 return MTP_RESPONSE_INVALID_STORAGE_ID;
1088 MtpString path;
1089
1090 MtpString fromPath;
1091 int64_t fileLength;
1092 MtpObjectFormat format;
1093 MtpObjectInfo info(objectHandle);
1094 result = mDatabase->getObjectInfo(objectHandle, info);
1095 if (result != MTP_RESPONSE_OK)
1096 return result;
1097 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1098 if (result != MTP_RESPONSE_OK)
1099 return result;
1100
1101 // special case the root
1102 if (parent == 0) {
1103 path = storage->getPath();
1104 } else {
1105 int64_t parentLength;
1106 MtpObjectFormat parentFormat;
1107 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1108 if (result != MTP_RESPONSE_OK)
1109 return result;
1110 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1111 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1112 }
1113
1114 // check space first
1115 if ((uint64_t) fileLength > storage->getFreeSpace())
1116 return MTP_RESPONSE_STORAGE_FULL;
1117
1118 if (path[path.size() - 1] != '/')
1119 path += "/";
1120 path += info.mName;
1121
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001122 MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001123 if (handle == kInvalidObjectHandle) {
1124 return MTP_RESPONSE_GENERAL_ERROR;
1125 }
1126
1127 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001128 if (format == MTP_FORMAT_ASSOCIATION) {
1129 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001130 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001131 if (ret) {
1132 result = MTP_RESPONSE_GENERAL_ERROR;
1133 }
1134 } else {
1135 if (copyFile(fromPath, path)) {
1136 result = MTP_RESPONSE_GENERAL_ERROR;
1137 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001138 }
1139
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001140 mDatabase->endCopyObject(handle, result);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001141 mResponse.setParameter(1, handle);
1142 return result;
1143}
1144
Mike Lockwood16864ba2010-05-11 17:16:59 -04001145MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001146 if (!hasStorage())
1147 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001148 MtpResponseCode result = MTP_RESPONSE_OK;
1149 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001150 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001151 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001152 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001153
Jerry Zhang487be612016-10-24 12:10:41 -07001154 auto start = std::chrono::steady_clock::now();
1155
Mike Lockwood16864ba2010-05-11 17:16:59 -04001156 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001157 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001158 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1159 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001160 }
1161
Mike Lockwoodef441d92011-07-14 21:00:02 -04001162 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001163 ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001164 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1165 result = MTP_RESPONSE_GENERAL_ERROR;
1166 goto done;
1167 }
1168 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001169
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001170 if (mSendObjectFormat == MTP_FORMAT_ASSOCIATION) {
1171 if (initialData != 0)
1172 ALOGE("Expected folder size to be 0!");
1173 mSendObjectHandle = kInvalidObjectHandle;
1174 mSendObjectFormat = 0;
1175 mSendObjectModifiedTime = 0;
1176 return result;
1177 }
1178
Mike Lockwood16864ba2010-05-11 17:16:59 -04001179 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001180 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001181 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001182 result = MTP_RESPONSE_GENERAL_ERROR;
1183 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001184 }
Jerry Zhange242f122017-10-16 14:54:08 -07001185 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001186 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001187 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001188 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001189 umask(mask);
1190
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001191 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001192 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001193 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001194
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001195 if (ret < 0) {
1196 ALOGE("failed to write initial data");
1197 result = MTP_RESPONSE_GENERAL_ERROR;
1198 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001199 mfr.offset = initialData;
1200 if (mSendObjectFileSize == 0xFFFFFFFF) {
1201 // tell driver to read until it receives a short packet
1202 mfr.length = 0xFFFFFFFF;
1203 } else {
1204 mfr.length = mSendObjectFileSize - initialData;
1205 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001206
Jerry Zhang54107562017-05-15 11:54:19 -07001207 mfr.command = 0;
1208 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001209
Jerry Zhang54107562017-05-15 11:54:19 -07001210 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001211 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001212 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1213 if ((ret < 0) && (errno == ECANCELED)) {
1214 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001215 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001216 }
caozhiyuan854cb172017-04-26 16:52:30 +08001217
1218 if (mSendObjectModifiedTime) {
1219 struct timespec newTime[2];
1220 newTime[0].tv_nsec = UTIME_NOW;
1221 newTime[1].tv_sec = mSendObjectModifiedTime;
1222 newTime[1].tv_nsec = 0;
1223 if (futimens(mfr.fd, newTime) < 0) {
1224 ALOGW("changing modified time failed, %s", strerror(errno));
1225 }
1226 }
1227
Jerry Zhang487be612016-10-24 12:10:41 -07001228 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001229 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001230
Mike Lockwood916076c2010-06-04 09:49:21 -04001231 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001232 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001233 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001234 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001235 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001236 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001237 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001238 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001239
1240done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001241 // reset so we don't attempt to send the data back
1242 mData.reset();
1243
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001244 mDatabase->endSendObject(mSendObjectHandle, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001245 mSendObjectHandle = kInvalidObjectHandle;
1246 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001247 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001248
1249 auto end = std::chrono::steady_clock::now();
1250 std::chrono::duration<double> diff = end - start;
1251 uint64_t finalsize = sstat.st_size;
1252 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1253 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001254 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001255}
1256
1257MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001258 if (!hasStorage())
1259 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001260 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001261 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001262 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001263 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001264 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1265 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001266
1267 MtpString filePath;
1268 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001269 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001270 if (result != MTP_RESPONSE_OK)
1271 return result;
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001272
Jerry Zhange5aa05d2017-10-13 12:14:42 -07001273 // Don't delete the actual files unless the database deletion is allowed
1274 result = mDatabase->beginDeleteObject(handle);
1275 if (result != MTP_RESPONSE_OK)
1276 return result;
1277
1278 bool success = deletePath((const char *)filePath);
1279
1280 mDatabase->endDeleteObject(handle, success);
1281 return success ? result : MTP_RESPONSE_PARTIAL_DELETION;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001282}
1283
1284MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001285 if (mRequest.getParameterCount() < 2)
1286 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001287 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001288 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001289 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001290 MtpDebug::getFormatCodeName(format));
1291 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001292 if (!property)
1293 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001294 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001295 delete property;
1296 return MTP_RESPONSE_OK;
1297}
1298
1299MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001300 if (mRequest.getParameterCount() < 1)
1301 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001302 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001303 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001304 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1305 if (!property)
1306 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1307 property->write(mData);
1308 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001309 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001310}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001311
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001312MtpResponseCode MtpServer::doSendPartialObject() {
1313 if (!hasStorage())
1314 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001315 if (mRequest.getParameterCount() < 4)
1316 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001317 MtpObjectHandle handle = mRequest.getParameter(1);
1318 uint64_t offset = mRequest.getParameter(2);
1319 uint64_t offset2 = mRequest.getParameter(3);
1320 offset = offset | (offset2 << 32);
1321 uint32_t length = mRequest.getParameter(4);
1322
1323 ObjectEdit* edit = getEditObject(handle);
1324 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001325 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001326 return MTP_RESPONSE_GENERAL_ERROR;
1327 }
1328
1329 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001330 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001331 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1332 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001333 return MTP_RESPONSE_GENERAL_ERROR;
1334 }
1335
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001336 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001337 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001338
Mike Lockwoodef441d92011-07-14 21:00:02 -04001339 // read the header, and possibly some data
Jerry Zhang63dac452017-12-06 15:19:36 -08001340 int ret = mData.read(mHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001341 if (ret < MTP_CONTAINER_HEADER_SIZE)
1342 return MTP_RESPONSE_GENERAL_ERROR;
1343 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1344
1345 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001346 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001347 offset += initialData;
1348 length -= initialData;
1349 }
1350
tao.pei07a9e542015-07-17 17:18:41 +08001351 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001352 if (ret < 0) {
1353 ALOGE("failed to write initial data");
1354 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001355 mtp_file_range mfr;
1356 mfr.fd = edit->mFD;
1357 mfr.offset = offset;
1358 mfr.length = length;
1359 mfr.command = 0;
1360 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001361
Jerry Zhang54107562017-05-15 11:54:19 -07001362 // transfer the file
Jerry Zhang63dac452017-12-06 15:19:36 -08001363 ret = mHandle->receiveFile(mfr, mfr.length == 0 &&
Jerry Zhang54107562017-05-15 11:54:19 -07001364 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1365 if ((ret < 0) && (errno == ECANCELED)) {
1366 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001367 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001368 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001369 if (ret < 0) {
1370 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001371 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001372 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1373 else
1374 return MTP_RESPONSE_GENERAL_ERROR;
1375 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001376
1377 // reset so we don't attempt to send this back
1378 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001379 mResponse.setParameter(1, length);
1380 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001381 if (end > edit->mSize) {
1382 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001383 }
1384 return MTP_RESPONSE_OK;
1385}
1386
1387MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001388 if (mRequest.getParameterCount() < 3)
1389 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001390 MtpObjectHandle handle = mRequest.getParameter(1);
1391 ObjectEdit* edit = getEditObject(handle);
1392 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001393 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001394 return MTP_RESPONSE_GENERAL_ERROR;
1395 }
1396
1397 uint64_t offset = mRequest.getParameter(2);
1398 uint64_t offset2 = mRequest.getParameter(3);
1399 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001400 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001401 return MTP_RESPONSE_GENERAL_ERROR;
1402 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001403 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001404 return MTP_RESPONSE_OK;
1405 }
1406}
1407
1408MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001409 if (mRequest.getParameterCount() < 1)
1410 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001411 MtpObjectHandle handle = mRequest.getParameter(1);
1412 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001413 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001414 return MTP_RESPONSE_GENERAL_ERROR;
1415 }
1416
1417 MtpString path;
1418 int64_t fileLength;
1419 MtpObjectFormat format;
1420 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1421 if (result != MTP_RESPONSE_OK)
1422 return result;
1423
1424 int fd = open((const char *)path, O_RDWR | O_EXCL);
1425 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001426 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001427 return MTP_RESPONSE_GENERAL_ERROR;
1428 }
1429
1430 addEditObject(handle, path, fileLength, format, fd);
1431 return MTP_RESPONSE_OK;
1432}
1433
1434MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001435 if (mRequest.getParameterCount() < 1)
1436 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001437 MtpObjectHandle handle = mRequest.getParameter(1);
1438 ObjectEdit* edit = getEditObject(handle);
1439 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001440 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001441 return MTP_RESPONSE_GENERAL_ERROR;
1442 }
1443
1444 commitEdit(edit);
1445 removeEditObject(handle);
1446 return MTP_RESPONSE_OK;
1447}
1448
Mike Lockwood7850ef92010-05-14 10:10:36 -04001449} // namespace android