Skip to content

Commit 6c1967d

Browse files
committed
updated
1 parent c2cebdd commit 6c1967d

7 files changed

Lines changed: 107 additions & 63 deletions

File tree

android/libhackrf/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class LibhackrfRecipe(NDKRecipe):
1111

1212
url = 'https://github.com/greatscottgadgets/hackrf/releases/download/v{version}/hackrf-{version}.tar.xz'
13-
patches = ['public_hackrf_open_setup.patch']
13+
patches = ['hackrf_android.patch']
1414
generated_libraries = ['libhackrf.so']
1515
site_packages_name = 'libhackrf'
1616
version = '2024.02.1'
@@ -24,6 +24,7 @@ def prebuild_arch(self, arch):
2424
super().prebuild_arch(arch)
2525

2626
if not os.path.exists(os.path.join(self.get_build_dir(arch.arch), 'android')):
27+
libusb_recipe = Recipe.get_recipe('libusb', arch)
2728

2829
os.mkdir(os.path.join(self.get_build_dir(arch.arch), 'android'))
2930
os.mkdir(os.path.join(self.get_build_dir(arch.arch), 'android', 'jni'))
@@ -33,31 +34,28 @@ def prebuild_arch(self, arch):
3334
shutil.copy(os.path.join(self.get_recipe_dir(), 'jni', 'libhackrf.mk'), os.path.join(self.get_build_dir(arch.arch), 'android', 'jni'))
3435
shutil.copy(os.path.join(self.get_recipe_dir(), 'jni', 'Android.mk'), os.path.join(self.get_build_dir(arch.arch), 'android', 'jni'))
3536

36-
def get_lib_dir(self, arch):
37-
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
37+
shutil.copy(os.path.join(libusb_recipe.get_build_dir(arch), 'libusb', 'libusb.h'), os.path.join(self.get_build_dir(arch.arch), 'android', 'libusb'))
38+
39+
def get_recipe_env(self, arch):
40+
env = super().get_recipe_env(arch)
41+
env['LDFLAGS'] += f'-L{self.ctx.get_libs_dir(arch.arch)}'
42+
43+
return env
3844

3945
def get_jni_dir(self, arch):
4046
return os.path.join(self.get_build_dir(arch.arch), 'android', 'jni')
4147

4248
def build_arch(self, arch, *extra_args):
43-
if not os.path.exists(os.path.join(self.get_build_dir(arch.arch), 'android', 'libusb', 'libusb-1.0.so')):
44-
libusb_recipe = Recipe.get_recipe('libusb', arch)
45-
shutil.copy(os.path.join(libusb_recipe.get_lib_dir(arch), 'libusb1.0.so'), os.path.join(self.get_build_dir(arch.arch), 'android', 'libusb', 'libusb-1.0.so'))
46-
shutil.copy(os.path.join(libusb_recipe.get_build_dir(arch), 'libusb', 'libusb.h'), os.path.join(self.get_build_dir(arch.arch), 'android', 'libusb'))
47-
4849
env = self.get_recipe_env(arch)
4950
with current_directory(self.get_build_dir(arch.arch)):
5051
shprint(
5152
sh.Command(os.path.join(self.ctx.ndk_dir, 'ndk-build')),
5253
'NDK_PROJECT_PATH=' + self.get_build_dir(arch.arch) + '/android',
53-
'APP_PLATFORM=android-' + str(self.ctx.ndk_api),
54-
'NDK='+self.ctx.ndk_dir,
55-
'APP_ABI=' + arch.arch,
5654
*extra_args,
5755
_env=env
5856
)
5957

60-
shutil.copyfile(os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch, 'libhackrf.so'), os.path.join(self.ctx.get_libs_dir(arch.arch), 'libhackrf.so'))
58+
shutil.copyfile(os.path.join(self.get_build_dir(arch.arch), 'android', 'libs', arch.arch, 'libhackrf.so'), os.path.join(self.ctx.get_libs_dir(arch.arch), 'libhackrf.so'))
6159

6260

6361
recipe = LibhackrfRecipe()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
diff -ruN hackrf/host/libhackrf/src/hackrf.c patched/host/libhackrf/src/hackrf.c
2+
--- hackrf/host/libhackrf/src/hackrf.c 2024-02-23 01:20:25
3+
+++ patched/host/libhackrf/src/hackrf.c 2024-08-14 05:30:49
4+
@@ -509,6 +509,30 @@
5+
}
6+
}
7+
8+
+int ADDCALL hackrf_init_on_android(void) {
9+
+
10+
+ int libusb_error;
11+
+ if (g_libusb_context != NULL) {
12+
+ return HACKRF_SUCCESS;
13+
+ }
14+
+
15+
+ libusb_error = libusb_set_option(NULL, LIBUSB_OPTION_WEAK_AUTHORITY, NULL);
16+
+ if (libusb_error != LIBUSB_SUCCESS) {
17+
+ last_libusb_error = libusb_error;
18+
+ return HACKRF_ERROR_LIBUSB;
19+
+ }
20+
+
21+
+ libusb_error = libusb_init(&g_libusb_context);
22+
+ if (libusb_error != 0) {
23+
+ last_libusb_error = libusb_error;
24+
+ return HACKRF_ERROR_LIBUSB;
25+
+ }
26+
+ else {
27+
+ return HACKRF_SUCCESS;
28+
+ }
29+
+
30+
+}
31+
+
32+
int ADDCALL hackrf_exit(void)
33+
{
34+
if (open_devices == 0) {
35+
@@ -811,6 +835,28 @@
36+
}
37+
38+
return hackrf_open_setup(usb_device, device);
39+
+}
40+
+
41+
+int ADDCALL hackrf_open_on_android(int fileDescriptor, hackrf_device** device) {
42+
+
43+
+ int libusb_error;
44+
+ if (device == NULL) {
45+
+ return HACKRF_ERROR_INVALID_PARAM;
46+
+ }
47+
+ libusb_device_handle* usb_device;
48+
+
49+
+ libusb_error = libusb_wrap_sys_device(g_libusb_context, (intptr_t)fileDescriptor, &usb_device);
50+
+ if (libusb_error < 0) {
51+
+ last_libusb_error = libusb_error;
52+
+ return HACKRF_ERROR_LIBUSB;
53+
+ }
54+
+
55+
+ if (usb_device == NULL) {
56+
+ return HACKRF_ERROR_NOT_FOUND;
57+
+ }
58+
+
59+
+ return hackrf_open_setup(usb_device, device);
60+
+
61+
}
62+
63+
int ADDCALL hackrf_open_by_serial(
64+
diff -ruN hackrf/host/libhackrf/src/hackrf.h patched/host/libhackrf/src/hackrf.h
65+
--- hackrf/host/libhackrf/src/hackrf.h 2024-02-23 01:20:25
66+
+++ patched/host/libhackrf/src/hackrf.h 2024-08-14 05:32:27
67+
@@ -1059,6 +1059,8 @@
68+
*/
69+
extern ADDAPI int ADDCALL hackrf_init();
70+
71+
+extern ADDAPI int ADDCALL hackrf_init_on_android();
72+
+
73+
/**
74+
* Exit libhackrf
75+
*
76+
@@ -1120,6 +1122,8 @@
77+
* @ingroup device
78+
*/
79+
extern ADDAPI int ADDCALL hackrf_open(hackrf_device** device);
80+
+
81+
+extern ADDAPI int ADDCALL hackrf_open_on_android(int fileDescriptor, hackrf_device** device);
82+
83+
/**
84+
* Open HackRF device by serial number

android/libhackrf/jni/Application.mk

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@
2323
APP_ABI := all
2424

2525
APP_CFLAGS := \
26-
-Werror=implicit-function-declaration \
27-
-Werror=missing-prototypes \
28-
-Werror=strict-prototypes \
29-
-Werror=format-security \
30-
-Werror=uninitialized \
31-
-Werror=implicit-int \
32-
-Werror=init-self \
3326
-Wwrite-strings \
34-
-Werror=undef \
3527
-std=gnu11 \
3628
-Wshadow \
3729
-Wunused \
3830
-Wextra \
39-
-Wall \
31+
-Wall
4032

4133
APP_LDFLAGS := -llog

android/libhackrf/jni/libhackrf.mk

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@ LIBHACKRF_ROOT_ABS := $(LOCAL_PATH)/../..
2929
include $(CLEAR_VARS)
3030

3131
LOCAL_SRC_FILES := $(LIBHACKRF_ROOT_REL)/host/libhackrf/src/hackrf.c
32-
33-
LOCAL_C_INCLUDES += $(LIBHACKRF_ROOT_ABS)/android/libusb
32+
LOCAL_EXPORT_C_INCLUDES := $(LIBUSB_ROOT_ABS)/host/libhackrf/src
3433

3534
LOCAL_CFLAGS := \
3635
-I$(LIBHACKRF_ROOT_ABS)/android/libusb \
3736
-fvisibility=hidden \
38-
-pthread \
39-
-w
40-
41-
LOCAL_LDFLAGS := -L$(LIBHACKRF_ROOT_ABS)/android/libusb
37+
-pthread
4238

4339
LOCAL_LDLIBS := -llog
4440

android/libhackrf/public_hackrf_open_setup.patch

Lines changed: 0 additions & 12 deletions
This file was deleted.

android/libusb/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class LibusbRecipe(NDKRecipe):
1717
def should_build(self, arch):
1818
return True
1919

20-
def get_lib_dir(self, arch):
21-
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
22-
2320
def get_jni_dir(self, arch):
2421
return os.path.join(self.get_build_dir(arch.arch), 'android', 'jni')
2522

@@ -36,7 +33,7 @@ def build_arch(self, arch, *extra_args):
3633
_env=env
3734
)
3835

39-
shutil.copyfile(os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch, 'libusb1.0.so'), os.path.join(self.ctx.get_libs_dir(arch.arch), 'libusb-1.0.so'))
36+
shutil.copyfile(os.path.join(self.get_build_dir(arch.arch), 'android', 'libs', arch.arch, 'libusb1.0.so'), os.path.join(self.ctx.get_libs_dir(arch.arch), 'libusb1.0.so'))
4037

4138

4239
recipe = LibusbRecipe()

android/python_hackrf/__init__.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class PythonHackrfRecipe(CythonRecipe):
88
url = 'https://github.com/GvozdevLeonid/python_hackrf/releases/download/v.{version}/python_hackrf-{version}.tar.gz'
9-
depends = ['python3', 'setuptools', 'libusb', 'libhackrf', 'numpy']
9+
depends = ['python3', 'setuptools', 'numpy', 'libusb', 'libhackrf']
1010
site_packages_name = 'python_hackrf'
1111
name = 'python_hackrf'
1212
version = '1.1.1'
@@ -17,33 +17,22 @@ def get_recipe_env(self, arch):
1717
libhackrf_recipe = Recipe.get_recipe('libhackrf', arch)
1818
libusb_recipe = Recipe.get_recipe('libusb', arch)
1919

20+
libhackrf_h_dir = os.path.join(libhackrf_recipe.get_build_dir(arch), 'host', 'libhackrf', 'src')
2021
libusb_h_dir = os.path.join(libusb_recipe.get_build_dir(arch), 'libusb')
21-
libusb_so_dir = libusb_recipe.get_lib_dir(arch)
2222

23-
libhackrf_h_dir = os.path.join(libhackrf_recipe.get_build_dir(arch), 'libhackrf')
24-
libhackrf_so_dir = libhackrf_recipe.get_lib_dir(arch)
25-
26-
env['LDFLAGS'] += f' -L{libusb_so_dir} -L{libhackrf_so_dir}'
23+
env['LDFLAGS'] += f' -L{self.ctx.get_libs_dir(arch.arch)}'
2724
env['CFLAGS'] += f' -I{libusb_h_dir} -I{libhackrf_h_dir}'
2825

2926
return env
3027

31-
def postbuild_arch(self, arch):
32-
super().postbuild_arch(arch)
33-
34-
python_hackrf_dir = os.path.join(self.ctx.get_python_install_dir(arch.arch), 'python_hackrf')
35-
os.makedirs(python_hackrf_dir, exist_ok=True)
36-
37-
try:
38-
39-
shutil.move(os.path.join(self.ctx.get_python_install_dir(arch.arch), 'pyhackrf_tools'), os.path.join(python_hackrf_dir, 'pyhackrf_tools'))
40-
shutil.move(os.path.join(self.ctx.get_python_install_dir(arch.arch), 'pylibhackrf'), os.path.join(python_hackrf_dir, 'pylibhackrf'))
28+
def prebuild_arch(self, arch):
29+
super().prebuild_arch(arch)
4130

42-
shutil.copy(os.path.join(self.get_build_dir(arch.arch), '__init__.py'), python_hackrf_dir)
43-
shutil.copy(os.path.join(self.get_build_dir(arch.arch), '__main__.py'), python_hackrf_dir)
31+
if not os.path.exists(os.path.join(self.get_build_dir(arch.arch), 'python_hackrf', 'pylibhackrf', 'hackrf.c')):
32+
libhackrf_recipe = Recipe.get_recipe('libhackrf', arch)
4433

45-
except FileNotFoundError:
46-
pass
34+
shutil.copy(os.path.join(libhackrf_recipe.get_build_dir(arch), 'host', 'libhackrf', 'src', 'hackrf.h'), os.path.join(self.get_build_dir(arch.arch), 'python_hackrf', 'pylibhackrf'))
35+
shutil.copy(os.path.join(libhackrf_recipe.get_build_dir(arch), 'host', 'libhackrf', 'src', 'hackrf.c'), os.path.join(self.get_build_dir(arch.arch), 'python_hackrf', 'pylibhackrf'))
4736

4837

4938
recipe = PythonHackrfRecipe()

0 commit comments

Comments
 (0)