blob: e0d679d53845ad4d0c9ec8f7c1992b4a50f72001 [file] [log] [blame]
Mike Lockwood5ed68d22010-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 Lockwooda6c490b2010-06-05 22:45:01 -040017#define LOG_TAG "MtpDevice"
Mike Lockwoodb14e5882010-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 Lockwood0cf89f22010-07-26 20:40:45 -040026#include "MtpUtils.h"
Mike Lockwooda6c490b2010-06-05 22:45:01 -040027
Mike Lockwood5ed68d22010-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 Lockwood0cf89f22010-07-26 20:40:45 -040035#include <endian.h>
Mike Lockwood5ed68d22010-05-25 19:08:48 -040036
37#include <usbhost/usbhost.h>
38
Mike Lockwood5ed68d22010-05-25 19:08:48 -040039namespace android {
40
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -050041#if 0
Mike Lockwood23f1b332010-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 Lockwoodd4fb52e2011-02-15 14:55:51 -050051#endif
Mike Lockwood23f1b332010-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) {
Steve Block29357bc2012-01-06 19:20:56 +000056 ALOGE("usb_device_new failed for %s", deviceName);
Mike Lockwood23f1b332010-12-30 15:38:45 -050057 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 Root31c52e72011-02-02 13:43:55 -080073 char* manufacturerName = usb_device_get_manufacturer_name(device);
74 char* productName = usb_device_get_product_name(device);
Steve Blockb8a80522011-12-20 16:23:08 +000075 ALOGD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
Kenny Root31c52e72011-02-02 13:43:55 -080076 free(manufacturerName);
77 free(productName);
Mike Lockwood23f1b332010-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 Root31c52e72011-02-02 13:43:55 -080082 if (!interfaceName) {
Mike Lockwood23f1b332010-12-30 15:38:45 -050083 continue;
Kenny Root31c52e72011-02-02 13:43:55 -080084 } else if (strcmp(interfaceName, "MTP")) {
85 free(interfaceName);
86 continue;
87 }
88 free(interfaceName);
89
Mike Lockwood23f1b332010-12-30 15:38:45 -050090 // Looks like an android style MTP device
Kenny Root31c52e72011-02-02 13:43:55 -080091 char* manufacturerName = usb_device_get_manufacturer_name(device);
92 char* productName = usb_device_get_product_name(device);
Steve Blockb8a80522011-12-20 16:23:08 +000093 ALOGD("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
Kenny Root31c52e72011-02-02 13:43:55 -080094 free(manufacturerName);
95 free(productName);
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -050096 }
97#if 0
98 else {
Mike Lockwood23f1b332010-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 Lockwoodf41ef0e2011-01-27 10:47:40 -0800111 int ret = usb_device_control_transfer(device,
Mike Lockwood23f1b332010-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 Lockwoodf41ef0e2011-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 Lockwood23f1b332010-12-30 15:38:45 -0500116 if (ret > 0) {
117 printf("got MTP string %s\n", buffer);
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800118 ret = usb_device_control_transfer(device,
Mike Lockwood23f1b332010-12-30 15:38:45 -0500119 USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 1,
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800120 0, 4, buffer, sizeof(buffer), 0);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500121 printf("OS descriptor got %d\n", ret);
122 } else {
123 printf("no MTP string\n");
124 }
125 }
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -0500126#endif
Mike Lockwood23f1b332010-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) {
Steve Block29357bc2012-01-06 19:20:56 +0000137 ALOGE("endpoints not found\n");
Kenny Root31c52e72011-02-02 13:43:55 -0800138 usb_device_close(device);
Mike Lockwood23f1b332010-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) {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("endpoints not found\n");
Kenny Root31c52e72011-02-02 13:43:55 -0800153 usb_device_close(device);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500154 return NULL;
155 }
156
157 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
Steve Block29357bc2012-01-06 19:20:56 +0000158 ALOGE("usb_device_claim_interface failed errno: %d\n", errno);
Kenny Root31c52e72011-02-02 13:43:55 -0800159 usb_device_close(device);
Mike Lockwood23f1b332010-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);
Steve Block29357bc2012-01-06 19:20:56 +0000171 ALOGE("device not found");
Mike Lockwood23f1b332010-12-30 15:38:45 -0500172 return NULL;
173}
174
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400175MtpDevice::MtpDevice(struct usb_device* device, int interface,
Mike Lockwood42d0b792011-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 Lockwood5ed68d22010-05-25 19:08:48 -0400179 : mDevice(device),
180 mInterface(interface),
Mike Lockwood42d0b792011-01-04 14:48:57 -0500181 mRequestIn1(NULL),
182 mRequestIn2(NULL),
183 mRequestOut(NULL),
184 mRequestIntr(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400185 mDeviceInfo(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400186 mSessionID(0),
Mike Lockwoodf7454622010-12-09 18:34:18 -0800187 mTransactionID(0),
188 mReceivedResponse(false)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400189{
Mike Lockwood42d0b792011-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 Lockwood5ed68d22010-05-25 19:08:48 -0400194}
195
196MtpDevice::~MtpDevice() {
197 close();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700198 for (size_t i = 0; i < mDeviceProperties.size(); i++)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400199 delete mDeviceProperties[i];
Mike Lockwood42d0b792011-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 Lockwood5ed68d22010-05-25 19:08:48 -0400204}
205
206void MtpDevice::initialize() {
207 openSession();
208 mDeviceInfo = getDeviceInfo();
209 if (mDeviceInfo) {
Mike Lockwooda6c490b2010-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 Lockwood0c7c7c72010-12-07 11:24:28 -0800215 if (property)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400216 mDeviceProperties.push(property);
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400217 }
218 }
Mike Lockwood5ed68d22010-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 Lockwood0c7c7c72010-12-07 11:24:28 -0800230void MtpDevice::print() {
231 if (mDeviceInfo) {
232 mDeviceInfo->print();
233
234 if (mDeviceInfo->mDeviceProperties) {
Steve Blockdf64d152012-01-04 20:05:49 +0000235 ALOGI("***** DEVICE PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800236 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 Root31c52e72011-02-02 13:43:55 -0800242 delete property;
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800243 }
244 }
245 }
246 }
247
248 if (mDeviceInfo->mPlaybackFormats) {
Steve Blockdf64d152012-01-04 20:05:49 +0000249 ALOGI("***** OBJECT PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800250 int count = mDeviceInfo->mPlaybackFormats->size();
251 for (int i = 0; i < count; i++) {
252 MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
Steve Blockdf64d152012-01-04 20:05:49 +0000253 ALOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800254 MtpObjectPropertyList* props = getObjectPropsSupported(format);
255 if (props) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700256 for (size_t j = 0; j < props->size(); j++) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800257 MtpObjectProperty prop = (*props)[j];
Mike Lockwood99e393a2010-12-07 18:53:04 -0800258 MtpProperty* property = getObjectPropDesc(prop, format);
Kenny Root31c52e72011-02-02 13:43:55 -0800259 if (property) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800260 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800261 delete property;
262 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000263 ALOGE("could not fetch property: %s",
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800264 MtpDebug::getObjectPropCodeName(prop));
Kenny Root31c52e72011-02-02 13:43:55 -0800265 }
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800266 }
267 }
268 }
269 }
270}
271
Mike Lockwood5ed68d22010-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 Lockwood0cf89f22010-07-26 20:40:45 -0400280 Mutex::Autolock autoLock(mMutex);
281
Mike Lockwood5ed68d22010-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 Lockwood0cf89f22010-07-26 20:40:45 -0400306 Mutex::Autolock autoLock(mMutex);
307
Mike Lockwood5ed68d22010-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 Lockwood5ed68d22010-05-25 19:08:48 -0400314 if (ret == MTP_RESPONSE_OK) {
315 MtpDeviceInfo* info = new MtpDeviceInfo;
Mike Lockwoodab063842014-11-12 14:20:06 -0800316 if (info->read(mData))
317 return info;
318 else
319 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400320 }
321 return NULL;
322}
323
324MtpStorageIDList* MtpDevice::getStorageIDs() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400325 Mutex::Autolock autoLock(mMutex);
326
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400327 mRequest.reset();
328 if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
329 return NULL;
330 if (!readData())
331 return NULL;
332 MtpResponseCode ret = readResponse();
333 if (ret == MTP_RESPONSE_OK) {
334 return mData.getAUInt32();
335 }
336 return NULL;
337}
338
339MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400340 Mutex::Autolock autoLock(mMutex);
341
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400342 mRequest.reset();
343 mRequest.setParameter(1, storageID);
344 if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
345 return NULL;
346 if (!readData())
347 return NULL;
348 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400349 if (ret == MTP_RESPONSE_OK) {
350 MtpStorageInfo* info = new MtpStorageInfo(storageID);
Mike Lockwoodab063842014-11-12 14:20:06 -0800351 if (info->read(mData))
352 return info;
353 else
354 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400355 }
356 return NULL;
357}
358
359MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
360 MtpObjectFormat format, MtpObjectHandle parent) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400361 Mutex::Autolock autoLock(mMutex);
362
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400363 mRequest.reset();
364 mRequest.setParameter(1, storageID);
365 mRequest.setParameter(2, format);
366 mRequest.setParameter(3, parent);
367 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
368 return NULL;
369 if (!readData())
370 return NULL;
371 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400372 if (ret == MTP_RESPONSE_OK) {
373 return mData.getAUInt32();
374 }
375 return NULL;
376}
377
378MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400379 Mutex::Autolock autoLock(mMutex);
380
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400381 // FIXME - we might want to add some caching here
382
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400383 mRequest.reset();
384 mRequest.setParameter(1, handle);
385 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
386 return NULL;
387 if (!readData())
388 return NULL;
389 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400390 if (ret == MTP_RESPONSE_OK) {
391 MtpObjectInfo* info = new MtpObjectInfo(handle);
Mike Lockwoodab063842014-11-12 14:20:06 -0800392 if (info->read(mData))
393 return info;
394 else
395 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400396 }
397 return NULL;
398}
399
Mike Lockwood3e072b32010-06-10 16:34:20 -0400400void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400401 Mutex::Autolock autoLock(mMutex);
402
Mike Lockwood3e072b32010-06-10 16:34:20 -0400403 mRequest.reset();
404 mRequest.setParameter(1, handle);
405 if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
406 MtpResponseCode ret = readResponse();
407 if (ret == MTP_RESPONSE_OK) {
408 return mData.getData(outLength);
409 }
410 }
411 outLength = 0;
412 return NULL;
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400413}
Mike Lockwood3e072b32010-06-10 16:34:20 -0400414
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400415MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
416 Mutex::Autolock autoLock(mMutex);
417
418 mRequest.reset();
419 MtpObjectHandle parent = info->mParent;
420 if (parent == 0)
421 parent = MTP_PARENT_ROOT;
422
423 mRequest.setParameter(1, info->mStorageID);
424 mRequest.setParameter(2, info->mParent);
425
426 mData.putUInt32(info->mStorageID);
427 mData.putUInt16(info->mFormat);
428 mData.putUInt16(info->mProtectionStatus);
429 mData.putUInt32(info->mCompressedSize);
430 mData.putUInt16(info->mThumbFormat);
431 mData.putUInt32(info->mThumbCompressedSize);
432 mData.putUInt32(info->mThumbPixWidth);
433 mData.putUInt32(info->mThumbPixHeight);
434 mData.putUInt32(info->mImagePixWidth);
435 mData.putUInt32(info->mImagePixHeight);
436 mData.putUInt32(info->mImagePixDepth);
437 mData.putUInt32(info->mParent);
438 mData.putUInt16(info->mAssociationType);
439 mData.putUInt32(info->mAssociationDesc);
440 mData.putUInt32(info->mSequenceNumber);
441 mData.putString(info->mName);
442
443 char created[100], modified[100];
444 formatDateTime(info->mDateCreated, created, sizeof(created));
445 formatDateTime(info->mDateModified, modified, sizeof(modified));
446
447 mData.putString(created);
448 mData.putString(modified);
449 if (info->mKeywords)
450 mData.putString(info->mKeywords);
451 else
452 mData.putEmptyString();
453
454 if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400455 MtpResponseCode ret = readResponse();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400456 if (ret == MTP_RESPONSE_OK) {
457 info->mStorageID = mResponse.getParameter(1);
458 info->mParent = mResponse.getParameter(2);
459 info->mHandle = mResponse.getParameter(3);
460 return info->mHandle;
461 }
462 }
463 return (MtpObjectHandle)-1;
464}
465
466bool MtpDevice::sendObject(MtpObjectInfo* info, int srcFD) {
467 Mutex::Autolock autoLock(mMutex);
468
469 int remaining = info->mCompressedSize;
470 mRequest.reset();
471 mRequest.setParameter(1, info->mHandle);
472 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400473 // send data header
474 writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
475
476 char buffer[65536];
477 while (remaining > 0) {
478 int count = read(srcFD, buffer, sizeof(buffer));
479 if (count > 0) {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500480 int written = mData.write(mRequestOut, buffer, count);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400481 // FIXME check error
482 remaining -= count;
483 } else {
484 break;
485 }
486 }
487 }
488 MtpResponseCode ret = readResponse();
489 return (remaining == 0 && ret == MTP_RESPONSE_OK);
490}
491
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400492bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400493 Mutex::Autolock autoLock(mMutex);
494
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400495 mRequest.reset();
496 mRequest.setParameter(1, handle);
497 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
498 MtpResponseCode ret = readResponse();
499 if (ret == MTP_RESPONSE_OK)
500 return true;
501 }
502 return false;
503}
504
505MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
506 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800507 if (info) {
508 MtpObjectHandle parent = info->mParent;
509 delete info;
510 return parent;
511 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400512 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800513 }
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400514}
515
516MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
517 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800518 if (info) {
519 MtpObjectHandle storageId = info->mStorageID;
520 delete info;
521 return storageId;
522 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400523 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800524 }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400525}
526
Mike Lockwood98693f62010-12-07 10:58:56 -0800527MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
528 Mutex::Autolock autoLock(mMutex);
529
530 mRequest.reset();
531 mRequest.setParameter(1, format);
532 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
533 return NULL;
534 if (!readData())
535 return NULL;
536 MtpResponseCode ret = readResponse();
537 if (ret == MTP_RESPONSE_OK) {
538 return mData.getAUInt16();
539 }
540 return NULL;
541
542}
543
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400544MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400545 Mutex::Autolock autoLock(mMutex);
546
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400547 mRequest.reset();
548 mRequest.setParameter(1, code);
549 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
550 return NULL;
551 if (!readData())
552 return NULL;
553 MtpResponseCode ret = readResponse();
554 if (ret == MTP_RESPONSE_OK) {
555 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800556 if (property->read(mData))
557 return property;
558 else
559 delete property;
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400560 }
561 return NULL;
562}
563
Mike Lockwood99e393a2010-12-07 18:53:04 -0800564MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood98693f62010-12-07 10:58:56 -0800565 Mutex::Autolock autoLock(mMutex);
566
567 mRequest.reset();
568 mRequest.setParameter(1, code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800569 mRequest.setParameter(2, format);
Mike Lockwood98693f62010-12-07 10:58:56 -0800570 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
571 return NULL;
572 if (!readData())
573 return NULL;
574 MtpResponseCode ret = readResponse();
575 if (ret == MTP_RESPONSE_OK) {
576 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800577 if (property->read(mData))
578 return property;
579 else
580 delete property;
Mike Lockwood98693f62010-12-07 10:58:56 -0800581 }
582 return NULL;
583}
584
Mike Lockwood23f1b332010-12-30 15:38:45 -0500585bool MtpDevice::readObject(MtpObjectHandle handle,
586 bool (* callback)(void* data, int offset, int length, void* clientData),
Mike Lockwoodab063842014-11-12 14:20:06 -0800587 size_t objectSize, void* clientData) {
Mike Lockwood23f1b332010-12-30 15:38:45 -0500588 Mutex::Autolock autoLock(mMutex);
589 bool result = false;
590
591 mRequest.reset();
592 mRequest.setParameter(1, handle);
593 if (sendRequest(MTP_OPERATION_GET_OBJECT)
594 && mData.readDataHeader(mRequestIn1)) {
595 uint32_t length = mData.getContainerLength();
596 if (length - MTP_CONTAINER_HEADER_SIZE != objectSize) {
Steve Block29357bc2012-01-06 19:20:56 +0000597 ALOGE("readObject error objectSize: %d, length: %d",
Mike Lockwood23f1b332010-12-30 15:38:45 -0500598 objectSize, length);
599 goto fail;
600 }
601 length -= MTP_CONTAINER_HEADER_SIZE;
602 uint32_t remaining = length;
603 int offset = 0;
604
605 int initialDataLength = 0;
606 void* initialData = mData.getData(initialDataLength);
607 if (initialData) {
608 if (initialDataLength > 0) {
609 if (!callback(initialData, 0, initialDataLength, clientData))
610 goto fail;
611 remaining -= initialDataLength;
612 offset += initialDataLength;
613 }
614 free(initialData);
615 }
616
617 // USB reads greater than 16K don't work
618 char buffer1[16384], buffer2[16384];
619 mRequestIn1->buffer = buffer1;
620 mRequestIn2->buffer = buffer2;
621 struct usb_request* req = mRequestIn1;
622 void* writeBuffer = NULL;
623 int writeLength = 0;
624
625 while (remaining > 0 || writeBuffer) {
626 if (remaining > 0) {
627 // queue up a read request
628 req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
629 if (mData.readDataAsync(req)) {
Steve Block29357bc2012-01-06 19:20:56 +0000630 ALOGE("readDataAsync failed");
Mike Lockwood23f1b332010-12-30 15:38:45 -0500631 goto fail;
632 }
633 } else {
634 req = NULL;
635 }
636
637 if (writeBuffer) {
638 // write previous buffer
639 if (!callback(writeBuffer, offset, writeLength, clientData)) {
Steve Block29357bc2012-01-06 19:20:56 +0000640 ALOGE("write failed");
Mike Lockwood23f1b332010-12-30 15:38:45 -0500641 // wait for pending read before failing
642 if (req)
643 mData.readDataWait(mDevice);
644 goto fail;
645 }
646 offset += writeLength;
647 writeBuffer = NULL;
648 }
649
650 // wait for read to complete
651 if (req) {
652 int read = mData.readDataWait(mDevice);
653 if (read < 0)
654 goto fail;
655
656 if (read > 0) {
657 writeBuffer = req->buffer;
658 writeLength = read;
659 remaining -= read;
660 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
661 } else {
662 writeBuffer = NULL;
663 }
664 }
665 }
666
667 MtpResponseCode response = readResponse();
668 if (response == MTP_RESPONSE_OK)
669 result = true;
670 }
671
672fail:
673 return result;
674}
675
676
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500677// reads the object's data and writes it to the specified file path
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500678bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Steve Blockb8a80522011-12-20 16:23:08 +0000679 ALOGD("readObject: %s", destPath);
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700680 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500681 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000682 ALOGE("open failed for %s", destPath);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400683 return false;
684 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400685
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500686 fchown(fd, getuid(), group);
687 // set permissions
688 int mask = umask(0);
689 fchmod(fd, perm);
690 umask(mask);
691
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500692 Mutex::Autolock autoLock(mMutex);
693 bool result = false;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400694
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500695 mRequest.reset();
696 mRequest.setParameter(1, handle);
697 if (sendRequest(MTP_OPERATION_GET_OBJECT)
Mike Lockwood42d0b792011-01-04 14:48:57 -0500698 && mData.readDataHeader(mRequestIn1)) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500699 uint32_t length = mData.getContainerLength();
700 if (length < MTP_CONTAINER_HEADER_SIZE)
701 goto fail;
702 length -= MTP_CONTAINER_HEADER_SIZE;
703 uint32_t remaining = length;
704
705 int initialDataLength = 0;
706 void* initialData = mData.getData(initialDataLength);
707 if (initialData) {
708 if (initialDataLength > 0) {
Kenny Root31c52e72011-02-02 13:43:55 -0800709 if (write(fd, initialData, initialDataLength) != initialDataLength) {
710 free(initialData);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500711 goto fail;
Kenny Root31c52e72011-02-02 13:43:55 -0800712 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500713 remaining -= initialDataLength;
714 }
715 free(initialData);
716 }
717
718 // USB reads greater than 16K don't work
719 char buffer1[16384], buffer2[16384];
Mike Lockwood42d0b792011-01-04 14:48:57 -0500720 mRequestIn1->buffer = buffer1;
721 mRequestIn2->buffer = buffer2;
722 struct usb_request* req = mRequestIn1;
723 void* writeBuffer = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500724 int writeLength = 0;
725
726 while (remaining > 0 || writeBuffer) {
727 if (remaining > 0) {
728 // queue up a read request
Mike Lockwood42d0b792011-01-04 14:48:57 -0500729 req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
730 if (mData.readDataAsync(req)) {
Steve Block29357bc2012-01-06 19:20:56 +0000731 ALOGE("readDataAsync failed");
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500732 goto fail;
733 }
734 } else {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500735 req = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500736 }
737
738 if (writeBuffer) {
739 // write previous buffer
740 if (write(fd, writeBuffer, writeLength) != writeLength) {
Steve Block29357bc2012-01-06 19:20:56 +0000741 ALOGE("write failed");
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500742 // wait for pending read before failing
Mike Lockwood42d0b792011-01-04 14:48:57 -0500743 if (req)
744 mData.readDataWait(mDevice);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500745 goto fail;
746 }
747 writeBuffer = NULL;
748 }
749
750 // wait for read to complete
Mike Lockwood42d0b792011-01-04 14:48:57 -0500751 if (req) {
752 int read = mData.readDataWait(mDevice);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500753 if (read < 0)
754 goto fail;
755
Mike Lockwood23f1b332010-12-30 15:38:45 -0500756 if (read > 0) {
757 writeBuffer = req->buffer;
758 writeLength = read;
759 remaining -= read;
760 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
761 } else {
762 writeBuffer = NULL;
763 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500764 }
765 }
766
767 MtpResponseCode response = readResponse();
768 if (response == MTP_RESPONSE_OK)
769 result = true;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400770 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500771
772fail:
773 ::close(fd);
774 return result;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400775}
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400776
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400777bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block3856b092011-10-20 11:56:00 +0100778 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodf7454622010-12-09 18:34:18 -0800779 mReceivedResponse = false;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400780 mRequest.setOperationCode(operation);
781 if (mTransactionID > 0)
782 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500783 int ret = mRequest.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400784 mRequest.dump();
785 return (ret > 0);
786}
787
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400788bool MtpDevice::sendData() {
Steve Block3856b092011-10-20 11:56:00 +0100789 ALOGV("sendData\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400790 mData.setOperationCode(mRequest.getOperationCode());
791 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500792 int ret = mData.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400793 mData.dump();
794 return (ret > 0);
795}
796
797bool MtpDevice::readData() {
798 mData.reset();
Mike Lockwood42d0b792011-01-04 14:48:57 -0500799 int ret = mData.read(mRequestIn1);
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("readData returned %d\n", ret);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400801 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodf7454622010-12-09 18:34:18 -0800802 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
Steve Blockb8a80522011-12-20 16:23:08 +0000803 ALOGD("got response packet instead of data packet");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800804 // we got a response packet rather than data
805 // copy it to mResponse
806 mResponse.copyFrom(mData);
807 mReceivedResponse = true;
808 return false;
809 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400810 mData.dump();
811 return true;
812 }
813 else {
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400815 return false;
816 }
817}
818
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400819bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
820 mData.setOperationCode(operation);
821 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500822 return (!mData.writeDataHeader(mRequestOut, dataLength));
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400823}
824
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400825MtpResponseCode MtpDevice::readResponse() {
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("readResponse\n");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800827 if (mReceivedResponse) {
828 mReceivedResponse = false;
829 return mResponse.getResponseCode();
830 }
Mike Lockwood42d0b792011-01-04 14:48:57 -0500831 int ret = mResponse.read(mRequestIn1);
Mike Lockwood3d744572011-03-14 10:33:22 -0400832 // handle zero length packets, which might occur if the data transfer
833 // ends on a packet boundary
834 if (ret == 0)
835 ret = mResponse.read(mRequestIn1);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400836 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
837 mResponse.dump();
838 return mResponse.getResponseCode();
Mike Lockwoodf7454622010-12-09 18:34:18 -0800839 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000840 ALOGD("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400841 return -1;
842 }
843}
844
845} // namespace android