blob: 7d7ea130748978f9182716984b6034ceb33ba042 [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
Jaesung Chung33d58162016-02-01 16:41:51 +0900184 int ret = usb_device_claim_interface(device, interface->bInterfaceNumber);
185 if (ret && errno == EBUSY) {
186 // disconnect kernel driver and try again
187 usb_device_connect_kernel_driver(device, interface->bInterfaceNumber, false);
188 ret = usb_device_claim_interface(device, interface->bInterfaceNumber);
189 }
190 if (ret) {
Steve Block29357bc2012-01-06 19:20:56 +0000191 ALOGE("usb_device_claim_interface failed errno: %d\n", errno);
Kenny Root31c52e72011-02-02 13:43:55 -0800192 usb_device_close(device);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500193 return NULL;
194 }
195
196 MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
197 ep_in_desc, ep_out_desc, ep_intr_desc);
198 mtpDevice->initialize();
199 return mtpDevice;
200 }
201 }
202
203 usb_device_close(device);
Steve Block29357bc2012-01-06 19:20:56 +0000204 ALOGE("device not found");
Mike Lockwood23f1b332010-12-30 15:38:45 -0500205 return NULL;
206}
207
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400208MtpDevice::MtpDevice(struct usb_device* device, int interface,
Mike Lockwood42d0b792011-01-04 14:48:57 -0500209 const struct usb_endpoint_descriptor *ep_in,
210 const struct usb_endpoint_descriptor *ep_out,
211 const struct usb_endpoint_descriptor *ep_intr)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400212 : mDevice(device),
213 mInterface(interface),
Mike Lockwood42d0b792011-01-04 14:48:57 -0500214 mRequestIn1(NULL),
215 mRequestIn2(NULL),
216 mRequestOut(NULL),
217 mRequestIntr(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400218 mDeviceInfo(NULL),
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400219 mSessionID(0),
Mike Lockwoodf7454622010-12-09 18:34:18 -0800220 mTransactionID(0),
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900221 mReceivedResponse(false),
222 mProcessingEvent(false),
223 mCurrentEventHandle(0)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400224{
Mike Lockwood42d0b792011-01-04 14:48:57 -0500225 mRequestIn1 = usb_request_new(device, ep_in);
226 mRequestIn2 = usb_request_new(device, ep_in);
227 mRequestOut = usb_request_new(device, ep_out);
228 mRequestIntr = usb_request_new(device, ep_intr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400229}
230
231MtpDevice::~MtpDevice() {
232 close();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700233 for (size_t i = 0; i < mDeviceProperties.size(); i++)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400234 delete mDeviceProperties[i];
Mike Lockwood42d0b792011-01-04 14:48:57 -0500235 usb_request_free(mRequestIn1);
236 usb_request_free(mRequestIn2);
237 usb_request_free(mRequestOut);
238 usb_request_free(mRequestIntr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400239}
240
241void MtpDevice::initialize() {
242 openSession();
243 mDeviceInfo = getDeviceInfo();
244 if (mDeviceInfo) {
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400245 if (mDeviceInfo->mDeviceProperties) {
246 int count = mDeviceInfo->mDeviceProperties->size();
247 for (int i = 0; i < count; i++) {
248 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
249 MtpProperty* property = getDevicePropDesc(propCode);
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800250 if (property)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400251 mDeviceProperties.push(property);
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400252 }
253 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400254 }
255}
256
257void MtpDevice::close() {
258 if (mDevice) {
259 usb_device_release_interface(mDevice, mInterface);
260 usb_device_close(mDevice);
261 mDevice = NULL;
262 }
263}
264
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800265void MtpDevice::print() {
266 if (mDeviceInfo) {
267 mDeviceInfo->print();
268
269 if (mDeviceInfo->mDeviceProperties) {
Steve Blockdf64d152012-01-04 20:05:49 +0000270 ALOGI("***** DEVICE PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800271 int count = mDeviceInfo->mDeviceProperties->size();
272 for (int i = 0; i < count; i++) {
273 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
274 MtpProperty* property = getDevicePropDesc(propCode);
275 if (property) {
276 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800277 delete property;
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800278 }
279 }
280 }
281 }
282
283 if (mDeviceInfo->mPlaybackFormats) {
Steve Blockdf64d152012-01-04 20:05:49 +0000284 ALOGI("***** OBJECT PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800285 int count = mDeviceInfo->mPlaybackFormats->size();
286 for (int i = 0; i < count; i++) {
287 MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
Steve Blockdf64d152012-01-04 20:05:49 +0000288 ALOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800289 MtpObjectPropertyList* props = getObjectPropsSupported(format);
290 if (props) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700291 for (size_t j = 0; j < props->size(); j++) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800292 MtpObjectProperty prop = (*props)[j];
Mike Lockwood99e393a2010-12-07 18:53:04 -0800293 MtpProperty* property = getObjectPropDesc(prop, format);
Kenny Root31c52e72011-02-02 13:43:55 -0800294 if (property) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800295 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800296 delete property;
297 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000298 ALOGE("could not fetch property: %s",
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800299 MtpDebug::getObjectPropCodeName(prop));
Kenny Root31c52e72011-02-02 13:43:55 -0800300 }
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800301 }
302 }
303 }
304 }
305}
306
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400307const char* MtpDevice::getDeviceName() {
308 if (mDevice)
309 return usb_device_get_name(mDevice);
310 else
311 return "???";
312}
313
314bool MtpDevice::openSession() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400315 Mutex::Autolock autoLock(mMutex);
316
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400317 mSessionID = 0;
318 mTransactionID = 0;
319 MtpSessionID newSession = 1;
320 mRequest.reset();
321 mRequest.setParameter(1, newSession);
322 if (!sendRequest(MTP_OPERATION_OPEN_SESSION))
323 return false;
324 MtpResponseCode ret = readResponse();
325 if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN)
326 newSession = mResponse.getParameter(1);
327 else if (ret != MTP_RESPONSE_OK)
328 return false;
329
330 mSessionID = newSession;
331 mTransactionID = 1;
332 return true;
333}
334
335bool MtpDevice::closeSession() {
336 // FIXME
337 return true;
338}
339
340MtpDeviceInfo* MtpDevice::getDeviceInfo() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400341 Mutex::Autolock autoLock(mMutex);
342
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400343 mRequest.reset();
344 if (!sendRequest(MTP_OPERATION_GET_DEVICE_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 MtpDeviceInfo* info = new MtpDeviceInfo;
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
359MtpStorageIDList* MtpDevice::getStorageIDs() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400360 Mutex::Autolock autoLock(mMutex);
361
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400362 mRequest.reset();
363 if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
364 return NULL;
365 if (!readData())
366 return NULL;
367 MtpResponseCode ret = readResponse();
368 if (ret == MTP_RESPONSE_OK) {
369 return mData.getAUInt32();
370 }
371 return NULL;
372}
373
374MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400375 Mutex::Autolock autoLock(mMutex);
376
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400377 mRequest.reset();
378 mRequest.setParameter(1, storageID);
379 if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
380 return NULL;
381 if (!readData())
382 return NULL;
383 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400384 if (ret == MTP_RESPONSE_OK) {
385 MtpStorageInfo* info = new MtpStorageInfo(storageID);
Mike Lockwoodab063842014-11-12 14:20:06 -0800386 if (info->read(mData))
387 return info;
388 else
389 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400390 }
391 return NULL;
392}
393
394MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
395 MtpObjectFormat format, MtpObjectHandle parent) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400396 Mutex::Autolock autoLock(mMutex);
397
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400398 mRequest.reset();
399 mRequest.setParameter(1, storageID);
400 mRequest.setParameter(2, format);
401 mRequest.setParameter(3, parent);
402 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
403 return NULL;
404 if (!readData())
405 return NULL;
406 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400407 if (ret == MTP_RESPONSE_OK) {
408 return mData.getAUInt32();
409 }
410 return NULL;
411}
412
413MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400414 Mutex::Autolock autoLock(mMutex);
415
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400416 // FIXME - we might want to add some caching here
417
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400418 mRequest.reset();
419 mRequest.setParameter(1, handle);
420 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
421 return NULL;
422 if (!readData())
423 return NULL;
424 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400425 if (ret == MTP_RESPONSE_OK) {
426 MtpObjectInfo* info = new MtpObjectInfo(handle);
Mike Lockwoodab063842014-11-12 14:20:06 -0800427 if (info->read(mData))
428 return info;
429 else
430 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400431 }
432 return NULL;
433}
434
Mike Lockwood3e072b32010-06-10 16:34:20 -0400435void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400436 Mutex::Autolock autoLock(mMutex);
437
Mike Lockwood3e072b32010-06-10 16:34:20 -0400438 mRequest.reset();
439 mRequest.setParameter(1, handle);
440 if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
441 MtpResponseCode ret = readResponse();
442 if (ret == MTP_RESPONSE_OK) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900443 return mData.getData(&outLength);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400444 }
445 }
446 outLength = 0;
447 return NULL;
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400448}
Mike Lockwood3e072b32010-06-10 16:34:20 -0400449
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400450MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
451 Mutex::Autolock autoLock(mMutex);
452
453 mRequest.reset();
454 MtpObjectHandle parent = info->mParent;
455 if (parent == 0)
456 parent = MTP_PARENT_ROOT;
457
458 mRequest.setParameter(1, info->mStorageID);
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900459 mRequest.setParameter(2, parent);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400460
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900461 mData.reset();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400462 mData.putUInt32(info->mStorageID);
463 mData.putUInt16(info->mFormat);
464 mData.putUInt16(info->mProtectionStatus);
465 mData.putUInt32(info->mCompressedSize);
466 mData.putUInt16(info->mThumbFormat);
467 mData.putUInt32(info->mThumbCompressedSize);
468 mData.putUInt32(info->mThumbPixWidth);
469 mData.putUInt32(info->mThumbPixHeight);
470 mData.putUInt32(info->mImagePixWidth);
471 mData.putUInt32(info->mImagePixHeight);
472 mData.putUInt32(info->mImagePixDepth);
473 mData.putUInt32(info->mParent);
474 mData.putUInt16(info->mAssociationType);
475 mData.putUInt32(info->mAssociationDesc);
476 mData.putUInt32(info->mSequenceNumber);
477 mData.putString(info->mName);
478
479 char created[100], modified[100];
480 formatDateTime(info->mDateCreated, created, sizeof(created));
481 formatDateTime(info->mDateModified, modified, sizeof(modified));
482
483 mData.putString(created);
484 mData.putString(modified);
485 if (info->mKeywords)
486 mData.putString(info->mKeywords);
487 else
488 mData.putEmptyString();
489
490 if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400491 MtpResponseCode ret = readResponse();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400492 if (ret == MTP_RESPONSE_OK) {
493 info->mStorageID = mResponse.getParameter(1);
494 info->mParent = mResponse.getParameter(2);
495 info->mHandle = mResponse.getParameter(3);
496 return info->mHandle;
497 }
498 }
499 return (MtpObjectHandle)-1;
500}
501
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900502bool MtpDevice::sendObject(MtpObjectHandle handle, int size, int srcFD) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400503 Mutex::Autolock autoLock(mMutex);
504
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900505 int remaining = size;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400506 mRequest.reset();
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900507 mRequest.setParameter(1, handle);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400508 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400509 // send data header
510 writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
511
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900512 // USB writes greater than 16K don't work
513 char buffer[MTP_BUFFER_SIZE];
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400514 while (remaining > 0) {
515 int count = read(srcFD, buffer, sizeof(buffer));
516 if (count > 0) {
Mike Lockwood42d0b792011-01-04 14:48:57 -0500517 int written = mData.write(mRequestOut, buffer, count);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400518 // FIXME check error
519 remaining -= count;
520 } else {
521 break;
522 }
523 }
524 }
525 MtpResponseCode ret = readResponse();
526 return (remaining == 0 && ret == MTP_RESPONSE_OK);
527}
528
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400529bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400530 Mutex::Autolock autoLock(mMutex);
531
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400532 mRequest.reset();
533 mRequest.setParameter(1, handle);
534 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
535 MtpResponseCode ret = readResponse();
536 if (ret == MTP_RESPONSE_OK)
537 return true;
538 }
539 return false;
540}
541
542MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
543 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800544 if (info) {
545 MtpObjectHandle parent = info->mParent;
546 delete info;
547 return parent;
548 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400549 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800550 }
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400551}
552
553MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
554 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800555 if (info) {
556 MtpObjectHandle storageId = info->mStorageID;
557 delete info;
558 return storageId;
559 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400560 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800561 }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400562}
563
Mike Lockwood98693f62010-12-07 10:58:56 -0800564MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
565 Mutex::Autolock autoLock(mMutex);
566
567 mRequest.reset();
568 mRequest.setParameter(1, format);
569 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
570 return NULL;
571 if (!readData())
572 return NULL;
573 MtpResponseCode ret = readResponse();
574 if (ret == MTP_RESPONSE_OK) {
575 return mData.getAUInt16();
576 }
577 return NULL;
578
579}
580
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400581MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400582 Mutex::Autolock autoLock(mMutex);
583
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400584 mRequest.reset();
585 mRequest.setParameter(1, code);
586 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
587 return NULL;
588 if (!readData())
589 return NULL;
590 MtpResponseCode ret = readResponse();
591 if (ret == MTP_RESPONSE_OK) {
592 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800593 if (property->read(mData))
594 return property;
595 else
596 delete property;
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400597 }
598 return NULL;
599}
600
Mike Lockwood99e393a2010-12-07 18:53:04 -0800601MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood98693f62010-12-07 10:58:56 -0800602 Mutex::Autolock autoLock(mMutex);
603
604 mRequest.reset();
605 mRequest.setParameter(1, code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800606 mRequest.setParameter(2, format);
Mike Lockwood98693f62010-12-07 10:58:56 -0800607 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
608 return NULL;
609 if (!readData())
610 return NULL;
611 MtpResponseCode ret = readResponse();
612 if (ret == MTP_RESPONSE_OK) {
613 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800614 if (property->read(mData))
615 return property;
616 else
617 delete property;
Mike Lockwood98693f62010-12-07 10:58:56 -0800618 }
619 return NULL;
620}
621
Mike Lockwood23f1b332010-12-30 15:38:45 -0500622bool MtpDevice::readObject(MtpObjectHandle handle,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900623 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900624 uint32_t expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900625 void* clientData) {
626 return readObjectInternal(handle, callback, &expectedLength, clientData);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500627}
628
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500629// reads the object's data and writes it to the specified file path
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500630bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Steve Blockb8a80522011-12-20 16:23:08 +0000631 ALOGD("readObject: %s", destPath);
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700632 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500633 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000634 ALOGE("open failed for %s", destPath);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400635 return false;
636 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400637
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500638 fchown(fd, getuid(), group);
639 // set permissions
640 int mask = umask(0);
641 fchmod(fd, perm);
642 umask(mask);
643
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900644 bool result = readObject(handle, fd);
645 ::close(fd);
646 return result;
647}
648
649bool MtpDevice::readObject(MtpObjectHandle handle, int fd) {
650 ALOGD("readObject: %d", fd);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900651 return readObjectInternal(handle, writeToFd, NULL /* expected size */, &fd);
652}
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900653
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900654bool MtpDevice::readObjectInternal(MtpObjectHandle handle,
655 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900656 const uint32_t* expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900657 void* clientData) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500658 Mutex::Autolock autoLock(mMutex);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400659
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500660 mRequest.reset();
661 mRequest.setParameter(1, handle);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900662 if (!sendRequest(MTP_OPERATION_GET_OBJECT)) {
663 ALOGE("Failed to send a read request.");
664 return false;
665 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500666
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900667 return readData(callback, expectedLength, nullptr, clientData);
668}
669
670bool MtpDevice::readData(ReadObjectCallback callback,
671 const uint32_t* expectedLength,
672 uint32_t* writtenSize,
673 void* clientData) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900674 if (!mData.readDataHeader(mRequestIn1)) {
675 ALOGE("Failed to read header.");
676 return false;
677 }
678
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900679 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
680 mResponse.copyFrom(mData);
681 return mResponse.getResponseCode() == MTP_RESPONSE_OK ? 0 : -1;
682 }
683
Daichi Hironoa04e6fa2015-12-24 14:20:44 +0900684 // If object size 0 byte, the remote device can reply response packet
685 // without sending any data packets.
686 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
687 mResponse.copyFrom(mData);
688 return mResponse.getResponseCode() == MTP_RESPONSE_OK;
689 }
690
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900691 const uint32_t fullLength = mData.getContainerLength();
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900692 if (fullLength < MTP_CONTAINER_HEADER_SIZE) {
693 ALOGE("fullLength is too short: %d", fullLength);
694 return false;
695 }
696 const uint32_t length = fullLength - MTP_CONTAINER_HEADER_SIZE;
697 if (expectedLength && length != *expectedLength) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900698 ALOGE("readObject error length: %d", fullLength);
699 return false;
700 }
701
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900702 uint32_t offset = 0;
703 bool writingError = false;
704
705 {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500706 int initialDataLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900707 void* const initialData = mData.getData(&initialDataLength);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500708 if (initialData) {
709 if (initialDataLength > 0) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900710 if (!callback(initialData, offset, initialDataLength, clientData)) {
Daichi Hirono81ca5ad2015-08-18 21:13:40 +0900711 ALOGE("Failed to write initial data.");
712 writingError = true;
Kenny Root31c52e72011-02-02 13:43:55 -0800713 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900714 offset += initialDataLength;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500715 }
716 free(initialData);
717 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900718 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500719
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900720 // USB reads greater than 16K don't work.
721 char buffer1[MTP_BUFFER_SIZE], buffer2[MTP_BUFFER_SIZE];
722 mRequestIn1->buffer = buffer1;
723 mRequestIn2->buffer = buffer2;
724 struct usb_request* req = NULL;
725
726 while (offset < length) {
727 // Wait for previous read to complete.
Mike Lockwood42d0b792011-01-04 14:48:57 -0500728 void* writeBuffer = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500729 int writeLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900730 if (req) {
731 const int read = mData.readDataWait(mDevice);
732 if (read < 0) {
733 ALOGE("readDataWait failed.");
734 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500735 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900736 writeBuffer = req->buffer;
737 writeLength = read;
738 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500739
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900740 // Request to read next chunk.
741 const uint32_t nextOffset = offset + writeLength;
742 if (nextOffset < length) {
743 // Queue up a read request.
744 const size_t remaining = length - nextOffset;
745 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900746 req->buffer_length = remaining > MTP_BUFFER_SIZE ?
747 static_cast<size_t>(MTP_BUFFER_SIZE) : remaining;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900748 if (mData.readDataAsync(req) != 0) {
749 ALOGE("readDataAsync failed");
750 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500751 }
752 }
753
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900754 // Write previous buffer.
755 if (writeBuffer && !writingError) {
756 if (!callback(writeBuffer, offset, writeLength, clientData)) {
757 ALOGE("write failed");
758 writingError = true;
759 }
760 }
761 offset = nextOffset;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400762 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500763
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900764 if (writtenSize) {
765 *writtenSize = length;
766 }
767
768 return readResponse() == MTP_RESPONSE_OK;
769}
770
771bool MtpDevice::readPartialObject(MtpObjectHandle handle,
772 uint32_t offset,
773 uint32_t size,
774 uint32_t *writtenSize,
775 ReadObjectCallback callback,
776 void* clientData) {
777 Mutex::Autolock autoLock(mMutex);
778
779 mRequest.reset();
780 mRequest.setParameter(1, handle);
781 mRequest.setParameter(2, offset);
782 mRequest.setParameter(3, size);
783 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT)) {
784 ALOGE("Failed to send a read request.");
785 return false;
786 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900787 // The expected size is null because it requires the exact number of bytes to read though
788 // MTP_OPERATION_GET_PARTIAL_OBJECT allows devices to return shorter length of bytes than
789 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900790 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
791}
792
793bool MtpDevice::readPartialObject64(MtpObjectHandle handle,
794 uint64_t offset,
795 uint32_t size,
796 uint32_t *writtenSize,
797 ReadObjectCallback callback,
798 void* clientData) {
799 Mutex::Autolock autoLock(mMutex);
800
801 mRequest.reset();
802 mRequest.setParameter(1, handle);
803 mRequest.setParameter(2, 0xffffffff & offset);
804 mRequest.setParameter(3, 0xffffffff & (offset >> 32));
805 mRequest.setParameter(4, size);
806 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT_64)) {
807 ALOGE("Failed to send a read request.");
808 return false;
809 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900810 // The expected size is null because it requires the exact number of bytes to read though
811 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 allows devices to return shorter length of bytes than
812 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900813 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400814}
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400815
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400816bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodf7454622010-12-09 18:34:18 -0800818 mReceivedResponse = false;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400819 mRequest.setOperationCode(operation);
820 if (mTransactionID > 0)
821 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500822 int ret = mRequest.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400823 mRequest.dump();
824 return (ret > 0);
825}
826
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400827bool MtpDevice::sendData() {
Steve Block3856b092011-10-20 11:56:00 +0100828 ALOGV("sendData\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400829 mData.setOperationCode(mRequest.getOperationCode());
830 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500831 int ret = mData.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400832 mData.dump();
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900833 return (ret >= 0);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400834}
835
836bool MtpDevice::readData() {
837 mData.reset();
Mike Lockwood42d0b792011-01-04 14:48:57 -0500838 int ret = mData.read(mRequestIn1);
Steve Block3856b092011-10-20 11:56:00 +0100839 ALOGV("readData returned %d\n", ret);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400840 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodf7454622010-12-09 18:34:18 -0800841 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
Steve Blockb8a80522011-12-20 16:23:08 +0000842 ALOGD("got response packet instead of data packet");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800843 // we got a response packet rather than data
844 // copy it to mResponse
845 mResponse.copyFrom(mData);
846 mReceivedResponse = true;
847 return false;
848 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400849 mData.dump();
850 return true;
851 }
852 else {
Steve Block3856b092011-10-20 11:56:00 +0100853 ALOGV("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400854 return false;
855 }
856}
857
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400858bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
859 mData.setOperationCode(operation);
860 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500861 return (!mData.writeDataHeader(mRequestOut, dataLength));
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400862}
863
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400864MtpResponseCode MtpDevice::readResponse() {
Steve Block3856b092011-10-20 11:56:00 +0100865 ALOGV("readResponse\n");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800866 if (mReceivedResponse) {
867 mReceivedResponse = false;
868 return mResponse.getResponseCode();
869 }
Mike Lockwood42d0b792011-01-04 14:48:57 -0500870 int ret = mResponse.read(mRequestIn1);
Mike Lockwood3d744572011-03-14 10:33:22 -0400871 // handle zero length packets, which might occur if the data transfer
872 // ends on a packet boundary
873 if (ret == 0)
874 ret = mResponse.read(mRequestIn1);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400875 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
876 mResponse.dump();
877 return mResponse.getResponseCode();
Mike Lockwoodf7454622010-12-09 18:34:18 -0800878 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000879 ALOGD("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400880 return -1;
881 }
882}
883
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900884int MtpDevice::submitEventRequest() {
885 if (mEventMutex.tryLock()) {
886 // An event is being reaped on another thread.
887 return -1;
888 }
889 if (mProcessingEvent) {
890 // An event request was submitted, but no reapEventRequest called so far.
891 return -1;
892 }
893 Mutex::Autolock autoLock(mEventMutexForInterrupt);
894 mEventPacket.sendRequest(mRequestIntr);
895 const int currentHandle = ++mCurrentEventHandle;
896 mProcessingEvent = true;
897 mEventMutex.unlock();
898 return currentHandle;
899}
900
Daichi Hirono59a90602016-01-11 13:36:57 +0900901int MtpDevice::reapEventRequest(int handle, uint32_t (*parameters)[3]) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900902 Mutex::Autolock autoLock(mEventMutex);
Daichi Hirono59a90602016-01-11 13:36:57 +0900903 if (!mProcessingEvent || mCurrentEventHandle != handle || !parameters) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900904 return -1;
905 }
906 mProcessingEvent = false;
907 const int readSize = mEventPacket.readResponse(mRequestIntr->dev);
908 const int result = mEventPacket.getEventCode();
Daichi Hirono59a90602016-01-11 13:36:57 +0900909 // MTP event has three parameters.
910 (*parameters)[0] = mEventPacket.getParameter(1);
911 (*parameters)[1] = mEventPacket.getParameter(2);
912 (*parameters)[2] = mEventPacket.getParameter(3);
913 return readSize != 0 ? result : 0;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900914}
915
916void MtpDevice::discardEventRequest(int handle) {
917 Mutex::Autolock autoLock(mEventMutexForInterrupt);
918 if (mCurrentEventHandle != handle) {
919 return;
920 }
921 usb_request_cancel(mRequestIntr);
922}
923
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400924} // namespace android