blob: 2efa4c900437d65a41ddadcf460188a0ca2e7343 [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"
26#include "MtpStorageInfo.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040027
Mike Lockwood7850ef92010-05-14 10:10:36 -040028using namespace android;
29
Mike Lockwood16864ba2010-05-11 17:16:59 -040030static struct usb_device *sCameraDevice = NULL;
31static int sCameraInterface = 0;
32static MtpClient *sClient = NULL;
33
34
35static void start_session(struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
36 struct usb_endpoint *ep_intr)
37{
38 if (sClient)
39 delete sClient;
40 sClient = new MtpClient(ep_in, ep_out, ep_intr);
41 sClient->openSession();
Mike Lockwood335dd2b2010-05-19 10:33:39 -040042 MtpDeviceInfo* info = sClient->getDeviceInfo();
43 if (info) {
44 info->print();
45 delete info;
46 }
47 MtpStorageIDList* storageIDs = sClient->getStorageIDs();
48 if (storageIDs) {
49 for (int i = 0; i < storageIDs->size(); i++) {
50 MtpStorageInfo* info = sClient->getStorageInfo((*storageIDs)[i]);
51 if (info) {
52 info->print();
53 delete info;
54 }
55 }
56 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040057}
58
59static void usb_device_added(const char *devname)
60{
61 struct usb_descriptor_header* desc;
62 struct usb_descriptor_iter iter;
63
64 struct usb_device *device = usb_device_open(devname);
65 if (!device) return;
66
67 usb_descriptor_iter_init(device, &iter);
68
69 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
70 if (desc->bDescriptorType == USB_DT_INTERFACE) {
71 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
72
73 if (interface->bInterfaceClass == USB_CLASS_STILL_IMAGE &&
74 interface->bInterfaceSubClass == 1 && // Still Image Capture
75 interface->bInterfaceProtocol == 1) // Picture Transfer Protocol (PIMA 15470)
76 {
77 printf("Found camera: \"%s\" \"%s\"\n", usb_device_get_manufacturer_name(device),
78 usb_device_get_product_name(device));
79
80 // interface should be followed by three endpoints
81 struct usb_endpoint_descriptor *ep, *ep_in_desc = NULL, *ep_out_desc = NULL, *ep_intr_desc = NULL;
82 for (int i = 0; i < 3; i++) {
83 ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
84 if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
85 fprintf(stderr, "endpoints not found\n");
86 return;
87 }
88 if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
89 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
90 ep_in_desc = ep;
91 else
92 ep_out_desc = ep;
93 } else if (ep->bmAttributes == USB_ENDPOINT_XFER_INT &&
94 ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
95 ep_intr_desc = ep;
96 }
97 }
98 if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
99 fprintf(stderr, "endpoints not found\n");
100 return;
101 }
102
103 struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
104 struct usb_endpoint *ep_out = usb_endpoint_open(device, ep_out_desc);
105 struct usb_endpoint *ep_intr = usb_endpoint_open(device, ep_intr_desc);
106
107 if (usb_device_claim_interface(device, interface->bInterfaceNumber)) {
108 fprintf(stderr, "usb_device_claim_interface failed\n");
109 usb_endpoint_close(ep_in);
110 usb_endpoint_close(ep_out);
111 usb_endpoint_close(ep_intr);
112 return;
113 }
114
115 if (sCameraDevice) {
116 usb_device_release_interface(sCameraDevice, sCameraInterface);
117 usb_device_close(sCameraDevice);
118 }
119 sCameraDevice = device;
120 start_session(ep_in, ep_out, ep_intr);
121 }
122 }
123 }
124
125 if (device != sCameraDevice)
126 usb_device_close(device);
127}
128
129static void usb_device_removed(const char *devname)
130{
131 if (sCameraDevice && !strcmp(devname, usb_device_get_name(sCameraDevice))) {
132 delete sClient;
133 printf("Camera removed!\n");
134 usb_device_release_interface(sCameraDevice, sCameraInterface);
135 usb_device_close(sCameraDevice);
136 sCameraDevice = NULL;
137 }
138}
139
140int main(int argc, char* argv[])
141{
142 if (usb_host_init(usb_device_added, usb_device_removed)) {
143 fprintf(stderr, "usb_host_init failed\n");
144 return -1;
145 }
146
147 while (1) {
148 sleep(1);
149 }
150
151 return 0;
Mike Lockwood7850ef92010-05-14 10:10:36 -0400152}