Skip to content

Commit 62766ba

Browse files
committed
added dynamic linking, fixed some issues
1 parent bbbab0f commit 62766ba

14 files changed

Lines changed: 1171 additions & 5240 deletions

MANIFEST.in

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
include python_hackrf/pylibhackrf/hackrf.h
2-
include python_hackrf/pylibhackrf/chackrf.pxd
3-
include python_hackrf/pylibhackrf/pyhackrf.pyx
4-
include README.md
51
include pyproject.toml
2+
include README.md

python_hackrf/pyhackrf_tools/__android.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from jnius import autoclass
66
except ImportError:
77
def autoclass(item):
8-
raise RuntimeError("autoclass not available")
8+
raise RuntimeError('autoclass not available')
99

1010
try:
1111
from android.broadcast import BroadcastReceiver
1212
except ImportError:
1313
def BroadcastReceiver(item):
14-
raise RuntimeError("BroadcastReceiver not available")
14+
raise RuntimeError('BroadcastReceiver not available')
1515

1616

1717
class USBBroadcastReceiver:
@@ -43,7 +43,7 @@ def get_usb_device_descriptor():
4343

4444
activity = autoclass('org.kivy.android.PythonActivity').mActivity
4545
usb_manager = activity.getSystemService(Context.USB_SERVICE)
46-
permission_intent = "libusb.android.USB_PERMISSION"
46+
permission_intent = 'libusb.android.USB_PERMISSION'
4747

4848
flags = PendingIntent.FLAG_IMMUTABLE
4949
mPermissionIntent = PendingIntent.getBroadcast(activity, 0, autoclass('android.content.Intent')(permission_intent), flags)
@@ -72,6 +72,6 @@ def get_device():
7272
file_descriptor = get_usb_device_descriptor()
7373

7474
if file_descriptor is not None:
75-
device = pyhackrf.pyhackrf_init_on_android(file_descriptor)
75+
device = pyhackrf.pyhackrf_open(file_descriptor)
7676

7777
return device

python_hackrf/pyhackrf_tools/pyhackrf_info.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def pyhackrf_info(print_to_console: bool = True, initialize: bool = True) -> Non
5757
for operacake_board_address in operacake_boards:
5858
mode = device.pyhackrf_get_operacake_mode(operacake_board_address)
5959
print_info += f'Opera Cake found, address: {operacake_board_address} | switching mode: {mode}'
60+
61+
device.pyhackrf_close()
6062
else:
6163
print_info += 'No HackRF boards found.'
6264

python_hackrf/pyhackrf_tools/pyhackrf_operacake.py

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
2727
serial_number: str = None,
28-
print_to_console: bool = True, ) -> None | str:
28+
print_to_console: bool = True,
29+
) -> None | str:
2930

3031
initialize = True if device is None else False
3132

3233
if initialize:
3334
pyhackrf.pyhackrf_init()
35+
3436
if serial_number is not None:
3537
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
3638
else:
@@ -53,16 +55,25 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
5355
return operacake_info
5456

5557
if initialize:
58+
device.pyhackrf_close()
5659
pyhackrf.pyhackrf_exit()
5760

5861

59-
def pyhackrf_set_operacake_mode(address: int, mode: str, serial_number: str = None) -> None:
60-
pyhackrf.pyhackrf_init()
62+
def pyhackrf_set_operacake_mode(address: int,
63+
mode: str,
64+
serial_number: str = None,
65+
device: pyhackrf.PyHackrfDevice = None,
66+
) -> None:
6167

62-
if serial_number is not None:
63-
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
64-
else:
65-
device = pyhackrf.pyhackrf_open()
68+
initialize = True if device is None else False
69+
70+
if initialize:
71+
pyhackrf.pyhackrf_init()
72+
73+
if serial_number is not None:
74+
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
75+
else:
76+
device = pyhackrf.pyhackrf_open()
6677

6778
if mode == 'frequency':
6879
mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_FREQUENCY
@@ -73,55 +84,93 @@ def pyhackrf_set_operacake_mode(address: int, mode: str, serial_number: str = No
7384

7485
device.pyhackrf_set_operacake_mode(address, mode)
7586

76-
pyhackrf.pyhackrf_exit()
87+
if initialize:
88+
device.pyhackrf_close()
89+
pyhackrf.pyhackrf_exit()
7790

7891

79-
def pyhackrf_set_operacake_freq_ranges(freq_ranges: list, serial_number: str = None) -> None:
80-
pyhackrf.pyhackrf_init()
92+
def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
93+
serial_number: str = None,
94+
device: pyhackrf.PyHackrfDevice = None,
95+
) -> None:
8196

82-
if serial_number is not None:
83-
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
84-
else:
85-
device = pyhackrf.pyhackrf_open()
97+
initialize = True if device is None else False
98+
99+
if initialize:
100+
pyhackrf.pyhackrf_init()
101+
102+
if serial_number is not None:
103+
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
104+
else:
105+
device = pyhackrf.pyhackrf_open()
86106

87107
device.pyhackrf_set_operacake_freq_ranges(freq_ranges)
88108

89-
pyhackrf.pyhackrf_exit()
109+
if initialize:
110+
device.pyhackrf_close()
111+
pyhackrf.pyhackrf_exit()
90112

91113

92-
def pyhackrf_set_operacake_dwell_times(dwell_times: list, serial_number: str = None) -> None:
93-
pyhackrf.pyhackrf_init()
114+
def pyhackrf_set_operacake_dwell_times(dwell_times: list,
115+
serial_number: str = None,
116+
device: pyhackrf.PyHackrfDevice = None,
117+
) -> None:
94118

95-
if serial_number is not None:
96-
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
97-
else:
98-
device = pyhackrf.pyhackrf_open()
119+
initialize = True if device is None else False
120+
121+
if initialize:
122+
pyhackrf.pyhackrf_init()
123+
124+
if serial_number is not None:
125+
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
126+
else:
127+
device = pyhackrf.pyhackrf_open()
99128

100129
device.pyhackrf_set_operacake_dwell_times(dwell_times)
101130

102-
pyhackrf.pyhackrf_exit()
131+
if initialize:
132+
device.pyhackrf_close()
133+
pyhackrf.pyhackrf_exit()
103134

104135

105-
def pyhackrf_set_operacake_ports(address: int, port_a: str, port_b: str, serial_number: str = None, ) -> None:
106-
pyhackrf.pyhackrf_init()
136+
def pyhackrf_set_operacake_ports(address: int,
137+
port_a: str,
138+
port_b: str,
139+
serial_number: str = None,
140+
device: pyhackrf.PyHackrfDevice = None,
141+
) -> None:
107142

108-
if serial_number is not None:
109-
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
110-
else:
111-
device = pyhackrf.pyhackrf_open()
143+
initialize = True if device is None else False
144+
145+
if initialize:
146+
pyhackrf.pyhackrf_init()
147+
148+
if serial_number is not None:
149+
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
150+
else:
151+
device = pyhackrf.pyhackrf_open()
112152

113153
device.pyhackrf_set_operacake_ports(address, port_a, port_b)
114154

115-
pyhackrf.pyhackrf_exit()
155+
if initialize:
156+
device.pyhackrf_close()
157+
pyhackrf.pyhackrf_exit()
116158

117159

118-
def pyhackrf_operacake_gpio_test(address: int, serial_number: str = None, ) -> None:
119-
pyhackrf.pyhackrf_init()
160+
def pyhackrf_operacake_gpio_test(address: int,
161+
serial_number: str = None,
162+
device: pyhackrf.PyHackrfDevice = None,
163+
) -> None:
120164

121-
if serial_number is not None:
122-
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
123-
else:
124-
device = pyhackrf.pyhackrf_open()
165+
initialize = True if device is None else False
166+
167+
if initialize:
168+
pyhackrf.pyhackrf_init()
169+
170+
if serial_number is not None:
171+
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
172+
else:
173+
device = pyhackrf.pyhackrf_open()
125174

126175
test_result = device.pyhackrf_operacake_gpio_test(address)
127176
if test_result == 0xFFFF:
@@ -145,4 +194,7 @@ def pyhackrf_operacake_gpio_test(address: int, serial_number: str = None, ) -> N
145194
print("u1ctrl \t%d\t%d\t%d\n", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
146195
else:
147196
print('GPIO test passed')
148-
pyhackrf.pyhackrf_exit()
197+
198+
if initialize:
199+
device.pyhackrf_close()
200+
pyhackrf.pyhackrf_exit()

python_hackrf/pyhackrf_tools/pyhackrf_sweep.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,5 +421,7 @@ def pyhackrf_sweep(frequencies: list = [0, 6000], lna_gain: int = 16, vga_gain:
421421
if print_to_console:
422422
sys.stderr.write('pyhackrf_exit() done\n')
423423

424-
clear_queue(sweep_queue)
424+
if sweep_queue is not None:
425+
clear_queue(sweep_queue)
426+
425427
run_available = False
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from . import pyhackrf # noqa F401
1+
try:
2+
from . import pyhackrf_android as pyhackrf # noqa F401
3+
except ImportError:
4+
from . import pyhackrf # noqa F401

python_hackrf/pylibhackrf/chackrf.pxd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from libc.stdint cimport *
2424

25-
cdef extern from "hackrf.h":
25+
cdef extern from 'hackrf.h':
2626
int SAMPLES_PER_BLOCK
2727
int BYTES_PER_BLOCK
2828
int MAX_SWEEP_RANGES
@@ -162,8 +162,6 @@ cdef extern from "hackrf.h":
162162

163163
int hackrf_init()
164164

165-
int hackrf_init_on_android(int fileDescriptor, hackrf_device** device)
166-
167165
int hackrf_exit()
168166

169167
const char* hackrf_library_version()

0 commit comments

Comments
 (0)