blob: e148b0c31c42dfaa0ea0e6617409a2224a1dd42f [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,
70// 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;
440 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400441 response = doGetObjectPropDesc();
442 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400443 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
444 response = doGetDevicePropDesc();
445 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700446 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
447 response = doSendPartialObject();
448 break;
449 case MTP_OPERATION_TRUNCATE_OBJECT:
450 response = doTruncateObject();
451 break;
452 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
453 response = doBeginEditObject();
454 break;
455 case MTP_OPERATION_END_EDIT_OBJECT:
456 response = doEndEditObject();
457 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400458 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700459 ALOGE("got unsupported command %s (%x)",
460 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400461 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
462 break;
463 }
464
Mike Lockwood916076c2010-06-04 09:49:21 -0400465 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
466 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400467 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400468 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400469}
470
471MtpResponseCode MtpServer::doGetDeviceInfo() {
472 MtpStringBuffer string;
473
Mike Lockwood782aef12010-08-10 07:37:50 -0400474 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
475 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
476 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
477
Mike Lockwood16864ba2010-05-11 17:16:59 -0400478 // fill in device info
479 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400480 if (mPtp) {
481 mData.putUInt32(0);
482 } else {
483 // MTP Vendor Extension ID
484 mData.putUInt32(6);
485 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400486 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400487 if (mPtp) {
488 // no extensions
489 string.set("");
490 } else {
491 // MTP extensions
492 string.set("microsoft.com: 1.0; android.com: 1.0;");
493 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400494 mData.putString(string); // MTP Extensions
495 mData.putUInt16(0); //Functional Mode
496 mData.putAUInt16(kSupportedOperationCodes,
497 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400498 mData.putAUInt16(kSupportedEventCodes,
499 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400500 mData.putAUInt16(deviceProperties); // Device Properties Supported
501 mData.putAUInt16(captureFormats); // Capture Formats
502 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500503
Alex Klyubin792298f2016-12-21 11:20:22 -0800504 mData.putString(mDeviceInfoManufacturer); // Manufacturer
505 mData.putString(mDeviceInfoModel); // Model
506 mData.putString(mDeviceInfoDeviceVersion); // Device Version
507 mData.putString(mDeviceInfoSerialNumber); // Serial Number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400508
Mike Lockwood782aef12010-08-10 07:37:50 -0400509 delete playbackFormats;
510 delete captureFormats;
511 delete deviceProperties;
512
Mike Lockwood16864ba2010-05-11 17:16:59 -0400513 return MTP_RESPONSE_OK;
514}
515
516MtpResponseCode MtpServer::doOpenSession() {
517 if (mSessionOpen) {
518 mResponse.setParameter(1, mSessionID);
519 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
520 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800521 if (mRequest.getParameterCount() < 1)
522 return MTP_RESPONSE_INVALID_PARAMETER;
523
Mike Lockwood16864ba2010-05-11 17:16:59 -0400524 mSessionID = mRequest.getParameter(1);
525 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400526
527 mDatabase->sessionStarted();
528
Mike Lockwood16864ba2010-05-11 17:16:59 -0400529 return MTP_RESPONSE_OK;
530}
531
532MtpResponseCode MtpServer::doCloseSession() {
533 if (!mSessionOpen)
534 return MTP_RESPONSE_SESSION_NOT_OPEN;
535 mSessionID = 0;
536 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400537 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400538 return MTP_RESPONSE_OK;
539}
540
541MtpResponseCode MtpServer::doGetStorageIDs() {
542 if (!mSessionOpen)
543 return MTP_RESPONSE_SESSION_NOT_OPEN;
544
545 int count = mStorages.size();
546 mData.putUInt32(count);
547 for (int i = 0; i < count; i++)
548 mData.putUInt32(mStorages[i]->getStorageID());
549
550 return MTP_RESPONSE_OK;
551}
552
553MtpResponseCode MtpServer::doGetStorageInfo() {
554 MtpStringBuffer string;
555
556 if (!mSessionOpen)
557 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800558 if (mRequest.getParameterCount() < 1)
559 return MTP_RESPONSE_INVALID_PARAMETER;
560
Mike Lockwood16864ba2010-05-11 17:16:59 -0400561 MtpStorageID id = mRequest.getParameter(1);
562 MtpStorage* storage = getStorage(id);
563 if (!storage)
564 return MTP_RESPONSE_INVALID_STORAGE_ID;
565
566 mData.putUInt16(storage->getType());
567 mData.putUInt16(storage->getFileSystemType());
568 mData.putUInt16(storage->getAccessCapability());
569 mData.putUInt64(storage->getMaxCapacity());
570 mData.putUInt64(storage->getFreeSpace());
571 mData.putUInt32(1024*1024*1024); // Free Space in Objects
572 string.set(storage->getDescription());
573 mData.putString(string);
574 mData.putEmptyString(); // Volume Identifier
575
576 return MTP_RESPONSE_OK;
577}
578
579MtpResponseCode MtpServer::doGetObjectPropsSupported() {
580 if (!mSessionOpen)
581 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800582 if (mRequest.getParameterCount() < 1)
583 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400584 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800585 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400586 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400587 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400588 return MTP_RESPONSE_OK;
589}
590
591MtpResponseCode MtpServer::doGetObjectHandles() {
592 if (!mSessionOpen)
593 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800594 if (mRequest.getParameterCount() < 3)
595 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400596 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400597 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400598 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400599 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500600
601 if (!hasStorage(storageID))
602 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400603
604 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
605 mData.putAUInt32(handles);
606 delete handles;
607 return MTP_RESPONSE_OK;
608}
609
Mike Lockwood343af4e2010-08-02 10:52:20 -0400610MtpResponseCode MtpServer::doGetNumObjects() {
611 if (!mSessionOpen)
612 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwoodab063842014-11-12 14:20:06 -0800613 if (mRequest.getParameterCount() < 3)
614 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400615 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
616 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
617 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400618 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500619 if (!hasStorage(storageID))
620 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400621
622 int count = mDatabase->getNumObjects(storageID, format, parent);
623 if (count >= 0) {
624 mResponse.setParameter(1, count);
625 return MTP_RESPONSE_OK;
626 } else {
627 mResponse.setParameter(1, 0);
628 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
629 }
630}
631
Mike Lockwood438344f2010-08-03 15:30:09 -0400632MtpResponseCode MtpServer::doGetObjectReferences() {
633 if (!mSessionOpen)
634 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500635 if (!hasStorage())
636 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800637 if (mRequest.getParameterCount() < 1)
638 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwooda8494402011-02-18 09:07:14 -0500639 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400640
641 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400642 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400643 if (handles) {
644 mData.putAUInt32(handles);
645 delete handles;
646 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400647 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400648 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400649 return MTP_RESPONSE_OK;
650}
651
652MtpResponseCode MtpServer::doSetObjectReferences() {
653 if (!mSessionOpen)
654 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500655 if (!hasStorage())
656 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800657 if (mRequest.getParameterCount() < 1)
658 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400659 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500660
Mike Lockwood438344f2010-08-03 15:30:09 -0400661 MtpObjectHandleList* references = mData.getAUInt32();
Mike Lockwoodab063842014-11-12 14:20:06 -0800662 if (!references)
663 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood438344f2010-08-03 15:30:09 -0400664 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
665 delete references;
666 return result;
667}
668
Mike Lockwood16864ba2010-05-11 17:16:59 -0400669MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500670 if (!hasStorage())
671 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800672 if (mRequest.getParameterCount() < 2)
673 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400674 MtpObjectHandle handle = mRequest.getParameter(1);
675 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100676 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400677 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400678
Mike Lockwood8277cec2010-08-10 15:20:35 -0400679 return mDatabase->getObjectPropertyValue(handle, property, mData);
680}
681
682MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500683 if (!hasStorage())
684 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800685 if (mRequest.getParameterCount() < 2)
686 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400687 MtpObjectHandle handle = mRequest.getParameter(1);
688 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400690 MtpDebug::getObjectPropCodeName(property));
691
692 return mDatabase->setObjectPropertyValue(handle, property, mData);
693}
694
695MtpResponseCode MtpServer::doGetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800696 if (mRequest.getParameterCount() < 1)
697 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400698 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400700 MtpDebug::getDevicePropCodeName(property));
701
702 return mDatabase->getDevicePropertyValue(property, mData);
703}
704
705MtpResponseCode MtpServer::doSetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800706 if (mRequest.getParameterCount() < 1)
707 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400708 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400710 MtpDebug::getDevicePropCodeName(property));
711
712 return mDatabase->setDevicePropertyValue(property, mData);
713}
714
715MtpResponseCode MtpServer::doResetDevicePropValue() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800716 if (mRequest.getParameterCount() < 1)
717 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400718 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100719 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400720 MtpDebug::getDevicePropCodeName(property));
721
722 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400723}
724
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400725MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500726 if (!hasStorage())
727 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800728 if (mRequest.getParameterCount() < 5)
729 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400730
731 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500732 // use uint32_t so we can support 0xFFFFFFFF
733 uint32_t format = mRequest.getParameter(2);
734 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400735 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500736 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400738 handle, MtpDebug::getFormatCodeName(format),
739 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
740
741 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
742}
743
Mike Lockwood16864ba2010-05-11 17:16:59 -0400744MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500745 if (!hasStorage())
746 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800747 if (mRequest.getParameterCount() < 1)
748 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400749 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700750 MtpObjectInfo info(handle);
751 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
752 if (result == MTP_RESPONSE_OK) {
753 char date[20];
754
755 mData.putUInt32(info.mStorageID);
756 mData.putUInt16(info.mFormat);
757 mData.putUInt16(info.mProtectionStatus);
758
759 // if object is being edited the database size may be out of date
760 uint32_t size = info.mCompressedSize;
761 ObjectEdit* edit = getEditObject(handle);
762 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700763 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700764 mData.putUInt32(size);
765
766 mData.putUInt16(info.mThumbFormat);
767 mData.putUInt32(info.mThumbCompressedSize);
768 mData.putUInt32(info.mThumbPixWidth);
769 mData.putUInt32(info.mThumbPixHeight);
770 mData.putUInt32(info.mImagePixWidth);
771 mData.putUInt32(info.mImagePixHeight);
772 mData.putUInt32(info.mImagePixDepth);
773 mData.putUInt32(info.mParent);
774 mData.putUInt16(info.mAssociationType);
775 mData.putUInt32(info.mAssociationDesc);
776 mData.putUInt32(info.mSequenceNumber);
777 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700778 formatDateTime(info.mDateCreated, date, sizeof(date));
779 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700780 formatDateTime(info.mDateModified, date, sizeof(date));
781 mData.putString(date); // date modified
782 mData.putEmptyString(); // keywords
783 }
784 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400785}
786
787MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500788 if (!hasStorage())
789 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -0800790 if (mRequest.getParameterCount() < 1)
791 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400792 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400793 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400794 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800795 MtpObjectFormat format;
796 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400797 if (result != MTP_RESPONSE_OK)
798 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400799
Jerry Zhang487be612016-10-24 12:10:41 -0700800 auto start = std::chrono::steady_clock::now();
801
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400802 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400803 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400804 mfr.fd = open(filePath, O_RDONLY);
805 if (mfr.fd < 0) {
806 return MTP_RESPONSE_GENERAL_ERROR;
807 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400808 mfr.offset = 0;
809 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400810 mfr.command = mRequest.getOperationCode();
811 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400812
813 // then transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700814 int ret = sHandle->sendFile(mfr);
tao.pei07a9e542015-07-17 17:18:41 +0800815 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -0700816 ALOGE("Mtp send file got error %s", strerror(errno));
tao.pei07a9e542015-07-17 17:18:41 +0800817 if (errno == ECANCELED) {
818 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
819 } else {
820 result = MTP_RESPONSE_GENERAL_ERROR;
821 }
822 } else {
823 result = MTP_RESPONSE_OK;
824 }
825
Jerry Zhang487be612016-10-24 12:10:41 -0700826 auto end = std::chrono::steady_clock::now();
827 std::chrono::duration<double> diff = end - start;
828 struct stat sstat;
829 fstat(mfr.fd, &sstat);
830 uint64_t finalsize = sstat.st_size;
831 ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
832 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwoodc6588762010-06-22 15:03:53 -0400833 close(mfr.fd);
tao.pei07a9e542015-07-17 17:18:41 +0800834 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400835}
836
Mike Lockwood64000782011-04-24 18:40:17 -0700837MtpResponseCode MtpServer::doGetThumb() {
Mike Lockwoodab063842014-11-12 14:20:06 -0800838 if (mRequest.getParameterCount() < 1)
839 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood64000782011-04-24 18:40:17 -0700840 MtpObjectHandle handle = mRequest.getParameter(1);
841 size_t thumbSize;
842 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
843 if (thumb) {
844 // send data
845 mData.setOperationCode(mRequest.getOperationCode());
846 mData.setTransactionID(mRequest.getTransactionID());
Jerry Zhang487be612016-10-24 12:10:41 -0700847 mData.writeData(sHandle, thumb, thumbSize);
Mike Lockwood64000782011-04-24 18:40:17 -0700848 free(thumb);
849 return MTP_RESPONSE_OK;
850 } else {
851 return MTP_RESPONSE_GENERAL_ERROR;
852 }
853}
854
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700855MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500856 if (!hasStorage())
857 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500858 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700859 uint64_t offset;
860 uint32_t length;
861 offset = mRequest.getParameter(2);
862 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800863 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 takes 4 arguments
864 if (mRequest.getParameterCount() < 4)
865 return MTP_RESPONSE_INVALID_PARAMETER;
866
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700867 // android extension with 64 bit offset
868 uint64_t offset2 = mRequest.getParameter(3);
869 offset = offset | (offset2 << 32);
870 length = mRequest.getParameter(4);
871 } else {
Mike Lockwoode48cf5b2014-12-17 12:22:36 -0800872 // MTP_OPERATION_GET_PARTIAL_OBJECT takes 3 arguments
873 if (mRequest.getParameterCount() < 3)
874 return MTP_RESPONSE_INVALID_PARAMETER;
875
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700876 // standard GetPartialObject
877 length = mRequest.getParameter(3);
878 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500879 MtpString pathBuf;
880 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800881 MtpObjectFormat format;
882 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500883 if (result != MTP_RESPONSE_OK)
884 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700885 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500886 length = fileLength - offset;
887
888 const char* filePath = (const char *)pathBuf;
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700889 ALOGV("sending partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500890 mtp_file_range mfr;
891 mfr.fd = open(filePath, O_RDONLY);
892 if (mfr.fd < 0) {
893 return MTP_RESPONSE_GENERAL_ERROR;
894 }
895 mfr.offset = offset;
896 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400897 mfr.command = mRequest.getOperationCode();
898 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500899 mResponse.setParameter(1, length);
900
Mike Lockwoodef441d92011-07-14 21:00:02 -0400901 // transfer the file
Jerry Zhang487be612016-10-24 12:10:41 -0700902 int ret = sHandle->sendFile(mfr);
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
tao.pei07a9e542015-07-17 17:18:41 +0800904 result = MTP_RESPONSE_OK;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500905 if (ret < 0) {
906 if (errno == ECANCELED)
tao.pei07a9e542015-07-17 17:18:41 +0800907 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500908 else
tao.pei07a9e542015-07-17 17:18:41 +0800909 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500910 }
tao.pei07a9e542015-07-17 17:18:41 +0800911 close(mfr.fd);
912 return result;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500913}
914
Mike Lockwood16864ba2010-05-11 17:16:59 -0400915MtpResponseCode MtpServer::doSendObjectInfo() {
916 MtpString path;
Mike Lockwoodab063842014-11-12 14:20:06 -0800917 uint16_t temp16;
918 uint32_t temp32;
919
920 if (mRequest.getParameterCount() < 2)
921 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400922 MtpStorageID storageID = mRequest.getParameter(1);
923 MtpStorage* storage = getStorage(storageID);
924 MtpObjectHandle parent = mRequest.getParameter(2);
925 if (!storage)
926 return MTP_RESPONSE_INVALID_STORAGE_ID;
927
928 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400929 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400930 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400931 parent = 0;
932 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800933 int64_t length;
934 MtpObjectFormat format;
935 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400936 if (result != MTP_RESPONSE_OK)
937 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800938 if (format != MTP_FORMAT_ASSOCIATION)
939 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400940 }
941
942 // read only the fields we need
Mike Lockwoodab063842014-11-12 14:20:06 -0800943 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // storage ID
944 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
945 MtpObjectFormat format = temp16;
946 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // protection status
947 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
948 mSendObjectFileSize = temp32;
949 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb format
950 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb compressed size
951 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix width
952 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // thumb pix height
953 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix width
954 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image pix height
955 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // image bit depth
956 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // parent
957 if (!mData.getUInt16(temp16)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800958 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwoodab063842014-11-12 14:20:06 -0800959 if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
Mike Lockwood16864ba2010-05-11 17:16:59 -0400960 MtpStringBuffer name, created, modified;
Mike Lockwoodab063842014-11-12 14:20:06 -0800961 if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
Marco Nelissen7ea72dc2016-09-19 14:08:16 -0700962 if (name.getCharCount() == 0) {
963 ALOGE("empty name");
964 return MTP_RESPONSE_INVALID_PARAMETER;
965 }
Mike Lockwoodab063842014-11-12 14:20:06 -0800966 if (!mData.getString(created)) return MTP_RESPONSE_INVALID_PARAMETER; // date created
967 if (!mData.getString(modified)) return MTP_RESPONSE_INVALID_PARAMETER; // date modified
Mike Lockwood16864ba2010-05-11 17:16:59 -0400968 // keywords follow
969
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400971 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400972 if (!parseDateTime(modified, modifiedTime))
973 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400974
975 if (path[path.size() - 1] != '/')
976 path += "/";
977 path += (const char *)name;
978
Mike Lockwood20c3be02010-12-12 12:17:43 -0800979 // check space first
980 if (mSendObjectFileSize > storage->getFreeSpace())
981 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400982 uint64_t maxFileSize = storage->getMaxFileSize();
983 // check storage max file size
984 if (maxFileSize != 0) {
985 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
986 // is >= 0xFFFFFFFF
987 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
988 return MTP_RESPONSE_OBJECT_TOO_LARGE;
989 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800990
Steve Blockb8a80522011-12-20 16:23:08 +0000991 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400992 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
993 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400994 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400995 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400996 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400997
998 if (format == MTP_FORMAT_ASSOCIATION) {
999 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001000 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001001 umask(mask);
1002 if (ret && ret != -EEXIST)
1003 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001004 chown((const char *)path, getuid(), mFileGroup);
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
1023MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001024 if (!hasStorage())
1025 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -04001026 MtpResponseCode result = MTP_RESPONSE_OK;
1027 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001028 int ret, initialData;
tao.pei07a9e542015-07-17 17:18:41 +08001029 bool isCanceled = false;
Manoj Gupta33587d12017-04-18 16:41:09 -07001030 struct stat sstat = {};
Mike Lockwood4714b072010-07-12 08:49:01 -04001031
Jerry Zhang487be612016-10-24 12:10:41 -07001032 auto start = std::chrono::steady_clock::now();
1033
Mike Lockwood16864ba2010-05-11 17:16:59 -04001034 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +00001035 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -04001036 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
1037 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001038 }
1039
Mike Lockwoodef441d92011-07-14 21:00:02 -04001040 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001041 ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001042 if (ret < MTP_CONTAINER_HEADER_SIZE) {
1043 result = MTP_RESPONSE_GENERAL_ERROR;
1044 goto done;
1045 }
1046 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001047
1048 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -07001049 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001050 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -04001051 result = MTP_RESPONSE_GENERAL_ERROR;
1052 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -04001053 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001054 fchown(mfr.fd, getuid(), mFileGroup);
1055 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -04001056 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001057 fchmod(mfr.fd, mFilePermission);
1058 umask(mask);
1059
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001060 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -04001061 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001062 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001063
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001064 if (ret < 0) {
1065 ALOGE("failed to write initial data");
1066 result = MTP_RESPONSE_GENERAL_ERROR;
1067 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001068 mfr.offset = initialData;
1069 if (mSendObjectFileSize == 0xFFFFFFFF) {
1070 // tell driver to read until it receives a short packet
1071 mfr.length = 0xFFFFFFFF;
1072 } else {
1073 mfr.length = mSendObjectFileSize - initialData;
1074 }
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001075
Jerry Zhang54107562017-05-15 11:54:19 -07001076 mfr.command = 0;
1077 mfr.transaction_id = 0;
Yunlian Jiang8ddc3522017-02-21 15:58:09 -08001078
Jerry Zhang54107562017-05-15 11:54:19 -07001079 // transfer the file
1080 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1081 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1082 if ((ret < 0) && (errno == ECANCELED)) {
1083 isCanceled = true;
Mike Lockwood0cc79c62011-10-13 11:38:20 -04001084 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001085 }
caozhiyuan854cb172017-04-26 16:52:30 +08001086
1087 if (mSendObjectModifiedTime) {
1088 struct timespec newTime[2];
1089 newTime[0].tv_nsec = UTIME_NOW;
1090 newTime[1].tv_sec = mSendObjectModifiedTime;
1091 newTime[1].tv_nsec = 0;
1092 if (futimens(mfr.fd, newTime) < 0) {
1093 ALOGW("changing modified time failed, %s", strerror(errno));
1094 }
1095 }
1096
Jerry Zhang487be612016-10-24 12:10:41 -07001097 fstat(mfr.fd, &sstat);
Mike Lockwoodc6588762010-06-22 15:03:53 -04001098 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -04001099
Mike Lockwood916076c2010-06-04 09:49:21 -04001100 if (ret < 0) {
Jerry Zhang487be612016-10-24 12:10:41 -07001101 ALOGE("Mtp receive file got error %s", strerror(errno));
Mike Lockwood916076c2010-06-04 09:49:21 -04001102 unlink(mSendObjectFilePath);
tao.pei07a9e542015-07-17 17:18:41 +08001103 if (isCanceled)
Mike Lockwood4714b072010-07-12 08:49:01 -04001104 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -04001105 else
Mike Lockwood4714b072010-07-12 08:49:01 -04001106 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -04001107 }
Mike Lockwood4714b072010-07-12 08:49:01 -04001108
1109done:
Mike Lockwoodef441d92011-07-14 21:00:02 -04001110 // reset so we don't attempt to send the data back
1111 mData.reset();
1112
Mike Lockwood4714b072010-07-12 08:49:01 -04001113 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -08001114 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001115 mSendObjectHandle = kInvalidObjectHandle;
1116 mSendObjectFormat = 0;
caozhiyuan854cb172017-04-26 16:52:30 +08001117 mSendObjectModifiedTime = 0;
Jerry Zhang487be612016-10-24 12:10:41 -07001118
1119 auto end = std::chrono::steady_clock::now();
1120 std::chrono::duration<double> diff = end - start;
1121 uint64_t finalsize = sstat.st_size;
1122 ALOGV("Got a file over MTP. Time: %fs, Size: %" PRIu64 ", Rate: %f bytes/s",
1123 diff.count(), finalsize, ((double) finalsize) / diff.count());
Mike Lockwood4714b072010-07-12 08:49:01 -04001124 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001125}
1126
Mike Lockwoodd3211492010-09-13 17:15:58 -04001127static void deleteRecursive(const char* path) {
1128 char pathbuf[PATH_MAX];
Mark Salyzynd239cb62014-06-18 16:32:27 -07001129 size_t pathLength = strlen(path);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001130 if (pathLength >= sizeof(pathbuf) - 1) {
Steve Block29357bc2012-01-06 19:20:56 +00001131 ALOGE("path too long: %s\n", path);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001132 }
1133 strcpy(pathbuf, path);
1134 if (pathbuf[pathLength - 1] != '/') {
1135 pathbuf[pathLength++] = '/';
1136 }
1137 char* fileSpot = pathbuf + pathLength;
1138 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
1139
1140 DIR* dir = opendir(path);
1141 if (!dir) {
Steve Block29357bc2012-01-06 19:20:56 +00001142 ALOGE("opendir %s failed: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001143 return;
1144 }
1145
1146 struct dirent* entry;
1147 while ((entry = readdir(dir))) {
1148 const char* name = entry->d_name;
1149
1150 // ignore "." and ".."
1151 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
1152 continue;
1153 }
1154
1155 int nameLength = strlen(name);
1156 if (nameLength > pathRemaining) {
Steve Block29357bc2012-01-06 19:20:56 +00001157 ALOGE("path %s/%s too long\n", path, name);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001158 continue;
1159 }
1160 strcpy(fileSpot, name);
1161
Mike Lockwoodd3211492010-09-13 17:15:58 -04001162 if (entry->d_type == DT_DIR) {
1163 deleteRecursive(pathbuf);
1164 rmdir(pathbuf);
1165 } else {
1166 unlink(pathbuf);
1167 }
1168 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -05001169 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001170}
1171
1172static void deletePath(const char* path) {
1173 struct stat statbuf;
1174 if (stat(path, &statbuf) == 0) {
1175 if (S_ISDIR(statbuf.st_mode)) {
1176 deleteRecursive(path);
1177 rmdir(path);
1178 } else {
1179 unlink(path);
1180 }
1181 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001182 ALOGE("deletePath stat failed for %s: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001183 }
1184}
1185
Mike Lockwood16864ba2010-05-11 17:16:59 -04001186MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001187 if (!hasStorage())
1188 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Marco Nelissenea9f2152015-01-23 10:55:25 -08001189 if (mRequest.getParameterCount() < 1)
Mike Lockwoodab063842014-11-12 14:20:06 -08001190 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001191 MtpObjectHandle handle = mRequest.getParameter(1);
Marco Nelissenea9f2152015-01-23 10:55:25 -08001192 MtpObjectFormat format;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001193 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1194 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001195
1196 MtpString filePath;
1197 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001198 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001199 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001201 result = mDatabase->deleteFile(handle);
1202 // Don't delete the actual files unless the database deletion is allowed
1203 if (result == MTP_RESPONSE_OK) {
1204 deletePath((const char *)filePath);
1205 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001206 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001207
1208 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001209}
1210
1211MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001212 if (mRequest.getParameterCount() < 2)
1213 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001214 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001215 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001217 MtpDebug::getFormatCodeName(format));
1218 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001219 if (!property)
1220 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001221 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001222 delete property;
1223 return MTP_RESPONSE_OK;
1224}
1225
1226MtpResponseCode MtpServer::doGetDevicePropDesc() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001227 if (mRequest.getParameterCount() < 1)
1228 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood8277cec2010-08-10 15:20:35 -04001229 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001231 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1232 if (!property)
1233 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1234 property->write(mData);
1235 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001236 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001237}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001238
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001239MtpResponseCode MtpServer::doSendPartialObject() {
1240 if (!hasStorage())
1241 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodab063842014-11-12 14:20:06 -08001242 if (mRequest.getParameterCount() < 4)
1243 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001244 MtpObjectHandle handle = mRequest.getParameter(1);
1245 uint64_t offset = mRequest.getParameter(2);
1246 uint64_t offset2 = mRequest.getParameter(3);
1247 offset = offset | (offset2 << 32);
1248 uint32_t length = mRequest.getParameter(4);
1249
1250 ObjectEdit* edit = getEditObject(handle);
1251 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001252 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001253 return MTP_RESPONSE_GENERAL_ERROR;
1254 }
1255
1256 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001257 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001258 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1259 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001260 return MTP_RESPONSE_GENERAL_ERROR;
1261 }
1262
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001263 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001264 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001265
Mike Lockwoodef441d92011-07-14 21:00:02 -04001266 // read the header, and possibly some data
Jerry Zhang487be612016-10-24 12:10:41 -07001267 int ret = mData.read(sHandle);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001268 if (ret < MTP_CONTAINER_HEADER_SIZE)
1269 return MTP_RESPONSE_GENERAL_ERROR;
1270 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1271
1272 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001273 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001274 offset += initialData;
1275 length -= initialData;
1276 }
1277
tao.pei07a9e542015-07-17 17:18:41 +08001278 bool isCanceled = false;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001279 if (ret < 0) {
1280 ALOGE("failed to write initial data");
1281 } else {
Jerry Zhang54107562017-05-15 11:54:19 -07001282 mtp_file_range mfr;
1283 mfr.fd = edit->mFD;
1284 mfr.offset = offset;
1285 mfr.length = length;
1286 mfr.command = 0;
1287 mfr.transaction_id = 0;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001288
Jerry Zhang54107562017-05-15 11:54:19 -07001289 // transfer the file
1290 ret = sHandle->receiveFile(mfr, mfr.length == 0 &&
1291 initialData == MTP_BUFFER_SIZE - MTP_CONTAINER_HEADER_SIZE);
1292 if ((ret < 0) && (errno == ECANCELED)) {
1293 isCanceled = true;
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001294 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001295 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001296 if (ret < 0) {
1297 mResponse.setParameter(1, 0);
tao.pei07a9e542015-07-17 17:18:41 +08001298 if (isCanceled)
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001299 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1300 else
1301 return MTP_RESPONSE_GENERAL_ERROR;
1302 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001303
1304 // reset so we don't attempt to send this back
1305 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001306 mResponse.setParameter(1, length);
1307 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001308 if (end > edit->mSize) {
1309 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001310 }
1311 return MTP_RESPONSE_OK;
1312}
1313
1314MtpResponseCode MtpServer::doTruncateObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001315 if (mRequest.getParameterCount() < 3)
1316 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001317 MtpObjectHandle handle = mRequest.getParameter(1);
1318 ObjectEdit* edit = getEditObject(handle);
1319 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001320 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001321 return MTP_RESPONSE_GENERAL_ERROR;
1322 }
1323
1324 uint64_t offset = mRequest.getParameter(2);
1325 uint64_t offset2 = mRequest.getParameter(3);
1326 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001327 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001328 return MTP_RESPONSE_GENERAL_ERROR;
1329 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001330 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001331 return MTP_RESPONSE_OK;
1332 }
1333}
1334
1335MtpResponseCode MtpServer::doBeginEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001336 if (mRequest.getParameterCount() < 1)
1337 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001338 MtpObjectHandle handle = mRequest.getParameter(1);
1339 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001340 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001341 return MTP_RESPONSE_GENERAL_ERROR;
1342 }
1343
1344 MtpString path;
1345 int64_t fileLength;
1346 MtpObjectFormat format;
1347 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1348 if (result != MTP_RESPONSE_OK)
1349 return result;
1350
1351 int fd = open((const char *)path, O_RDWR | O_EXCL);
1352 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001353 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001354 return MTP_RESPONSE_GENERAL_ERROR;
1355 }
1356
1357 addEditObject(handle, path, fileLength, format, fd);
1358 return MTP_RESPONSE_OK;
1359}
1360
1361MtpResponseCode MtpServer::doEndEditObject() {
Mike Lockwoodab063842014-11-12 14:20:06 -08001362 if (mRequest.getParameterCount() < 1)
1363 return MTP_RESPONSE_INVALID_PARAMETER;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001364 MtpObjectHandle handle = mRequest.getParameter(1);
1365 ObjectEdit* edit = getEditObject(handle);
1366 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001367 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001368 return MTP_RESPONSE_GENERAL_ERROR;
1369 }
1370
1371 commitEdit(edit);
1372 removeEditObject(handle);
1373 return MTP_RESPONSE_OK;
1374}
1375
Mike Lockwood7850ef92010-05-14 10:10:36 -04001376} // namespace android