Skip to content

Commit 7bfbb8b

Browse files
Toby Graypbatard
Toby Gray
authored andcommitted
Android: Add formal Android support
* Also fix an issue with LIBUSB_LOG_LEVEL_NONE
1 parent 9222a54 commit 7bfbb8b

File tree

7 files changed

+216
-27
lines changed

7 files changed

+216
-27
lines changed

android/README

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
libusb for Android
2+
==================
3+
4+
To build libusb for Android do the following:
5+
6+
1. Download the latest NDK from:
7+
http://developer.android.com/tools/sdk/ndk/index.html
8+
9+
2. Extract the NDK.
10+
11+
3. Open a shell and make sure there exist an NDK global variable
12+
set to the directory where you extracted the NDK.
13+
14+
4. Change directory to libusb's "android/jni"
15+
16+
5. Run "$NDK/ndk-build".
17+
18+
The libusb library, examples and tests can then be found in:
19+
"android/libs/$ARCH"
20+
21+
Where $ARCH is one of:
22+
armeabi
23+
armeabi-v7a
24+
x86

android/config.h

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Android build config for libusbx
3+
* Copyright © 2012-2013 RealVNC Ltd. <[email protected]>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
/* Start with debug message logging enabled */
21+
/* #undef ENABLE_DEBUG_LOGGING */
22+
23+
/* Message logging */
24+
#define ENABLE_LOGGING
25+
26+
/* Define to 1 if you have the <dlfcn.h> header file. */
27+
#define HAVE_DLFCN_H 1
28+
29+
/* Define to 1 if you have the `gettimeofday' function. */
30+
#define HAVE_GETTIMEOFDAY 1
31+
32+
/* Define to 1 if you have the <inttypes.h> header file. */
33+
#define HAVE_INTTYPES_H 1
34+
35+
/* Linux backend */
36+
#define OS_LINUX 1
37+
38+
/* Enable output to system log */
39+
#define USE_SYSTEM_LOGGING_FACILITY 1
40+
41+
/* type of second poll() argument */
42+
#define POLL_NFDS_TYPE nfds_t
43+
44+
/* Use POSIX Threads */
45+
#define THREADS_POSIX 1
46+
47+
/* Default visibility */
48+
#define DEFAULT_VISIBILITY __attribute__((visibility("default")))
49+
50+
/* Define to 1 if you have the <memory.h> header file. */
51+
#define HAVE_MEMORY_H 1
52+
53+
/* Define to 1 if you have the <poll.h> header file. */
54+
#define HAVE_POLL_H 1
55+
56+
/* Define to 1 if you have the <signal.h> header file. */
57+
#define HAVE_SIGNAL_H 1
58+
59+
/* Define to 1 if you have the <sys/stat.h> header file. */
60+
#define HAVE_SYS_STAT_H 1
61+
62+
/* Define to 1 if you have the <sys/time.h> header file. */
63+
#define HAVE_SYS_TIME_H 1
64+
65+
/* Define to 1 if you have the <sys/types.h> header file. */
66+
#define HAVE_SYS_TYPES_H 1
67+
68+
/* Define to 1 if you have the <unistd.h> header file. */
69+
#define HAVE_UNISTD_H 1
70+
71+
/* Define to 1 if you have the <linux/filter.h> header file. */
72+
#define HAVE_LINUX_FILTER_H 1
73+
74+
/* Define to 1 if you have the <linux/netlink.h> header file. */
75+
#define HAVE_LINUX_NETLINK_H 1
76+
77+
/* Define to 1 if you have the <asm/types.h> header file. */
78+
#define HAVE_ASM_TYPES_H 1
79+
80+
/* Define to 1 if you have the <sys/socket.h> header file. */
81+
#define HAVE_SYS_SOCKET_H 1
82+
83+
/* Add defines which Android is missing */
84+
#ifndef TIMESPEC_TO_TIMEVAL
85+
#define TIMESPEC_TO_TIMEVAL(tv, ts) \
86+
do { \
87+
(tv)->tv_sec = (ts)->tv_sec; \
88+
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
89+
} while (0)
90+
#endif

android/jni/Android.mk

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# Android build config for libusbx
3+
# Copyright © 2012-2013 RealVNC Ltd. <[email protected]>
4+
#
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
#
10+
# This library is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with this library; if not, write to the Free Software
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
#
19+
20+
LOCAL_PATH:= $(call my-dir)
21+
22+
# libusb
23+
24+
include $(CLEAR_VARS)
25+
26+
LIBUSB_ROOT_REL:= ../..
27+
LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
28+
29+
LOCAL_SRC_FILES := \
30+
$(LIBUSB_ROOT_REL)/libusb/core.c \
31+
$(LIBUSB_ROOT_REL)/libusb/descriptor.c \
32+
$(LIBUSB_ROOT_REL)/libusb/hotplug.c \
33+
$(LIBUSB_ROOT_REL)/libusb/io.c \
34+
$(LIBUSB_ROOT_REL)/libusb/sync.c \
35+
$(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \
36+
$(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \
37+
$(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \
38+
$(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c
39+
40+
LOCAL_C_INCLUDES += \
41+
$(LOCAL_PATH)/.. \
42+
$(LIBUSB_ROOT_ABS)/libusb \
43+
$(LIBUSB_ROOT_ABS)/libusb/os
44+
45+
LOCAL_EXPORT_C_INCLUDES := \
46+
$(LIBUSB_ROOT_ABS)/libusb
47+
48+
LOCAL_LDLIBS := -llog
49+
50+
LOCAL_MODULE := libusb1.0
51+
52+
include $(BUILD_SHARED_LIBRARY)
53+
54+
55+
# listdevs
56+
57+
include $(CLEAR_VARS)
58+
59+
LOCAL_SRC_FILES := \
60+
$(LIBUSB_ROOT_REL)/examples/listdevs.c
61+
62+
LOCAL_C_INCLUDES += \
63+
$(LIBUSB_ROOT_ABS)
64+
65+
LOCAL_SHARED_LIBRARIES += libusb1.0
66+
67+
LOCAL_MODULE:= listdevs
68+
69+
include $(BUILD_EXECUTABLE)

android/jni/Application.mk

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Android application build config for libusb
2+
# Copyright © 2012-2013 RealVNC Ltd. <[email protected]>
3+
#
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation; either
7+
# version 2.1 of the License, or (at your option) any later version.
8+
#
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Lesser General Public
15+
# License along with this library; if not, write to the Free Software
16+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
#
18+
19+
APP_ABI := armeabi armeabi-v7a x86

libusb/core.c

+10-25
Original file line numberDiff line numberDiff line change
@@ -2038,14 +2038,22 @@ static void usbi_log_str(struct libusb_context *ctx,
20382038
WCHAR wbuf[USBI_MAX_LOG_LEN];
20392039
MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf));
20402040
OutputDebugStringW(wbuf);
2041+
#elif defined(__ANDROID__)
2042+
int priority = ANDROID_LOG_UNKNOWN;
2043+
switch (level) {
2044+
case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
2045+
case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
2046+
case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break;
2047+
case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
2048+
}
2049+
__android_log_write(priority, "libusb", str);
20412050
#elif defined(HAVE_SYSLOG_FUNC)
20422051
int syslog_level = LOG_INFO;
20432052
switch (level) {
20442053
case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
20452054
case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
20462055
case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break;
20472056
case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break;
2048-
case LIBUSB_LOG_LEVEL_NONE: break;
20492057
}
20502058
syslog(syslog_level, "%s", str);
20512059
#else /* All of gcc, Clang, XCode seem to use #warning */
@@ -2086,28 +2094,6 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
20862094
return;
20872095
#endif
20882096

2089-
#ifdef __ANDROID__
2090-
int prio;
2091-
switch (level) {
2092-
case LOG_LEVEL_INFO:
2093-
prio = ANDROID_LOG_INFO;
2094-
break;
2095-
case LOG_LEVEL_WARNING:
2096-
prio = ANDROID_LOG_WARN;
2097-
break;
2098-
case LOG_LEVEL_ERROR:
2099-
prio = ANDROID_LOG_ERROR;
2100-
break;
2101-
case LOG_LEVEL_DEBUG:
2102-
prio = ANDROID_LOG_DEBUG;
2103-
break;
2104-
default:
2105-
prio = ANDROID_LOG_UNKNOWN;
2106-
break;
2107-
}
2108-
2109-
__android_log_vprint(prio, "LibUsb", format, args);
2110-
#else
21112097
usbi_gettimeofday(&now, NULL);
21122098
if ((global_debug) && (!has_debug_header_been_displayed)) {
21132099
has_debug_header_been_displayed = 1;
@@ -2135,7 +2121,7 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
21352121
prefix = "debug";
21362122
break;
21372123
case LIBUSB_LOG_LEVEL_NONE:
2138-
break;
2124+
return;
21392125
default:
21402126
prefix = "unknown";
21412127
break;
@@ -2171,7 +2157,6 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
21712157
strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
21722158

21732159
usbi_log_str(ctx, level, buf);
2174-
#endif
21752160
}
21762161

21772162
void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,

libusb/os/threads_posix.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
6363
int usbi_get_tid(void)
6464
{
6565
int ret = -1;
66-
#if defined(__linux__)
66+
#if defined(__ANDROID__)
67+
ret = gettid();
68+
#elif defined(__linux__)
6769
ret = syscall(SYS_gettid);
6870
#elif defined(__OpenBSD__)
6971
/* The following only works with OpenBSD > 5.1 as it requires

libusb/version_nano.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define LIBUSB_NANO 10806
1+
#define LIBUSB_NANO 10807

0 commit comments

Comments
 (0)