blob: ef22111d4b2a1f90640dbfbedf07e18750a4867d [file] [log] [blame]
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001/*
2 * Copyright (C) 2009 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#define LOG_TAG "UsbService"
18#include "utils/Log.h"
19
20#include "jni.h"
21#include "JNIHelp.h"
22#include "android_runtime/AndroidRuntime.h"
23#include "utils/Vector.h"
24
25#include <usbhost/usbhost.h>
26
27#include <stdio.h>
28#include <asm/byteorder.h>
29
30namespace android
31{
32
33static struct file_descriptor_offsets_t
34{
35 jclass mClass;
36 jmethodID mConstructor;
37 jfieldID mDescriptor;
38} gFileDescriptorOffsets;
39
40static struct parcel_file_descriptor_offsets_t
41{
42 jclass mClass;
43 jmethodID mConstructor;
44} gParcelFileDescriptorOffsets;
45
46static jmethodID method_usbDeviceAdded;
47static jmethodID method_usbDeviceRemoved;
48
49static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
50 if (env->ExceptionCheck()) {
51 LOGE("An exception was thrown by callback '%s'.", methodName);
52 LOGE_EX(env);
53 env->ExceptionClear();
54 }
55}
56
57static int usb_device_added(const char *devname, void* client_data) {
58 struct usb_descriptor_header* desc;
59 struct usb_descriptor_iter iter;
60
61 struct usb_device *device = usb_device_open(devname);
62 if (!device) {
63 LOGE("usb_device_open failed\n");
64 return 0;
65 }
66
67 JNIEnv* env = AndroidRuntime::getJNIEnv();
68 jobject thiz = (jobject)client_data;
69 Vector<int> interfaceValues;
70 Vector<int> endpointValues;
71 const usb_device_descriptor* deviceDesc = usb_device_get_device_descriptor(device);
72
73 uint16_t vendorId = usb_device_get_vendor_id(device);
74 uint16_t productId = usb_device_get_product_id(device);
75 uint8_t deviceClass = deviceDesc->bDeviceClass;
76 uint8_t deviceSubClass = deviceDesc->bDeviceSubClass;
77 uint8_t protocol = deviceDesc->bDeviceProtocol;
78
79 usb_descriptor_iter_init(device, &iter);
80
81 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
82 if (desc->bDescriptorType == USB_DT_INTERFACE) {
83 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
84
85 // push class, subclass, protocol and number of endpoints into interfaceValues vector
86 interfaceValues.add(interface->bInterfaceNumber);
87 interfaceValues.add(interface->bInterfaceClass);
88 interfaceValues.add(interface->bInterfaceSubClass);
89 interfaceValues.add(interface->bInterfaceProtocol);
90 interfaceValues.add(interface->bNumEndpoints);
91 } else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
92 struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)desc;
93
94 // push address, attributes, max packet size and interval into endpointValues vector
95 endpointValues.add(endpoint->bEndpointAddress);
96 endpointValues.add(endpoint->bmAttributes);
97 endpointValues.add(__le16_to_cpu(endpoint->wMaxPacketSize));
98 endpointValues.add(endpoint->bInterval);
99 }
100 }
101
102 usb_device_close(device);
103
104 // handle generic device notification
105 int length = interfaceValues.size();
106 jintArray interfaceArray = env->NewIntArray(length);
107 env->SetIntArrayRegion(interfaceArray, 0, length, interfaceValues.array());
108
109 length = endpointValues.size();
110 jintArray endpointArray = env->NewIntArray(length);
111 env->SetIntArrayRegion(endpointArray, 0, length, endpointValues.array());
112
113 env->CallVoidMethod(thiz, method_usbDeviceAdded,
114 env->NewStringUTF(devname), vendorId, productId, deviceClass,
115 deviceSubClass, protocol, interfaceArray, endpointArray);
116 checkAndClearExceptionFromCallback(env, __FUNCTION__);
117
118 return 0;
119}
120
121static int usb_device_removed(const char *devname, void* client_data) {
122 JNIEnv* env = AndroidRuntime::getJNIEnv();
123 jobject thiz = (jobject)client_data;
124
125 env->CallVoidMethod(thiz, method_usbDeviceRemoved, env->NewStringUTF(devname));
126 checkAndClearExceptionFromCallback(env, __FUNCTION__);
127 return 0;
128}
129
130static void android_server_UsbService_monitorUsbHostBus(JNIEnv *env, jobject thiz)
131{
132 struct usb_host_context* context = usb_host_init();
133 if (!context) {
134 LOGE("usb_host_init failed");
135 return;
136 }
137 // this will never return so it is safe to pass thiz directly
138 usb_host_run(context, usb_device_added, usb_device_removed, NULL, (void *)thiz);
139}
140
141static jobject android_server_UsbService_openDevice(JNIEnv *env, jobject thiz, jstring deviceName)
142{
143 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
144 struct usb_device* device = usb_device_open(deviceNameStr);
145 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
146
147 if (!device)
148 return NULL;
149
150 int fd = usb_device_get_fd(device);
151 if (fd < 0)
152 return NULL;
153 int newFD = dup(fd);
154 usb_device_close(device);
155
156 jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass,
157 gFileDescriptorOffsets.mConstructor);
158 if (fileDescriptor != NULL) {
159 env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, newFD);
160 } else {
161 return NULL;
162 }
163 return env->NewObject(gParcelFileDescriptorOffsets.mClass,
164 gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
165}
166
167static JNINativeMethod method_table[] = {
168 { "monitorUsbHostBus", "()V", (void*)android_server_UsbService_monitorUsbHostBus },
169 { "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
170 (void*)android_server_UsbService_openDevice },
171};
172
173int register_android_server_UsbService(JNIEnv *env)
174{
175 jclass clazz = env->FindClass("com/android/server/UsbService");
176 if (clazz == NULL) {
177 LOGE("Can't find com/android/server/UsbService");
178 return -1;
179 }
180 method_usbDeviceAdded = env->GetMethodID(clazz, "usbDeviceAdded", "(Ljava/lang/String;IIIII[I[I)V");
181 if (method_usbDeviceAdded == NULL) {
182 LOGE("Can't find usbDeviceAdded");
183 return -1;
184 }
185 method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", "(Ljava/lang/String;)V");
186 if (method_usbDeviceRemoved == NULL) {
187 LOGE("Can't find usbDeviceRemoved");
188 return -1;
189 }
190
191 clazz = env->FindClass("java/io/FileDescriptor");
192 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
193 gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
194 gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
195 gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
196 LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
197 "Unable to find descriptor field in java.io.FileDescriptor");
198
199 clazz = env->FindClass("android/os/ParcelFileDescriptor");
200 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
201 gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
202 gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
203 LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
204 "Unable to find constructor for android.os.ParcelFileDescriptor");
205
206 return jniRegisterNativeMethods(env, "com/android/server/UsbService",
207 method_table, NELEM(method_table));
208}
209
210};