summaryrefslogtreecommitdiffstats
path: root/services/jni
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2011-02-15 09:50:22 -0500
committerMike Lockwood <lockwood@android.com>2011-02-16 08:25:16 -0500
commit9182d3c4eb1f9065cb33df5a3594969dd0d42acc (patch)
tree3fa11b1752bef1a6bfc9d27c6455aa4b3854f03b /services/jni
parent2fa160840c81c32aa9703d08ced24a2427863bf4 (diff)
downloadframeworks_base-9182d3c4eb1f9065cb33df5a3594969dd0d42acc.zip
frameworks_base-9182d3c4eb1f9065cb33df5a3594969dd0d42acc.tar.gz
frameworks_base-9182d3c4eb1f9065cb33df5a3594969dd0d42acc.tar.bz2
UsbManager: New APIs for USB accessories
USB accessories are peripherals that connect to android devices as a USB host. When connected, the accessory will first identify itself to the android device by sending manufacturer, product, accessory type and version strings to the device, and then request the device to enter USB accessory mode. The device will then enable the USB accessory kernel driver and disable all other USB functionality except possibly adb (adb can be used while the android device is connected to the PC and the PC is running software that emulates a USB accessory) The class android.hardware.UsbAccessory is used to describe the currently attached USB accessory. UsbAccessory contains the manufacturer, product, accessory type and version strings to identify the accessory. The accessory can be opened as a ParcelFileDescriptor, which can be used to communicate with the accessory over two bulk endpoints. The Intents UsbManager.USB_ACCESSORY_ATTACHED and UsbManager.USB_ACCESSORY_DETACHED are broadcast when accessories are connected and disconnected to the device. The USB_ACCESSORY_ATTACHED contains a UsbAccessory object for the attached accessory as an extra. The Intent also contains string extras for the manufacturer, product, accessory type and version strings to allow filtering on these strings. Change-Id: Ie77cbf51814a4aa44a6b1e62673bfe4c6aa81755 Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'services/jni')
-rw-r--r--services/jni/com_android_server_UsbService.cpp66
1 files changed, 65 insertions, 1 deletions
diff --git a/services/jni/com_android_server_UsbService.cpp b/services/jni/com_android_server_UsbService.cpp
index ef22111..192daaf 100644
--- a/services/jni/com_android_server_UsbService.cpp
+++ b/services/jni/com_android_server_UsbService.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * 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.
@@ -26,6 +26,13 @@
#include <stdio.h>
#include <asm/byteorder.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/usb/f_accessory.h>
+
+#define DRIVER_NAME "/dev/usb_accessory"
namespace android
{
@@ -164,10 +171,67 @@ static jobject android_server_UsbService_openDevice(JNIEnv *env, jobject thiz, j
gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
}
+static void set_accessory_string(JNIEnv *env, int fd, int cmd, jobjectArray strArray, int index)
+{
+ char buffer[256];
+
+ buffer[0] = 0;
+ int length = ioctl(fd, cmd, buffer);
+ LOGD("ioctl returned %d", length);
+ if (buffer[0]) {
+ jstring obj = env->NewStringUTF(buffer);
+ env->SetObjectArrayElement(strArray, index, obj);
+ env->DeleteLocalRef(obj);
+ }
+}
+
+
+static jobjectArray android_server_UsbService_getAccessoryStrings(JNIEnv *env, jobject thiz)
+{
+ int fd = open(DRIVER_NAME, O_RDWR);
+ if (fd < 0) {
+ LOGE("could not open %s", DRIVER_NAME);
+ return NULL;
+ }
+ jclass stringClass = env->FindClass("java/lang/String");
+ jobjectArray strArray = env->NewObjectArray(4, stringClass, NULL);
+ if (!strArray) goto out;
+ set_accessory_string(env, fd, ACCESSORY_GET_STRING_MANUFACTURER, strArray, 0);
+ set_accessory_string(env, fd, ACCESSORY_GET_STRING_MODEL, strArray, 1);
+ set_accessory_string(env, fd, ACCESSORY_GET_STRING_TYPE, strArray, 2);
+ set_accessory_string(env, fd, ACCESSORY_GET_STRING_VERSION, strArray, 3);
+
+out:
+ close(fd);
+ return strArray;
+}
+
+static jobject android_server_UsbService_openAccessory(JNIEnv *env, jobject thiz)
+{
+ int fd = open(DRIVER_NAME, O_RDWR);
+ if (fd < 0) {
+ LOGE("could not open %s", DRIVER_NAME);
+ return NULL;
+ }
+ jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass,
+ gFileDescriptorOffsets.mConstructor);
+ if (fileDescriptor != NULL) {
+ env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, fd);
+ } else {
+ return NULL;
+ }
+ return env->NewObject(gParcelFileDescriptorOffsets.mClass,
+ gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
+}
+
static JNINativeMethod method_table[] = {
{ "monitorUsbHostBus", "()V", (void*)android_server_UsbService_monitorUsbHostBus },
{ "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
(void*)android_server_UsbService_openDevice },
+ { "nativeGetAccessoryStrings", "()[Ljava/lang/String;",
+ (void*)android_server_UsbService_getAccessoryStrings },
+ { "nativeOpenAccessory","()Landroid/os/ParcelFileDescriptor;",
+ (void*)android_server_UsbService_openAccessory },
};
int register_android_server_UsbService(JNIEnv *env)