blob: c3755f31c1a3337407b0a744675567fb5230556f [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 Lockwood21ef7d02010-06-30 17:00:35 -040033#include "MtpProperty.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040034#include "MtpServer.h"
35#include "MtpStorage.h"
36#include "MtpStringBuffer.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040037
Mike Lockwood8065e202010-07-15 13:36:52 -040038#include <linux/usb/f_mtp.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040039
Mike Lockwood7850ef92010-05-14 10:10:36 -040040namespace android {
41
Mike Lockwood16864ba2010-05-11 17:16:59 -040042static const MtpOperationCode kSupportedOperationCodes[] = {
43 MTP_OPERATION_GET_DEVICE_INFO,
44 MTP_OPERATION_OPEN_SESSION,
45 MTP_OPERATION_CLOSE_SESSION,
46 MTP_OPERATION_GET_STORAGE_IDS,
47 MTP_OPERATION_GET_STORAGE_INFO,
48 MTP_OPERATION_GET_NUM_OBJECTS,
49 MTP_OPERATION_GET_OBJECT_HANDLES,
50 MTP_OPERATION_GET_OBJECT_INFO,
51 MTP_OPERATION_GET_OBJECT,
52// MTP_OPERATION_GET_THUMB,
53 MTP_OPERATION_DELETE_OBJECT,
54 MTP_OPERATION_SEND_OBJECT_INFO,
55 MTP_OPERATION_SEND_OBJECT,
56// MTP_OPERATION_INITIATE_CAPTURE,
57// MTP_OPERATION_FORMAT_STORE,
58// MTP_OPERATION_RESET_DEVICE,
59// MTP_OPERATION_SELF_TEST,
60// MTP_OPERATION_SET_OBJECT_PROTECTION,
61// MTP_OPERATION_POWER_DOWN,
Mike Lockwoode3e76c42010-09-02 14:57:30 -040062 MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040063 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
64 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
65 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040066// MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
67// MTP_OPERATION_MOVE_OBJECT,
68// MTP_OPERATION_COPY_OBJECT,
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -050069 MTP_OPERATION_GET_PARTIAL_OBJECT,
Mike Lockwood16864ba2010-05-11 17:16:59 -040070// MTP_OPERATION_INITIATE_OPEN_CAPTURE,
71 MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
Mike Lockwood8277cec2010-08-10 15:20:35 -040072 MTP_OPERATION_GET_OBJECT_PROP_DESC,
Mike Lockwood677f5702010-09-23 23:04:28 -040073 MTP_OPERATION_GET_OBJECT_PROP_VALUE,
74 MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwoodb6da06e2010-10-14 18:03:25 -040075 MTP_OPERATION_GET_OBJECT_PROP_LIST,
76// MTP_OPERATION_SET_OBJECT_PROP_LIST,
77// MTP_OPERATION_GET_INTERDEPENDENT_PROP_DESC,
78// MTP_OPERATION_SEND_OBJECT_PROP_LIST,
Mike Lockwood438344f2010-08-03 15:30:09 -040079 MTP_OPERATION_GET_OBJECT_REFERENCES,
80 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040081// MTP_OPERATION_SKIP,
82};
83
Mike Lockwood873871f2010-07-12 18:54:16 -040084static const MtpEventCode kSupportedEventCodes[] = {
85 MTP_EVENT_OBJECT_ADDED,
86 MTP_EVENT_OBJECT_REMOVED,
87};
88
Mike Lockwood1865a5d2010-07-03 00:44:05 -040089MtpServer::MtpServer(int fd, MtpDatabase* database,
Mike Lockwood8e2a2802010-07-02 15:15:07 -040090 int fileGroup, int filePerm, int directoryPerm)
Mike Lockwood16864ba2010-05-11 17:16:59 -040091 : mFD(fd),
Mike Lockwood1865a5d2010-07-03 00:44:05 -040092 mDatabase(database),
Mike Lockwood8e2a2802010-07-02 15:15:07 -040093 mFileGroup(fileGroup),
94 mFilePermission(filePerm),
95 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -040096 mSessionID(0),
97 mSessionOpen(false),
98 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -040099 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -0400100 mSendObjectFileSize(0)
101{
Mike Lockwood16864ba2010-05-11 17:16:59 -0400102}
103
104MtpServer::~MtpServer() {
105}
106
107void MtpServer::addStorage(const char* filePath) {
108 int index = mStorages.size() + 1;
109 index |= index << 16; // set high and low part to our index
110 MtpStorage* storage = new MtpStorage(index, filePath, mDatabase);
111 addStorage(storage);
112}
113
114MtpStorage* MtpServer::getStorage(MtpStorageID id) {
115 for (int i = 0; i < mStorages.size(); i++) {
116 MtpStorage* storage = mStorages[i];
117 if (storage->getStorageID() == id)
118 return storage;
119 }
120 return NULL;
121}
122
Mike Lockwood16864ba2010-05-11 17:16:59 -0400123void MtpServer::run() {
124 int fd = mFD;
125
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400126 LOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400127
128 while (1) {
129 int ret = mRequest.read(fd);
130 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400131 LOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400132 if (errno == ECANCELED) {
133 // return to top of loop and wait for next command
134 continue;
135 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400136 break;
137 }
138 MtpOperationCode operation = mRequest.getOperationCode();
139 MtpTransactionID transaction = mRequest.getTransactionID();
140
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400141 LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400142 mRequest.dump();
143
144 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400145 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400146 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
147 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
148 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400149 if (dataIn) {
150 int ret = mData.read(fd);
151 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400152 LOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400153 if (errno == ECANCELED) {
154 // return to top of loop and wait for next command
155 continue;
156 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400157 break;
158 }
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400159 LOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400160 mData.dump();
161 } else {
162 mData.reset();
163 }
164
Mike Lockwood916076c2010-06-04 09:49:21 -0400165 if (handleRequest()) {
166 if (!dataIn && mData.hasData()) {
167 mData.setOperationCode(operation);
168 mData.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400169 LOGV("sending data:");
Mike Lockwood23d20712010-10-11 17:31:44 -0400170 mData.dump();
Mike Lockwood916076c2010-06-04 09:49:21 -0400171 ret = mData.write(fd);
172 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400173 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400174 if (errno == ECANCELED) {
175 // return to top of loop and wait for next command
176 continue;
177 }
178 break;
179 }
180 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400181
Mike Lockwood916076c2010-06-04 09:49:21 -0400182 mResponse.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400183 LOGV("sending response %04X", mResponse.getResponseCode());
Mike Lockwood916076c2010-06-04 09:49:21 -0400184 ret = mResponse.write(fd);
Mike Lockwood23d20712010-10-11 17:31:44 -0400185 mResponse.dump();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400186 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400187 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400188 if (errno == ECANCELED) {
189 // return to top of loop and wait for next command
190 continue;
191 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400192 break;
193 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400194 } else {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400195 LOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400196 }
197 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400198
199 if (mSessionOpen)
200 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400201}
202
Mike Lockwood873871f2010-07-12 18:54:16 -0400203void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400204 if (mSessionOpen) {
205 LOGD("sendObjectAdded %d\n", handle);
206 mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED);
207 mEvent.setTransactionID(mRequest.getTransactionID());
208 mEvent.setParameter(1, handle);
209 int ret = mEvent.write(mFD);
210 LOGD("mEvent.write returned %d\n", ret);
211 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400212}
213
214void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400215 if (mSessionOpen) {
216 LOGD("sendObjectRemoved %d\n", handle);
217 mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED);
218 mEvent.setTransactionID(mRequest.getTransactionID());
219 mEvent.setParameter(1, handle);
220 int ret = mEvent.write(mFD);
221 LOGD("mEvent.write returned %d\n", ret);
222 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400223}
224
Mike Lockwood916076c2010-06-04 09:49:21 -0400225bool MtpServer::handleRequest() {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400226 MtpOperationCode operation = mRequest.getOperationCode();
227 MtpResponseCode response;
228
229 mResponse.reset();
230
231 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
232 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400233 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400234 mSendObjectHandle = kInvalidObjectHandle;
235 }
236
237 switch (operation) {
238 case MTP_OPERATION_GET_DEVICE_INFO:
239 response = doGetDeviceInfo();
240 break;
241 case MTP_OPERATION_OPEN_SESSION:
242 response = doOpenSession();
243 break;
244 case MTP_OPERATION_CLOSE_SESSION:
245 response = doCloseSession();
246 break;
247 case MTP_OPERATION_GET_STORAGE_IDS:
248 response = doGetStorageIDs();
249 break;
250 case MTP_OPERATION_GET_STORAGE_INFO:
251 response = doGetStorageInfo();
252 break;
253 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
254 response = doGetObjectPropsSupported();
255 break;
256 case MTP_OPERATION_GET_OBJECT_HANDLES:
257 response = doGetObjectHandles();
258 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400259 case MTP_OPERATION_GET_NUM_OBJECTS:
260 response = doGetNumObjects();
261 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400262 case MTP_OPERATION_GET_OBJECT_REFERENCES:
263 response = doGetObjectReferences();
264 break;
265 case MTP_OPERATION_SET_OBJECT_REFERENCES:
266 response = doSetObjectReferences();
267 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400268 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
269 response = doGetObjectPropValue();
270 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400271 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
272 response = doSetObjectPropValue();
273 break;
274 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
275 response = doGetDevicePropValue();
276 break;
277 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
278 response = doSetDevicePropValue();
279 break;
280 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
281 response = doResetDevicePropValue();
282 break;
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400283 case MTP_OPERATION_GET_OBJECT_PROP_LIST:
284 response = doGetObjectPropList();
285 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400286 case MTP_OPERATION_GET_OBJECT_INFO:
287 response = doGetObjectInfo();
288 break;
289 case MTP_OPERATION_GET_OBJECT:
290 response = doGetObject();
291 break;
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500292 case MTP_OPERATION_GET_PARTIAL_OBJECT:
293 response = doGetPartialObject();
294 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400295 case MTP_OPERATION_SEND_OBJECT_INFO:
296 response = doSendObjectInfo();
297 break;
298 case MTP_OPERATION_SEND_OBJECT:
299 response = doSendObject();
300 break;
301 case MTP_OPERATION_DELETE_OBJECT:
302 response = doDeleteObject();
303 break;
304 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400305 response = doGetObjectPropDesc();
306 break;
Mike Lockwoode3e76c42010-09-02 14:57:30 -0400307 case MTP_OPERATION_GET_DEVICE_PROP_DESC:
308 response = doGetDevicePropDesc();
309 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400310 default:
Mike Lockwooda881b442010-09-23 22:32:05 -0400311 LOGE("got unsupported command %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400312 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
313 break;
314 }
315
Mike Lockwood916076c2010-06-04 09:49:21 -0400316 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
317 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400318 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400319 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400320}
321
322MtpResponseCode MtpServer::doGetDeviceInfo() {
323 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700324 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400325
Mike Lockwood782aef12010-08-10 07:37:50 -0400326 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
327 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
328 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
329
Mike Lockwood16864ba2010-05-11 17:16:59 -0400330 // fill in device info
331 mData.putUInt16(MTP_STANDARD_VERSION);
332 mData.putUInt32(6); // MTP Vendor Extension ID
333 mData.putUInt16(MTP_STANDARD_VERSION);
334 string.set("microsoft.com: 1.0;");
335 mData.putString(string); // MTP Extensions
336 mData.putUInt16(0); //Functional Mode
337 mData.putAUInt16(kSupportedOperationCodes,
338 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400339 mData.putAUInt16(kSupportedEventCodes,
340 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400341 mData.putAUInt16(deviceProperties); // Device Properties Supported
342 mData.putAUInt16(captureFormats); // Capture Formats
343 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400344 // FIXME
345 string.set("Google, Inc.");
346 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700347
348 property_get("ro.product.model", prop_value, "MTP Device");
349 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400350 mData.putString(string); // Model
351 string.set("1.0");
352 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700353
354 property_get("ro.serialno", prop_value, "????????");
355 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400356 mData.putString(string); // Serial Number
357
Mike Lockwood782aef12010-08-10 07:37:50 -0400358 delete playbackFormats;
359 delete captureFormats;
360 delete deviceProperties;
361
Mike Lockwood16864ba2010-05-11 17:16:59 -0400362 return MTP_RESPONSE_OK;
363}
364
365MtpResponseCode MtpServer::doOpenSession() {
366 if (mSessionOpen) {
367 mResponse.setParameter(1, mSessionID);
368 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
369 }
370 mSessionID = mRequest.getParameter(1);
371 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400372
373 mDatabase->sessionStarted();
374
Mike Lockwood16864ba2010-05-11 17:16:59 -0400375 return MTP_RESPONSE_OK;
376}
377
378MtpResponseCode MtpServer::doCloseSession() {
379 if (!mSessionOpen)
380 return MTP_RESPONSE_SESSION_NOT_OPEN;
381 mSessionID = 0;
382 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400383 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400384 return MTP_RESPONSE_OK;
385}
386
387MtpResponseCode MtpServer::doGetStorageIDs() {
388 if (!mSessionOpen)
389 return MTP_RESPONSE_SESSION_NOT_OPEN;
390
391 int count = mStorages.size();
392 mData.putUInt32(count);
393 for (int i = 0; i < count; i++)
394 mData.putUInt32(mStorages[i]->getStorageID());
395
396 return MTP_RESPONSE_OK;
397}
398
399MtpResponseCode MtpServer::doGetStorageInfo() {
400 MtpStringBuffer string;
401
402 if (!mSessionOpen)
403 return MTP_RESPONSE_SESSION_NOT_OPEN;
404 MtpStorageID id = mRequest.getParameter(1);
405 MtpStorage* storage = getStorage(id);
406 if (!storage)
407 return MTP_RESPONSE_INVALID_STORAGE_ID;
408
409 mData.putUInt16(storage->getType());
410 mData.putUInt16(storage->getFileSystemType());
411 mData.putUInt16(storage->getAccessCapability());
412 mData.putUInt64(storage->getMaxCapacity());
413 mData.putUInt64(storage->getFreeSpace());
414 mData.putUInt32(1024*1024*1024); // Free Space in Objects
415 string.set(storage->getDescription());
416 mData.putString(string);
417 mData.putEmptyString(); // Volume Identifier
418
419 return MTP_RESPONSE_OK;
420}
421
422MtpResponseCode MtpServer::doGetObjectPropsSupported() {
423 if (!mSessionOpen)
424 return MTP_RESPONSE_SESSION_NOT_OPEN;
425 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood782aef12010-08-10 07:37:50 -0400426 MtpDevicePropertyList* properties = mDatabase->getSupportedObjectProperties(format);
427 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400428 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400429 return MTP_RESPONSE_OK;
430}
431
432MtpResponseCode MtpServer::doGetObjectHandles() {
433 if (!mSessionOpen)
434 return MTP_RESPONSE_SESSION_NOT_OPEN;
435 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400436 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400437 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
438 // 0x00000000 for all objects?
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400439 if (parent == 0xFFFFFFFF)
440 parent = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400441
442 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
443 mData.putAUInt32(handles);
444 delete handles;
445 return MTP_RESPONSE_OK;
446}
447
Mike Lockwood343af4e2010-08-02 10:52:20 -0400448MtpResponseCode MtpServer::doGetNumObjects() {
449 if (!mSessionOpen)
450 return MTP_RESPONSE_SESSION_NOT_OPEN;
451 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
452 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
453 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
454 // 0x00000000 for all objects?
455 if (parent == 0xFFFFFFFF)
456 parent = 0;
457
458 int count = mDatabase->getNumObjects(storageID, format, parent);
459 if (count >= 0) {
460 mResponse.setParameter(1, count);
461 return MTP_RESPONSE_OK;
462 } else {
463 mResponse.setParameter(1, 0);
464 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
465 }
466}
467
Mike Lockwood438344f2010-08-03 15:30:09 -0400468MtpResponseCode MtpServer::doGetObjectReferences() {
469 if (!mSessionOpen)
470 return MTP_RESPONSE_SESSION_NOT_OPEN;
471 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400472
473 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400474 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400475 if (handles) {
476 mData.putAUInt32(handles);
477 delete handles;
478 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400479 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400480 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400481 return MTP_RESPONSE_OK;
482}
483
484MtpResponseCode MtpServer::doSetObjectReferences() {
485 if (!mSessionOpen)
486 return MTP_RESPONSE_SESSION_NOT_OPEN;
487 MtpStorageID handle = mRequest.getParameter(1);
488 MtpObjectHandleList* references = mData.getAUInt32();
489 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
490 delete references;
491 return result;
492}
493
Mike Lockwood16864ba2010-05-11 17:16:59 -0400494MtpResponseCode MtpServer::doGetObjectPropValue() {
495 MtpObjectHandle handle = mRequest.getParameter(1);
496 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400497 LOGD("GetObjectPropValue %d %s\n", handle,
498 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400499
Mike Lockwood8277cec2010-08-10 15:20:35 -0400500 return mDatabase->getObjectPropertyValue(handle, property, mData);
501}
502
503MtpResponseCode MtpServer::doSetObjectPropValue() {
504 MtpObjectHandle handle = mRequest.getParameter(1);
505 MtpObjectProperty property = mRequest.getParameter(2);
506 LOGD("SetObjectPropValue %d %s\n", handle,
507 MtpDebug::getObjectPropCodeName(property));
508
509 return mDatabase->setObjectPropertyValue(handle, property, mData);
510}
511
512MtpResponseCode MtpServer::doGetDevicePropValue() {
513 MtpDeviceProperty property = mRequest.getParameter(1);
514 LOGD("GetDevicePropValue %s\n",
515 MtpDebug::getDevicePropCodeName(property));
516
517 return mDatabase->getDevicePropertyValue(property, mData);
518}
519
520MtpResponseCode MtpServer::doSetDevicePropValue() {
521 MtpDeviceProperty property = mRequest.getParameter(1);
522 LOGD("SetDevicePropValue %s\n",
523 MtpDebug::getDevicePropCodeName(property));
524
525 return mDatabase->setDevicePropertyValue(property, mData);
526}
527
528MtpResponseCode MtpServer::doResetDevicePropValue() {
529 MtpDeviceProperty property = mRequest.getParameter(1);
530 LOGD("ResetDevicePropValue %s\n",
531 MtpDebug::getDevicePropCodeName(property));
532
533 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400534}
535
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400536MtpResponseCode MtpServer::doGetObjectPropList() {
537
538 MtpObjectHandle handle = mRequest.getParameter(1);
539 MtpObjectFormat format = mRequest.getParameter(2);
540 MtpDeviceProperty property = mRequest.getParameter(3);
541 int groupCode = mRequest.getParameter(4);
Mike Lockwoodf05ff072010-11-23 18:45:25 -0500542 int depth = mRequest.getParameter(5);
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400543 LOGD("GetObjectPropList %d format: %s property: %s group: %d depth: %d\n",
544 handle, MtpDebug::getFormatCodeName(format),
545 MtpDebug::getObjectPropCodeName(property), groupCode, depth);
546
547 return mDatabase->getObjectPropertyList(handle, format, property, groupCode, depth, mData);
548}
549
Mike Lockwood16864ba2010-05-11 17:16:59 -0400550MtpResponseCode MtpServer::doGetObjectInfo() {
551 MtpObjectHandle handle = mRequest.getParameter(1);
552 return mDatabase->getObjectInfo(handle, mData);
553}
554
555MtpResponseCode MtpServer::doGetObject() {
556 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400557 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400558 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400559 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
560 if (result != MTP_RESPONSE_OK)
561 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400562
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400563 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400564 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400565 mfr.fd = open(filePath, O_RDONLY);
566 if (mfr.fd < 0) {
567 return MTP_RESPONSE_GENERAL_ERROR;
568 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400569 mfr.offset = 0;
570 mfr.length = fileLength;
571
572 // send data header
573 mData.setOperationCode(mRequest.getOperationCode());
574 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood23d20712010-10-11 17:31:44 -0400575 mData.writeDataHeader(mFD, fileLength + MTP_CONTAINER_HEADER_SIZE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400576
577 // then transfer the file
578 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400579 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400580 if (ret < 0) {
581 if (errno == ECANCELED)
582 return MTP_RESPONSE_TRANSACTION_CANCELLED;
583 else
584 return MTP_RESPONSE_GENERAL_ERROR;
585 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400586 return MTP_RESPONSE_OK;
587}
588
Mike Lockwoodd81ce3c2010-11-23 09:08:01 -0500589MtpResponseCode MtpServer::doGetPartialObject() {
590 MtpObjectHandle handle = mRequest.getParameter(1);
591 uint32_t offset = mRequest.getParameter(2);
592 uint32_t length = mRequest.getParameter(3);
593 MtpString pathBuf;
594 int64_t fileLength;
595 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
596 if (result != MTP_RESPONSE_OK)
597 return result;
598 if (offset + length > fileLength)
599 length = fileLength - offset;
600
601 const char* filePath = (const char *)pathBuf;
602 mtp_file_range mfr;
603 mfr.fd = open(filePath, O_RDONLY);
604 if (mfr.fd < 0) {
605 return MTP_RESPONSE_GENERAL_ERROR;
606 }
607 mfr.offset = offset;
608 mfr.length = length;
609 mResponse.setParameter(1, length);
610
611 // send data header
612 mData.setOperationCode(mRequest.getOperationCode());
613 mData.setTransactionID(mRequest.getTransactionID());
614 mData.writeDataHeader(mFD, length + MTP_CONTAINER_HEADER_SIZE);
615
616 // then transfer the file
617 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
618 close(mfr.fd);
619 if (ret < 0) {
620 if (errno == ECANCELED)
621 return MTP_RESPONSE_TRANSACTION_CANCELLED;
622 else
623 return MTP_RESPONSE_GENERAL_ERROR;
624 }
625 return MTP_RESPONSE_OK;
626}
627
Mike Lockwood16864ba2010-05-11 17:16:59 -0400628MtpResponseCode MtpServer::doSendObjectInfo() {
629 MtpString path;
630 MtpStorageID storageID = mRequest.getParameter(1);
631 MtpStorage* storage = getStorage(storageID);
632 MtpObjectHandle parent = mRequest.getParameter(2);
633 if (!storage)
634 return MTP_RESPONSE_INVALID_STORAGE_ID;
635
636 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400637 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400638 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400639 parent = 0;
640 } else {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400641 int64_t dummy;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400642 int result = mDatabase->getObjectFilePath(parent, path, dummy);
643 if (result != MTP_RESPONSE_OK)
644 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400645 }
646
647 // read only the fields we need
648 mData.getUInt32(); // storage ID
649 MtpObjectFormat format = mData.getUInt16();
650 mData.getUInt16(); // protection status
651 mSendObjectFileSize = mData.getUInt32();
652 mData.getUInt16(); // thumb format
653 mData.getUInt32(); // thumb compressed size
654 mData.getUInt32(); // thumb pix width
655 mData.getUInt32(); // thumb pix height
656 mData.getUInt32(); // image pix width
657 mData.getUInt32(); // image pix height
658 mData.getUInt32(); // image bit depth
659 mData.getUInt32(); // parent
660 uint16_t associationType = mData.getUInt16();
661 uint32_t associationDesc = mData.getUInt32(); // association desc
662 mData.getUInt32(); // sequence number
663 MtpStringBuffer name, created, modified;
664 mData.getString(name); // file name
665 mData.getString(created); // date created
666 mData.getString(modified); // date modified
667 // keywords follow
668
Mike Lockwoodf5b2ff22010-11-17 15:42:09 -0500669 LOGD("name: %s format: %04X\n", (const char *)name, format);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400670 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400671 if (!parseDateTime(modified, modifiedTime))
672 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400673
674 if (path[path.size() - 1] != '/')
675 path += "/";
676 path += (const char *)name;
677
Mike Lockwood4714b072010-07-12 08:49:01 -0400678 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
679 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400680 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400681 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400682 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400683
684 if (format == MTP_FORMAT_ASSOCIATION) {
685 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400686 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400687 umask(mask);
688 if (ret && ret != -EEXIST)
689 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400690 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400691 } else {
692 mSendObjectFilePath = path;
693 // save the handle for the SendObject call, which should follow
694 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400695 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400696 }
697
698 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400699 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400700 mResponse.setParameter(3, handle);
701
702 return MTP_RESPONSE_OK;
703}
704
705MtpResponseCode MtpServer::doSendObject() {
Mike Lockwood4714b072010-07-12 08:49:01 -0400706 MtpResponseCode result = MTP_RESPONSE_OK;
707 mode_t mask;
708 int ret;
Mike Lockwoode1b8cf12010-11-16 17:38:43 -0500709 uint64_t actualSize = -1;
Mike Lockwood4714b072010-07-12 08:49:01 -0400710
Mike Lockwood16864ba2010-05-11 17:16:59 -0400711 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400712 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400713 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
714 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400715 }
716
717 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400718 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400719 // FIXME - check for errors here.
720
721 // reset so we don't attempt to send this back
722 mData.reset();
723
724 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400725 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
726 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400727 result = MTP_RESPONSE_GENERAL_ERROR;
728 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400729 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400730 fchown(mfr.fd, getuid(), mFileGroup);
731 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400732 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400733 fchmod(mfr.fd, mFilePermission);
734 umask(mask);
735
Mike Lockwood16864ba2010-05-11 17:16:59 -0400736 mfr.offset = 0;
737 mfr.length = mSendObjectFileSize;
738
Mike Lockwoodf5b2ff22010-11-17 15:42:09 -0500739 LOGD("receiving %s\n", (const char *)mSendObjectFilePath);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400740 // transfer the file
741 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400742 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400743
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400744 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400745
Mike Lockwood916076c2010-06-04 09:49:21 -0400746 if (ret < 0) {
747 unlink(mSendObjectFilePath);
748 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400749 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400750 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400751 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode1b8cf12010-11-16 17:38:43 -0500752 } else if (mSendObjectFileSize == 0xFFFFFFFF) {
753 // actual size is likely > 4 gig so stat the file to compute actual length
754 struct stat s;
755 if (lstat(mSendObjectFilePath, &s) == 0) {
756 actualSize = s.st_size;
757 LOGD("actualSize: %lld\n", actualSize);
758 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400759 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400760
761done:
762 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
Mike Lockwoode1b8cf12010-11-16 17:38:43 -0500763 actualSize, result == MTP_RESPONSE_OK);
Mike Lockwood4714b072010-07-12 08:49:01 -0400764 mSendObjectHandle = kInvalidObjectHandle;
765 mSendObjectFormat = 0;
766 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400767}
768
Mike Lockwoodd3211492010-09-13 17:15:58 -0400769static void deleteRecursive(const char* path) {
770 char pathbuf[PATH_MAX];
771 int pathLength = strlen(path);
772 if (pathLength >= sizeof(pathbuf) - 1) {
773 LOGE("path too long: %s\n", path);
774 }
775 strcpy(pathbuf, path);
776 if (pathbuf[pathLength - 1] != '/') {
777 pathbuf[pathLength++] = '/';
778 }
779 char* fileSpot = pathbuf + pathLength;
780 int pathRemaining = sizeof(pathbuf) - pathLength - 1;
781
782 DIR* dir = opendir(path);
783 if (!dir) {
784 LOGE("opendir %s failed: %s", path, strerror(errno));
785 return;
786 }
787
788 struct dirent* entry;
789 while ((entry = readdir(dir))) {
790 const char* name = entry->d_name;
791
792 // ignore "." and ".."
793 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
794 continue;
795 }
796
797 int nameLength = strlen(name);
798 if (nameLength > pathRemaining) {
799 LOGE("path %s/%s too long\n", path, name);
800 continue;
801 }
802 strcpy(fileSpot, name);
803
804 int type = entry->d_type;
805 if (entry->d_type == DT_DIR) {
806 deleteRecursive(pathbuf);
807 rmdir(pathbuf);
808 } else {
809 unlink(pathbuf);
810 }
811 }
Mike Lockwood7ce05cf2010-11-11 11:22:32 -0500812 closedir(dir);
Mike Lockwoodd3211492010-09-13 17:15:58 -0400813}
814
815static void deletePath(const char* path) {
816 struct stat statbuf;
817 if (stat(path, &statbuf) == 0) {
818 if (S_ISDIR(statbuf.st_mode)) {
819 deleteRecursive(path);
820 rmdir(path);
821 } else {
822 unlink(path);
823 }
824 } else {
825 LOGE("deletePath stat failed for %s: %s", path, strerror(errno));
826 }
827}
828
Mike Lockwood16864ba2010-05-11 17:16:59 -0400829MtpResponseCode MtpServer::doDeleteObject() {
830 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodd3211492010-09-13 17:15:58 -0400831 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400832 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
833 // FIXME - implement deleting objects by format
Mike Lockwood16864ba2010-05-11 17:16:59 -0400834
835 MtpString filePath;
836 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400837 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength);
838 if (result == MTP_RESPONSE_OK) {
839 LOGV("deleting %s", (const char *)filePath);
Mike Lockwoodd3211492010-09-13 17:15:58 -0400840 deletePath((const char *)filePath);
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400841 return mDatabase->deleteFile(handle);
842 } else {
843 return result;
844 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400845}
846
847MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400848 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400849 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400850 LOGD("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
851 MtpDebug::getFormatCodeName(format));
852 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400853 if (!property)
854 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400855 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400856 delete property;
857 return MTP_RESPONSE_OK;
858}
859
860MtpResponseCode MtpServer::doGetDevicePropDesc() {
861 MtpDeviceProperty propCode = mRequest.getParameter(1);
862 LOGD("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
863 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
864 if (!property)
865 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
866 property->write(mData);
867 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400868 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400869}
Mike Lockwood7850ef92010-05-14 10:10:36 -0400870
871} // namespace android