blob: 4047e2e8777e57635e4d0a419e07111ed5c3930d [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>
23#include <errno.h>
Mike Lockwoodd3211492010-09-13 17:15:58 -040024#include <sys/stat.h>
25#include <dirent.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040026
Mike Lockwoodc42aa122010-06-14 17:58:08 -070027#include <cutils/properties.h>
28
Mike Lockwooda881b442010-09-23 22:32:05 -040029#define LOG_TAG "MtpServer"
30
Mike Lockwood16864ba2010-05-11 17:16:59 -040031#include "MtpDebug.h"
Mike Lockwood7f53a192010-07-09 10:45:22 -040032#include "MtpDatabase.h"
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070033#include "MtpObjectInfo.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040034#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040035#include "MtpServer.h"
36#include "MtpStorage.h"
37#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040038
Mike Lockwood8065e202010-07-15 13:36:52 -040039#include <linux/usb/f_mtp.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040040
Mike Lockwood7850ef92010-05-14 10:10:36 -040041namespace android {
42
Mike Lockwood16864ba2010-05-11 17:16:59 -040043static const MtpOperationCode kSupportedOperationCodes[] = {
44 MTP_OPERATION_GET_DEVICE_INFO,
45 MTP_OPERATION_OPEN_SESSION,
46 MTP_OPERATION_CLOSE_SESSION,
47 MTP_OPERATION_GET_STORAGE_IDS,
48 MTP_OPERATION_GET_STORAGE_INFO,
49 MTP_OPERATION_GET_NUM_OBJECTS,
50 MTP_OPERATION_GET_OBJECT_HANDLES,
51 MTP_OPERATION_GET_OBJECT_INFO,
52 MTP_OPERATION_GET_OBJECT,
Mike Lockwood64000782011-04-24 18:40:17 -070053 MTP_OPERATION_GET_THUMB,
Mike Lockwood16864ba2010-05-11 17:16:59 -040054 MTP_OPERATION_DELETE_OBJECT,
55 MTP_OPERATION_SEND_OBJECT_INFO,
56 MTP_OPERATION_SEND_OBJECT,
57// MTP_OPERATION_INITIATE_CAPTURE,
58// MTP_OPERATION_FORMAT_STORE,
59// MTP_OPERATION_RESET_DEVICE,
60// MTP_OPERATION_SELF_TEST,
61// MTP_OPERATION_SET_OBJECT_PROTECTION,
62// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040063 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040064 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
65 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
66 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040067// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
68// MTP_OPERATION_MOVE_OBJECT,
69// MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050070 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040071// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
72 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040073 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040074 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
75 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040076 MTP_OPERATION_GET_OBJECT_PROP_LIST,
77// MTP_OPERATION_SET_OBJECT_PROP_LIST,
78// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
79// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040080 MTP_OPERATION_GET_OBJECT_REFERENCES,
81 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040082// MTP_OPERATION_SKIP,
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070083 // Android extension for direct file IO
84 MTP_OPERATION_GET_PARTIAL_OBJECT_64,
85 MTP_OPERATION_SEND_PARTIAL_OBJECT,
86 MTP_OPERATION_TRUNCATE_OBJECT,
87 MTP_OPERATION_BEGIN_EDIT_OBJECT,
88 MTP_OPERATION_END_EDIT_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040089};
90
Mike Lockwood873871f2010-07-12 18:54:16 -040091static const MtpEventCode kSupportedEventCodes[] = {
92 MTP_EVENT_OBJECT_ADDED,
93 MTP_EVENT_OBJECT_REMOVED,
Mike Lockwooda8494402011-02-18 09:07:14 -050094 MTP_EVENT_STORE_ADDED,
95 MTP_EVENT_STORE_REMOVED,
Mike Lockwood873871f2010-07-12 18:54:16 -040096};
97
Mike Lockwood3d1d7762011-06-21 08:27:06 -040098MtpServer::MtpServer(int fd, MtpDatabase* database, bool ptp,
Mike Lockwood8e2a2802010-07-02 15:15:07 -040099 int fileGroup, int filePerm, int directoryPerm)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400100 : mFD(fd),
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400101 mDatabase(database),
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400102 mPtp(ptp),
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400103 mFileGroup(fileGroup),
104 mFilePermission(filePerm),
105 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400106 mSessionID(0),
107 mSessionOpen(false),
108 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400109 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400110 mSendObjectFileSize(0)
111{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400112}
113
114MtpServer::~MtpServer() {
115}
116
Mike Lockwooda8494402011-02-18 09:07:14 -0500117void MtpServer::addStorage(MtpStorage* storage) {
118 Mutex::Autolock autoLock(mMutex);
119
120 mStorages.push(storage);
121 sendStoreAdded(storage->getStorageID());
122}
123
124void MtpServer::removeStorage(MtpStorage* storage) {
125 Mutex::Autolock autoLock(mMutex);
126
127 for (int i = 0; i < mStorages.size(); i++) {
128 if (mStorages[i] == storage) {
129 mStorages.removeAt(i);
130 sendStoreRemoved(storage->getStorageID());
131 break;
132 }
133 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400134}
135
136MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800137 if (id == 0)
138 return mStorages[0];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400139 for (int i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800140 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400141 if (storage->getStorageID() == id)
142 return storage;
143 }
144 return NULL;
145}
146
Mike Lockwooda8494402011-02-18 09:07:14 -0500147bool MtpServer::hasStorage(MtpStorageID id) {
148 if (id == 0 || id == 0xFFFFFFFF)
149 return mStorages.size() > 0;
150 return (getStorage(id) != NULL);
151}
152
Mike Lockwood16864ba2010-05-11 17:16:59 -0400153void MtpServer::run() {
154 int fd = mFD;
155
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400156 LOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400157
158 while (1) {
159 int ret = mRequest.read(fd);
160 if (ret < 0) {
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800161 LOGV("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400162 if (errno == ECANCELED) {
163 // return to top of loop and wait for next command
164 continue;
165 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400166 break;
167 }
168 MtpOperationCode operation = mRequest.getOperationCode();
169 MtpTransactionID transaction = mRequest.getTransactionID();
170
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400171 LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172 mRequest.dump();
173
174 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400175 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400176 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
177 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
178 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400179 if (dataIn) {
180 int ret = mData.read(fd);
181 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400182 LOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400183 if (errno == ECANCELED) {
184 // return to top of loop and wait for next command
185 continue;
186 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400187 break;
188 }
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400189 LOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400190 mData.dump();
191 } else {
192 mData.reset();
193 }
194
Mike Lockwood916076c2010-06-04 09:49:21 -0400195 if (handleRequest()) {
196 if (!dataIn && mData.hasData()) {
197 mData.setOperationCode(operation);
198 mData.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400199 LOGV("sending data:");
Mike Lockwood23d20712010-10-11 17:31:44 -0400200 mData.dump();
Mike Lockwood916076c2010-06-04 09:49:21 -0400201 ret = mData.write(fd);
202 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400203 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400204 if (errno == ECANCELED) {
205 // return to top of loop and wait for next command
206 continue;
207 }
208 break;
209 }
210 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400211
Mike Lockwood916076c2010-06-04 09:49:21 -0400212 mResponse.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400213 LOGV("sending response %04X", mResponse.getResponseCode());
Mike Lockwood916076c2010-06-04 09:49:21 -0400214 ret = mResponse.write(fd);
Mike Lockwood23d20712010-10-11 17:31:44 -0400215 mResponse.dump();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400216 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400217 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400218 if (errno == ECANCELED) {
219 // return to top of loop and wait for next command
220 continue;
221 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400222 break;
223 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400224 } else {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400225 LOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400226 }
227 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400228
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700229 // commit any open edits
230 int count = mObjectEditList.size();
231 for (int i = 0; i < count; i++) {
232 ObjectEdit* edit = mObjectEditList[i];
233 commitEdit(edit);
234 delete edit;
235 }
236 mObjectEditList.clear();
237
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400238 if (mSessionOpen)
239 mDatabase->sessionEnded();
Mike Lockwooddec73882011-07-11 15:04:38 -0400240 close(fd);
241 mFD = -1;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400242}
243
Mike Lockwood873871f2010-07-12 18:54:16 -0400244void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500245 LOGV("sendObjectAdded %d\n", handle);
246 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400247}
248
249void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500250 LOGV("sendObjectRemoved %d\n", handle);
251 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
252}
253
254void MtpServer::sendStoreAdded(MtpStorageID id) {
255 LOGV("sendStoreAdded %08X\n", id);
256 sendEvent(MTP_EVENT_STORE_ADDED, id);
257}
258
259void MtpServer::sendStoreRemoved(MtpStorageID id) {
260 LOGV("sendStoreRemoved %08X\n", id);
261 sendEvent(MTP_EVENT_STORE_REMOVED, id);
262}
263
264void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400265 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500266 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400267 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500268 mEvent.setParameter(1, param1);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400269 int ret = mEvent.write(mFD);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800270 LOGV("mEvent.write returned %d\n", ret);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400271 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400272}
273
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700274void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
275 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700276 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700277 mObjectEditList.add(edit);
278}
279
280MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
281 int count = mObjectEditList.size();
282 for (int i = 0; i < count; i++) {
283 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700284 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700285 }
286 return NULL;
287}
288
289void MtpServer::removeEditObject(MtpObjectHandle handle) {
290 int count = mObjectEditList.size();
291 for (int i = 0; i < count; i++) {
292 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700293 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700294 delete edit;
295 mObjectEditList.removeAt(i);
296 return;
297 }
298 }
299 LOGE("ObjectEdit not found in removeEditObject");
300}
301
302void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700303 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700304}
305
306
Mike Lockwood916076c2010-06-04 09:49:21 -0400307bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500308 Mutex::Autolock autoLock(mMutex);
309
Mike Lockwood16864ba2010-05-11 17:16:59 -0400310 MtpOperationCode operation = mRequest.getOperationCode();
311 MtpResponseCode response;
312
313 mResponse.reset();
314
315 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
316 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400317 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400318 mSendObjectHandle = kInvalidObjectHandle;
319 }
320
321 switch (operation) {
322 case MTP_OPERATION_GET_DEVICE_INFO:
323 response = doGetDeviceInfo();
324 break;
325 case MTP_OPERATION_OPEN_SESSION:
326 response = doOpenSession();
327 break;
328 case MTP_OPERATION_CLOSE_SESSION:
329 response = doCloseSession();
330 break;
331 case MTP_OPERATION_GET_STORAGE_IDS:
332 response = doGetStorageIDs();
333 break;
334 case MTP_OPERATION_GET_STORAGE_INFO:
335 response = doGetStorageInfo();
336 break;
337 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
338 response = doGetObjectPropsSupported();
339 break;
340 case MTP_OPERATION_GET_OBJECT_HANDLES:
341 response = doGetObjectHandles();
342 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400343 case MTP_OPERATION_GET_NUM_OBJECTS:
344 response = doGetNumObjects();
345 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400346 case MTP_OPERATION_GET_OBJECT_REFERENCES:
347 response = doGetObjectReferences();
348 break;
349 case MTP_OPERATION_SET_OBJECT_REFERENCES:
350 response = doSetObjectReferences();
351 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400352 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
353 response = doGetObjectPropValue();
354 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400355 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
356 response = doSetObjectPropValue();
357 break;
358 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
359 response = doGetDevicePropValue();
360 break;
361 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
362 response = doSetDevicePropValue();
363 break;
364 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
365 response = doResetDevicePropValue();
366 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400367 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
368 response = doGetObjectPropList();
369 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400370 case MTP_OPERATION_GET_OBJECT_INFO:
371 response = doGetObjectInfo();
372 break;
373 case MTP_OPERATION_GET_OBJECT:
374 response = doGetObject();
375 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700376 case MTP_OPERATION_GET_THUMB:
377 response = doGetThumb();
378 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500379 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700380 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
381 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500382 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400383 case MTP_OPERATION_SEND_OBJECT_INFO:
384 response = doSendObjectInfo();
385 break;
386 case MTP_OPERATION_SEND_OBJECT:
387 response = doSendObject();
388 break;
389 case MTP_OPERATION_DELETE_OBJECT:
390 response = doDeleteObject();
391 break;
392 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400393 response = doGetObjectPropDesc();
394 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400395 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
396 response = doGetDevicePropDesc();
397 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700398 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
399 response = doSendPartialObject();
400 break;
401 case MTP_OPERATION_TRUNCATE_OBJECT:
402 response = doTruncateObject();
403 break;
404 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
405 response = doBeginEditObject();
406 break;
407 case MTP_OPERATION_END_EDIT_OBJECT:
408 response = doEndEditObject();
409 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400410 default:
Mike Lockwooda881b442010-09-23 22:32:05 -0400411 LOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400412 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
413 break;
414 }
415
Mike Lockwood916076c2010-06-04 09:49:21 -0400416 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
417 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400418 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400419 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400420}
421
422MtpResponseCode MtpServer::doGetDeviceInfo() {
423 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700424 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400425
Mike Lockwood782aef12010-08-10 07:37:50 -0400426 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
427 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
428 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
429
Mike Lockwood16864ba2010-05-11 17:16:59 -0400430 // fill in device info
431 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400432 if (mPtp) {
433 mData.putUInt32(0);
434 } else {
435 // MTP Vendor Extension ID
436 mData.putUInt32(6);
437 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400438 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood3d1d7762011-06-21 08:27:06 -0400439 if (mPtp) {
440 // no extensions
441 string.set("");
442 } else {
443 // MTP extensions
444 string.set("microsoft.com: 1.0; android.com: 1.0;");
445 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 mData.putString(string); // MTP Extensions
447 mData.putUInt16(0); //Functional Mode
448 mData.putAUInt16(kSupportedOperationCodes,
449 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400450 mData.putAUInt16(kSupportedEventCodes,
451 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400452 mData.putAUInt16(deviceProperties); // Device Properties Supported
453 mData.putAUInt16(captureFormats); // Capture Formats
454 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500455
456 property_get("ro.product.manufacturer", prop_value, "unknown manufacturer");
457 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400458 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700459
460 property_get("ro.product.model", prop_value, "MTP Device");
461 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400462 mData.putString(string); // Model
463 string.set("1.0");
464 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700465
466 property_get("ro.serialno", prop_value, "????????");
467 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400468 mData.putString(string); // Serial Number
469
Mike Lockwood782aef12010-08-10 07:37:50 -0400470 delete playbackFormats;
471 delete captureFormats;
472 delete deviceProperties;
473
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474 return MTP_RESPONSE_OK;
475}
476
477MtpResponseCode MtpServer::doOpenSession() {
478 if (mSessionOpen) {
479 mResponse.setParameter(1, mSessionID);
480 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
481 }
482 mSessionID = mRequest.getParameter(1);
483 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400484
485 mDatabase->sessionStarted();
486
Mike Lockwood16864ba2010-05-11 17:16:59 -0400487 return MTP_RESPONSE_OK;
488}
489
490MtpResponseCode MtpServer::doCloseSession() {
491 if (!mSessionOpen)
492 return MTP_RESPONSE_SESSION_NOT_OPEN;
493 mSessionID = 0;
494 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400495 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400496 return MTP_RESPONSE_OK;
497}
498
499MtpResponseCode MtpServer::doGetStorageIDs() {
500 if (!mSessionOpen)
501 return MTP_RESPONSE_SESSION_NOT_OPEN;
502
503 int count = mStorages.size();
504 mData.putUInt32(count);
505 for (int i = 0; i < count; i++)
506 mData.putUInt32(mStorages[i]->getStorageID());
507
508 return MTP_RESPONSE_OK;
509}
510
511MtpResponseCode MtpServer::doGetStorageInfo() {
512 MtpStringBuffer string;
513
514 if (!mSessionOpen)
515 return MTP_RESPONSE_SESSION_NOT_OPEN;
516 MtpStorageID id = mRequest.getParameter(1);
517 MtpStorage* storage = getStorage(id);
518 if (!storage)
519 return MTP_RESPONSE_INVALID_STORAGE_ID;
520
521 mData.putUInt16(storage->getType());
522 mData.putUInt16(storage->getFileSystemType());
523 mData.putUInt16(storage->getAccessCapability());
524 mData.putUInt64(storage->getMaxCapacity());
525 mData.putUInt64(storage->getFreeSpace());
526 mData.putUInt32(1024*1024*1024); // Free Space in Objects
527 string.set(storage->getDescription());
528 mData.putString(string);
529 mData.putEmptyString(); // Volume Identifier
530
531 return MTP_RESPONSE_OK;
532}
533
534MtpResponseCode MtpServer::doGetObjectPropsSupported() {
535 if (!mSessionOpen)
536 return MTP_RESPONSE_SESSION_NOT_OPEN;
537 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800538 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400539 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400540 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400541 return MTP_RESPONSE_OK;
542}
543
544MtpResponseCode MtpServer::doGetObjectHandles() {
545 if (!mSessionOpen)
546 return MTP_RESPONSE_SESSION_NOT_OPEN;
547 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400548 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400549 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400550 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500551
552 if (!hasStorage(storageID))
553 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400554
555 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
556 mData.putAUInt32(handles);
557 delete handles;
558 return MTP_RESPONSE_OK;
559}
560
Mike Lockwood343af4e2010-08-02 10:52:20 -0400561MtpResponseCode MtpServer::doGetNumObjects() {
562 if (!mSessionOpen)
563 return MTP_RESPONSE_SESSION_NOT_OPEN;
564 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
565 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
566 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400567 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500568 if (!hasStorage(storageID))
569 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400570
571 int count = mDatabase->getNumObjects(storageID, format, parent);
572 if (count >= 0) {
573 mResponse.setParameter(1, count);
574 return MTP_RESPONSE_OK;
575 } else {
576 mResponse.setParameter(1, 0);
577 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
578 }
579}
580
Mike Lockwood438344f2010-08-03 15:30:09 -0400581MtpResponseCode MtpServer::doGetObjectReferences() {
582 if (!mSessionOpen)
583 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500584 if (!hasStorage())
585 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
586 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400587
588 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400589 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400590 if (handles) {
591 mData.putAUInt32(handles);
592 delete handles;
593 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400594 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400595 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400596 return MTP_RESPONSE_OK;
597}
598
599MtpResponseCode MtpServer::doSetObjectReferences() {
600 if (!mSessionOpen)
601 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500602 if (!hasStorage())
603 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood438344f2010-08-03 15:30:09 -0400604 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500605
Mike Lockwood438344f2010-08-03 15:30:09 -0400606 MtpObjectHandleList* references = mData.getAUInt32();
607 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
608 delete references;
609 return result;
610}
611
Mike Lockwood16864ba2010-05-11 17:16:59 -0400612MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500613 if (!hasStorage())
614 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400615 MtpObjectHandle handle = mRequest.getParameter(1);
616 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800617 LOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400618 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400619
Mike Lockwood8277cec2010-08-10 15:20:35 -0400620 return mDatabase->getObjectPropertyValue(handle, property, mData);
621}
622
623MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500624 if (!hasStorage())
625 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400626 MtpObjectHandle handle = mRequest.getParameter(1);
627 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800628 LOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400629 MtpDebug::getObjectPropCodeName(property));
630
631 return mDatabase->setObjectPropertyValue(handle, property, mData);
632}
633
634MtpResponseCode MtpServer::doGetDevicePropValue() {
635 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800636 LOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400637 MtpDebug::getDevicePropCodeName(property));
638
639 return mDatabase->getDevicePropertyValue(property, mData);
640}
641
642MtpResponseCode MtpServer::doSetDevicePropValue() {
643 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800644 LOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400645 MtpDebug::getDevicePropCodeName(property));
646
647 return mDatabase->setDevicePropertyValue(property, mData);
648}
649
650MtpResponseCode MtpServer::doResetDevicePropValue() {
651 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800652 LOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400653 MtpDebug::getDevicePropCodeName(property));
654
655 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400656}
657
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400658MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500659 if (!hasStorage())
660 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400661
662 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500663 // use uint32_t so we can support 0xFFFFFFFF
664 uint32_t format = mRequest.getParameter(2);
665 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400666 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500667 int depth = mRequest.getParameter(5);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800668 LOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400669 handle, MtpDebug::getFormatCodeName(format),
670 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
671
672 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
673}
674
Mike Lockwood16864ba2010-05-11 17:16:59 -0400675MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500676 if (!hasStorage())
677 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400678 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700679 MtpObjectInfo info(handle);
680 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
681 if (result == MTP_RESPONSE_OK) {
682 char date[20];
683
684 mData.putUInt32(info.mStorageID);
685 mData.putUInt16(info.mFormat);
686 mData.putUInt16(info.mProtectionStatus);
687
688 // if object is being edited the database size may be out of date
689 uint32_t size = info.mCompressedSize;
690 ObjectEdit* edit = getEditObject(handle);
691 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700692 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700693 mData.putUInt32(size);
694
695 mData.putUInt16(info.mThumbFormat);
696 mData.putUInt32(info.mThumbCompressedSize);
697 mData.putUInt32(info.mThumbPixWidth);
698 mData.putUInt32(info.mThumbPixHeight);
699 mData.putUInt32(info.mImagePixWidth);
700 mData.putUInt32(info.mImagePixHeight);
701 mData.putUInt32(info.mImagePixDepth);
702 mData.putUInt32(info.mParent);
703 mData.putUInt16(info.mAssociationType);
704 mData.putUInt32(info.mAssociationDesc);
705 mData.putUInt32(info.mSequenceNumber);
706 mData.putString(info.mName);
707 mData.putEmptyString(); // date created
708 formatDateTime(info.mDateModified, date, sizeof(date));
709 mData.putString(date); // date modified
710 mData.putEmptyString(); // keywords
711 }
712 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400713}
714
715MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500716 if (!hasStorage())
717 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400718 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400719 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400720 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800721 MtpObjectFormat format;
722 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400723 if (result != MTP_RESPONSE_OK)
724 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400725
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400726 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400727 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400728 mfr.fd = open(filePath, O_RDONLY);
729 if (mfr.fd < 0) {
730 return MTP_RESPONSE_GENERAL_ERROR;
731 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400732 mfr.offset = 0;
733 mfr.length = fileLength;
734
735 // send data header
736 mData.setOperationCode(mRequest.getOperationCode());
737 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood23d20712010-10-11 17:31:44 -0400738 mData.writeDataHeader(mFD, fileLength + MTP_CONTAINER_HEADER_SIZE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400739
740 // then transfer the file
741 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400742 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400743 if (ret < 0) {
744 if (errno == ECANCELED)
745 return MTP_RESPONSE_TRANSACTION_CANCELLED;
746 else
747 return MTP_RESPONSE_GENERAL_ERROR;
748 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400749 return MTP_RESPONSE_OK;
750}
751
Mike Lockwood64000782011-04-24 18:40:17 -0700752MtpResponseCode MtpServer::doGetThumb() {
753 MtpObjectHandle handle = mRequest.getParameter(1);
754 size_t thumbSize;
755 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
756 if (thumb) {
757 // send data
758 mData.setOperationCode(mRequest.getOperationCode());
759 mData.setTransactionID(mRequest.getTransactionID());
760 mData.writeData(mFD, thumb, thumbSize);
761 free(thumb);
762 return MTP_RESPONSE_OK;
763 } else {
764 return MTP_RESPONSE_GENERAL_ERROR;
765 }
766}
767
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700768MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500769 if (!hasStorage())
770 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500771 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700772 uint64_t offset;
773 uint32_t length;
774 offset = mRequest.getParameter(2);
775 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
776 // android extension with 64 bit offset
777 uint64_t offset2 = mRequest.getParameter(3);
778 offset = offset | (offset2 << 32);
779 length = mRequest.getParameter(4);
780 } else {
781 // standard GetPartialObject
782 length = mRequest.getParameter(3);
783 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500784 MtpString pathBuf;
785 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800786 MtpObjectFormat format;
787 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500788 if (result != MTP_RESPONSE_OK)
789 return result;
790 if (offset + length > fileLength)
791 length = fileLength - offset;
792
793 const char* filePath = (const char *)pathBuf;
794 mtp_file_range mfr;
795 mfr.fd = open(filePath, O_RDONLY);
796 if (mfr.fd < 0) {
797 return MTP_RESPONSE_GENERAL_ERROR;
798 }
799 mfr.offset = offset;
800 mfr.length = length;
801 mResponse.setParameter(1, length);
802
803 // send data header
804 mData.setOperationCode(mRequest.getOperationCode());
805 mData.setTransactionID(mRequest.getTransactionID());
806 mData.writeDataHeader(mFD, length + MTP_CONTAINER_HEADER_SIZE);
807
808 // then transfer the file
809 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
810 close(mfr.fd);
811 if (ret < 0) {
812 if (errno == ECANCELED)
813 return MTP_RESPONSE_TRANSACTION_CANCELLED;
814 else
815 return MTP_RESPONSE_GENERAL_ERROR;
816 }
817 return MTP_RESPONSE_OK;
818}
819
Mike Lockwood16864ba2010-05-11 17:16:59 -0400820MtpResponseCode MtpServer::doSendObjectInfo() {
821 MtpString path;
822 MtpStorageID storageID = mRequest.getParameter(1);
823 MtpStorage* storage = getStorage(storageID);
824 MtpObjectHandle parent = mRequest.getParameter(2);
825 if (!storage)
826 return MTP_RESPONSE_INVALID_STORAGE_ID;
827
828 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400829 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400830 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400831 parent = 0;
832 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800833 int64_t length;
834 MtpObjectFormat format;
835 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400836 if (result != MTP_RESPONSE_OK)
837 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800838 if (format != MTP_FORMAT_ASSOCIATION)
839 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400840 }
841
842 // read only the fields we need
843 mData.getUInt32(); // storage ID
844 MtpObjectFormat format = mData.getUInt16();
845 mData.getUInt16(); // protection status
846 mSendObjectFileSize = mData.getUInt32();
847 mData.getUInt16(); // thumb format
848 mData.getUInt32(); // thumb compressed size
849 mData.getUInt32(); // thumb pix width
850 mData.getUInt32(); // thumb pix height
851 mData.getUInt32(); // image pix width
852 mData.getUInt32(); // image pix height
853 mData.getUInt32(); // image bit depth
854 mData.getUInt32(); // parent
855 uint16_t associationType = mData.getUInt16();
856 uint32_t associationDesc = mData.getUInt32(); // association desc
857 mData.getUInt32(); // sequence number
858 MtpStringBuffer name, created, modified;
859 mData.getString(name); // file name
860 mData.getString(created); // date created
861 mData.getString(modified); // date modified
862 // keywords follow
863
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800864 LOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400865 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400866 if (!parseDateTime(modified, modifiedTime))
867 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400868
869 if (path[path.size() - 1] != '/')
870 path += "/";
871 path += (const char *)name;
872
Mike Lockwood20c3be02010-12-12 12:17:43 -0800873 // check space first
874 if (mSendObjectFileSize > storage->getFreeSpace())
875 return MTP_RESPONSE_STORAGE_FULL;
Mike Lockwood9b88b722011-07-11 09:18:03 -0400876 uint64_t maxFileSize = storage->getMaxFileSize();
877 // check storage max file size
878 if (maxFileSize != 0) {
879 // if mSendObjectFileSize is 0xFFFFFFFF, then all we know is the file size
880 // is >= 0xFFFFFFFF
881 if (mSendObjectFileSize > maxFileSize || mSendObjectFileSize == 0xFFFFFFFF)
882 return MTP_RESPONSE_OBJECT_TOO_LARGE;
883 }
Mike Lockwood20c3be02010-12-12 12:17:43 -0800884
Mike Lockwooda8494402011-02-18 09:07:14 -0500885LOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400886 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
887 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400888 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400889 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400890 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400891
892 if (format == MTP_FORMAT_ASSOCIATION) {
893 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400894 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400895 umask(mask);
896 if (ret && ret != -EEXIST)
897 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400898 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwoodaa952402011-01-18 11:06:19 -0800899
900 // SendObject does not get sent for directories, so call endSendObject here instead
901 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400902 } else {
903 mSendObjectFilePath = path;
904 // save the handle for the SendObject call, which should follow
905 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400906 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400907 }
908
909 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400910 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400911 mResponse.setParameter(3, handle);
912
913 return MTP_RESPONSE_OK;
914}
915
916MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500917 if (!hasStorage())
918 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -0400919 MtpResponseCode result = MTP_RESPONSE_OK;
920 mode_t mask;
921 int ret;
922
Mike Lockwood16864ba2010-05-11 17:16:59 -0400923 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400924 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400925 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
926 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400927 }
928
929 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400930 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400931 // FIXME - check for errors here.
932
933 // reset so we don't attempt to send this back
934 mData.reset();
935
936 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400937 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
938 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400939 result = MTP_RESPONSE_GENERAL_ERROR;
940 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400941 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400942 fchown(mfr.fd, getuid(), mFileGroup);
943 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400944 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400945 fchmod(mfr.fd, mFilePermission);
946 umask(mask);
947
Mike Lockwood16864ba2010-05-11 17:16:59 -0400948 mfr.offset = 0;
949 mfr.length = mSendObjectFileSize;
950
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800951 LOGV("receiving %s\n", (const char *)mSendObjectFilePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400952 // transfer the file
953 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400954 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400955
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400956 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400957
Mike Lockwood916076c2010-06-04 09:49:21 -0400958 if (ret < 0) {
959 unlink(mSendObjectFilePath);
960 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400961 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400962 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400963 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400964 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400965
966done:
967 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -0800968 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -0400969 mSendObjectHandle = kInvalidObjectHandle;
970 mSendObjectFormat = 0;
971 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400972}
973
Mike Lockwoodd3211492010-09-13 17:15:58 -0400974static void deleteRecursive(const char* path) {
975 char pathbuf[PATH_MAX];
976 int pathLength = strlen(path);
977 if (pathLength >= sizeof(pathbuf) - 1) {
978 LOGE("path too long: %s\n", path);
979 }
980 strcpy(pathbuf, path);
981 if (pathbuf[pathLength - 1] != '/') {
982 pathbuf[pathLength++] = '/';
983 }
984 char* fileSpot = pathbuf + pathLength;
985 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
986
987 DIR* dir = opendir(path);
988 if (!dir) {
989 LOGE("opendir %s failed: %s", path, strerror(errno));
990 return;
991 }
992
993 struct dirent* entry;
994 while ((entry = readdir(dir))) {
995 const char* name = entry->d_name;
996
997 // ignore "." and ".."
998 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
999 continue;
1000 }
1001
1002 int nameLength = strlen(name);
1003 if (nameLength > pathRemaining) {
1004 LOGE("path %s/%s too long\n", path, name);
1005 continue;
1006 }
1007 strcpy(fileSpot, name);
1008
1009 int type = entry->d_type;
1010 if (entry->d_type == DT_DIR) {
1011 deleteRecursive(pathbuf);
1012 rmdir(pathbuf);
1013 } else {
1014 unlink(pathbuf);
1015 }
1016 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -05001017 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001018}
1019
1020static void deletePath(const char* path) {
1021 struct stat statbuf;
1022 if (stat(path, &statbuf) == 0) {
1023 if (S_ISDIR(statbuf.st_mode)) {
1024 deleteRecursive(path);
1025 rmdir(path);
1026 } else {
1027 unlink(path);
1028 }
1029 } else {
1030 LOGE("deletePath stat failed for %s: %s", path, strerror(errno));
1031 }
1032}
1033
Mike Lockwood16864ba2010-05-11 17:16:59 -04001034MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001035 if (!hasStorage())
1036 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001037 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001038 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001039 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1040 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001041
1042 MtpString filePath;
1043 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001044 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001045 if (result == MTP_RESPONSE_OK) {
1046 LOGV("deleting %s", (const char *)filePath);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001047 deletePath((const char *)filePath);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001048 return mDatabase->deleteFile(handle);
1049 } else {
1050 return result;
1051 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001052}
1053
1054MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001055 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001056 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -08001057 LOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001058 MtpDebug::getFormatCodeName(format));
1059 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001060 if (!property)
1061 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001062 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001063 delete property;
1064 return MTP_RESPONSE_OK;
1065}
1066
1067MtpResponseCode MtpServer::doGetDevicePropDesc() {
1068 MtpDeviceProperty propCode = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -08001069 LOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001070 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1071 if (!property)
1072 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1073 property->write(mData);
1074 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001075 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001076}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001077
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001078MtpResponseCode MtpServer::doSendPartialObject() {
1079 if (!hasStorage())
1080 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
1081 MtpObjectHandle handle = mRequest.getParameter(1);
1082 uint64_t offset = mRequest.getParameter(2);
1083 uint64_t offset2 = mRequest.getParameter(3);
1084 offset = offset | (offset2 << 32);
1085 uint32_t length = mRequest.getParameter(4);
1086
1087 ObjectEdit* edit = getEditObject(handle);
1088 if (!edit) {
1089 LOGE("object not open for edit in doSendPartialObject");
1090 return MTP_RESPONSE_GENERAL_ERROR;
1091 }
1092
1093 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001094 if (offset > edit->mSize) {
1095 LOGD("writing past end of object, offset: %lld, edit->mSize: %lld", offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001096 return MTP_RESPONSE_GENERAL_ERROR;
1097 }
1098
1099 // read the header
1100 int ret = mData.readDataHeader(mFD);
1101 // FIXME - check for errors here.
1102
1103 // reset so we don't attempt to send this back
1104 mData.reset();
1105
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001106 const char* filePath = (const char *)edit->mPath;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001107 LOGV("receiving partial %s %lld %ld\n", filePath, offset, length);
1108 mtp_file_range mfr;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001109 mfr.fd = edit->mFD;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001110 mfr.offset = offset;
1111 mfr.length = length;
1112
1113 // transfer the file
1114 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
1115 LOGV("MTP_RECEIVE_FILE returned %d", ret);
1116 if (ret < 0) {
1117 mResponse.setParameter(1, 0);
1118 if (errno == ECANCELED)
1119 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1120 else
1121 return MTP_RESPONSE_GENERAL_ERROR;
1122 }
1123 mResponse.setParameter(1, length);
1124 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001125 if (end > edit->mSize) {
1126 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001127 }
1128 return MTP_RESPONSE_OK;
1129}
1130
1131MtpResponseCode MtpServer::doTruncateObject() {
1132 MtpObjectHandle handle = mRequest.getParameter(1);
1133 ObjectEdit* edit = getEditObject(handle);
1134 if (!edit) {
1135 LOGE("object not open for edit in doTruncateObject");
1136 return MTP_RESPONSE_GENERAL_ERROR;
1137 }
1138
1139 uint64_t offset = mRequest.getParameter(2);
1140 uint64_t offset2 = mRequest.getParameter(3);
1141 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001142 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001143 return MTP_RESPONSE_GENERAL_ERROR;
1144 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001145 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001146 return MTP_RESPONSE_OK;
1147 }
1148}
1149
1150MtpResponseCode MtpServer::doBeginEditObject() {
1151 MtpObjectHandle handle = mRequest.getParameter(1);
1152 if (getEditObject(handle)) {
1153 LOGE("object already open for edit in doBeginEditObject");
1154 return MTP_RESPONSE_GENERAL_ERROR;
1155 }
1156
1157 MtpString path;
1158 int64_t fileLength;
1159 MtpObjectFormat format;
1160 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1161 if (result != MTP_RESPONSE_OK)
1162 return result;
1163
1164 int fd = open((const char *)path, O_RDWR | O_EXCL);
1165 if (fd < 0) {
1166 LOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
1167 return MTP_RESPONSE_GENERAL_ERROR;
1168 }
1169
1170 addEditObject(handle, path, fileLength, format, fd);
1171 return MTP_RESPONSE_OK;
1172}
1173
1174MtpResponseCode MtpServer::doEndEditObject() {
1175 MtpObjectHandle handle = mRequest.getParameter(1);
1176 ObjectEdit* edit = getEditObject(handle);
1177 if (!edit) {
1178 LOGE("object not open for edit in doEndEditObject");
1179 return MTP_RESPONSE_GENERAL_ERROR;
1180 }
1181
1182 commitEdit(edit);
1183 removeEditObject(handle);
1184 return MTP_RESPONSE_OK;
1185}
1186
Mike Lockwood7850ef92010-05-14 10:10:36 -04001187} // namespace android