blob: 5da4d45a82c011761905e5a87dfaa508a92242a5 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -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
17#include <unistd.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <usbhost/usbhost.h>
22#include <linux/usb/ch9.h>
23
24#include "MtpClient.h"
Mike Lockwood335dd2b2010-05-19 10:33:39 -040025#include "MtpDeviceInfo.h"
Mike Lockwoode13401b2010-05-19 15:12:14 -040026#include "MtpObjectInfo.h"
Mike Lockwood335dd2b2010-05-19 10:33:39 -040027#include "MtpStorageInfo.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040028
Mike Lockwood7850ef92010-05-14 10:10:36 -040029using namespace android;
30
Mike Lockwood16864ba2010-05-11 17:16:59 -040031static struct usb_device *sCameraDevice = NULL;
32static int sCameraInterface = 0;
33static MtpClient *sClient = NULL;
34
35
36static void start_session(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
37 struct usb_endpoint *ep_intr)
38{
39 if (sClient)
40 delete sClient;
41 sClient = new MtpClient(ep_in, ep_out, ep_intr);
42 sClient->openSession();
Mike Lockwood335dd2b2010-05-19 10:33:39 -040043 MtpDeviceInfo* info = sClient->getDeviceInfo();
44 if (info) {
45 info->print();
46 delete info;
47 }
48 MtpStorageIDList* storageIDs = sClient->getStorageIDs();
49 if (storageIDs) {
50 for (int i = 0; i < storageIDs->size(); i++) {
Mike Lockwoode13401b2010-05-19 15:12:14 -040051 MtpStorageID storageID = (*storageIDs)[i];
52 MtpStorageInfo* info = sClient->getStorageInfo(storageID);
Mike Lockwood335dd2b2010-05-19 10:33:39 -040053 if (info) {
54 info->print();
55 delete info;
56 }
Mike Lockwoode13401b2010-05-19 15:12:14 -040057 MtpObjectHandleList* objects = sClient->getObjectHandles(storageID, 0, MTP_PARENT_ROOT);
58 if (objects) {
59 for (int j = 0; j < objects->size(); j++) {
60 MtpObjectHandle handle = (*objects)[j];
61 MtpObjectInfo* info = sClient->getObjectInfo(handle);
62 if (info) {
63 info->print();
64 delete info;
65 }
66 }
67 delete objects;
68 }
Mike Lockwood335dd2b2010-05-19 10:33:39 -040069 }
70 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040071}
72
73static void usb_device_added(const char *devname)
74{
75 struct usb_descriptor_header* desc;
76 struct usb_descriptor_iter iter;
77
78 struct usb_device *device = usb_device_open(devname);
79 if (!device) return;
80
81 usb_descriptor_iter_init(device, &iter);
82
83 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
84 if (desc->bDescriptorType == USB_DT_INTERFACE) {
85 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
86
87 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
88 interface->bInterfaceSubClass == 1 && // Still Image Capture
89 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
90 {
91 printf("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
92 usb_device_get_product_name(device));
93
94 // interface should be followed by three endpoints
95 struct usb_endpoint_descriptor *ep, *ep_in_desc = NULL, *ep_out_desc = NULL, *ep_intr_desc = NULL;
96 for (int i = 0; i < 3; i++) {
97 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
98 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
99 fprintf(stderr, "endpoints not found\n");
100 return;
101 }
102 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
103 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
104 ep_in_desc = ep;
105 else
106 ep_out_desc = ep;
107 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
108 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
109 ep_intr_desc = ep;
110 }
111 }
112 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
113 fprintf(stderr, "endpoints not found\n");
114 return;
115 }
116
117 struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
118 struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
119 struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
120
121 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
122 fprintf(stderr, "usb_device_claim_interface failed\n");
123 usb_endpoint_close(ep_in);
124 usb_endpoint_close(ep_out);
125 usb_endpoint_close(ep_intr);
126 return;
127 }
128
129 if (sCameraDevice) {
130 usb_device_release_interface(sCameraDevice, sCameraInterface);
131 usb_device_close(sCameraDevice);
132 }
133 sCameraDevice = device;
134 start_session(ep_in, ep_out, ep_intr);
135 }
136 }
137 }
138
139 if (device != sCameraDevice)
140 usb_device_close(device);
141}
142
143static void usb_device_removed(const char *devname)
144{
145 if (sCameraDevice && !strcmp(devname, usb_device_get_name(sCameraDevice))) {
146 delete sClient;
147 printf("Camera removed!\n");
148 usb_device_release_interface(sCameraDevice, sCameraInterface);
149 usb_device_close(sCameraDevice);
150 sCameraDevice = NULL;
151 }
152}
153
154int main(int argc, char* argv[])
155{
156 if (usb_host_init(usb_device_added, usb_device_removed)) {
157 fprintf(stderr, "usb_host_init failed\n");
158 return -1;
159 }
160
161 while (1) {
162 sleep(1);
163 }
164
165 return 0;
Mike Lockwood7850ef92010-05-14 10:10:36 -0400166}