blob: 1e64e4f1f913df3bac23ef023dc5e1462f17dcf8 [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,
Mike Lockwood343af4e2010-08-02 10:52:20 -040058// MTP_OPERATION_GET_DEVICE_PROP_DESC,
Mike Lockwood8277cec2010-08-10 15:20:35 -040059 MTP_OPERATION_GET_DEVICE_PROP_VALUE,
60 MTP_OPERATION_SET_DEVICE_PROP_VALUE,
61 MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
Mike Lockwood16864ba2010-05-11 17:16:59 -040062// 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,
Mike Lockwood8277cec2010-08-10 15:20:35 -040068 MTP_OPERATION_GET_OBJECT_PROP_DESC,
69// MTP_OPERATION_GET_OBJECT_PROP_VALUE,
Mike Lockwood343af4e2010-08-02 10:52:20 -040070// MTP_OPERATION_SET_OBJECT_PROP_VALUE,
Mike Lockwood438344f2010-08-03 15:30:09 -040071 MTP_OPERATION_GET_OBJECT_REFERENCES,
72 MTP_OPERATION_SET_OBJECT_REFERENCES,
Mike Lockwood16864ba2010-05-11 17:16:59 -040073// 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 Lockwood1865a5d2010-07-03 00:44:05 -040081MtpServer::MtpServer(int fd, MtpDatabase* database,
Mike Lockwood8e2a2802010-07-02 15:15:07 -040082 int fileGroup, int filePerm, int directoryPerm)
Mike Lockwood16864ba2010-05-11 17:16:59 -040083 : mFD(fd),
Mike Lockwood1865a5d2010-07-03 00:44:05 -040084 mDatabase(database),
Mike Lockwood8e2a2802010-07-02 15:15:07 -040085 mFileGroup(fileGroup),
86 mFilePermission(filePerm),
87 mDirectoryPermission(directoryPerm),
Mike Lockwood16864ba2010-05-11 17:16:59 -040088 mSessionID(0),
89 mSessionOpen(false),
90 mSendObjectHandle(kInvalidObjectHandle),
Mike Lockwood4714b072010-07-12 08:49:01 -040091 mSendObjectFormat(0),
Mike Lockwood16864ba2010-05-11 17:16:59 -040092 mSendObjectFileSize(0)
93{
Mike Lockwood16864ba2010-05-11 17:16:59 -040094}
95
96MtpServer::~MtpServer() {
97}
98
99void MtpServer::addStorage(const char* filePath) {
100 int index = mStorages.size() + 1;
101 index |= index << 16; // set high and low part to our index
102 MtpStorage* storage = new MtpStorage(index, filePath, mDatabase);
103 addStorage(storage);
104}
105
106MtpStorage* MtpServer::getStorage(MtpStorageID id) {
107 for (int i = 0; i < mStorages.size(); i++) {
108 MtpStorage* storage = mStorages[i];
109 if (storage->getStorageID() == id)
110 return storage;
111 }
112 return NULL;
113}
114
Mike Lockwood16864ba2010-05-11 17:16:59 -0400115void MtpServer::run() {
116 int fd = mFD;
117
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400118 LOGV("MtpServer::run fd: %d\n", fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400119
120 while (1) {
121 int ret = mRequest.read(fd);
122 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400123 LOGE("request read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400124 if (errno == ECANCELED) {
125 // return to top of loop and wait for next command
126 continue;
127 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400128 break;
129 }
130 MtpOperationCode operation = mRequest.getOperationCode();
131 MtpTransactionID transaction = mRequest.getTransactionID();
132
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400133 LOGV("operation: %s", MtpDebug::getOperationCodeName(operation));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400134 mRequest.dump();
135
136 // FIXME need to generalize this
Mike Lockwood438344f2010-08-03 15:30:09 -0400137 bool dataIn = (operation == MTP_OPERATION_SEND_OBJECT_INFO
Mike Lockwood8277cec2010-08-10 15:20:35 -0400138 || operation == MTP_OPERATION_SET_OBJECT_REFERENCES
139 || operation == MTP_OPERATION_SET_OBJECT_PROP_VALUE
140 || operation == MTP_OPERATION_SET_DEVICE_PROP_VALUE);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400141 if (dataIn) {
142 int ret = mData.read(fd);
143 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400144 LOGE("data read returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400145 if (errno == ECANCELED) {
146 // return to top of loop and wait for next command
147 continue;
148 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400149 break;
150 }
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400151 LOGV("received data:");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400152 mData.dump();
153 } else {
154 mData.reset();
155 }
156
Mike Lockwood916076c2010-06-04 09:49:21 -0400157 if (handleRequest()) {
158 if (!dataIn && mData.hasData()) {
159 mData.setOperationCode(operation);
160 mData.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400161 LOGV("sending data:");
Mike Lockwood916076c2010-06-04 09:49:21 -0400162 ret = mData.write(fd);
163 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400164 LOGE("request write 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 }
169 break;
170 }
171 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400172
Mike Lockwood916076c2010-06-04 09:49:21 -0400173 mResponse.setTransactionID(transaction);
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400174 LOGV("sending response %04X", mResponse.getResponseCode());
Mike Lockwood916076c2010-06-04 09:49:21 -0400175 ret = mResponse.write(fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400176 if (ret < 0) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400177 LOGE("request write returned %d, errno: %d", ret, errno);
Mike Lockwood916076c2010-06-04 09:49:21 -0400178 if (errno == ECANCELED) {
179 // return to top of loop and wait for next command
180 continue;
181 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400182 break;
183 }
Mike Lockwood916076c2010-06-04 09:49:21 -0400184 } else {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400185 LOGV("skipping response\n");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400186 }
187 }
188}
189
Mike Lockwood873871f2010-07-12 18:54:16 -0400190void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400191 if (mSessionOpen) {
192 LOGD("sendObjectAdded %d\n", handle);
193 mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED);
194 mEvent.setTransactionID(mRequest.getTransactionID());
195 mEvent.setParameter(1, handle);
196 int ret = mEvent.write(mFD);
197 LOGD("mEvent.write returned %d\n", ret);
198 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400199}
200
201void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400202 if (mSessionOpen) {
203 LOGD("sendObjectRemoved %d\n", handle);
204 mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED);
205 mEvent.setTransactionID(mRequest.getTransactionID());
206 mEvent.setParameter(1, handle);
207 int ret = mEvent.write(mFD);
208 LOGD("mEvent.write returned %d\n", ret);
209 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400210}
211
Mike Lockwood916076c2010-06-04 09:49:21 -0400212bool MtpServer::handleRequest() {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400213 MtpOperationCode operation = mRequest.getOperationCode();
214 MtpResponseCode response;
215
216 mResponse.reset();
217
218 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
219 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400220 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400221 mSendObjectHandle = kInvalidObjectHandle;
222 }
223
224 switch (operation) {
225 case MTP_OPERATION_GET_DEVICE_INFO:
226 response = doGetDeviceInfo();
227 break;
228 case MTP_OPERATION_OPEN_SESSION:
229 response = doOpenSession();
230 break;
231 case MTP_OPERATION_CLOSE_SESSION:
232 response = doCloseSession();
233 break;
234 case MTP_OPERATION_GET_STORAGE_IDS:
235 response = doGetStorageIDs();
236 break;
237 case MTP_OPERATION_GET_STORAGE_INFO:
238 response = doGetStorageInfo();
239 break;
240 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
241 response = doGetObjectPropsSupported();
242 break;
243 case MTP_OPERATION_GET_OBJECT_HANDLES:
244 response = doGetObjectHandles();
245 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400246 case MTP_OPERATION_GET_NUM_OBJECTS:
247 response = doGetNumObjects();
248 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400249 case MTP_OPERATION_GET_OBJECT_REFERENCES:
250 response = doGetObjectReferences();
251 break;
252 case MTP_OPERATION_SET_OBJECT_REFERENCES:
253 response = doSetObjectReferences();
254 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400255 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
256 response = doGetObjectPropValue();
257 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400258 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
259 response = doSetObjectPropValue();
260 break;
261 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
262 response = doGetDevicePropValue();
263 break;
264 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
265 response = doSetDevicePropValue();
266 break;
267 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
268 response = doResetDevicePropValue();
269 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400270 case MTP_OPERATION_GET_OBJECT_INFO:
271 response = doGetObjectInfo();
272 break;
273 case MTP_OPERATION_GET_OBJECT:
274 response = doGetObject();
275 break;
276 case MTP_OPERATION_SEND_OBJECT_INFO:
277 response = doSendObjectInfo();
278 break;
279 case MTP_OPERATION_SEND_OBJECT:
280 response = doSendObject();
281 break;
282 case MTP_OPERATION_DELETE_OBJECT:
283 response = doDeleteObject();
284 break;
285 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400286 response = doGetObjectPropDesc();
287 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400288 default:
289 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
290 break;
291 }
292
Mike Lockwood916076c2010-06-04 09:49:21 -0400293 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
294 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400295 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400296 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400297}
298
299MtpResponseCode MtpServer::doGetDeviceInfo() {
300 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700301 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400302
Mike Lockwood782aef12010-08-10 07:37:50 -0400303 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
304 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
305 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
306
Mike Lockwood16864ba2010-05-11 17:16:59 -0400307 // fill in device info
308 mData.putUInt16(MTP_STANDARD_VERSION);
309 mData.putUInt32(6); // MTP Vendor Extension ID
310 mData.putUInt16(MTP_STANDARD_VERSION);
311 string.set("microsoft.com: 1.0;");
312 mData.putString(string); // MTP Extensions
313 mData.putUInt16(0); //Functional Mode
314 mData.putAUInt16(kSupportedOperationCodes,
315 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400316 mData.putAUInt16(kSupportedEventCodes,
317 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400318 mData.putAUInt16(deviceProperties); // Device Properties Supported
319 mData.putAUInt16(captureFormats); // Capture Formats
320 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400321 // FIXME
322 string.set("Google, Inc.");
323 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700324
325 property_get("ro.product.model", prop_value, "MTP Device");
326 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400327 mData.putString(string); // Model
328 string.set("1.0");
329 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700330
331 property_get("ro.serialno", prop_value, "????????");
332 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400333 mData.putString(string); // Serial Number
334
Mike Lockwood782aef12010-08-10 07:37:50 -0400335 delete playbackFormats;
336 delete captureFormats;
337 delete deviceProperties;
338
Mike Lockwood16864ba2010-05-11 17:16:59 -0400339 return MTP_RESPONSE_OK;
340}
341
342MtpResponseCode MtpServer::doOpenSession() {
343 if (mSessionOpen) {
344 mResponse.setParameter(1, mSessionID);
345 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
346 }
347 mSessionID = mRequest.getParameter(1);
348 mSessionOpen = true;
349 return MTP_RESPONSE_OK;
350}
351
352MtpResponseCode MtpServer::doCloseSession() {
353 if (!mSessionOpen)
354 return MTP_RESPONSE_SESSION_NOT_OPEN;
355 mSessionID = 0;
356 mSessionOpen = false;
357 return MTP_RESPONSE_OK;
358}
359
360MtpResponseCode MtpServer::doGetStorageIDs() {
361 if (!mSessionOpen)
362 return MTP_RESPONSE_SESSION_NOT_OPEN;
363
364 int count = mStorages.size();
365 mData.putUInt32(count);
366 for (int i = 0; i < count; i++)
367 mData.putUInt32(mStorages[i]->getStorageID());
368
369 return MTP_RESPONSE_OK;
370}
371
372MtpResponseCode MtpServer::doGetStorageInfo() {
373 MtpStringBuffer string;
374
375 if (!mSessionOpen)
376 return MTP_RESPONSE_SESSION_NOT_OPEN;
377 MtpStorageID id = mRequest.getParameter(1);
378 MtpStorage* storage = getStorage(id);
379 if (!storage)
380 return MTP_RESPONSE_INVALID_STORAGE_ID;
381
382 mData.putUInt16(storage->getType());
383 mData.putUInt16(storage->getFileSystemType());
384 mData.putUInt16(storage->getAccessCapability());
385 mData.putUInt64(storage->getMaxCapacity());
386 mData.putUInt64(storage->getFreeSpace());
387 mData.putUInt32(1024*1024*1024); // Free Space in Objects
388 string.set(storage->getDescription());
389 mData.putString(string);
390 mData.putEmptyString(); // Volume Identifier
391
392 return MTP_RESPONSE_OK;
393}
394
395MtpResponseCode MtpServer::doGetObjectPropsSupported() {
396 if (!mSessionOpen)
397 return MTP_RESPONSE_SESSION_NOT_OPEN;
398 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood782aef12010-08-10 07:37:50 -0400399 MtpDevicePropertyList* properties = mDatabase->getSupportedObjectProperties(format);
400 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400401 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400402 return MTP_RESPONSE_OK;
403}
404
405MtpResponseCode MtpServer::doGetObjectHandles() {
406 if (!mSessionOpen)
407 return MTP_RESPONSE_SESSION_NOT_OPEN;
408 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400409 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400410 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
411 // 0x00000000 for all objects?
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400412 if (parent == 0xFFFFFFFF)
413 parent = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400414
415 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
416 mData.putAUInt32(handles);
417 delete handles;
418 return MTP_RESPONSE_OK;
419}
420
Mike Lockwood343af4e2010-08-02 10:52:20 -0400421MtpResponseCode MtpServer::doGetNumObjects() {
422 if (!mSessionOpen)
423 return MTP_RESPONSE_SESSION_NOT_OPEN;
424 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
425 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
426 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
427 // 0x00000000 for all objects?
428 if (parent == 0xFFFFFFFF)
429 parent = 0;
430
431 int count = mDatabase->getNumObjects(storageID, format, parent);
432 if (count >= 0) {
433 mResponse.setParameter(1, count);
434 return MTP_RESPONSE_OK;
435 } else {
436 mResponse.setParameter(1, 0);
437 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
438 }
439}
440
Mike Lockwood438344f2010-08-03 15:30:09 -0400441MtpResponseCode MtpServer::doGetObjectReferences() {
442 if (!mSessionOpen)
443 return MTP_RESPONSE_SESSION_NOT_OPEN;
444 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400445
446 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400447 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400448 if (handles) {
449 mData.putAUInt32(handles);
450 delete handles;
451 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400452 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400453 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400454 return MTP_RESPONSE_OK;
455}
456
457MtpResponseCode MtpServer::doSetObjectReferences() {
458 if (!mSessionOpen)
459 return MTP_RESPONSE_SESSION_NOT_OPEN;
460 MtpStorageID handle = mRequest.getParameter(1);
461 MtpObjectHandleList* references = mData.getAUInt32();
462 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
463 delete references;
464 return result;
465}
466
Mike Lockwood16864ba2010-05-11 17:16:59 -0400467MtpResponseCode MtpServer::doGetObjectPropValue() {
468 MtpObjectHandle handle = mRequest.getParameter(1);
469 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400470 LOGD("GetObjectPropValue %d %s\n", handle,
471 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400472
Mike Lockwood8277cec2010-08-10 15:20:35 -0400473 return mDatabase->getObjectPropertyValue(handle, property, mData);
474}
475
476MtpResponseCode MtpServer::doSetObjectPropValue() {
477 MtpObjectHandle handle = mRequest.getParameter(1);
478 MtpObjectProperty property = mRequest.getParameter(2);
479 LOGD("SetObjectPropValue %d %s\n", handle,
480 MtpDebug::getObjectPropCodeName(property));
481
482 return mDatabase->setObjectPropertyValue(handle, property, mData);
483}
484
485MtpResponseCode MtpServer::doGetDevicePropValue() {
486 MtpDeviceProperty property = mRequest.getParameter(1);
487 LOGD("GetDevicePropValue %s\n",
488 MtpDebug::getDevicePropCodeName(property));
489
490 return mDatabase->getDevicePropertyValue(property, mData);
491}
492
493MtpResponseCode MtpServer::doSetDevicePropValue() {
494 MtpDeviceProperty property = mRequest.getParameter(1);
495 LOGD("SetDevicePropValue %s\n",
496 MtpDebug::getDevicePropCodeName(property));
497
498 return mDatabase->setDevicePropertyValue(property, mData);
499}
500
501MtpResponseCode MtpServer::doResetDevicePropValue() {
502 MtpDeviceProperty property = mRequest.getParameter(1);
503 LOGD("ResetDevicePropValue %s\n",
504 MtpDebug::getDevicePropCodeName(property));
505
506 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400507}
508
509MtpResponseCode MtpServer::doGetObjectInfo() {
510 MtpObjectHandle handle = mRequest.getParameter(1);
511 return mDatabase->getObjectInfo(handle, mData);
512}
513
514MtpResponseCode MtpServer::doGetObject() {
515 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400516 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400517 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400518 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
519 if (result != MTP_RESPONSE_OK)
520 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400521
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400522 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400523 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400524 mfr.fd = open(filePath, O_RDONLY);
525 if (mfr.fd < 0) {
526 return MTP_RESPONSE_GENERAL_ERROR;
527 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400528 mfr.offset = 0;
529 mfr.length = fileLength;
530
531 // send data header
532 mData.setOperationCode(mRequest.getOperationCode());
533 mData.setTransactionID(mRequest.getTransactionID());
534 mData.writeDataHeader(mFD, fileLength);
535
536 // then transfer the file
537 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400538 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400539 if (ret < 0) {
540 if (errno == ECANCELED)
541 return MTP_RESPONSE_TRANSACTION_CANCELLED;
542 else
543 return MTP_RESPONSE_GENERAL_ERROR;
544 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400545 return MTP_RESPONSE_OK;
546}
547
548MtpResponseCode MtpServer::doSendObjectInfo() {
549 MtpString path;
550 MtpStorageID storageID = mRequest.getParameter(1);
551 MtpStorage* storage = getStorage(storageID);
552 MtpObjectHandle parent = mRequest.getParameter(2);
553 if (!storage)
554 return MTP_RESPONSE_INVALID_STORAGE_ID;
555
556 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400557 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400558 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400559 parent = 0;
560 } else {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400561 int64_t dummy;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400562 int result = mDatabase->getObjectFilePath(parent, path, dummy);
563 if (result != MTP_RESPONSE_OK)
564 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400565 }
566
567 // read only the fields we need
568 mData.getUInt32(); // storage ID
569 MtpObjectFormat format = mData.getUInt16();
570 mData.getUInt16(); // protection status
571 mSendObjectFileSize = mData.getUInt32();
572 mData.getUInt16(); // thumb format
573 mData.getUInt32(); // thumb compressed size
574 mData.getUInt32(); // thumb pix width
575 mData.getUInt32(); // thumb pix height
576 mData.getUInt32(); // image pix width
577 mData.getUInt32(); // image pix height
578 mData.getUInt32(); // image bit depth
579 mData.getUInt32(); // parent
580 uint16_t associationType = mData.getUInt16();
581 uint32_t associationDesc = mData.getUInt32(); // association desc
582 mData.getUInt32(); // sequence number
583 MtpStringBuffer name, created, modified;
584 mData.getString(name); // file name
585 mData.getString(created); // date created
586 mData.getString(modified); // date modified
587 // keywords follow
588
Mike Lockwoodfceef462010-05-14 15:35:17 -0400589 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400590 if (!parseDateTime(modified, modifiedTime))
591 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400592
593 if (path[path.size() - 1] != '/')
594 path += "/";
595 path += (const char *)name;
596
Mike Lockwood4714b072010-07-12 08:49:01 -0400597 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
598 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400599 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400600 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400601 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400602
603 if (format == MTP_FORMAT_ASSOCIATION) {
604 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400605 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400606 umask(mask);
607 if (ret && ret != -EEXIST)
608 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400609 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400610 } else {
611 mSendObjectFilePath = path;
612 // save the handle for the SendObject call, which should follow
613 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400614 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400615 }
616
617 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400618 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400619 mResponse.setParameter(3, handle);
620
621 return MTP_RESPONSE_OK;
622}
623
624MtpResponseCode MtpServer::doSendObject() {
Mike Lockwood4714b072010-07-12 08:49:01 -0400625 MtpResponseCode result = MTP_RESPONSE_OK;
626 mode_t mask;
627 int ret;
628
Mike Lockwood16864ba2010-05-11 17:16:59 -0400629 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400630 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400631 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
632 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400633 }
634
635 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400636 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400637 // FIXME - check for errors here.
638
639 // reset so we don't attempt to send this back
640 mData.reset();
641
642 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400643 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
644 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400645 result = MTP_RESPONSE_GENERAL_ERROR;
646 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400647 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400648 fchown(mfr.fd, getuid(), mFileGroup);
649 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400650 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400651 fchmod(mfr.fd, mFilePermission);
652 umask(mask);
653
Mike Lockwood16864ba2010-05-11 17:16:59 -0400654 mfr.offset = 0;
655 mfr.length = mSendObjectFileSize;
656
657 // transfer the file
658 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400659 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400660
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400661 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400662
Mike Lockwood916076c2010-06-04 09:49:21 -0400663 if (ret < 0) {
664 unlink(mSendObjectFilePath);
665 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400666 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400667 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400668 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400669 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400670
671done:
672 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
673 result == MTP_RESPONSE_OK);
674 mSendObjectHandle = kInvalidObjectHandle;
675 mSendObjectFormat = 0;
676 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400677}
678
679MtpResponseCode MtpServer::doDeleteObject() {
680 MtpObjectHandle handle = mRequest.getParameter(1);
681 MtpObjectFormat format = mRequest.getParameter(1);
682 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
683 // FIXME - implement deleting objects by format
684 // FIXME - handle non-empty directories
685
686 MtpString filePath;
687 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400688 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength);
689 if (result == MTP_RESPONSE_OK) {
690 LOGV("deleting %s", (const char *)filePath);
691 // one of these should work
692 rmdir((const char *)filePath);
693 unlink((const char *)filePath);
694 return mDatabase->deleteFile(handle);
695 } else {
696 return result;
697 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400698}
699
700MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400701 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400702 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400703 LOGD("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
704 MtpDebug::getFormatCodeName(format));
705 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400706 if (!property)
707 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400708 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400709 delete property;
710 return MTP_RESPONSE_OK;
711}
712
713MtpResponseCode MtpServer::doGetDevicePropDesc() {
714 MtpDeviceProperty propCode = mRequest.getParameter(1);
715 LOGD("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
716 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
717 if (!property)
718 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
719 property->write(mData);
720 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400721 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400722}
Mike Lockwood7850ef92010-05-14 10:10:36 -0400723
724} // namespace android