Skip to content

Commit 1110748

Browse files
committed
DO NOT MERGE: USB accessory support library
This provides a mechanism for developing applications to work with USB accessories in versions of android prior to the introduction of the android.hardware.UsbManager APIs. Applications should link against the com.android.future.usb.accessory library to use this support. Change-Id: I0b61e20b63eec42c506f0895a0c9a439bdfdf7f5 Signed-off-by: Mike Lockwood <lockwood@android.com>
1 parent 40bbf92 commit 1110748

File tree

14 files changed

+1317
-2
lines changed

14 files changed

+1317
-2
lines changed

data/etc/android.hardware.usb.accessory.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
<!-- This is the standard feature indicating that the device supports USB accessories. -->
1818
<permissions>
1919
<feature name="android.hardware.usb.accessory" />
20+
<library name="com.android.future.usb.accessory"
21+
file="/system/framework/com.android.future.usb.accessory.jar" />
2022
</permissions>

libs/usb/Android.mk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright (C) 2011 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+
LOCAL_PATH := $(call my-dir)
18+
19+
include $(CLEAR_VARS)
20+
21+
LOCAL_SRC_FILES := $(call all-java-files-under,src)
22+
23+
LOCAL_MODULE_TAGS := optional
24+
25+
LOCAL_MODULE:= com.android.future.usb.accessory
26+
27+
include $(BUILD_JAVA_LIBRARY)

libs/usb/src/com/google/android/usb/UsbAccessory.java renamed to libs/usb/src/com/android/future/usb/UsbAccessory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.android.usb;
17+
package com.android.future.usb;
1818

1919
/**
2020
* A class representing a USB accessory.

libs/usb/src/com/google/android/usb/UsbManager.java renamed to libs/usb/src/com/android/future/usb/UsbManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717

18-
package com.google.android.usb;
18+
package com.android.future.usb;
1919

2020
import android.content.Context;
2121
import android.content.Intent;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Copyright (C) 2011 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+
LOCAL_PATH:= $(call my-dir)
18+
include $(CLEAR_VARS)
19+
20+
LOCAL_MODULE_TAGS := tests
21+
22+
LOCAL_SRC_FILES := $(call all-subdir-java-files)
23+
24+
LOCAL_PACKAGE_NAME := AccessoryChatGB
25+
26+
LOCAL_JAVA_LIBRARIES := com.android.future.usb.accessory
27+
28+
# Force an old SDK version to make sure we aren't using newer UsbManager APIs
29+
LOCAL_SDK_VERSION := 8
30+
31+
include $(BUILD_PACKAGE)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2011 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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
package="com.android.accessorychat">
19+
20+
<application>
21+
<uses-library android:name="com.android.future.usb.accessory" />
22+
23+
<activity android:name="AccessoryChat" android:label="Accessory Chat GB">
24+
<intent-filter>
25+
<action android:name="android.intent.action.MAIN" />
26+
<category android:name="android.intent.category.DEFAULT" />
27+
<category android:name="android.intent.category.LAUNCHER" />
28+
</intent-filter>
29+
30+
<intent-filter>
31+
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
32+
</intent-filter>
33+
34+
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
35+
android:resource="@xml/accessory_filter" />
36+
</activity>
37+
</application>
38+
<uses-sdk android:minSdkVersion="10" />
39+
</manifest>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This is a test app for the USB accessory APIs. It consists of two parts:
2+
3+
AccessoryChat - A Java app with a chat-like UI that sends and receives strings
4+
via the UsbAccessory class.
5+
6+
accessorychat - A C command-line program that communicates with AccessoryChat.
7+
This program behaves as if it were a USB accessory.
8+
It builds both for the host (Linux PC) and as an android
9+
command line program, which will work if run as root on an
10+
android device with USB host support
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
LOCAL_PATH:= $(call my-dir)
2+
3+
# Build for Linux (desktop) host
4+
ifeq ($(HOST_OS),linux)
5+
6+
include $(CLEAR_VARS)
7+
8+
LOCAL_MODULE_TAGS := optional
9+
10+
LOCAL_SRC_FILES := accessorychat.c usbhost.c
11+
12+
LOCAL_MODULE := accessorychat
13+
14+
LOCAL_C_INCLUDES += bionic/libc/kernel/common
15+
LOCAL_STATIC_LIBRARIES := libcutils
16+
LOCAL_LDLIBS += -lpthread
17+
LOCAL_CFLAGS := -g -O0
18+
19+
include $(BUILD_HOST_EXECUTABLE)
20+
21+
endif
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (C) 2011 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+
#include <stdlib.h>
21+
22+
#include <sys/types.h>
23+
#include <sys/stat.h>
24+
#include <fcntl.h>
25+
#include <errno.h>
26+
#include <pthread.h>
27+
28+
#include <usbhost/usbhost.h>
29+
#include <linux/usb/f_accessory.h>
30+
31+
struct usb_device *sDevice = NULL;
32+
33+
static void* read_thread(void* arg) {
34+
int endpoint = (int)arg;
35+
int ret = 0;
36+
37+
while (sDevice && ret >= 0) {
38+
char buffer[16384];
39+
40+
ret = usb_device_bulk_transfer(sDevice, endpoint, buffer, sizeof(buffer), 1000);
41+
if (ret < 0 && errno == ETIMEDOUT)
42+
ret = 0;
43+
if (ret > 0) {
44+
fwrite(buffer, 1, ret, stdout);
45+
printf("\n");
46+
fflush(stdout);
47+
}
48+
}
49+
50+
return NULL;
51+
}
52+
53+
static void* write_thread(void* arg) {
54+
int endpoint = (int)arg;
55+
int ret = 0;
56+
57+
while (ret >= 0) {
58+
char buffer[16384];
59+
char *line = fgets(buffer, sizeof(buffer), stdin);
60+
if (!line || !sDevice)
61+
break;
62+
ret = usb_device_bulk_transfer(sDevice, endpoint, line, strlen(line), 1000);
63+
}
64+
65+
return NULL;
66+
}
67+
68+
static void send_string(struct usb_device *device, int index, const char* string) {
69+
int ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
70+
ACCESSORY_SEND_STRING, 0, index, (void *)string, strlen(string) + 1, 0);
71+
}
72+
73+
static int usb_device_added(const char *devname, void* client_data) {
74+
struct usb_descriptor_header* desc;
75+
struct usb_descriptor_iter iter;
76+
uint16_t vendorId, productId;
77+
int ret;
78+
pthread_t th;
79+
80+
struct usb_device *device = usb_device_open(devname);
81+
if (!device) {
82+
fprintf(stderr, "usb_device_open failed\n");
83+
return 0;
84+
}
85+
86+
vendorId = usb_device_get_vendor_id(device);
87+
productId = usb_device_get_product_id(device);
88+
89+
if (vendorId == 0x18D1 || vendorId == 0x22B8) {
90+
if (!sDevice && (productId == 0x2D00 || productId == 0x2D01)) {
91+
struct usb_descriptor_header* desc;
92+
struct usb_descriptor_iter iter;
93+
struct usb_interface_descriptor *intf = NULL;
94+
struct usb_endpoint_descriptor *ep1 = NULL;
95+
struct usb_endpoint_descriptor *ep2 = NULL;
96+
97+
printf("Found android device in accessory mode\n");
98+
sDevice = device;
99+
100+
usb_descriptor_iter_init(device, &iter);
101+
while ((desc = usb_descriptor_iter_next(&iter)) != NULL && (!intf || !ep1 || !ep2)) {
102+
if (desc->bDescriptorType == USB_DT_INTERFACE) {
103+
intf = (struct usb_interface_descriptor *)desc;
104+
} else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
105+
if (ep1)
106+
ep2 = (struct usb_endpoint_descriptor *)desc;
107+
else
108+
ep1 = (struct usb_endpoint_descriptor *)desc;
109+
}
110+
}
111+
112+
if (!intf) {
113+
fprintf(stderr, "interface not found\n");
114+
exit(1);
115+
}
116+
if (!ep1 || !ep2) {
117+
fprintf(stderr, "endpoints not found\n");
118+
exit(1);
119+
}
120+
121+
if (usb_device_claim_interface(device, intf->bInterfaceNumber)) {
122+
fprintf(stderr, "usb_device_claim_interface failed errno: %d\n", errno);
123+
exit(1);
124+
}
125+
126+
if ((ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
127+
pthread_create(&th, NULL, read_thread, (void *)ep1->bEndpointAddress);
128+
pthread_create(&th, NULL, write_thread, (void *)ep2->bEndpointAddress);
129+
} else {
130+
pthread_create(&th, NULL, read_thread, (void *)ep2->bEndpointAddress);
131+
pthread_create(&th, NULL, write_thread, (void *)ep1->bEndpointAddress);
132+
}
133+
} else {
134+
printf("Found possible android device - attempting to switch to accessory mode\n");
135+
136+
send_string(device, ACCESSORY_STRING_MANUFACTURER, "Google, Inc.");
137+
send_string(device, ACCESSORY_STRING_MODEL, "AccessoryChat");
138+
send_string(device, ACCESSORY_STRING_TYPE, "Sample Program");
139+
send_string(device, ACCESSORY_STRING_VERSION, "1.0");
140+
141+
ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
142+
ACCESSORY_START, 0, 0, 0, 0, 0);
143+
return 0;
144+
}
145+
}
146+
147+
if (device != sDevice)
148+
usb_device_close(device);
149+
150+
return 0;
151+
}
152+
153+
static int usb_device_removed(const char *devname, void* client_data) {
154+
if (sDevice && !strcmp(usb_device_get_name(sDevice), devname)) {
155+
usb_device_close(sDevice);
156+
sDevice = NULL;
157+
// exit when we are disconnected
158+
return 1;
159+
}
160+
return 0;
161+
}
162+
163+
164+
int main(int argc, char* argv[]) {
165+
struct usb_host_context* context = usb_host_init();
166+
if (!context) {
167+
fprintf(stderr, "usb_host_init failed");
168+
return 1;
169+
}
170+
171+
// this will never return so it is safe to pass thiz directly
172+
usb_host_run(context, usb_device_added, usb_device_removed, NULL, NULL);
173+
return 0;
174+
}

0 commit comments

Comments
 (0)