Skip to content

Commit 7539180

Browse files
committed
cleaned up the code
1 parent 58e5c92 commit 7539180

File tree

7 files changed

+164
-131
lines changed

7 files changed

+164
-131
lines changed

android/libhackrf/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# ruff: noqa: RUF012
12
import os
23
import shutil
4+
from typing import Any
35

46
import sh
57
from pythonforandroid.archs import Arch
@@ -11,13 +13,13 @@
1113
class LibhackrfRecipe(NDKRecipe):
1214

1315
url = 'https://github.com/greatscottgadgets/hackrf/archive/refs/tags/v{version}.tar.gz'
14-
patches = ('hackrf_android.patch', )
15-
generated_libraries = ('libhackrf.so', )
16+
patches = ['hackrf_android.patch']
17+
generated_libraries = ['libhackrf.so']
1618
site_packages_name = 'libhackrf'
17-
version = '2024.02.1'
1819
library_release = '2024.02.1'
1920
library_version = '0.9'
20-
depends = ('libusb', )
21+
version = '2024.02.1'
22+
depends = ['libusb']
2123
name = 'libhackrf'
2224

2325
def should_build(self, arch: Arch) -> bool:
@@ -39,9 +41,9 @@ def prebuild_arch(self, arch: Arch) -> None:
3941

4042
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'))
4143

42-
def get_recipe_env(self, arch: Arch) -> dict:
43-
env = super().get_recipe_env(arch)
44-
env['LDFLAGS'] += f'-L{self.ctx.get_libs_dir(arch.arch)}'
44+
def get_recipe_env(self, arch: Arch) -> dict[str, Any]:
45+
env: dict[str, Any] = super().get_recipe_env(arch)
46+
env['LDFLAGS'] = env['LDFLAGS'] + f'-L{self.ctx.get_libs_dir(arch.arch)}'
4547

4648
return env
4749

@@ -52,8 +54,7 @@ def get_lib_dir(self, arch: Arch) -> str:
5254
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
5355

5456
def build_arch(self, arch: Arch, *extra_args) -> None:
55-
env = self.get_recipe_env(arch)
56-
57+
env: dict[str, Any] = self.get_recipe_env(arch)
5758
shutil.copyfile(os.path.join(self.ctx.get_libs_dir(arch.arch), 'libusb1.0.so'), os.path.join(self.get_build_dir(arch.arch), 'android', 'jni', 'libusb1.0.so'))
5859

5960
with current_directory(self.get_build_dir(arch.arch)):

android/libusb/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# ruff: noqa: RUF012
12
import os
23
import shutil
4+
from typing import Any
35

46
import sh
57
from pythonforandroid.archs import Arch
@@ -11,7 +13,7 @@
1113
class LibusbRecipe(NDKRecipe):
1214

1315
url = 'https://github.com/libusb/libusb/archive/refs/tags/v{version}.tar.gz'
14-
generated_libraries = ('libusb-1.0.so', )
16+
generated_libraries = ['libusb-1.0.so']
1517
site_packages_name = 'libusb'
1618
version = '1.0.26'
1719
name = 'libusb'
@@ -25,16 +27,16 @@ def get_jni_dir(self, arch: Arch) -> str:
2527
def get_lib_dir(self, arch: Arch) -> str:
2628
return os.path.join(self.get_build_dir(arch.arch), 'android', 'obj', 'local', arch.arch)
2729

28-
def build_arch(self, arch: Arch, *extra_args) -> None:
29-
env = self.get_recipe_env(arch)
30+
def build_arch(self, arch: Arch, *args: Any) -> None:
31+
env: dict[str, Any] = self.get_recipe_env(arch)
3032
with current_directory(self.get_build_dir(arch.arch)):
3133
shprint(
3234
sh.Command(os.path.join(self.ctx.ndk_dir, 'ndk-build')),
3335
'NDK_PROJECT_PATH=' + self.get_build_dir(arch.arch) + '/android',
3436
'APP_PLATFORM=android-' + str(self.ctx.ndk_api),
3537
'NDK=' + self.ctx.ndk_dir,
3638
'APP_ABI=' + arch.arch,
37-
*extra_args,
39+
*args,
3840
_env=env,
3941
)
4042

python_hackrf/__main__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def main() -> None:
129129
return
130130

131131
if args.t is not None:
132-
str_dwell_times = args.f.split(',')
132+
str_dwell_times = args.t.split(',')
133133
dwell_times = []
134134
for dwell_time in str_dwell_times:
135135
try:
@@ -215,17 +215,17 @@ def main() -> None:
215215

216216
pyhackrf_sweep.pyhackrf_sweep(
217217
frequencies=frequencies,
218-
sample_rate=int(args.s) * 1e6,
219-
baseband_filter_bandwidth=float(args.b) * 1e6 if args.b is not None else None,
218+
sample_rate=int(float(args.s) * 1e6),
219+
baseband_filter_bandwidth=int(float(args.b) * 1e6) if args.b is not None else None,
220220
lna_gain=int(args.l),
221221
vga_gain=int(args.g),
222222
bin_width=int(args.w),
223223
amp_enable=args.a,
224224
antenna_enable=args.p,
225-
sweep_style=pyhackrf.py_sweep_style.LINEAR if args.S == 'L' else (pyhackrf.py_sweep_style.INTERLEAVED if args.S == 'I' else -1),
225+
sweep_style=pyhackrf.py_sweep_style.LINEAR if args.S == 'L' else (pyhackrf.py_sweep_style.INTERLEAVED if args.S == 'I' else -1), # type: ignore
226226
serial_number=args.d,
227227
binary_output=args.B,
228-
one_shot=args.__dict__.get('1'),
228+
one_shot=args.__dict__.get('1'), # type: ignore
229229
num_sweeps=int(args.N) if args.N is not None else None,
230230
filename=args.r,
231231
print_to_console=True,
@@ -234,11 +234,11 @@ def main() -> None:
234234
elif args.command == 'transfer':
235235
pyhackrf_transfer.pyhackrf_transfer(
236236
frequency=int(args.freq_hz),
237-
sample_rate=int(args.s) * 1e6,
238-
baseband_filter_bandwidth=float(args.b) * 1e6 if args.b is not None else None,
237+
sample_rate=int(float(args.s) * 1e6),
238+
baseband_filter_bandwidth=int(float(args.b) * 1e6) if args.b is not None else None,
239239
i_frequency=int(args.i_freq_hz) if args.i_freq_hz is not None else None,
240240
lo_frequency=int(args.lo_freq_hz) if args.lo_freq_hz is not None else None,
241-
image_reject=pyhackrf.py_rf_path_filter.RF_PATH_FILTER_BYPASS if args.image_reject == 'bypass' else (pyhackrf.py_rf_path_filter.RF_PATH_FILTER_LOW_PASS if args.image_reject == 'low' else (pyhackrf.py_rf_path_filter.RF_PATH_FILTER_HIGH_PASS if args.image_reject == 'high' else -1)),
241+
image_reject=pyhackrf.py_rf_path_filter.RF_PATH_FILTER_BYPASS if args.image_reject == 'bypass' else (pyhackrf.py_rf_path_filter.RF_PATH_FILTER_LOW_PASS if args.image_reject == 'low' else (pyhackrf.py_rf_path_filter.RF_PATH_FILTER_HIGH_PASS if args.image_reject == 'high' else -1)), # type: ignore
242242
rx_lna_gain=int(args.l),
243243
rx_vga_gain=int(args.g),
244244
tx_vga_gain=int(args.x),

python_hackrf/pyhackrf_tools/pyhackrf_info.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,31 @@ def pyhackrf_info(print_to_console: bool = True, initialize: bool = True) -> str
3636
for i in range(device_list.device_count):
3737
print_info += 'Found HackRF:\n'
3838
device = pyhackrf.pyhackrf_open_by_serial(device_list.serial_numbers[i])
39-
board_id, board_id_name = device.pyhackrf_board_id_read()
40-
board_rev, board_rev_name = device.pyhackrf_board_rev_read()
41-
read_partid_serialno = device.pyhackrf_board_partid_serialno_read()
42-
print_info += f'Index: {i}\n'
43-
print_info += f'Serial number: {device_list.serial_numbers[i]}\n'
44-
print_info += f'Board ID Number: {board_id} ({board_id_name})\n'
45-
print_info += f'Firmware Version: {device.pyhackrf_version_string_read()} ({device.pyhackrf_usb_api_version_read()})\n'
46-
print_info += f'Part ID Number: 0x{read_partid_serialno[0][0]:08x} 0x{read_partid_serialno[0][1]:08x}\n'
47-
if board_rev not in {0xFE, 0xFF}:
48-
print_info += f'Hardware Revision: {board_rev_name}\n'
49-
if board_rev > 0:
50-
if (board_rev & 0x80):
51-
print_info += 'Hardware appears to have been manufactured by Great Scott Gadgets.\n'
52-
else:
53-
print_info += 'Hardware does not appear to have been manufactured by Great Scott Gadgets.\n'
54-
else:
55-
print_info += f'{board_rev_name}\n'
56-
operacake_boards = device.pyhackrf_get_operacake_boards()
57-
for operacake_board_address in operacake_boards:
58-
mode = device.pyhackrf_get_operacake_mode(operacake_board_address)
59-
print_info += f'Opera Cake found, address: {operacake_board_address} | switching mode: {mode}'
60-
61-
device.pyhackrf_close()
39+
if device:
40+
board_id, board_id_name = device.pyhackrf_board_id_read()
41+
board_rev, board_rev_name = device.pyhackrf_board_rev_read()
42+
read_partid_serialno = device.pyhackrf_board_partid_serialno_read()
43+
print_info += f'Index: {i}\n'
44+
print_info += f'Serial number: {device_list.serial_numbers[i]}\n'
45+
print_info += f'Board ID Number: {board_id} ({board_id_name})\n'
46+
print_info += f'Firmware Version: {device.pyhackrf_version_string_read()} ({device.pyhackrf_usb_api_version_read()})\n'
47+
print_info += f'Part ID Number: 0x{read_partid_serialno[0][0]:08x} 0x{read_partid_serialno[0][1]:08x}\n'
48+
if board_rev not in {0xFE, 0xFF}:
49+
print_info += f'Hardware Revision: {board_rev_name}\n'
50+
if board_rev > 0:
51+
if (board_rev & 0x80):
52+
print_info += 'Hardware appears to have been manufactured by Great Scott Gadgets.\n'
53+
else:
54+
print_info += 'Hardware does not appear to have been manufactured by Great Scott Gadgets.\n'
55+
else:
56+
print_info += f'{board_rev_name}\n'
57+
58+
operacake_boards = device.pyhackrf_get_operacake_boards()
59+
for operacake_board_address in operacake_boards:
60+
mode = device.pyhackrf_get_operacake_mode(operacake_board_address)
61+
print_info += f'Opera Cake found, address: {operacake_board_address} | switching mode: {mode}'
62+
63+
device.pyhackrf_close()
6264
else:
6365
print_info += 'No HackRF boards found.'
6466

@@ -73,7 +75,7 @@ def pyhackrf_info(print_to_console: bool = True, initialize: bool = True) -> str
7375
return print_info
7476

7577

76-
def pyhackrf_serial_numbers_list_info(print_to_console: bool = True, initialize: bool = True) -> tuple[int, list] | None:
78+
def pyhackrf_serial_numbers_list_info(print_to_console: bool = True, initialize: bool = True) -> tuple[int, list[str]] | None:
7779
if initialize:
7880
pyhackrf.pyhackrf_init()
7981

python_hackrf/pyhackrf_tools/pyhackrf_operacake.py

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from python_hackrf import pyhackrf
2424

2525

26-
def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
26+
def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice | None = None,
2727
serial_number: str | None = None,
2828
print_to_console: bool = True,
2929
) -> str | None:
@@ -39,8 +39,12 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
3939
device = pyhackrf.pyhackrf_open()
4040

4141
operacake_info = ''
42-
boards = device.pyhackrf_get_operacake_boards()
43-
if len(boards):
42+
boards = []
43+
44+
if device:
45+
boards = device.pyhackrf_get_operacake_boards()
46+
47+
if device and len(boards):
4448
operacake_info += 'Opera Cakes found:\n'
4549
for i in range(len(boards)):
4650
address = boards[i]
@@ -50,7 +54,8 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
5054
operacake_info = 'Opera Cakes found: None'
5155

5256
if initialize:
53-
device.pyhackrf_close()
57+
if device:
58+
device.pyhackrf_close()
5459
pyhackrf.pyhackrf_exit()
5560

5661
if print_to_console:
@@ -63,7 +68,7 @@ def pyhackrf_operacake_info(device: pyhackrf.PyHackrfDevice = None,
6368
def pyhackrf_set_operacake_mode(address: int,
6469
mode: str,
6570
serial_number: str | None = None,
66-
device: pyhackrf.PyHackrfDevice = None,
71+
device: pyhackrf.PyHackrfDevice | None = None,
6772
) -> None:
6873

6974
initialize = True if device is None else False
@@ -77,22 +82,24 @@ def pyhackrf_set_operacake_mode(address: int,
7782
device = pyhackrf.pyhackrf_open()
7883

7984
if mode == 'frequency':
80-
mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_FREQUENCY
85+
operacake_mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_FREQUENCY
8186
elif mode == 'time':
82-
mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_TIME
87+
operacake_mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_TIME
8388
else:
84-
mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_MANUAL
89+
operacake_mode = pyhackrf.py_operacake_switching_mode.OPERACAKE_MODE_MANUAL
8590

86-
device.pyhackrf_set_operacake_mode(address, mode)
91+
if device:
92+
device.pyhackrf_set_operacake_mode(address, operacake_mode)
8793

8894
if initialize:
89-
device.pyhackrf_close()
95+
if device:
96+
device.pyhackrf_close()
9097
pyhackrf.pyhackrf_exit()
9198

9299

93-
def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
100+
def pyhackrf_set_operacake_freq_ranges(freq_ranges: list[tuple[int, int, int]],
94101
serial_number: str | None = None,
95-
device: pyhackrf.PyHackrfDevice = None,
102+
device: pyhackrf.PyHackrfDevice | None = None,
96103
) -> None:
97104

98105
initialize = True if device is None else False
@@ -105,16 +112,18 @@ def pyhackrf_set_operacake_freq_ranges(freq_ranges: list,
105112
else:
106113
device = pyhackrf.pyhackrf_open()
107114

108-
device.pyhackrf_set_operacake_freq_ranges(freq_ranges)
115+
if device:
116+
device.pyhackrf_set_operacake_freq_ranges(freq_ranges)
109117

110118
if initialize:
111-
device.pyhackrf_close()
119+
if device:
120+
device.pyhackrf_close()
112121
pyhackrf.pyhackrf_exit()
113122

114123

115-
def pyhackrf_set_operacake_dwell_times(dwell_times: list,
124+
def pyhackrf_set_operacake_dwell_times(dwell_times: list[tuple[int, int]],
116125
serial_number: str | None = None,
117-
device: pyhackrf.PyHackrfDevice = None,
126+
device: pyhackrf.PyHackrfDevice | None = None,
118127
) -> None:
119128

120129
initialize = True if device is None else False
@@ -127,18 +136,20 @@ def pyhackrf_set_operacake_dwell_times(dwell_times: list,
127136
else:
128137
device = pyhackrf.pyhackrf_open()
129138

130-
device.pyhackrf_set_operacake_dwell_times(dwell_times)
139+
if device:
140+
device.pyhackrf_set_operacake_dwell_times(dwell_times)
131141

132142
if initialize:
133-
device.pyhackrf_close()
143+
if device:
144+
device.pyhackrf_close()
134145
pyhackrf.pyhackrf_exit()
135146

136147

137148
def pyhackrf_set_operacake_ports(address: int,
138149
port_a: str,
139150
port_b: str,
140151
serial_number: str | None = None,
141-
device: pyhackrf.PyHackrfDevice = None,
152+
device: pyhackrf.PyHackrfDevice | None = None,
142153
) -> None:
143154

144155
initialize = True if device is None else False
@@ -151,16 +162,18 @@ def pyhackrf_set_operacake_ports(address: int,
151162
else:
152163
device = pyhackrf.pyhackrf_open()
153164

154-
device.pyhackrf_set_operacake_ports(address, port_a, port_b)
165+
if device:
166+
device.pyhackrf_set_operacake_ports(address, port_a, port_b)
155167

156168
if initialize:
157-
device.pyhackrf_close()
169+
if device:
170+
device.pyhackrf_close()
158171
pyhackrf.pyhackrf_exit()
159172

160173

161174
def pyhackrf_operacake_gpio_test(address: int,
162175
serial_number: str | None = None,
163-
device: pyhackrf.PyHackrfDevice = None,
176+
device: pyhackrf.PyHackrfDevice | None = None,
164177
) -> None:
165178

166179
initialize = True if device is None else False
@@ -173,30 +186,34 @@ def pyhackrf_operacake_gpio_test(address: int,
173186
else:
174187
device = pyhackrf.pyhackrf_open()
175188

176-
test_result = device.pyhackrf_operacake_gpio_test(address)
177-
if test_result == 0xFFFF:
178-
print('GPIO mode disabled.')
179-
print('Remove additional add-on boards and retry.')
180-
181-
elif test_result:
182-
reg, mask = 0x7, 0x7
183-
184-
print("u2ctrl1\t%d\t%d\t%d\n", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
185-
test_result >>= 3
186-
reg = test_result & mask
187-
print("u2ctrl0\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
188-
test_result >>= 3
189-
reg = test_result & mask
190-
print("u3ctrl1\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
191-
test_result >>= 3
192-
reg = test_result & mask
193-
print("u3ctrl0\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
194-
test_result >>= 3
195-
reg = test_result & mask
196-
print("u1ctrl \t%d\t%d\t%d\n", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
189+
if device:
190+
test_result = device.pyhackrf_operacake_gpio_test(address)
191+
if test_result == 0xFFFF:
192+
print('GPIO mode disabled.')
193+
print('Remove additional add-on boards and retry.')
194+
195+
elif test_result:
196+
reg, mask = 0x7, 0x7
197+
198+
print("u2ctrl1\t%d\t%d\t%d\n", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
199+
test_result >>= 3
200+
reg = test_result & mask
201+
print("u2ctrl0\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
202+
test_result >>= 3
203+
reg = test_result & mask
204+
print("u3ctrl1\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
205+
test_result >>= 3
206+
reg = test_result & mask
207+
print("u3ctrl0\t%d\t%d\t%d", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
208+
test_result >>= 3
209+
reg = test_result & mask
210+
print("u1ctrl \t%d\t%d\t%d\n", (reg >> 2) & 1, (reg >> 1) & 1, reg & 1)
211+
else:
212+
print('GPIO test passed')
197213
else:
198-
print('GPIO test passed')
214+
print('device not found')
199215

200216
if initialize:
201-
device.pyhackrf_close()
217+
if device:
218+
device.pyhackrf_close()
202219
pyhackrf.pyhackrf_exit()

0 commit comments

Comments
 (0)