[转]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查看信息。

  1. C:\Users\XXXX>ndk-build --version  
  2. GNU Make 3.81  
  3. Copyright (C) 2006  Free Software Foundation, Inc.  
  4. This is free software; see the source for copying conditions.  
  5. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A  
  6. PARTICULAR PURPOSE.  
  7.   
  8. 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

  1. /* Start with debug message logging enabled */  
  2. /* #undef ENABLE_DEBUG_LOGGING */  
  3.   
  4. /* Message logging */  
  5. #define ENABLE_LOGGING  
  6.   
  7. /* Define to 1 if you have the <dlfcn.h> header file. */  
  8. #define HAVE_DLFCN_H 1  
  9.   
  10. /* Define to 1 if you have the `gettimeofday' function. */  
  11. #define HAVE_GETTIMEOFDAY 1  
  12.   
  13. /* Define to 1 if you have the <inttypes.h> header file. */  
  14. #define HAVE_INTTYPES_H 1  
  15.   
  16. /* Linux backend */  
  17. #define OS_LINUX 1  
  18.   
  19. /* Enable output to system log */  
  20. #define USE_SYSTEM_LOGGING_FACILITY 1  
  21.   
  22. /* type of second poll() argument */  
  23. #define POLL_NFDS_TYPE nfds_t  
  24.   
  25. /* Use POSIX Threads */  
  26. #define THREADS_POSIX 1  
  27.   
  28. /* Default visibility */  
  29. #define DEFAULT_VISIBILITY __attribute__((visibility("default")))  
  30.   
  31. /* Define to 1 if you have the <memory.h> header file. */  
  32. #define HAVE_MEMORY_H 1  
  33.   
  34. /* Define to 1 if you have the <poll.h> header file. */  
  35. #define HAVE_POLL_H 1  
  36.   
  37. /* Define to 1 if you have the <signal.h> header file. */  
  38. #define HAVE_SIGNAL_H 1  
  39.   
  40. /* Define to 1 if you have the <sys/stat.h> header file. */  
  41. #define HAVE_SYS_STAT_H 1  
  42.   
  43. /* Define to 1 if you have the <sys/time.h> header file. */  
  44. #define HAVE_SYS_TIME_H 1  
  45.   
  46. /* Define to 1 if you have the <sys/types.h> header file. */  
  47. #define HAVE_SYS_TYPES_H 1  
  48.   
  49. /* Define to 1 if you have the <unistd.h> header file. */  
  50. #define HAVE_UNISTD_H 1  
  51.   
  52. /* Define to 1 if you have the <linux/filter.h> header file. */  
  53. #define HAVE_LINUX_FILTER_H 1  
  54.   
  55. /* Define to 1 if you have the <linux/netlink.h> header file. */  
  56. #define HAVE_LINUX_NETLINK_H 1  
  57.   
  58. /* Define to 1 if you have the <asm/types.h> header file. */  
  59. #define HAVE_ASM_TYPES_H 1  
  60.   
  61. /* Define to 1 if you have the <sys/socket.h> header file. */  
  62. #define HAVE_SYS_SOCKET_H 1  


Android.mk

  1. LOCAL_PATH:= $(call my-dir)  
  2.   
  3. include $(LOCAL_PATH)/libusb.mk  
  4. include $(LOCAL_PATH)/examples.mk  
  5. include $(LOCAL_PATH)/tests.mk  


Application.mk

  1. APP_ABI := all  
  2.   
  3. APP_LDFLAGS := -llog  


examples.mk

  1. # Android build config for libusb examples  
  2. # Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>  
  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. LOCAL_PATH:= $(call my-dir)  
  20. LIBUSB_ROOT_REL:= ../..  
  21. LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..  
  22.   
  23. # listdevs  
  24.   
  25. include $(CLEAR_VARS)  
  26.   
  27. LOCAL_SRC_FILES := \  
  28.   $(LIBUSB_ROOT_REL)/examples/listdevs.c  
  29.   
  30. LOCAL_C_INCLUDES += \  
  31.   $(LIBUSB_ROOT_ABS)  
  32.   
  33. LOCAL_SHARED_LIBRARIES += libusb1.0  
  34.   
  35. LOCAL_MODULE:= listdevs  
  36.   
  37. include $(BUILD_EXECUTABLE)  
  38.   
  39. # xusb  
  40.   
  41. include $(CLEAR_VARS)  
  42.   
  43. LOCAL_SRC_FILES := \  
  44.   $(LIBUSB_ROOT_REL)/examples/xusb.c  
  45.   
  46. LOCAL_C_INCLUDES += \  
  47.   $(LIBUSB_ROOT_ABS)  
  48.   
  49. LOCAL_SHARED_LIBRARIES += libusb1.0  
  50.   
  51. LOCAL_MODULE:= xusb  
  52.   
  53. include $(BUILD_EXECUTABLE)  
  54.   
  55. # hotplugtest  
  56.   
  57. include $(CLEAR_VARS)  
  58.   
  59. LOCAL_SRC_FILES := \  
  60.   $(LIBUSB_ROOT_REL)/examples/hotplugtest.c  
  61.   
  62. LOCAL_C_INCLUDES += \  
  63.   $(LIBUSB_ROOT_ABS)  
  64.   
  65. LOCAL_SHARED_LIBRARIES += libusb1.0  
  66.   
  67. LOCAL_MODULE:= hotplugtest  
  68.   
  69. include $(BUILD_EXECUTABLE)  
  70.   
  71. # fxload  
  72.   
  73. include $(CLEAR_VARS)  
  74.   
  75. LOCAL_SRC_FILES := \  
  76.   $(LIBUSB_ROOT_REL)/examples/fxload.c \  
  77.   $(LIBUSB_ROOT_REL)/examples/ezusb.c  
  78.   
  79. LOCAL_C_INCLUDES += \  
  80.   $(LIBUSB_ROOT_ABS)  
  81.   
  82. LOCAL_SHARED_LIBRARIES += libusb1.0  
  83.   
  84. LOCAL_MODULE:= fxload  
  85.   
  86. include $(BUILD_EXECUTABLE)  
  87.   
  88. # sam3u_benchmake  
  89.   
  90. include $(CLEAR_VARS)  
  91.   
  92. LOCAL_SRC_FILES := \  
  93.   $(LIBUSB_ROOT_REL)/examples/sam3u_benchmark.c  
  94.   
  95. LOCAL_C_INCLUDES += \  
  96.   $(LIBUSB_ROOT_ABS)  
  97.   
  98. LOCAL_SHARED_LIBRARIES += libusb1.0  
  99.   
  100. LOCAL_MODULE:= sam3u_benchmark  
  101.   
  102. include $(BUILD_EXECUTABLE)  
  103.   
  104. # dpfp  
  105.   
  106. include $(CLEAR_VARS)  
  107.   
  108. LOCAL_SRC_FILES := \  
  109.   $(LIBUSB_ROOT_REL)/examples/dpfp.c  
  110.   
  111. LOCAL_C_INCLUDES += \  
  112.   $(LIBUSB_ROOT_ABS)  
  113.   
  114. LOCAL_SHARED_LIBRARIES += libusb1.0  
  115.   
  116. LOCAL_MODULE:= dpfp  
  117.   
  118. include $(BUILD_EXECUTABLE)  
  119.   
  120. # dpfp_threaded  
  121.   
  122. include $(CLEAR_VARS)  
  123.   
  124. LOCAL_SRC_FILES := \  
  125.   $(LIBUSB_ROOT_REL)/examples/dpfp_threaded.c  
  126.   
  127. LOCAL_C_INCLUDES += \  
  128.   $(LIBUSB_ROOT_ABS)  
  129.   
  130. LOCAL_SHARED_LIBRARIES += libusb1.0  
  131.   
  132. LOCAL_MODULE:= dpfp_threaded  
  133.   
  134. include $(BUILD_EXECUTABLE)  


libusb.mk

  1. # Android build config for libusb  
  2. # Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>  
  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. LOCAL_PATH:= $(call my-dir)  
  20. LIBUSB_ROOT_REL:= ../..  
  21. LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..  
  22.   
  23. # libusb  
  24.   
  25. include $(CLEAR_VARS)  
  26.   
  27. LIBUSB_ROOT_REL:= ../..  
  28. LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..  
  29.   
  30. LOCAL_SRC_FILES := \  
  31.   $(LIBUSB_ROOT_REL)/libusb/core.c \  
  32.   $(LIBUSB_ROOT_REL)/libusb/descriptor.c \  
  33.   $(LIBUSB_ROOT_REL)/libusb/hotplug.c \  
  34.   $(LIBUSB_ROOT_REL)/libusb/io.c \  
  35.   $(LIBUSB_ROOT_REL)/libusb/sync.c \  
  36.   $(LIBUSB_ROOT_REL)/libusb/strerror.c \  
  37.   $(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \  
  38.   $(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \  
  39.   $(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \  
  40.   $(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c  
  41.   
  42. LOCAL_C_INCLUDES += \  
  43.   $(LOCAL_PATH)/.. \  
  44.   $(LIBUSB_ROOT_ABS)/libusb \  
  45.   $(LIBUSB_ROOT_ABS)/libusb/os  
  46.   
  47. LOCAL_EXPORT_C_INCLUDES := \  
  48.   $(LIBUSB_ROOT_ABS)/libusb  
  49.   
  50. LOCAL_LDLIBS := -llog  
  51.   
  52. LOCAL_MODULE := libusb1.0  
  53.   
  54. include $(BUILD_SHARED_LIBRARY)  


tests.mk

  1. # Android build config for libusb tests  
  2. # Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>  
  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. LOCAL_PATH:= $(call my-dir)  
  20. LIBUSB_ROOT_REL:= ../..  
  21. LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..  
  22.   
  23. # testlib  
  24.   
  25. include $(CLEAR_VARS)  
  26.   
  27. LOCAL_SRC_FILES := \  
  28.   $(LIBUSB_ROOT_REL)/tests/testlib.c  
  29.   
  30. LOCAL_C_INCLUDES += \  
  31.   $(LIBUSB_ROOT_ABS)/tests  
  32.   
  33. LOCAL_EXPORT_C_INCLUDES := \  
  34.   $(LIBUSB_ROOT_ABS)/tests  
  35.   
  36. LOCAL_MODULE := testlib  
  37.   
  38. include $(BUILD_STATIC_LIBRARY)  
  39.   
  40.   
  41. # stress  
  42.   
  43. include $(CLEAR_VARS)  
  44.   
  45. LOCAL_SRC_FILES := \  
  46.   $(LIBUSB_ROOT_REL)/tests/stress.c  
  47.   
  48. LOCAL_C_INCLUDES += \  
  49.   $(LIBUSB_ROOT_ABS)  
  50.   
  51. LOCAL_SHARED_LIBRARIES += libusb1.0  
  52. LOCAL_STATIC_LIBRARIES += testlib  
  53.   
  54. LOCAL_MODULE:= stress  
  55.   
  56. 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

  1. LOCAL_PATH:= $(call my-dir)  
  2.   
  3. include $(LOCAL_PATH)/libhackrf.mk  
  4. include $(LOCAL_PATH)/../../libusb/Android.mk  

 


Application.mk

  1. APP_ABI := armeabi armeabi-v7a  
  2.   
  3. APP_LDFLAGS := -llog  


libhackrf.mk

  1. # Android build config for libhackrf  
  2. # Copyright © 2014 <pagekpang@gmail.com>  
  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.   
  15. LOCAL_PATH:= $(call my-dir)  
  16. LIBUSB_ROOT_REL:= ../..  
  17. LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..  
  18.   
  19. # libhackrf  
  20. #D:\dev\android-ndk-r9d\ndk-build  
  21.   
  22.   
  23. include $(CLEAR_VARS)  
  24.   
  25. LOCAL_SRC_FILES := $(LIBUSB_ROOT_REL)/hackrf.c  
  26.   
  27. LOCAL_C_INCLUDES += $(LIBUSB_ROOT_ABS)/ \  
  28.                     $(LIBUSB_ROOT_ABS)/libusb/  
  29.   
  30. LOCAL_LDLIBS := -llog  
  31.   
  32. LOCAL_MODULE := libhackrf  
  33.   
  34. LOCAL_SHARED_LIBRARIES += libusb1.0  
  35.   
  36. include $(BUILD_SHARED_LIBRARY)  


4)切换到android目录,使用ndk-build进行编译


总体编译完成后,即可得到libhackrf.so和libusb.so以及对映的头文件,可以拷贝到android中使用。

posted @ 2015-05-25 16:25  jason_leeee  阅读(1531)  评论(0编辑  收藏  举报