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