blob: 236f3a916905daedf9a9b5a7953818fae2e62303 [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 Zhang487be612016-10-24 12:10:41 -070017#include <android-base/properties.h>
18#include <chrono>
Jerry Zhang487be612016-10-24 12:10:41 -070019#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040026#include <sys/stat.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040027#include <sys/stat.h>
caozhiyuan854cb172017-04-26 16:52:30 +080028#include <sys/time.h>
Mike Lockwoodc42aa122010-06-14 17:58:08 -070029
Mike Lockwooda881b442010-09-23 22:32:05 -040030#define LOG_TAG "MtpServer"
31
Mike Lockwood16864ba2010-05-11 17:16:59 -040032#include "MtpDebug.h"
Mike Lockwood7f53a192010-07-09 10:45:22 -040033#include "MtpDatabase.h"
Jerry Zhangdf69dd32017-05-03 17:17:49 -070034#include "MtpDevHandle.h"
35#include "MtpFfsCompatHandle.h"
36#include "MtpFfsHandle.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070037#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040038#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040039#include "MtpServer.h"
40#include "MtpStorage.h"
41#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040042
Mike Lockwood7850ef92010-05-14 10:10:36 -040043namespace android {
44
Mike Lockwood16864ba2010-05-11 17:16:59 -040045static const MtpOperationCode kSupportedOperationCodes[] = {
46 MTP_OPERATION_GET_DEVICE_INFO,
47 MTP_OPERATION_OPEN_SESSION,
48 MTP_OPERATION_CLOSE_SESSION,
49 MTP_OPERATION_GET_STORAGE_IDS,
50 MTP_OPERATION_GET_STORAGE_INFO,
51 MTP_OPERATION_GET_NUM_OBJECTS,
52 MTP_OPERATION_GET_OBJECT_HANDLES,
53 MTP_OPERATION_GET_OBJECT_INFO,
54 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070055 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040056 MTP_OPERATION_DELETE_OBJECT,
57 MTP_OPERATION_SEND_OBJECT_INFO,
58 MTP_OPERATION_SEND_OBJECT,
59// MTP_OPERATION_INITIATE_CAPTURE,
60// MTP_OPERATION_FORMAT_STORE,
Jerry Zhang6dafecc2017-02-23 16:39:30 -080061 MTP_OPERATION_RESET_DEVICE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040062// MTP_OPERATION_SELF_TEST,
63// MTP_OPERATION_SET_OBJECT_PROTECTION,
64// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040065 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040066 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
67 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
68 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040069// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
Jerry Zhang708b3e02017-09-26 17:53:39 -070070 MTP_OPERATION_MOVE_OBJECT,
71 MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050072 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040073// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
74 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040075 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040076 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
77 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040078 MTP_OPERATION_GET_OBJECT_PROP_LIST,
79// MTP_OPERATION_SET_OBJECT_PROP_LIST,
80// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
81// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040082 MTP_OPERATION_GET_OBJECT_REFERENCES,
83 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040084// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070085 // Android extension for direct file IO
86 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
87 MTP_OPERATION_SEND_PARTIAL_OBJECT,
88 MTP_OPERATION_TRUNCATE_OBJECT,
89 MTP_OPERATION_BEGIN_EDIT_OBJECT,
90 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040091};
92
Mike Lockwood873871f2010-07-12 18:54:16 -040093static const MtpEventCode kSupportedEventCodes[] = {
94 MTP_EVENT_OBJECT_ADDED,
95 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050096 MTP_EVENT_STORE_ADDED,
97 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -080098 MTP_EVENT_DEVICE_PROP_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -040099};
100
Jerry Zhang487be612016-10-24 12:10:41 -0700101MtpServer::MtpServer(MtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800102 int fileGroup, int filePerm, int directoryPerm,
103 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),
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400109 mFileGroup(fileGroup),
110 mFilePermission(filePerm),
111 mDirectoryPermission(directoryPerm),
Alex Klyubin792298f2016-12-21 11:20:22 -0800112 mDeviceInfoManufacturer(deviceInfoManufacturer),
113 mDeviceInfoModel(deviceInfoModel),
114 mDeviceInfoDeviceVersion(deviceInfoDeviceVersion),
115 mDeviceInfoSerialNumber(deviceInfoSerialNumber),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400116 mSessionID(0),
117 mSessionOpen(false),
118 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400119 mSendObjectFormat(0),
caozhiyuan854cb172017-04-26 16:52:30 +0800120 mSendObjectFileSize(0),
121 mSendObjectModifiedTime(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400122{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400123}
124
125MtpServer::~MtpServer() {
126}
127
Jerry Zhang487be612016-10-24 12:10:41 -0700128IMtpHandle* MtpServer::sHandle = nullptr;
129
130int MtpServer::configure(bool usePtp) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700131 bool ffs_ok = access(FFS_MTP_EP0, W_OK) == 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700132 if (sHandle == nullptr) {
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700133 if (ffs_ok) {
134 bool aio_compat = android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false);
135 sHandle = aio_compat ? new MtpFfsCompatHandle() : new MtpFfsHandle();
136 } else {
137 sHandle = new MtpDevHandle();
138 }
Jerry Zhang487be612016-10-24 12:10:41 -0700139 }
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700140 if (sHandle->configure(usePtp)) {
141 ALOGE("Failed to configure Mtp driver!");
142 return -1;
143 }
144 android::base::SetProperty("sys.usb.ffs.mtp.ready", "1");
145 return 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700146}
147
Mike Lockwooda8494402011-02-18 09:07:14 -0500148void MtpServer::addStorage(MtpStorage* storage) {
149 Mutex::Autolock autoLock(mMutex);
150
151 mStorages.push(storage);
152 sendStoreAdded(storage->getStorageID());
153}
154
155void MtpServer::removeStorage(MtpStorage* storage) {
156 Mutex::Autolock autoLock(mMutex);
157
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700158 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500159 if (mStorages[i] == storage) {
160 mStorages.removeAt(i);
161 sendStoreRemoved(storage->getStorageID());
162 break;
163 }
164 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165}
166
167MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800168 if (id == 0)
169 return mStorages[0];
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700170 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800171 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172 if (storage->getStorageID() == id)
173 return storage;
174 }
Jerry Zhang487be612016-10-24 12:10:41 -0700175 return nullptr;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400176}
177
Mike Lockwooda8494402011-02-18 09:07:14 -0500178bool MtpServer::hasStorage(MtpStorageID id) {
179 if (id == 0 || id == 0xFFFFFFFF)
180 return mStorages.size() > 0;
Jerry Zhang487be612016-10-24 12:10:41 -0700181 return (getStorage(id) != nullptr);
Mike Lockwooda8494402011-02-18 09:07:14 -0500182}
183
Mike Lockwood16864ba2010-05-11 17:16:59 -0400184void MtpServer::run() {
Jerry Zhang487be612016-10-24 12:10:41 -0700185 if (!sHandle) {
186 ALOGE("MtpServer was never configured!");
187 return;
188 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400189
Jerry Zhang487be612016-10-24 12:10:41 -0700190 if (sHandle->start()) {
191 ALOGE("Failed to start usb driver!");
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -0800192 sHandle->close();
Jerry Zhang487be612016-10-24 12:10:41 -0700193 return;
194 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400195
196 while (1) {
Jerry Zhang487be612016-10-24 12:10:41 -0700197 int ret = mRequest.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400198 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700199 ALOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400200 if (errno == ECANCELED) {
201 // return to top of loop and wait for next command
202 continue;
203 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400204 break;
205 }
206 MtpOperationCode operation = mRequest.getOperationCode();
207 MtpTransactionID transaction = mRequest.getTransactionID();
208
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400210 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400211 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400212 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
213 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
214 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400215 if (dataIn) {
Jerry Zhang487be612016-10-24 12:10:41 -0700216 int ret = mData.read(sHandle);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400217 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000218 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400219 if (errno == ECANCELED) {
220 // return to top of loop and wait for next command
221 continue;
222 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400223 break;
224 }
Steve Block3856b092011-10-20 11:56:00 +0100225 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400226 } else {
227 mData.reset();
228 }
229
Mike Lockwood916076c2010-06-04 09:49:21 -0400230 if (handleRequest()) {
231 if (!dataIn && mData.hasData()) {
232 mData.setOperationCode(operation);
233 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("sending data:");
Jerry Zhang487be612016-10-24 12:10:41 -0700235 ret = mData.write(sHandle);
Mike Lockwood916076c2010-06-04 09:49:21 -0400236 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000237 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400238 if (errno == ECANCELED) {
239 // return to top of loop and wait for next command
240 continue;
241 }
242 break;
243 }
244 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400245
Mike Lockwood916076c2010-06-04 09:49:21 -0400246 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("sending response %04X", mResponse.getResponseCode());
Jerry Zhang487be612016-10-24 12:10:41 -0700248 ret = mResponse.write(sHandle);
tao.pei07a9e542015-07-17 17:18:41 +0800249 const int savedErrno = errno;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400250 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000251 ALOGE("request write returned %d, errno: %d", ret, errno);
tao.pei07a9e542015-07-17 17:18:41 +0800252 if (savedErrno == ECANCELED) {
Mike Lockwood916076c2010-06-04 09:49:21 -0400253 // return to top of loop and wait for next command
254 continue;
255 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400256 break;
257 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400258 } else {
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400260 }
261 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400262
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700263 // commit any open edits
264 int count = mObjectEditList.size();
265 for (int i = 0; i < count; i++) {
266 ObjectEdit* edit = mObjectEditList[i];
267 commitEdit(edit);
268 delete edit;
269 }
270 mObjectEditList.clear();
271
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400272 if (mSessionOpen)
273 mDatabase->sessionEnded();
Jerry Zhang487be612016-10-24 12:10:41 -0700274
275 sHandle->close();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400276}
277
Mike Lockwood873871f2010-07-12 18:54:16 -0400278void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500280 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400281}
282
283void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100284 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500285 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
286}
287
288void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100289 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500290 sendEvent(MTP_EVENT_STORE_ADDED, id);
291}
292
293void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100294 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500295 sendEvent(MTP_EVENT_STORE_REMOVED, id);
296}
297
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800298void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
299 ALOGV("sendDevicePropertyChanged %d\n", property);
300 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
301}
302
Mike Lockwooda8494402011-02-18 09:07:14 -0500303void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400304 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500305 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400306 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500307 mEvent.setParameter(1, param1);
Jerry Zhang487be612016-10-24 12:10:41 -0700308 if (mEvent.write(sHandle))
309 ALOGE("Mtp send event failed: %s", strerror(errno));
Mike Lockwood73ecd232010-07-19 14:29:58 -0400310 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400311}
312
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700313void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
314 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700315 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700316 mObjectEditList.add(edit);
317}
318
319MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
320 int count = mObjectEditList.size();
321 for (int i = 0; i < count; i++) {
322 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700323 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700324 }
Jerry Zhang487be612016-10-24 12:10:41 -0700325 return nullptr;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700326}
327
328void MtpServer::removeEditObject(MtpObjectHandle handle) {
329 int count = mObjectEditList.size();
330 for (int i = 0; i < count; i++) {
331 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700332 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700333 delete edit;
334 mObjectEditList.removeAt(i);
335 return;
336 }
337 }
Steve Block29357bc2012-01-06 19:20:56 +0000338 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700339}
340
341void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700342 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700343}
344
345
Mike Lockwood916076c2010-06-04 09:49:21 -0400346bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500347 Mutex::Autolock autoLock(mMutex);
348
Mike Lockwood16864ba2010-05-11 17:16:59 -0400349 MtpOperationCode operation = mRequest.getOperationCode();
350 MtpResponseCode response;
351
352 mResponse.reset();
353
354 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
355 // FIXME - need to delete mSendObjectHandle from the database
Steve Block29357bc2012-01-06 19:20:56 +0000356 ALOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400357 mSendObjectHandle = kInvalidObjectHandle;
358 }
359
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700360 int containertype = mRequest.getContainerType();
361 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
362 ALOGE("wrong container type %d", containertype);
363 return false;
364 }
365
366 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
367
Mike Lockwood16864ba2010-05-11 17:16:59 -0400368 switch (operation) {
369 case MTP_OPERATION_GET_DEVICE_INFO:
370 response = doGetDeviceInfo();
371 break;
372 case MTP_OPERATION_OPEN_SESSION:
373 response = doOpenSession();
374 break;
Jerry Zhang6dafecc2017-02-23 16:39:30 -0800375 case MTP_OPERATION_RESET_DEVICE:
Mike Lockwood16864ba2010-05-11 17:16:59 -0400376 case MTP_OPERATION_CLOSE_SESSION:
377 response = doCloseSession();
378 break;
379 case MTP_OPERATION_GET_STORAGE_IDS:
380 response = doGetStorageIDs();
381 break;
382 case MTP_OPERATION_GET_STORAGE_INFO:
383 response = doGetStorageInfo();
384 break;
385 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
386 response = doGetObjectPropsSupported();
387 break;
388 case MTP_OPERATION_GET_OBJECT_HANDLES:
389 response = doGetObjectHandles();
390 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400391 case MTP_OPERATION_GET_NUM_OBJECTS:
392 response = doGetNumObjects();
393 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400394 case MTP_OPERATION_GET_OBJECT_REFERENCES:
395 response = doGetObjectReferences();
396 break;
397 case MTP_OPERATION_SET_OBJECT_REFERENCES:
398 response = doSetObjectReferences();
399 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400400 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
401 response = doGetObjectPropValue();
402 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400403 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
404 response = doSetObjectPropValue();
405 break;
406 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
407 response = doGetDevicePropValue();
408 break;
409 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
410 response = doSetDevicePropValue();
411 break;
412 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
413 response = doResetDevicePropValue();
414 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400415 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
416 response = doGetObjectPropList();
417 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400418 case MTP_OPERATION_GET_OBJECT_INFO:
419 response = doGetObjectInfo();
420 break;
421 case MTP_OPERATION_GET_OBJECT:
422 response = doGetObject();
423 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700424 case MTP_OPERATION_GET_THUMB:
425 response = doGetThumb();
426 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500427 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700428 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
429 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500430 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400431 case MTP_OPERATION_SEND_OBJECT_INFO:
432 response = doSendObjectInfo();
433 break;
434 case MTP_OPERATION_SEND_OBJECT:
435 response = doSendObject();
436 break;
437 case MTP_OPERATION_DELETE_OBJECT:
438 response = doDeleteObject();
439 break;
Jerry Zhang708b3e02017-09-26 17:53:39 -0700440 case MTP_OPERATION_COPY_OBJECT:
441 response = doCopyObject();
442 break;
443 case MTP_OPERATION_MOVE_OBJECT:
444 response = doMoveObject();
445 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400447 response = doGetObjectPropDesc();
448 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400449 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
450 response = doGetDevicePropDesc();
451 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700452 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
453 response = doSendPartialObject();
454 break;
455 case MTP_OPERATION_TRUNCATE_OBJECT:
456 response = doTruncateObject();
457 break;
458 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
459 response = doBeginEditObject();
460 break;
461 case MTP_OPERATION_END_EDIT_OBJECT:
462 response = doEndEditObject();
463 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400464 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700465 ALOGE("got unsupported command %s (%x)",
466 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400467 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
468 break;
469 }
470
Mike Lockwood916076c2010-06-04 09:49:21 -0400471 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
472 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400473 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400474 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400475}
476
477MtpResponseCode MtpServer::doGetDeviceInfo() {
478 MtpStringBuffer string;
479
Mike Lockwood782aef12010-08-10 07:37:50 -0400480 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
481 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
482 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
483
Mike Lockwood16864ba2010-05-11 17:16:59 -0400484 // fill in device info
485 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400486 if (mPtp) {
487 mData.putUInt32(0);
488 } else {
489 // MTP Vendor Extension ID
490 mData.putUInt32(6);
491 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400492 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400493 if (mPtp) {
494 // no extensions
495 string.set("");
496 } else {
497 // MTP extensions
498 string.set("microsoft.com: 1.0; android.com: 1.0;");
499 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400500 mData.putString(string); // MTP Extensions
501 mData.putUInt16(0); //Functional Mode
502 mData.putAUInt16(kSupportedOperationCodes,
503 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400504 mData.putAUInt16(kSupportedEventCodes,
505 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400506 mData.putAUInt16(deviceProperties); // Device Properties Supported
507 mData.putAUInt16(captureFormats); // Capture Formats
508 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500509
Alex Klyubin792298f2016-12-21 11:20:22 -0800510 mData.putString(mDeviceInfoManufacturer); // Manufacturer
511 mData.putString(mDeviceInfoModel); // Model
512 mData.putString(mDeviceInfoDeviceVersion); // Device Version
513 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400514
Mike Lockwood782aef12010-08-10 07:37:50 -0400515 delete playbackFormats;
516 delete captureFormats;
517 delete deviceProperties;
518
Mike Lockwood16864ba2010-05-11 17:16:59 -0400519 return MTP_RESPONSE_OK;
520}
521
522MtpResponseCode MtpServer::doOpenSession() {
523 if (mSessionOpen) {
524 mResponse.setParameter(1, mSessionID);
525 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
526 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800527 if (mRequest.getParameterCount() < 1)
528 return MTP_RESPONSE_INVALID_PARAMETER;
529
Mike Lockwood16864ba2010-05-11 17:16:59 -0400530 mSessionID = mRequest.getParameter(1);
531 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400532
533 mDatabase->sessionStarted();
534
Mike Lockwood16864ba2010-05-11 17:16:59 -0400535 return MTP_RESPONSE_OK;
536}
537
538MtpResponseCode MtpServer::doCloseSession() {
539 if (!mSessionOpen)
540 return MTP_RESPONSE_SESSION_NOT_OPEN;
541 mSessionID = 0;
542 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400543 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400544 return MTP_RESPONSE_OK;
545}
546
547MtpResponseCode MtpServer::doGetStorageIDs() {
548 if (!mSessionOpen)
549 return MTP_RESPONSE_SESSION_NOT_OPEN;
550
551 int count = mStorages.size();
552 mData.putUInt32(count);
553 for (int i = 0; i < count; i++)
554 mData.putUInt32(mStorages[i]->getStorageID());
555
556 return MTP_RESPONSE_OK;
557}
558
559MtpResponseCode MtpServer::doGetStorageInfo() {
560 MtpStringBuffer string;
561
562 if (!mSessionOpen)
563 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800564 if (mRequest.getParameterCount() < 1)
565 return MTP_RESPONSE_INVALID_PARAMETER;
566
Mike Lockwood16864ba2010-05-11 17:16:59 -0400567 MtpStorageID id = mRequest.getParameter(1);
568 MtpStorage* storage = getStorage(id);
569 if (!storage)
570 return MTP_RESPONSE_INVALID_STORAGE_ID;
571
572 mData.putUInt16(storage->getType());
573 mData.putUInt16(storage->getFileSystemType());
574 mData.putUInt16(storage->getAccessCapability());
575 mData.putUInt64(storage->getMaxCapacity());
576 mData.putUInt64(storage->getFreeSpace());
577 mData.putUInt32(1024*1024*1024); // Free Space in Objects
578 string.set(storage->getDescription());
579 mData.putString(string);
580 mData.putEmptyString(); // Volume Identifier
581
582 return MTP_RESPONSE_OK;
583}
584
585MtpResponseCode MtpServer::doGetObjectPropsSupported() {
586 if (!mSessionOpen)
587 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800588 if (mRequest.getParameterCount() < 1)
589 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400590 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800591 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400592 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400593 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400594 return MTP_RESPONSE_OK;
595}
596
597MtpResponseCode MtpServer::doGetObjectHandles() {
598 if (!mSessionOpen)
599 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800600 if (mRequest.getParameterCount() < 3)
601 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400602 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400603 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400604 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400605 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500606
607 if (!hasStorage(storageID))
608 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400609
610 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
611 mData.putAUInt32(handles);
612 delete handles;
613 return MTP_RESPONSE_OK;
614}
615
Mike Lockwood343af4e2010-08-02 10:52:20 -0400616MtpResponseCode MtpServer::doGetNumObjects() {
617 if (!mSessionOpen)
618 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800619 if (mRequest.getParameterCount() < 3)
620 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400621 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
622 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
623 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400624 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500625 if (!hasStorage(storageID))
626 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400627
628 int count = mDatabase->getNumObjects(storageID, format, parent);
629 if (count >= 0) {
630 mResponse.setParameter(1, count);
631 return MTP_RESPONSE_OK;
632 } else {
633 mResponse.setParameter(1, 0);
634 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
635 }
636}
637
Mike Lockwood438344f2010-08-03 15:30:09 -0400638MtpResponseCode MtpServer::doGetObjectReferences() {
639 if (!mSessionOpen)
640 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500641 if (!hasStorage())
642 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800643 if (mRequest.getParameterCount() < 1)
644 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500645 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400646
647 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400648 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400649 if (handles) {
650 mData.putAUInt32(handles);
651 delete handles;
652 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400653 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400654 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400655 return MTP_RESPONSE_OK;
656}
657
658MtpResponseCode MtpServer::doSetObjectReferences() {
659 if (!mSessionOpen)
660 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500661 if (!hasStorage())
662 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800663 if (mRequest.getParameterCount() < 1)
664 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400665 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500666
Mike Lockwood438344f2010-08-03 15:30:09 -0400667 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800668 if (!references)
669 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400670 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
671 delete references;
672 return result;
673}
674
Mike Lockwood16864ba2010-05-11 17:16:59 -0400675MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500676 if (!hasStorage())
677 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800678 if (mRequest.getParameterCount() < 2)
679 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400680 MtpObjectHandle handle = mRequest.getParameter(1);
681 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100682 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400683 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400684
Mike Lockwood8277cec2010-08-10 15:20:35 -0400685 return mDatabase->getObjectPropertyValue(handle, property, mData);
686}
687
688MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500689 if (!hasStorage())
690 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800691 if (mRequest.getParameterCount() < 2)
692 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400693 MtpObjectHandle handle = mRequest.getParameter(1);
694 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400696 MtpDebug::getObjectPropCodeName(property));
697
698 return mDatabase->setObjectPropertyValue(handle, property, mData);
699}
700
701MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800702 if (mRequest.getParameterCount() < 1)
703 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400704 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400706 MtpDebug::getDevicePropCodeName(property));
707
708 return mDatabase->getDevicePropertyValue(property, mData);
709}
710
711MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800712 if (mRequest.getParameterCount() < 1)
713 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400714 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400716 MtpDebug::getDevicePropCodeName(property));
717
718 return mDatabase->setDevicePropertyValue(property, mData);
719}
720
721MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800722 if (mRequest.getParameterCount() < 1)
723 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400724 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400726 MtpDebug::getDevicePropCodeName(property));
727
728 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400729}
730
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400731MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500732 if (!hasStorage())
733 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800734 if (mRequest.getParameterCount() < 5)
735 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400736
737 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500738 // use uint32_t so we can support 0xFFFFFFFF
739 uint32_t format = mRequest.getParameter(2);
740 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400741 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500742 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400744 handle, MtpDebug::getFormatCodeName(format),
745 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
746
747 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
748}
749
Mike Lockwood16864ba2010-05-11 17:16:59 -0400750MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500751 if (!hasStorage())
752 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800753 if (mRequest.getParameterCount() < 1)
754 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400755 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700756 MtpObjectInfo info(handle);
757 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
758 if (result == MTP_RESPONSE_OK) {
759 char date[20];
760
761 mData.putUInt32(info.mStorageID);
762 mData.putUInt16(info.mFormat);
763 mData.putUInt16(info.mProtectionStatus);
764
765 // if object is being edited the database size may be out of date
766 uint32_t size = info.mCompressedSize;
767 ObjectEdit* edit = getEditObject(handle);
768 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700769 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700770 mData.putUInt32(size);
771
772 mData.putUInt16(info.mThumbFormat);
773 mData.putUInt32(info.mThumbCompressedSize);
774 mData.putUInt32(info.mThumbPixWidth);
775 mData.putUInt32(info.mThumbPixHeight);
776 mData.putUInt32(info.mImagePixWidth);
777 mData.putUInt32(info.mImagePixHeight);
778 mData.putUInt32(info.mImagePixDepth);
779 mData.putUInt32(info.mParent);
780 mData.putUInt16(info.mAssociationType);
781 mData.putUInt32(info.mAssociationDesc);
782 mData.putUInt32(info.mSequenceNumber);
783 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700784 formatDateTime(info.mDateCreated, date, sizeof(date));
785 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700786 formatDateTime(info.mDateModified, date, sizeof(date));
787 mData.putString(date); // date modified
788 mData.putEmptyString(); // keywords
789 }
790 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400791}
792
793MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500794 if (!hasStorage())
795 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800796 if (mRequest.getParameterCount() < 1)
797 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400798 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400799 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400800 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800801 MtpObjectFormat format;
802 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400803 if (result != MTP_RESPONSE_OK)
804 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400805
Jerry Zhang487be612016-10-24 12:10:41 -0700806 auto start = std::chrono::steady_clock::now();
807
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400808 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400809 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400810 mfr.fd = open(filePath, O_RDONLY);
811 if (mfr.fd < 0) {
812 return MTP_RESPONSE_GENERAL_ERROR;
813 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400814 mfr.offset = 0;
815 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400816 mfr.command = mRequest.getOperationCode();
817 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400818
819 // then transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700820 int ret = sHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800821 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700822 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800823 if (errno == ECANCELED) {
824 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
825 } else {
826 result = MTP_RESPONSE_GENERAL_ERROR;
827 }
828 } else {
829 result = MTP_RESPONSE_OK;
830 }
831
Jerry Zhang487be612016-10-24 12:10:41 -0700832 auto end = std::chrono::steady_clock::now();
833 std::chrono::duration<double> diff = end - start;
834 struct stat sstat;
835 fstat(mfr.fd, &sstat);
836 uint64_t finalsize = sstat.st_size;
837 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
838 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400839 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800840 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400841}
842
Mike Lockwood64000782011-04-24 18:40:17 -0700843MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800844 if (mRequest.getParameterCount() < 1)
845 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700846 MtpObjectHandle handle = mRequest.getParameter(1);
847 size_t thumbSize;
848 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
849 if (thumb) {
850 // send data
851 mData.setOperationCode(mRequest.getOperationCode());
852 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang487be612016-10-24 12:10:41 -0700853 mData.writeData(sHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700854 free(thumb);
855 return MTP_RESPONSE_OK;
856 } else {
857 return MTP_RESPONSE_GENERAL_ERROR;
858 }
859}
860
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700861MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500862 if (!hasStorage())
863 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500864 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700865 uint64_t offset;
866 uint32_t length;
867 offset = mRequest.getParameter(2);
868 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800869 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
870 if (mRequest.getParameterCount() < 4)
871 return MTP_RESPONSE_INVALID_PARAMETER;
872
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700873 // android extension with 64 bit offset
874 uint64_t offset2 = mRequest.getParameter(3);
875 offset = offset | (offset2 << 32);
876 length = mRequest.getParameter(4);
877 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800878 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
879 if (mRequest.getParameterCount() < 3)
880 return MTP_RESPONSE_INVALID_PARAMETER;
881
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700882 // standard GetPartialObject
883 length = mRequest.getParameter(3);
884 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500885 MtpString pathBuf;
886 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800887 MtpObjectFormat format;
888 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500889 if (result != MTP_RESPONSE_OK)
890 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700891 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500892 length = fileLength - offset;
893
894 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700895 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500896 mtp_file_range mfr;
897 mfr.fd = open(filePath, O_RDONLY);
898 if (mfr.fd < 0) {
899 return MTP_RESPONSE_GENERAL_ERROR;
900 }
901 mfr.offset = offset;
902 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400903 mfr.command = mRequest.getOperationCode();
904 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500905 mResponse.setParameter(1, length);
906
Mike Lockwoodef441d92011-07-14 21:00:02 -0400907 // transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700908 int ret = sHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100909 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800910 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500911 if (ret < 0) {
912 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800913 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500914 else
tao.pei07a9e542015-07-17 17:18:41 +0800915 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500916 }
tao.pei07a9e542015-07-17 17:18:41 +0800917 close(mfr.fd);
918 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500919}
920
Mike Lockwood16864ba2010-05-11 17:16:59 -0400921MtpResponseCode MtpServer::doSendObjectInfo() {
922 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800923 uint16_t temp16;
924 uint32_t temp32;
925
926 if (mRequest.getParameterCount() < 2)
927 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400928 MtpStorageID storageID = mRequest.getParameter(1);
929 MtpStorage* storage = getStorage(storageID);
930 MtpObjectHandle parent = mRequest.getParameter(2);
931 if (!storage)
932 return MTP_RESPONSE_INVALID_STORAGE_ID;
933
934 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400935 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400936 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400937 parent = 0;
938 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800939 int64_t length;
940 MtpObjectFormat format;
941 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400942 if (result != MTP_RESPONSE_OK)
943 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800944 if (format != MTP_FORMAT_ASSOCIATION)
945 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400946 }
947
948 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800949 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
950 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
951 MtpObjectFormat format = temp16;
952 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
953 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
954 mSendObjectFileSize = temp32;
955 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
956 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
957 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
958 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
959 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
960 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
961 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
962 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
963 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800964 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800965 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400966 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800967 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700968 if (name.getCharCount() == 0) {
969 ALOGE("empty name");
970 return MTP_RESPONSE_INVALID_PARAMETER;
971 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800972 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
973 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400974 // keywords follow
975
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400977 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400978 if (!parseDateTime(modified, modifiedTime))
979 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400980
981 if (path[path.size() - 1] != '/')
982 path += "/";
983 path += (const char *)name;
984
Mike Lockwood20c3be02010-12-12 12:17:43 -0800985 // check space first
986 if (mSendObjectFileSize > storage->getFreeSpace())
987 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400988 uint64_t maxFileSize = storage->getMaxFileSize();
989 // check storage max file size
990 if (maxFileSize != 0) {
991 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
992 // is >= 0xFFFFFFFF
993 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
994 return MTP_RESPONSE_OBJECT_TOO_LARGE;
995 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800996
Steve Blockb8a80522011-12-20 16:23:08 +0000997 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400998 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
999 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -04001000 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -04001001 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -04001002 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001003
1004 if (format == MTP_FORMAT_ASSOCIATION) {
1005 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001006 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001007 umask(mask);
1008 if (ret && ret != -EEXIST)
1009 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001010 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwoodaa952402011-01-18 11:06:19 -08001011
1012 // SendObject does not get sent for directories, so call endSendObject here instead
1013 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001014 } else {
1015 mSendObjectFilePath = path;
1016 // save the handle for the SendObject call, which should follow
1017 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -04001018 mSendObjectFormat = format;
caozhiyuan854cb172017-04-26 16:52:30 +08001019 mSendObjectModifiedTime = modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001020 }
1021
1022 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001023 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001024 mResponse.setParameter(3, handle);
1025
1026 return MTP_RESPONSE_OK;
1027}
1028
Jerry Zhang708b3e02017-09-26 17:53:39 -07001029MtpResponseCode MtpServer::doMoveObject() {
1030 if (!hasStorage())
1031 return MTP_RESPONSE_GENERAL_ERROR;
1032 if (mRequest.getParameterCount() < 3)
1033 return MTP_RESPONSE_INVALID_PARAMETER;
1034 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1035 MtpStorageID storageID = mRequest.getParameter(2);
1036 MtpStorage* storage = getStorage(storageID);
1037 MtpObjectHandle parent = mRequest.getParameter(3);
1038 if (!storage)
1039 return MTP_RESPONSE_INVALID_STORAGE_ID;
1040 MtpString path;
1041 MtpResponseCode result;
1042
1043 MtpString fromPath;
1044 int64_t fileLength;
1045 MtpObjectFormat format;
1046 MtpObjectInfo info(objectHandle);
1047 result = mDatabase->getObjectInfo(objectHandle, info);
1048 if (result != MTP_RESPONSE_OK)
1049 return result;
1050 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1051 if (result != MTP_RESPONSE_OK)
1052 return result;
1053
1054 // special case the root
1055 if (parent == 0) {
1056 path = storage->getPath();
1057 } else {
1058 int64_t parentLength;
1059 MtpObjectFormat parentFormat;
1060 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1061 if (result != MTP_RESPONSE_OK)
1062 return result;
1063 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1064 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1065 }
1066
1067 if (path[path.size() - 1] != '/')
1068 path += "/";
1069 path += info.mName;
1070
1071 result = mDatabase->moveObject(objectHandle, parent, path);
1072 if (result != MTP_RESPONSE_OK)
1073 return result;
1074
1075 if (info.mStorageID == storageID) {
1076 ALOGV("Moving file from %s to %s", (const char*)fromPath, (const char*)path);
1077 if (rename(fromPath, path)) {
1078 ALOGE("rename() failed from %s to %s", (const char*)fromPath, (const char*)path);
1079 result = MTP_RESPONSE_GENERAL_ERROR;
1080 }
1081 } else {
1082 ALOGV("Moving across storages from %s to %s", (const char*)fromPath, (const char*)path);
1083 if (copyFile(fromPath, path)) {
1084 result = MTP_RESPONSE_GENERAL_ERROR;
1085 } else {
1086 deletePath(fromPath);
1087 }
1088 }
1089
1090 // If the move failed, undo the database change
1091 if (result != MTP_RESPONSE_OK)
1092 if (mDatabase->moveObject(objectHandle, info.mParent, fromPath) != MTP_RESPONSE_OK)
1093 ALOGE("Couldn't undo failed move");
1094
1095 return result;
1096}
1097
1098MtpResponseCode MtpServer::doCopyObject() {
1099 if (!hasStorage())
1100 return MTP_RESPONSE_GENERAL_ERROR;
1101 MtpResponseCode result = MTP_RESPONSE_OK;
1102 if (mRequest.getParameterCount() < 3)
1103 return MTP_RESPONSE_INVALID_PARAMETER;
1104 MtpObjectHandle objectHandle = mRequest.getParameter(1);
1105 MtpStorageID storageID = mRequest.getParameter(2);
1106 MtpStorage* storage = getStorage(storageID);
1107 MtpObjectHandle parent = mRequest.getParameter(3);
1108 if (!storage)
1109 return MTP_RESPONSE_INVALID_STORAGE_ID;
1110 MtpString path;
1111
1112 MtpString fromPath;
1113 int64_t fileLength;
1114 MtpObjectFormat format;
1115 MtpObjectInfo info(objectHandle);
1116 result = mDatabase->getObjectInfo(objectHandle, info);
1117 if (result != MTP_RESPONSE_OK)
1118 return result;
1119 result = mDatabase->getObjectFilePath(objectHandle, fromPath, fileLength, format);
1120 if (result != MTP_RESPONSE_OK)
1121 return result;
1122
1123 // special case the root
1124 if (parent == 0) {
1125 path = storage->getPath();
1126 } else {
1127 int64_t parentLength;
1128 MtpObjectFormat parentFormat;
1129 result = mDatabase->getObjectFilePath(parent, path, parentLength, parentFormat);
1130 if (result != MTP_RESPONSE_OK)
1131 return result;
1132 if (parentFormat != MTP_FORMAT_ASSOCIATION)
1133 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
1134 }
1135
1136 // check space first
1137 if ((uint64_t) fileLength > storage->getFreeSpace())
1138 return MTP_RESPONSE_STORAGE_FULL;
1139
1140 if (path[path.size() - 1] != '/')
1141 path += "/";
1142 path += info.mName;
1143
1144 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
1145 format, parent, storageID, fileLength, info.mDateModified);
1146 if (handle == kInvalidObjectHandle) {
1147 return MTP_RESPONSE_GENERAL_ERROR;
1148 }
1149
1150 ALOGV("Copying file from %s to %s", (const char*)fromPath, (const char*)path);
1151 if (copyFile(fromPath, path)) {
1152 result = MTP_RESPONSE_GENERAL_ERROR;
1153 }
1154
1155 mDatabase->endSendObject(path, handle, format, result);
1156 mResponse.setParameter(1, handle);
1157 return result;
1158}
1159
Mike Lockwood16864ba2010-05-11 17:16:59 -04001160MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001161 if (!hasStorage())
1162 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001163 MtpResponseCode result = MTP_RESPONSE_OK;
1164 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001165 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001166 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001167 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001168
Jerry Zhang487be612016-10-24 12:10:41 -07001169 auto start = std::chrono::steady_clock::now();
1170
Mike Lockwood16864ba2010-05-11 17:16:59 -04001171 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001172 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001173 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1174 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001175 }
1176
Mike Lockwoodef441d92011-07-14 21:00:02 -04001177 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001178 ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001179 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1180 result = MTP_RESPONSE_GENERAL_ERROR;
1181 goto done;
1182 }
1183 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001184
1185 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001186 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001187 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001188 result = MTP_RESPONSE_GENERAL_ERROR;
1189 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001190 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001191 fchown(mfr.fd, getuid(), mFileGroup);
1192 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001193 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001194 fchmod(mfr.fd, mFilePermission);
1195 umask(mask);
1196
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001197 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001198 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001199 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001200
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001201 if (ret < 0) {
1202 ALOGE("failed to write initial data");
1203 result = MTP_RESPONSE_GENERAL_ERROR;
1204 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001205 mfr.offset = initialData;
1206 if (mSendObjectFileSize == 0xFFFFFFFF) {
1207 // tell driver to read until it receives a short packet
1208 mfr.length = 0xFFFFFFFF;
1209 } else {
1210 mfr.length = mSendObjectFileSize - initialData;
1211 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001212
Jerry Zhang54107562017-05-15 11:54:19 -07001213 mfr.command = 0;
1214 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001215
Jerry Zhang54107562017-05-15 11:54:19 -07001216 // transfer the file
1217 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1218 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1219 if ((ret < 0) && (errno == ECANCELED)) {
1220 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001221 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001222 }
caozhiyuan854cb172017-04-26 16:52:30 +08001223
1224 if (mSendObjectModifiedTime) {
1225 struct timespec newTime[2];
1226 newTime[0].tv_nsec = UTIME_NOW;
1227 newTime[1].tv_sec = mSendObjectModifiedTime;
1228 newTime[1].tv_nsec = 0;
1229 if (futimens(mfr.fd, newTime) < 0) {
1230 ALOGW("changing modified time failed, %s", strerror(errno));
1231 }
1232 }
1233
Jerry Zhang487be612016-10-24 12:10:41 -07001234 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001235 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001236
Mike Lockwood916076c2010-06-04 09:49:21 -04001237 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001238 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001239 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001240 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001241 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001242 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001243 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001244 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001245
1246done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001247 // reset so we don't attempt to send the data back
1248 mData.reset();
1249
Mike Lockwood4714b072010-07-12 08:49:01 -04001250 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -08001251 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001252 mSendObjectHandle = kInvalidObjectHandle;
1253 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001254 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001255
1256 auto end = std::chrono::steady_clock::now();
1257 std::chrono::duration<double> diff = end - start;
1258 uint64_t finalsize = sstat.st_size;
1259 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1260 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001261 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001262}
1263
1264MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001265 if (!hasStorage())
1266 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001267 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001268 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001269 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001270 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001271 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1272 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001273
1274 MtpString filePath;
1275 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001276 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001277 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001278 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001279 result = mDatabase->deleteFile(handle);
1280 // Don't delete the actual files unless the database deletion is allowed
1281 if (result == MTP_RESPONSE_OK) {
1282 deletePath((const char *)filePath);
1283 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001284 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001285
1286 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001287}
1288
1289MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001290 if (mRequest.getParameterCount() < 2)
1291 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001292 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001293 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001294 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001295 MtpDebug::getFormatCodeName(format));
1296 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001297 if (!property)
1298 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001299 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001300 delete property;
1301 return MTP_RESPONSE_OK;
1302}
1303
1304MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001305 if (mRequest.getParameterCount() < 1)
1306 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001307 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001308 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001309 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1310 if (!property)
1311 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1312 property->write(mData);
1313 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001314 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001315}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001316
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001317MtpResponseCode MtpServer::doSendPartialObject() {
1318 if (!hasStorage())
1319 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001320 if (mRequest.getParameterCount() < 4)
1321 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001322 MtpObjectHandle handle = mRequest.getParameter(1);
1323 uint64_t offset = mRequest.getParameter(2);
1324 uint64_t offset2 = mRequest.getParameter(3);
1325 offset = offset | (offset2 << 32);
1326 uint32_t length = mRequest.getParameter(4);
1327
1328 ObjectEdit* edit = getEditObject(handle);
1329 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001330 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001331 return MTP_RESPONSE_GENERAL_ERROR;
1332 }
1333
1334 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001335 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001336 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1337 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001338 return MTP_RESPONSE_GENERAL_ERROR;
1339 }
1340
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001341 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001342 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001343
Mike Lockwoodef441d92011-07-14 21:00:02 -04001344 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001345 int ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001346 if (ret < MTP_CONTAINER_HEADER_SIZE)
1347 return MTP_RESPONSE_GENERAL_ERROR;
1348 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1349
1350 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001351 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001352 offset += initialData;
1353 length -= initialData;
1354 }
1355
tao.pei07a9e542015-07-17 17:18:41 +08001356 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001357 if (ret < 0) {
1358 ALOGE("failed to write initial data");
1359 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001360 mtp_file_range mfr;
1361 mfr.fd = edit->mFD;
1362 mfr.offset = offset;
1363 mfr.length = length;
1364 mfr.command = 0;
1365 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001366
Jerry Zhang54107562017-05-15 11:54:19 -07001367 // transfer the file
1368 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1369 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1370 if ((ret < 0) && (errno == ECANCELED)) {
1371 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001372 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001373 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001374 if (ret < 0) {
1375 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001376 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001377 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1378 else
1379 return MTP_RESPONSE_GENERAL_ERROR;
1380 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001381
1382 // reset so we don't attempt to send this back
1383 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001384 mResponse.setParameter(1, length);
1385 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001386 if (end > edit->mSize) {
1387 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001388 }
1389 return MTP_RESPONSE_OK;
1390}
1391
1392MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001393 if (mRequest.getParameterCount() < 3)
1394 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001395 MtpObjectHandle handle = mRequest.getParameter(1);
1396 ObjectEdit* edit = getEditObject(handle);
1397 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001398 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001399 return MTP_RESPONSE_GENERAL_ERROR;
1400 }
1401
1402 uint64_t offset = mRequest.getParameter(2);
1403 uint64_t offset2 = mRequest.getParameter(3);
1404 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001405 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001406 return MTP_RESPONSE_GENERAL_ERROR;
1407 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001408 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001409 return MTP_RESPONSE_OK;
1410 }
1411}
1412
1413MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001414 if (mRequest.getParameterCount() < 1)
1415 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001416 MtpObjectHandle handle = mRequest.getParameter(1);
1417 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001418 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001419 return MTP_RESPONSE_GENERAL_ERROR;
1420 }
1421
1422 MtpString path;
1423 int64_t fileLength;
1424 MtpObjectFormat format;
1425 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1426 if (result != MTP_RESPONSE_OK)
1427 return result;
1428
1429 int fd = open((const char *)path, O_RDWR | O_EXCL);
1430 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001431 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001432 return MTP_RESPONSE_GENERAL_ERROR;
1433 }
1434
1435 addEditObject(handle, path, fileLength, format, fd);
1436 return MTP_RESPONSE_OK;
1437}
1438
1439MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001440 if (mRequest.getParameterCount() < 1)
1441 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001442 MtpObjectHandle handle = mRequest.getParameter(1);
1443 ObjectEdit* edit = getEditObject(handle);
1444 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001445 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001446 return MTP_RESPONSE_GENERAL_ERROR;
1447 }
1448
1449 commitEdit(edit);
1450 removeEditObject(handle);
1451 return MTP_RESPONSE_OK;
1452}
1453
Mike Lockwood7850ef92010-05-14 10:10:36 -04001454} // namespace android