blob: 30843a752cef14eb7900747f98a1a160775ade97 [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"
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +090022#include "MtpEventPacket.h"
Mike Lockwoodb14e5882010-06-29 18:11:52 -040023#include "MtpObjectInfo.h"
24#include "MtpProperty.h"
25#include "MtpStorageInfo.h"
26#include "MtpStringBuffer.h"
Mike Lockwood0cf89f22010-07-26 20:40:45 -040027#include "MtpUtils.h"
Mike Lockwooda6c490b2010-06-05 22:45:01 -040028
Mike Lockwood5ed68d22010-05-25 19:08:48 -040029#include <stdio.h>
30#include <stdlib.h>
31#include <sys/types.h>
32#include <sys/ioctl.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <errno.h>
Mike Lockwood0cf89f22010-07-26 20:40:45 -040036#include <endian.h>
Mike Lockwood5ed68d22010-05-25 19:08:48 -040037
38#include <usbhost/usbhost.h>
39
Mike Lockwood5ed68d22010-05-25 19:08:48 -040040namespace android {
41
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -050042#if 0
Mike Lockwood23f1b332010-12-30 15:38:45 -050043static bool isMtpDevice(uint16_t vendor, uint16_t product) {
44 // Sandisk Sansa Fuze
45 if (vendor == 0x0781 && product == 0x74c2)
46 return true;
47 // Samsung YP-Z5
48 if (vendor == 0x04e8 && product == 0x503c)
49 return true;
50 return false;
51}
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -050052#endif
Mike Lockwood23f1b332010-12-30 15:38:45 -050053
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +090054namespace {
55
Daichi Hirono4a7cea82015-12-11 15:49:43 +090056bool writeToFd(void* data, uint32_t /* unused_offset */, uint32_t length, void* clientData) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +090057 const int fd = *static_cast<int*>(clientData);
Daichi Hirono4a7cea82015-12-11 15:49:43 +090058 const ssize_t result = write(fd, data, length);
59 if (result < 0) {
60 return false;
61 }
62 return static_cast<uint32_t>(result) == length;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +090063}
64
Daichi Hirono4a7cea82015-12-11 15:49:43 +090065} // namespace
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +090066
Mike Lockwood23f1b332010-12-30 15:38:45 -050067MtpDevice* MtpDevice::open(const char* deviceName, int fd) {
68 struct usb_device *device = usb_device_new(deviceName, fd);
69 if (!device) {
Steve Block29357bc2012-01-06 19:20:56 +000070 ALOGE("usb_device_new failed for %s", deviceName);
Mike Lockwood23f1b332010-12-30 15:38:45 -050071 return NULL;
72 }
73
74 struct usb_descriptor_header* desc;
75 struct usb_descriptor_iter iter;
76
77 usb_descriptor_iter_init(device, &iter);
78
79 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
80 if (desc->bDescriptorType == USB_DT_INTERFACE) {
81 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
82
83 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
84 interface->bInterfaceSubClass == 1 && // Still Image Capture
85 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
86 {
Kenny Root31c52e72011-02-02 13:43:55 -080087 char* manufacturerName = usb_device_get_manufacturer_name(device);
88 char* productName = usb_device_get_product_name(device);
Steve Blockb8a80522011-12-20 16:23:08 +000089 ALOGD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
Kenny Root31c52e72011-02-02 13:43:55 -080090 free(manufacturerName);
91 free(productName);
Mike Lockwood23f1b332010-12-30 15:38:45 -050092 } else if (interface->bInterfaceClass == 0xFF &&
93 interface->bInterfaceSubClass == 0xFF &&
94 interface->bInterfaceProtocol == 0) {
95 char* interfaceName = usb_device_get_string(device, interface->iInterface);
Kenny Root31c52e72011-02-02 13:43:55 -080096 if (!interfaceName) {
Mike Lockwood23f1b332010-12-30 15:38:45 -050097 continue;
Kenny Root31c52e72011-02-02 13:43:55 -080098 } else if (strcmp(interfaceName, "MTP")) {
99 free(interfaceName);
100 continue;
101 }
102 free(interfaceName);
103
Mike Lockwood23f1b332010-12-30 15:38:45 -0500104 // Looks like an android style MTP device
Kenny Root31c52e72011-02-02 13:43:55 -0800105 char* manufacturerName = usb_device_get_manufacturer_name(device);
106 char* productName = usb_device_get_product_name(device);
Steve Blockb8a80522011-12-20 16:23:08 +0000107 ALOGD("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
Kenny Root31c52e72011-02-02 13:43:55 -0800108 free(manufacturerName);
109 free(productName);
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -0500110 }
111#if 0
112 else {
Mike Lockwood23f1b332010-12-30 15:38:45 -0500113 // look for special cased devices based on vendor/product ID
114 // we are doing this mainly for testing purposes
115 uint16_t vendor = usb_device_get_vendor_id(device);
116 uint16_t product = usb_device_get_product_id(device);
117 if (!isMtpDevice(vendor, product)) {
118 // not an MTP or PTP device
119 continue;
120 }
121 // request MTP OS string and descriptor
122 // some music players need to see this before entering MTP mode.
123 char buffer[256];
124 memset(buffer, 0, sizeof(buffer));
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800125 int ret = usb_device_control_transfer(device,
Mike Lockwood23f1b332010-12-30 15:38:45 -0500126 USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_STANDARD,
127 USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | 0xEE,
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800128 0, buffer, sizeof(buffer), 0);
129 printf("usb_device_control_transfer returned %d errno: %d\n", ret, errno);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500130 if (ret > 0) {
131 printf("got MTP string %s\n", buffer);
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800132 ret = usb_device_control_transfer(device,
Mike Lockwood23f1b332010-12-30 15:38:45 -0500133 USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 1,
Mike Lockwoodf41ef0e2011-01-27 10:47:40 -0800134 0, 4, buffer, sizeof(buffer), 0);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500135 printf("OS descriptor got %d\n", ret);
136 } else {
137 printf("no MTP string\n");
138 }
139 }
Daichi Hironod6dabe92015-12-02 15:08:48 +0900140#else
141 else {
142 continue;
143 }
Mike Lockwoodd4fb52e2011-02-15 14:55:51 -0500144#endif
Mike Lockwood23f1b332010-12-30 15:38:45 -0500145 // if we got here, then we have a likely MTP or PTP device
146
147 // interface should be followed by three endpoints
148 struct usb_endpoint_descriptor *ep;
149 struct usb_endpoint_descriptor *ep_in_desc = NULL;
150 struct usb_endpoint_descriptor *ep_out_desc = NULL;
151 struct usb_endpoint_descriptor *ep_intr_desc = NULL;
Bo Huang8023f3a2013-06-09 08:53:21 +0800152 //USB3 add USB_DT_SS_ENDPOINT_COMP as companion descriptor;
153 struct usb_ss_ep_comp_descriptor *ep_ss_ep_comp_desc = NULL;
Mike Lockwood23f1b332010-12-30 15:38:45 -0500154 for (int i = 0; i < 3; i++) {
155 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
Bo Huang8023f3a2013-06-09 08:53:21 +0800156 if (ep && ep->bDescriptorType == USB_DT_SS_ENDPOINT_COMP) {
157 ALOGD("Descriptor type is USB_DT_SS_ENDPOINT_COMP for USB3 \n");
158 ep_ss_ep_comp_desc = (usb_ss_ep_comp_descriptor*)ep;
159 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
160 }
161
Mike Lockwood23f1b332010-12-30 15:38:45 -0500162 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
Steve Block29357bc2012-01-06 19:20:56 +0000163 ALOGE("endpoints not found\n");
Kenny Root31c52e72011-02-02 13:43:55 -0800164 usb_device_close(device);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500165 return NULL;
166 }
Bo Huang8023f3a2013-06-09 08:53:21 +0800167
Mike Lockwood23f1b332010-12-30 15:38:45 -0500168 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
169 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
170 ep_in_desc = ep;
171 else
172 ep_out_desc = ep;
173 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
174 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
175 ep_intr_desc = ep;
176 }
177 }
178 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
Steve Block29357bc2012-01-06 19:20:56 +0000179 ALOGE("endpoints not found\n");
Kenny Root31c52e72011-02-02 13:43:55 -0800180 usb_device_close(device);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500181 return NULL;
182 }
183
184 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
Steve Block29357bc2012-01-06 19:20:56 +0000185 ALOGE("usb_device_claim_interface failed errno: %d\n", errno);
Kenny Root31c52e72011-02-02 13:43:55 -0800186 usb_device_close(device);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500187 return NULL;
188 }
189
190 MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
191 ep_in_desc, ep_out_desc, ep_intr_desc);
192 mtpDevice->initialize();
193 return mtpDevice;
194 }
195 }
196
197 usb_device_close(device);
Steve Block29357bc2012-01-06 19:20:56 +0000198 ALOGE("device not found");
Mike Lockwood23f1b332010-12-30 15:38:45 -0500199 return NULL;
200}
201
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400202MtpDevice::MtpDevice(struct usb_device* device, int interface,
Mike Lockwood42d0b792011-01-04 14:48:57 -0500203 const struct usb_endpoint_descriptor *ep_in,
204 const struct usb_endpoint_descriptor *ep_out,
205 const struct usb_endpoint_descriptor *ep_intr)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400206 : mDevice(device),
207 mInterface(interface),
Mike Lockwood42d0b792011-01-04 14:48:57 -0500208 mRequestIn1(NULL),
209 mRequestIn2(NULL),
210 mRequestOut(NULL),
211 mRequestIntr(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400212 mDeviceInfo(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400213 mSessionID(0),
Mike Lockwoodf7454622010-12-09 18:34:18 -0800214 mTransactionID(0),
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900215 mReceivedResponse(false),
216 mProcessingEvent(false),
217 mCurrentEventHandle(0)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400218{
Mike Lockwood42d0b792011-01-04 14:48:57 -0500219 mRequestIn1 = usb_request_new(device, ep_in);
220 mRequestIn2 = usb_request_new(device, ep_in);
221 mRequestOut = usb_request_new(device, ep_out);
222 mRequestIntr = usb_request_new(device, ep_intr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400223}
224
225MtpDevice::~MtpDevice() {
226 close();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700227 for (size_t i = 0; i < mDeviceProperties.size(); i++)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400228 delete mDeviceProperties[i];
Mike Lockwood42d0b792011-01-04 14:48:57 -0500229 usb_request_free(mRequestIn1);
230 usb_request_free(mRequestIn2);
231 usb_request_free(mRequestOut);
232 usb_request_free(mRequestIntr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400233}
234
235void MtpDevice::initialize() {
236 openSession();
237 mDeviceInfo = getDeviceInfo();
238 if (mDeviceInfo) {
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400239 if (mDeviceInfo->mDeviceProperties) {
240 int count = mDeviceInfo->mDeviceProperties->size();
241 for (int i = 0; i < count; i++) {
242 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
243 MtpProperty* property = getDevicePropDesc(propCode);
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800244 if (property)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400245 mDeviceProperties.push(property);
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400246 }
247 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400248 }
249}
250
251void MtpDevice::close() {
252 if (mDevice) {
253 usb_device_release_interface(mDevice, mInterface);
254 usb_device_close(mDevice);
255 mDevice = NULL;
256 }
257}
258
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800259void MtpDevice::print() {
260 if (mDeviceInfo) {
261 mDeviceInfo->print();
262
263 if (mDeviceInfo->mDeviceProperties) {
Steve Blockdf64d152012-01-04 20:05:49 +0000264 ALOGI("***** DEVICE PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800265 int count = mDeviceInfo->mDeviceProperties->size();
266 for (int i = 0; i < count; i++) {
267 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
268 MtpProperty* property = getDevicePropDesc(propCode);
269 if (property) {
270 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800271 delete property;
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800272 }
273 }
274 }
275 }
276
277 if (mDeviceInfo->mPlaybackFormats) {
Steve Blockdf64d152012-01-04 20:05:49 +0000278 ALOGI("***** OBJECT PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800279 int count = mDeviceInfo->mPlaybackFormats->size();
280 for (int i = 0; i < count; i++) {
281 MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
Steve Blockdf64d152012-01-04 20:05:49 +0000282 ALOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800283 MtpObjectPropertyList* props = getObjectPropsSupported(format);
284 if (props) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700285 for (size_t j = 0; j < props->size(); j++) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800286 MtpObjectProperty prop = (*props)[j];
Mike Lockwood99e393a2010-12-07 18:53:04 -0800287 MtpProperty* property = getObjectPropDesc(prop, format);
Kenny Root31c52e72011-02-02 13:43:55 -0800288 if (property) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800289 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800290 delete property;
291 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000292 ALOGE("could not fetch property: %s",
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800293 MtpDebug::getObjectPropCodeName(prop));
Kenny Root31c52e72011-02-02 13:43:55 -0800294 }
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800295 }
296 }
297 }
298 }
299}
300
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400301const char* MtpDevice::getDeviceName() {
302 if (mDevice)
303 return usb_device_get_name(mDevice);
304 else
305 return "???";
306}
307
308bool MtpDevice::openSession() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400309 Mutex::Autolock autoLock(mMutex);
310
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400311 mSessionID = 0;
312 mTransactionID = 0;
313 MtpSessionID newSession = 1;
314 mRequest.reset();
315 mRequest.setParameter(1, newSession);
316 if (!sendRequest(MTP_OPERATION_OPEN_SESSION))
317 return false;
318 MtpResponseCode ret = readResponse();
319 if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN)
320 newSession = mResponse.getParameter(1);
321 else if (ret != MTP_RESPONSE_OK)
322 return false;
323
324 mSessionID = newSession;
325 mTransactionID = 1;
326 return true;
327}
328
329bool MtpDevice::closeSession() {
330 // FIXME
331 return true;
332}
333
334MtpDeviceInfo* MtpDevice::getDeviceInfo() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400335 Mutex::Autolock autoLock(mMutex);
336
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400337 mRequest.reset();
338 if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO))
339 return NULL;
340 if (!readData())
341 return NULL;
342 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400343 if (ret == MTP_RESPONSE_OK) {
344 MtpDeviceInfo* info = new MtpDeviceInfo;
Mike Lockwoodab063842014-11-12 14:20:06 -0800345 if (info->read(mData))
346 return info;
347 else
348 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400349 }
350 return NULL;
351}
352
353MtpStorageIDList* MtpDevice::getStorageIDs() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400354 Mutex::Autolock autoLock(mMutex);
355
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400356 mRequest.reset();
357 if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
358 return NULL;
359 if (!readData())
360 return NULL;
361 MtpResponseCode ret = readResponse();
362 if (ret == MTP_RESPONSE_OK) {
363 return mData.getAUInt32();
364 }
365 return NULL;
366}
367
368MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400369 Mutex::Autolock autoLock(mMutex);
370
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400371 mRequest.reset();
372 mRequest.setParameter(1, storageID);
373 if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
374 return NULL;
375 if (!readData())
376 return NULL;
377 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400378 if (ret == MTP_RESPONSE_OK) {
379 MtpStorageInfo* info = new MtpStorageInfo(storageID);
Mike Lockwoodab063842014-11-12 14:20:06 -0800380 if (info->read(mData))
381 return info;
382 else
383 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400384 }
385 return NULL;
386}
387
388MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
389 MtpObjectFormat format, MtpObjectHandle parent) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400390 Mutex::Autolock autoLock(mMutex);
391
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400392 mRequest.reset();
393 mRequest.setParameter(1, storageID);
394 mRequest.setParameter(2, format);
395 mRequest.setParameter(3, parent);
396 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
397 return NULL;
398 if (!readData())
399 return NULL;
400 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400401 if (ret == MTP_RESPONSE_OK) {
402 return mData.getAUInt32();
403 }
404 return NULL;
405}
406
407MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400408 Mutex::Autolock autoLock(mMutex);
409
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400410 // FIXME - we might want to add some caching here
411
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400412 mRequest.reset();
413 mRequest.setParameter(1, handle);
414 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
415 return NULL;
416 if (!readData())
417 return NULL;
418 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400419 if (ret == MTP_RESPONSE_OK) {
420 MtpObjectInfo* info = new MtpObjectInfo(handle);
Mike Lockwoodab063842014-11-12 14:20:06 -0800421 if (info->read(mData))
422 return info;
423 else
424 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400425 }
426 return NULL;
427}
428
Mike Lockwood3e072b32010-06-10 16:34:20 -0400429void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400430 Mutex::Autolock autoLock(mMutex);
431
Mike Lockwood3e072b32010-06-10 16:34:20 -0400432 mRequest.reset();
433 mRequest.setParameter(1, handle);
434 if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
435 MtpResponseCode ret = readResponse();
436 if (ret == MTP_RESPONSE_OK) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900437 return mData.getData(&outLength);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400438 }
439 }
440 outLength = 0;
441 return NULL;
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400442}
Mike Lockwood3e072b32010-06-10 16:34:20 -0400443
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400444MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
445 Mutex::Autolock autoLock(mMutex);
446
447 mRequest.reset();
448 MtpObjectHandle parent = info->mParent;
449 if (parent == 0)
450 parent = MTP_PARENT_ROOT;
451
452 mRequest.setParameter(1, info->mStorageID);
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900453 mRequest.setParameter(2, parent);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400454
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900455 mData.reset();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400456 mData.putUInt32(info->mStorageID);
457 mData.putUInt16(info->mFormat);
458 mData.putUInt16(info->mProtectionStatus);
459 mData.putUInt32(info->mCompressedSize);
460 mData.putUInt16(info->mThumbFormat);
461 mData.putUInt32(info->mThumbCompressedSize);
462 mData.putUInt32(info->mThumbPixWidth);
463 mData.putUInt32(info->mThumbPixHeight);
464 mData.putUInt32(info->mImagePixWidth);
465 mData.putUInt32(info->mImagePixHeight);
466 mData.putUInt32(info->mImagePixDepth);
467 mData.putUInt32(info->mParent);
468 mData.putUInt16(info->mAssociationType);
469 mData.putUInt32(info->mAssociationDesc);
470 mData.putUInt32(info->mSequenceNumber);
471 mData.putString(info->mName);
472
473 char created[100], modified[100];
474 formatDateTime(info->mDateCreated, created, sizeof(created));
475 formatDateTime(info->mDateModified, modified, sizeof(modified));
476
477 mData.putString(created);
478 mData.putString(modified);
479 if (info->mKeywords)
480 mData.putString(info->mKeywords);
481 else
482 mData.putEmptyString();
483
484 if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400485 MtpResponseCode ret = readResponse();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400486 if (ret == MTP_RESPONSE_OK) {
487 info->mStorageID = mResponse.getParameter(1);
488 info->mParent = mResponse.getParameter(2);
489 info->mHandle = mResponse.getParameter(3);
490 return info->mHandle;
491 }
492 }
493 return (MtpObjectHandle)-1;
494}
495
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900496bool MtpDevice::sendObject(MtpObjectHandle handle, int size, int srcFD) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400497 Mutex::Autolock autoLock(mMutex);
498
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900499 int remaining = size;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400500 mRequest.reset();
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900501 mRequest.setParameter(1, handle);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400502 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400503 // send data header
504 writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
505
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900506 // USB writes greater than 16K don't work
507 char buffer[MTP_BUFFER_SIZE];
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400508 while (remaining > 0) {
509 int count = read(srcFD, buffer, sizeof(buffer));
510 if (count > 0) {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500511 int written = mData.write(mRequestOut, buffer, count);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400512 // FIXME check error
513 remaining -= count;
514 } else {
515 break;
516 }
517 }
518 }
519 MtpResponseCode ret = readResponse();
520 return (remaining == 0 && ret == MTP_RESPONSE_OK);
521}
522
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400523bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400524 Mutex::Autolock autoLock(mMutex);
525
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400526 mRequest.reset();
527 mRequest.setParameter(1, handle);
528 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
529 MtpResponseCode ret = readResponse();
530 if (ret == MTP_RESPONSE_OK)
531 return true;
532 }
533 return false;
534}
535
536MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
537 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800538 if (info) {
539 MtpObjectHandle parent = info->mParent;
540 delete info;
541 return parent;
542 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400543 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800544 }
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400545}
546
547MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
548 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800549 if (info) {
550 MtpObjectHandle storageId = info->mStorageID;
551 delete info;
552 return storageId;
553 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400554 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800555 }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400556}
557
Mike Lockwood98693f62010-12-07 10:58:56 -0800558MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
559 Mutex::Autolock autoLock(mMutex);
560
561 mRequest.reset();
562 mRequest.setParameter(1, format);
563 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
564 return NULL;
565 if (!readData())
566 return NULL;
567 MtpResponseCode ret = readResponse();
568 if (ret == MTP_RESPONSE_OK) {
569 return mData.getAUInt16();
570 }
571 return NULL;
572
573}
574
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400575MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400576 Mutex::Autolock autoLock(mMutex);
577
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400578 mRequest.reset();
579 mRequest.setParameter(1, code);
580 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
581 return NULL;
582 if (!readData())
583 return NULL;
584 MtpResponseCode ret = readResponse();
585 if (ret == MTP_RESPONSE_OK) {
586 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800587 if (property->read(mData))
588 return property;
589 else
590 delete property;
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400591 }
592 return NULL;
593}
594
Mike Lockwood99e393a2010-12-07 18:53:04 -0800595MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood98693f62010-12-07 10:58:56 -0800596 Mutex::Autolock autoLock(mMutex);
597
598 mRequest.reset();
599 mRequest.setParameter(1, code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800600 mRequest.setParameter(2, format);
Mike Lockwood98693f62010-12-07 10:58:56 -0800601 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
602 return NULL;
603 if (!readData())
604 return NULL;
605 MtpResponseCode ret = readResponse();
606 if (ret == MTP_RESPONSE_OK) {
607 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800608 if (property->read(mData))
609 return property;
610 else
611 delete property;
Mike Lockwood98693f62010-12-07 10:58:56 -0800612 }
613 return NULL;
614}
615
Mike Lockwood23f1b332010-12-30 15:38:45 -0500616bool MtpDevice::readObject(MtpObjectHandle handle,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900617 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900618 uint32_t expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900619 void* clientData) {
620 return readObjectInternal(handle, callback, &expectedLength, clientData);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500621}
622
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500623// reads the object's data and writes it to the specified file path
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500624bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Steve Blockb8a80522011-12-20 16:23:08 +0000625 ALOGD("readObject: %s", destPath);
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700626 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500627 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000628 ALOGE("open failed for %s", destPath);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400629 return false;
630 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400631
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500632 fchown(fd, getuid(), group);
633 // set permissions
634 int mask = umask(0);
635 fchmod(fd, perm);
636 umask(mask);
637
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900638 bool result = readObject(handle, fd);
639 ::close(fd);
640 return result;
641}
642
643bool MtpDevice::readObject(MtpObjectHandle handle, int fd) {
644 ALOGD("readObject: %d", fd);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900645 return readObjectInternal(handle, writeToFd, NULL /* expected size */, &fd);
646}
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900647
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900648bool MtpDevice::readObjectInternal(MtpObjectHandle handle,
649 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900650 const uint32_t* expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900651 void* clientData) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500652 Mutex::Autolock autoLock(mMutex);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400653
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500654 mRequest.reset();
655 mRequest.setParameter(1, handle);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900656 if (!sendRequest(MTP_OPERATION_GET_OBJECT)) {
657 ALOGE("Failed to send a read request.");
658 return false;
659 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500660
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900661 return readData(callback, expectedLength, nullptr, clientData);
662}
663
664bool MtpDevice::readData(ReadObjectCallback callback,
665 const uint32_t* expectedLength,
666 uint32_t* writtenSize,
667 void* clientData) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900668 if (!mData.readDataHeader(mRequestIn1)) {
669 ALOGE("Failed to read header.");
670 return false;
671 }
672
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900673 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
674 mResponse.copyFrom(mData);
675 return mResponse.getResponseCode() == MTP_RESPONSE_OK ? 0 : -1;
676 }
677
Daichi Hironoa04e6fa2015-12-24 14:20:44 +0900678 // If object size 0 byte, the remote device can reply response packet
679 // without sending any data packets.
680 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
681 mResponse.copyFrom(mData);
682 return mResponse.getResponseCode() == MTP_RESPONSE_OK;
683 }
684
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900685 const uint32_t fullLength = mData.getContainerLength();
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900686 if (fullLength < MTP_CONTAINER_HEADER_SIZE) {
687 ALOGE("fullLength is too short: %d", fullLength);
688 return false;
689 }
690 const uint32_t length = fullLength - MTP_CONTAINER_HEADER_SIZE;
691 if (expectedLength && length != *expectedLength) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900692 ALOGE("readObject error length: %d", fullLength);
693 return false;
694 }
695
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900696 uint32_t offset = 0;
697 bool writingError = false;
698
699 {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500700 int initialDataLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900701 void* const initialData = mData.getData(&initialDataLength);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500702 if (initialData) {
703 if (initialDataLength > 0) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900704 if (!callback(initialData, offset, initialDataLength, clientData)) {
Daichi Hirono81ca5ad2015-08-18 21:13:40 +0900705 ALOGE("Failed to write initial data.");
706 writingError = true;
Kenny Root31c52e72011-02-02 13:43:55 -0800707 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900708 offset += initialDataLength;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500709 }
710 free(initialData);
711 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900712 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500713
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900714 // USB reads greater than 16K don't work.
715 char buffer1[MTP_BUFFER_SIZE], buffer2[MTP_BUFFER_SIZE];
716 mRequestIn1->buffer = buffer1;
717 mRequestIn2->buffer = buffer2;
718 struct usb_request* req = NULL;
719
720 while (offset < length) {
721 // Wait for previous read to complete.
Mike Lockwood42d0b792011-01-04 14:48:57 -0500722 void* writeBuffer = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500723 int writeLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900724 if (req) {
725 const int read = mData.readDataWait(mDevice);
726 if (read < 0) {
727 ALOGE("readDataWait failed.");
728 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500729 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900730 writeBuffer = req->buffer;
731 writeLength = read;
732 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500733
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900734 // Request to read next chunk.
735 const uint32_t nextOffset = offset + writeLength;
736 if (nextOffset < length) {
737 // Queue up a read request.
738 const size_t remaining = length - nextOffset;
739 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900740 req->buffer_length = remaining > MTP_BUFFER_SIZE ?
741 static_cast<size_t>(MTP_BUFFER_SIZE) : remaining;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900742 if (mData.readDataAsync(req) != 0) {
743 ALOGE("readDataAsync failed");
744 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500745 }
746 }
747
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900748 // Write previous buffer.
749 if (writeBuffer && !writingError) {
750 if (!callback(writeBuffer, offset, writeLength, clientData)) {
751 ALOGE("write failed");
752 writingError = true;
753 }
754 }
755 offset = nextOffset;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400756 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500757
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900758 if (writtenSize) {
759 *writtenSize = length;
760 }
761
762 return readResponse() == MTP_RESPONSE_OK;
763}
764
765bool MtpDevice::readPartialObject(MtpObjectHandle handle,
766 uint32_t offset,
767 uint32_t size,
768 uint32_t *writtenSize,
769 ReadObjectCallback callback,
770 void* clientData) {
771 Mutex::Autolock autoLock(mMutex);
772
773 mRequest.reset();
774 mRequest.setParameter(1, handle);
775 mRequest.setParameter(2, offset);
776 mRequest.setParameter(3, size);
777 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT)) {
778 ALOGE("Failed to send a read request.");
779 return false;
780 }
781 return readData(callback, NULL /* expected size */, writtenSize, clientData);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400782}
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400783
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400784bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodf7454622010-12-09 18:34:18 -0800786 mReceivedResponse = false;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400787 mRequest.setOperationCode(operation);
788 if (mTransactionID > 0)
789 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500790 int ret = mRequest.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400791 mRequest.dump();
792 return (ret > 0);
793}
794
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400795bool MtpDevice::sendData() {
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("sendData\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400797 mData.setOperationCode(mRequest.getOperationCode());
798 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500799 int ret = mData.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400800 mData.dump();
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900801 return (ret >= 0);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400802}
803
804bool MtpDevice::readData() {
805 mData.reset();
Mike Lockwood42d0b792011-01-04 14:48:57 -0500806 int ret = mData.read(mRequestIn1);
Steve Block3856b092011-10-20 11:56:00 +0100807 ALOGV("readData returned %d\n", ret);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400808 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodf7454622010-12-09 18:34:18 -0800809 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
Steve Blockb8a80522011-12-20 16:23:08 +0000810 ALOGD("got response packet instead of data packet");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800811 // we got a response packet rather than data
812 // copy it to mResponse
813 mResponse.copyFrom(mData);
814 mReceivedResponse = true;
815 return false;
816 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400817 mData.dump();
818 return true;
819 }
820 else {
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400822 return false;
823 }
824}
825
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400826bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
827 mData.setOperationCode(operation);
828 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500829 return (!mData.writeDataHeader(mRequestOut, dataLength));
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400830}
831
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400832MtpResponseCode MtpDevice::readResponse() {
Steve Block3856b092011-10-20 11:56:00 +0100833 ALOGV("readResponse\n");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800834 if (mReceivedResponse) {
835 mReceivedResponse = false;
836 return mResponse.getResponseCode();
837 }
Mike Lockwood42d0b792011-01-04 14:48:57 -0500838 int ret = mResponse.read(mRequestIn1);
Mike Lockwood3d744572011-03-14 10:33:22 -0400839 // handle zero length packets, which might occur if the data transfer
840 // ends on a packet boundary
841 if (ret == 0)
842 ret = mResponse.read(mRequestIn1);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400843 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
844 mResponse.dump();
845 return mResponse.getResponseCode();
Mike Lockwoodf7454622010-12-09 18:34:18 -0800846 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000847 ALOGD("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400848 return -1;
849 }
850}
851
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900852int MtpDevice::submitEventRequest() {
853 if (mEventMutex.tryLock()) {
854 // An event is being reaped on another thread.
855 return -1;
856 }
857 if (mProcessingEvent) {
858 // An event request was submitted, but no reapEventRequest called so far.
859 return -1;
860 }
861 Mutex::Autolock autoLock(mEventMutexForInterrupt);
862 mEventPacket.sendRequest(mRequestIntr);
863 const int currentHandle = ++mCurrentEventHandle;
864 mProcessingEvent = true;
865 mEventMutex.unlock();
866 return currentHandle;
867}
868
869int MtpDevice::reapEventRequest(int handle) {
870 Mutex::Autolock autoLock(mEventMutex);
871 if (!mProcessingEvent || mCurrentEventHandle != handle) {
872 return -1;
873 }
874 mProcessingEvent = false;
875 const int readSize = mEventPacket.readResponse(mRequestIntr->dev);
876 const int result = mEventPacket.getEventCode();
877 return readSize == 0 ? 0 : result;
878}
879
880void MtpDevice::discardEventRequest(int handle) {
881 Mutex::Autolock autoLock(mEventMutexForInterrupt);
882 if (mCurrentEventHandle != handle) {
883 return;
884 }
885 usb_request_cancel(mRequestIntr);
886}
887
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400888} // namespace android