blob: 52ea3633f84d98a34a4b7c457f22cc95effed371 [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),
Daichi Hirono5c664712016-10-04 17:45:31 +0900223 mCurrentEventHandle(0),
224 mLastSendObjectInfoTransactionID(0),
Daichi Hironod95d34d2016-10-04 17:34:43 +0900225 mLastSendObjectInfoObjectHandle(0),
226 mPacketDivisionMode(FIRST_PACKET_HAS_PAYLOAD)
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400227{
Mike Lockwood42d0b792011-01-04 14:48:57 -0500228 mRequestIn1 = usb_request_new(device, ep_in);
229 mRequestIn2 = usb_request_new(device, ep_in);
230 mRequestOut = usb_request_new(device, ep_out);
231 mRequestIntr = usb_request_new(device, ep_intr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400232}
233
234MtpDevice::~MtpDevice() {
235 close();
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700236 for (size_t i = 0; i < mDeviceProperties.size(); i++)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400237 delete mDeviceProperties[i];
Mike Lockwood42d0b792011-01-04 14:48:57 -0500238 usb_request_free(mRequestIn1);
239 usb_request_free(mRequestIn2);
240 usb_request_free(mRequestOut);
241 usb_request_free(mRequestIntr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400242}
243
244void MtpDevice::initialize() {
245 openSession();
246 mDeviceInfo = getDeviceInfo();
247 if (mDeviceInfo) {
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400248 if (mDeviceInfo->mDeviceProperties) {
249 int count = mDeviceInfo->mDeviceProperties->size();
250 for (int i = 0; i < count; i++) {
251 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
252 MtpProperty* property = getDevicePropDesc(propCode);
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800253 if (property)
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400254 mDeviceProperties.push(property);
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400255 }
256 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400257 }
258}
259
260void MtpDevice::close() {
261 if (mDevice) {
262 usb_device_release_interface(mDevice, mInterface);
263 usb_device_close(mDevice);
264 mDevice = NULL;
265 }
266}
267
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800268void MtpDevice::print() {
Yunlian Jiang02690682017-01-31 17:11:19 -0800269 if (!mDeviceInfo)
270 return;
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800271
Yunlian Jiang02690682017-01-31 17:11:19 -0800272 mDeviceInfo->print();
273
274 if (mDeviceInfo->mDeviceProperties) {
275 ALOGI("***** DEVICE PROPERTIES *****\n");
276 int count = mDeviceInfo->mDeviceProperties->size();
277 for (int i = 0; i < count; i++) {
278 MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
279 MtpProperty* property = getDevicePropDesc(propCode);
280 if (property) {
281 property->print();
282 delete property;
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800283 }
284 }
285 }
286
287 if (mDeviceInfo->mPlaybackFormats) {
Steve Blockdf64d152012-01-04 20:05:49 +0000288 ALOGI("***** OBJECT PROPERTIES *****\n");
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800289 int count = mDeviceInfo->mPlaybackFormats->size();
290 for (int i = 0; i < count; i++) {
291 MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
Steve Blockdf64d152012-01-04 20:05:49 +0000292 ALOGI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800293 MtpObjectPropertyList* props = getObjectPropsSupported(format);
294 if (props) {
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700295 for (size_t j = 0; j < props->size(); j++) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800296 MtpObjectProperty prop = (*props)[j];
Mike Lockwood99e393a2010-12-07 18:53:04 -0800297 MtpProperty* property = getObjectPropDesc(prop, format);
Kenny Root31c52e72011-02-02 13:43:55 -0800298 if (property) {
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800299 property->print();
Kenny Root31c52e72011-02-02 13:43:55 -0800300 delete property;
301 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000302 ALOGE("could not fetch property: %s",
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800303 MtpDebug::getObjectPropCodeName(prop));
Kenny Root31c52e72011-02-02 13:43:55 -0800304 }
Mike Lockwood0c7c7c72010-12-07 11:24:28 -0800305 }
306 }
307 }
308 }
309}
310
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400311const char* MtpDevice::getDeviceName() {
312 if (mDevice)
313 return usb_device_get_name(mDevice);
314 else
315 return "???";
316}
317
318bool MtpDevice::openSession() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400319 Mutex::Autolock autoLock(mMutex);
320
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400321 mSessionID = 0;
322 mTransactionID = 0;
323 MtpSessionID newSession = 1;
324 mRequest.reset();
325 mRequest.setParameter(1, newSession);
326 if (!sendRequest(MTP_OPERATION_OPEN_SESSION))
327 return false;
328 MtpResponseCode ret = readResponse();
329 if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN)
330 newSession = mResponse.getParameter(1);
331 else if (ret != MTP_RESPONSE_OK)
332 return false;
333
334 mSessionID = newSession;
335 mTransactionID = 1;
336 return true;
337}
338
339bool MtpDevice::closeSession() {
340 // FIXME
341 return true;
342}
343
344MtpDeviceInfo* MtpDevice::getDeviceInfo() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400345 Mutex::Autolock autoLock(mMutex);
346
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400347 mRequest.reset();
348 if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO))
349 return NULL;
350 if (!readData())
351 return NULL;
352 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400353 if (ret == MTP_RESPONSE_OK) {
354 MtpDeviceInfo* info = new MtpDeviceInfo;
Mike Lockwoodab063842014-11-12 14:20:06 -0800355 if (info->read(mData))
356 return info;
357 else
358 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400359 }
360 return NULL;
361}
362
363MtpStorageIDList* MtpDevice::getStorageIDs() {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400364 Mutex::Autolock autoLock(mMutex);
365
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400366 mRequest.reset();
367 if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
368 return NULL;
369 if (!readData())
370 return NULL;
371 MtpResponseCode ret = readResponse();
372 if (ret == MTP_RESPONSE_OK) {
373 return mData.getAUInt32();
374 }
375 return NULL;
376}
377
378MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400379 Mutex::Autolock autoLock(mMutex);
380
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400381 mRequest.reset();
382 mRequest.setParameter(1, storageID);
383 if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
384 return NULL;
385 if (!readData())
386 return NULL;
387 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400388 if (ret == MTP_RESPONSE_OK) {
389 MtpStorageInfo* info = new MtpStorageInfo(storageID);
Mike Lockwoodab063842014-11-12 14:20:06 -0800390 if (info->read(mData))
391 return info;
392 else
393 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400394 }
395 return NULL;
396}
397
398MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
399 MtpObjectFormat format, MtpObjectHandle parent) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400400 Mutex::Autolock autoLock(mMutex);
401
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400402 mRequest.reset();
403 mRequest.setParameter(1, storageID);
404 mRequest.setParameter(2, format);
405 mRequest.setParameter(3, parent);
406 if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
407 return NULL;
408 if (!readData())
409 return NULL;
410 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400411 if (ret == MTP_RESPONSE_OK) {
412 return mData.getAUInt32();
413 }
414 return NULL;
415}
416
417MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400418 Mutex::Autolock autoLock(mMutex);
419
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400420 // FIXME - we might want to add some caching here
421
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400422 mRequest.reset();
423 mRequest.setParameter(1, handle);
424 if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
425 return NULL;
426 if (!readData())
427 return NULL;
428 MtpResponseCode ret = readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400429 if (ret == MTP_RESPONSE_OK) {
430 MtpObjectInfo* info = new MtpObjectInfo(handle);
Mike Lockwoodab063842014-11-12 14:20:06 -0800431 if (info->read(mData))
432 return info;
433 else
434 delete info;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400435 }
436 return NULL;
437}
438
Mike Lockwood3e072b32010-06-10 16:34:20 -0400439void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400440 Mutex::Autolock autoLock(mMutex);
441
Mike Lockwood3e072b32010-06-10 16:34:20 -0400442 mRequest.reset();
443 mRequest.setParameter(1, handle);
444 if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
445 MtpResponseCode ret = readResponse();
446 if (ret == MTP_RESPONSE_OK) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900447 return mData.getData(&outLength);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400448 }
449 }
450 outLength = 0;
451 return NULL;
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400452}
Mike Lockwood3e072b32010-06-10 16:34:20 -0400453
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400454MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
455 Mutex::Autolock autoLock(mMutex);
456
457 mRequest.reset();
458 MtpObjectHandle parent = info->mParent;
459 if (parent == 0)
460 parent = MTP_PARENT_ROOT;
461
462 mRequest.setParameter(1, info->mStorageID);
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900463 mRequest.setParameter(2, parent);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400464
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900465 mData.reset();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400466 mData.putUInt32(info->mStorageID);
467 mData.putUInt16(info->mFormat);
468 mData.putUInt16(info->mProtectionStatus);
469 mData.putUInt32(info->mCompressedSize);
470 mData.putUInt16(info->mThumbFormat);
471 mData.putUInt32(info->mThumbCompressedSize);
472 mData.putUInt32(info->mThumbPixWidth);
473 mData.putUInt32(info->mThumbPixHeight);
474 mData.putUInt32(info->mImagePixWidth);
475 mData.putUInt32(info->mImagePixHeight);
476 mData.putUInt32(info->mImagePixDepth);
477 mData.putUInt32(info->mParent);
478 mData.putUInt16(info->mAssociationType);
479 mData.putUInt32(info->mAssociationDesc);
480 mData.putUInt32(info->mSequenceNumber);
481 mData.putString(info->mName);
482
483 char created[100], modified[100];
484 formatDateTime(info->mDateCreated, created, sizeof(created));
485 formatDateTime(info->mDateModified, modified, sizeof(modified));
486
487 mData.putString(created);
488 mData.putString(modified);
489 if (info->mKeywords)
490 mData.putString(info->mKeywords);
491 else
492 mData.putEmptyString();
493
494 if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400495 MtpResponseCode ret = readResponse();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400496 if (ret == MTP_RESPONSE_OK) {
Daichi Hirono5c664712016-10-04 17:45:31 +0900497 mLastSendObjectInfoTransactionID = mRequest.getTransactionID();
498 mLastSendObjectInfoObjectHandle = mResponse.getParameter(3);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400499 info->mStorageID = mResponse.getParameter(1);
500 info->mParent = mResponse.getParameter(2);
501 info->mHandle = mResponse.getParameter(3);
502 return info->mHandle;
503 }
504 }
505 return (MtpObjectHandle)-1;
506}
507
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900508bool MtpDevice::sendObject(MtpObjectHandle handle, int size, int srcFD) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400509 Mutex::Autolock autoLock(mMutex);
510
Daichi Hirono5c664712016-10-04 17:45:31 +0900511 if (mLastSendObjectInfoTransactionID + 1 != mTransactionID ||
512 mLastSendObjectInfoObjectHandle != handle) {
513 ALOGE("A sendObject request must follow the sendObjectInfo request.");
514 return false;
515 }
516
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400517 mRequest.reset();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400518 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Daichi Hironod95d34d2016-10-04 17:34:43 +0900519 mData.setOperationCode(mRequest.getOperationCode());
520 mData.setTransactionID(mRequest.getTransactionID());
521 const int writeResult = mData.write(mRequestOut, mPacketDivisionMode, srcFD, size);
522 const MtpResponseCode ret = readResponse();
523 return ret == MTP_RESPONSE_OK && writeResult > 0;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400524 }
Daichi Hironod95d34d2016-10-04 17:34:43 +0900525 return false;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400526}
527
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400528bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400529 Mutex::Autolock autoLock(mMutex);
530
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400531 mRequest.reset();
532 mRequest.setParameter(1, handle);
533 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
534 MtpResponseCode ret = readResponse();
535 if (ret == MTP_RESPONSE_OK)
536 return true;
537 }
538 return false;
539}
540
541MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
542 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800543 if (info) {
544 MtpObjectHandle parent = info->mParent;
545 delete info;
546 return parent;
547 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400548 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800549 }
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400550}
551
552MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
553 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800554 if (info) {
555 MtpObjectHandle storageId = info->mStorageID;
556 delete info;
557 return storageId;
558 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400559 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800560 }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400561}
562
Mike Lockwood98693f62010-12-07 10:58:56 -0800563MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
564 Mutex::Autolock autoLock(mMutex);
565
566 mRequest.reset();
567 mRequest.setParameter(1, format);
568 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
569 return NULL;
570 if (!readData())
571 return NULL;
572 MtpResponseCode ret = readResponse();
573 if (ret == MTP_RESPONSE_OK) {
574 return mData.getAUInt16();
575 }
576 return NULL;
577
578}
579
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400580MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400581 Mutex::Autolock autoLock(mMutex);
582
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400583 mRequest.reset();
584 mRequest.setParameter(1, code);
585 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
586 return NULL;
587 if (!readData())
588 return NULL;
589 MtpResponseCode ret = readResponse();
590 if (ret == MTP_RESPONSE_OK) {
591 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800592 if (property->read(mData))
593 return property;
594 else
595 delete property;
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400596 }
597 return NULL;
598}
599
Mike Lockwood99e393a2010-12-07 18:53:04 -0800600MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood98693f62010-12-07 10:58:56 -0800601 Mutex::Autolock autoLock(mMutex);
602
603 mRequest.reset();
604 mRequest.setParameter(1, code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800605 mRequest.setParameter(2, format);
Mike Lockwood98693f62010-12-07 10:58:56 -0800606 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
607 return NULL;
608 if (!readData())
609 return NULL;
Daichi Hirono66a9abe2016-03-24 20:56:13 +0900610 const MtpResponseCode ret = readResponse();
Mike Lockwood98693f62010-12-07 10:58:56 -0800611 if (ret == MTP_RESPONSE_OK) {
612 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800613 if (property->read(mData))
614 return property;
615 else
616 delete property;
Mike Lockwood98693f62010-12-07 10:58:56 -0800617 }
618 return NULL;
619}
620
Daichi Hirono66a9abe2016-03-24 20:56:13 +0900621bool MtpDevice::getObjectPropValue(MtpObjectHandle handle, MtpProperty* property) {
622 if (property == nullptr)
623 return false;
624
625 Mutex::Autolock autoLock(mMutex);
626
627 mRequest.reset();
628 mRequest.setParameter(1, handle);
629 mRequest.setParameter(2, property->getPropertyCode());
630 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_VALUE))
631 return false;
632 if (!readData())
633 return false;
634 if (readResponse() != MTP_RESPONSE_OK)
635 return false;
636 property->setCurrentValue(mData);
637 return true;
638}
639
Mike Lockwood23f1b332010-12-30 15:38:45 -0500640bool MtpDevice::readObject(MtpObjectHandle handle,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900641 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900642 uint32_t expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900643 void* clientData) {
644 return readObjectInternal(handle, callback, &expectedLength, clientData);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500645}
646
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500647// reads the object's data and writes it to the specified file path
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500648bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Steve Blockb8a80522011-12-20 16:23:08 +0000649 ALOGD("readObject: %s", destPath);
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700650 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500651 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000652 ALOGE("open failed for %s", destPath);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400653 return false;
654 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400655
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500656 fchown(fd, getuid(), group);
657 // set permissions
658 int mask = umask(0);
659 fchmod(fd, perm);
660 umask(mask);
661
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900662 bool result = readObject(handle, fd);
663 ::close(fd);
664 return result;
665}
666
667bool MtpDevice::readObject(MtpObjectHandle handle, int fd) {
668 ALOGD("readObject: %d", fd);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900669 return readObjectInternal(handle, writeToFd, NULL /* expected size */, &fd);
670}
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900671
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900672bool MtpDevice::readObjectInternal(MtpObjectHandle handle,
673 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900674 const uint32_t* expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900675 void* clientData) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500676 Mutex::Autolock autoLock(mMutex);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400677
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500678 mRequest.reset();
679 mRequest.setParameter(1, handle);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900680 if (!sendRequest(MTP_OPERATION_GET_OBJECT)) {
681 ALOGE("Failed to send a read request.");
682 return false;
683 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500684
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900685 return readData(callback, expectedLength, nullptr, clientData);
686}
687
688bool MtpDevice::readData(ReadObjectCallback callback,
689 const uint32_t* expectedLength,
690 uint32_t* writtenSize,
691 void* clientData) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900692 if (!mData.readDataHeader(mRequestIn1)) {
693 ALOGE("Failed to read header.");
694 return false;
695 }
696
Daichi Hironod95d34d2016-10-04 17:34:43 +0900697 // If object size 0 byte, the remote device may reply a response packet without sending any data
698 // packets.
Daichi Hironoa04e6fa2015-12-24 14:20:44 +0900699 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
700 mResponse.copyFrom(mData);
701 return mResponse.getResponseCode() == MTP_RESPONSE_OK;
702 }
703
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900704 const uint32_t fullLength = mData.getContainerLength();
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900705 if (fullLength < MTP_CONTAINER_HEADER_SIZE) {
706 ALOGE("fullLength is too short: %d", fullLength);
707 return false;
708 }
709 const uint32_t length = fullLength - MTP_CONTAINER_HEADER_SIZE;
710 if (expectedLength && length != *expectedLength) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900711 ALOGE("readObject error length: %d", fullLength);
712 return false;
713 }
714
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900715 uint32_t offset = 0;
716 bool writingError = false;
717
718 {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500719 int initialDataLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900720 void* const initialData = mData.getData(&initialDataLength);
Daichi Hironod95d34d2016-10-04 17:34:43 +0900721 if (fullLength > MTP_CONTAINER_HEADER_SIZE && initialDataLength == 0) {
722 // According to the MTP spec, the responder (MTP device) can choose two ways of sending
723 // data. a) The first packet contains the head and as much of the payload as possible
724 // b) The first packet contains only the header. The initiator (MTP host) needs
725 // to remember which way the responder used, and send upcoming data in the same way.
726 ALOGD("Found short packet that contains only a header.");
727 mPacketDivisionMode = FIRST_PACKET_ONLY_HEADER;
728 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500729 if (initialData) {
730 if (initialDataLength > 0) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900731 if (!callback(initialData, offset, initialDataLength, clientData)) {
Daichi Hirono81ca5ad2015-08-18 21:13:40 +0900732 ALOGE("Failed to write initial data.");
733 writingError = true;
Kenny Root31c52e72011-02-02 13:43:55 -0800734 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900735 offset += initialDataLength;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500736 }
737 free(initialData);
738 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900739 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500740
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900741 // USB reads greater than 16K don't work.
742 char buffer1[MTP_BUFFER_SIZE], buffer2[MTP_BUFFER_SIZE];
743 mRequestIn1->buffer = buffer1;
744 mRequestIn2->buffer = buffer2;
745 struct usb_request* req = NULL;
746
747 while (offset < length) {
748 // Wait for previous read to complete.
Mike Lockwood42d0b792011-01-04 14:48:57 -0500749 void* writeBuffer = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500750 int writeLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900751 if (req) {
752 const int read = mData.readDataWait(mDevice);
753 if (read < 0) {
754 ALOGE("readDataWait failed.");
755 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500756 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900757 writeBuffer = req->buffer;
758 writeLength = read;
759 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500760
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900761 // Request to read next chunk.
762 const uint32_t nextOffset = offset + writeLength;
763 if (nextOffset < length) {
764 // Queue up a read request.
765 const size_t remaining = length - nextOffset;
766 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900767 req->buffer_length = remaining > MTP_BUFFER_SIZE ?
768 static_cast<size_t>(MTP_BUFFER_SIZE) : remaining;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900769 if (mData.readDataAsync(req) != 0) {
770 ALOGE("readDataAsync failed");
771 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500772 }
773 }
774
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900775 // Write previous buffer.
776 if (writeBuffer && !writingError) {
777 if (!callback(writeBuffer, offset, writeLength, clientData)) {
778 ALOGE("write failed");
779 writingError = true;
780 }
781 }
782 offset = nextOffset;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400783 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500784
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900785 if (writtenSize) {
786 *writtenSize = length;
787 }
788
789 return readResponse() == MTP_RESPONSE_OK;
790}
791
792bool MtpDevice::readPartialObject(MtpObjectHandle handle,
793 uint32_t offset,
794 uint32_t size,
795 uint32_t *writtenSize,
796 ReadObjectCallback callback,
797 void* clientData) {
798 Mutex::Autolock autoLock(mMutex);
799
800 mRequest.reset();
801 mRequest.setParameter(1, handle);
802 mRequest.setParameter(2, offset);
803 mRequest.setParameter(3, size);
804 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT)) {
805 ALOGE("Failed to send a read request.");
806 return false;
807 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900808 // The expected size is null because it requires the exact number of bytes to read though
809 // MTP_OPERATION_GET_PARTIAL_OBJECT allows devices to return shorter length of bytes than
810 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900811 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
812}
813
814bool MtpDevice::readPartialObject64(MtpObjectHandle handle,
815 uint64_t offset,
816 uint32_t size,
817 uint32_t *writtenSize,
818 ReadObjectCallback callback,
819 void* clientData) {
820 Mutex::Autolock autoLock(mMutex);
821
822 mRequest.reset();
823 mRequest.setParameter(1, handle);
824 mRequest.setParameter(2, 0xffffffff & offset);
825 mRequest.setParameter(3, 0xffffffff & (offset >> 32));
826 mRequest.setParameter(4, size);
827 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT_64)) {
828 ALOGE("Failed to send a read request.");
829 return false;
830 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900831 // The expected size is null because it requires the exact number of bytes to read though
832 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 allows devices to return shorter length of bytes than
833 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900834 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400835}
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400836
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400837bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block3856b092011-10-20 11:56:00 +0100838 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodf7454622010-12-09 18:34:18 -0800839 mReceivedResponse = false;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400840 mRequest.setOperationCode(operation);
841 if (mTransactionID > 0)
842 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500843 int ret = mRequest.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400844 mRequest.dump();
845 return (ret > 0);
846}
847
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400848bool MtpDevice::sendData() {
Steve Block3856b092011-10-20 11:56:00 +0100849 ALOGV("sendData\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400850 mData.setOperationCode(mRequest.getOperationCode());
851 mData.setTransactionID(mRequest.getTransactionID());
Daichi Hironod95d34d2016-10-04 17:34:43 +0900852 int ret = mData.write(mRequestOut, mPacketDivisionMode);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400853 mData.dump();
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900854 return (ret >= 0);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400855}
856
857bool MtpDevice::readData() {
858 mData.reset();
Mike Lockwood42d0b792011-01-04 14:48:57 -0500859 int ret = mData.read(mRequestIn1);
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("readData returned %d\n", ret);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400861 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodf7454622010-12-09 18:34:18 -0800862 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
Steve Blockb8a80522011-12-20 16:23:08 +0000863 ALOGD("got response packet instead of data packet");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800864 // we got a response packet rather than data
865 // copy it to mResponse
866 mResponse.copyFrom(mData);
867 mReceivedResponse = true;
868 return false;
869 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400870 mData.dump();
871 return true;
872 }
873 else {
Steve Block3856b092011-10-20 11:56:00 +0100874 ALOGV("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400875 return false;
876 }
877}
878
879MtpResponseCode MtpDevice::readResponse() {
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("readResponse\n");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800881 if (mReceivedResponse) {
882 mReceivedResponse = false;
883 return mResponse.getResponseCode();
884 }
Mike Lockwood42d0b792011-01-04 14:48:57 -0500885 int ret = mResponse.read(mRequestIn1);
Mike Lockwood3d744572011-03-14 10:33:22 -0400886 // handle zero length packets, which might occur if the data transfer
887 // ends on a packet boundary
888 if (ret == 0)
889 ret = mResponse.read(mRequestIn1);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400890 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
891 mResponse.dump();
892 return mResponse.getResponseCode();
Mike Lockwoodf7454622010-12-09 18:34:18 -0800893 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000894 ALOGD("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400895 return -1;
896 }
897}
898
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900899int MtpDevice::submitEventRequest() {
900 if (mEventMutex.tryLock()) {
901 // An event is being reaped on another thread.
902 return -1;
903 }
904 if (mProcessingEvent) {
905 // An event request was submitted, but no reapEventRequest called so far.
906 return -1;
907 }
908 Mutex::Autolock autoLock(mEventMutexForInterrupt);
909 mEventPacket.sendRequest(mRequestIntr);
910 const int currentHandle = ++mCurrentEventHandle;
911 mProcessingEvent = true;
912 mEventMutex.unlock();
913 return currentHandle;
914}
915
Daichi Hirono59a90602016-01-11 13:36:57 +0900916int MtpDevice::reapEventRequest(int handle, uint32_t (*parameters)[3]) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900917 Mutex::Autolock autoLock(mEventMutex);
Daichi Hirono59a90602016-01-11 13:36:57 +0900918 if (!mProcessingEvent || mCurrentEventHandle != handle || !parameters) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900919 return -1;
920 }
921 mProcessingEvent = false;
922 const int readSize = mEventPacket.readResponse(mRequestIntr->dev);
923 const int result = mEventPacket.getEventCode();
Daichi Hirono59a90602016-01-11 13:36:57 +0900924 // MTP event has three parameters.
925 (*parameters)[0] = mEventPacket.getParameter(1);
926 (*parameters)[1] = mEventPacket.getParameter(2);
927 (*parameters)[2] = mEventPacket.getParameter(3);
928 return readSize != 0 ? result : 0;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900929}
930
931void MtpDevice::discardEventRequest(int handle) {
932 Mutex::Autolock autoLock(mEventMutexForInterrupt);
933 if (mCurrentEventHandle != handle) {
934 return;
935 }
936 usb_request_cancel(mRequestIntr);
937}
938
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400939} // namespace android