blob: adfe3a904b416a14bf66971e288070862e4316db [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>
24
Mike Lockwoodc42aa122010-06-14 17:58:08 -070025#include <cutils/properties.h>
26
Mike Lockwood16864ba2010-05-11 17:16:59 -040027#include "MtpDebug.h"
Mike Lockwood7f53a192010-07-09 10:45:22 -040028#include "MtpDatabase.h"
Mike Lockwood21ef7d02010-06-30 17:00:35 -040029#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040030#include "MtpServer.h"
31#include "MtpStorage.h"
32#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040033
Mike Lockwood8065e202010-07-15 13:36:52 -040034#include <linux/usb/f_mtp.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040035
Mike Lockwood7850ef92010-05-14 10:10:36 -040036namespace android {
37
Mike Lockwood16864ba2010-05-11 17:16:59 -040038static const MtpOperationCode kSupportedOperationCodes[] = {
39 MTP_OPERATION_GET_DEVICE_INFO,
40 MTP_OPERATION_OPEN_SESSION,
41 MTP_OPERATION_CLOSE_SESSION,
42 MTP_OPERATION_GET_STORAGE_IDS,
43 MTP_OPERATION_GET_STORAGE_INFO,
44 MTP_OPERATION_GET_NUM_OBJECTS,
45 MTP_OPERATION_GET_OBJECT_HANDLES,
46 MTP_OPERATION_GET_OBJECT_INFO,
47 MTP_OPERATION_GET_OBJECT,
48// MTP_OPERATION_GET_THUMB,
49 MTP_OPERATION_DELETE_OBJECT,
50 MTP_OPERATION_SEND_OBJECT_INFO,
51 MTP_OPERATION_SEND_OBJECT,
52// MTP_OPERATION_INITIATE_CAPTURE,
53// MTP_OPERATION_FORMAT_STORE,
54// MTP_OPERATION_RESET_DEVICE,
55// MTP_OPERATION_SELF_TEST,
56// MTP_OPERATION_SET_OBJECT_PROTECTION,
57// MTP_OPERATION_POWER_DOWN,
58 MTP_OPERATION_GET_DEVICE_PROP_DESC,
59 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
60 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
61 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
62// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
63// MTP_OPERATION_MOVE_OBJECT,
64// MTP_OPERATION_COPY_OBJECT,
65// MTP_OPERATION_GET_PARTIAL_OBJECT,
66// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
67 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
68// MTP_OPERATION_GET_OBJECT_PROP_DESC,
69 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
70 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
71// MTP_OPERATION_GET_OBJECT_REFERENCES,
72// MTP_OPERATION_SET_OBJECT_REFERENCES,
73// MTP_OPERATION_SKIP,
74};
75
Mike Lockwood873871f2010-07-12 18:54:16 -040076static const MtpEventCode kSupportedEventCodes[] = {
77 MTP_EVENT_OBJECT_ADDED,
78 MTP_EVENT_OBJECT_REMOVED,
79};
80
Mike Lockwood16864ba2010-05-11 17:16:59 -040081static const MtpObjectProperty kSupportedObjectProperties[] = {
82 MTP_PROPERTY_STORAGE_ID,
83 MTP_PROPERTY_OBJECT_FORMAT,
84 MTP_PROPERTY_OBJECT_SIZE,
85 MTP_PROPERTY_OBJECT_FILE_NAME,
86 MTP_PROPERTY_PARENT_OBJECT,
87};
88
89static const MtpObjectFormat kSupportedPlaybackFormats[] = {
Mike Lockwoodfceef462010-05-14 15:35:17 -040090 // MTP_FORMAT_UNDEFINED,
Mike Lockwood16864ba2010-05-11 17:16:59 -040091 MTP_FORMAT_ASSOCIATION,
Mike Lockwoodfceef462010-05-14 15:35:17 -040092 // MTP_FORMAT_TEXT,
93 // MTP_FORMAT_HTML,
Mike Lockwood16864ba2010-05-11 17:16:59 -040094 MTP_FORMAT_MP3,
Mike Lockwoodfceef462010-05-14 15:35:17 -040095 //MTP_FORMAT_AVI,
96 MTP_FORMAT_MPEG,
97 // MTP_FORMAT_ASF,
98 MTP_FORMAT_EXIF_JPEG,
99 MTP_FORMAT_TIFF_EP,
100 // MTP_FORMAT_BMP,
101 MTP_FORMAT_GIF,
102 MTP_FORMAT_JFIF,
103 MTP_FORMAT_PNG,
104 MTP_FORMAT_TIFF,
105 MTP_FORMAT_WMA,
106 MTP_FORMAT_OGG,
107 MTP_FORMAT_AAC,
108 // MTP_FORMAT_FLAC,
109 // MTP_FORMAT_WMV,
110 MTP_FORMAT_MP4_CONTAINER,
111 MTP_FORMAT_MP2,
112 MTP_FORMAT_3GP_CONTAINER,
113 // MTP_FORMAT_ABSTRACT_AUDIO_ALBUM,
114 // MTP_FORMAT_ABSTRACT_AV_PLAYLIST,
115 // MTP_FORMAT_WPL_PLAYLIST,
116 // MTP_FORMAT_M3U_PLAYLIST,
117 // MTP_FORMAT_MPL_PLAYLIST,
118 // MTP_FORMAT_PLS_PLAYLIST,
Mike Lockwood16864ba2010-05-11 17:16:59 -0400119};
120
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400121MtpServer::MtpServer(int fd, MtpDatabase* database,
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400122 int fileGroup, int filePerm, int directoryPerm)
Mike Lockwood16864ba2010-05-11 17:16:59 -0400123 : mFD(fd),
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400124 mDatabase(database),
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400125 mFileGroup(fileGroup),
126 mFilePermission(filePerm),
127 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400128 mSessionID(0),
129 mSessionOpen(false),
130 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -0400131 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400132 mSendObjectFileSize(0)
133{
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400134 initObjectProperties();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400135}
136
137MtpServer::~MtpServer() {
138}
139
140void MtpServer::addStorage(const char* filePath) {
141 int index = mStorages.size() + 1;
142 index |= index << 16; // set high and low part to our index
143 MtpStorage* storage = new MtpStorage(index, filePath, mDatabase);
144 addStorage(storage);
145}
146
147MtpStorage* MtpServer::getStorage(MtpStorageID id) {
148 for (int i = 0; i < mStorages.size(); i++) {
149 MtpStorage* storage = mStorages[i];
150 if (storage->getStorageID() == id)
151 return storage;
152 }
153 return NULL;
154}
155
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156void MtpServer::run() {
157 int fd = mFD;
158
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400159 LOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400160
161 while (1) {
162 int ret = mRequest.read(fd);
163 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400164 LOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400165 if (errno == ECANCELED) {
166 // return to top of loop and wait for next command
167 continue;
168 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169 break;
170 }
171 MtpOperationCode operation = mRequest.getOperationCode();
172 MtpTransactionID transaction = mRequest.getTransactionID();
173
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400174 LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400175 mRequest.dump();
176
177 // FIXME need to generalize this
178 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO);
179 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 Lockwood916076c2010-06-04 09:49:21 -0400200 mData.dump();
201 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 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 }
227}
228
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400229MtpProperty* MtpServer::getObjectProperty(MtpPropertyCode propCode) {
230 for (int i = 0; i < mObjectProperties.size(); i++) {
231 MtpProperty* property = mObjectProperties[i];
232 if (property->getPropertyCode() == propCode)
233 return property;
234 }
235 return NULL;
236}
237
238MtpProperty* MtpServer::getDeviceProperty(MtpPropertyCode propCode) {
239 for (int i = 0; i < mDeviceProperties.size(); i++) {
240 MtpProperty* property = mDeviceProperties[i];
241 if (property->getPropertyCode() == propCode)
242 return property;
243 }
244 return NULL;
245}
246
Mike Lockwood873871f2010-07-12 18:54:16 -0400247void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400248 if (mSessionOpen) {
249 LOGD("sendObjectAdded %d\n", handle);
250 mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED);
251 mEvent.setTransactionID(mRequest.getTransactionID());
252 mEvent.setParameter(1, handle);
253 int ret = mEvent.write(mFD);
254 LOGD("mEvent.write returned %d\n", ret);
255 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400256}
257
258void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400259 if (mSessionOpen) {
260 LOGD("sendObjectRemoved %d\n", handle);
261 mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED);
262 mEvent.setTransactionID(mRequest.getTransactionID());
263 mEvent.setParameter(1, handle);
264 int ret = mEvent.write(mFD);
265 LOGD("mEvent.write returned %d\n", ret);
266 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400267}
268
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400269void MtpServer::initObjectProperties() {
270 mObjectProperties.push(new MtpProperty(MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT16));
271 mObjectProperties.push(new MtpProperty(MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16));
272 mObjectProperties.push(new MtpProperty(MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64));
273 mObjectProperties.push(new MtpProperty(MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR));
274 mObjectProperties.push(new MtpProperty(MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32));
275}
276
Mike Lockwood916076c2010-06-04 09:49:21 -0400277bool MtpServer::handleRequest() {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400278 MtpOperationCode operation = mRequest.getOperationCode();
279 MtpResponseCode response;
280
281 mResponse.reset();
282
283 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
284 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400285 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400286 mSendObjectHandle = kInvalidObjectHandle;
287 }
288
289 switch (operation) {
290 case MTP_OPERATION_GET_DEVICE_INFO:
291 response = doGetDeviceInfo();
292 break;
293 case MTP_OPERATION_OPEN_SESSION:
294 response = doOpenSession();
295 break;
296 case MTP_OPERATION_CLOSE_SESSION:
297 response = doCloseSession();
298 break;
299 case MTP_OPERATION_GET_STORAGE_IDS:
300 response = doGetStorageIDs();
301 break;
302 case MTP_OPERATION_GET_STORAGE_INFO:
303 response = doGetStorageInfo();
304 break;
305 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
306 response = doGetObjectPropsSupported();
307 break;
308 case MTP_OPERATION_GET_OBJECT_HANDLES:
309 response = doGetObjectHandles();
310 break;
311 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
312 response = doGetObjectPropValue();
313 break;
314 case MTP_OPERATION_GET_OBJECT_INFO:
315 response = doGetObjectInfo();
316 break;
317 case MTP_OPERATION_GET_OBJECT:
318 response = doGetObject();
319 break;
320 case MTP_OPERATION_SEND_OBJECT_INFO:
321 response = doSendObjectInfo();
322 break;
323 case MTP_OPERATION_SEND_OBJECT:
324 response = doSendObject();
325 break;
326 case MTP_OPERATION_DELETE_OBJECT:
327 response = doDeleteObject();
328 break;
329 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400330 response = doGetObjectPropDesc();
331 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400332 default:
333 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
334 break;
335 }
336
Mike Lockwood916076c2010-06-04 09:49:21 -0400337 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
338 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400339 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400340 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400341}
342
343MtpResponseCode MtpServer::doGetDeviceInfo() {
344 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700345 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400346
347 // fill in device info
348 mData.putUInt16(MTP_STANDARD_VERSION);
349 mData.putUInt32(6); // MTP Vendor Extension ID
350 mData.putUInt16(MTP_STANDARD_VERSION);
351 string.set("microsoft.com: 1.0;");
352 mData.putString(string); // MTP Extensions
353 mData.putUInt16(0); //Functional Mode
354 mData.putAUInt16(kSupportedOperationCodes,
355 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400356 mData.putAUInt16(kSupportedEventCodes,
357 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood16864ba2010-05-11 17:16:59 -0400358 mData.putEmptyArray(); // Device Properties Supported
359 mData.putEmptyArray(); // Capture Formats
360 mData.putAUInt16(kSupportedPlaybackFormats,
361 sizeof(kSupportedPlaybackFormats) / sizeof(uint16_t)); // Playback Formats
362 // FIXME
363 string.set("Google, Inc.");
364 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700365
366 property_get("ro.product.model", prop_value, "MTP Device");
367 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400368 mData.putString(string); // Model
369 string.set("1.0");
370 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700371
372 property_get("ro.serialno", prop_value, "????????");
373 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400374 mData.putString(string); // Serial Number
375
376 return MTP_RESPONSE_OK;
377}
378
379MtpResponseCode MtpServer::doOpenSession() {
380 if (mSessionOpen) {
381 mResponse.setParameter(1, mSessionID);
382 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
383 }
384 mSessionID = mRequest.getParameter(1);
385 mSessionOpen = true;
386 return MTP_RESPONSE_OK;
387}
388
389MtpResponseCode MtpServer::doCloseSession() {
390 if (!mSessionOpen)
391 return MTP_RESPONSE_SESSION_NOT_OPEN;
392 mSessionID = 0;
393 mSessionOpen = false;
394 return MTP_RESPONSE_OK;
395}
396
397MtpResponseCode MtpServer::doGetStorageIDs() {
398 if (!mSessionOpen)
399 return MTP_RESPONSE_SESSION_NOT_OPEN;
400
401 int count = mStorages.size();
402 mData.putUInt32(count);
403 for (int i = 0; i < count; i++)
404 mData.putUInt32(mStorages[i]->getStorageID());
405
406 return MTP_RESPONSE_OK;
407}
408
409MtpResponseCode MtpServer::doGetStorageInfo() {
410 MtpStringBuffer string;
411
412 if (!mSessionOpen)
413 return MTP_RESPONSE_SESSION_NOT_OPEN;
414 MtpStorageID id = mRequest.getParameter(1);
415 MtpStorage* storage = getStorage(id);
416 if (!storage)
417 return MTP_RESPONSE_INVALID_STORAGE_ID;
418
419 mData.putUInt16(storage->getType());
420 mData.putUInt16(storage->getFileSystemType());
421 mData.putUInt16(storage->getAccessCapability());
422 mData.putUInt64(storage->getMaxCapacity());
423 mData.putUInt64(storage->getFreeSpace());
424 mData.putUInt32(1024*1024*1024); // Free Space in Objects
425 string.set(storage->getDescription());
426 mData.putString(string);
427 mData.putEmptyString(); // Volume Identifier
428
429 return MTP_RESPONSE_OK;
430}
431
432MtpResponseCode MtpServer::doGetObjectPropsSupported() {
433 if (!mSessionOpen)
434 return MTP_RESPONSE_SESSION_NOT_OPEN;
435 MtpObjectFormat format = mRequest.getParameter(1);
436 mData.putAUInt16(kSupportedObjectProperties,
437 sizeof(kSupportedObjectProperties) / sizeof(uint16_t));
438 return MTP_RESPONSE_OK;
439}
440
441MtpResponseCode MtpServer::doGetObjectHandles() {
442 if (!mSessionOpen)
443 return MTP_RESPONSE_SESSION_NOT_OPEN;
444 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400445 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400446 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
447 // 0x00000000 for all objects?
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400448 if (parent == 0xFFFFFFFF)
449 parent = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400450
451 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
452 mData.putAUInt32(handles);
453 delete handles;
454 return MTP_RESPONSE_OK;
455}
456
457MtpResponseCode MtpServer::doGetObjectPropValue() {
458 MtpObjectHandle handle = mRequest.getParameter(1);
459 MtpObjectProperty property = mRequest.getParameter(2);
460
461 return mDatabase->getObjectProperty(handle, property, mData);
462}
463
464MtpResponseCode MtpServer::doGetObjectInfo() {
465 MtpObjectHandle handle = mRequest.getParameter(1);
466 return mDatabase->getObjectInfo(handle, mData);
467}
468
469MtpResponseCode MtpServer::doGetObject() {
470 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400471 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400472 int64_t fileLength;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400473 if (!mDatabase->getObjectFilePath(handle, pathBuf, fileLength))
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400475 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400476
477 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400478 mfr.fd = open(filePath, O_RDONLY);
479 if (mfr.fd < 0) {
480 return MTP_RESPONSE_GENERAL_ERROR;
481 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400482 mfr.offset = 0;
483 mfr.length = fileLength;
484
485 // send data header
486 mData.setOperationCode(mRequest.getOperationCode());
487 mData.setTransactionID(mRequest.getTransactionID());
488 mData.writeDataHeader(mFD, fileLength);
489
490 // then transfer the file
491 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400492 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400493 if (ret < 0) {
494 if (errno == ECANCELED)
495 return MTP_RESPONSE_TRANSACTION_CANCELLED;
496 else
497 return MTP_RESPONSE_GENERAL_ERROR;
498 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400499 return MTP_RESPONSE_OK;
500}
501
502MtpResponseCode MtpServer::doSendObjectInfo() {
503 MtpString path;
504 MtpStorageID storageID = mRequest.getParameter(1);
505 MtpStorage* storage = getStorage(storageID);
506 MtpObjectHandle parent = mRequest.getParameter(2);
507 if (!storage)
508 return MTP_RESPONSE_INVALID_STORAGE_ID;
509
510 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400511 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400512 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400513 parent = 0;
514 } else {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400515 int64_t dummy;
516 if (!mDatabase->getObjectFilePath(parent, path, dummy))
517 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
518 }
519
520 // read only the fields we need
521 mData.getUInt32(); // storage ID
522 MtpObjectFormat format = mData.getUInt16();
523 mData.getUInt16(); // protection status
524 mSendObjectFileSize = mData.getUInt32();
525 mData.getUInt16(); // thumb format
526 mData.getUInt32(); // thumb compressed size
527 mData.getUInt32(); // thumb pix width
528 mData.getUInt32(); // thumb pix height
529 mData.getUInt32(); // image pix width
530 mData.getUInt32(); // image pix height
531 mData.getUInt32(); // image bit depth
532 mData.getUInt32(); // parent
533 uint16_t associationType = mData.getUInt16();
534 uint32_t associationDesc = mData.getUInt32(); // association desc
535 mData.getUInt32(); // sequence number
536 MtpStringBuffer name, created, modified;
537 mData.getString(name); // file name
538 mData.getString(created); // date created
539 mData.getString(modified); // date modified
540 // keywords follow
541
Mike Lockwoodfceef462010-05-14 15:35:17 -0400542 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400543 if (!parseDateTime(modified, modifiedTime))
544 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400545
546 if (path[path.size() - 1] != '/')
547 path += "/";
548 path += (const char *)name;
549
Mike Lockwoodfceef462010-05-14 15:35:17 -0400550 mDatabase->beginTransaction();
Mike Lockwood4714b072010-07-12 08:49:01 -0400551 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
552 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400553 if (handle == kInvalidObjectHandle) {
554 mDatabase->rollbackTransaction();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400555 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400556 }
Mike Lockwoodfceef462010-05-14 15:35:17 -0400557 mDatabase->commitTransaction();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400558
559 if (format == MTP_FORMAT_ASSOCIATION) {
560 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400561 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400562 umask(mask);
563 if (ret && ret != -EEXIST)
564 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400565 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400566 } else {
567 mSendObjectFilePath = path;
568 // save the handle for the SendObject call, which should follow
569 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400570 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400571 }
572
573 mResponse.setParameter(1, storageID);
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400574 mResponse.setParameter(2, (parent == 0 ? 0xFFFFFFFF: parent));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400575 mResponse.setParameter(3, handle);
576
577 return MTP_RESPONSE_OK;
578}
579
580MtpResponseCode MtpServer::doSendObject() {
Mike Lockwood4714b072010-07-12 08:49:01 -0400581 MtpResponseCode result = MTP_RESPONSE_OK;
582 mode_t mask;
583 int ret;
584
Mike Lockwood16864ba2010-05-11 17:16:59 -0400585 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400586 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400587 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
588 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400589 }
590
591 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400592 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400593 // FIXME - check for errors here.
594
595 // reset so we don't attempt to send this back
596 mData.reset();
597
598 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400599 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
600 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400601 result = MTP_RESPONSE_GENERAL_ERROR;
602 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400603 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400604 fchown(mfr.fd, getuid(), mFileGroup);
605 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400606 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400607 fchmod(mfr.fd, mFilePermission);
608 umask(mask);
609
Mike Lockwood16864ba2010-05-11 17:16:59 -0400610 mfr.offset = 0;
611 mfr.length = mSendObjectFileSize;
612
613 // transfer the file
614 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400615 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400616
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400617 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400618
Mike Lockwood916076c2010-06-04 09:49:21 -0400619 if (ret < 0) {
620 unlink(mSendObjectFilePath);
621 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400622 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400623 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400624 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400625 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400626
627done:
628 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
629 result == MTP_RESPONSE_OK);
630 mSendObjectHandle = kInvalidObjectHandle;
631 mSendObjectFormat = 0;
632 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400633}
634
635MtpResponseCode MtpServer::doDeleteObject() {
636 MtpObjectHandle handle = mRequest.getParameter(1);
637 MtpObjectFormat format = mRequest.getParameter(1);
638 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
639 // FIXME - implement deleting objects by format
640 // FIXME - handle non-empty directories
641
642 MtpString filePath;
643 int64_t fileLength;
644 if (!mDatabase->getObjectFilePath(handle, filePath, fileLength))
645 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
646
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400647 LOGV("deleting %s", (const char *)filePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400648 // one of these should work
649 rmdir((const char *)filePath);
650 unlink((const char *)filePath);
651
652 mDatabase->deleteFile(handle);
653
654 return MTP_RESPONSE_OK;
655}
656
657MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400658 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400659 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400660 MtpProperty* property = getObjectProperty(propCode);
661 if (!property)
662 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400663
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400664 property->write(mData);
665 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400666}
Mike Lockwood7850ef92010-05-14 10:10:36 -0400667
668} // namespace android