blob: 1e414075c40012a204e6e02cd3db5d9666767bd3 [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 Lockwood9c04c4c2010-08-02 10:37:41 -0400473 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
474 if (result != MTP_RESPONSE_OK)
475 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400476
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400477 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400478 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400479 mfr.fd = open(filePath, O_RDONLY);
480 if (mfr.fd < 0) {
481 return MTP_RESPONSE_GENERAL_ERROR;
482 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400483 mfr.offset = 0;
484 mfr.length = fileLength;
485
486 // send data header
487 mData.setOperationCode(mRequest.getOperationCode());
488 mData.setTransactionID(mRequest.getTransactionID());
489 mData.writeDataHeader(mFD, fileLength);
490
491 // then transfer the file
492 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400493 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400494 if (ret < 0) {
495 if (errno == ECANCELED)
496 return MTP_RESPONSE_TRANSACTION_CANCELLED;
497 else
498 return MTP_RESPONSE_GENERAL_ERROR;
499 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400500 return MTP_RESPONSE_OK;
501}
502
503MtpResponseCode MtpServer::doSendObjectInfo() {
504 MtpString path;
505 MtpStorageID storageID = mRequest.getParameter(1);
506 MtpStorage* storage = getStorage(storageID);
507 MtpObjectHandle parent = mRequest.getParameter(2);
508 if (!storage)
509 return MTP_RESPONSE_INVALID_STORAGE_ID;
510
511 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400512 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400513 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400514 parent = 0;
515 } else {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400516 int64_t dummy;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400517 int result = mDatabase->getObjectFilePath(parent, path, dummy);
518 if (result != MTP_RESPONSE_OK)
519 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400520 }
521
522 // read only the fields we need
523 mData.getUInt32(); // storage ID
524 MtpObjectFormat format = mData.getUInt16();
525 mData.getUInt16(); // protection status
526 mSendObjectFileSize = mData.getUInt32();
527 mData.getUInt16(); // thumb format
528 mData.getUInt32(); // thumb compressed size
529 mData.getUInt32(); // thumb pix width
530 mData.getUInt32(); // thumb pix height
531 mData.getUInt32(); // image pix width
532 mData.getUInt32(); // image pix height
533 mData.getUInt32(); // image bit depth
534 mData.getUInt32(); // parent
535 uint16_t associationType = mData.getUInt16();
536 uint32_t associationDesc = mData.getUInt32(); // association desc
537 mData.getUInt32(); // sequence number
538 MtpStringBuffer name, created, modified;
539 mData.getString(name); // file name
540 mData.getString(created); // date created
541 mData.getString(modified); // date modified
542 // keywords follow
543
Mike Lockwoodfceef462010-05-14 15:35:17 -0400544 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400545 if (!parseDateTime(modified, modifiedTime))
546 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400547
548 if (path[path.size() - 1] != '/')
549 path += "/";
550 path += (const char *)name;
551
Mike Lockwood4714b072010-07-12 08:49:01 -0400552 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
553 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400554 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400555 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400556 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400557
558 if (format == MTP_FORMAT_ASSOCIATION) {
559 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400560 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400561 umask(mask);
562 if (ret && ret != -EEXIST)
563 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400564 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400565 } else {
566 mSendObjectFilePath = path;
567 // save the handle for the SendObject call, which should follow
568 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400569 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400570 }
571
572 mResponse.setParameter(1, storageID);
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400573 mResponse.setParameter(2, (parent == 0 ? 0xFFFFFFFF: parent));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400574 mResponse.setParameter(3, handle);
575
576 return MTP_RESPONSE_OK;
577}
578
579MtpResponseCode MtpServer::doSendObject() {
Mike Lockwood4714b072010-07-12 08:49:01 -0400580 MtpResponseCode result = MTP_RESPONSE_OK;
581 mode_t mask;
582 int ret;
583
Mike Lockwood16864ba2010-05-11 17:16:59 -0400584 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400585 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400586 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
587 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400588 }
589
590 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400591 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400592 // FIXME - check for errors here.
593
594 // reset so we don't attempt to send this back
595 mData.reset();
596
597 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400598 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
599 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400600 result = MTP_RESPONSE_GENERAL_ERROR;
601 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400602 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400603 fchown(mfr.fd, getuid(), mFileGroup);
604 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400605 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400606 fchmod(mfr.fd, mFilePermission);
607 umask(mask);
608
Mike Lockwood16864ba2010-05-11 17:16:59 -0400609 mfr.offset = 0;
610 mfr.length = mSendObjectFileSize;
611
612 // transfer the file
613 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400614 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400615
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400616 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400617
Mike Lockwood916076c2010-06-04 09:49:21 -0400618 if (ret < 0) {
619 unlink(mSendObjectFilePath);
620 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400621 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400622 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400623 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400624 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400625
626done:
627 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
628 result == MTP_RESPONSE_OK);
629 mSendObjectHandle = kInvalidObjectHandle;
630 mSendObjectFormat = 0;
631 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400632}
633
634MtpResponseCode MtpServer::doDeleteObject() {
635 MtpObjectHandle handle = mRequest.getParameter(1);
636 MtpObjectFormat format = mRequest.getParameter(1);
637 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
638 // FIXME - implement deleting objects by format
639 // FIXME - handle non-empty directories
640
641 MtpString filePath;
642 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400643 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength);
644 if (result == MTP_RESPONSE_OK) {
645 LOGV("deleting %s", (const char *)filePath);
646 // one of these should work
647 rmdir((const char *)filePath);
648 unlink((const char *)filePath);
649 return mDatabase->deleteFile(handle);
650 } else {
651 return result;
652 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400653}
654
655MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400656 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400657 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400658 MtpProperty* property = getObjectProperty(propCode);
659 if (!property)
660 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400661
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400662 property->write(mData);
663 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400664}
Mike Lockwood7850ef92010-05-14 10:10:36 -0400665
666} // namespace android