blob: c9821149589de1adfcca045ed22df2796292d8ca [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 }
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400188
189 if (mSessionOpen)
190 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400191}
192
Mike Lockwood873871f2010-07-12 18:54:16 -0400193void MtpServer::sendObjectAdded(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400194 if (mSessionOpen) {
195 LOGD("sendObjectAdded %d\n", handle);
196 mEvent.setEventCode(MTP_EVENT_OBJECT_ADDED);
197 mEvent.setTransactionID(mRequest.getTransactionID());
198 mEvent.setParameter(1, handle);
199 int ret = mEvent.write(mFD);
200 LOGD("mEvent.write returned %d\n", ret);
201 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400202}
203
204void MtpServer::sendObjectRemoved(MtpObjectHandle handle) {
Mike Lockwood73ecd232010-07-19 14:29:58 -0400205 if (mSessionOpen) {
206 LOGD("sendObjectRemoved %d\n", handle);
207 mEvent.setEventCode(MTP_EVENT_OBJECT_REMOVED);
208 mEvent.setTransactionID(mRequest.getTransactionID());
209 mEvent.setParameter(1, handle);
210 int ret = mEvent.write(mFD);
211 LOGD("mEvent.write returned %d\n", ret);
212 }
Mike Lockwood873871f2010-07-12 18:54:16 -0400213}
214
Mike Lockwood916076c2010-06-04 09:49:21 -0400215bool MtpServer::handleRequest() {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400216 MtpOperationCode operation = mRequest.getOperationCode();
217 MtpResponseCode response;
218
219 mResponse.reset();
220
221 if (mSendObjectHandle != kInvalidObjectHandle && operation != MTP_OPERATION_SEND_OBJECT) {
222 // FIXME - need to delete mSendObjectHandle from the database
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400223 LOGE("expected SendObject after SendObjectInfo");
Mike Lockwood16864ba2010-05-11 17:16:59 -0400224 mSendObjectHandle = kInvalidObjectHandle;
225 }
226
227 switch (operation) {
228 case MTP_OPERATION_GET_DEVICE_INFO:
229 response = doGetDeviceInfo();
230 break;
231 case MTP_OPERATION_OPEN_SESSION:
232 response = doOpenSession();
233 break;
234 case MTP_OPERATION_CLOSE_SESSION:
235 response = doCloseSession();
236 break;
237 case MTP_OPERATION_GET_STORAGE_IDS:
238 response = doGetStorageIDs();
239 break;
240 case MTP_OPERATION_GET_STORAGE_INFO:
241 response = doGetStorageInfo();
242 break;
243 case MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED:
244 response = doGetObjectPropsSupported();
245 break;
246 case MTP_OPERATION_GET_OBJECT_HANDLES:
247 response = doGetObjectHandles();
248 break;
Mike Lockwood343af4e2010-08-02 10:52:20 -0400249 case MTP_OPERATION_GET_NUM_OBJECTS:
250 response = doGetNumObjects();
251 break;
Mike Lockwood438344f2010-08-03 15:30:09 -0400252 case MTP_OPERATION_GET_OBJECT_REFERENCES:
253 response = doGetObjectReferences();
254 break;
255 case MTP_OPERATION_SET_OBJECT_REFERENCES:
256 response = doSetObjectReferences();
257 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400258 case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
259 response = doGetObjectPropValue();
260 break;
Mike Lockwood8277cec2010-08-10 15:20:35 -0400261 case MTP_OPERATION_SET_OBJECT_PROP_VALUE:
262 response = doSetObjectPropValue();
263 break;
264 case MTP_OPERATION_GET_DEVICE_PROP_VALUE:
265 response = doGetDevicePropValue();
266 break;
267 case MTP_OPERATION_SET_DEVICE_PROP_VALUE:
268 response = doSetDevicePropValue();
269 break;
270 case MTP_OPERATION_RESET_DEVICE_PROP_VALUE:
271 response = doResetDevicePropValue();
272 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400273 case MTP_OPERATION_GET_OBJECT_INFO:
274 response = doGetObjectInfo();
275 break;
276 case MTP_OPERATION_GET_OBJECT:
277 response = doGetObject();
278 break;
279 case MTP_OPERATION_SEND_OBJECT_INFO:
280 response = doSendObjectInfo();
281 break;
282 case MTP_OPERATION_SEND_OBJECT:
283 response = doSendObject();
284 break;
285 case MTP_OPERATION_DELETE_OBJECT:
286 response = doDeleteObject();
287 break;
288 case MTP_OPERATION_GET_OBJECT_PROP_DESC:
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400289 response = doGetObjectPropDesc();
290 break;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400291 default:
292 response = MTP_RESPONSE_OPERATION_NOT_SUPPORTED;
293 break;
294 }
295
Mike Lockwood916076c2010-06-04 09:49:21 -0400296 if (response == MTP_RESPONSE_TRANSACTION_CANCELLED)
297 return false;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400298 mResponse.setResponseCode(response);
Mike Lockwood916076c2010-06-04 09:49:21 -0400299 return true;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400300}
301
302MtpResponseCode MtpServer::doGetDeviceInfo() {
303 MtpStringBuffer string;
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700304 char prop_value[PROPERTY_VALUE_MAX];
Mike Lockwood16864ba2010-05-11 17:16:59 -0400305
Mike Lockwood782aef12010-08-10 07:37:50 -0400306 MtpObjectFormatList* playbackFormats = mDatabase->getSupportedPlaybackFormats();
307 MtpObjectFormatList* captureFormats = mDatabase->getSupportedCaptureFormats();
308 MtpDevicePropertyList* deviceProperties = mDatabase->getSupportedDeviceProperties();
309
Mike Lockwood16864ba2010-05-11 17:16:59 -0400310 // fill in device info
311 mData.putUInt16(MTP_STANDARD_VERSION);
312 mData.putUInt32(6); // MTP Vendor Extension ID
313 mData.putUInt16(MTP_STANDARD_VERSION);
314 string.set("microsoft.com: 1.0;");
315 mData.putString(string); // MTP Extensions
316 mData.putUInt16(0); //Functional Mode
317 mData.putAUInt16(kSupportedOperationCodes,
318 sizeof(kSupportedOperationCodes) / sizeof(uint16_t)); // Operations Supported
Mike Lockwood873871f2010-07-12 18:54:16 -0400319 mData.putAUInt16(kSupportedEventCodes,
320 sizeof(kSupportedEventCodes) / sizeof(uint16_t)); // Events Supported
Mike Lockwood782aef12010-08-10 07:37:50 -0400321 mData.putAUInt16(deviceProperties); // Device Properties Supported
322 mData.putAUInt16(captureFormats); // Capture Formats
323 mData.putAUInt16(playbackFormats); // Playback Formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400324 // FIXME
325 string.set("Google, Inc.");
326 mData.putString(string); // Manufacturer
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700327
328 property_get("ro.product.model", prop_value, "MTP Device");
329 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400330 mData.putString(string); // Model
331 string.set("1.0");
332 mData.putString(string); // Device Version
Mike Lockwoodc42aa122010-06-14 17:58:08 -0700333
334 property_get("ro.serialno", prop_value, "????????");
335 string.set(prop_value);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400336 mData.putString(string); // Serial Number
337
Mike Lockwood782aef12010-08-10 07:37:50 -0400338 delete playbackFormats;
339 delete captureFormats;
340 delete deviceProperties;
341
Mike Lockwood16864ba2010-05-11 17:16:59 -0400342 return MTP_RESPONSE_OK;
343}
344
345MtpResponseCode MtpServer::doOpenSession() {
346 if (mSessionOpen) {
347 mResponse.setParameter(1, mSessionID);
348 return MTP_RESPONSE_SESSION_ALREADY_OPEN;
349 }
350 mSessionID = mRequest.getParameter(1);
351 mSessionOpen = true;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400352
353 mDatabase->sessionStarted();
354
Mike Lockwood16864ba2010-05-11 17:16:59 -0400355 return MTP_RESPONSE_OK;
356}
357
358MtpResponseCode MtpServer::doCloseSession() {
359 if (!mSessionOpen)
360 return MTP_RESPONSE_SESSION_NOT_OPEN;
361 mSessionID = 0;
362 mSessionOpen = false;
Mike Lockwood6b3a9d12010-08-31 16:25:12 -0400363 mDatabase->sessionEnded();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400364 return MTP_RESPONSE_OK;
365}
366
367MtpResponseCode MtpServer::doGetStorageIDs() {
368 if (!mSessionOpen)
369 return MTP_RESPONSE_SESSION_NOT_OPEN;
370
371 int count = mStorages.size();
372 mData.putUInt32(count);
373 for (int i = 0; i < count; i++)
374 mData.putUInt32(mStorages[i]->getStorageID());
375
376 return MTP_RESPONSE_OK;
377}
378
379MtpResponseCode MtpServer::doGetStorageInfo() {
380 MtpStringBuffer string;
381
382 if (!mSessionOpen)
383 return MTP_RESPONSE_SESSION_NOT_OPEN;
384 MtpStorageID id = mRequest.getParameter(1);
385 MtpStorage* storage = getStorage(id);
386 if (!storage)
387 return MTP_RESPONSE_INVALID_STORAGE_ID;
388
389 mData.putUInt16(storage->getType());
390 mData.putUInt16(storage->getFileSystemType());
391 mData.putUInt16(storage->getAccessCapability());
392 mData.putUInt64(storage->getMaxCapacity());
393 mData.putUInt64(storage->getFreeSpace());
394 mData.putUInt32(1024*1024*1024); // Free Space in Objects
395 string.set(storage->getDescription());
396 mData.putString(string);
397 mData.putEmptyString(); // Volume Identifier
398
399 return MTP_RESPONSE_OK;
400}
401
402MtpResponseCode MtpServer::doGetObjectPropsSupported() {
403 if (!mSessionOpen)
404 return MTP_RESPONSE_SESSION_NOT_OPEN;
405 MtpObjectFormat format = mRequest.getParameter(1);
Mike Lockwood782aef12010-08-10 07:37:50 -0400406 MtpDevicePropertyList* properties = mDatabase->getSupportedObjectProperties(format);
407 mData.putAUInt16(properties);
Mike Lockwoodbf9b2052010-08-10 15:11:32 -0400408 delete properties;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400409 return MTP_RESPONSE_OK;
410}
411
412MtpResponseCode MtpServer::doGetObjectHandles() {
413 if (!mSessionOpen)
414 return MTP_RESPONSE_SESSION_NOT_OPEN;
415 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
Mike Lockwoode13401b2010-05-19 15:12:14 -0400416 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
Mike Lockwood16864ba2010-05-11 17:16:59 -0400417 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
418 // 0x00000000 for all objects?
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400419 if (parent == 0xFFFFFFFF)
420 parent = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400421
422 MtpObjectHandleList* handles = mDatabase->getObjectList(storageID, format, parent);
423 mData.putAUInt32(handles);
424 delete handles;
425 return MTP_RESPONSE_OK;
426}
427
Mike Lockwood343af4e2010-08-02 10:52:20 -0400428MtpResponseCode MtpServer::doGetNumObjects() {
429 if (!mSessionOpen)
430 return MTP_RESPONSE_SESSION_NOT_OPEN;
431 MtpStorageID storageID = mRequest.getParameter(1); // 0xFFFFFFFF for all storage
432 MtpObjectFormat format = mRequest.getParameter(2); // 0 for all formats
433 MtpObjectHandle parent = mRequest.getParameter(3); // 0xFFFFFFFF for objects with no parent
434 // 0x00000000 for all objects?
435 if (parent == 0xFFFFFFFF)
436 parent = 0;
437
438 int count = mDatabase->getNumObjects(storageID, format, parent);
439 if (count >= 0) {
440 mResponse.setParameter(1, count);
441 return MTP_RESPONSE_OK;
442 } else {
443 mResponse.setParameter(1, 0);
444 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
445 }
446}
447
Mike Lockwood438344f2010-08-03 15:30:09 -0400448MtpResponseCode MtpServer::doGetObjectReferences() {
449 if (!mSessionOpen)
450 return MTP_RESPONSE_SESSION_NOT_OPEN;
451 MtpStorageID handle = mRequest.getParameter(1);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400452
453 // FIXME - check for invalid object handle
Mike Lockwood438344f2010-08-03 15:30:09 -0400454 MtpObjectHandleList* handles = mDatabase->getObjectReferences(handle);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400455 if (handles) {
456 mData.putAUInt32(handles);
457 delete handles;
458 } else {
Mike Lockwood438344f2010-08-03 15:30:09 -0400459 mData.putEmptyArray();
Mike Lockwood438344f2010-08-03 15:30:09 -0400460 }
Mike Lockwood438344f2010-08-03 15:30:09 -0400461 return MTP_RESPONSE_OK;
462}
463
464MtpResponseCode MtpServer::doSetObjectReferences() {
465 if (!mSessionOpen)
466 return MTP_RESPONSE_SESSION_NOT_OPEN;
467 MtpStorageID handle = mRequest.getParameter(1);
468 MtpObjectHandleList* references = mData.getAUInt32();
469 MtpResponseCode result = mDatabase->setObjectReferences(handle, references);
470 delete references;
471 return result;
472}
473
Mike Lockwood16864ba2010-05-11 17:16:59 -0400474MtpResponseCode MtpServer::doGetObjectPropValue() {
475 MtpObjectHandle handle = mRequest.getParameter(1);
476 MtpObjectProperty property = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400477 LOGD("GetObjectPropValue %d %s\n", handle,
478 MtpDebug::getObjectPropCodeName(property));
Mike Lockwood16864ba2010-05-11 17:16:59 -0400479
Mike Lockwood8277cec2010-08-10 15:20:35 -0400480 return mDatabase->getObjectPropertyValue(handle, property, mData);
481}
482
483MtpResponseCode MtpServer::doSetObjectPropValue() {
484 MtpObjectHandle handle = mRequest.getParameter(1);
485 MtpObjectProperty property = mRequest.getParameter(2);
486 LOGD("SetObjectPropValue %d %s\n", handle,
487 MtpDebug::getObjectPropCodeName(property));
488
489 return mDatabase->setObjectPropertyValue(handle, property, mData);
490}
491
492MtpResponseCode MtpServer::doGetDevicePropValue() {
493 MtpDeviceProperty property = mRequest.getParameter(1);
494 LOGD("GetDevicePropValue %s\n",
495 MtpDebug::getDevicePropCodeName(property));
496
497 return mDatabase->getDevicePropertyValue(property, mData);
498}
499
500MtpResponseCode MtpServer::doSetDevicePropValue() {
501 MtpDeviceProperty property = mRequest.getParameter(1);
502 LOGD("SetDevicePropValue %s\n",
503 MtpDebug::getDevicePropCodeName(property));
504
505 return mDatabase->setDevicePropertyValue(property, mData);
506}
507
508MtpResponseCode MtpServer::doResetDevicePropValue() {
509 MtpDeviceProperty property = mRequest.getParameter(1);
510 LOGD("ResetDevicePropValue %s\n",
511 MtpDebug::getDevicePropCodeName(property));
512
513 return mDatabase->resetDeviceProperty(property);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400514}
515
516MtpResponseCode MtpServer::doGetObjectInfo() {
517 MtpObjectHandle handle = mRequest.getParameter(1);
518 return mDatabase->getObjectInfo(handle, mData);
519}
520
521MtpResponseCode MtpServer::doGetObject() {
522 MtpObjectHandle handle = mRequest.getParameter(1);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400523 MtpString pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400524 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400525 int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
526 if (result != MTP_RESPONSE_OK)
527 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400528
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400529 const char* filePath = (const char *)pathBuf;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400530 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400531 mfr.fd = open(filePath, O_RDONLY);
532 if (mfr.fd < 0) {
533 return MTP_RESPONSE_GENERAL_ERROR;
534 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400535 mfr.offset = 0;
536 mfr.length = fileLength;
537
538 // send data header
539 mData.setOperationCode(mRequest.getOperationCode());
540 mData.setTransactionID(mRequest.getTransactionID());
541 mData.writeDataHeader(mFD, fileLength);
542
543 // then transfer the file
544 int ret = ioctl(mFD, MTP_SEND_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400545 close(mfr.fd);
Mike Lockwood916076c2010-06-04 09:49:21 -0400546 if (ret < 0) {
547 if (errno == ECANCELED)
548 return MTP_RESPONSE_TRANSACTION_CANCELLED;
549 else
550 return MTP_RESPONSE_GENERAL_ERROR;
551 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400552 return MTP_RESPONSE_OK;
553}
554
555MtpResponseCode MtpServer::doSendObjectInfo() {
556 MtpString path;
557 MtpStorageID storageID = mRequest.getParameter(1);
558 MtpStorage* storage = getStorage(storageID);
559 MtpObjectHandle parent = mRequest.getParameter(2);
560 if (!storage)
561 return MTP_RESPONSE_INVALID_STORAGE_ID;
562
563 // special case the root
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400564 if (parent == MTP_PARENT_ROOT) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400565 path = storage->getPath();
Mike Lockwood1865a5d2010-07-03 00:44:05 -0400566 parent = 0;
567 } else {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400568 int64_t dummy;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400569 int result = mDatabase->getObjectFilePath(parent, path, dummy);
570 if (result != MTP_RESPONSE_OK)
571 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400572 }
573
574 // read only the fields we need
575 mData.getUInt32(); // storage ID
576 MtpObjectFormat format = mData.getUInt16();
577 mData.getUInt16(); // protection status
578 mSendObjectFileSize = mData.getUInt32();
579 mData.getUInt16(); // thumb format
580 mData.getUInt32(); // thumb compressed size
581 mData.getUInt32(); // thumb pix width
582 mData.getUInt32(); // thumb pix height
583 mData.getUInt32(); // image pix width
584 mData.getUInt32(); // image pix height
585 mData.getUInt32(); // image bit depth
586 mData.getUInt32(); // parent
587 uint16_t associationType = mData.getUInt16();
588 uint32_t associationDesc = mData.getUInt32(); // association desc
589 mData.getUInt32(); // sequence number
590 MtpStringBuffer name, created, modified;
591 mData.getString(name); // file name
592 mData.getString(created); // date created
593 mData.getString(modified); // date modified
594 // keywords follow
595
Mike Lockwoodfceef462010-05-14 15:35:17 -0400596 time_t modifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400597 if (!parseDateTime(modified, modifiedTime))
598 modifiedTime = 0;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400599
600 if (path[path.size() - 1] != '/')
601 path += "/";
602 path += (const char *)name;
603
Mike Lockwood4714b072010-07-12 08:49:01 -0400604 MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
605 format, parent, storageID, mSendObjectFileSize, modifiedTime);
Mike Lockwoodfceef462010-05-14 15:35:17 -0400606 if (handle == kInvalidObjectHandle) {
Mike Lockwood16864ba2010-05-11 17:16:59 -0400607 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoodfceef462010-05-14 15:35:17 -0400608 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400609
610 if (format == MTP_FORMAT_ASSOCIATION) {
611 mode_t mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400612 int ret = mkdir((const char *)path, mDirectoryPermission);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400613 umask(mask);
614 if (ret && ret != -EEXIST)
615 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400616 chown((const char *)path, getuid(), mFileGroup);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400617 } else {
618 mSendObjectFilePath = path;
619 // save the handle for the SendObject call, which should follow
620 mSendObjectHandle = handle;
Mike Lockwood4714b072010-07-12 08:49:01 -0400621 mSendObjectFormat = format;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400622 }
623
624 mResponse.setParameter(1, storageID);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400625 mResponse.setParameter(2, parent);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400626 mResponse.setParameter(3, handle);
627
628 return MTP_RESPONSE_OK;
629}
630
631MtpResponseCode MtpServer::doSendObject() {
Mike Lockwood4714b072010-07-12 08:49:01 -0400632 MtpResponseCode result = MTP_RESPONSE_OK;
633 mode_t mask;
634 int ret;
635
Mike Lockwood16864ba2010-05-11 17:16:59 -0400636 if (mSendObjectHandle == kInvalidObjectHandle) {
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400637 LOGE("Expected SendObjectInfo before SendObject");
Mike Lockwood4714b072010-07-12 08:49:01 -0400638 result = MTP_RESPONSE_NO_VALID_OBJECT_INFO;
639 goto done;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400640 }
641
642 // read the header
Mike Lockwood4714b072010-07-12 08:49:01 -0400643 ret = mData.readDataHeader(mFD);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400644 // FIXME - check for errors here.
645
646 // reset so we don't attempt to send this back
647 mData.reset();
648
649 mtp_file_range mfr;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400650 mfr.fd = open(mSendObjectFilePath, O_RDWR | O_CREAT | O_TRUNC);
651 if (mfr.fd < 0) {
Mike Lockwood4714b072010-07-12 08:49:01 -0400652 result = MTP_RESPONSE_GENERAL_ERROR;
653 goto done;
Mike Lockwoodc6588762010-06-22 15:03:53 -0400654 }
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400655 fchown(mfr.fd, getuid(), mFileGroup);
656 // set permissions
Mike Lockwood4714b072010-07-12 08:49:01 -0400657 mask = umask(0);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400658 fchmod(mfr.fd, mFilePermission);
659 umask(mask);
660
Mike Lockwood16864ba2010-05-11 17:16:59 -0400661 mfr.offset = 0;
662 mfr.length = mSendObjectFileSize;
663
664 // transfer the file
665 ret = ioctl(mFD, MTP_RECEIVE_FILE, (unsigned long)&mfr);
Mike Lockwoodc6588762010-06-22 15:03:53 -0400666 close(mfr.fd);
Mike Lockwood8e2a2802010-07-02 15:15:07 -0400667
Mike Lockwoodb14e5882010-06-29 18:11:52 -0400668 LOGV("MTP_RECEIVE_FILE returned %d", ret);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400669
Mike Lockwood916076c2010-06-04 09:49:21 -0400670 if (ret < 0) {
671 unlink(mSendObjectFilePath);
672 if (errno == ECANCELED)
Mike Lockwood4714b072010-07-12 08:49:01 -0400673 result = MTP_RESPONSE_TRANSACTION_CANCELLED;
Mike Lockwood916076c2010-06-04 09:49:21 -0400674 else
Mike Lockwood4714b072010-07-12 08:49:01 -0400675 result = MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwood916076c2010-06-04 09:49:21 -0400676 }
Mike Lockwood4714b072010-07-12 08:49:01 -0400677
678done:
679 mDatabase->endSendObject(mSendObjectFilePath, mSendObjectHandle, mSendObjectFormat,
680 result == MTP_RESPONSE_OK);
681 mSendObjectHandle = kInvalidObjectHandle;
682 mSendObjectFormat = 0;
683 return result;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400684}
685
686MtpResponseCode MtpServer::doDeleteObject() {
687 MtpObjectHandle handle = mRequest.getParameter(1);
688 MtpObjectFormat format = mRequest.getParameter(1);
689 // FIXME - support deleting all objects if handle is 0xFFFFFFFF
690 // FIXME - implement deleting objects by format
691 // FIXME - handle non-empty directories
692
693 MtpString filePath;
694 int64_t fileLength;
Mike Lockwood9c04c4c2010-08-02 10:37:41 -0400695 int result = mDatabase->getObjectFilePath(handle, filePath, fileLength);
696 if (result == MTP_RESPONSE_OK) {
697 LOGV("deleting %s", (const char *)filePath);
698 // one of these should work
699 rmdir((const char *)filePath);
700 unlink((const char *)filePath);
701 return mDatabase->deleteFile(handle);
702 } else {
703 return result;
704 }
Mike Lockwood16864ba2010-05-11 17:16:59 -0400705}
706
707MtpResponseCode MtpServer::doGetObjectPropDesc() {
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400708 MtpObjectProperty propCode = mRequest.getParameter(1);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400709 MtpObjectFormat format = mRequest.getParameter(2);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400710 LOGD("GetObjectPropDesc %s %s\n", MtpDebug::getObjectPropCodeName(propCode),
711 MtpDebug::getFormatCodeName(format));
712 MtpProperty* property = mDatabase->getObjectPropertyDesc(propCode, format);
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400713 if (!property)
714 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400715 property->write(mData);
Mike Lockwood8277cec2010-08-10 15:20:35 -0400716 delete property;
717 return MTP_RESPONSE_OK;
718}
719
720MtpResponseCode MtpServer::doGetDevicePropDesc() {
721 MtpDeviceProperty propCode = mRequest.getParameter(1);
722 LOGD("GetDevicePropDesc %s\n", MtpDebug::getDevicePropCodeName(propCode));
723 MtpProperty* property = mDatabase->getDevicePropertyDesc(propCode);
724 if (!property)
725 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
726 property->write(mData);
727 delete property;
Mike Lockwood21ef7d02010-06-30 17:00:35 -0400728 return MTP_RESPONSE_OK;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400729}
Mike Lockwood7850ef92010-05-14 10:10:36 -0400730
731} // namespace android