blob: 9ec73c43f86ac7844960c8be2d9cea03c4b29d07 [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 Lockwood1865a5d2010-07-03 00:44:05 -040098MtpServer::MtpServer(int fd, MtpDatabase* database,
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 Lockwood8e2a2802010-07-02 15:15:07 -0400102 mFileGroup(fileGroup),
103 mFilePermission(filePerm),
104 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400105 mSessionID(0),
106 mSessionOpen(false),
107 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400108 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400109 mSendObjectFileSize(0)
110{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400111}
112
113MtpServer::~MtpServer() {
114}
115
Mike Lockwooda8494402011-02-18 09:07:14 -0500116void MtpServer::addStorage(MtpStorage* storage) {
117 Mutex::Autolock autoLock(mMutex);
118
119 mStorages.push(storage);
120 sendStoreAdded(storage->getStorageID());
121}
122
123void MtpServer::removeStorage(MtpStorage* storage) {
124 Mutex::Autolock autoLock(mMutex);
125
126 for (int i = 0; i < mStorages.size(); i++) {
127 if (mStorages[i] == storage) {
128 mStorages.removeAt(i);
129 sendStoreRemoved(storage->getStorageID());
130 break;
131 }
132 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400133}
134
135MtpStorage* MtpServer::getStorage(MtpStorageID id) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800136 if (id == 0)
137 return mStorages[0];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400138 for (int i = 0; i < mStorages.size(); i++) {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800139 MtpStorage* storage = mStorages[i];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400140 if (storage->getStorageID() == id)
141 return storage;
142 }
143 return NULL;
144}
145
Mike Lockwooda8494402011-02-18 09:07:14 -0500146bool MtpServer::hasStorage(MtpStorageID id) {
147 if (id == 0 || id == 0xFFFFFFFF)
148 return mStorages.size() > 0;
149 return (getStorage(id) != NULL);
150}
151
Mike Lockwood16864ba2010-05-11 17:16:59 -0400152void MtpServer::run() {
153 int fd = mFD;
154
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400155 LOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156
157 while (1) {
158 int ret = mRequest.read(fd);
159 if (ret < 0) {
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800160 LOGV("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400161 if (errno == ECANCELED) {
162 // return to top of loop and wait for next command
163 continue;
164 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165 break;
166 }
167 MtpOperationCode operation = mRequest.getOperationCode();
168 MtpTransactionID transaction = mRequest.getTransactionID();
169
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400170 LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400171 mRequest.dump();
172
173 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400174 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400175 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
176 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
177 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400178 if (dataIn) {
179 int ret = mData.read(fd);
180 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400181 LOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400182 if (errno == ECANCELED) {
183 // return to top of loop and wait for next command
184 continue;
185 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400186 break;
187 }
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400188 LOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400189 mData.dump();
190 } else {
191 mData.reset();
192 }
193
Mike Lockwood916076c2010-06-04 09:49:21 -0400194 if (handleRequest()) {
195 if (!dataIn && mData.hasData()) {
196 mData.setOperationCode(operation);
197 mData.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400198 LOGV("sending data:");
Mike Lockwood23d20712010-10-11 17:31:44 -0400199 mData.dump();
Mike Lockwood916076c2010-06-04 09:49:21 -0400200 ret = mData.write(fd);
201 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400202 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400203 if (errno == ECANCELED) {
204 // return to top of loop and wait for next command
205 continue;
206 }
207 break;
208 }
209 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400210
Mike Lockwood916076c2010-06-04 09:49:21 -0400211 mResponse.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400212 LOGV("sending response %04X", mResponse.getResponseCode());
Mike Lockwood916076c2010-06-04 09:49:21 -0400213 ret = mResponse.write(fd);
Mike Lockwood23d20712010-10-11 17:31:44 -0400214 mResponse.dump();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400215 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400216 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400217 if (errno == ECANCELED) {
218 // return to top of loop and wait for next command
219 continue;
220 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400221 break;
222 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400223 } else {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400224 LOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400225 }
226 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400227
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700228 // commit any open edits
229 int count = mObjectEditList.size();
230 for (int i = 0; i < count; i++) {
231 ObjectEdit* edit = mObjectEditList[i];
232 commitEdit(edit);
233 delete edit;
234 }
235 mObjectEditList.clear();
236
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400237 if (mSessionOpen)
238 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400239}
240
Mike Lockwood873871f2010-07-12 18:54:16 -0400241void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500242 LOGV("sendObjectAdded %d\n", handle);
243 sendEvent(MTP_EVENT_OBJECT_ADDED, handle);
Mike Lockwood873871f2010-07-12 18:54:16 -0400244}
245
246void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500247 LOGV("sendObjectRemoved %d\n", handle);
248 sendEvent(MTP_EVENT_OBJECT_REMOVED, handle);
249}
250
251void MtpServer::sendStoreAdded(MtpStorageID id) {
252 LOGV("sendStoreAdded %08X\n", id);
253 sendEvent(MTP_EVENT_STORE_ADDED, id);
254}
255
256void MtpServer::sendStoreRemoved(MtpStorageID id) {
257 LOGV("sendStoreRemoved %08X\n", id);
258 sendEvent(MTP_EVENT_STORE_REMOVED, id);
259}
260
261void MtpServer::sendEvent(MtpEventCode code, uint32_t param1) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400262 if (mSessionOpen) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500263 mEvent.setEventCode(code);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400264 mEvent.setTransactionID(mRequest.getTransactionID());
Mike Lockwooda8494402011-02-18 09:07:14 -0500265 mEvent.setParameter(1, param1);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400266 int ret = mEvent.write(mFD);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800267 LOGV("mEvent.write returned %d\n", ret);
Mike Lockwood73ecd232010-07-19 14:29:58 -0400268 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400269}
270
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700271void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
272 uint64_t size, MtpObjectFormat format, int fd) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700273 ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700274 mObjectEditList.add(edit);
275}
276
277MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
278 int count = mObjectEditList.size();
279 for (int i = 0; i < count; i++) {
280 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700281 if (edit->mHandle == handle) return edit;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700282 }
283 return NULL;
284}
285
286void MtpServer::removeEditObject(MtpObjectHandle handle) {
287 int count = mObjectEditList.size();
288 for (int i = 0; i < count; i++) {
289 ObjectEdit* edit = mObjectEditList[i];
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700290 if (edit->mHandle == handle) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700291 delete edit;
292 mObjectEditList.removeAt(i);
293 return;
294 }
295 }
296 LOGE("ObjectEdit not found in removeEditObject");
297}
298
299void MtpServer::commitEdit(ObjectEdit* edit) {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700300 mDatabase->endSendObject((const char *)edit->mPath, edit->mHandle, edit->mFormat, true);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700301}
302
303
Mike Lockwood916076c2010-06-04 09:49:21 -0400304bool MtpServer::handleRequest() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500305 Mutex::Autolock autoLock(mMutex);
306
Mike Lockwood16864ba2010-05-11 17:16:59 -0400307 MtpOperationCode operation = mRequest.getOperationCode();
308 MtpResponseCode response;
309
310 mResponse.reset();
311
312 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
313 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400314 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400315 mSendObjectHandle = kInvalidObjectHandle;
316 }
317
318 switch (operation) {
319 case MTP_OPERATION_GET_DEVICE_INFO:
320 response = doGetDeviceInfo();
321 break;
322 case MTP_OPERATION_OPEN_SESSION:
323 response = doOpenSession();
324 break;
325 case MTP_OPERATION_CLOSE_SESSION:
326 response = doCloseSession();
327 break;
328 case MTP_OPERATION_GET_STORAGE_IDS:
329 response = doGetStorageIDs();
330 break;
331 case MTP_OPERATION_GET_STORAGE_INFO:
332 response = doGetStorageInfo();
333 break;
334 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
335 response = doGetObjectPropsSupported();
336 break;
337 case MTP_OPERATION_GET_OBJECT_HANDLES:
338 response = doGetObjectHandles();
339 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400340 case MTP_OPERATION_GET_NUM_OBJECTS:
341 response = doGetNumObjects();
342 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400343 case MTP_OPERATION_GET_OBJECT_REFERENCES:
344 response = doGetObjectReferences();
345 break;
346 case MTP_OPERATION_SET_OBJECT_REFERENCES:
347 response = doSetObjectReferences();
348 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400349 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
350 response = doGetObjectPropValue();
351 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400352 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
353 response = doSetObjectPropValue();
354 break;
355 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
356 response = doGetDevicePropValue();
357 break;
358 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
359 response = doSetDevicePropValue();
360 break;
361 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
362 response = doResetDevicePropValue();
363 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400364 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
365 response = doGetObjectPropList();
366 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400367 case MTP_OPERATION_GET_OBJECT_INFO:
368 response = doGetObjectInfo();
369 break;
370 case MTP_OPERATION_GET_OBJECT:
371 response = doGetObject();
372 break;
Mike Lockwood64000782011-04-24 18:40:17 -0700373 case MTP_OPERATION_GET_THUMB:
374 response = doGetThumb();
375 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500376 case MTP_OPERATION_GET_PARTIAL_OBJECT:
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700377 case MTP_OPERATION_GET_PARTIAL_OBJECT_64:
378 response = doGetPartialObject(operation);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500379 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400380 case MTP_OPERATION_SEND_OBJECT_INFO:
381 response = doSendObjectInfo();
382 break;
383 case MTP_OPERATION_SEND_OBJECT:
384 response = doSendObject();
385 break;
386 case MTP_OPERATION_DELETE_OBJECT:
387 response = doDeleteObject();
388 break;
389 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400390 response = doGetObjectPropDesc();
391 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400392 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
393 response = doGetDevicePropDesc();
394 break;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700395 case MTP_OPERATION_SEND_PARTIAL_OBJECT:
396 response = doSendPartialObject();
397 break;
398 case MTP_OPERATION_TRUNCATE_OBJECT:
399 response = doTruncateObject();
400 break;
401 case MTP_OPERATION_BEGIN_EDIT_OBJECT:
402 response = doBeginEditObject();
403 break;
404 case MTP_OPERATION_END_EDIT_OBJECT:
405 response = doEndEditObject();
406 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400407 default:
Mike Lockwooda881b442010-09-23 22:32:05 -0400408 LOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400409 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
410 break;
411 }
412
Mike Lockwood916076c2010-06-04 09:49:21 -0400413 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
414 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400415 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400416 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400417}
418
419MtpResponseCode MtpServer::doGetDeviceInfo() {
420 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700421 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400422
Mike Lockwood782aef12010-08-10 07:37:50 -0400423 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
424 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
425 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
426
Mike Lockwood16864ba2010-05-11 17:16:59 -0400427 // fill in device info
428 mData.putUInt16(MTP_STANDARD_VERSION);
429 mData.putUInt32(6); // MTP Vendor Extension ID
430 mData.putUInt16(MTP_STANDARD_VERSION);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700431 string.set("microsoft.com: 1.0; android.com: 1.0;");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400432 mData.putString(string); // MTP Extensions
433 mData.putUInt16(0); //Functional Mode
434 mData.putAUInt16(kSupportedOperationCodes,
435 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400436 mData.putAUInt16(kSupportedEventCodes,
437 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400438 mData.putAUInt16(deviceProperties); // Device Properties Supported
439 mData.putAUInt16(captureFormats); // Capture Formats
440 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood8d08c5a2011-01-31 16:44:44 -0500441
442 property_get("ro.product.manufacturer", prop_value, "unknown manufacturer");
443 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400444 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700445
446 property_get("ro.product.model", prop_value, "MTP Device");
447 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400448 mData.putString(string); // Model
449 string.set("1.0");
450 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700451
452 property_get("ro.serialno", prop_value, "????????");
453 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400454 mData.putString(string); // Serial Number
455
Mike Lockwood782aef12010-08-10 07:37:50 -0400456 delete playbackFormats;
457 delete captureFormats;
458 delete deviceProperties;
459
Mike Lockwood16864ba2010-05-11 17:16:59 -0400460 return MTP_RESPONSE_OK;
461}
462
463MtpResponseCode MtpServer::doOpenSession() {
464 if (mSessionOpen) {
465 mResponse.setParameter(1, mSessionID);
466 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
467 }
468 mSessionID = mRequest.getParameter(1);
469 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400470
471 mDatabase->sessionStarted();
472
Mike Lockwood16864ba2010-05-11 17:16:59 -0400473 return MTP_RESPONSE_OK;
474}
475
476MtpResponseCode MtpServer::doCloseSession() {
477 if (!mSessionOpen)
478 return MTP_RESPONSE_SESSION_NOT_OPEN;
479 mSessionID = 0;
480 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400481 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400482 return MTP_RESPONSE_OK;
483}
484
485MtpResponseCode MtpServer::doGetStorageIDs() {
486 if (!mSessionOpen)
487 return MTP_RESPONSE_SESSION_NOT_OPEN;
488
489 int count = mStorages.size();
490 mData.putUInt32(count);
491 for (int i = 0; i < count; i++)
492 mData.putUInt32(mStorages[i]->getStorageID());
493
494 return MTP_RESPONSE_OK;
495}
496
497MtpResponseCode MtpServer::doGetStorageInfo() {
498 MtpStringBuffer string;
499
500 if (!mSessionOpen)
501 return MTP_RESPONSE_SESSION_NOT_OPEN;
502 MtpStorageID id = mRequest.getParameter(1);
503 MtpStorage* storage = getStorage(id);
504 if (!storage)
505 return MTP_RESPONSE_INVALID_STORAGE_ID;
506
507 mData.putUInt16(storage->getType());
508 mData.putUInt16(storage->getFileSystemType());
509 mData.putUInt16(storage->getAccessCapability());
510 mData.putUInt64(storage->getMaxCapacity());
511 mData.putUInt64(storage->getFreeSpace());
512 mData.putUInt32(1024*1024*1024); // Free Space in Objects
513 string.set(storage->getDescription());
514 mData.putString(string);
515 mData.putEmptyString(); // Volume Identifier
516
517 return MTP_RESPONSE_OK;
518}
519
520MtpResponseCode MtpServer::doGetObjectPropsSupported() {
521 if (!mSessionOpen)
522 return MTP_RESPONSE_SESSION_NOT_OPEN;
523 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood2e09e282010-12-07 10:51:20 -0800524 MtpObjectPropertyList* properties = mDatabase->getSupportedObjectProperties(format);
Mike Lockwood782aef12010-08-10 07:37:50 -0400525 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400526 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400527 return MTP_RESPONSE_OK;
528}
529
530MtpResponseCode MtpServer::doGetObjectHandles() {
531 if (!mSessionOpen)
532 return MTP_RESPONSE_SESSION_NOT_OPEN;
533 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400534 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400535 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400536 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500537
538 if (!hasStorage(storageID))
539 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400540
541 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
542 mData.putAUInt32(handles);
543 delete handles;
544 return MTP_RESPONSE_OK;
545}
546
Mike Lockwood343af4e2010-08-02 10:52:20 -0400547MtpResponseCode MtpServer::doGetNumObjects() {
548 if (!mSessionOpen)
549 return MTP_RESPONSE_SESSION_NOT_OPEN;
550 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
551 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
552 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
Mike Lockwooddc3185e2011-06-17 13:44:24 -0400553 // 0x00000000 for all objects
Mike Lockwooda8494402011-02-18 09:07:14 -0500554 if (!hasStorage(storageID))
555 return MTP_RESPONSE_INVALID_STORAGE_ID;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400556
557 int count = mDatabase->getNumObjects(storageID, format, parent);
558 if (count >= 0) {
559 mResponse.setParameter(1, count);
560 return MTP_RESPONSE_OK;
561 } else {
562 mResponse.setParameter(1, 0);
563 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
564 }
565}
566
Mike Lockwood438344f2010-08-03 15:30:09 -0400567MtpResponseCode MtpServer::doGetObjectReferences() {
568 if (!mSessionOpen)
569 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500570 if (!hasStorage())
571 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
572 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400573
574 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400575 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400576 if (handles) {
577 mData.putAUInt32(handles);
578 delete handles;
579 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400580 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400581 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400582 return MTP_RESPONSE_OK;
583}
584
585MtpResponseCode MtpServer::doSetObjectReferences() {
586 if (!mSessionOpen)
587 return MTP_RESPONSE_SESSION_NOT_OPEN;
Mike Lockwooda8494402011-02-18 09:07:14 -0500588 if (!hasStorage())
589 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood438344f2010-08-03 15:30:09 -0400590 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwooda8494402011-02-18 09:07:14 -0500591
Mike Lockwood438344f2010-08-03 15:30:09 -0400592 MtpObjectHandleList* references = mData.getAUInt32();
593 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
594 delete references;
595 return result;
596}
597
Mike Lockwood16864ba2010-05-11 17:16:59 -0400598MtpResponseCode MtpServer::doGetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500599 if (!hasStorage())
600 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400601 MtpObjectHandle handle = mRequest.getParameter(1);
602 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800603 LOGV("GetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400604 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400605
Mike Lockwood8277cec2010-08-10 15:20:35 -0400606 return mDatabase->getObjectPropertyValue(handle, property, mData);
607}
608
609MtpResponseCode MtpServer::doSetObjectPropValue() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500610 if (!hasStorage())
611 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400612 MtpObjectHandle handle = mRequest.getParameter(1);
613 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800614 LOGV("SetObjectPropValue %d %s\n", handle,
Mike Lockwood8277cec2010-08-10 15:20:35 -0400615 MtpDebug::getObjectPropCodeName(property));
616
617 return mDatabase->setObjectPropertyValue(handle, property, mData);
618}
619
620MtpResponseCode MtpServer::doGetDevicePropValue() {
621 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800622 LOGV("GetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400623 MtpDebug::getDevicePropCodeName(property));
624
625 return mDatabase->getDevicePropertyValue(property, mData);
626}
627
628MtpResponseCode MtpServer::doSetDevicePropValue() {
629 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800630 LOGV("SetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400631 MtpDebug::getDevicePropCodeName(property));
632
633 return mDatabase->setDevicePropertyValue(property, mData);
634}
635
636MtpResponseCode MtpServer::doResetDevicePropValue() {
637 MtpDeviceProperty property = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800638 LOGV("ResetDevicePropValue %s\n",
Mike Lockwood8277cec2010-08-10 15:20:35 -0400639 MtpDebug::getDevicePropCodeName(property));
640
641 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400642}
643
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400644MtpResponseCode MtpServer::doGetObjectPropList() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500645 if (!hasStorage())
646 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400647
648 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood40ce1f22010-12-01 18:46:23 -0500649 // use uint32_t so we can support 0xFFFFFFFF
650 uint32_t format = mRequest.getParameter(2);
651 uint32_t property = mRequest.getParameter(3);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400652 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500653 int depth = mRequest.getParameter(5);
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800654 LOGV("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400655 handle, MtpDebug::getFormatCodeName(format),
656 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
657
658 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
659}
660
Mike Lockwood16864ba2010-05-11 17:16:59 -0400661MtpResponseCode MtpServer::doGetObjectInfo() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500662 if (!hasStorage())
663 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400664 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700665 MtpObjectInfo info(handle);
666 MtpResponseCode result = mDatabase->getObjectInfo(handle, info);
667 if (result == MTP_RESPONSE_OK) {
668 char date[20];
669
670 mData.putUInt32(info.mStorageID);
671 mData.putUInt16(info.mFormat);
672 mData.putUInt16(info.mProtectionStatus);
673
674 // if object is being edited the database size may be out of date
675 uint32_t size = info.mCompressedSize;
676 ObjectEdit* edit = getEditObject(handle);
677 if (edit)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -0700678 size = (edit->mSize > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700679 mData.putUInt32(size);
680
681 mData.putUInt16(info.mThumbFormat);
682 mData.putUInt32(info.mThumbCompressedSize);
683 mData.putUInt32(info.mThumbPixWidth);
684 mData.putUInt32(info.mThumbPixHeight);
685 mData.putUInt32(info.mImagePixWidth);
686 mData.putUInt32(info.mImagePixHeight);
687 mData.putUInt32(info.mImagePixDepth);
688 mData.putUInt32(info.mParent);
689 mData.putUInt16(info.mAssociationType);
690 mData.putUInt32(info.mAssociationDesc);
691 mData.putUInt32(info.mSequenceNumber);
692 mData.putString(info.mName);
693 mData.putEmptyString(); // date created
694 formatDateTime(info.mDateModified, date, sizeof(date));
695 mData.putString(date); // date modified
696 mData.putEmptyString(); // keywords
697 }
698 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400699}
700
701MtpResponseCode MtpServer::doGetObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500702 if (!hasStorage())
703 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400704 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400705 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400706 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800707 MtpObjectFormat format;
708 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400709 if (result != MTP_RESPONSE_OK)
710 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400711
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400712 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400713 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400714 mfr.fd = open(filePath, O_RDONLY);
715 if (mfr.fd < 0) {
716 return MTP_RESPONSE_GENERAL_ERROR;
717 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400718 mfr.offset = 0;
719 mfr.length = fileLength;
720
721 // send data header
722 mData.setOperationCode(mRequest.getOperationCode());
723 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood23d20712010-10-11 17:31:44 -0400724 mData.writeDataHeader(mFD, fileLength + MTP_CONTAINER_HEADER_SIZE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400725
726 // then transfer the file
727 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400728 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400729 if (ret < 0) {
730 if (errno == ECANCELED)
731 return MTP_RESPONSE_TRANSACTION_CANCELLED;
732 else
733 return MTP_RESPONSE_GENERAL_ERROR;
734 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400735 return MTP_RESPONSE_OK;
736}
737
Mike Lockwood64000782011-04-24 18:40:17 -0700738MtpResponseCode MtpServer::doGetThumb() {
739 MtpObjectHandle handle = mRequest.getParameter(1);
740 size_t thumbSize;
741 void* thumb = mDatabase->getThumbnail(handle, thumbSize);
742 if (thumb) {
743 // send data
744 mData.setOperationCode(mRequest.getOperationCode());
745 mData.setTransactionID(mRequest.getTransactionID());
746 mData.writeData(mFD, thumb, thumbSize);
747 free(thumb);
748 return MTP_RESPONSE_OK;
749 } else {
750 return MTP_RESPONSE_GENERAL_ERROR;
751 }
752}
753
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700754MtpResponseCode MtpServer::doGetPartialObject(MtpOperationCode operation) {
Mike Lockwooda8494402011-02-18 09:07:14 -0500755 if (!hasStorage())
756 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500757 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700758 uint64_t offset;
759 uint32_t length;
760 offset = mRequest.getParameter(2);
761 if (operation == MTP_OPERATION_GET_PARTIAL_OBJECT_64) {
762 // android extension with 64 bit offset
763 uint64_t offset2 = mRequest.getParameter(3);
764 offset = offset | (offset2 << 32);
765 length = mRequest.getParameter(4);
766 } else {
767 // standard GetPartialObject
768 length = mRequest.getParameter(3);
769 }
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500770 MtpString pathBuf;
771 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800772 MtpObjectFormat format;
773 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500774 if (result != MTP_RESPONSE_OK)
775 return result;
776 if (offset + length > fileLength)
777 length = fileLength - offset;
778
779 const char* filePath = (const char *)pathBuf;
780 mtp_file_range mfr;
781 mfr.fd = open(filePath, O_RDONLY);
782 if (mfr.fd < 0) {
783 return MTP_RESPONSE_GENERAL_ERROR;
784 }
785 mfr.offset = offset;
786 mfr.length = length;
787 mResponse.setParameter(1, length);
788
789 // send data header
790 mData.setOperationCode(mRequest.getOperationCode());
791 mData.setTransactionID(mRequest.getTransactionID());
792 mData.writeDataHeader(mFD, length + MTP_CONTAINER_HEADER_SIZE);
793
794 // then transfer the file
795 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
796 close(mfr.fd);
797 if (ret < 0) {
798 if (errno == ECANCELED)
799 return MTP_RESPONSE_TRANSACTION_CANCELLED;
800 else
801 return MTP_RESPONSE_GENERAL_ERROR;
802 }
803 return MTP_RESPONSE_OK;
804}
805
Mike Lockwood16864ba2010-05-11 17:16:59 -0400806MtpResponseCode MtpServer::doSendObjectInfo() {
807 MtpString path;
808 MtpStorageID storageID = mRequest.getParameter(1);
809 MtpStorage* storage = getStorage(storageID);
810 MtpObjectHandle parent = mRequest.getParameter(2);
811 if (!storage)
812 return MTP_RESPONSE_INVALID_STORAGE_ID;
813
814 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400815 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400816 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400817 parent = 0;
818 } else {
Mike Lockwoodfd346262010-12-08 16:08:01 -0800819 int64_t length;
820 MtpObjectFormat format;
821 int result = mDatabase->getObjectFilePath(parent, path, length, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400822 if (result != MTP_RESPONSE_OK)
823 return result;
Mike Lockwoodfd346262010-12-08 16:08:01 -0800824 if (format != MTP_FORMAT_ASSOCIATION)
825 return MTP_RESPONSE_INVALID_PARENT_OBJECT;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400826 }
827
828 // read only the fields we need
829 mData.getUInt32(); // storage ID
830 MtpObjectFormat format = mData.getUInt16();
831 mData.getUInt16(); // protection status
832 mSendObjectFileSize = mData.getUInt32();
833 mData.getUInt16(); // thumb format
834 mData.getUInt32(); // thumb compressed size
835 mData.getUInt32(); // thumb pix width
836 mData.getUInt32(); // thumb pix height
837 mData.getUInt32(); // image pix width
838 mData.getUInt32(); // image pix height
839 mData.getUInt32(); // image bit depth
840 mData.getUInt32(); // parent
841 uint16_t associationType = mData.getUInt16();
842 uint32_t associationDesc = mData.getUInt32(); // association desc
843 mData.getUInt32(); // sequence number
844 MtpStringBuffer name, created, modified;
845 mData.getString(name); // file name
846 mData.getString(created); // date created
847 mData.getString(modified); // date modified
848 // keywords follow
849
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800850 LOGV("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400851 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400852 if (!parseDateTime(modified, modifiedTime))
853 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400854
855 if (path[path.size() - 1] != '/')
856 path += "/";
857 path += (const char *)name;
858
Mike Lockwood20c3be02010-12-12 12:17:43 -0800859 // check space first
860 if (mSendObjectFileSize > storage->getFreeSpace())
861 return MTP_RESPONSE_STORAGE_FULL;
862
Mike Lockwooda8494402011-02-18 09:07:14 -0500863LOGD("path: %s parent: %d storageID: %08X", (const char*)path, parent, storageID);
Mike Lockwood4714b072010-07-12 08:49:01 -0400864 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
865 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400866 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400867 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400868 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400869
870 if (format == MTP_FORMAT_ASSOCIATION) {
871 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400872 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400873 umask(mask);
874 if (ret && ret != -EEXIST)
875 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400876 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwoodaa952402011-01-18 11:06:19 -0800877
878 // SendObject does not get sent for directories, so call endSendObject here instead
879 mDatabase->endSendObject(path, handle, MTP_FORMAT_ASSOCIATION, MTP_RESPONSE_OK);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400880 } else {
881 mSendObjectFilePath = path;
882 // save the handle for the SendObject call, which should follow
883 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400884 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400885 }
886
887 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400888 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400889 mResponse.setParameter(3, handle);
890
891 return MTP_RESPONSE_OK;
892}
893
894MtpResponseCode MtpServer::doSendObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -0500895 if (!hasStorage())
896 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood4714b072010-07-12 08:49:01 -0400897 MtpResponseCode result = MTP_RESPONSE_OK;
898 mode_t mask;
899 int ret;
900
Mike Lockwood16864ba2010-05-11 17:16:59 -0400901 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400902 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400903 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
904 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400905 }
906
907 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400908 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400909 // FIXME - check for errors here.
910
911 // reset so we don't attempt to send this back
912 mData.reset();
913
914 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400915 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
916 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400917 result = MTP_RESPONSE_GENERAL_ERROR;
918 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400919 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400920 fchown(mfr.fd, getuid(), mFileGroup);
921 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400922 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400923 fchmod(mfr.fd, mFilePermission);
924 umask(mask);
925
Mike Lockwood16864ba2010-05-11 17:16:59 -0400926 mfr.offset = 0;
927 mfr.length = mSendObjectFileSize;
928
Mike Lockwood59d6ae52011-01-21 21:00:54 -0800929 LOGV("receiving %s\n", (const char *)mSendObjectFilePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400930 // transfer the file
931 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400932 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400933
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400934 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400935
Mike Lockwood916076c2010-06-04 09:49:21 -0400936 if (ret < 0) {
937 unlink(mSendObjectFilePath);
938 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400939 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400940 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400941 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400942 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400943
944done:
945 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoodaa952402011-01-18 11:06:19 -0800946 result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -0400947 mSendObjectHandle = kInvalidObjectHandle;
948 mSendObjectFormat = 0;
949 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400950}
951
Mike Lockwoodd3211492010-09-13 17:15:58 -0400952static void deleteRecursive(const char* path) {
953 char pathbuf[PATH_MAX];
954 int pathLength = strlen(path);
955 if (pathLength >= sizeof(pathbuf) - 1) {
956 LOGE("path too long: %s\n", path);
957 }
958 strcpy(pathbuf, path);
959 if (pathbuf[pathLength - 1] != '/') {
960 pathbuf[pathLength++] = '/';
961 }
962 char* fileSpot = pathbuf + pathLength;
963 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
964
965 DIR* dir = opendir(path);
966 if (!dir) {
967 LOGE("opendir %s failed: %s", path, strerror(errno));
968 return;
969 }
970
971 struct dirent* entry;
972 while ((entry = readdir(dir))) {
973 const char* name = entry->d_name;
974
975 // ignore "." and ".."
976 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
977 continue;
978 }
979
980 int nameLength = strlen(name);
981 if (nameLength > pathRemaining) {
982 LOGE("path %s/%s too long\n", path, name);
983 continue;
984 }
985 strcpy(fileSpot, name);
986
987 int type = entry->d_type;
988 if (entry->d_type == DT_DIR) {
989 deleteRecursive(pathbuf);
990 rmdir(pathbuf);
991 } else {
992 unlink(pathbuf);
993 }
994 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -0500995 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -0400996}
997
998static void deletePath(const char* path) {
999 struct stat statbuf;
1000 if (stat(path, &statbuf) == 0) {
1001 if (S_ISDIR(statbuf.st_mode)) {
1002 deleteRecursive(path);
1003 rmdir(path);
1004 } else {
1005 unlink(path);
1006 }
1007 } else {
1008 LOGE("deletePath stat failed for %s: %s", path, strerror(errno));
1009 }
1010}
1011
Mike Lockwood16864ba2010-05-11 17:16:59 -04001012MtpResponseCode MtpServer::doDeleteObject() {
Mike Lockwooda8494402011-02-18 09:07:14 -05001013 if (!hasStorage())
1014 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001015 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001016 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001017 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
1018 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -04001019
1020 MtpString filePath;
1021 int64_t fileLength;
Mike Lockwoodfd346262010-12-08 16:08:01 -08001022 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001023 if (result == MTP_RESPONSE_OK) {
1024 LOGV("deleting %s", (const char *)filePath);
Mike Lockwoodd3211492010-09-13 17:15:58 -04001025 deletePath((const char *)filePath);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -04001026 return mDatabase->deleteFile(handle);
1027 } else {
1028 return result;
1029 }
Mike Lockwood16864ba2010-05-11 17:16:59 -04001030}
1031
1032MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001033 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -04001034 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood59d6ae52011-01-21 21:00:54 -08001035 LOGV("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
Mike Lockwood8277cec2010-08-10 15:20:35 -04001036 MtpDebug::getFormatCodeName(format));
1037 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001038 if (!property)
1039 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001040 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -04001041 delete property;
1042 return MTP_RESPONSE_OK;
1043}
1044
1045MtpResponseCode MtpServer::doGetDevicePropDesc() {
1046 MtpDeviceProperty propCode = mRequest.getParameter(1);
Mike Lockwood59d6ae52011-01-21 21:00:54 -08001047 LOGV("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
Mike Lockwood8277cec2010-08-10 15:20:35 -04001048 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
1049 if (!property)
1050 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
1051 property->write(mData);
1052 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -04001053 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -04001054}
Mike Lockwood7850ef92010-05-14 10:10:36 -04001055
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001056MtpResponseCode MtpServer::doSendPartialObject() {
1057 if (!hasStorage())
1058 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
1059 MtpObjectHandle handle = mRequest.getParameter(1);
1060 uint64_t offset = mRequest.getParameter(2);
1061 uint64_t offset2 = mRequest.getParameter(3);
1062 offset = offset | (offset2 << 32);
1063 uint32_t length = mRequest.getParameter(4);
1064
1065 ObjectEdit* edit = getEditObject(handle);
1066 if (!edit) {
1067 LOGE("object not open for edit in doSendPartialObject");
1068 return MTP_RESPONSE_GENERAL_ERROR;
1069 }
1070
1071 // can't start writing past the end of the file
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001072 if (offset > edit->mSize) {
1073 LOGD("writing past end of object, offset: %lld, edit->mSize: %lld", offset, edit->mSize);
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001074 return MTP_RESPONSE_GENERAL_ERROR;
1075 }
1076
1077 // read the header
1078 int ret = mData.readDataHeader(mFD);
1079 // FIXME - check for errors here.
1080
1081 // reset so we don't attempt to send this back
1082 mData.reset();
1083
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001084 const char* filePath = (const char *)edit->mPath;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001085 LOGV("receiving partial %s %lld %ld\n", filePath, offset, length);
1086 mtp_file_range mfr;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001087 mfr.fd = edit->mFD;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001088 mfr.offset = offset;
1089 mfr.length = length;
1090
1091 // transfer the file
1092 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
1093 LOGV("MTP_RECEIVE_FILE returned %d", ret);
1094 if (ret < 0) {
1095 mResponse.setParameter(1, 0);
1096 if (errno == ECANCELED)
1097 return MTP_RESPONSE_TRANSACTION_CANCELLED;
1098 else
1099 return MTP_RESPONSE_GENERAL_ERROR;
1100 }
1101 mResponse.setParameter(1, length);
1102 uint64_t end = offset + length;
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001103 if (end > edit->mSize) {
1104 edit->mSize = end;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001105 }
1106 return MTP_RESPONSE_OK;
1107}
1108
1109MtpResponseCode MtpServer::doTruncateObject() {
1110 MtpObjectHandle handle = mRequest.getParameter(1);
1111 ObjectEdit* edit = getEditObject(handle);
1112 if (!edit) {
1113 LOGE("object not open for edit in doTruncateObject");
1114 return MTP_RESPONSE_GENERAL_ERROR;
1115 }
1116
1117 uint64_t offset = mRequest.getParameter(2);
1118 uint64_t offset2 = mRequest.getParameter(3);
1119 offset |= (offset2 << 32);
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001120 if (ftruncate(edit->mFD, offset) != 0) {
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001121 return MTP_RESPONSE_GENERAL_ERROR;
1122 } else {
Mike Lockwoodc3f16e52011-04-25 12:56:21 -07001123 edit->mSize = offset;
Mike Lockwood7d77dcf2011-04-21 17:05:55 -07001124 return MTP_RESPONSE_OK;
1125 }
1126}
1127
1128MtpResponseCode MtpServer::doBeginEditObject() {
1129 MtpObjectHandle handle = mRequest.getParameter(1);
1130 if (getEditObject(handle)) {
1131 LOGE("object already open for edit in doBeginEditObject");
1132 return MTP_RESPONSE_GENERAL_ERROR;
1133 }
1134
1135 MtpString path;
1136 int64_t fileLength;
1137 MtpObjectFormat format;
1138 int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);
1139 if (result != MTP_RESPONSE_OK)
1140 return result;
1141
1142 int fd = open((const char *)path, O_RDWR | O_EXCL);
1143 if (fd < 0) {
1144 LOGE("open failed for %s in doBeginEditObject (%d)", (const char *)path, errno);
1145 return MTP_RESPONSE_GENERAL_ERROR;
1146 }
1147
1148 addEditObject(handle, path, fileLength, format, fd);
1149 return MTP_RESPONSE_OK;
1150}
1151
1152MtpResponseCode MtpServer::doEndEditObject() {
1153 MtpObjectHandle handle = mRequest.getParameter(1);
1154 ObjectEdit* edit = getEditObject(handle);
1155 if (!edit) {
1156 LOGE("object not open for edit in doEndEditObject");
1157 return MTP_RESPONSE_GENERAL_ERROR;
1158 }
1159
1160 commitEdit(edit);
1161 removeEditObject(handle);
1162 return MTP_RESPONSE_OK;
1163}
1164
Mike Lockwood7850ef92010-05-14 10:10:36 -04001165} // namespace android