blob: bd89a51d436e9bb5e453bb4b1dfeabc1de2f282a [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);
Daichi Hironob3be0062016-02-25 12:42:58 +0900508 bool error = false;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400509 if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400510 // send data header
511 writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);
512
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900513 // USB writes greater than 16K don't work
514 char buffer[MTP_BUFFER_SIZE];
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400515 while (remaining > 0) {
516 int count = read(srcFD, buffer, sizeof(buffer));
517 if (count > 0) {
Daichi Hironob3be0062016-02-25 12:42:58 +0900518 if (mData.write(mRequestOut, buffer, count) < 0) {
519 error = true;
520 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400521 // FIXME check error
522 remaining -= count;
523 } else {
524 break;
525 }
526 }
527 }
528 MtpResponseCode ret = readResponse();
Daichi Hironob3be0062016-02-25 12:42:58 +0900529 return (remaining == 0 && ret == MTP_RESPONSE_OK && !error);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400530}
531
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400532bool MtpDevice::deleteObject(MtpObjectHandle handle) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400533 Mutex::Autolock autoLock(mMutex);
534
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400535 mRequest.reset();
536 mRequest.setParameter(1, handle);
537 if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
538 MtpResponseCode ret = readResponse();
539 if (ret == MTP_RESPONSE_OK)
540 return true;
541 }
542 return false;
543}
544
545MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
546 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800547 if (info) {
548 MtpObjectHandle parent = info->mParent;
549 delete info;
550 return parent;
551 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400552 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800553 }
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400554}
555
556MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
557 MtpObjectInfo* info = getObjectInfo(handle);
Kenny Root31c52e72011-02-02 13:43:55 -0800558 if (info) {
559 MtpObjectHandle storageId = info->mStorageID;
560 delete info;
561 return storageId;
562 } else {
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400563 return -1;
Kenny Root31c52e72011-02-02 13:43:55 -0800564 }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400565}
566
Mike Lockwood98693f62010-12-07 10:58:56 -0800567MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
568 Mutex::Autolock autoLock(mMutex);
569
570 mRequest.reset();
571 mRequest.setParameter(1, format);
572 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
573 return NULL;
574 if (!readData())
575 return NULL;
576 MtpResponseCode ret = readResponse();
577 if (ret == MTP_RESPONSE_OK) {
578 return mData.getAUInt16();
579 }
580 return NULL;
581
582}
583
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400584MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400585 Mutex::Autolock autoLock(mMutex);
586
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400587 mRequest.reset();
588 mRequest.setParameter(1, code);
589 if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
590 return NULL;
591 if (!readData())
592 return NULL;
593 MtpResponseCode ret = readResponse();
594 if (ret == MTP_RESPONSE_OK) {
595 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800596 if (property->read(mData))
597 return property;
598 else
599 delete property;
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400600 }
601 return NULL;
602}
603
Mike Lockwood99e393a2010-12-07 18:53:04 -0800604MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
Mike Lockwood98693f62010-12-07 10:58:56 -0800605 Mutex::Autolock autoLock(mMutex);
606
607 mRequest.reset();
608 mRequest.setParameter(1, code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800609 mRequest.setParameter(2, format);
Mike Lockwood98693f62010-12-07 10:58:56 -0800610 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
611 return NULL;
612 if (!readData())
613 return NULL;
Daichi Hirono66a9abe2016-03-24 20:56:13 +0900614 const MtpResponseCode ret = readResponse();
Mike Lockwood98693f62010-12-07 10:58:56 -0800615 if (ret == MTP_RESPONSE_OK) {
616 MtpProperty* property = new MtpProperty;
Mike Lockwoodab063842014-11-12 14:20:06 -0800617 if (property->read(mData))
618 return property;
619 else
620 delete property;
Mike Lockwood98693f62010-12-07 10:58:56 -0800621 }
622 return NULL;
623}
624
Daichi Hirono66a9abe2016-03-24 20:56:13 +0900625bool MtpDevice::getObjectPropValue(MtpObjectHandle handle, MtpProperty* property) {
626 if (property == nullptr)
627 return false;
628
629 Mutex::Autolock autoLock(mMutex);
630
631 mRequest.reset();
632 mRequest.setParameter(1, handle);
633 mRequest.setParameter(2, property->getPropertyCode());
634 if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_VALUE))
635 return false;
636 if (!readData())
637 return false;
638 if (readResponse() != MTP_RESPONSE_OK)
639 return false;
640 property->setCurrentValue(mData);
641 return true;
642}
643
Mike Lockwood23f1b332010-12-30 15:38:45 -0500644bool MtpDevice::readObject(MtpObjectHandle handle,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900645 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900646 uint32_t expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900647 void* clientData) {
648 return readObjectInternal(handle, callback, &expectedLength, clientData);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500649}
650
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500651// reads the object's data and writes it to the specified file path
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500652bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
Steve Blockb8a80522011-12-20 16:23:08 +0000653 ALOGD("readObject: %s", destPath);
Nick Kralevichaf8e8aa2012-06-26 13:32:23 -0700654 int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500655 if (fd < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000656 ALOGE("open failed for %s", destPath);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400657 return false;
658 }
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400659
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500660 fchown(fd, getuid(), group);
661 // set permissions
662 int mask = umask(0);
663 fchmod(fd, perm);
664 umask(mask);
665
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900666 bool result = readObject(handle, fd);
667 ::close(fd);
668 return result;
669}
670
671bool MtpDevice::readObject(MtpObjectHandle handle, int fd) {
672 ALOGD("readObject: %d", fd);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900673 return readObjectInternal(handle, writeToFd, NULL /* expected size */, &fd);
674}
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900675
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900676bool MtpDevice::readObjectInternal(MtpObjectHandle handle,
677 ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900678 const uint32_t* expectedLength,
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900679 void* clientData) {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500680 Mutex::Autolock autoLock(mMutex);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400681
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500682 mRequest.reset();
683 mRequest.setParameter(1, handle);
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900684 if (!sendRequest(MTP_OPERATION_GET_OBJECT)) {
685 ALOGE("Failed to send a read request.");
686 return false;
687 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500688
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900689 return readData(callback, expectedLength, nullptr, clientData);
690}
691
692bool MtpDevice::readData(ReadObjectCallback callback,
693 const uint32_t* expectedLength,
694 uint32_t* writtenSize,
695 void* clientData) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900696 if (!mData.readDataHeader(mRequestIn1)) {
697 ALOGE("Failed to read header.");
698 return false;
699 }
700
Daichi Hironoa04e6fa2015-12-24 14:20:44 +0900701 // If object size 0 byte, the remote device can reply response packet
702 // without sending any data packets.
703 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
704 mResponse.copyFrom(mData);
705 return mResponse.getResponseCode() == MTP_RESPONSE_OK;
706 }
707
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900708 const uint32_t fullLength = mData.getContainerLength();
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900709 if (fullLength < MTP_CONTAINER_HEADER_SIZE) {
710 ALOGE("fullLength is too short: %d", fullLength);
711 return false;
712 }
713 const uint32_t length = fullLength - MTP_CONTAINER_HEADER_SIZE;
714 if (expectedLength && length != *expectedLength) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900715 ALOGE("readObject error length: %d", fullLength);
716 return false;
717 }
718
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900719 uint32_t offset = 0;
720 bool writingError = false;
721
722 {
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500723 int initialDataLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900724 void* const initialData = mData.getData(&initialDataLength);
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500725 if (initialData) {
726 if (initialDataLength > 0) {
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900727 if (!callback(initialData, offset, initialDataLength, clientData)) {
Daichi Hirono81ca5ad2015-08-18 21:13:40 +0900728 ALOGE("Failed to write initial data.");
729 writingError = true;
Kenny Root31c52e72011-02-02 13:43:55 -0800730 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900731 offset += initialDataLength;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500732 }
733 free(initialData);
734 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900735 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500736
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900737 // USB reads greater than 16K don't work.
738 char buffer1[MTP_BUFFER_SIZE], buffer2[MTP_BUFFER_SIZE];
739 mRequestIn1->buffer = buffer1;
740 mRequestIn2->buffer = buffer2;
741 struct usb_request* req = NULL;
742
743 while (offset < length) {
744 // Wait for previous read to complete.
Mike Lockwood42d0b792011-01-04 14:48:57 -0500745 void* writeBuffer = NULL;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500746 int writeLength = 0;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900747 if (req) {
748 const int read = mData.readDataWait(mDevice);
749 if (read < 0) {
750 ALOGE("readDataWait failed.");
751 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500752 }
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900753 writeBuffer = req->buffer;
754 writeLength = read;
755 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500756
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900757 // Request to read next chunk.
758 const uint32_t nextOffset = offset + writeLength;
759 if (nextOffset < length) {
760 // Queue up a read request.
761 const size_t remaining = length - nextOffset;
762 req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900763 req->buffer_length = remaining > MTP_BUFFER_SIZE ?
764 static_cast<size_t>(MTP_BUFFER_SIZE) : remaining;
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900765 if (mData.readDataAsync(req) != 0) {
766 ALOGE("readDataAsync failed");
767 return false;
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500768 }
769 }
770
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900771 // Write previous buffer.
772 if (writeBuffer && !writingError) {
773 if (!callback(writeBuffer, offset, writeLength, clientData)) {
774 ALOGE("write failed");
775 writingError = true;
776 }
777 }
778 offset = nextOffset;
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400779 }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500780
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900781 if (writtenSize) {
782 *writtenSize = length;
783 }
784
785 return readResponse() == MTP_RESPONSE_OK;
786}
787
788bool MtpDevice::readPartialObject(MtpObjectHandle handle,
789 uint32_t offset,
790 uint32_t size,
791 uint32_t *writtenSize,
792 ReadObjectCallback callback,
793 void* clientData) {
794 Mutex::Autolock autoLock(mMutex);
795
796 mRequest.reset();
797 mRequest.setParameter(1, handle);
798 mRequest.setParameter(2, offset);
799 mRequest.setParameter(3, size);
800 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT)) {
801 ALOGE("Failed to send a read request.");
802 return false;
803 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900804 // The expected size is null because it requires the exact number of bytes to read though
805 // MTP_OPERATION_GET_PARTIAL_OBJECT allows devices to return shorter length of bytes than
806 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900807 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
808}
809
810bool MtpDevice::readPartialObject64(MtpObjectHandle handle,
811 uint64_t offset,
812 uint32_t size,
813 uint32_t *writtenSize,
814 ReadObjectCallback callback,
815 void* clientData) {
816 Mutex::Autolock autoLock(mMutex);
817
818 mRequest.reset();
819 mRequest.setParameter(1, handle);
820 mRequest.setParameter(2, 0xffffffff & offset);
821 mRequest.setParameter(3, 0xffffffff & (offset >> 32));
822 mRequest.setParameter(4, size);
823 if (!sendRequest(MTP_OPERATION_GET_PARTIAL_OBJECT_64)) {
824 ALOGE("Failed to send a read request.");
825 return false;
826 }
Daichi Hirono610d4a12016-01-28 14:26:36 +0900827 // The expected size is null because it requires the exact number of bytes to read though
828 // MTP_OPERATION_GET_PARTIAL_OBJECT_64 allows devices to return shorter length of bytes than
829 // requested. Destination's buffer length should be checked in |callback|.
Daichi Hirono326edd92016-01-22 15:34:46 +0900830 return readData(callback, nullptr /* expected size */, writtenSize, clientData);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400831}
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400832
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400833bool MtpDevice::sendRequest(MtpOperationCode operation) {
Steve Block3856b092011-10-20 11:56:00 +0100834 ALOGV("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
Mike Lockwoodf7454622010-12-09 18:34:18 -0800835 mReceivedResponse = false;
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400836 mRequest.setOperationCode(operation);
837 if (mTransactionID > 0)
838 mRequest.setTransactionID(mTransactionID++);
Mike Lockwood42d0b792011-01-04 14:48:57 -0500839 int ret = mRequest.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400840 mRequest.dump();
841 return (ret > 0);
842}
843
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400844bool MtpDevice::sendData() {
Steve Block3856b092011-10-20 11:56:00 +0100845 ALOGV("sendData\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400846 mData.setOperationCode(mRequest.getOperationCode());
847 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500848 int ret = mData.write(mRequestOut);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400849 mData.dump();
Tomasz Mikolajewski64c948b2015-08-13 15:31:02 +0900850 return (ret >= 0);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400851}
852
853bool MtpDevice::readData() {
854 mData.reset();
Mike Lockwood42d0b792011-01-04 14:48:57 -0500855 int ret = mData.read(mRequestIn1);
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("readData returned %d\n", ret);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400857 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
Mike Lockwoodf7454622010-12-09 18:34:18 -0800858 if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
Steve Blockb8a80522011-12-20 16:23:08 +0000859 ALOGD("got response packet instead of data packet");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800860 // we got a response packet rather than data
861 // copy it to mResponse
862 mResponse.copyFrom(mData);
863 mReceivedResponse = true;
864 return false;
865 }
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400866 mData.dump();
867 return true;
868 }
869 else {
Steve Block3856b092011-10-20 11:56:00 +0100870 ALOGV("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400871 return false;
872 }
873}
874
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400875bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
876 mData.setOperationCode(operation);
877 mData.setTransactionID(mRequest.getTransactionID());
Mike Lockwood42d0b792011-01-04 14:48:57 -0500878 return (!mData.writeDataHeader(mRequestOut, dataLength));
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400879}
880
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400881MtpResponseCode MtpDevice::readResponse() {
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("readResponse\n");
Mike Lockwoodf7454622010-12-09 18:34:18 -0800883 if (mReceivedResponse) {
884 mReceivedResponse = false;
885 return mResponse.getResponseCode();
886 }
Mike Lockwood42d0b792011-01-04 14:48:57 -0500887 int ret = mResponse.read(mRequestIn1);
Mike Lockwood3d744572011-03-14 10:33:22 -0400888 // handle zero length packets, which might occur if the data transfer
889 // ends on a packet boundary
890 if (ret == 0)
891 ret = mResponse.read(mRequestIn1);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400892 if (ret >= MTP_CONTAINER_HEADER_SIZE) {
893 mResponse.dump();
894 return mResponse.getResponseCode();
Mike Lockwoodf7454622010-12-09 18:34:18 -0800895 } else {
Steve Blockb8a80522011-12-20 16:23:08 +0000896 ALOGD("readResponse failed\n");
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400897 return -1;
898 }
899}
900
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900901int MtpDevice::submitEventRequest() {
902 if (mEventMutex.tryLock()) {
903 // An event is being reaped on another thread.
904 return -1;
905 }
906 if (mProcessingEvent) {
907 // An event request was submitted, but no reapEventRequest called so far.
908 return -1;
909 }
910 Mutex::Autolock autoLock(mEventMutexForInterrupt);
911 mEventPacket.sendRequest(mRequestIntr);
912 const int currentHandle = ++mCurrentEventHandle;
913 mProcessingEvent = true;
914 mEventMutex.unlock();
915 return currentHandle;
916}
917
Daichi Hirono59a90602016-01-11 13:36:57 +0900918int MtpDevice::reapEventRequest(int handle, uint32_t (*parameters)[3]) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900919 Mutex::Autolock autoLock(mEventMutex);
Daichi Hirono59a90602016-01-11 13:36:57 +0900920 if (!mProcessingEvent || mCurrentEventHandle != handle || !parameters) {
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900921 return -1;
922 }
923 mProcessingEvent = false;
924 const int readSize = mEventPacket.readResponse(mRequestIntr->dev);
925 const int result = mEventPacket.getEventCode();
Daichi Hirono59a90602016-01-11 13:36:57 +0900926 // MTP event has three parameters.
927 (*parameters)[0] = mEventPacket.getParameter(1);
928 (*parameters)[1] = mEventPacket.getParameter(2);
929 (*parameters)[2] = mEventPacket.getParameter(3);
930 return readSize != 0 ? result : 0;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900931}
932
933void MtpDevice::discardEventRequest(int handle) {
934 Mutex::Autolock autoLock(mEventMutexForInterrupt);
935 if (mCurrentEventHandle != handle) {
936 return;
937 }
938 usb_request_cancel(mRequestIntr);
939}
940
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400941} // namespace android