blob: 157f2ce028d8091c44be81333b468c904d1863ad [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
328 switch (operation) {
329 case MTP_OPERATION_GET_DEVICE_INFO:
330 response = doGetDeviceInfo();
331 break;
332 case MTP_OPERATION_OPEN_SESSION:
333 response = doOpenSession();
334 break;
335 case MTP_OPERATION_CLOSE_SESSION:
336 response = doCloseSession();
337 break;
338 case MTP_OPERATION_GET_STORAGE_IDS:
339 response = doGetStorageIDs();
340 break;
341 case MTP_OPERATION_GET_STORAGE_INFO:
342 response = doGetStorageInfo();
343 break;
344 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
345 response = doGetObjectPropsSupported();
346 break;
347 case MTP_OPERATION_GET_OBJECT_HANDLES:
348 response = doGetObjectHandles();
349 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400350 case MTP_OPERATION_GET_NUM_OBJECTS:
351 response = doGetNumObjects();
352 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400353 case MTP_OPERATION_GET_OBJECT_REFERENCES:
354 response = doGetObjectReferences();
355 break;
356 case MTP_OPERATION_SET_OBJECT_REFERENCES:
357 response = doSetObjectReferences();
358 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400359 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
360 response = doGetObjectPropValue();
361 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400362 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
363 response = doSetObjectPropValue();
364 break;
365 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
366 response = doGetDevicePropValue();
367 break;
368 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
369 response = doSetDevicePropValue();
370 break;
371 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
372 response = doResetDevicePropValue();
373 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400374 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
375 response = doGetObjectPropList();
376 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400377 case MTP_OPERATION_GET_OBJECT_INFO:
378 response = doGetObjectInfo();
379 break;
380 case MTP_OPERATION_GET_OBJECT:
381 response = doGetObject();
382 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700383 case MTP_OPERATION_GET_THUMB:
384 response = doGetThumb();
385 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500386 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700387 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
388 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500389 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400390 case MTP_OPERATION_SEND_OBJECT_INFO:
391 response = doSendObjectInfo();
392 break;
393 case MTP_OPERATION_SEND_OBJECT:
394 response = doSendObject();
395 break;
396 case MTP_OPERATION_DELETE_OBJECT:
397 response = doDeleteObject();
398 break;
399 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400400 response = doGetObjectPropDesc();
401 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400402 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
403 response = doGetDevicePropDesc();
404 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700405 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
406 response = doSendPartialObject();
407 break;
408 case MTP_OPERATION_TRUNCATE_OBJECT:
409 response = doTruncateObject();
410 break;
411 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
412 response = doBeginEditObject();
413 break;
414 case MTP_OPERATION_END_EDIT_OBJECT:
415 response = doEndEditObject();
416 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400417 default:
Steve Block29357bc2012-01-06 19:20:56 +0000418 ALOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400419 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
420 break;
421 }
422
Mike Lockwood916076c2010-06-04 09:49:21 -0400423 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
424 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400425 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400426 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400427}
428
429MtpResponseCode MtpServer::doGetDeviceInfo() {
430 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700431 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400432
Mike Lockwood782aef12010-08-10 07:37:50 -0400433 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
434 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
435 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
436
Mike Lockwood16864ba2010-05-11 17:16:59 -0400437 // fill in device info
438 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400439 if (mPtp) {
440 mData.putUInt32(0);
441 } else {
442 // MTP Vendor Extension ID
443 mData.putUInt32(6);
444 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400445 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400446 if (mPtp) {
447 // no extensions
448 string.set("");
449 } else {
450 // MTP extensions
451 string.set("microsoft.com: 1.0; android.com: 1.0;");
452 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400453 mData.putString(string); // MTP Extensions
454 mData.putUInt16(0); //Functional Mode
455 mData.putAUInt16(kSupportedOperationCodes,
456 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400457 mData.putAUInt16(kSupportedEventCodes,
458 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400459 mData.putAUInt16(deviceProperties); // Device Properties Supported
460 mData.putAUInt16(captureFormats); // Capture Formats
461 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500462
463 property_get("ro.product.manufacturer", prop_value, "unknown manufacturer");
464 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400465 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700466
467 property_get("ro.product.model", prop_value, "MTP Device");
468 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400469 mData.putString(string); // Model
470 string.set("1.0");
471 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700472
473 property_get("ro.serialno", prop_value, "????????");
474 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400475 mData.putString(string); // Serial Number
476
Mike Lockwood782aef12010-08-10 07:37:50 -0400477 delete playbackFormats;
478 delete captureFormats;
479 delete deviceProperties;
480
Mike Lockwood16864ba2010-05-11 17:16:59 -0400481 return MTP_RESPONSE_OK;
482}
483
484MtpResponseCode MtpServer::doOpenSession() {
485 if (mSessionOpen) {
486 mResponse.setParameter(1, mSessionID);
487 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
488 }
489 mSessionID = mRequest.getParameter(1);
490 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400491
492 mDatabase->sessionStarted();
493
Mike Lockwood16864ba2010-05-11 17:16:59 -0400494 return MTP_RESPONSE_OK;
495}
496
497MtpResponseCode MtpServer::doCloseSession() {
498 if (!mSessionOpen)
499 return MTP_RESPONSE_SESSION_NOT_OPEN;
500 mSessionID = 0;
501 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400502 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400503 return MTP_RESPONSE_OK;
504}
505
506MtpResponseCode MtpServer::doGetStorageIDs() {
507 if (!mSessionOpen)
508 return MTP_RESPONSE_SESSION_NOT_OPEN;
509
510 int count = mStorages.size();
511 mData.putUInt32(count);
512 for (int i = 0; i < count; i++)
513 mData.putUInt32(mStorages[i]->getStorageID());
514
515 return MTP_RESPONSE_OK;
516}
517
518MtpResponseCode MtpServer::doGetStorageInfo() {
519 MtpStringBuffer string;
520
521 if (!mSessionOpen)
522 return MTP_RESPONSE_SESSION_NOT_OPEN;
523 MtpStorageID id = mRequest.getParameter(1);
524 MtpStorage* storage = getStorage(id);
525 if (!storage)
526 return MTP_RESPONSE_INVALID_STORAGE_ID;
527
528 mData.putUInt16(storage->getType());
529 mData.putUInt16(storage->getFileSystemType());
530 mData.putUInt16(storage->getAccessCapability());
531 mData.putUInt64(storage->getMaxCapacity());
532 mData.putUInt64(storage->getFreeSpace());
533 mData.putUInt32(1024*1024*1024); // Free Space in Objects
534 string.set(storage->getDescription());
535 mData.putString(string);
536 mData.putEmptyString(); // Volume Identifier
537
538 return MTP_RESPONSE_OK;
539}
540
541MtpResponseCode MtpServer::doGetObjectPropsSupported() {
542 if (!mSessionOpen)
543 return MTP_RESPONSE_SESSION_NOT_OPEN;
544 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800545 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400546 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400547 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400548 return MTP_RESPONSE_OK;
549}
550
551MtpResponseCode MtpServer::doGetObjectHandles() {
552 if (!mSessionOpen)
553 return MTP_RESPONSE_SESSION_NOT_OPEN;
554 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400555 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400556 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400557 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500558
559 if (!hasStorage(storageID))
560 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400561
562 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
563 mData.putAUInt32(handles);
564 delete handles;
565 return MTP_RESPONSE_OK;
566}
567
Mike Lockwood343af4e2010-08-02 10:52:20 -0400568MtpResponseCode MtpServer::doGetNumObjects() {
569 if (!mSessionOpen)
570 return MTP_RESPONSE_SESSION_NOT_OPEN;
571 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
572 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
573 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400574 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500575 if (!hasStorage(storageID))
576 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400577
578 int count = mDatabase->getNumObjects(storageID, format, parent);
579 if (count >= 0) {
580 mResponse.setParameter(1, count);
581 return MTP_RESPONSE_OK;
582 } else {
583 mResponse.setParameter(1, 0);
584 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
585 }
586}
587
Mike Lockwood438344f2010-08-03 15:30:09 -0400588MtpResponseCode MtpServer::doGetObjectReferences() {
589 if (!mSessionOpen)
590 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500591 if (!hasStorage())
592 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
593 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400594
595 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400596 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400597 if (handles) {
598 mData.putAUInt32(handles);
599 delete handles;
600 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400601 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400602 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400603 return MTP_RESPONSE_OK;
604}
605
606MtpResponseCode MtpServer::doSetObjectReferences() {
607 if (!mSessionOpen)
608 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500609 if (!hasStorage())
610 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood438344f2010-08-03 15:30:09 -0400611 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500612
Mike Lockwood438344f2010-08-03 15:30:09 -0400613 MtpObjectHandleList* references = mData.getAUInt32();
614 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
615 delete references;
616 return result;
617}
618
Mike Lockwood16864ba2010-05-11 17:16:59 -0400619MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500620 if (!hasStorage())
621 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400622 MtpObjectHandle handle = mRequest.getParameter(1);
623 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100624 ALOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400625 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400626
Mike Lockwood8277cec2010-08-10 15:20:35 -0400627 return mDatabase->getObjectPropertyValue(handle, property, mData);
628}
629
630MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500631 if (!hasStorage())
632 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400633 MtpObjectHandle handle = mRequest.getParameter(1);
634 MtpObjectProperty property = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +0100635 ALOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400636 MtpDebug::getObjectPropCodeName(property));
637
638 return mDatabase->setObjectPropertyValue(handle, property, mData);
639}
640
641MtpResponseCode MtpServer::doGetDevicePropValue() {
642 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100643 ALOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400644 MtpDebug::getDevicePropCodeName(property));
645
646 return mDatabase->getDevicePropertyValue(property, mData);
647}
648
649MtpResponseCode MtpServer::doSetDevicePropValue() {
650 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400652 MtpDebug::getDevicePropCodeName(property));
653
654 return mDatabase->setDevicePropertyValue(property, mData);
655}
656
657MtpResponseCode MtpServer::doResetDevicePropValue() {
658 MtpDeviceProperty property = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +0100659 ALOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400660 MtpDebug::getDevicePropCodeName(property));
661
662 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400663}
664
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400665MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500666 if (!hasStorage())
667 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400668
669 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500670 // use uint32_t so we can support 0xFFFFFFFF
671 uint32_t format = mRequest.getParameter(2);
672 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400673 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500674 int depth = mRequest.getParameter(5);
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400676 handle, MtpDebug::getFormatCodeName(format),
677 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
678
679 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
680}
681
Mike Lockwood16864ba2010-05-11 17:16:59 -0400682MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500683 if (!hasStorage())
684 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400685 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700686 MtpObjectInfo info(handle);
687 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
688 if (result == MTP_RESPONSE_OK) {
689 char date[20];
690
691 mData.putUInt32(info.mStorageID);
692 mData.putUInt16(info.mFormat);
693 mData.putUInt16(info.mProtectionStatus);
694
695 // if object is being edited the database size may be out of date
696 uint32_t size = info.mCompressedSize;
697 ObjectEdit* edit = getEditObject(handle);
698 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700699 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700700 mData.putUInt32(size);
701
702 mData.putUInt16(info.mThumbFormat);
703 mData.putUInt32(info.mThumbCompressedSize);
704 mData.putUInt32(info.mThumbPixWidth);
705 mData.putUInt32(info.mThumbPixHeight);
706 mData.putUInt32(info.mImagePixWidth);
707 mData.putUInt32(info.mImagePixHeight);
708 mData.putUInt32(info.mImagePixDepth);
709 mData.putUInt32(info.mParent);
710 mData.putUInt16(info.mAssociationType);
711 mData.putUInt32(info.mAssociationDesc);
712 mData.putUInt32(info.mSequenceNumber);
713 mData.putString(info.mName);
Mike Lockwoodec24fa42013-04-01 10:51:35 -0700714 formatDateTime(info.mDateCreated, date, sizeof(date));
715 mData.putString(date); // date created
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700716 formatDateTime(info.mDateModified, date, sizeof(date));
717 mData.putString(date); // date modified
718 mData.putEmptyString(); // keywords
719 }
720 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400721}
722
723MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500724 if (!hasStorage())
725 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400726 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400727 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400728 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800729 MtpObjectFormat format;
730 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400731 if (result != MTP_RESPONSE_OK)
732 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400733
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400734 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400735 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400736 mfr.fd = open(filePath, O_RDONLY);
737 if (mfr.fd < 0) {
738 return MTP_RESPONSE_GENERAL_ERROR;
739 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400740 mfr.offset = 0;
741 mfr.length = fileLength;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400742 mfr.command = mRequest.getOperationCode();
743 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400744
745 // then transfer the file
Mike Lockwoodef441d92011-07-14 21:00:02 -0400746 int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400748 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400749 if (ret < 0) {
750 if (errno == ECANCELED)
751 return MTP_RESPONSE_TRANSACTION_CANCELLED;
752 else
753 return MTP_RESPONSE_GENERAL_ERROR;
754 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400755 return MTP_RESPONSE_OK;
756}
757
Mike Lockwood64000782011-04-24 18:40:17 -0700758MtpResponseCode MtpServer::doGetThumb() {
759 MtpObjectHandle handle = mRequest.getParameter(1);
760 size_t thumbSize;
761 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
762 if (thumb) {
763 // send data
764 mData.setOperationCode(mRequest.getOperationCode());
765 mData.setTransactionID(mRequest.getTransactionID());
766 mData.writeData(mFD, thumb, thumbSize);
767 free(thumb);
768 return MTP_RESPONSE_OK;
769 } else {
770 return MTP_RESPONSE_GENERAL_ERROR;
771 }
772}
773
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700774MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500775 if (!hasStorage())
776 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500777 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700778 uint64_t offset;
779 uint32_t length;
780 offset = mRequest.getParameter(2);
781 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
782 // android extension with 64 bit offset
783 uint64_t offset2 = mRequest.getParameter(3);
784 offset = offset | (offset2 << 32);
785 length = mRequest.getParameter(4);
786 } else {
787 // standard GetPartialObject
788 length = mRequest.getParameter(3);
789 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500790 MtpString pathBuf;
791 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800792 MtpObjectFormat format;
793 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500794 if (result != MTP_RESPONSE_OK)
795 return result;
796 if (offset + length > fileLength)
797 length = fileLength - offset;
798
799 const char* filePath = (const char *)pathBuf;
800 mtp_file_range mfr;
801 mfr.fd = open(filePath, O_RDONLY);
802 if (mfr.fd < 0) {
803 return MTP_RESPONSE_GENERAL_ERROR;
804 }
805 mfr.offset = offset;
806 mfr.length = length;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400807 mfr.command = mRequest.getOperationCode();
808 mfr.transaction_id = mRequest.getTransactionID();
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500809 mResponse.setParameter(1, length);
810
Mike Lockwoodef441d92011-07-14 21:00:02 -0400811 // transfer the file
812 int ret = ioctl(mFD, MTP_SEND_FILE_WITH_HEADER, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +0100813 ALOGV("MTP_SEND_FILE_WITH_HEADER returned %d\n", ret);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500814 close(mfr.fd);
815 if (ret < 0) {
816 if (errno == ECANCELED)
817 return MTP_RESPONSE_TRANSACTION_CANCELLED;
818 else
819 return MTP_RESPONSE_GENERAL_ERROR;
820 }
821 return MTP_RESPONSE_OK;
822}
823
Mike Lockwood16864ba2010-05-11 17:16:59 -0400824MtpResponseCode MtpServer::doSendObjectInfo() {
825 MtpString path;
826 MtpStorageID storageID = mRequest.getParameter(1);
827 MtpStorage* storage = getStorage(storageID);
828 MtpObjectHandle parent = mRequest.getParameter(2);
829 if (!storage)
830 return MTP_RESPONSE_INVALID_STORAGE_ID;
831
832 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400833 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400834 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400835 parent = 0;
836 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800837 int64_t length;
838 MtpObjectFormat format;
839 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400840 if (result != MTP_RESPONSE_OK)
841 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800842 if (format != MTP_FORMAT_ASSOCIATION)
843 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400844 }
845
846 // read only the fields we need
847 mData.getUInt32(); // storage ID
848 MtpObjectFormat format = mData.getUInt16();
849 mData.getUInt16(); // protection status
850 mSendObjectFileSize = mData.getUInt32();
851 mData.getUInt16(); // thumb format
852 mData.getUInt32(); // thumb compressed size
853 mData.getUInt32(); // thumb pix width
854 mData.getUInt32(); // thumb pix height
855 mData.getUInt32(); // image pix width
856 mData.getUInt32(); // image pix height
857 mData.getUInt32(); // image bit depth
858 mData.getUInt32(); // parent
859 uint16_t associationType = mData.getUInt16();
860 uint32_t associationDesc = mData.getUInt32(); // association desc
861 mData.getUInt32(); // sequence number
862 MtpStringBuffer name, created, modified;
863 mData.getString(name); // file name
864 mData.getString(created); // date created
865 mData.getString(modified); // date modified
866 // keywords follow
867
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400869 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400870 if (!parseDateTime(modified, modifiedTime))
871 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400872
873 if (path[path.size() - 1] != '/')
874 path += "/";
875 path += (const char *)name;
876
Mike Lockwood20c3be02010-12-12 12:17:43 -0800877 // check space first
878 if (mSendObjectFileSize > storage->getFreeSpace())
879 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400880 uint64_t maxFileSize = storage->getMaxFileSize();
881 // check storage max file size
882 if (maxFileSize != 0) {
883 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
884 // is >= 0xFFFFFFFF
885 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
886 return MTP_RESPONSE_OBJECT_TOO_LARGE;
887 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800888
Steve Blockb8a80522011-12-20 16:23:08 +0000889 ALOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400890 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
891 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400892 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400893 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400894 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400895
896 if (format == MTP_FORMAT_ASSOCIATION) {
897 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400898 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400899 umask(mask);
900 if (ret && ret != -EEXIST)
901 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400902 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwoodaa952402011-01-18 11:06:19 -0800903
904 // SendObject does not get sent for directories, so call endSendObject here instead
905 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400906 } else {
907 mSendObjectFilePath = path;
908 // save the handle for the SendObject call, which should follow
909 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400910 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400911 }
912
913 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400914 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400915 mResponse.setParameter(3, handle);
916
917 return MTP_RESPONSE_OK;
918}
919
920MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500921 if (!hasStorage())
922 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -0400923 MtpResponseCode result = MTP_RESPONSE_OK;
924 mode_t mask;
Mike Lockwoodef441d92011-07-14 21:00:02 -0400925 int ret, initialData;
Mike Lockwood4714b072010-07-12 08:49:01 -0400926
Mike Lockwood16864ba2010-05-11 17:16:59 -0400927 if (mSendObjectHandle == kInvalidObjectHandle) {
Steve Block29357bc2012-01-06 19:20:56 +0000928 ALOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400929 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
930 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400931 }
932
Mike Lockwoodef441d92011-07-14 21:00:02 -0400933 // read the header, and possibly some data
934 ret = mData.read(mFD);
935 if (ret < MTP_CONTAINER_HEADER_SIZE) {
936 result = MTP_RESPONSE_GENERAL_ERROR;
937 goto done;
938 }
939 initialData = ret - MTP_CONTAINER_HEADER_SIZE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400940
941 mtp_file_range mfr;
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700942 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400943 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400944 result = MTP_RESPONSE_GENERAL_ERROR;
945 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400946 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400947 fchown(mfr.fd, getuid(), mFileGroup);
948 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400949 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400950 fchmod(mfr.fd, mFilePermission);
951 umask(mask);
952
Mike Lockwoodef441d92011-07-14 21:00:02 -0400953 if (initialData > 0)
954 ret = write(mfr.fd, mData.getData(), initialData);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400955
Mike Lockwoodef441d92011-07-14 21:00:02 -0400956 if (mSendObjectFileSize - initialData > 0) {
957 mfr.offset = initialData;
Mike Lockwood0cc79c62011-10-13 11:38:20 -0400958 if (mSendObjectFileSize == 0xFFFFFFFF) {
959 // tell driver to read until it receives a short packet
960 mfr.length = 0xFFFFFFFF;
961 } else {
962 mfr.length = mSendObjectFileSize - initialData;
963 }
Mike Lockwoodef441d92011-07-14 21:00:02 -0400964
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("receiving %s\n", (const char *)mSendObjectFilePath);
Mike Lockwoodef441d92011-07-14 21:00:02 -0400966 // transfer the file
967 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("MTP_RECEIVE_FILE returned %d\n", ret);
Mike Lockwoodef441d92011-07-14 21:00:02 -0400969 }
Mike Lockwoodc6588762010-06-22 15:03:53 -0400970 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400971
Mike Lockwood916076c2010-06-04 09:49:21 -0400972 if (ret < 0) {
973 unlink(mSendObjectFilePath);
974 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400975 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400976 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400977 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400978 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400979
980done:
Mike Lockwoodef441d92011-07-14 21:00:02 -0400981 // reset so we don't attempt to send the data back
982 mData.reset();
983
Mike Lockwood4714b072010-07-12 08:49:01 -0400984 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -0800985 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -0400986 mSendObjectHandle = kInvalidObjectHandle;
987 mSendObjectFormat = 0;
988 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400989}
990
Mike Lockwoodd3211492010-09-13 17:15:58 -0400991static void deleteRecursive(const char* path) {
992 char pathbuf[PATH_MAX];
993 int pathLength = strlen(path);
994 if (pathLength >= sizeof(pathbuf) - 1) {
Steve Block29357bc2012-01-06 19:20:56 +0000995 ALOGE("path too long: %s\n", path);
Mike Lockwoodd3211492010-09-13 17:15:58 -0400996 }
997 strcpy(pathbuf, path);
998 if (pathbuf[pathLength - 1] != '/') {
999 pathbuf[pathLength++] = '/';
1000 }
1001 char* fileSpot = pathbuf + pathLength;
1002 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
1003
1004 DIR* dir = opendir(path);
1005 if (!dir) {
Steve Block29357bc2012-01-06 19:20:56 +00001006 ALOGE("opendir %s failed: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001007 return;
1008 }
1009
1010 struct dirent* entry;
1011 while ((entry = readdir(dir))) {
1012 const char* name = entry->d_name;
1013
1014 // ignore "." and ".."
1015 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
1016 continue;
1017 }
1018
1019 int nameLength = strlen(name);
1020 if (nameLength > pathRemaining) {
Steve Block29357bc2012-01-06 19:20:56 +00001021 ALOGE("path %s/%s too long\n", path, name);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001022 continue;
1023 }
1024 strcpy(fileSpot, name);
1025
1026 int type = entry->d_type;
1027 if (entry->d_type == DT_DIR) {
1028 deleteRecursive(pathbuf);
1029 rmdir(pathbuf);
1030 } else {
1031 unlink(pathbuf);
1032 }
1033 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -05001034 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001035}
1036
1037static void deletePath(const char* path) {
1038 struct stat statbuf;
1039 if (stat(path, &statbuf) == 0) {
1040 if (S_ISDIR(statbuf.st_mode)) {
1041 deleteRecursive(path);
1042 rmdir(path);
1043 } else {
1044 unlink(path);
1045 }
1046 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001047 ALOGE("deletePath stat failed for %s: %s", path, strerror(errno));
Mike Lockwoodd3211492010-09-13 17:15:58 -04001048 }
1049}
1050
Mike Lockwood16864ba2010-05-11 17:16:59 -04001051MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001052 if (!hasStorage())
1053 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001054 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001055 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001056 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1057 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001058
1059 MtpString filePath;
1060 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001061 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001062 if (result == MTP_RESPONSE_OK) {
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("deleting %s", (const char *)filePath);
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001064 result = mDatabase->deleteFile(handle);
1065 // Don't delete the actual files unless the database deletion is allowed
1066 if (result == MTP_RESPONSE_OK) {
1067 deletePath((const char *)filePath);
1068 }
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001069 }
Mike Lockwooda9a46c12011-12-01 16:58:41 -05001070
1071 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001072}
1073
1074MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001075 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001076 MtpObjectFormat format = mRequest.getParameter(2);
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001078 MtpDebug::getFormatCodeName(format));
1079 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001080 if (!property)
1081 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001082 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001083 delete property;
1084 return MTP_RESPONSE_OK;
1085}
1086
1087MtpResponseCode MtpServer::doGetDevicePropDesc() {
1088 MtpDeviceProperty propCode = mRequest.getParameter(1);
Steve Block3856b092011-10-20 11:56:00 +01001089 ALOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001090 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1091 if (!property)
1092 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1093 property->write(mData);
1094 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001095 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001096}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001097
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001098MtpResponseCode MtpServer::doSendPartialObject() {
1099 if (!hasStorage())
1100 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
1101 MtpObjectHandle handle = mRequest.getParameter(1);
1102 uint64_t offset = mRequest.getParameter(2);
1103 uint64_t offset2 = mRequest.getParameter(3);
1104 offset = offset | (offset2 << 32);
1105 uint32_t length = mRequest.getParameter(4);
1106
1107 ObjectEdit* edit = getEditObject(handle);
1108 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001109 ALOGE("object not open for edit in doSendPartialObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001110 return MTP_RESPONSE_GENERAL_ERROR;
1111 }
1112
1113 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001114 if (offset > edit->mSize) {
Steve Blockb8a80522011-12-20 16:23:08 +00001115 ALOGD("writing past end of object, offset: %lld, edit->mSize: %lld", offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001116 return MTP_RESPONSE_GENERAL_ERROR;
1117 }
1118
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001119 const char* filePath = (const char *)edit->mPath;
Mark Salyzyndb43b342014-04-04 14:47:28 -07001120 ALOGV("receiving partial %s %lld %" PRIu32 "\n", filePath, offset, length);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001121
Mike Lockwoodef441d92011-07-14 21:00:02 -04001122 // read the header, and possibly some data
1123 int ret = mData.read(mFD);
1124 if (ret < MTP_CONTAINER_HEADER_SIZE)
1125 return MTP_RESPONSE_GENERAL_ERROR;
1126 int initialData = ret - MTP_CONTAINER_HEADER_SIZE;
1127
1128 if (initialData > 0) {
Mike Lockwoood0a694952013-02-08 13:25:01 -08001129 ret = pwrite(edit->mFD, mData.getData(), initialData, offset);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001130 offset += initialData;
1131 length -= initialData;
1132 }
1133
1134 if (length > 0) {
1135 mtp_file_range mfr;
1136 mfr.fd = edit->mFD;
1137 mfr.offset = offset;
1138 mfr.length = length;
1139
1140 // transfer the file
1141 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Steve Block3856b092011-10-20 11:56:00 +01001142 ALOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwoodef441d92011-07-14 21:00:02 -04001143 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001144 if (ret < 0) {
1145 mResponse.setParameter(1, 0);
1146 if (errno == ECANCELED)
1147 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1148 else
1149 return MTP_RESPONSE_GENERAL_ERROR;
1150 }
Mike Lockwoodef441d92011-07-14 21:00:02 -04001151
1152 // reset so we don't attempt to send this back
1153 mData.reset();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001154 mResponse.setParameter(1, length);
1155 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001156 if (end > edit->mSize) {
1157 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001158 }
1159 return MTP_RESPONSE_OK;
1160}
1161
1162MtpResponseCode MtpServer::doTruncateObject() {
1163 MtpObjectHandle handle = mRequest.getParameter(1);
1164 ObjectEdit* edit = getEditObject(handle);
1165 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001166 ALOGE("object not open for edit in doTruncateObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001167 return MTP_RESPONSE_GENERAL_ERROR;
1168 }
1169
1170 uint64_t offset = mRequest.getParameter(2);
1171 uint64_t offset2 = mRequest.getParameter(3);
1172 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001173 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001174 return MTP_RESPONSE_GENERAL_ERROR;
1175 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001176 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001177 return MTP_RESPONSE_OK;
1178 }
1179}
1180
1181MtpResponseCode MtpServer::doBeginEditObject() {
1182 MtpObjectHandle handle = mRequest.getParameter(1);
1183 if (getEditObject(handle)) {
Steve Block29357bc2012-01-06 19:20:56 +00001184 ALOGE("object already open for edit in doBeginEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001185 return MTP_RESPONSE_GENERAL_ERROR;
1186 }
1187
1188 MtpString path;
1189 int64_t fileLength;
1190 MtpObjectFormat format;
1191 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1192 if (result != MTP_RESPONSE_OK)
1193 return result;
1194
1195 int fd = open((const char *)path, O_RDWR | O_EXCL);
1196 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001197 ALOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001198 return MTP_RESPONSE_GENERAL_ERROR;
1199 }
1200
1201 addEditObject(handle, path, fileLength, format, fd);
1202 return MTP_RESPONSE_OK;
1203}
1204
1205MtpResponseCode MtpServer::doEndEditObject() {
1206 MtpObjectHandle handle = mRequest.getParameter(1);
1207 ObjectEdit* edit = getEditObject(handle);
1208 if (!edit) {
Steve Block29357bc2012-01-06 19:20:56 +00001209 ALOGE("object not open for edit in doEndEditObject");
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001210 return MTP_RESPONSE_GENERAL_ERROR;
1211 }
1212
1213 commitEdit(edit);
1214 removeEditObject(handle);
1215 return MTP_RESPONSE_OK;
1216}
1217
Mike Lockwood7850ef92010-05-14 10:10:36 -04001218} // namespace android