blob: bb0414d8fa925ac5eb5614b2272d5af5fd680daa [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 Zhange242f122017-10-16 14:54:08 -070017#include <android-base/logging.h>
Jerry Zhang487be612016-10-24 12:10:41 -070018#include <android-base/properties.h>
19#include <chrono>
Jerry Zhang487be612016-10-24 12:10:41 -070020#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040024#include <stdio.h>
25#include <stdlib.h>
26#include <sys/types.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040027#include <sys/stat.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040028#include <sys/stat.h>
caozhiyuan854cb172017-04-26 16:52:30 +080029#include <sys/time.h>
Mike Lockwoodc42aa122010-06-14 17:58:08 -070030
Mike Lockwooda881b442010-09-23 22:32:05 -040031#define LOG_TAG "MtpServer"
32
Mike Lockwood16864ba2010-05-11 17:16:59 -040033#include "MtpDebug.h"
Mike Lockwood7f53a192010-07-09 10:45:22 -040034#include "MtpDatabase.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070035#include "MtpDevHandle.h"
36#include "MtpFfsCompatHandle.h"
37#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070038#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040039#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040040#include "MtpServer.h"
41#include "MtpStorage.h"
42#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040043
Mike Lockwood7850ef92010-05-14 10:10:36 -040044namespace android {
45
Mike Lockwood16864ba2010-05-11 17:16:59 -040046static const MtpOperationCode kSupportedOperationCodes[] = {
47 MTP_OPERATION_GET_DEVICE_INFO,
48 MTP_OPERATION_OPEN_SESSION,
49 MTP_OPERATION_CLOSE_SESSION,
50 MTP_OPERATION_GET_STORAGE_IDS,
51 MTP_OPERATION_GET_STORAGE_INFO,
52 MTP_OPERATION_GET_NUM_OBJECTS,
53 MTP_OPERATION_GET_OBJECT_HANDLES,
54 MTP_OPERATION_GET_OBJECT_INFO,
55 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070056 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040057 MTP_OPERATION_DELETE_OBJECT,
58 MTP_OPERATION_SEND_OBJECT_INFO,
59 MTP_OPERATION_SEND_OBJECT,
60// MTP_OPERATION_INITIATE_CAPTURE,
61// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080062 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040063// MTP_OPERATION_SELF_TEST,
64// MTP_OPERATION_SET_OBJECT_PROTECTION,
65// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040066 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040067 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
68 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
69 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040070// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070071 MTP_OPERATION_MOVE_OBJECT,
72 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050073 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040074// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
75 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040076 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040077 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
78 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040079 MTP_OPERATION_GET_OBJECT_PROP_LIST,
80// MTP_OPERATION_SET_OBJECT_PROP_LIST,
81// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
82// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040083 MTP_OPERATION_GET_OBJECT_REFERENCES,
84 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040085// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070086 // Android extension for direct file IO
87 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
88 MTP_OPERATION_SEND_PARTIAL_OBJECT,
89 MTP_OPERATION_TRUNCATE_OBJECT,
90 MTP_OPERATION_BEGIN_EDIT_OBJECT,
91 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040092};
93
Mike Lockwood873871f2010-07-12 18:54:16 -040094static const MtpEventCode kSupportedEventCodes[] = {
95 MTP_EVENT_OBJECT_ADDED,
96 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050097 MTP_EVENT_STORE_ADDED,
98 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -080099 MTP_EVENT_DEVICE_PROP_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -0400100};
101
Jerry Zhang487be612016-10-24 12:10:41 -0700102MtpServer::MtpServer(MtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800103 const MtpString& deviceInfoManufacturer,
104 const MtpString& deviceInfoModel,
105 const MtpString& deviceInfoDeviceVersion,
106 const MtpString& deviceInfoSerialNumber)
Jerry Zhang487be612016-10-24 12:10:41 -0700107 : mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400108 mPtp(ptp),
Alex Klyubin792298f2016-12-21 11:20:22 -0800109 mDeviceInfoManufacturer(deviceInfoManufacturer),
110 mDeviceInfoModel(deviceInfoModel),
111 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
112 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400113 mSessionID(0),
114 mSessionOpen(false),
115 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400116 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800117 mSendObjectFileSize(0),
118 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400119{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400120}
121
122MtpServer::~MtpServer() {
123}
124
Jerry Zhang487be612016-10-24 12:10:41 -0700125IMtpHandle* MtpServer::sHandle = nullptr;
126
127int MtpServer::configure(bool usePtp) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700128 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700129 if (sHandle == nullptr) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700130 if (ffs_ok) {
131 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
132 sHandle = aio_compat ? new MtpFfsCompatHandle() : new MtpFfsHandle();
133 } else {
134 sHandle = new MtpDevHandle();
135 }
Jerry Zhang487be612016-10-24 12:10:41 -0700136 }
Jerry Zhangfbc8b062017-10-23 12:03:40 -0700137
138 int ret = sHandle->configure(usePtp);
139 if (ret) ALOGE("Failed to configure MTP driver!");
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700140 android::base::SetProperty("sys.usb.ffs.mtp.ready", "1");
Jerry Zhangfbc8b062017-10-23 12:03:40 -0700141 return ret;
Jerry Zhang487be612016-10-24 12:10:41 -0700142}
143
Mike Lockwooda8494402011-02-18 09:07:14 -0500144void MtpServer::addStorage(MtpStorage* storage) {
145 Mutex::Autolock autoLock(mMutex);
146
147 mStorages.push(storage);
148 sendStoreAdded(storage->getStorageID());
149}
150
151void MtpServer::removeStorage(MtpStorage* storage) {
152 Mutex::Autolock autoLock(mMutex);
153
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700154 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500155 if (mStorages[i] == storage) {
156 mStorages.removeAt(i);
157 sendStoreRemoved(storage->getStorageID());
158 break;
159 }
160 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400161}
162
163MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800164 if (id == 0)
165 return mStorages[0];
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700166 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800167 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400168 if (storage->getStorageID() == id)
169 return storage;
170 }
Jerry Zhang487be612016-10-24 12:10:41 -0700171 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172}
173
Mike Lockwooda8494402011-02-18 09:07:14 -0500174bool MtpServer::hasStorage(MtpStorageID id) {
175 if (id == 0 || id == 0xFFFFFFFF)
176 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700177 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500178}
179
Mike Lockwood16864ba2010-05-11 17:16:59 -0400180void MtpServer::run() {
Jerry Zhang487be612016-10-24 12:10:41 -0700181 if (!sHandle) {
182 ALOGE("MtpServer was never configured!");
183 return;
184 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400185
Jerry Zhang487be612016-10-24 12:10:41 -0700186 if (sHandle->start()) {
187 ALOGE("Failed to start usb driver!");
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -0800188 sHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700189 return;
190 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400191
192 while (1) {
Jerry Zhang487be612016-10-24 12:10:41 -0700193 int ret = mRequest.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400194 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700195 ALOGE("request 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 }
202 MtpOperationCode operation = mRequest.getOperationCode();
203 MtpTransactionID transaction = mRequest.getTransactionID();
204
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400206 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400207 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400208 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
209 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
210 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400211 if (dataIn) {
Jerry Zhang487be612016-10-24 12:10:41 -0700212 int ret = mData.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400213 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000214 ALOGE("data read 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 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400219 break;
220 }
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400222 } else {
223 mData.reset();
224 }
225
Mike Lockwood916076c2010-06-04 09:49:21 -0400226 if (handleRequest()) {
227 if (!dataIn && mData.hasData()) {
228 mData.setOperationCode(operation);
229 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("sending data:");
Jerry Zhang487be612016-10-24 12:10:41 -0700231 ret = mData.write(sHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400232 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000233 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400234 if (errno == ECANCELED) {
235 // return to top of loop and wait for next command
236 continue;
237 }
238 break;
239 }
240 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400241
Mike Lockwood916076c2010-06-04 09:49:21 -0400242 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang487be612016-10-24 12:10:41 -0700244 ret = mResponse.write(sHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800245 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400246 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000247 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800248 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400249 // return to top of loop and wait for next command
250 continue;
251 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400252 break;
253 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400254 } else {
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400256 }
257 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400258
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700259 // commit any open edits
260 int count = mObjectEditList.size();
261 for (int i = 0; i < count; i++) {
262 ObjectEdit* edit = mObjectEditList[i];
263 commitEdit(edit);
264 delete edit;
265 }
266 mObjectEditList.clear();
267
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400268 if (mSessionOpen)
269 mDatabase->sessionEnded();
Jerry Zhang487be612016-10-24 12:10:41 -0700270
271 sHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400272}
273
Mike Lockwood873871f2010-07-12 18:54:16 -0400274void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500276 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400277}
278
279void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100280 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500281 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
282}
283
284void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500286 sendEvent(MTP_EVENT_STORE_ADDED, id);
287}
288
289void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100290 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500291 sendEvent(MTP_EVENT_STORE_REMOVED, id);
292}
293
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800294void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
295 ALOGV("sendDevicePropertyChanged %d\n", property);
296 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
297}
298
Mike Lockwooda8494402011-02-18 09:07:14 -0500299void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400300 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500301 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400302 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500303 mEvent.setParameter(1, param1);
Jerry Zhang487be612016-10-24 12:10:41 -0700304 if (mEvent.write(sHandle))
305 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400306 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400307}
308
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700309void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
310 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700311 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700312 mObjectEditList.add(edit);
313}
314
315MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
316 int count = mObjectEditList.size();
317 for (int i = 0; i < count; i++) {
318 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700319 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700320 }
Jerry Zhang487be612016-10-24 12:10:41 -0700321 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700322}
323
324void MtpServer::removeEditObject(MtpObjectHandle handle) {
325 int count = mObjectEditList.size();
326 for (int i = 0; i < count; i++) {
327 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700328 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700329 delete edit;
330 mObjectEditList.removeAt(i);
331 return;
332 }
333 }
Steve Block29357bc2012-01-06 19:20:56 +0000334 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700335}
336
337void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700338 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700339}
340
341
Mike Lockwood916076c2010-06-04 09:49:21 -0400342bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500343 Mutex::Autolock autoLock(mMutex);
344
Mike Lockwood16864ba2010-05-11 17:16:59 -0400345 MtpOperationCode operation = mRequest.getOperationCode();
346 MtpResponseCode response;
347
348 mResponse.reset();
349
350 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
351 // FIXME - need to delete mSendObjectHandle from the database
Steve Block29357bc2012-01-06 19:20:56 +0000352 ALOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400353 mSendObjectHandle = kInvalidObjectHandle;
354 }
355
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700356 int containertype = mRequest.getContainerType();
357 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
358 ALOGE("wrong container type %d", containertype);
359 return false;
360 }
361
362 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
363
Mike Lockwood16864ba2010-05-11 17:16:59 -0400364 switch (operation) {
365 case MTP_OPERATION_GET_DEVICE_INFO:
366 response = doGetDeviceInfo();
367 break;
368 case MTP_OPERATION_OPEN_SESSION:
369 response = doOpenSession();
370 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800371 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400372 case MTP_OPERATION_CLOSE_SESSION:
373 response = doCloseSession();
374 break;
375 case MTP_OPERATION_GET_STORAGE_IDS:
376 response = doGetStorageIDs();
377 break;
378 case MTP_OPERATION_GET_STORAGE_INFO:
379 response = doGetStorageInfo();
380 break;
381 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
382 response = doGetObjectPropsSupported();
383 break;
384 case MTP_OPERATION_GET_OBJECT_HANDLES:
385 response = doGetObjectHandles();
386 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400387 case MTP_OPERATION_GET_NUM_OBJECTS:
388 response = doGetNumObjects();
389 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400390 case MTP_OPERATION_GET_OBJECT_REFERENCES:
391 response = doGetObjectReferences();
392 break;
393 case MTP_OPERATION_SET_OBJECT_REFERENCES:
394 response = doSetObjectReferences();
395 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400396 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
397 response = doGetObjectPropValue();
398 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400399 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
400 response = doSetObjectPropValue();
401 break;
402 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
403 response = doGetDevicePropValue();
404 break;
405 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
406 response = doSetDevicePropValue();
407 break;
408 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
409 response = doResetDevicePropValue();
410 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400411 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
412 response = doGetObjectPropList();
413 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400414 case MTP_OPERATION_GET_OBJECT_INFO:
415 response = doGetObjectInfo();
416 break;
417 case MTP_OPERATION_GET_OBJECT:
418 response = doGetObject();
419 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700420 case MTP_OPERATION_GET_THUMB:
421 response = doGetThumb();
422 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500423 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700424 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
425 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500426 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400427 case MTP_OPERATION_SEND_OBJECT_INFO:
428 response = doSendObjectInfo();
429 break;
430 case MTP_OPERATION_SEND_OBJECT:
431 response = doSendObject();
432 break;
433 case MTP_OPERATION_DELETE_OBJECT:
434 response = doDeleteObject();
435 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700436 case MTP_OPERATION_COPY_OBJECT:
437 response = doCopyObject();
438 break;
439 case MTP_OPERATION_MOVE_OBJECT:
440 response = doMoveObject();
441 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400442 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400443 response = doGetObjectPropDesc();
444 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400445 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
446 response = doGetDevicePropDesc();
447 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700448 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
449 response = doSendPartialObject();
450 break;
451 case MTP_OPERATION_TRUNCATE_OBJECT:
452 response = doTruncateObject();
453 break;
454 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
455 response = doBeginEditObject();
456 break;
457 case MTP_OPERATION_END_EDIT_OBJECT:
458 response = doEndEditObject();
459 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400460 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700461 ALOGE("got unsupported command %s (%x)",
462 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400463 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
464 break;
465 }
466
Mike Lockwood916076c2010-06-04 09:49:21 -0400467 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
468 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400469 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400470 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400471}
472
473MtpResponseCode MtpServer::doGetDeviceInfo() {
474 MtpStringBuffer string;
475
Mike Lockwood782aef12010-08-10 07:37:50 -0400476 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
477 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
478 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
479
Mike Lockwood16864ba2010-05-11 17:16:59 -0400480 // fill in device info
481 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400482 if (mPtp) {
483 mData.putUInt32(0);
484 } else {
485 // MTP Vendor Extension ID
486 mData.putUInt32(6);
487 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400488 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400489 if (mPtp) {
490 // no extensions
491 string.set("");
492 } else {
493 // MTP extensions
494 string.set("microsoft.com: 1.0; android.com: 1.0;");
495 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400496 mData.putString(string); // MTP Extensions
497 mData.putUInt16(0); //Functional Mode
498 mData.putAUInt16(kSupportedOperationCodes,
499 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400500 mData.putAUInt16(kSupportedEventCodes,
501 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400502 mData.putAUInt16(deviceProperties); // Device Properties Supported
503 mData.putAUInt16(captureFormats); // Capture Formats
504 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500505
Alex Klyubin792298f2016-12-21 11:20:22 -0800506 mData.putString(mDeviceInfoManufacturer); // Manufacturer
507 mData.putString(mDeviceInfoModel); // Model
508 mData.putString(mDeviceInfoDeviceVersion); // Device Version
509 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400510
Mike Lockwood782aef12010-08-10 07:37:50 -0400511 delete playbackFormats;
512 delete captureFormats;
513 delete deviceProperties;
514
Mike Lockwood16864ba2010-05-11 17:16:59 -0400515 return MTP_RESPONSE_OK;
516}
517
518MtpResponseCode MtpServer::doOpenSession() {
519 if (mSessionOpen) {
520 mResponse.setParameter(1, mSessionID);
521 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
522 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800523 if (mRequest.getParameterCount() < 1)
524 return MTP_RESPONSE_INVALID_PARAMETER;
525
Mike Lockwood16864ba2010-05-11 17:16:59 -0400526 mSessionID = mRequest.getParameter(1);
527 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400528
529 mDatabase->sessionStarted();
530
Mike Lockwood16864ba2010-05-11 17:16:59 -0400531 return MTP_RESPONSE_OK;
532}
533
534MtpResponseCode MtpServer::doCloseSession() {
535 if (!mSessionOpen)
536 return MTP_RESPONSE_SESSION_NOT_OPEN;
537 mSessionID = 0;
538 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400539 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400540 return MTP_RESPONSE_OK;
541}
542
543MtpResponseCode MtpServer::doGetStorageIDs() {
544 if (!mSessionOpen)
545 return MTP_RESPONSE_SESSION_NOT_OPEN;
546
547 int count = mStorages.size();
548 mData.putUInt32(count);
549 for (int i = 0; i < count; i++)
550 mData.putUInt32(mStorages[i]->getStorageID());
551
552 return MTP_RESPONSE_OK;
553}
554
555MtpResponseCode MtpServer::doGetStorageInfo() {
556 MtpStringBuffer string;
557
558 if (!mSessionOpen)
559 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800560 if (mRequest.getParameterCount() < 1)
561 return MTP_RESPONSE_INVALID_PARAMETER;
562
Mike Lockwood16864ba2010-05-11 17:16:59 -0400563 MtpStorageID id = mRequest.getParameter(1);
564 MtpStorage* storage = getStorage(id);
565 if (!storage)
566 return MTP_RESPONSE_INVALID_STORAGE_ID;
567
568 mData.putUInt16(storage->getType());
569 mData.putUInt16(storage->getFileSystemType());
570 mData.putUInt16(storage->getAccessCapability());
571 mData.putUInt64(storage->getMaxCapacity());
572 mData.putUInt64(storage->getFreeSpace());
573 mData.putUInt32(1024*1024*1024); // Free Space in Objects
574 string.set(storage->getDescription());
575 mData.putString(string);
576 mData.putEmptyString(); // Volume Identifier
577
578 return MTP_RESPONSE_OK;
579}
580
581MtpResponseCode MtpServer::doGetObjectPropsSupported() {
582 if (!mSessionOpen)
583 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800584 if (mRequest.getParameterCount() < 1)
585 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400586 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800587 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400588 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400589 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400590 return MTP_RESPONSE_OK;
591}
592
593MtpResponseCode MtpServer::doGetObjectHandles() {
594 if (!mSessionOpen)
595 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800596 if (mRequest.getParameterCount() < 3)
597 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400598 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400599 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400600 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400601 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500602
603 if (!hasStorage(storageID))
604 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400605
606 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
607 mData.putAUInt32(handles);
608 delete handles;
609 return MTP_RESPONSE_OK;
610}
611
Mike Lockwood343af4e2010-08-02 10:52:20 -0400612MtpResponseCode MtpServer::doGetNumObjects() {
613 if (!mSessionOpen)
614 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800615 if (mRequest.getParameterCount() < 3)
616 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400617 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
618 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
619 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400620 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500621 if (!hasStorage(storageID))
622 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400623
624 int count = mDatabase->getNumObjects(storageID, format, parent);
625 if (count >= 0) {
626 mResponse.setParameter(1, count);
627 return MTP_RESPONSE_OK;
628 } else {
629 mResponse.setParameter(1, 0);
630 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
631 }
632}
633
Mike Lockwood438344f2010-08-03 15:30:09 -0400634MtpResponseCode MtpServer::doGetObjectReferences() {
635 if (!mSessionOpen)
636 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500637 if (!hasStorage())
638 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800639 if (mRequest.getParameterCount() < 1)
640 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500641 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400642
643 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400644 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400645 if (handles) {
646 mData.putAUInt32(handles);
647 delete handles;
648 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400649 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400650 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400651 return MTP_RESPONSE_OK;
652}
653
654MtpResponseCode MtpServer::doSetObjectReferences() {
655 if (!mSessionOpen)
656 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500657 if (!hasStorage())
658 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800659 if (mRequest.getParameterCount() < 1)
660 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400661 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500662
Mike Lockwood438344f2010-08-03 15:30:09 -0400663 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800664 if (!references)
665 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400666 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
667 delete references;
668 return result;
669}
670
Mike Lockwood16864ba2010-05-11 17:16:59 -0400671MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500672 if (!hasStorage())
673 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800674 if (mRequest.getParameterCount() < 2)
675 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400676 MtpObjectHandle handle = mRequest.getParameter(1);
677 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400679 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400680
Mike Lockwood8277cec2010-08-10 15:20:35 -0400681 return mDatabase->getObjectPropertyValue(handle, property, mData);
682}
683
684MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500685 if (!hasStorage())
686 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800687 if (mRequest.getParameterCount() < 2)
688 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400689 MtpObjectHandle handle = mRequest.getParameter(1);
690 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400692 MtpDebug::getObjectPropCodeName(property));
693
694 return mDatabase->setObjectPropertyValue(handle, property, mData);
695}
696
697MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800698 if (mRequest.getParameterCount() < 1)
699 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400700 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400702 MtpDebug::getDevicePropCodeName(property));
703
704 return mDatabase->getDevicePropertyValue(property, mData);
705}
706
707MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800708 if (mRequest.getParameterCount() < 1)
709 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400710 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400712 MtpDebug::getDevicePropCodeName(property));
713
714 return mDatabase->setDevicePropertyValue(property, mData);
715}
716
717MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800718 if (mRequest.getParameterCount() < 1)
719 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400720 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400722 MtpDebug::getDevicePropCodeName(property));
723
724 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400725}
726
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400727MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500728 if (!hasStorage())
729 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800730 if (mRequest.getParameterCount() < 5)
731 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400732
733 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500734 // use uint32_t so we can support 0xFFFFFFFF
735 uint32_t format = mRequest.getParameter(2);
736 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400737 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500738 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400740 handle, MtpDebug::getFormatCodeName(format),
741 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
742
743 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
744}
745
Mike Lockwood16864ba2010-05-11 17:16:59 -0400746MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500747 if (!hasStorage())
748 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800749 if (mRequest.getParameterCount() < 1)
750 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400751 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700752 MtpObjectInfo info(handle);
753 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
754 if (result == MTP_RESPONSE_OK) {
755 char date[20];
756
757 mData.putUInt32(info.mStorageID);
758 mData.putUInt16(info.mFormat);
759 mData.putUInt16(info.mProtectionStatus);
760
761 // if object is being edited the database size may be out of date
762 uint32_t size = info.mCompressedSize;
763 ObjectEdit* edit = getEditObject(handle);
764 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700765 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700766 mData.putUInt32(size);
767
768 mData.putUInt16(info.mThumbFormat);
769 mData.putUInt32(info.mThumbCompressedSize);
770 mData.putUInt32(info.mThumbPixWidth);
771 mData.putUInt32(info.mThumbPixHeight);
772 mData.putUInt32(info.mImagePixWidth);
773 mData.putUInt32(info.mImagePixHeight);
774 mData.putUInt32(info.mImagePixDepth);
775 mData.putUInt32(info.mParent);
776 mData.putUInt16(info.mAssociationType);
777 mData.putUInt32(info.mAssociationDesc);
778 mData.putUInt32(info.mSequenceNumber);
779 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700780 formatDateTime(info.mDateCreated, date, sizeof(date));
781 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700782 formatDateTime(info.mDateModified, date, sizeof(date));
783 mData.putString(date); // date modified
784 mData.putEmptyString(); // keywords
785 }
786 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400787}
788
789MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500790 if (!hasStorage())
791 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800792 if (mRequest.getParameterCount() < 1)
793 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400794 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400795 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400796 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800797 MtpObjectFormat format;
798 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400799 if (result != MTP_RESPONSE_OK)
800 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400801
Jerry Zhang487be612016-10-24 12:10:41 -0700802 auto start = std::chrono::steady_clock::now();
803
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400804 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400805 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400806 mfr.fd = open(filePath, O_RDONLY);
807 if (mfr.fd < 0) {
808 return MTP_RESPONSE_GENERAL_ERROR;
809 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400810 mfr.offset = 0;
811 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400812 mfr.command = mRequest.getOperationCode();
813 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400814
815 // then transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700816 int ret = sHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800817 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700818 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800819 if (errno == ECANCELED) {
820 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
821 } else {
822 result = MTP_RESPONSE_GENERAL_ERROR;
823 }
824 } else {
825 result = MTP_RESPONSE_OK;
826 }
827
Jerry Zhang487be612016-10-24 12:10:41 -0700828 auto end = std::chrono::steady_clock::now();
829 std::chrono::duration<double> diff = end - start;
830 struct stat sstat;
831 fstat(mfr.fd, &sstat);
832 uint64_t finalsize = sstat.st_size;
833 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
834 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400835 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800836 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400837}
838
Mike Lockwood64000782011-04-24 18:40:17 -0700839MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800840 if (mRequest.getParameterCount() < 1)
841 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700842 MtpObjectHandle handle = mRequest.getParameter(1);
843 size_t thumbSize;
844 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
845 if (thumb) {
846 // send data
847 mData.setOperationCode(mRequest.getOperationCode());
848 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang487be612016-10-24 12:10:41 -0700849 mData.writeData(sHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700850 free(thumb);
851 return MTP_RESPONSE_OK;
852 } else {
853 return MTP_RESPONSE_GENERAL_ERROR;
854 }
855}
856
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700857MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500858 if (!hasStorage())
859 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500860 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700861 uint64_t offset;
862 uint32_t length;
863 offset = mRequest.getParameter(2);
864 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800865 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
866 if (mRequest.getParameterCount() < 4)
867 return MTP_RESPONSE_INVALID_PARAMETER;
868
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700869 // android extension with 64 bit offset
870 uint64_t offset2 = mRequest.getParameter(3);
871 offset = offset | (offset2 << 32);
872 length = mRequest.getParameter(4);
873 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800874 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
875 if (mRequest.getParameterCount() < 3)
876 return MTP_RESPONSE_INVALID_PARAMETER;
877
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700878 // standard GetPartialObject
879 length = mRequest.getParameter(3);
880 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500881 MtpString pathBuf;
882 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800883 MtpObjectFormat format;
884 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500885 if (result != MTP_RESPONSE_OK)
886 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700887 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500888 length = fileLength - offset;
889
890 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700891 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500892 mtp_file_range mfr;
893 mfr.fd = open(filePath, O_RDONLY);
894 if (mfr.fd < 0) {
895 return MTP_RESPONSE_GENERAL_ERROR;
896 }
897 mfr.offset = offset;
898 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400899 mfr.command = mRequest.getOperationCode();
900 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500901 mResponse.setParameter(1, length);
902
Mike Lockwoodef441d92011-07-14 21:00:02 -0400903 // transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700904 int ret = sHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800906 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500907 if (ret < 0) {
908 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800909 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500910 else
tao.pei07a9e542015-07-17 17:18:41 +0800911 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500912 }
tao.pei07a9e542015-07-17 17:18:41 +0800913 close(mfr.fd);
914 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500915}
916
Mike Lockwood16864ba2010-05-11 17:16:59 -0400917MtpResponseCode MtpServer::doSendObjectInfo() {
918 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800919 uint16_t temp16;
920 uint32_t temp32;
921
922 if (mRequest.getParameterCount() < 2)
923 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400924 MtpStorageID storageID = mRequest.getParameter(1);
925 MtpStorage* storage = getStorage(storageID);
926 MtpObjectHandle parent = mRequest.getParameter(2);
927 if (!storage)
928 return MTP_RESPONSE_INVALID_STORAGE_ID;
929
930 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400931 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400932 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400933 parent = 0;
934 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800935 int64_t length;
936 MtpObjectFormat format;
937 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400938 if (result != MTP_RESPONSE_OK)
939 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800940 if (format != MTP_FORMAT_ASSOCIATION)
941 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400942 }
943
944 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800945 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
946 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
947 MtpObjectFormat format = temp16;
948 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
949 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
950 mSendObjectFileSize = temp32;
951 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
952 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
953 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
954 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
955 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
956 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
957 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
958 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
959 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800960 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800961 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400962 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800963 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700964 if (name.getCharCount() == 0) {
965 ALOGE("empty name");
966 return MTP_RESPONSE_INVALID_PARAMETER;
967 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800968 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
969 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400970 // keywords follow
971
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400973 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400974 if (!parseDateTime(modified, modifiedTime))
975 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400976
977 if (path[path.size() - 1] != '/')
978 path += "/";
979 path += (const char *)name;
980
Mike Lockwood20c3be02010-12-12 12:17:43 -0800981 // check space first
982 if (mSendObjectFileSize > storage->getFreeSpace())
983 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400984 uint64_t maxFileSize = storage->getMaxFileSize();
985 // check storage max file size
986 if (maxFileSize != 0) {
987 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
988 // is >= 0xFFFFFFFF
989 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
990 return MTP_RESPONSE_OBJECT_TOO_LARGE;
991 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800992
Steve Blockb8a80522011-12-20 16:23:08 +0000993 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400994 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
995 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400996 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400997 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400998 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400999
1000 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001001 int ret = makeFolder((const char *)path);
1002 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001003 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001004
1005 // SendObject does not get sent for directories, so call endSendObject here instead
1006 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001007 } else {
1008 mSendObjectFilePath = path;
1009 // save the handle for the SendObject call, which should follow
1010 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -04001011 mSendObjectFormat = format;
caozhiyuan854cb172017-04-26 16:52:30 +08001012 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001013 }
1014
1015 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001016 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001017 mResponse.setParameter(3, handle);
1018
1019 return MTP_RESPONSE_OK;
1020}
1021
Jerry Zhang708b3e02017-09-26 17:53:39 -07001022MtpResponseCode MtpServer::doMoveObject() {
1023 if (!hasStorage())
1024 return MTP_RESPONSE_GENERAL_ERROR;
1025 if (mRequest.getParameterCount() < 3)
1026 return MTP_RESPONSE_INVALID_PARAMETER;
1027 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1028 MtpStorageID storageID = mRequest.getParameter(2);
1029 MtpStorage* storage = getStorage(storageID);
1030 MtpObjectHandle parent = mRequest.getParameter(3);
1031 if (!storage)
1032 return MTP_RESPONSE_INVALID_STORAGE_ID;
1033 MtpString path;
1034 MtpResponseCode result;
1035
1036 MtpString fromPath;
1037 int64_t fileLength;
1038 MtpObjectFormat format;
1039 MtpObjectInfo info(objectHandle);
1040 result = mDatabase->getObjectInfo(objectHandle, info);
1041 if (result != MTP_RESPONSE_OK)
1042 return result;
1043 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1044 if (result != MTP_RESPONSE_OK)
1045 return result;
1046
1047 // special case the root
1048 if (parent == 0) {
1049 path = storage->getPath();
1050 } else {
1051 int64_t parentLength;
1052 MtpObjectFormat parentFormat;
1053 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1054 if (result != MTP_RESPONSE_OK)
1055 return result;
1056 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1057 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1058 }
1059
1060 if (path[path.size() - 1] != '/')
1061 path += "/";
1062 path += info.mName;
1063
Jerry Zhang708b3e02017-09-26 17:53:39 -07001064 if (info.mStorageID == storageID) {
1065 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
1066 if (rename(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001067 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001068 result = MTP_RESPONSE_GENERAL_ERROR;
1069 }
1070 } else {
1071 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001072 if (format == MTP_FORMAT_ASSOCIATION) {
1073 int ret = makeFolder((const char *)path);
1074 ret += copyRecursive(fromPath, path);
1075 if (ret) {
1076 result = MTP_RESPONSE_GENERAL_ERROR;
1077 } else {
1078 deletePath(fromPath);
1079 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001080 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001081 if (copyFile(fromPath, path)) {
1082 result = MTP_RESPONSE_GENERAL_ERROR;
1083 } else {
1084 deletePath(fromPath);
1085 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001086 }
1087 }
1088
1089 // If the move failed, undo the database change
Jerry Zhangee8946f2017-10-31 12:26:07 -07001090 if (result == MTP_RESPONSE_OK)
1091 result = mDatabase->moveObject(objectHandle, parent, storageID, path);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001092
1093 return result;
1094}
1095
1096MtpResponseCode MtpServer::doCopyObject() {
1097 if (!hasStorage())
1098 return MTP_RESPONSE_GENERAL_ERROR;
1099 MtpResponseCode result = MTP_RESPONSE_OK;
1100 if (mRequest.getParameterCount() < 3)
1101 return MTP_RESPONSE_INVALID_PARAMETER;
1102 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1103 MtpStorageID storageID = mRequest.getParameter(2);
1104 MtpStorage* storage = getStorage(storageID);
1105 MtpObjectHandle parent = mRequest.getParameter(3);
1106 if (!storage)
1107 return MTP_RESPONSE_INVALID_STORAGE_ID;
1108 MtpString path;
1109
1110 MtpString fromPath;
1111 int64_t fileLength;
1112 MtpObjectFormat format;
1113 MtpObjectInfo info(objectHandle);
1114 result = mDatabase->getObjectInfo(objectHandle, info);
1115 if (result != MTP_RESPONSE_OK)
1116 return result;
1117 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1118 if (result != MTP_RESPONSE_OK)
1119 return result;
1120
1121 // special case the root
1122 if (parent == 0) {
1123 path = storage->getPath();
1124 } else {
1125 int64_t parentLength;
1126 MtpObjectFormat parentFormat;
1127 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1128 if (result != MTP_RESPONSE_OK)
1129 return result;
1130 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1131 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1132 }
1133
1134 // check space first
1135 if ((uint64_t) fileLength > storage->getFreeSpace())
1136 return MTP_RESPONSE_STORAGE_FULL;
1137
1138 if (path[path.size() - 1] != '/')
1139 path += "/";
1140 path += info.mName;
1141
1142 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
1143 format, parent, storageID, fileLength, info.mDateModified);
1144 if (handle == kInvalidObjectHandle) {
1145 return MTP_RESPONSE_GENERAL_ERROR;
1146 }
1147
1148 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001149 if (format == MTP_FORMAT_ASSOCIATION) {
1150 int ret = makeFolder((const char *)path);
kyle_tso6de16602017-11-22 18:14:51 +08001151 ret += copyRecursive(fromPath, path);
Jerry Zhange242f122017-10-16 14:54:08 -07001152 if (ret) {
1153 result = MTP_RESPONSE_GENERAL_ERROR;
1154 }
1155 } else {
1156 if (copyFile(fromPath, path)) {
1157 result = MTP_RESPONSE_GENERAL_ERROR;
1158 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001159 }
1160
1161 mDatabase->endSendObject(path, handle, format, result);
kyle_tso6de16602017-11-22 18:14:51 +08001162 if (format == MTP_FORMAT_ASSOCIATION)
1163 mDatabase->doScanDirectory(path);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001164 mResponse.setParameter(1, handle);
1165 return result;
1166}
1167
Mike Lockwood16864ba2010-05-11 17:16:59 -04001168MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001169 if (!hasStorage())
1170 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001171 MtpResponseCode result = MTP_RESPONSE_OK;
1172 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001173 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001174 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001175 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001176
Jerry Zhang487be612016-10-24 12:10:41 -07001177 auto start = std::chrono::steady_clock::now();
1178
Mike Lockwood16864ba2010-05-11 17:16:59 -04001179 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001180 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001181 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1182 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001183 }
1184
Mike Lockwoodef441d92011-07-14 21:00:02 -04001185 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001186 ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001187 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1188 result = MTP_RESPONSE_GENERAL_ERROR;
1189 goto done;
1190 }
1191 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001192
1193 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001194 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001195 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001196 result = MTP_RESPONSE_GENERAL_ERROR;
1197 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001198 }
Jerry Zhange242f122017-10-16 14:54:08 -07001199 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001200 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001201 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001202 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001203 umask(mask);
1204
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001205 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001206 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001207 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001208
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001209 if (ret < 0) {
1210 ALOGE("failed to write initial data");
1211 result = MTP_RESPONSE_GENERAL_ERROR;
1212 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001213 mfr.offset = initialData;
1214 if (mSendObjectFileSize == 0xFFFFFFFF) {
1215 // tell driver to read until it receives a short packet
1216 mfr.length = 0xFFFFFFFF;
1217 } else {
1218 mfr.length = mSendObjectFileSize - initialData;
1219 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001220
Jerry Zhang54107562017-05-15 11:54:19 -07001221 mfr.command = 0;
1222 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001223
Jerry Zhang54107562017-05-15 11:54:19 -07001224 // transfer the file
1225 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1226 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1227 if ((ret < 0) && (errno == ECANCELED)) {
1228 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001229 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001230 }
caozhiyuan854cb172017-04-26 16:52:30 +08001231
1232 if (mSendObjectModifiedTime) {
1233 struct timespec newTime[2];
1234 newTime[0].tv_nsec = UTIME_NOW;
1235 newTime[1].tv_sec = mSendObjectModifiedTime;
1236 newTime[1].tv_nsec = 0;
1237 if (futimens(mfr.fd, newTime) < 0) {
1238 ALOGW("changing modified time failed, %s", strerror(errno));
1239 }
1240 }
1241
Jerry Zhang487be612016-10-24 12:10:41 -07001242 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001243 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001244
Mike Lockwood916076c2010-06-04 09:49:21 -04001245 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001246 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001247 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001248 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001249 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001250 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001251 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001252 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001253
1254done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001255 // reset so we don't attempt to send the data back
1256 mData.reset();
1257
Mike Lockwood4714b072010-07-12 08:49:01 -04001258 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -08001259 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001260 mSendObjectHandle = kInvalidObjectHandle;
1261 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001262 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001263
1264 auto end = std::chrono::steady_clock::now();
1265 std::chrono::duration<double> diff = end - start;
1266 uint64_t finalsize = sstat.st_size;
1267 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1268 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001269 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001270}
1271
1272MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001273 if (!hasStorage())
1274 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001275 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001276 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001277 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001278 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001279 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1280 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001281
1282 MtpString filePath;
1283 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001284 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001285 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001287 result = mDatabase->deleteFile(handle);
1288 // Don't delete the actual files unless the database deletion is allowed
1289 if (result == MTP_RESPONSE_OK) {
1290 deletePath((const char *)filePath);
1291 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001292 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001293
1294 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001295}
1296
1297MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001298 if (mRequest.getParameterCount() < 2)
1299 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001300 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001301 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001302 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001303 MtpDebug::getFormatCodeName(format));
1304 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001305 if (!property)
1306 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001307 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001308 delete property;
1309 return MTP_RESPONSE_OK;
1310}
1311
1312MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001313 if (mRequest.getParameterCount() < 1)
1314 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001315 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001316 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001317 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1318 if (!property)
1319 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1320 property->write(mData);
1321 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001322 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001323}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001324
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001325MtpResponseCode MtpServer::doSendPartialObject() {
1326 if (!hasStorage())
1327 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001328 if (mRequest.getParameterCount() < 4)
1329 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001330 MtpObjectHandle handle = mRequest.getParameter(1);
1331 uint64_t offset = mRequest.getParameter(2);
1332 uint64_t offset2 = mRequest.getParameter(3);
1333 offset = offset | (offset2 << 32);
1334 uint32_t length = mRequest.getParameter(4);
1335
1336 ObjectEdit* edit = getEditObject(handle);
1337 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001338 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001339 return MTP_RESPONSE_GENERAL_ERROR;
1340 }
1341
1342 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001343 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001344 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1345 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001346 return MTP_RESPONSE_GENERAL_ERROR;
1347 }
1348
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001349 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001350 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001351
Mike Lockwoodef441d92011-07-14 21:00:02 -04001352 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001353 int ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001354 if (ret < MTP_CONTAINER_HEADER_SIZE)
1355 return MTP_RESPONSE_GENERAL_ERROR;
1356 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1357
1358 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001359 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001360 offset += initialData;
1361 length -= initialData;
1362 }
1363
tao.pei07a9e542015-07-17 17:18:41 +08001364 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001365 if (ret < 0) {
1366 ALOGE("failed to write initial data");
1367 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001368 mtp_file_range mfr;
1369 mfr.fd = edit->mFD;
1370 mfr.offset = offset;
1371 mfr.length = length;
1372 mfr.command = 0;
1373 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001374
Jerry Zhang54107562017-05-15 11:54:19 -07001375 // transfer the file
1376 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1377 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1378 if ((ret < 0) && (errno == ECANCELED)) {
1379 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001380 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001381 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001382 if (ret < 0) {
1383 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001384 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001385 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1386 else
1387 return MTP_RESPONSE_GENERAL_ERROR;
1388 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001389
1390 // reset so we don't attempt to send this back
1391 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001392 mResponse.setParameter(1, length);
1393 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001394 if (end > edit->mSize) {
1395 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001396 }
1397 return MTP_RESPONSE_OK;
1398}
1399
1400MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001401 if (mRequest.getParameterCount() < 3)
1402 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001403 MtpObjectHandle handle = mRequest.getParameter(1);
1404 ObjectEdit* edit = getEditObject(handle);
1405 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001406 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001407 return MTP_RESPONSE_GENERAL_ERROR;
1408 }
1409
1410 uint64_t offset = mRequest.getParameter(2);
1411 uint64_t offset2 = mRequest.getParameter(3);
1412 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001413 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001414 return MTP_RESPONSE_GENERAL_ERROR;
1415 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001416 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001417 return MTP_RESPONSE_OK;
1418 }
1419}
1420
1421MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001422 if (mRequest.getParameterCount() < 1)
1423 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001424 MtpObjectHandle handle = mRequest.getParameter(1);
1425 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001426 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001427 return MTP_RESPONSE_GENERAL_ERROR;
1428 }
1429
1430 MtpString path;
1431 int64_t fileLength;
1432 MtpObjectFormat format;
1433 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1434 if (result != MTP_RESPONSE_OK)
1435 return result;
1436
1437 int fd = open((const char *)path, O_RDWR | O_EXCL);
1438 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001439 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001440 return MTP_RESPONSE_GENERAL_ERROR;
1441 }
1442
1443 addEditObject(handle, path, fileLength, format, fd);
1444 return MTP_RESPONSE_OK;
1445}
1446
1447MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001448 if (mRequest.getParameterCount() < 1)
1449 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001450 MtpObjectHandle handle = mRequest.getParameter(1);
1451 ObjectEdit* edit = getEditObject(handle);
1452 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001453 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001454 return MTP_RESPONSE_GENERAL_ERROR;
1455 }
1456
1457 commitEdit(edit);
1458 removeEditObject(handle);
1459 return MTP_RESPONSE_OK;
1460}
1461
Mike Lockwood7850ef92010-05-14 10:10:36 -04001462} // namespace android