blob: aa4396748b3109a92d13db0362f90375f1caecf4 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/stat.h>
22#include <fcntl.h>
Mark Salyzyndb43b342014-04-04 14:47:28 -070023#include <inttypes.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040024#include <errno.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040025#include <sys/stat.h>
26#include <dirent.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040027
Mike Lockwoodc42aa122010-06-14 17:58:08 -070028#include <cutils/properties.h>
29
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"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070034#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040035#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040036#include "MtpServer.h"
37#include "MtpStorage.h"
38#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040039
Mike Lockwood8065e202010-07-15 13:36:52 -040040#include <linux/usb/f_mtp.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040041
Mike Lockwood7850ef92010-05-14 10:10:36 -040042namespace android {
43
Mike Lockwood16864ba2010-05-11 17:16:59 -040044static const MtpOperationCode kSupportedOperationCodes[] = {
45 MTP_OPERATION_GET_DEVICE_INFO,
46 MTP_OPERATION_OPEN_SESSION,
47 MTP_OPERATION_CLOSE_SESSION,
48 MTP_OPERATION_GET_STORAGE_IDS,
49 MTP_OPERATION_GET_STORAGE_INFO,
50 MTP_OPERATION_GET_NUM_OBJECTS,
51 MTP_OPERATION_GET_OBJECT_HANDLES,
52 MTP_OPERATION_GET_OBJECT_INFO,
53 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070054 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040055 MTP_OPERATION_DELETE_OBJECT,
56 MTP_OPERATION_SEND_OBJECT_INFO,
57 MTP_OPERATION_SEND_OBJECT,
58// MTP_OPERATION_INITIATE_CAPTURE,
59// MTP_OPERATION_FORMAT_STORE,
60// MTP_OPERATION_RESET_DEVICE,
61// MTP_OPERATION_SELF_TEST,
62// MTP_OPERATION_SET_OBJECT_PROTECTION,
63// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040064 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040065 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
66 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
67 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040068// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
69// MTP_OPERATION_MOVE_OBJECT,
70// MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050071 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040072// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
73 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040074 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040075 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
76 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040077 MTP_OPERATION_GET_OBJECT_PROP_LIST,
78// MTP_OPERATION_SET_OBJECT_PROP_LIST,
79// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
80// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040081 MTP_OPERATION_GET_OBJECT_REFERENCES,
82 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040083// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070084 // Android extension for direct file IO
85 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
86 MTP_OPERATION_SEND_PARTIAL_OBJECT,
87 MTP_OPERATION_TRUNCATE_OBJECT,
88 MTP_OPERATION_BEGIN_EDIT_OBJECT,
89 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040090};
91
Mike Lockwood873871f2010-07-12 18:54:16 -040092static const MtpEventCode kSupportedEventCodes[] = {
93 MTP_EVENT_OBJECT_ADDED,
94 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050095 MTP_EVENT_STORE_ADDED,
96 MTP_EVENT_STORE_REMOVED,
Mike Lockwood0fa848d2014-03-07 13:29:59 -080097 MTP_EVENT_DEVICE_PROP_CHANGED,
Mike Lockwood873871f2010-07-12 18:54:16 -040098};
99
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400100MtpServer::MtpServer(int fd, MtpDatabase* database, bool ptp,
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400101 int fileGroup, int filePerm, int directoryPerm)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400102 : mFD(fd),
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400103 mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400104 mPtp(ptp),
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400105 mFileGroup(fileGroup),
106 mFilePermission(filePerm),
107 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400108 mSessionID(0),
109 mSessionOpen(false),
110 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400111 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400112 mSendObjectFileSize(0)
113{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400114}
115
116MtpServer::~MtpServer() {
117}
118
Mike Lockwooda8494402011-02-18 09:07:14 -0500119void MtpServer::addStorage(MtpStorage* storage) {
120 Mutex::Autolock autoLock(mMutex);
121
122 mStorages.push(storage);
123 sendStoreAdded(storage->getStorageID());
124}
125
126void MtpServer::removeStorage(MtpStorage* storage) {
127 Mutex::Autolock autoLock(mMutex);
128
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700129 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500130 if (mStorages[i] == storage) {
131 mStorages.removeAt(i);
132 sendStoreRemoved(storage->getStorageID());
133 break;
134 }
135 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400136}
137
138MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800139 if (id == 0)
140 return mStorages[0];
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700141 for (size_t i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800142 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400143 if (storage->getStorageID() == id)
144 return storage;
145 }
146 return NULL;
147}
148
Mike Lockwooda8494402011-02-18 09:07:14 -0500149bool MtpServer::hasStorage(MtpStorageID id) {
150 if (id == 0 || id == 0xFFFFFFFF)
151 return mStorages.size() > 0;
152 return (getStorage(id) != NULL);
153}
154
Mike Lockwood16864ba2010-05-11 17:16:59 -0400155void MtpServer::run() {
156 int fd = mFD;
157
Steve Block3856b092011-10-20 11:56:00 +0100158 ALOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400159
160 while (1) {
161 int ret = mRequest.read(fd);
162 if (ret < 0) {
Steve Block3856b092011-10-20 11:56:00 +0100163 ALOGV("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400164 if (errno == ECANCELED) {
165 // return to top of loop and wait for next command
166 continue;
167 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400168 break;
169 }
170 MtpOperationCode operation = mRequest.getOperationCode();
171 MtpTransactionID transaction = mRequest.getTransactionID();
172
Steve Block3856b092011-10-20 11:56:00 +0100173 ALOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400174 mRequest.dump();
175
176 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400177 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400178 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
179 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
180 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400181 if (dataIn) {
182 int ret = mData.read(fd);
183 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000184 ALOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400185 if (errno == ECANCELED) {
186 // return to top of loop and wait for next command
187 continue;
188 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400189 break;
190 }
Steve Block3856b092011-10-20 11:56:00 +0100191 ALOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400192 mData.dump();
193 } else {
194 mData.reset();
195 }
196
Mike Lockwood916076c2010-06-04 09:49:21 -0400197 if (handleRequest()) {
198 if (!dataIn && mData.hasData()) {
199 mData.setOperationCode(operation);
200 mData.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("sending data:");
Mike Lockwood23d20712010-10-11 17:31:44 -0400202 mData.dump();
Mike Lockwood916076c2010-06-04 09:49:21 -0400203 ret = mData.write(fd);
204 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000205 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400206 if (errno == ECANCELED) {
207 // return to top of loop and wait for next command
208 continue;
209 }
210 break;
211 }
212 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400213
Mike Lockwood916076c2010-06-04 09:49:21 -0400214 mResponse.setTransactionID(transaction);
Steve Block3856b092011-10-20 11:56:00 +0100215 ALOGV("sending response %04X", mResponse.getResponseCode());
Mike Lockwood916076c2010-06-04 09:49:21 -0400216 ret = mResponse.write(fd);
Mike Lockwood23d20712010-10-11 17:31:44 -0400217 mResponse.dump();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400218 if (ret < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400220 if (errno == ECANCELED) {
221 // return to top of loop and wait for next command
222 continue;
223 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400224 break;
225 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400226 } else {
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400228 }
229 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400230
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700231 // commit any open edits
232 int count = mObjectEditList.size();
233 for (int i = 0; i < count; i++) {
234 ObjectEdit* edit = mObjectEditList[i];
235 commitEdit(edit);
236 delete edit;
237 }
238 mObjectEditList.clear();
239
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400240 if (mSessionOpen)
241 mDatabase->sessionEnded();
Mike Lockwooddec73882011-07-11 15:04:38 -0400242 close(fd);
243 mFD = -1;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400244}
245
Mike Lockwood873871f2010-07-12 18:54:16 -0400246void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("sendObjectAdded %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500248 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400249}
250
251void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("sendObjectRemoved %d\n", handle);
Mike Lockwooda8494402011-02-18 09:07:14 -0500253 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
254}
255
256void MtpServer::sendStoreAdded(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("sendStoreAdded %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500258 sendEvent(MTP_EVENT_STORE_ADDED, id);
259}
260
261void MtpServer::sendStoreRemoved(MtpStorageID id) {
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("sendStoreRemoved %08X\n", id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500263 sendEvent(MTP_EVENT_STORE_REMOVED, id);
264}
265
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800266void MtpServer::sendDevicePropertyChanged(MtpDeviceProperty property) {
267 ALOGV("sendDevicePropertyChanged %d\n", property);
268 sendEvent(MTP_EVENT_DEVICE_PROP_CHANGED, property);
269}
270
Mike Lockwooda8494402011-02-18 09:07:14 -0500271void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400272 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500273 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400274 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500275 mEvent.setParameter(1, param1);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400276 int ret = mEvent.write(mFD);
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("mEvent.write returned %d\n", ret);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400278 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400279}
280
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700281void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
282 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700283 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700284 mObjectEditList.add(edit);
285}
286
287MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
288 int count = mObjectEditList.size();
289 for (int i = 0; i < count; i++) {
290 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700291 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700292 }
293 return NULL;
294}
295
296void MtpServer::removeEditObject(MtpObjectHandle handle) {
297 int count = mObjectEditList.size();
298 for (int i = 0; i < count; i++) {
299 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700300 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700301 delete edit;
302 mObjectEditList.removeAt(i);
303 return;
304 }
305 }
Steve Block29357bc2012-01-06 19:20:56 +0000306 ALOGE("ObjectEdit not found in removeEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700307}
308
309void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700310 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700311}
312
313
Mike Lockwood916076c2010-06-04 09:49:21 -0400314bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500315 Mutex::Autolock autoLock(mMutex);
316
Mike Lockwood16864ba2010-05-11 17:16:59 -0400317 MtpOperationCode operation = mRequest.getOperationCode();
318 MtpResponseCode response;
319
320 mResponse.reset();
321
322 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
323 // FIXME - need to delete mSendObjectHandle from the database
Steve Block29357bc2012-01-06 19:20:56 +0000324 ALOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400325 mSendObjectHandle = kInvalidObjectHandle;
326 }
327
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700328 int containertype = mRequest.getContainerType();
329 if (containertype != MTP_CONTAINER_TYPE_COMMAND) {
330 ALOGE("wrong container type %d", containertype);
331 return false;
332 }
333
334 ALOGV("got command %s (%x)", MtpDebug::getOperationCodeName(operation), operation);
335
Mike Lockwood16864ba2010-05-11 17:16:59 -0400336 switch (operation) {
337 case MTP_OPERATION_GET_DEVICE_INFO:
338 response = doGetDeviceInfo();
339 break;
340 case MTP_OPERATION_OPEN_SESSION:
341 response = doOpenSession();
342 break;
343 case MTP_OPERATION_CLOSE_SESSION:
344 response = doCloseSession();
345 break;
346 case MTP_OPERATION_GET_STORAGE_IDS:
347 response = doGetStorageIDs();
348 break;
349 case MTP_OPERATION_GET_STORAGE_INFO:
350 response = doGetStorageInfo();
351 break;
352 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
353 response = doGetObjectPropsSupported();
354 break;
355 case MTP_OPERATION_GET_OBJECT_HANDLES:
356 response = doGetObjectHandles();
357 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400358 case MTP_OPERATION_GET_NUM_OBJECTS:
359 response = doGetNumObjects();
360 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400361 case MTP_OPERATION_GET_OBJECT_REFERENCES:
362 response = doGetObjectReferences();
363 break;
364 case MTP_OPERATION_SET_OBJECT_REFERENCES:
365 response = doSetObjectReferences();
366 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400367 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
368 response = doGetObjectPropValue();
369 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400370 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
371 response = doSetObjectPropValue();
372 break;
373 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
374 response = doGetDevicePropValue();
375 break;
376 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
377 response = doSetDevicePropValue();
378 break;
379 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
380 response = doResetDevicePropValue();
381 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400382 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
383 response = doGetObjectPropList();
384 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400385 case MTP_OPERATION_GET_OBJECT_INFO:
386 response = doGetObjectInfo();
387 break;
388 case MTP_OPERATION_GET_OBJECT:
389 response = doGetObject();
390 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700391 case MTP_OPERATION_GET_THUMB:
392 response = doGetThumb();
393 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500394 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700395 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
396 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500397 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400398 case MTP_OPERATION_SEND_OBJECT_INFO:
399 response = doSendObjectInfo();
400 break;
401 case MTP_OPERATION_SEND_OBJECT:
402 response = doSendObject();
403 break;
404 case MTP_OPERATION_DELETE_OBJECT:
405 response = doDeleteObject();
406 break;
407 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400408 response = doGetObjectPropDesc();
409 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400410 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
411 response = doGetDevicePropDesc();
412 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700413 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
414 response = doSendPartialObject();
415 break;
416 case MTP_OPERATION_TRUNCATE_OBJECT:
417 response = doTruncateObject();
418 break;
419 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
420 response = doBeginEditObject();
421 break;
422 case MTP_OPERATION_END_EDIT_OBJECT:
423 response = doEndEditObject();
424 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400425 default:
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700426 ALOGE("got unsupported command %s (%x)",
427 MtpDebug::getOperationCodeName(operation), operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400428 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
429 break;
430 }
431
Mike Lockwood916076c2010-06-04 09:49:21 -0400432 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
433 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400434 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400435 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400436}
437
438MtpResponseCode MtpServer::doGetDeviceInfo() {
439 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700440 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400441
Mike Lockwood782aef12010-08-10 07:37:50 -0400442 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
443 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
444 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
445
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 // fill in device info
447 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400448 if (mPtp) {
449 mData.putUInt32(0);
450 } else {
451 // MTP Vendor Extension ID
452 mData.putUInt32(6);
453 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400454 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400455 if (mPtp) {
456 // no extensions
457 string.set("");
458 } else {
459 // MTP extensions
460 string.set("microsoft.com: 1.0; android.com: 1.0;");
461 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400462 mData.putString(string); // MTP Extensions
463 mData.putUInt16(0); //Functional Mode
464 mData.putAUInt16(kSupportedOperationCodes,
465 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400466 mData.putAUInt16(kSupportedEventCodes,
467 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400468 mData.putAUInt16(deviceProperties); // Device Properties Supported
469 mData.putAUInt16(captureFormats); // Capture Formats
470 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500471
472 property_get("ro.product.manufacturer", prop_value, "unknown manufacturer");
473 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700475
476 property_get("ro.product.model", prop_value, "MTP Device");
477 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400478 mData.putString(string); // Model
479 string.set("1.0");
480 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700481
482 property_get("ro.serialno", prop_value, "????????");
483 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400484 mData.putString(string); // Serial Number
485
Mike Lockwood782aef12010-08-10 07:37:50 -0400486 delete playbackFormats;
487 delete captureFormats;
488 delete deviceProperties;
489
Mike Lockwood16864ba2010-05-11 17:16:59 -0400490 return MTP_RESPONSE_OK;
491}
492
493MtpResponseCode MtpServer::doOpenSession() {
494 if (mSessionOpen) {
495 mResponse.setParameter(1, mSessionID);
496 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
497 }
498 mSessionID = mRequest.getParameter(1);
499 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400500
501 mDatabase->sessionStarted();
502
Mike Lockwood16864ba2010-05-11 17:16:59 -0400503 return MTP_RESPONSE_OK;
504}
505
506MtpResponseCode MtpServer::doCloseSession() {
507 if (!mSessionOpen)
508 return MTP_RESPONSE_SESSION_NOT_OPEN;
509 mSessionID = 0;
510 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400511 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400512 return MTP_RESPONSE_OK;
513}
514
515MtpResponseCode MtpServer::doGetStorageIDs() {
516 if (!mSessionOpen)
517 return MTP_RESPONSE_SESSION_NOT_OPEN;
518
519 int count = mStorages.size();
520 mData.putUInt32(count);
521 for (int i = 0; i < count; i++)
522 mData.putUInt32(mStorages[i]->getStorageID());
523
524 return MTP_RESPONSE_OK;
525}
526
527MtpResponseCode MtpServer::doGetStorageInfo() {
528 MtpStringBuffer string;
529
530 if (!mSessionOpen)
531 return MTP_RESPONSE_SESSION_NOT_OPEN;
532 MtpStorageID id = mRequest.getParameter(1);
533 MtpStorage* storage = getStorage(id);
534 if (!storage)
535 return MTP_RESPONSE_INVALID_STORAGE_ID;
536
537 mData.putUInt16(storage->getType());
538 mData.putUInt16(storage->getFileSystemType());
539 mData.putUInt16(storage->getAccessCapability());
540 mData.putUInt64(storage->getMaxCapacity());
541 mData.putUInt64(storage->getFreeSpace());
542 mData.putUInt32(1024*1024*1024); // Free Space in Objects
543 string.set(storage->getDescription());
544 mData.putString(string);
545 mData.putEmptyString(); // Volume Identifier
546
547 return MTP_RESPONSE_OK;
548}
549
550MtpResponseCode MtpServer::doGetObjectPropsSupported() {
551 if (!mSessionOpen)
552 return MTP_RESPONSE_SESSION_NOT_OPEN;
553 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800554 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400555 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400556 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400557 return MTP_RESPONSE_OK;
558}
559
560MtpResponseCode MtpServer::doGetObjectHandles() {
561 if (!mSessionOpen)
562 return MTP_RESPONSE_SESSION_NOT_OPEN;
563 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400564 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400565 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400566 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500567
568 if (!hasStorage(storageID))
569 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400570
571 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
572 mData.putAUInt32(handles);
573 delete handles;
574 return MTP_RESPONSE_OK;
575}
576
Mike Lockwood343af4e2010-08-02 10:52:20 -0400577MtpResponseCode MtpServer::doGetNumObjects() {
578 if (!mSessionOpen)
579 return MTP_RESPONSE_SESSION_NOT_OPEN;
580 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
581 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
582 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400583 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500584 if (!hasStorage(storageID))
585 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400586
587 int count = mDatabase->getNumObjects(storageID, format, parent);
588 if (count >= 0) {
589 mResponse.setParameter(1, count);
590 return MTP_RESPONSE_OK;
591 } else {
592 mResponse.setParameter(1, 0);
593 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
594 }
595}
596
Mike Lockwood438344f2010-08-03 15:30:09 -0400597MtpResponseCode MtpServer::doGetObjectReferences() {
598 if (!mSessionOpen)
599 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500600 if (!hasStorage())
601 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
602 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400603
604 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400605 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400606 if (handles) {
607 mData.putAUInt32(handles);
608 delete handles;
609 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400610 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400611 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400612 return MTP_RESPONSE_OK;
613}
614
615MtpResponseCode MtpServer::doSetObjectReferences() {
616 if (!mSessionOpen)
617 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500618 if (!hasStorage())
619 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood438344f2010-08-03 15:30:09 -0400620 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500621
Mike Lockwood438344f2010-08-03 15:30:09 -0400622 MtpObjectHandleList* references = mData.getAUInt32();
623 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
624 delete references;
625 return result;
626}
627
Mike Lockwood16864ba2010-05-11 17:16:59 -0400628MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500629 if (!hasStorage())
630 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400631 MtpObjectHandle handle = mRequest.getParameter(1);
632 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100633 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400634 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400635
Mike Lockwood8277cec2010-08-10 15:20:35 -0400636 return mDatabase->getObjectPropertyValue(handle, property, mData);
637}
638
639MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500640 if (!hasStorage())
641 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400642 MtpObjectHandle handle = mRequest.getParameter(1);
643 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100644 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400645 MtpDebug::getObjectPropCodeName(property));
646
647 return mDatabase->setObjectPropertyValue(handle, property, mData);
648}
649
650MtpResponseCode MtpServer::doGetDevicePropValue() {
651 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100652 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400653 MtpDebug::getDevicePropCodeName(property));
654
655 return mDatabase->getDevicePropertyValue(property, mData);
656}
657
658MtpResponseCode MtpServer::doSetDevicePropValue() {
659 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100660 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400661 MtpDebug::getDevicePropCodeName(property));
662
663 return mDatabase->setDevicePropertyValue(property, mData);
664}
665
666MtpResponseCode MtpServer::doResetDevicePropValue() {
667 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400669 MtpDebug::getDevicePropCodeName(property));
670
671 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400672}
673
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400674MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500675 if (!hasStorage())
676 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400677
678 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500679 // use uint32_t so we can support 0xFFFFFFFF
680 uint32_t format = mRequest.getParameter(2);
681 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400682 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500683 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400685 handle, MtpDebug::getFormatCodeName(format),
686 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
687
688 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
689}
690
Mike Lockwood16864ba2010-05-11 17:16:59 -0400691MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500692 if (!hasStorage())
693 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400694 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700695 MtpObjectInfo info(handle);
696 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
697 if (result == MTP_RESPONSE_OK) {
698 char date[20];
699
700 mData.putUInt32(info.mStorageID);
701 mData.putUInt16(info.mFormat);
702 mData.putUInt16(info.mProtectionStatus);
703
704 // if object is being edited the database size may be out of date
705 uint32_t size = info.mCompressedSize;
706 ObjectEdit* edit = getEditObject(handle);
707 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700708 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700709 mData.putUInt32(size);
710
711 mData.putUInt16(info.mThumbFormat);
712 mData.putUInt32(info.mThumbCompressedSize);
713 mData.putUInt32(info.mThumbPixWidth);
714 mData.putUInt32(info.mThumbPixHeight);
715 mData.putUInt32(info.mImagePixWidth);
716 mData.putUInt32(info.mImagePixHeight);
717 mData.putUInt32(info.mImagePixDepth);
718 mData.putUInt32(info.mParent);
719 mData.putUInt16(info.mAssociationType);
720 mData.putUInt32(info.mAssociationDesc);
721 mData.putUInt32(info.mSequenceNumber);
722 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700723 formatDateTime(info.mDateCreated, date, sizeof(date));
724 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700725 formatDateTime(info.mDateModified, date, sizeof(date));
726 mData.putString(date); // date modified
727 mData.putEmptyString(); // keywords
728 }
729 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400730}
731
732MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500733 if (!hasStorage())
734 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400735 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400736 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400737 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800738 MtpObjectFormat format;
739 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400740 if (result != MTP_RESPONSE_OK)
741 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400742
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400743 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400744 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400745 mfr.fd = open(filePath, O_RDONLY);
746 if (mfr.fd < 0) {
747 return MTP_RESPONSE_GENERAL_ERROR;
748 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400749 mfr.offset = 0;
750 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400751 mfr.command = mRequest.getOperationCode();
752 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400753
754 // then transfer the file
Mike Lockwoodef441d92011-07-14 21:00:02 -0400755 int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400757 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400758 if (ret < 0) {
759 if (errno == ECANCELED)
760 return MTP_RESPONSE_TRANSACTION_CANCELLED;
761 else
762 return MTP_RESPONSE_GENERAL_ERROR;
763 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400764 return MTP_RESPONSE_OK;
765}
766
Mike Lockwood64000782011-04-24 18:40:17 -0700767MtpResponseCode MtpServer::doGetThumb() {
768 MtpObjectHandle handle = mRequest.getParameter(1);
769 size_t thumbSize;
770 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
771 if (thumb) {
772 // send data
773 mData.setOperationCode(mRequest.getOperationCode());
774 mData.setTransactionID(mRequest.getTransactionID());
775 mData.writeData(mFD, thumb, thumbSize);
776 free(thumb);
777 return MTP_RESPONSE_OK;
778 } else {
779 return MTP_RESPONSE_GENERAL_ERROR;
780 }
781}
782
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700783MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500784 if (!hasStorage())
785 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500786 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700787 uint64_t offset;
788 uint32_t length;
789 offset = mRequest.getParameter(2);
790 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
791 // android extension with 64 bit offset
792 uint64_t offset2 = mRequest.getParameter(3);
793 offset = offset | (offset2 << 32);
794 length = mRequest.getParameter(4);
795 } else {
796 // standard GetPartialObject
797 length = mRequest.getParameter(3);
798 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500799 MtpString pathBuf;
800 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800801 MtpObjectFormat format;
802 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500803 if (result != MTP_RESPONSE_OK)
804 return result;
Mark Salyzynd239cb62014-06-18 16:32:27 -0700805 if (offset + length > (uint64_t)fileLength)
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500806 length = fileLength - offset;
807
808 const char* filePath = (const char *)pathBuf;
809 mtp_file_range mfr;
810 mfr.fd = open(filePath, O_RDONLY);
811 if (mfr.fd < 0) {
812 return MTP_RESPONSE_GENERAL_ERROR;
813 }
814 mfr.offset = offset;
815 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400816 mfr.command = mRequest.getOperationCode();
817 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500818 mResponse.setParameter(1, length);
819
Mike Lockwoodef441d92011-07-14 21:00:02 -0400820 // transfer the file
821 int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +0100822 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500823 close(mfr.fd);
824 if (ret < 0) {
825 if (errno == ECANCELED)
826 return MTP_RESPONSE_TRANSACTION_CANCELLED;
827 else
828 return MTP_RESPONSE_GENERAL_ERROR;
829 }
830 return MTP_RESPONSE_OK;
831}
832
Mike Lockwood16864ba2010-05-11 17:16:59 -0400833MtpResponseCode MtpServer::doSendObjectInfo() {
834 MtpString path;
835 MtpStorageID storageID = mRequest.getParameter(1);
836 MtpStorage* storage = getStorage(storageID);
837 MtpObjectHandle parent = mRequest.getParameter(2);
838 if (!storage)
839 return MTP_RESPONSE_INVALID_STORAGE_ID;
840
841 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400842 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400843 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400844 parent = 0;
845 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800846 int64_t length;
847 MtpObjectFormat format;
848 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400849 if (result != MTP_RESPONSE_OK)
850 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800851 if (format != MTP_FORMAT_ASSOCIATION)
852 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400853 }
854
855 // read only the fields we need
856 mData.getUInt32(); // storage ID
857 MtpObjectFormat format = mData.getUInt16();
858 mData.getUInt16(); // protection status
859 mSendObjectFileSize = mData.getUInt32();
860 mData.getUInt16(); // thumb format
861 mData.getUInt32(); // thumb compressed size
862 mData.getUInt32(); // thumb pix width
863 mData.getUInt32(); // thumb pix height
864 mData.getUInt32(); // image pix width
865 mData.getUInt32(); // image pix height
866 mData.getUInt32(); // image bit depth
867 mData.getUInt32(); // parent
868 uint16_t associationType = mData.getUInt16();
869 uint32_t associationDesc = mData.getUInt32(); // association desc
870 mData.getUInt32(); // sequence number
871 MtpStringBuffer name, created, modified;
872 mData.getString(name); // file name
873 mData.getString(created); // date created
874 mData.getString(modified); // date modified
875 // keywords follow
876
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400878 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400879 if (!parseDateTime(modified, modifiedTime))
880 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400881
882 if (path[path.size() - 1] != '/')
883 path += "/";
884 path += (const char *)name;
885
Mike Lockwood20c3be02010-12-12 12:17:43 -0800886 // check space first
887 if (mSendObjectFileSize > storage->getFreeSpace())
888 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400889 uint64_t maxFileSize = storage->getMaxFileSize();
890 // check storage max file size
891 if (maxFileSize != 0) {
892 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
893 // is >= 0xFFFFFFFF
894 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
895 return MTP_RESPONSE_OBJECT_TOO_LARGE;
896 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800897
Steve Blockb8a80522011-12-20 16:23:08 +0000898 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400899 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
900 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400901 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400902 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400903 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400904
905 if (format == MTP_FORMAT_ASSOCIATION) {
906 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400907 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400908 umask(mask);
909 if (ret && ret != -EEXIST)
910 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400911 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwoodaa952402011-01-18 11:06:19 -0800912
913 // SendObject does not get sent for directories, so call endSendObject here instead
914 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400915 } else {
916 mSendObjectFilePath = path;
917 // save the handle for the SendObject call, which should follow
918 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400919 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400920 }
921
922 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400923 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400924 mResponse.setParameter(3, handle);
925
926 return MTP_RESPONSE_OK;
927}
928
929MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500930 if (!hasStorage())
931 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -0400932 MtpResponseCode result = MTP_RESPONSE_OK;
933 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400934 int ret, initialData;
Mike Lockwood4714b072010-07-12 08:49:01 -0400935
Mike Lockwood16864ba2010-05-11 17:16:59 -0400936 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +0000937 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400938 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
939 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400940 }
941
Mike Lockwoodef441d92011-07-14 21:00:02 -0400942 // read the header, and possibly some data
943 ret = mData.read(mFD);
944 if (ret < MTP_CONTAINER_HEADER_SIZE) {
945 result = MTP_RESPONSE_GENERAL_ERROR;
946 goto done;
947 }
948 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400949
950 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700951 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400952 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400953 result = MTP_RESPONSE_GENERAL_ERROR;
954 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400955 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400956 fchown(mfr.fd, getuid(), mFileGroup);
957 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400958 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400959 fchmod(mfr.fd, mFilePermission);
960 umask(mask);
961
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700962 if (initialData > 0) {
Mike Lockwoodef441d92011-07-14 21:00:02 -0400963 ret = write(mfr.fd, mData.getData(), initialData);
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700964 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400965
Marco Nelissendcd89ec2014-06-24 10:49:08 -0700966 if (ret < 0) {
967 ALOGE("failed to write initial data");
968 result = MTP_RESPONSE_GENERAL_ERROR;
969 } else {
970 if (mSendObjectFileSize - initialData > 0) {
971 mfr.offset = initialData;
972 if (mSendObjectFileSize == 0xFFFFFFFF) {
973 // tell driver to read until it receives a short packet
974 mfr.length = 0xFFFFFFFF;
975 } else {
976 mfr.length = mSendObjectFileSize - initialData;
977 }
978
979 ALOGV("receiving %s\n", (const char *)mSendObjectFilePath);
980 // transfer the file
981 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
982 ALOGV("MTP_RECEIVE_FILE returned %d\n", ret);
Mike Lockwood0cc79c62011-10-13 11:38:20 -0400983 }
Mike Lockwoodef441d92011-07-14 21:00:02 -0400984 }
Mike Lockwoodc6588762010-06-22 15:03:53 -0400985 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400986
Mike Lockwood916076c2010-06-04 09:49:21 -0400987 if (ret < 0) {
988 unlink(mSendObjectFilePath);
989 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400990 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400991 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400992 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400993 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400994
995done:
Mike Lockwoodef441d92011-07-14 21:00:02 -0400996 // reset so we don't attempt to send the data back
997 mData.reset();
998
Mike Lockwood4714b072010-07-12 08:49:01 -0400999 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -08001000 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -04001001 mSendObjectHandle = kInvalidObjectHandle;
1002 mSendObjectFormat = 0;
1003 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001004}
1005
Mike Lockwoodd3211492010-09-13 17:15:58 -04001006static void deleteRecursive(const char* path) {
1007 char pathbuf[PATH_MAX];
Mark Salyzynd239cb62014-06-18 16:32:27 -07001008 size_t pathLength = strlen(path);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001009 if (pathLength >= sizeof(pathbuf) - 1) {
Steve Block29357bc2012-01-06 19:20:56 +00001010 ALOGE("path too long: %s\n", path);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001011 }
1012 strcpy(pathbuf, path);
1013 if (pathbuf[pathLength - 1] != '/') {
1014 pathbuf[pathLength++] = '/';
1015 }
1016 char* fileSpot = pathbuf + pathLength;
1017 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
1018
1019 DIR* dir = opendir(path);
1020 if (!dir) {
Steve Block29357bc2012-01-06 19:20:56 +00001021 ALOGE("opendir %s failed: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001022 return;
1023 }
1024
1025 struct dirent* entry;
1026 while ((entry = readdir(dir))) {
1027 const char* name = entry->d_name;
1028
1029 // ignore "." and ".."
1030 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
1031 continue;
1032 }
1033
1034 int nameLength = strlen(name);
1035 if (nameLength > pathRemaining) {
Steve Block29357bc2012-01-06 19:20:56 +00001036 ALOGE("path %s/%s too long\n", path, name);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001037 continue;
1038 }
1039 strcpy(fileSpot, name);
1040
1041 int type = entry->d_type;
1042 if (entry->d_type == DT_DIR) {
1043 deleteRecursive(pathbuf);
1044 rmdir(pathbuf);
1045 } else {
1046 unlink(pathbuf);
1047 }
1048 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -05001049 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001050}
1051
1052static void deletePath(const char* path) {
1053 struct stat statbuf;
1054 if (stat(path, &statbuf) == 0) {
1055 if (S_ISDIR(statbuf.st_mode)) {
1056 deleteRecursive(path);
1057 rmdir(path);
1058 } else {
1059 unlink(path);
1060 }
1061 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001062 ALOGE("deletePath stat failed for %s: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001063 }
1064}
1065
Mike Lockwood16864ba2010-05-11 17:16:59 -04001066MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001067 if (!hasStorage())
1068 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001069 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001070 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001071 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1072 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001073
1074 MtpString filePath;
1075 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001076 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001077 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001078 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001079 result = mDatabase->deleteFile(handle);
1080 // Don't delete the actual files unless the database deletion is allowed
1081 if (result == MTP_RESPONSE_OK) {
1082 deletePath((const char *)filePath);
1083 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001084 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001085
1086 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001087}
1088
1089MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001090 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001091 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001093 MtpDebug::getFormatCodeName(format));
1094 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001095 if (!property)
1096 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001097 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001098 delete property;
1099 return MTP_RESPONSE_OK;
1100}
1101
1102MtpResponseCode MtpServer::doGetDevicePropDesc() {
1103 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001104 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001105 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1106 if (!property)
1107 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1108 property->write(mData);
1109 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001110 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001111}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001112
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001113MtpResponseCode MtpServer::doSendPartialObject() {
1114 if (!hasStorage())
1115 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
1116 MtpObjectHandle handle = mRequest.getParameter(1);
1117 uint64_t offset = mRequest.getParameter(2);
1118 uint64_t offset2 = mRequest.getParameter(3);
1119 offset = offset | (offset2 << 32);
1120 uint32_t length = mRequest.getParameter(4);
1121
1122 ObjectEdit* edit = getEditObject(handle);
1123 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001124 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001125 return MTP_RESPONSE_GENERAL_ERROR;
1126 }
1127
1128 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001129 if (offset > edit->mSize) {
Mark Salyzynd239cb62014-06-18 16:32:27 -07001130 ALOGD("writing past end of object, offset: %" PRIu64 ", edit->mSize: %" PRIu64,
1131 offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001132 return MTP_RESPONSE_GENERAL_ERROR;
1133 }
1134
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001135 const char* filePath = (const char *)edit->mPath;
Mark Salyzynd239cb62014-06-18 16:32:27 -07001136 ALOGV("receiving partial %s %" PRIu64 " %" PRIu32, filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001137
Mike Lockwoodef441d92011-07-14 21:00:02 -04001138 // read the header, and possibly some data
1139 int ret = mData.read(mFD);
1140 if (ret < MTP_CONTAINER_HEADER_SIZE)
1141 return MTP_RESPONSE_GENERAL_ERROR;
1142 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1143
1144 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001145 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001146 offset += initialData;
1147 length -= initialData;
1148 }
1149
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001150 if (ret < 0) {
1151 ALOGE("failed to write initial data");
1152 } else {
1153 if (length > 0) {
1154 mtp_file_range mfr;
1155 mfr.fd = edit->mFD;
1156 mfr.offset = offset;
1157 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -04001158
Marco Nelissendcd89ec2014-06-24 10:49:08 -07001159 // transfer the file
1160 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
1161 ALOGV("MTP_RECEIVE_FILE returned %d", ret);
1162 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001163 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001164 if (ret < 0) {
1165 mResponse.setParameter(1, 0);
1166 if (errno == ECANCELED)
1167 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1168 else
1169 return MTP_RESPONSE_GENERAL_ERROR;
1170 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001171
1172 // reset so we don't attempt to send this back
1173 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001174 mResponse.setParameter(1, length);
1175 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001176 if (end > edit->mSize) {
1177 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001178 }
1179 return MTP_RESPONSE_OK;
1180}
1181
1182MtpResponseCode MtpServer::doTruncateObject() {
1183 MtpObjectHandle handle = mRequest.getParameter(1);
1184 ObjectEdit* edit = getEditObject(handle);
1185 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001186 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001187 return MTP_RESPONSE_GENERAL_ERROR;
1188 }
1189
1190 uint64_t offset = mRequest.getParameter(2);
1191 uint64_t offset2 = mRequest.getParameter(3);
1192 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001193 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001194 return MTP_RESPONSE_GENERAL_ERROR;
1195 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001196 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001197 return MTP_RESPONSE_OK;
1198 }
1199}
1200
1201MtpResponseCode MtpServer::doBeginEditObject() {
1202 MtpObjectHandle handle = mRequest.getParameter(1);
1203 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001204 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001205 return MTP_RESPONSE_GENERAL_ERROR;
1206 }
1207
1208 MtpString path;
1209 int64_t fileLength;
1210 MtpObjectFormat format;
1211 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1212 if (result != MTP_RESPONSE_OK)
1213 return result;
1214
1215 int fd = open((const char *)path, O_RDWR | O_EXCL);
1216 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001217 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001218 return MTP_RESPONSE_GENERAL_ERROR;
1219 }
1220
1221 addEditObject(handle, path, fileLength, format, fd);
1222 return MTP_RESPONSE_OK;
1223}
1224
1225MtpResponseCode MtpServer::doEndEditObject() {
1226 MtpObjectHandle handle = mRequest.getParameter(1);
1227 ObjectEdit* edit = getEditObject(handle);
1228 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001229 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001230 return MTP_RESPONSE_GENERAL_ERROR;
1231 }
1232
1233 commitEdit(edit);
1234 removeEditObject(handle);
1235 return MTP_RESPONSE_OK;
1236}
1237
Mike Lockwood7850ef92010-05-14 10:10:36 -04001238} // namespace android