2014年开源夏令营-android下编译libusb和libhackrf


        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

/* Start with debug message logging enabled */
/* #undef ENABLE_DEBUG_LOGGING */

/* Message logging */
#define ENABLE_LOGGING

/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1

/* Define to 1 if you have the `gettimeofday' function. */
#define HAVE_GETTIMEOFDAY 1

/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1

/* Linux backend */
#define OS_LINUX 1

/* Enable output to system log */
#define USE_SYSTEM_LOGGING_FACILITY 1

/* type of second poll() argument */
#define POLL_NFDS_TYPE nfds_t

/* Use POSIX Threads */
#define THREADS_POSIX 1

/* Default visibility */
#define DEFAULT_VISIBILITY __attribute__((visibility("default")))

/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1

/* Define to 1 if you have the <poll.h> header file. */
#define HAVE_POLL_H 1

/* Define to 1 if you have the <signal.h> header file. */
#define HAVE_SIGNAL_H 1

/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1

/* Define to 1 if you have the <sys/time.h> header file. */
#define HAVE_SYS_TIME_H 1

/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1

/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1

/* Define to 1 if you have the <linux/filter.h> header file. */
#define HAVE_LINUX_FILTER_H 1

/* Define to 1 if you have the <linux/netlink.h> header file. */
#define HAVE_LINUX_NETLINK_H 1

/* Define to 1 if you have the <asm/types.h> header file. */
#define HAVE_ASM_TYPES_H 1

/* Define to 1 if you have the <sys/socket.h> header file. */
#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
# Android build config for libusb examples
# 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)/../..

# listdevs

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/listdevs.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= listdevs

include $(BUILD_EXECUTABLE)

# xusb

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/xusb.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= xusb

include $(BUILD_EXECUTABLE)

# hotplugtest

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/hotplugtest.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= hotplugtest

include $(BUILD_EXECUTABLE)

# fxload

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/fxload.c \
  $(LIBUSB_ROOT_REL)/examples/ezusb.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= fxload

include $(BUILD_EXECUTABLE)

# sam3u_benchmake

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/sam3u_benchmark.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= sam3u_benchmark

include $(BUILD_EXECUTABLE)

# dpfp

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/dpfp.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= dpfp

include $(BUILD_EXECUTABLE)

# dpfp_threaded

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
  $(LIBUSB_ROOT_REL)/examples/dpfp_threaded.c

LOCAL_C_INCLUDES += \
  $(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0

LOCAL_MODULE:= dpfp_threaded

include $(BUILD_EXECUTABLE)

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中使用。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
交叉编译LIBUSB是指在一个平台上编译生成可以在另一个不同的平台上运行的LIBUSB库文件。通常情况下,交叉编译是在一个主机系统上进行,而生成的库文件将用于目标系统。根据引用内容和,我们可以总结出交叉编译LIBUSB的步骤如下: 1. 首先,需要在主机系统上获取LIBUSB的源代码。可以从LIBUSB的官方网站或其他可靠来源下载。 2. 安装交叉编译工具链,该工具链能够将源代码编译为目标系统可执行文件。这包括交叉编译器、链接器和库文件等。 3. 配置交叉编译环境。根据目标系统的架构和操作系统类型,设置交叉编译工具链的路径、编译选项和目标系统的相关信息。 4. 运行配置命令,用于生成Makefile或其他构建脚本,以便在接下来的步骤中编译LIBUSB。 5. 使用交叉编译工具链编译LIBUSB的源代码。根据具体的编译选项和参数,执行编译命令来生成目标系统的LIBUSB库文件。 6. 在目标系统上进行安装。这包括将编译生成的库文件、头文件和其他相关文件复制到目标系统的相应目录中,以便其他程序可以使用该库进行开发和运行。 根据引用内容,在完成交叉编译后,我们可以在目标系统中的/usr/local目录下找到编译生成的LIBUSB文件和相关内容。 请注意,具体的交叉编译步骤可能会因不同的操作系统和工具链而有所差异。因此,在实际进行交叉编译LIBUSB时,应该参考官方文档或相关资源以获取更具体的指导和说明。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Libusb交叉编译](https://blog.csdn.net/beArobot/article/details/88627831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Linux aarch64交叉编译libusb库](https://blog.csdn.net/vviccc/article/details/126411782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值