summaryrefslogtreecommitdiffstats
path: root/mtp/legacy/MtpDevice.cpp
blob: 089fc22728beabf71cff35edb2cd43ef46beea34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++
 */

#include "MtpDevice.h"
#include "MtpDebug.h"
#include "MtpDeviceInfo.h"
#include "MtpObjectInfo.h"
#include "MtpProperty.h"
#include "MtpStorageInfo.h"
#include "MtpStringBuffer.h"
#include "MtpUtils.h"
#include "MtpDataPacket.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <endian.h>

#include <usbhost/usbhost.h>


#if 0
static bool isMtpDevice(uint16_t vendor, uint16_t product) {
	// Sandisk Sansa Fuze
	if (vendor == 0x0781 && product == 0x74c2)
		return true;
	// Samsung YP-Z5
	if (vendor == 0x04e8 && product == 0x503c)
		return true;
	return false;
}
#endif

#ifdef HAS_USBHOST_TIMEOUT
static const int USB_CONTROL_TRANSFER_TIMEOUT_MS = 200;
#endif

MtpDevice* MtpDevice::open(const char* deviceName, int fd) {
	struct usb_device *device = usb_device_new(deviceName, fd);
	if (!device) {
		MTPE("usb_device_new failed for %s", deviceName);
		return NULL;
	}

	struct usb_descriptor_header* desc;
	struct usb_descriptor_iter iter;

	usb_descriptor_iter_init(device, &iter);

	while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
		if (desc->bDescriptorType == USB_DT_INTERFACE) {
			struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;

			if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
				interface->bInterfaceSubClass == 1 && // Still Image Capture
				interface->bInterfaceProtocol == 1)	 // Picture Transfer Protocol (PIMA 15470)
			{
#ifdef HAS_USBHOST_TIMEOUT
				char* manufacturerName = usb_device_get_manufacturer_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
				char* productName = usb_device_get_product_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
#else
				char* manufacturerName = usb_device_get_manufacturer_name(device);
				char* productName = usb_device_get_product_name(device);
#endif
				MTPD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
				free(manufacturerName);
				free(productName);
			} else if (interface->bInterfaceClass == 0xFF &&
					interface->bInterfaceSubClass == 0xFF &&
					interface->bInterfaceProtocol == 0) {
#ifdef HAS_USBHOST_TIMEOUT
				char* interfaceName = usb_device_get_string(device, interface->iInterface, USB_CONTROL_TRANSFER_TIMEOUT_MS);
#else
				char* interfaceName = usb_device_get_string(device, interface->iInterface);
#endif
				if (!interfaceName) {
					continue;
				} else if (strcmp(interfaceName, "MTP")) {
					free(interfaceName);
					continue;
				}
				free(interfaceName);

				// Looks like an android style MTP device
#ifdef HAS_USBHOST_TIMEOUT
				char* manufacturerName = usb_device_get_manufacturer_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
				char* productName = usb_device_get_product_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
#else
				char* manufacturerName = usb_device_get_manufacturer_name(device);
				char* productName = usb_device_get_product_name(device);
#endif
				MTPI("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
				free(manufacturerName);
				free(productName);
			}
#if 0
			 else {
				// look for special cased devices based on vendor/product ID
				// we are doing this mainly for testing purposes
				uint16_t vendor = usb_device_get_vendor_id(device);
				uint16_t product = usb_device_get_product_id(device);
				if (!isMtpDevice(vendor, product)) {
					// not an MTP or PTP device
					continue;
				}
				// request MTP OS string and descriptor
				// some music players need to see this before entering MTP mode.
				char buffer[256];
				memset(buffer, 0, sizeof(buffer));
				int ret = usb_device_control_transfer(device,
						USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_STANDARD,
						USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | 0xEE,
						0, buffer, sizeof(buffer), 0);
				MTPE("usb_device_control_transfer returned %d errno: %d\n", ret, errno);
				if (ret > 0) {
					MTPI("got MTP string %s\n", buffer);
					ret = usb_device_control_transfer(device,
							USB_DIR_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, 1,
							0, 4, buffer, sizeof(buffer), 0);
					MTPI("OS descriptor got %d\n", ret);
				} else {
					MTPI("no MTP string\n");
				}
			}
#endif
			// if we got here, then we have a likely MTP or PTP device

			// interface should be followed by three endpoints
			struct usb_endpoint_descriptor *ep;
			struct usb_endpoint_descriptor *ep_in_desc = NULL;
			struct usb_endpoint_descriptor *ep_out_desc = NULL;
			struct usb_endpoint_descriptor *ep_intr_desc = NULL;
			for (int i = 0; i < 3; i++) {
				ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
				if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
					MTPE("endpoints not found\n");
					usb_device_close(device);
					return NULL;
				}
				if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
					if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
						ep_in_desc = ep;
					else
						ep_out_desc = ep;
				} else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
					ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
					ep_intr_desc = ep;
				}
			}
			if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
				MTPE("endpoints not found\n");
				usb_device_close(device);
				return NULL;
			}

			if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
				MTPE("usb_device_claim_interface failed errno: %d\n", errno);
				usb_device_close(device);
				return NULL;
			}

			MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
						ep_in_desc, ep_out_desc, ep_intr_desc);
			mtpDevice->initialize();
			return mtpDevice;
		}
	}

	usb_device_close(device);
	MTPE("device not found");
	return NULL;
}

MtpDevice::MtpDevice(struct usb_device* device, int interface,
			const struct usb_endpoint_descriptor *ep_in,
			const struct usb_endpoint_descriptor *ep_out,
			const struct usb_endpoint_descriptor *ep_intr)
	:   mDevice(device),
		mInterface(interface),
		mRequestIn1(NULL),
		mRequestIn2(NULL),
		mRequestOut(NULL),
		mRequestIntr(NULL),
		mDeviceInfo(NULL),
		mSessionID(0),
		mTransactionID(0),
		mReceivedResponse(false)
{
	mRequestIn1 = usb_request_new(device, ep_in);
	mRequestIn2 = usb_request_new(device, ep_in);
	mRequestOut = usb_request_new(device, ep_out);
	mRequestIntr = usb_request_new(device, ep_intr);
}

MtpDevice::~MtpDevice() {
	close();
	for (size_t i = 0; i < mDeviceProperties.size(); i++)
		delete mDeviceProperties[i];
	usb_request_free(mRequestIn1);
	usb_request_free(mRequestIn2);
	usb_request_free(mRequestOut);
	usb_request_free(mRequestIntr);
}

void MtpDevice::initialize() {
	openSession();
	mDeviceInfo = getDeviceInfo();
	if (mDeviceInfo) {
		if (mDeviceInfo->mDeviceProperties) {
			int count = mDeviceInfo->mDeviceProperties->size();
			for (int i = 0; i < count; i++) {
				MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
				MtpProperty* property = getDevicePropDesc(propCode);
				if (property)
					mDeviceProperties.push(property);
			}
		}
	}
}

void MtpDevice::close() {
	if (mDevice) {
		usb_device_release_interface(mDevice, mInterface);
		usb_device_close(mDevice);
		mDevice = NULL;
	}
}

void MtpDevice::print() {
	if (mDeviceInfo) {
		mDeviceInfo->print();

		if (mDeviceInfo->mDeviceProperties) {
			MTPI("***** DEVICE PROPERTIES *****\n");
			int count = mDeviceInfo->mDeviceProperties->size();
			for (int i = 0; i < count; i++) {
				MtpDeviceProperty propCode = (*mDeviceInfo->mDeviceProperties)[i];
				MtpProperty* property = getDevicePropDesc(propCode);
				if (property) {
					property->print();
					delete property;
				}
			}
		}
	}

	if (mDeviceInfo->mPlaybackFormats) {
			MTPI("***** OBJECT PROPERTIES *****\n");
		int count = mDeviceInfo->mPlaybackFormats->size();
		for (int i = 0; i < count; i++) {
			MtpObjectFormat format = (*mDeviceInfo->mPlaybackFormats)[i];
			MTPI("*** FORMAT: %s\n", MtpDebug::getFormatCodeName(format));
			MtpObjectPropertyList* props = getObjectPropsSupported(format);
			if (props) {
				for (size_t j = 0; j < props->size(); j++) {
					MtpObjectProperty prop = (*props)[j];
					MtpProperty* property = getObjectPropDesc(prop, format);
					if (property) {
						property->print();
						delete property;
					} else {
						MTPI("could not fetch property: %s",
								MtpDebug::getObjectPropCodeName(prop));
					}
				}
			}
		}
	}
}

const char* MtpDevice::getDeviceName() {
	if (mDevice)
		return usb_device_get_name(mDevice);
	else
		return "???";
}

bool MtpDevice::openSession() {
	android::Mutex::Autolock autoLock(mMutex);

	mSessionID = 0;
	mTransactionID = 0;
	MtpSessionID newSession = 1;
	mRequest.reset();
	mRequest.setParameter(1, newSession);
	if (!sendRequest(MTP_OPERATION_OPEN_SESSION))
		return false;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_SESSION_ALREADY_OPEN)
		newSession = mResponse.getParameter(1);
	else if (ret != MTP_RESPONSE_OK)
		return false;

	mSessionID = newSession;
	mTransactionID = 1;
	return true;
}

bool MtpDevice::closeSession() {
	// FIXME
	return true;
}

MtpDeviceInfo* MtpDevice::getDeviceInfo() {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	if (!sendRequest(MTP_OPERATION_GET_DEVICE_INFO))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		MtpDeviceInfo* info = new MtpDeviceInfo;
		info->read(mData);
		return info;
	}
	return NULL;
}

MtpStorageIDList* MtpDevice::getStorageIDs() {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	if (!sendRequest(MTP_OPERATION_GET_STORAGE_IDS))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		return mData.getAUInt32();
	}
	return NULL;
}

MtpStorageInfo* MtpDevice::getStorageInfo(MtpStorageID storageID) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, storageID);
	if (!sendRequest(MTP_OPERATION_GET_STORAGE_INFO))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		MtpStorageInfo* info = new MtpStorageInfo(storageID);
		info->read(mData);
		return info;
	}
	return NULL;
}

MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID,
			MtpObjectFormat format, MtpObjectHandle parent) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, storageID);
	mRequest.setParameter(2, format);
	mRequest.setParameter(3, parent);
	if (!sendRequest(MTP_OPERATION_GET_OBJECT_HANDLES))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		return mData.getAUInt32();
	}
	return NULL;
}

MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) {
	android::Mutex::Autolock autoLock(mMutex);

	// FIXME - we might want to add some caching here

	mRequest.reset();
	mRequest.setParameter(1, handle);
	if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		MtpObjectInfo* info = new MtpObjectInfo(handle);
		info->read(mData);
		return info;
	}
	return NULL;
}

void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, handle);
	if (sendRequest(MTP_OPERATION_GET_THUMB) && readData()) {
		MtpResponseCode ret = readResponse();
		if (ret == MTP_RESPONSE_OK) {
			return mData.getData(outLength);
		}
	}
	outLength = 0;
	return NULL;
}

MtpObjectHandle MtpDevice::sendObjectInfo(MtpObjectInfo* info) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	MtpObjectHandle parent = info->mParent;
	if (parent == 0)
		parent = MTP_PARENT_ROOT;

	mRequest.setParameter(1, info->mStorageID);
	mRequest.setParameter(2, info->mParent);

	mData.putUInt32(info->mStorageID);
	mData.putUInt16(info->mFormat);
	mData.putUInt16(info->mProtectionStatus);
	mData.putUInt32(info->mCompressedSize);
	mData.putUInt16(info->mThumbFormat);
	mData.putUInt32(info->mThumbCompressedSize);
	mData.putUInt32(info->mThumbPixWidth);
	mData.putUInt32(info->mThumbPixHeight);
	mData.putUInt32(info->mImagePixWidth);
	mData.putUInt32(info->mImagePixHeight);
	mData.putUInt32(info->mImagePixDepth);
	mData.putUInt32(info->mParent);
	mData.putUInt16(info->mAssociationType);
	mData.putUInt32(info->mAssociationDesc);
	mData.putUInt32(info->mSequenceNumber);
	mData.putString(info->mName);

	char created[100], modified[100];
	formatDateTime(info->mDateCreated, created, sizeof(created));
	formatDateTime(info->mDateModified, modified, sizeof(modified));

	mData.putString(created);
	mData.putString(modified);
	if (info->mKeywords)
		mData.putString(info->mKeywords);
	else
		mData.putEmptyString();

   if (sendRequest(MTP_OPERATION_SEND_OBJECT_INFO) && sendData()) {
		MtpResponseCode ret = readResponse();
		if (ret == MTP_RESPONSE_OK) {
			info->mStorageID = mResponse.getParameter(1);
			info->mParent = mResponse.getParameter(2);
			info->mHandle = mResponse.getParameter(3);
			return info->mHandle;
		}
	}
	return (MtpObjectHandle)-1;
}

bool MtpDevice::sendObject(MtpObjectInfo* info, int srcFD) {
	android::Mutex::Autolock autoLock(mMutex);

	int remaining = info->mCompressedSize;
	mRequest.reset();
	mRequest.setParameter(1, info->mHandle);
	if (sendRequest(MTP_OPERATION_SEND_OBJECT)) {
		// send data header
		writeDataHeader(MTP_OPERATION_SEND_OBJECT, remaining);

		char buffer[65536];
		while (remaining > 0) {
			int count = read(srcFD, buffer, sizeof(buffer));
			if (count > 0) {
				int written = mData.write(mRequestOut, buffer, count);
				// FIXME check error
				remaining -= count;
			} else {
				break;
			}
		}
	}
	MtpResponseCode ret = readResponse();
	return (remaining == 0 && ret == MTP_RESPONSE_OK);
}

bool MtpDevice::deleteObject(MtpObjectHandle handle) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, handle);
	if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) {
		MtpResponseCode ret = readResponse();
		if (ret == MTP_RESPONSE_OK)
			return true;
	}
	return false;
}

MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) {
	MtpObjectInfo* info = getObjectInfo(handle);
	if (info) {
		MtpObjectHandle parent = info->mParent;
		delete info;
		return parent;
	} else {
		return -1;
	}
}

MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) {
	MtpObjectInfo* info = getObjectInfo(handle);
	if (info) {
		MtpObjectHandle storageId = info->mStorageID;
		delete info;
		return storageId;
	} else {
		return -1;
	}
}

MtpObjectPropertyList* MtpDevice::getObjectPropsSupported(MtpObjectFormat format) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, format);
	if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		return mData.getAUInt16();
	}
	return NULL;

}

MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, code);
	if (!sendRequest(MTP_OPERATION_GET_DEVICE_PROP_DESC))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		MtpProperty* property = new MtpProperty;
		property->read(mData);
		return property;
	}
	return NULL;
}

MtpProperty* MtpDevice::getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format) {
	android::Mutex::Autolock autoLock(mMutex);

	mRequest.reset();
	mRequest.setParameter(1, code);
	mRequest.setParameter(2, format);
	if (!sendRequest(MTP_OPERATION_GET_OBJECT_PROP_DESC))
		return NULL;
	if (!readData())
		return NULL;
	MtpResponseCode ret = readResponse();
	if (ret == MTP_RESPONSE_OK) {
		MtpProperty* property = new MtpProperty;
		property->read(mData);
		return property;
	}
	return NULL;
}

bool MtpDevice::readObject(MtpObjectHandle handle,
		bool (* callback)(void* data, int offset, int length, void* clientData),
		int objectSize, void* clientData) {
	android::Mutex::Autolock autoLock(mMutex);
	bool result = false;

	mRequest.reset();
	mRequest.setParameter(1, handle);
	if (sendRequest(MTP_OPERATION_GET_OBJECT)
			&& mData.readDataHeader(mRequestIn1)) {
		uint32_t length = mData.getContainerLength();
		if ((int)length - MTP_CONTAINER_HEADER_SIZE != objectSize) {
			MTPE("readObject error objectSize: %d, length: %d",
					objectSize, length);
			goto fail;
		}
		length -= MTP_CONTAINER_HEADER_SIZE;
		uint32_t remaining = length;
		int offset = 0;

		int initialDataLength = 0;
		void* initialData = mData.getData(initialDataLength);
		if (initialData) {
			if (initialDataLength > 0) {
				if (!callback(initialData, 0, initialDataLength, clientData))
					goto fail;
				remaining -= initialDataLength;
				offset += initialDataLength;
			}
			free(initialData);
		}

		// USB reads greater than 16K don't work
		char buffer1[16384], buffer2[16384];
		mRequestIn1->buffer = buffer1;
		mRequestIn2->buffer = buffer2;
		struct usb_request* req = mRequestIn1;
		void* writeBuffer = NULL;
		int writeLength = 0;

		while (remaining > 0 || writeBuffer) {
			if (remaining > 0) {
				// queue up a read request
				req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
				if (mData.readDataAsync(req)) {
					MTPE("readDataAsync failed");
					goto fail;
				}
			} else {
				req = NULL;
			}

			if (writeBuffer) {
				// write previous buffer
				if (!callback(writeBuffer, offset, writeLength, clientData)) {
					MTPE("write failed");
					// wait for pending read before failing
					if (req)
						mData.readDataWait(mDevice);
					goto fail;
				}
				offset += writeLength;
				writeBuffer = NULL;
			}

			// wait for read to complete
			if (req) {
				int read = mData.readDataWait(mDevice);
				if (read < 0)
					goto fail;

				if (read > 0) {
					writeBuffer = req->buffer;
					writeLength = read;
					remaining -= read;
					req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
				} else {
					writeBuffer = NULL;
				}
			}
		}

		MtpResponseCode response = readResponse();
		if (response == MTP_RESPONSE_OK)
			result = true;
	}

fail:
	return result;
}


// reads the object's data and writes it to the specified file path
bool MtpDevice::readObject(MtpObjectHandle handle, const char* destPath, int group, int perm) {
	MTPI("readObject: %s", destPath);
	int fd = ::open(destPath, O_RDWR | O_CREAT | O_TRUNC, 0640);
	if (fd < 0) {
		MTPE("open failed for %s", destPath);
		return false;
	}

	fchown(fd, getuid(), group);
	// set permissions
	int mask = umask(0);
	fchmod(fd, perm);
	umask(mask);

	android::Mutex::Autolock autoLock(mMutex);
	bool result = false;

	mRequest.reset();
	mRequest.setParameter(1, handle);
	if (sendRequest(MTP_OPERATION_GET_OBJECT)
			&& mData.readDataHeader(mRequestIn1)) {
		uint32_t length = mData.getContainerLength();
		if (length < MTP_CONTAINER_HEADER_SIZE)
			goto fail;
		length -= MTP_CONTAINER_HEADER_SIZE;
		uint32_t remaining = length;

		int initialDataLength = 0;
		void* initialData = mData.getData(initialDataLength);
		if (initialData) {
			if (initialDataLength > 0) {
				if (write(fd, initialData, initialDataLength) != initialDataLength) {
					free(initialData);
					goto fail;
				}
				remaining -= initialDataLength;
			}
			free(initialData);
		}

		// USB reads greater than 16K don't work
		char buffer1[16384], buffer2[16384];
		mRequestIn1->buffer = buffer1;
		mRequestIn2->buffer = buffer2;
		struct usb_request* req = mRequestIn1;
		void* writeBuffer = NULL;
		int writeLength = 0;

		while (remaining > 0 || writeBuffer) {
			if (remaining > 0) {
				// queue up a read request
				req->buffer_length = (remaining > sizeof(buffer1) ? sizeof(buffer1) : remaining);
				if (mData.readDataAsync(req)) {
					MTPE("readDataAsync failed");
					goto fail;
				}
			} else {
				req = NULL;
			}

			if (writeBuffer) {
				// write previous buffer
				if (write(fd, writeBuffer, writeLength) != writeLength) {
					MTPE("write failed");
					// wait for pending read before failing
					if (req)
						mData.readDataWait(mDevice);
					goto fail;
				}
				writeBuffer = NULL;
			}

			// wait for read to complete
			if (req) {
				int read = mData.readDataWait(mDevice);
				if (read < 0)
					goto fail;

				if (read > 0) {
					writeBuffer = req->buffer;
					writeLength = read;
					remaining -= read;
					req = (req == mRequestIn1 ? mRequestIn2 : mRequestIn1);
				} else {
					writeBuffer = NULL;
				}
			}
		}

		MtpResponseCode response = readResponse();
		if (response == MTP_RESPONSE_OK)
			result = true;
	}

fail:
	::close(fd);
	return result;
}

bool MtpDevice::sendRequest(MtpOperationCode operation) {
	MTPD("sendRequest: %s\n", MtpDebug::getOperationCodeName(operation));
	mReceivedResponse = false;
	mRequest.setOperationCode(operation);
	if (mTransactionID > 0)
		mRequest.setTransactionID(mTransactionID++);
	int ret = mRequest.write(mRequestOut);
	mRequest.dump();
	return (ret > 0);
}

bool MtpDevice::sendData() {
	MTPD("sendData\n");
	mData.setOperationCode(mRequest.getOperationCode());
	mData.setTransactionID(mRequest.getTransactionID());
	int ret = mData.write(mRequestOut);
	mData.dump();
	return (ret > 0);
}

bool MtpDevice::readData() {
	mData.reset();
	int ret = mData.read(mRequestIn1);
	MTPD("readData returned %d\n", ret);
	if (ret >= MTP_CONTAINER_HEADER_SIZE) {
		if (mData.getContainerType() == MTP_CONTAINER_TYPE_RESPONSE) {
			MTPD("got response packet instead of data packet");
			// we got a response packet rather than data
			// copy it to mResponse
			mResponse.copyFrom(mData);
			mReceivedResponse = true;
			return false;
		}
		mData.dump();
		return true;
	}
	else {
		MTPE("readResponse failed\n");
		return false;
	}
}

bool MtpDevice::writeDataHeader(MtpOperationCode operation, int dataLength) {
	mData.setOperationCode(operation);
	mData.setTransactionID(mRequest.getTransactionID());
	return (!mData.writeDataHeader(mRequestOut, dataLength));
}

MtpResponseCode MtpDevice::readResponse() {
	MTPD("readResponse\n");
	if (mReceivedResponse) {
		mReceivedResponse = false;
		return mResponse.getResponseCode();
	}
	int ret = mResponse.read(mRequestIn1);
	// handle zero length packets, which might occur if the data transfer
	// ends on a packet boundary
	if (ret == 0)
		ret = mResponse.read(mRequestIn1);
	if (ret >= MTP_CONTAINER_HEADER_SIZE) {
		mResponse.dump();
		return mResponse.getResponseCode();
	} else {
		MTPE("readResponse failed\n");
		return -1;
	}
}