标签:
libhackrf是上层应用程序操作hackrf的入口库,是软件操作硬件的中间件,在android上使用hackrf当然也需要使用 libhackrf。操作系统与hackrf之间的通信是通过USB2.0完成的,libhackrf使用了libusb进行USB通信。android 并没有自带libusb,所以我们需要自行编译。
整体分为两个部分,首先是libusb的编译,然后是libhackrf的编译。
libusb
libusb的编译十分简单,因为其没有过多的依赖,而麻烦的是android对usb权限的控制,解决权限问题有两种方法,第一种是改造
libusb,并在android应用层获取usb的权限将usb的信息传到libusb。第二种是通过修改系统权限列表,使得当前程序在系统usb用户
组中。我这里为了更少的更改libusb和libhackrf文件,就选用了第二种方法。以下为libusb的编译步骤
1)确保NDK安装成功,可通过在cmd中运行ndk-build --version查看信息。
- C:\Users\XXXX>ndk-build --version
- GNU Make 3.81
- Copyright (C) 2006 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions.
- There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
- PARTICULAR PURPOSE.
-
- This program built for i586-pc-mingw32
2)下载libusb,可以到www.libusb.org下载。
3)加压libusb后在源码根目录创建android文件夹和相应文件。
|- libusb-master
|- android
|- jni
|- Android.mk
|- Application.mk
|- examples.mk
|- libusb.mk
|- tests.mk
|- libs
|- obj
|- config.h
|- libusb
...
文件如下:
config.h
-
- #define ENABLE_LOGGING
-
- #define HAVE_DLFCN_H 1
-
- #define HAVE_GETTIMEOFDAY 1
-
- #define HAVE_INTTYPES_H 1
-
- #define OS_LINUX 1
-
- #define USE_SYSTEM_LOGGING_FACILITY 1
-
- #define POLL_NFDS_TYPE nfds_t
-
- #define THREADS_POSIX 1
-
- #define DEFAULT_VISIBILITY __attribute__((visibility("default")))
-
- #define HAVE_MEMORY_H 1
-
- #define HAVE_POLL_H 1
-
- #define HAVE_SIGNAL_H 1
-
- #define HAVE_SYS_STAT_H 1
-
- #define HAVE_SYS_TIME_H 1
-
- #define HAVE_SYS_TYPES_H 1
-
- #define HAVE_UNISTD_H 1
-
- #define HAVE_LINUX_FILTER_H 1
-
- #define HAVE_LINUX_NETLINK_H 1
-
- #define HAVE_ASM_TYPES_H 1
-
- #define HAVE_SYS_SOCKET_H 1
Android.mk
- LOCAL_PATH:= $(call my-dir)
-
- include $(LOCAL_PATH)/libusb.mk
- include $(LOCAL_PATH)/examples.mk
- include $(LOCAL_PATH)/tests.mk
Application.mk
- APP_ABI := all
-
- APP_LDFLAGS := -llog
examples.mk
libusb.mk
- # Android build config for libusb
- # Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- #
-
- LOCAL_PATH:= $(call my-dir)
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- # libusb
-
- include $(CLEAR_VARS)
-
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- LOCAL_SRC_FILES := \
- $(LIBUSB_ROOT_REL)/libusb/core.c \
- $(LIBUSB_ROOT_REL)/libusb/descriptor.c \
- $(LIBUSB_ROOT_REL)/libusb/hotplug.c \
- $(LIBUSB_ROOT_REL)/libusb/io.c \
- $(LIBUSB_ROOT_REL)/libusb/sync.c \
- $(LIBUSB_ROOT_REL)/libusb/strerror.c \
- $(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \
- $(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \
- $(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \
- $(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c
-
- LOCAL_C_INCLUDES += \
- $(LOCAL_PATH)/.. \
- $(LIBUSB_ROOT_ABS)/libusb \
- $(LIBUSB_ROOT_ABS)/libusb/os
-
- LOCAL_EXPORT_C_INCLUDES := \
- $(LIBUSB_ROOT_ABS)/libusb
-
- LOCAL_LDLIBS := -llog
-
- LOCAL_MODULE := libusb1.0
-
- include $(BUILD_SHARED_LIBRARY)
tests.mk
- # Android build config for libusb tests
- # Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- #
-
- LOCAL_PATH:= $(call my-dir)
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- # testlib
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := \
- $(LIBUSB_ROOT_REL)/tests/testlib.c
-
- LOCAL_C_INCLUDES += \
- $(LIBUSB_ROOT_ABS)/tests
-
- LOCAL_EXPORT_C_INCLUDES := \
- $(LIBUSB_ROOT_ABS)/tests
-
- LOCAL_MODULE := testlib
-
- include $(BUILD_STATIC_LIBRARY)
-
-
- # stress
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := \
- $(LIBUSB_ROOT_REL)/tests/stress.c
-
- LOCAL_C_INCLUDES += \
- $(LIBUSB_ROOT_ABS)
-
- LOCAL_SHARED_LIBRARIES += libusb1.0
- LOCAL_STATIC_LIBRARIES += testlib
-
- LOCAL_MODULE:= stress
-
- include $(BUILD_EXECUTABLE)
4)切换到android目录,使用ndk-build进行编译
编译完成之后,可以在libs文件夹下看到不同平台的lib和可执行文件,拷贝即可使用。
libhackrf
libhackrf的编译,可以参照libusb的编译。在hackrf-master\host\libhackrf\src下创建如下结构
|- android/
|- jni/
|- Android.mk
|- Application.mk
|- libhackrf.mk
|- libs/
|- obj/
Android.mk
- LOCAL_PATH:= $(call my-dir)
-
- include $(LOCAL_PATH)/libhackrf.mk
- include $(LOCAL_PATH)/../../libusb/Android.mk
Application.mk
- APP_ABI := armeabi armeabi-v7a
-
- APP_LDFLAGS := -llog
libhackrf.mk
- # Android build config for libhackrf
- # Copyright © 2014 <pagekpang@gmail.com>
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
-
- LOCAL_PATH:= $(call my-dir)
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- # libhackrf
- #D:\dev\android-ndk-r9d\ndk-build
-
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := $(LIBUSB_ROOT_REL)/hackrf.c
-
- LOCAL_C_INCLUDES += $(LIBUSB_ROOT_ABS)/ \
- $(LIBUSB_ROOT_ABS)/libusb/
-
- LOCAL_LDLIBS := -llog
-
- LOCAL_MODULE := libhackrf
-
- LOCAL_SHARED_LIBRARIES += libusb1.0
-
- include $(BUILD_SHARED_LIBRARY)
4)切换到android目录,使用ndk-build进行编译
总体编译完成后,即可得到libhackrf.so和libusb.so以及对映的头文件,可以拷贝到android中使用。
[转]android下编译libusb和libhackrf
标签:
原文地址:http://www.cnblogs.com/jasonleeee/p/4528193.html