blob: 7b3e96e03bad3ec86b19f8db4150a9f41857cb27 [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 Zhangdf69dd32017-05-03 17:17:49 -0700137 if (sHandle->configure(usePtp)) {
138 ALOGE("Failed to configure Mtp driver!");
139 return -1;
140 }
141 android::base::SetProperty("sys.usb.ffs.mtp.ready", "1");
142 return 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700143}
144
Mike Lockwooda8494402011-02-18 09:07:14 -0500145void MtpServer::addStorage(MtpStorage* storage) {
146 Mutex::Autolock autoLock(mMutex);
147
148 mStorages.push(storage);
149 sendStoreAdded(storage->getStorageID());
150}
151
152void MtpServer::removeStorage(MtpStorage* storage) {
153 Mutex::Autolock autoLock(mMutex);
154
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700155 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500156 if (mStorages[i] == storage) {
157 mStorages.removeAt(i);
158 sendStoreRemoved(storage->getStorageID());
159 break;
160 }
161 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400162}
163
164MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800165 if (id == 0)
166 return mStorages[0];
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700167 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800168 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169 if (storage->getStorageID() == id)
170 return storage;
171 }
Jerry Zhang487be612016-10-24 12:10:41 -0700172 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400173}
174
Mike Lockwooda8494402011-02-18 09:07:14 -0500175bool MtpServer::hasStorage(MtpStorageID id) {
176 if (id == 0 || id == 0xFFFFFFFF)
177 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700178 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500179}
180
Mike Lockwood16864ba2010-05-11 17:16:59 -0400181void MtpServer::run() {
Jerry Zhang487be612016-10-24 12:10:41 -0700182 if (!sHandle) {
183 ALOGE("MtpServer was never configured!");
184 return;
185 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400186
Jerry Zhang487be612016-10-24 12:10:41 -0700187 if (sHandle->start()) {
188 ALOGE("Failed to start usb driver!");
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -0800189 sHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700190 return;
191 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400192
193 while (1) {
Jerry Zhang487be612016-10-24 12:10:41 -0700194 int ret = mRequest.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400195 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700196 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400197 if (errno == ECANCELED) {
198 // return to top of loop and wait for next command
199 continue;
200 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400201 break;
202 }
203 MtpOperationCode operation = mRequest.getOperationCode();
204 MtpTransactionID transaction = mRequest.getTransactionID();
205
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400207 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400208 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400209 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
210 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
211 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400212 if (dataIn) {
Jerry Zhang487be612016-10-24 12:10:41 -0700213 int ret = mData.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400214 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000215 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400216 if (errno == ECANCELED) {
217 // return to top of loop and wait for next command
218 continue;
219 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400220 break;
221 }
Steve Block3856b092011-10-20 11:56:00 +0100222 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400223 } else {
224 mData.reset();
225 }
226
Mike Lockwood916076c2010-06-04 09:49:21 -0400227 if (handleRequest()) {
228 if (!dataIn && mData.hasData()) {
229 mData.setOperationCode(operation);
230 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("sending data:");
Jerry Zhang487be612016-10-24 12:10:41 -0700232 ret = mData.write(sHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400233 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000234 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400235 if (errno == ECANCELED) {
236 // return to top of loop and wait for next command
237 continue;
238 }
239 break;
240 }
241 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400242
Mike Lockwood916076c2010-06-04 09:49:21 -0400243 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang487be612016-10-24 12:10:41 -0700245 ret = mResponse.write(sHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800246 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400247 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000248 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800249 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400250 // return to top of loop and wait for next command
251 continue;
252 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400253 break;
254 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400255 } else {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400257 }
258 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400259
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700260 // commit any open edits
261 int count = mObjectEditList.size();
262 for (int i = 0; i < count; i++) {
263 ObjectEdit* edit = mObjectEditList[i];
264 commitEdit(edit);
265 delete edit;
266 }
267 mObjectEditList.clear();
268
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400269 if (mSessionOpen)
270 mDatabase->sessionEnded();
Jerry Zhang487be612016-10-24 12:10:41 -0700271
272 sHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400273}
274
Mike Lockwood873871f2010-07-12 18:54:16 -0400275void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500277 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400278}
279
280void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100281 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500282 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
283}
284
285void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500287 sendEvent(MTP_EVENT_STORE_ADDED, id);
288}
289
290void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100291 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500292 sendEvent(MTP_EVENT_STORE_REMOVED, id);
293}
294
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800295void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
296 ALOGV("sendDevicePropertyChanged %d\n", property);
297 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
298}
299
Mike Lockwooda8494402011-02-18 09:07:14 -0500300void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400301 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500302 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400303 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500304 mEvent.setParameter(1, param1);
Jerry Zhang487be612016-10-24 12:10:41 -0700305 if (mEvent.write(sHandle))
306 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400307 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400308}
309
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700310void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
311 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700312 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700313 mObjectEditList.add(edit);
314}
315
316MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
317 int count = mObjectEditList.size();
318 for (int i = 0; i < count; i++) {
319 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700320 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700321 }
Jerry Zhang487be612016-10-24 12:10:41 -0700322 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700323}
324
325void MtpServer::removeEditObject(MtpObjectHandle handle) {
326 int count = mObjectEditList.size();
327 for (int i = 0; i < count; i++) {
328 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700329 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700330 delete edit;
331 mObjectEditList.removeAt(i);
332 return;
333 }
334 }
Steve Block29357bc2012-01-06 19:20:56 +0000335 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700336}
337
338void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700339 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700340}
341
342
Mike Lockwood916076c2010-06-04 09:49:21 -0400343bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500344 Mutex::Autolock autoLock(mMutex);
345
Mike Lockwood16864ba2010-05-11 17:16:59 -0400346 MtpOperationCode operation = mRequest.getOperationCode();
347 MtpResponseCode response;
348
349 mResponse.reset();
350
351 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
352 // FIXME - need to delete mSendObjectHandle from the database
Steve Block29357bc2012-01-06 19:20:56 +0000353 ALOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400354 mSendObjectHandle = kInvalidObjectHandle;
355 }
356
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700357 int containertype = mRequest.getContainerType();
358 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
359 ALOGE("wrong container type %d", containertype);
360 return false;
361 }
362
363 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
364
Mike Lockwood16864ba2010-05-11 17:16:59 -0400365 switch (operation) {
366 case MTP_OPERATION_GET_DEVICE_INFO:
367 response = doGetDeviceInfo();
368 break;
369 case MTP_OPERATION_OPEN_SESSION:
370 response = doOpenSession();
371 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800372 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400373 case MTP_OPERATION_CLOSE_SESSION:
374 response = doCloseSession();
375 break;
376 case MTP_OPERATION_GET_STORAGE_IDS:
377 response = doGetStorageIDs();
378 break;
379 case MTP_OPERATION_GET_STORAGE_INFO:
380 response = doGetStorageInfo();
381 break;
382 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
383 response = doGetObjectPropsSupported();
384 break;
385 case MTP_OPERATION_GET_OBJECT_HANDLES:
386 response = doGetObjectHandles();
387 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400388 case MTP_OPERATION_GET_NUM_OBJECTS:
389 response = doGetNumObjects();
390 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400391 case MTP_OPERATION_GET_OBJECT_REFERENCES:
392 response = doGetObjectReferences();
393 break;
394 case MTP_OPERATION_SET_OBJECT_REFERENCES:
395 response = doSetObjectReferences();
396 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400397 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
398 response = doGetObjectPropValue();
399 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400400 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
401 response = doSetObjectPropValue();
402 break;
403 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
404 response = doGetDevicePropValue();
405 break;
406 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
407 response = doSetDevicePropValue();
408 break;
409 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
410 response = doResetDevicePropValue();
411 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400412 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
413 response = doGetObjectPropList();
414 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400415 case MTP_OPERATION_GET_OBJECT_INFO:
416 response = doGetObjectInfo();
417 break;
418 case MTP_OPERATION_GET_OBJECT:
419 response = doGetObject();
420 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700421 case MTP_OPERATION_GET_THUMB:
422 response = doGetThumb();
423 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500424 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700425 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
426 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500427 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400428 case MTP_OPERATION_SEND_OBJECT_INFO:
429 response = doSendObjectInfo();
430 break;
431 case MTP_OPERATION_SEND_OBJECT:
432 response = doSendObject();
433 break;
434 case MTP_OPERATION_DELETE_OBJECT:
435 response = doDeleteObject();
436 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700437 case MTP_OPERATION_COPY_OBJECT:
438 response = doCopyObject();
439 break;
440 case MTP_OPERATION_MOVE_OBJECT:
441 response = doMoveObject();
442 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400443 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400444 response = doGetObjectPropDesc();
445 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400446 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
447 response = doGetDevicePropDesc();
448 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700449 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
450 response = doSendPartialObject();
451 break;
452 case MTP_OPERATION_TRUNCATE_OBJECT:
453 response = doTruncateObject();
454 break;
455 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
456 response = doBeginEditObject();
457 break;
458 case MTP_OPERATION_END_EDIT_OBJECT:
459 response = doEndEditObject();
460 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400461 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700462 ALOGE("got unsupported command %s (%x)",
463 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400464 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
465 break;
466 }
467
Mike Lockwood916076c2010-06-04 09:49:21 -0400468 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
469 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400470 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400471 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400472}
473
474MtpResponseCode MtpServer::doGetDeviceInfo() {
475 MtpStringBuffer string;
476
Mike Lockwood782aef12010-08-10 07:37:50 -0400477 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
478 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
479 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
480
Mike Lockwood16864ba2010-05-11 17:16:59 -0400481 // fill in device info
482 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400483 if (mPtp) {
484 mData.putUInt32(0);
485 } else {
486 // MTP Vendor Extension ID
487 mData.putUInt32(6);
488 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400489 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400490 if (mPtp) {
491 // no extensions
492 string.set("");
493 } else {
494 // MTP extensions
495 string.set("microsoft.com: 1.0; android.com: 1.0;");
496 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400497 mData.putString(string); // MTP Extensions
498 mData.putUInt16(0); //Functional Mode
499 mData.putAUInt16(kSupportedOperationCodes,
500 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400501 mData.putAUInt16(kSupportedEventCodes,
502 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400503 mData.putAUInt16(deviceProperties); // Device Properties Supported
504 mData.putAUInt16(captureFormats); // Capture Formats
505 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500506
Alex Klyubin792298f2016-12-21 11:20:22 -0800507 mData.putString(mDeviceInfoManufacturer); // Manufacturer
508 mData.putString(mDeviceInfoModel); // Model
509 mData.putString(mDeviceInfoDeviceVersion); // Device Version
510 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400511
Mike Lockwood782aef12010-08-10 07:37:50 -0400512 delete playbackFormats;
513 delete captureFormats;
514 delete deviceProperties;
515
Mike Lockwood16864ba2010-05-11 17:16:59 -0400516 return MTP_RESPONSE_OK;
517}
518
519MtpResponseCode MtpServer::doOpenSession() {
520 if (mSessionOpen) {
521 mResponse.setParameter(1, mSessionID);
522 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
523 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800524 if (mRequest.getParameterCount() < 1)
525 return MTP_RESPONSE_INVALID_PARAMETER;
526
Mike Lockwood16864ba2010-05-11 17:16:59 -0400527 mSessionID = mRequest.getParameter(1);
528 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400529
530 mDatabase->sessionStarted();
531
Mike Lockwood16864ba2010-05-11 17:16:59 -0400532 return MTP_RESPONSE_OK;
533}
534
535MtpResponseCode MtpServer::doCloseSession() {
536 if (!mSessionOpen)
537 return MTP_RESPONSE_SESSION_NOT_OPEN;
538 mSessionID = 0;
539 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400540 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400541 return MTP_RESPONSE_OK;
542}
543
544MtpResponseCode MtpServer::doGetStorageIDs() {
545 if (!mSessionOpen)
546 return MTP_RESPONSE_SESSION_NOT_OPEN;
547
548 int count = mStorages.size();
549 mData.putUInt32(count);
550 for (int i = 0; i < count; i++)
551 mData.putUInt32(mStorages[i]->getStorageID());
552
553 return MTP_RESPONSE_OK;
554}
555
556MtpResponseCode MtpServer::doGetStorageInfo() {
557 MtpStringBuffer string;
558
559 if (!mSessionOpen)
560 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800561 if (mRequest.getParameterCount() < 1)
562 return MTP_RESPONSE_INVALID_PARAMETER;
563
Mike Lockwood16864ba2010-05-11 17:16:59 -0400564 MtpStorageID id = mRequest.getParameter(1);
565 MtpStorage* storage = getStorage(id);
566 if (!storage)
567 return MTP_RESPONSE_INVALID_STORAGE_ID;
568
569 mData.putUInt16(storage->getType());
570 mData.putUInt16(storage->getFileSystemType());
571 mData.putUInt16(storage->getAccessCapability());
572 mData.putUInt64(storage->getMaxCapacity());
573 mData.putUInt64(storage->getFreeSpace());
574 mData.putUInt32(1024*1024*1024); // Free Space in Objects
575 string.set(storage->getDescription());
576 mData.putString(string);
577 mData.putEmptyString(); // Volume Identifier
578
579 return MTP_RESPONSE_OK;
580}
581
582MtpResponseCode MtpServer::doGetObjectPropsSupported() {
583 if (!mSessionOpen)
584 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800585 if (mRequest.getParameterCount() < 1)
586 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400587 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800588 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400589 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400590 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400591 return MTP_RESPONSE_OK;
592}
593
594MtpResponseCode MtpServer::doGetObjectHandles() {
595 if (!mSessionOpen)
596 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800597 if (mRequest.getParameterCount() < 3)
598 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400599 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400600 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400601 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400602 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500603
604 if (!hasStorage(storageID))
605 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400606
607 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
608 mData.putAUInt32(handles);
609 delete handles;
610 return MTP_RESPONSE_OK;
611}
612
Mike Lockwood343af4e2010-08-02 10:52:20 -0400613MtpResponseCode MtpServer::doGetNumObjects() {
614 if (!mSessionOpen)
615 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800616 if (mRequest.getParameterCount() < 3)
617 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400618 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
619 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
620 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400621 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500622 if (!hasStorage(storageID))
623 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400624
625 int count = mDatabase->getNumObjects(storageID, format, parent);
626 if (count >= 0) {
627 mResponse.setParameter(1, count);
628 return MTP_RESPONSE_OK;
629 } else {
630 mResponse.setParameter(1, 0);
631 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
632 }
633}
634
Mike Lockwood438344f2010-08-03 15:30:09 -0400635MtpResponseCode MtpServer::doGetObjectReferences() {
636 if (!mSessionOpen)
637 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500638 if (!hasStorage())
639 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800640 if (mRequest.getParameterCount() < 1)
641 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500642 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400643
644 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400645 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400646 if (handles) {
647 mData.putAUInt32(handles);
648 delete handles;
649 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400650 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400651 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400652 return MTP_RESPONSE_OK;
653}
654
655MtpResponseCode MtpServer::doSetObjectReferences() {
656 if (!mSessionOpen)
657 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500658 if (!hasStorage())
659 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800660 if (mRequest.getParameterCount() < 1)
661 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400662 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500663
Mike Lockwood438344f2010-08-03 15:30:09 -0400664 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800665 if (!references)
666 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400667 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
668 delete references;
669 return result;
670}
671
Mike Lockwood16864ba2010-05-11 17:16:59 -0400672MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500673 if (!hasStorage())
674 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800675 if (mRequest.getParameterCount() < 2)
676 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400677 MtpObjectHandle handle = mRequest.getParameter(1);
678 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400680 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400681
Mike Lockwood8277cec2010-08-10 15:20:35 -0400682 return mDatabase->getObjectPropertyValue(handle, property, mData);
683}
684
685MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500686 if (!hasStorage())
687 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800688 if (mRequest.getParameterCount() < 2)
689 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400690 MtpObjectHandle handle = mRequest.getParameter(1);
691 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400693 MtpDebug::getObjectPropCodeName(property));
694
695 return mDatabase->setObjectPropertyValue(handle, property, mData);
696}
697
698MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800699 if (mRequest.getParameterCount() < 1)
700 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400701 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400703 MtpDebug::getDevicePropCodeName(property));
704
705 return mDatabase->getDevicePropertyValue(property, mData);
706}
707
708MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800709 if (mRequest.getParameterCount() < 1)
710 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400711 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100712 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400713 MtpDebug::getDevicePropCodeName(property));
714
715 return mDatabase->setDevicePropertyValue(property, mData);
716}
717
718MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800719 if (mRequest.getParameterCount() < 1)
720 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400721 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400723 MtpDebug::getDevicePropCodeName(property));
724
725 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400726}
727
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400728MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500729 if (!hasStorage())
730 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800731 if (mRequest.getParameterCount() < 5)
732 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400733
734 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500735 // use uint32_t so we can support 0xFFFFFFFF
736 uint32_t format = mRequest.getParameter(2);
737 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400738 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500739 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100740 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400741 handle, MtpDebug::getFormatCodeName(format),
742 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
743
744 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
745}
746
Mike Lockwood16864ba2010-05-11 17:16:59 -0400747MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500748 if (!hasStorage())
749 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800750 if (mRequest.getParameterCount() < 1)
751 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400752 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700753 MtpObjectInfo info(handle);
754 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
755 if (result == MTP_RESPONSE_OK) {
756 char date[20];
757
758 mData.putUInt32(info.mStorageID);
759 mData.putUInt16(info.mFormat);
760 mData.putUInt16(info.mProtectionStatus);
761
762 // if object is being edited the database size may be out of date
763 uint32_t size = info.mCompressedSize;
764 ObjectEdit* edit = getEditObject(handle);
765 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700766 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700767 mData.putUInt32(size);
768
769 mData.putUInt16(info.mThumbFormat);
770 mData.putUInt32(info.mThumbCompressedSize);
771 mData.putUInt32(info.mThumbPixWidth);
772 mData.putUInt32(info.mThumbPixHeight);
773 mData.putUInt32(info.mImagePixWidth);
774 mData.putUInt32(info.mImagePixHeight);
775 mData.putUInt32(info.mImagePixDepth);
776 mData.putUInt32(info.mParent);
777 mData.putUInt16(info.mAssociationType);
778 mData.putUInt32(info.mAssociationDesc);
779 mData.putUInt32(info.mSequenceNumber);
780 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700781 formatDateTime(info.mDateCreated, date, sizeof(date));
782 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700783 formatDateTime(info.mDateModified, date, sizeof(date));
784 mData.putString(date); // date modified
785 mData.putEmptyString(); // keywords
786 }
787 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400788}
789
790MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500791 if (!hasStorage())
792 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800793 if (mRequest.getParameterCount() < 1)
794 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400795 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400796 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400797 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800798 MtpObjectFormat format;
799 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400800 if (result != MTP_RESPONSE_OK)
801 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400802
Jerry Zhang487be612016-10-24 12:10:41 -0700803 auto start = std::chrono::steady_clock::now();
804
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400805 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400806 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400807 mfr.fd = open(filePath, O_RDONLY);
808 if (mfr.fd < 0) {
809 return MTP_RESPONSE_GENERAL_ERROR;
810 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400811 mfr.offset = 0;
812 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400813 mfr.command = mRequest.getOperationCode();
814 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400815
816 // then transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700817 int ret = sHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800818 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700819 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800820 if (errno == ECANCELED) {
821 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
822 } else {
823 result = MTP_RESPONSE_GENERAL_ERROR;
824 }
825 } else {
826 result = MTP_RESPONSE_OK;
827 }
828
Jerry Zhang487be612016-10-24 12:10:41 -0700829 auto end = std::chrono::steady_clock::now();
830 std::chrono::duration<double> diff = end - start;
831 struct stat sstat;
832 fstat(mfr.fd, &sstat);
833 uint64_t finalsize = sstat.st_size;
834 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
835 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400836 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800837 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400838}
839
Mike Lockwood64000782011-04-24 18:40:17 -0700840MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800841 if (mRequest.getParameterCount() < 1)
842 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700843 MtpObjectHandle handle = mRequest.getParameter(1);
844 size_t thumbSize;
845 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
846 if (thumb) {
847 // send data
848 mData.setOperationCode(mRequest.getOperationCode());
849 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang487be612016-10-24 12:10:41 -0700850 mData.writeData(sHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700851 free(thumb);
852 return MTP_RESPONSE_OK;
853 } else {
854 return MTP_RESPONSE_GENERAL_ERROR;
855 }
856}
857
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700858MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500859 if (!hasStorage())
860 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500861 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700862 uint64_t offset;
863 uint32_t length;
864 offset = mRequest.getParameter(2);
865 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800866 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
867 if (mRequest.getParameterCount() < 4)
868 return MTP_RESPONSE_INVALID_PARAMETER;
869
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700870 // android extension with 64 bit offset
871 uint64_t offset2 = mRequest.getParameter(3);
872 offset = offset | (offset2 << 32);
873 length = mRequest.getParameter(4);
874 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800875 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
876 if (mRequest.getParameterCount() < 3)
877 return MTP_RESPONSE_INVALID_PARAMETER;
878
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700879 // standard GetPartialObject
880 length = mRequest.getParameter(3);
881 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500882 MtpString pathBuf;
883 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800884 MtpObjectFormat format;
885 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500886 if (result != MTP_RESPONSE_OK)
887 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700888 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500889 length = fileLength - offset;
890
891 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700892 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500893 mtp_file_range mfr;
894 mfr.fd = open(filePath, O_RDONLY);
895 if (mfr.fd < 0) {
896 return MTP_RESPONSE_GENERAL_ERROR;
897 }
898 mfr.offset = offset;
899 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400900 mfr.command = mRequest.getOperationCode();
901 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500902 mResponse.setParameter(1, length);
903
Mike Lockwoodef441d92011-07-14 21:00:02 -0400904 // transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700905 int ret = sHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100906 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800907 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500908 if (ret < 0) {
909 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800910 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500911 else
tao.pei07a9e542015-07-17 17:18:41 +0800912 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500913 }
tao.pei07a9e542015-07-17 17:18:41 +0800914 close(mfr.fd);
915 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500916}
917
Mike Lockwood16864ba2010-05-11 17:16:59 -0400918MtpResponseCode MtpServer::doSendObjectInfo() {
919 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800920 uint16_t temp16;
921 uint32_t temp32;
922
923 if (mRequest.getParameterCount() < 2)
924 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400925 MtpStorageID storageID = mRequest.getParameter(1);
926 MtpStorage* storage = getStorage(storageID);
927 MtpObjectHandle parent = mRequest.getParameter(2);
928 if (!storage)
929 return MTP_RESPONSE_INVALID_STORAGE_ID;
930
931 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400932 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400933 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400934 parent = 0;
935 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800936 int64_t length;
937 MtpObjectFormat format;
938 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400939 if (result != MTP_RESPONSE_OK)
940 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800941 if (format != MTP_FORMAT_ASSOCIATION)
942 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400943 }
944
945 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800946 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
947 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
948 MtpObjectFormat format = temp16;
949 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
950 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
951 mSendObjectFileSize = temp32;
952 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
953 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
954 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
955 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
956 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
957 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
958 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
959 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
960 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800961 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800962 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400963 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800964 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700965 if (name.getCharCount() == 0) {
966 ALOGE("empty name");
967 return MTP_RESPONSE_INVALID_PARAMETER;
968 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800969 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
970 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400971 // keywords follow
972
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400974 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400975 if (!parseDateTime(modified, modifiedTime))
976 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400977
978 if (path[path.size() - 1] != '/')
979 path += "/";
980 path += (const char *)name;
981
Mike Lockwood20c3be02010-12-12 12:17:43 -0800982 // check space first
983 if (mSendObjectFileSize > storage->getFreeSpace())
984 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400985 uint64_t maxFileSize = storage->getMaxFileSize();
986 // check storage max file size
987 if (maxFileSize != 0) {
988 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
989 // is >= 0xFFFFFFFF
990 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
991 return MTP_RESPONSE_OBJECT_TOO_LARGE;
992 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800993
Steve Blockb8a80522011-12-20 16:23:08 +0000994 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400995 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
996 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400997 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400998 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400999 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001000
1001 if (format == MTP_FORMAT_ASSOCIATION) {
Jerry Zhange242f122017-10-16 14:54:08 -07001002 int ret = makeFolder((const char *)path);
1003 if (ret)
Mike Lockwood16864ba2010-05-11 17:16:59 -04001004 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodaa952402011-01-18 11:06:19 -08001005
1006 // SendObject does not get sent for directories, so call endSendObject here instead
1007 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001008 } else {
1009 mSendObjectFilePath = path;
1010 // save the handle for the SendObject call, which should follow
1011 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -04001012 mSendObjectFormat = format;
caozhiyuan854cb172017-04-26 16:52:30 +08001013 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001014 }
1015
1016 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001017 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001018 mResponse.setParameter(3, handle);
1019
1020 return MTP_RESPONSE_OK;
1021}
1022
Jerry Zhang708b3e02017-09-26 17:53:39 -07001023MtpResponseCode MtpServer::doMoveObject() {
1024 if (!hasStorage())
1025 return MTP_RESPONSE_GENERAL_ERROR;
1026 if (mRequest.getParameterCount() < 3)
1027 return MTP_RESPONSE_INVALID_PARAMETER;
1028 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1029 MtpStorageID storageID = mRequest.getParameter(2);
1030 MtpStorage* storage = getStorage(storageID);
1031 MtpObjectHandle parent = mRequest.getParameter(3);
1032 if (!storage)
1033 return MTP_RESPONSE_INVALID_STORAGE_ID;
1034 MtpString path;
1035 MtpResponseCode result;
1036
1037 MtpString fromPath;
1038 int64_t fileLength;
1039 MtpObjectFormat format;
1040 MtpObjectInfo info(objectHandle);
1041 result = mDatabase->getObjectInfo(objectHandle, info);
1042 if (result != MTP_RESPONSE_OK)
1043 return result;
1044 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1045 if (result != MTP_RESPONSE_OK)
1046 return result;
1047
1048 // special case the root
1049 if (parent == 0) {
1050 path = storage->getPath();
1051 } else {
1052 int64_t parentLength;
1053 MtpObjectFormat parentFormat;
1054 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1055 if (result != MTP_RESPONSE_OK)
1056 return result;
1057 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1058 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1059 }
1060
1061 if (path[path.size() - 1] != '/')
1062 path += "/";
1063 path += info.mName;
1064
Jerry Zhang708b3e02017-09-26 17:53:39 -07001065 if (info.mStorageID == storageID) {
1066 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
1067 if (rename(fromPath, path)) {
Jerry Zhange242f122017-10-16 14:54:08 -07001068 PLOG(ERROR) << "rename() failed from " << fromPath << " to " << path;
Jerry Zhang708b3e02017-09-26 17:53:39 -07001069 result = MTP_RESPONSE_GENERAL_ERROR;
1070 }
1071 } else {
1072 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001073 if (format == MTP_FORMAT_ASSOCIATION) {
1074 int ret = makeFolder((const char *)path);
1075 ret += copyRecursive(fromPath, path);
1076 if (ret) {
1077 result = MTP_RESPONSE_GENERAL_ERROR;
1078 } else {
1079 deletePath(fromPath);
1080 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001081 } else {
Jerry Zhange242f122017-10-16 14:54:08 -07001082 if (copyFile(fromPath, path)) {
1083 result = MTP_RESPONSE_GENERAL_ERROR;
1084 } else {
1085 deletePath(fromPath);
1086 }
Jerry Zhang708b3e02017-09-26 17:53:39 -07001087 }
1088 }
1089
1090 // If the move failed, undo the database change
Jerry Zhangee8946f2017-10-31 12:26:07 -07001091 if (result == MTP_RESPONSE_OK)
1092 result = mDatabase->moveObject(objectHandle, parent, storageID, path);
Jerry Zhang708b3e02017-09-26 17:53:39 -07001093
1094 return result;
1095}
1096
1097MtpResponseCode MtpServer::doCopyObject() {
1098 if (!hasStorage())
1099 return MTP_RESPONSE_GENERAL_ERROR;
1100 MtpResponseCode result = MTP_RESPONSE_OK;
1101 if (mRequest.getParameterCount() < 3)
1102 return MTP_RESPONSE_INVALID_PARAMETER;
1103 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1104 MtpStorageID storageID = mRequest.getParameter(2);
1105 MtpStorage* storage = getStorage(storageID);
1106 MtpObjectHandle parent = mRequest.getParameter(3);
1107 if (!storage)
1108 return MTP_RESPONSE_INVALID_STORAGE_ID;
1109 MtpString path;
1110
1111 MtpString fromPath;
1112 int64_t fileLength;
1113 MtpObjectFormat format;
1114 MtpObjectInfo info(objectHandle);
1115 result = mDatabase->getObjectInfo(objectHandle, info);
1116 if (result != MTP_RESPONSE_OK)
1117 return result;
1118 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1119 if (result != MTP_RESPONSE_OK)
1120 return result;
1121
1122 // special case the root
1123 if (parent == 0) {
1124 path = storage->getPath();
1125 } else {
1126 int64_t parentLength;
1127 MtpObjectFormat parentFormat;
1128 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1129 if (result != MTP_RESPONSE_OK)
1130 return result;
1131 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1132 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1133 }
1134
1135 // check space first
1136 if ((uint64_t) fileLength > storage->getFreeSpace())
1137 return MTP_RESPONSE_STORAGE_FULL;
1138
1139 if (path[path.size() - 1] != '/')
1140 path += "/";
1141 path += info.mName;
1142
1143 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
1144 format, parent, storageID, fileLength, info.mDateModified);
1145 if (handle == kInvalidObjectHandle) {
1146 return MTP_RESPONSE_GENERAL_ERROR;
1147 }
1148
1149 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
Jerry Zhange242f122017-10-16 14:54:08 -07001150 if (format == MTP_FORMAT_ASSOCIATION) {
1151 int ret = makeFolder((const char *)path);
1152 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);
1162 mResponse.setParameter(1, handle);
1163 return result;
1164}
1165
Mike Lockwood16864ba2010-05-11 17:16:59 -04001166MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001167 if (!hasStorage())
1168 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001169 MtpResponseCode result = MTP_RESPONSE_OK;
1170 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001171 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001172 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001173 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001174
Jerry Zhang487be612016-10-24 12:10:41 -07001175 auto start = std::chrono::steady_clock::now();
1176
Mike Lockwood16864ba2010-05-11 17:16:59 -04001177 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001178 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001179 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1180 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001181 }
1182
Mike Lockwoodef441d92011-07-14 21:00:02 -04001183 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001184 ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001185 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1186 result = MTP_RESPONSE_GENERAL_ERROR;
1187 goto done;
1188 }
1189 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001190
1191 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001192 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001193 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001194 result = MTP_RESPONSE_GENERAL_ERROR;
1195 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001196 }
Jerry Zhange242f122017-10-16 14:54:08 -07001197 fchown(mfr.fd, getuid(), FILE_GROUP);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001198 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001199 mask = umask(0);
Jerry Zhange242f122017-10-16 14:54:08 -07001200 fchmod(mfr.fd, FILE_PERM);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001201 umask(mask);
1202
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001203 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001204 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001205 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001206
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001207 if (ret < 0) {
1208 ALOGE("failed to write initial data");
1209 result = MTP_RESPONSE_GENERAL_ERROR;
1210 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001211 mfr.offset = initialData;
1212 if (mSendObjectFileSize == 0xFFFFFFFF) {
1213 // tell driver to read until it receives a short packet
1214 mfr.length = 0xFFFFFFFF;
1215 } else {
1216 mfr.length = mSendObjectFileSize - initialData;
1217 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001218
Jerry Zhang54107562017-05-15 11:54:19 -07001219 mfr.command = 0;
1220 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001221
Jerry Zhang54107562017-05-15 11:54:19 -07001222 // transfer the file
1223 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1224 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1225 if ((ret < 0) && (errno == ECANCELED)) {
1226 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001227 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001228 }
caozhiyuan854cb172017-04-26 16:52:30 +08001229
1230 if (mSendObjectModifiedTime) {
1231 struct timespec newTime[2];
1232 newTime[0].tv_nsec = UTIME_NOW;
1233 newTime[1].tv_sec = mSendObjectModifiedTime;
1234 newTime[1].tv_nsec = 0;
1235 if (futimens(mfr.fd, newTime) < 0) {
1236 ALOGW("changing modified time failed, %s", strerror(errno));
1237 }
1238 }
1239
Jerry Zhang487be612016-10-24 12:10:41 -07001240 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001241 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001242
Mike Lockwood916076c2010-06-04 09:49:21 -04001243 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001244 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001245 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001246 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001247 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001248 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001249 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001250 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001251
1252done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001253 // reset so we don't attempt to send the data back
1254 mData.reset();
1255
Mike Lockwood4714b072010-07-12 08:49:01 -04001256 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -08001257 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001258 mSendObjectHandle = kInvalidObjectHandle;
1259 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001260 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001261
1262 auto end = std::chrono::steady_clock::now();
1263 std::chrono::duration<double> diff = end - start;
1264 uint64_t finalsize = sstat.st_size;
1265 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1266 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001267 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001268}
1269
1270MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001271 if (!hasStorage())
1272 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001273 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001274 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001275 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001276 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001277 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1278 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001279
1280 MtpString filePath;
1281 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001282 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001283 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001284 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001285 result = mDatabase->deleteFile(handle);
1286 // Don't delete the actual files unless the database deletion is allowed
1287 if (result == MTP_RESPONSE_OK) {
1288 deletePath((const char *)filePath);
1289 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001290 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001291
1292 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001293}
1294
1295MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001296 if (mRequest.getParameterCount() < 2)
1297 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001298 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001299 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001300 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001301 MtpDebug::getFormatCodeName(format));
1302 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001303 if (!property)
1304 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001305 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001306 delete property;
1307 return MTP_RESPONSE_OK;
1308}
1309
1310MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001311 if (mRequest.getParameterCount() < 1)
1312 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001313 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001314 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001315 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1316 if (!property)
1317 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1318 property->write(mData);
1319 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001320 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001321}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001322
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001323MtpResponseCode MtpServer::doSendPartialObject() {
1324 if (!hasStorage())
1325 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001326 if (mRequest.getParameterCount() < 4)
1327 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001328 MtpObjectHandle handle = mRequest.getParameter(1);
1329 uint64_t offset = mRequest.getParameter(2);
1330 uint64_t offset2 = mRequest.getParameter(3);
1331 offset = offset | (offset2 << 32);
1332 uint32_t length = mRequest.getParameter(4);
1333
1334 ObjectEdit* edit = getEditObject(handle);
1335 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001336 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001337 return MTP_RESPONSE_GENERAL_ERROR;
1338 }
1339
1340 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001341 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001342 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1343 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001344 return MTP_RESPONSE_GENERAL_ERROR;
1345 }
1346
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001347 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001348 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001349
Mike Lockwoodef441d92011-07-14 21:00:02 -04001350 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001351 int ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001352 if (ret < MTP_CONTAINER_HEADER_SIZE)
1353 return MTP_RESPONSE_GENERAL_ERROR;
1354 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1355
1356 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001357 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001358 offset += initialData;
1359 length -= initialData;
1360 }
1361
tao.pei07a9e542015-07-17 17:18:41 +08001362 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001363 if (ret < 0) {
1364 ALOGE("failed to write initial data");
1365 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001366 mtp_file_range mfr;
1367 mfr.fd = edit->mFD;
1368 mfr.offset = offset;
1369 mfr.length = length;
1370 mfr.command = 0;
1371 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001372
Jerry Zhang54107562017-05-15 11:54:19 -07001373 // transfer the file
1374 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1375 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1376 if ((ret < 0) && (errno == ECANCELED)) {
1377 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001378 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001379 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001380 if (ret < 0) {
1381 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001382 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001383 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1384 else
1385 return MTP_RESPONSE_GENERAL_ERROR;
1386 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001387
1388 // reset so we don't attempt to send this back
1389 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001390 mResponse.setParameter(1, length);
1391 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001392 if (end > edit->mSize) {
1393 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001394 }
1395 return MTP_RESPONSE_OK;
1396}
1397
1398MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001399 if (mRequest.getParameterCount() < 3)
1400 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001401 MtpObjectHandle handle = mRequest.getParameter(1);
1402 ObjectEdit* edit = getEditObject(handle);
1403 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001404 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001405 return MTP_RESPONSE_GENERAL_ERROR;
1406 }
1407
1408 uint64_t offset = mRequest.getParameter(2);
1409 uint64_t offset2 = mRequest.getParameter(3);
1410 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001411 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001412 return MTP_RESPONSE_GENERAL_ERROR;
1413 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001414 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001415 return MTP_RESPONSE_OK;
1416 }
1417}
1418
1419MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001420 if (mRequest.getParameterCount() < 1)
1421 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001422 MtpObjectHandle handle = mRequest.getParameter(1);
1423 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001424 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001425 return MTP_RESPONSE_GENERAL_ERROR;
1426 }
1427
1428 MtpString path;
1429 int64_t fileLength;
1430 MtpObjectFormat format;
1431 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1432 if (result != MTP_RESPONSE_OK)
1433 return result;
1434
1435 int fd = open((const char *)path, O_RDWR | O_EXCL);
1436 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001437 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001438 return MTP_RESPONSE_GENERAL_ERROR;
1439 }
1440
1441 addEditObject(handle, path, fileLength, format, fd);
1442 return MTP_RESPONSE_OK;
1443}
1444
1445MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001446 if (mRequest.getParameterCount() < 1)
1447 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001448 MtpObjectHandle handle = mRequest.getParameter(1);
1449 ObjectEdit* edit = getEditObject(handle);
1450 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001451 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001452 return MTP_RESPONSE_GENERAL_ERROR;
1453 }
1454
1455 commitEdit(edit);
1456 removeEditObject(handle);
1457 return MTP_RESPONSE_OK;
1458}
1459
Mike Lockwood7850ef92010-05-14 10:10:36 -04001460} // namespace android