blob: 8a8d493cd1826bdba0e646de9236fd88763ed49d [file] [log] [blame]
Mike Lockwood755fd612010-05-25 19:08:48 -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
Mike Lockwood90f48732010-06-05 22:45:01 -040017#define LOG_TAG "MtpDevice"
Mike Lockwood3e6616d2010-06-29 18:11:52 -040018
19#include "MtpDebug.h"
20#include "MtpDevice.h"
21#include "MtpDeviceInfo.h"
22#include "MtpObjectInfo.h"
23#include "MtpProperty.h"
24#include "MtpStorageInfo.h"
25#include "MtpStringBuffer.h"
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -040026#include "MtpUtils.h"
Mike Lockwood90f48732010-06-05 22:45:01 -040027
Mike Lockwood755fd612010-05-25 19:08:48 -040028#include <stdio.h>
29#include <stdlib.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <errno.h>
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -040035#include <endian.h>
Mike Lockwood755fd612010-05-25 19:08:48 -040036
37#include <usbhost/usbhost.h>
38
Mike Lockwood755fd612010-05-25 19:08:48 -040039namespace android {
40
Mike Lockwood8c553b92011-02-15 14:55:51 -050041#if 0
Mike Lockwood8182e722010-12-30 15:38:45 -050042static bool isMtpDevice(uint16_t vendor, uint16_t product) {
43 // Sandisk Sansa Fuze
44 if (vendor == 0x0781 && product == 0x74c2)
45 return true;
46 // Samsung YP-Z5
47 if (vendor == 0x04e8 && product == 0x503c)
48 return true;
49 return false;
50}
Mike Lockwood8c553b92011-02-15 14:55:51 -050051#endif
Mike Lockwood8182e722010-12-30 15:38:45 -050052
53MtpDevice* MtpDevice::open(const char* deviceName, int fd) {
54 struct usb_device *device = usb_device_new(deviceName, fd);
55 if (!device) {
56 LOGE("usb_device_new failed for %s", deviceName);
57 return NULL;
58 }
59
60 struct usb_descriptor_header* desc;
61 struct usb_descriptor_iter iter;
62
63 usb_descriptor_iter_init(device, &iter);
64
65 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
66 if (desc->bDescriptorType == USB_DT_INTERFACE) {
67 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
68
69 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
70 interface->bInterfaceSubClass == 1 && // Still Image Capture
71 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
72 {
Kenny Root3d384f82011-02-02 13:43:55 -080073 char* manufacturerName = usb_device_get_manufacturer_name(device);
74 char* productName = usb_device_get_product_name(device);
75 LOGD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
76 free(manufacturerName);
77 free(productName);
Mike Lockwood8182e722010-12-30 15:38:45 -050078 } else if (interface->bInterfaceClass == 0xFF &&
79 interface->bInterfaceSubClass == 0xFF &&
80 interface->bInterfaceProtocol == 0) {
81 char* interfaceName = usb_device_get_string(device, interface->iInterface);
Kenny Root3d384f82011-02-02 13:43:55 -080082 if (!interfaceName) {
Mike Lockwood8182e722010-12-30 15:38:45 -050083 continue;
Kenny Root3d384f82011-02-02 13:43:55 -080084 } else if (strcmp(interfaceName, "MTP")) {
85 free(interfaceName);
86 continue;
87 }
88 free(interfaceName);
89
Mike Lockwood8182e722010-12-30 15:38:45 -050090 // Looks like an android style MTP device
Kenny Root3d384f82011-02-02 13:43:55 -080091 char* manufacturerName = usb_device_get_manufacturer_name(device);
92 char* productName = usb_device_get_product_name(device);
93 LOGD("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
94 free(manufacturerName);
95 free(productName);
Mike Lockwood8c553b92011-02-15 14:55:51 -050096 }
97#if 0
98 else {
Mike Lockwood8182e722010-12-30 15:38:45 -050099 // look for special cased devices based on vendor/product ID
100 // we are doing this mainly for testing purposes
101 uint16_t vendor = usb_device_get_vendor_id(device);
102 uint16_t product = usb_device_get_product_id(device);
103 if (!isMtpDevice(vendor, product)) {
104 // not an MTP or PTP device
105 continue;
106 }
107 // request MTP OS string and descriptor
108 // some music players need to see this before entering MTP mode.
109 char buffer[256];
110 memset(buffer, 0, sizeof(buffer));
Mike Lockwood1fce24b2011-01-27 10:47:40 -0800111 int ret = usb_device_control_transfer(device,
Mike Lockwood8182e722010-12-30 15:38:45 -0500112 USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_STANDARD,
113 USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | 0xEE,
Mike Lockwood1fce24b2011-01-27 10:47:40 -0800114 0, buffer, sizeof(buffer), 0);
115 printf("usb_device_control_transfer returned %d errno: %d\n", ret, errno);
Mike Lockwood8182e722010-12-30 15:38:45 -0500116 if (ret > 0) {
117 printf("got MTP string %s\n", buffer);
Mike Lockwood1fce24b2011-01-27 10:47:40 -0800118 ret = usb_device_control_transfer(device,
Mike Lockwood8182e722010-12-30 15:38:45 -0500119 USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 1,
Mike Lockwood1fce24b2011-01-27 10:47:40 -0800120 0, 4, buffer, sizeof(buffer), 0);
Mike Lockwood8182e722010-12-30 15:38:45 -0500121 printf("OS descriptor got %d\n", ret);
122 } else {
123 printf("no MTP string\n");
124 }
125 }
Mike Lockwood8c553b92011-02-15 14:55:51 -0500126#endif
Mike Lockwood8182e722010-12-30 15:38:45 -0500127 // if we got here, then we have a likely MTP or PTP device
128
129 // interface should be followed by three endpoints
130 struct usb_endpoint_descriptor *ep;
131 struct usb_endpoint_descriptor *ep_in_desc = NULL;
132 struct usb_endpoint_descriptor *ep_out_desc = NULL;
133 struct usb_endpoint_descriptor *ep_intr_desc = NULL;
134 for (int i = 0; i < 3; i++) {
135 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
136 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
137 LOGE("endpoints not found\n");
Kenny Root3d384f82011-02-02 13:43:55 -0800138 usb_device_close(device);
Mike Lockwood8182e722010-12-30 15:38:45 -0500139 return NULL;
140 }
141 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
142 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
143 ep_in_desc = ep;
144 else
145 ep_out_desc = ep;
146 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
147 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
148 ep_intr_desc = ep;
149 }
150 }
151 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
152 LOGE("endpoints not found\n");
Kenny Root3d384f82011-02-02 13:43:55 -0800153 usb_device_close(device);
Mike Lockwood8182e722010-12-30 15:38:45 -0500154 return NULL;
155 }
156
157 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
158 LOGE("usb_device_claim_interface failed errno: %d\n", errno);
Kenny Root3d384f82011-02-02 13:43:55 -0800159 usb_device_close(device);
Mike Lockwood8182e722010-12-30 15:38:45 -0500160 return NULL;
161 }
162
163 MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
164 ep_in_desc, ep_out_desc, ep_intr_desc);
165 mtpDevice->initialize();
166 return mtpDevice;
167 }
168 }
169
170 usb_device_close(device);
171 LOGE("device not found");
172 return NULL;
173}
174
Mike Lockwood755fd612010-05-25 19:08:48 -0400175MtpDevice::MtpDevice(struct usb_device* device, int interface,
Mike Lockwood215b6822011-01-04 14:48:57 -0500176 const struct usb_endpoint_descriptor *ep_in,
177 const struct usb_endpoint_descriptor *ep_out,
178 const struct usb_endpoint_descriptor *ep_intr)
Mike Lockwood755fd612010-05-25 19:08:48 -0400179 : mDevice(device),
180 mInterface(interface),
Mike Lockwood215b6822011-01-04 14:48:57 -0500181 mRequestIn1(NULL),
182 mRequestIn2(NULL),
183 mRequestOut(NULL),
184 mRequestIntr(NULL),
Mike Lockwood755fd612010-05-25 19:08:48 -0400185 mDeviceInfo(NULL),
Mike Lockwood755fd612010-05-25 19:08:48 -0400186 mSessionID(0),
Mike Lockwoodbfd1d722010-12-09 18:34:18 -0800187 mTransactionID(0),
188 mReceivedResponse(false)
Mike Lockwood755fd612010-05-25 19:08:48 -0400189{
Mike Lockwood215b6822011-01-04 14:48:57 -0500190 mRequestIn1 = usb_request_new(device, ep_in);
191 mRequestIn2 = usb_request_new(device, ep_in);
192 mRequestOut = usb_request_new(device, ep_out);
193 mRequestIntr = usb_request_new(device, ep_intr);
Mike Lockwood755fd612010-05-25 19:08:48 -0400194}
195
196MtpDevice::~MtpDevice() {
197 close();
Mike Lockwood90f48732010-06-05 22:45:01 -0400198 for (int i = 0; i < mDeviceProperties.size(); i++)
199 delete mDeviceProperties[i];
Mike Lockwood215b6822011-01-04 14:48:57 -0500200 usb_request_free(mRequestIn1);
201 usb_request_free(mRequestIn2);
202 usb_request_free(mRequestOut);
203 usb_request_free(mRequestIntr);
Mike Lockwood755fd612010-05-25 19:08:48 -0400204}
205
206void MtpDevice::initialize() {
207 openSession();
208 mDeviceInfo = getDeviceInfo();
209 if (mDeviceInfo) {
Mike Lockwood90f48732010-06-05 22:45:01 -0400210 if (mDeviceInfo->mDeviceProperties) {
211 int count = mDeviceInfo->mDeviceProperties->size();
212 for (int i = 0; i < count; i++) {
213 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
214 MtpProperty* property = getDevicePropDesc(propCode);
Mike Lockwoode4880e42010-12-07 11:24:28 -0800215 if (property)
Mike Lockwood90f48732010-06-05 22:45:01 -0400216 mDeviceProperties.push(property);
Mike Lockwood90f48732010-06-05 22:45:01 -0400217 }
218 }
Mike Lockwood755fd612010-05-25 19:08:48 -0400219 }
220}
221
222void MtpDevice::close() {
223 if (mDevice) {
224 usb_device_release_interface(mDevice, mInterface);
225 usb_device_close(mDevice);
226 mDevice = NULL;
227 }
228}
229
Mike Lockwoode4880e42010-12-07 11:24:28 -0800230void MtpDevice::print() {
231 if (mDeviceInfo) {
232 mDeviceInfo->print();
233
234 if (mDeviceInfo->mDeviceProperties) {
235 LOGI("***** DEVICE PROPERTIES *****\n");
236 int count = mDeviceInfo->mDeviceProperties->size();
237 for (int i = 0; i < count; i++) {
238 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
239 MtpProperty* property = getDevicePropDesc(propCode);
240 if (property) {
241 property->print();
Kenny Root3d384f82011-02-02 13:43:55 -0800242 delete property;
Mike Lockwoode4880e42010-12-07 11:24:28 -0800243 }
244 }
245 }
246 }
247
248 if (mDeviceInfo->mPlaybackFormats) {
249 LOGI("***** OBJECT PROPERTIES *****\n");
250 int count = mDeviceInfo->mPlaybackFormats->size();
251 for (int i = 0; i < count; i++) {
252 MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
253 LOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
254 MtpObjectPropertyList* props = getObjectPropsSupported(format);
255 if (props) {
256 for (int j = 0; j < props->size(); j++) {
257 MtpObjectProperty prop = (*props)[j];
Mike Lockwooda194cc72010-12-07 18:53:04 -0800258 MtpProperty* property = getObjectPropDesc(prop, format);
Kenny Root3d384f82011-02-02 13:43:55 -0800259 if (property) {
Mike Lockwoode4880e42010-12-07 11:24:28 -0800260 property->print();
Kenny Root3d384f82011-02-02 13:43:55 -0800261 delete property;
262 } else {
Mike Lockwoode4880e42010-12-07 11:24:28 -0800263 LOGE("could not fetch property: %s",
264 MtpDebug::getObjectPropCodeName(prop));
Kenny Root3d384f82011-02-02 13:43:55 -0800265 }
Mike Lockwoode4880e42010-12-07 11:24:28 -0800266 }
267 }
268 }
269 }
270}
271
Mike Lockwood755fd612010-05-25 19:08:48 -0400272const char* MtpDevice::getDeviceName() {
273 if (mDevice)
274 return usb_device_get_name(mDevice);
275 else
276 return "???";
277}
278
279bool MtpDevice::openSession() {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400280 Mutex::Autolock autoLock(mMutex);
281
Mike Lockwood755fd612010-05-25 19:08:48 -0400282 mSessionID = 0;
283 mTransactionID = 0;
284 MtpSessionID newSession = 1;
285 mRequest.reset();
286 mRequest.setParameter(1, newSession);
287 if (!sendRequest(MTP_OPERATION_OPEN_SESSION))
288 return false;
289 MtpResponseCode ret = readResponse();
290 if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN)
291 newSession = mResponse.getParameter(1);
292 else if (ret != MTP_RESPONSE_OK)
293 return false;
294
295 mSessionID = newSession;
296 mTransactionID = 1;
297 return true;
298}
299
300bool MtpDevice::closeSession() {
301 // FIXME
302 return true;
303}
304
305MtpDeviceInfo* MtpDevice::getDeviceInfo() {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400306 Mutex::Autolock autoLock(mMutex);
307
Mike Lockwood755fd612010-05-25 19:08:48 -0400308 mRequest.reset();
309 if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO))
310 return NULL;
311 if (!readData())
312 return NULL;
313 MtpResponseCode ret = readResponse();
Mike Lockwood755fd612010-05-25 19:08:48 -0400314 if (ret == MTP_RESPONSE_OK) {
315 MtpDeviceInfo* info = new MtpDeviceInfo;
316 info->read(mData);
317 return info;
318 }
319 return NULL;
320}
321
322MtpStorageIDList* MtpDevice::getStorageIDs() {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400323 Mutex::Autolock autoLock(mMutex);
324
Mike Lockwood755fd612010-05-25 19:08:48 -0400325 mRequest.reset();
326 if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
327 return NULL;
328 if (!readData())
329 return NULL;
330 MtpResponseCode ret = readResponse();
331 if (ret == MTP_RESPONSE_OK) {
332 return mData.getAUInt32();
333 }
334 return NULL;
335}
336
337MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400338 Mutex::Autolock autoLock(mMutex);
339
Mike Lockwood755fd612010-05-25 19:08:48 -0400340 mRequest.reset();
341 mRequest.setParameter(1, storageID);
342 if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
343 return NULL;
344 if (!readData())
345 return NULL;
346 MtpResponseCode ret = readResponse();
Mike Lockwood755fd612010-05-25 19:08:48 -0400347 if (ret == MTP_RESPONSE_OK) {
348 MtpStorageInfo* info = new MtpStorageInfo(storageID);
349 info->read(mData);
350 return info;
351 }
352 return NULL;
353}
354
355MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
356 MtpObjectFormat format, MtpObjectHandle parent) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400357 Mutex::Autolock autoLock(mMutex);
358
Mike Lockwood755fd612010-05-25 19:08:48 -0400359 mRequest.reset();
360 mRequest.setParameter(1, storageID);
361 mRequest.setParameter(2, format);
362 mRequest.setParameter(3, parent);
363 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
364 return NULL;
365 if (!readData())
366 return NULL;
367 MtpResponseCode ret = readResponse();
Mike Lockwood755fd612010-05-25 19:08:48 -0400368 if (ret == MTP_RESPONSE_OK) {
369 return mData.getAUInt32();
370 }
371 return NULL;
372}
373
374MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400375 Mutex::Autolock autoLock(mMutex);
376
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400377 // FIXME - we might want to add some caching here
378
Mike Lockwood755fd612010-05-25 19:08:48 -0400379 mRequest.reset();
380 mRequest.setParameter(1, handle);
381 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
382 return NULL;
383 if (!readData())
384 return NULL;
385 MtpResponseCode ret = readResponse();
Mike Lockwood755fd612010-05-25 19:08:48 -0400386 if (ret == MTP_RESPONSE_OK) {
387 MtpObjectInfo* info = new MtpObjectInfo(handle);
388 info->read(mData);
389 return info;
390 }
391 return NULL;
392}
393
Mike Lockwooddda56862010-06-10 16:34:20 -0400394void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400395 Mutex::Autolock autoLock(mMutex);
396
Mike Lockwooddda56862010-06-10 16:34:20 -0400397 mRequest.reset();
398 mRequest.setParameter(1, handle);
399 if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
400 MtpResponseCode ret = readResponse();
401 if (ret == MTP_RESPONSE_OK) {
402 return mData.getData(outLength);
403 }
404 }
405 outLength = 0;
406 return NULL;
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400407}
Mike Lockwooddda56862010-06-10 16:34:20 -0400408
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400409MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
410 Mutex::Autolock autoLock(mMutex);
411
412 mRequest.reset();
413 MtpObjectHandle parent = info->mParent;
414 if (parent == 0)
415 parent = MTP_PARENT_ROOT;
416
417 mRequest.setParameter(1, info->mStorageID);
418 mRequest.setParameter(2, info->mParent);
419
420 mData.putUInt32(info->mStorageID);
421 mData.putUInt16(info->mFormat);
422 mData.putUInt16(info->mProtectionStatus);
423 mData.putUInt32(info->mCompressedSize);
424 mData.putUInt16(info->mThumbFormat);
425 mData.putUInt32(info->mThumbCompressedSize);
426 mData.putUInt32(info->mThumbPixWidth);
427 mData.putUInt32(info->mThumbPixHeight);
428 mData.putUInt32(info->mImagePixWidth);
429 mData.putUInt32(info->mImagePixHeight);
430 mData.putUInt32(info->mImagePixDepth);
431 mData.putUInt32(info->mParent);
432 mData.putUInt16(info->mAssociationType);
433 mData.putUInt32(info->mAssociationDesc);
434 mData.putUInt32(info->mSequenceNumber);
435 mData.putString(info->mName);
436
437 char created[100], modified[100];
438 formatDateTime(info->mDateCreated, created, sizeof(created));
439 formatDateTime(info->mDateModified, modified, sizeof(modified));
440
441 mData.putString(created);
442 mData.putString(modified);
443 if (info->mKeywords)
444 mData.putString(info->mKeywords);
445 else
446 mData.putEmptyString();
447
448 if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400449 MtpResponseCode ret = readResponse();
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400450 if (ret == MTP_RESPONSE_OK) {
451 info->mStorageID = mResponse.getParameter(1);
452 info->mParent = mResponse.getParameter(2);
453 info->mHandle = mResponse.getParameter(3);
454 return info->mHandle;
455 }
456 }
457 return (MtpObjectHandle)-1;
458}
459
460bool MtpDevice::sendObject(MtpObjectInfo* info, int srcFD) {
461 Mutex::Autolock autoLock(mMutex);
462
463 int remaining = info->mCompressedSize;
464 mRequest.reset();
465 mRequest.setParameter(1, info->mHandle);
466 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400467 // send data header
468 writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
469
470 char buffer[65536];
471 while (remaining > 0) {
472 int count = read(srcFD, buffer, sizeof(buffer));
473 if (count > 0) {
Mike Lockwood215b6822011-01-04 14:48:57 -0500474 int written = mData.write(mRequestOut, buffer, count);
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400475 // FIXME check error
476 remaining -= count;
477 } else {
478 break;
479 }
480 }
481 }
482 MtpResponseCode ret = readResponse();
483 return (remaining == 0 && ret == MTP_RESPONSE_OK);
484}
485
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400486bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400487 Mutex::Autolock autoLock(mMutex);
488
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400489 mRequest.reset();
490 mRequest.setParameter(1, handle);
491 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
492 MtpResponseCode ret = readResponse();
493 if (ret == MTP_RESPONSE_OK)
494 return true;
495 }
496 return false;
497}
498
499MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
500 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root3d384f82011-02-02 13:43:55 -0800501 if (info) {
502 MtpObjectHandle parent = info->mParent;
503 delete info;
504 return parent;
505 } else {
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400506 return -1;
Kenny Root3d384f82011-02-02 13:43:55 -0800507 }
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400508}
509
510MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
511 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root3d384f82011-02-02 13:43:55 -0800512 if (info) {
513 MtpObjectHandle storageId = info->mStorageID;
514 delete info;
515 return storageId;
516 } else {
Mike Lockwoode0a89f62010-06-11 16:34:52 -0400517 return -1;
Kenny Root3d384f82011-02-02 13:43:55 -0800518 }
Mike Lockwooddda56862010-06-10 16:34:20 -0400519}
520
Mike Lockwood5768f102010-12-07 10:58:56 -0800521MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
522 Mutex::Autolock autoLock(mMutex);
523
524 mRequest.reset();
525 mRequest.setParameter(1, format);
526 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
527 return NULL;
528 if (!readData())
529 return NULL;
530 MtpResponseCode ret = readResponse();
531 if (ret == MTP_RESPONSE_OK) {
532 return mData.getAUInt16();
533 }
534 return NULL;
535
536}
537
Mike Lockwood90f48732010-06-05 22:45:01 -0400538MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400539 Mutex::Autolock autoLock(mMutex);
540
Mike Lockwood90f48732010-06-05 22:45:01 -0400541 mRequest.reset();
542 mRequest.setParameter(1, code);
543 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
544 return NULL;
545 if (!readData())
546 return NULL;
547 MtpResponseCode ret = readResponse();
548 if (ret == MTP_RESPONSE_OK) {
549 MtpProperty* property = new MtpProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400550 property->read(mData);
Mike Lockwood90f48732010-06-05 22:45:01 -0400551 return property;
552 }
553 return NULL;
554}
555
Mike Lockwooda194cc72010-12-07 18:53:04 -0800556MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood5768f102010-12-07 10:58:56 -0800557 Mutex::Autolock autoLock(mMutex);
558
559 mRequest.reset();
560 mRequest.setParameter(1, code);
Mike Lockwooda194cc72010-12-07 18:53:04 -0800561 mRequest.setParameter(2, format);
Mike Lockwood5768f102010-12-07 10:58:56 -0800562 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
563 return NULL;
564 if (!readData())
565 return NULL;
566 MtpResponseCode ret = readResponse();
567 if (ret == MTP_RESPONSE_OK) {
568 MtpProperty* property = new MtpProperty;
569 property->read(mData);
570 return property;
571 }
572 return NULL;
573}
574
Mike Lockwood8182e722010-12-30 15:38:45 -0500575bool MtpDevice::readObject(MtpObjectHandle handle,
576 bool (* callback)(void* data, int offset, int length, void* clientData),
577 int objectSize, void* clientData) {
578 Mutex::Autolock autoLock(mMutex);
579 bool result = false;
580
581 mRequest.reset();
582 mRequest.setParameter(1, handle);
583 if (sendRequest(MTP_OPERATION_GET_OBJECT)
584 && mData.readDataHeader(mRequestIn1)) {
585 uint32_t length = mData.getContainerLength();
586 if (length - MTP_CONTAINER_HEADER_SIZE != objectSize) {
587 LOGE("readObject error objectSize: %d, length: %d",
588 objectSize, length);
589 goto fail;
590 }
591 length -= MTP_CONTAINER_HEADER_SIZE;
592 uint32_t remaining = length;
593 int offset = 0;
594
595 int initialDataLength = 0;
596 void* initialData = mData.getData(initialDataLength);
597 if (initialData) {
598 if (initialDataLength > 0) {
599 if (!callback(initialData, 0, initialDataLength, clientData))
600 goto fail;
601 remaining -= initialDataLength;
602 offset += initialDataLength;
603 }
604 free(initialData);
605 }
606
607 // USB reads greater than 16K don't work
608 char buffer1[16384], buffer2[16384];
609 mRequestIn1->buffer = buffer1;
610 mRequestIn2->buffer = buffer2;
611 struct usb_request* req = mRequestIn1;
612 void* writeBuffer = NULL;
613 int writeLength = 0;
614
615 while (remaining > 0 || writeBuffer) {
616 if (remaining > 0) {
617 // queue up a read request
618 req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
619 if (mData.readDataAsync(req)) {
620 LOGE("readDataAsync failed");
621 goto fail;
622 }
623 } else {
624 req = NULL;
625 }
626
627 if (writeBuffer) {
628 // write previous buffer
629 if (!callback(writeBuffer, offset, writeLength, clientData)) {
630 LOGE("write failed");
631 // wait for pending read before failing
632 if (req)
633 mData.readDataWait(mDevice);
634 goto fail;
635 }
636 offset += writeLength;
637 writeBuffer = NULL;
638 }
639
640 // wait for read to complete
641 if (req) {
642 int read = mData.readDataWait(mDevice);
643 if (read < 0)
644 goto fail;
645
646 if (read > 0) {
647 writeBuffer = req->buffer;
648 writeLength = read;
649 remaining -= read;
650 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
651 } else {
652 writeBuffer = NULL;
653 }
654 }
655 }
656
657 MtpResponseCode response = readResponse();
658 if (response == MTP_RESPONSE_OK)
659 result = true;
660 }
661
662fail:
663 return result;
664}
665
666
Mike Lockwood954c2672010-11-19 11:20:19 -0500667// reads the object's data and writes it to the specified file path
Mike Lockwood929b3da2010-11-19 13:52:20 -0500668bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Mike Lockwood954c2672010-11-19 11:20:19 -0500669 LOGD("readObject: %s", destPath);
670 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC);
671 if (fd < 0) {
672 LOGE("open failed for %s", destPath);
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400673 return false;
674 }
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400675
Mike Lockwood929b3da2010-11-19 13:52:20 -0500676 fchown(fd, getuid(), group);
677 // set permissions
678 int mask = umask(0);
679 fchmod(fd, perm);
680 umask(mask);
681
Mike Lockwood954c2672010-11-19 11:20:19 -0500682 Mutex::Autolock autoLock(mMutex);
683 bool result = false;
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400684
Mike Lockwood954c2672010-11-19 11:20:19 -0500685 mRequest.reset();
686 mRequest.setParameter(1, handle);
687 if (sendRequest(MTP_OPERATION_GET_OBJECT)
Mike Lockwood215b6822011-01-04 14:48:57 -0500688 && mData.readDataHeader(mRequestIn1)) {
Mike Lockwood954c2672010-11-19 11:20:19 -0500689 uint32_t length = mData.getContainerLength();
690 if (length < MTP_CONTAINER_HEADER_SIZE)
691 goto fail;
692 length -= MTP_CONTAINER_HEADER_SIZE;
693 uint32_t remaining = length;
694
695 int initialDataLength = 0;
696 void* initialData = mData.getData(initialDataLength);
697 if (initialData) {
698 if (initialDataLength > 0) {
Kenny Root3d384f82011-02-02 13:43:55 -0800699 if (write(fd, initialData, initialDataLength) != initialDataLength) {
700 free(initialData);
Mike Lockwood954c2672010-11-19 11:20:19 -0500701 goto fail;
Kenny Root3d384f82011-02-02 13:43:55 -0800702 }
Mike Lockwood954c2672010-11-19 11:20:19 -0500703 remaining -= initialDataLength;
704 }
705 free(initialData);
706 }
707
708 // USB reads greater than 16K don't work
709 char buffer1[16384], buffer2[16384];
Mike Lockwood215b6822011-01-04 14:48:57 -0500710 mRequestIn1->buffer = buffer1;
711 mRequestIn2->buffer = buffer2;
712 struct usb_request* req = mRequestIn1;
713 void* writeBuffer = NULL;
Mike Lockwood954c2672010-11-19 11:20:19 -0500714 int writeLength = 0;
715
716 while (remaining > 0 || writeBuffer) {
717 if (remaining > 0) {
718 // queue up a read request
Mike Lockwood215b6822011-01-04 14:48:57 -0500719 req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
720 if (mData.readDataAsync(req)) {
Mike Lockwood954c2672010-11-19 11:20:19 -0500721 LOGE("readDataAsync failed");
722 goto fail;
723 }
724 } else {
Mike Lockwood215b6822011-01-04 14:48:57 -0500725 req = NULL;
Mike Lockwood954c2672010-11-19 11:20:19 -0500726 }
727
728 if (writeBuffer) {
729 // write previous buffer
730 if (write(fd, writeBuffer, writeLength) != writeLength) {
731 LOGE("write failed");
732 // wait for pending read before failing
Mike Lockwood215b6822011-01-04 14:48:57 -0500733 if (req)
734 mData.readDataWait(mDevice);
Mike Lockwood954c2672010-11-19 11:20:19 -0500735 goto fail;
736 }
737 writeBuffer = NULL;
738 }
739
740 // wait for read to complete
Mike Lockwood215b6822011-01-04 14:48:57 -0500741 if (req) {
742 int read = mData.readDataWait(mDevice);
Mike Lockwood954c2672010-11-19 11:20:19 -0500743 if (read < 0)
744 goto fail;
745
Mike Lockwood8182e722010-12-30 15:38:45 -0500746 if (read > 0) {
747 writeBuffer = req->buffer;
748 writeLength = read;
749 remaining -= read;
750 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
751 } else {
752 writeBuffer = NULL;
753 }
Mike Lockwood954c2672010-11-19 11:20:19 -0500754 }
755 }
756
757 MtpResponseCode response = readResponse();
758 if (response == MTP_RESPONSE_OK)
759 result = true;
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400760 }
Mike Lockwood954c2672010-11-19 11:20:19 -0500761
762fail:
763 ::close(fd);
764 return result;
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400765}
Mike Lockwood90f48732010-06-05 22:45:01 -0400766
Mike Lockwood755fd612010-05-25 19:08:48 -0400767bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block06ade6a2011-10-20 11:56:00 +0100768 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodbfd1d722010-12-09 18:34:18 -0800769 mReceivedResponse = false;
Mike Lockwood755fd612010-05-25 19:08:48 -0400770 mRequest.setOperationCode(operation);
771 if (mTransactionID > 0)
772 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood215b6822011-01-04 14:48:57 -0500773 int ret = mRequest.write(mRequestOut);
Mike Lockwood755fd612010-05-25 19:08:48 -0400774 mRequest.dump();
775 return (ret > 0);
776}
777
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400778bool MtpDevice::sendData() {
Steve Block06ade6a2011-10-20 11:56:00 +0100779 ALOGV("sendData\n");
Mike Lockwood755fd612010-05-25 19:08:48 -0400780 mData.setOperationCode(mRequest.getOperationCode());
781 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood215b6822011-01-04 14:48:57 -0500782 int ret = mData.write(mRequestOut);
Mike Lockwood755fd612010-05-25 19:08:48 -0400783 mData.dump();
784 return (ret > 0);
785}
786
787bool MtpDevice::readData() {
788 mData.reset();
Mike Lockwood215b6822011-01-04 14:48:57 -0500789 int ret = mData.read(mRequestIn1);
Steve Block06ade6a2011-10-20 11:56:00 +0100790 ALOGV("readData returned %d\n", ret);
Mike Lockwood755fd612010-05-25 19:08:48 -0400791 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodbfd1d722010-12-09 18:34:18 -0800792 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
793 LOGD("got response packet instead of data packet");
794 // we got a response packet rather than data
795 // copy it to mResponse
796 mResponse.copyFrom(mData);
797 mReceivedResponse = true;
798 return false;
799 }
Mike Lockwood755fd612010-05-25 19:08:48 -0400800 mData.dump();
801 return true;
802 }
803 else {
Steve Block06ade6a2011-10-20 11:56:00 +0100804 ALOGV("readResponse failed\n");
Mike Lockwood755fd612010-05-25 19:08:48 -0400805 return false;
806 }
807}
808
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400809bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
810 mData.setOperationCode(operation);
811 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood215b6822011-01-04 14:48:57 -0500812 return (!mData.writeDataHeader(mRequestOut, dataLength));
Mike Lockwoodbc4cb0b2010-07-26 20:40:45 -0400813}
814
Mike Lockwood755fd612010-05-25 19:08:48 -0400815MtpResponseCode MtpDevice::readResponse() {
Steve Block06ade6a2011-10-20 11:56:00 +0100816 ALOGV("readResponse\n");
Mike Lockwoodbfd1d722010-12-09 18:34:18 -0800817 if (mReceivedResponse) {
818 mReceivedResponse = false;
819 return mResponse.getResponseCode();
820 }
Mike Lockwood215b6822011-01-04 14:48:57 -0500821 int ret = mResponse.read(mRequestIn1);
Mike Lockwood90f5d752011-03-14 10:33:22 -0400822 // handle zero length packets, which might occur if the data transfer
823 // ends on a packet boundary
824 if (ret == 0)
825 ret = mResponse.read(mRequestIn1);
Mike Lockwood755fd612010-05-25 19:08:48 -0400826 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
827 mResponse.dump();
828 return mResponse.getResponseCode();
Mike Lockwoodbfd1d722010-12-09 18:34:18 -0800829 } else {
Mike Lockwood90f48732010-06-05 22:45:01 -0400830 LOGD("readResponse failed\n");
Mike Lockwood755fd612010-05-25 19:08:48 -0400831 return -1;
832 }
833}
834
835} // namespace android