Skip to content

Commit e67f094

Browse files
committed
fixed issue on android. removed uuid
1 parent 813b461 commit e67f094

File tree

5 files changed

+62
-69
lines changed

5 files changed

+62
-69
lines changed

python_hackrf/pyhackrf_tools/pyhackrf_sweep.pyx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ cdef sweep_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buff
7979
timestamp = datetime.datetime.now()
8080
time_str = timestamp.strftime('%Y-%m-%d, %H:%M:%S.%f')
8181

82-
current_device_data = device_data[device.uuid]
82+
current_device_data = device_data[device.serialno]
8383
norm_factor = 1.0 / current_device_data['fft_size']
8484
data_length = current_device_data['fft_size'] * 2
8585
sweep_style = current_device_data['sweep_style']
@@ -111,11 +111,11 @@ cdef sweep_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buff
111111
current_device_data['one_shot'] or
112112
current_device_data['num_sweeps'] == current_device_data['sweep_count']
113113
):
114-
run_available[device.uuid] = False
114+
run_available[device.serialno] = False
115115
else:
116116
current_device_data['sweep_started'] = True
117117

118-
if not run_available[device.uuid]:
118+
if not run_available[device.serialno]:
119119
return -1
120120

121121
if not current_device_data['sweep_started']:
@@ -222,7 +222,7 @@ def pyhackrf_sweep(frequencies: list = None, sample_rate: int = 20_000_000, base
222222
else:
223223
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
224224

225-
run_available[device.uuid] = True
225+
run_available[device.serialno] = True
226226

227227
sample_rate = int(sample_rate) if int(sample_rate) in AVAILABLE_SAMPLING_RATES else 20_000_000
228228
baseband_filter_bandwidth = int(baseband_filter_bandwidth) if baseband_filter_bandwidth in AVAILABLE_BASEBAND_FILTER_BANDWIDTHS else pyhackrf.pyhackrf_compute_baseband_filter_bw(int(sample_rate * .75))
@@ -318,14 +318,14 @@ def pyhackrf_sweep(frequencies: list = None, sample_rate: int = 20_000_000, base
318318
current_device_data['start_frequency'] = int(frequencies[0] * 1e6)
319319
current_device_data['fft_size'] = fft_size
320320
current_device_data['window'] = np.hanning(fft_size)
321-
device_data[device.uuid] = current_device_data
321+
device_data[device.serialno] = current_device_data
322322

323323
device.pyhackrf_init_sweep(frequencies, num_ranges, pyhackrf.PY_BYTES_PER_BLOCK, int(TUNE_STEP * 1e6), OFFSET, current_device_data['sweep_style'])
324324
device.pyhackrf_start_rx_sweep()
325325

326326
time_start = time.time()
327327
time_prev = time.time()
328-
while device.pyhackrf_is_streaming() and run_available[device.uuid]:
328+
while device.pyhackrf_is_streaming() and run_available[device.serialno]:
329329
time.sleep(0.05)
330330
time_now = time.time()
331331
time_difference = time_now - time_prev
@@ -345,7 +345,7 @@ def pyhackrf_sweep(frequencies: list = None, sample_rate: int = 20_000_000, base
345345
current_device_data['file'].close()
346346

347347
if print_to_console:
348-
if not run_available[device.uuid]:
348+
if not run_available[device.serialno]:
349349
sys.stderr.write('\nExiting...\n')
350350
else:
351351
sys.stderr.write('\nExiting... [ pyhackrf streaming stopped ]\n')
@@ -358,8 +358,8 @@ def pyhackrf_sweep(frequencies: list = None, sample_rate: int = 20_000_000, base
358358
if print_to_console:
359359
sys.stderr.write(f'Total sweeps: {current_device_data["sweep_count"]} in {time_now - time_start:.5f} seconds ({sweep_rate :.2f} sweeps/second)\n')
360360

361-
device_data.pop(device.uuid, None)
362-
run_available.pop(device.uuid, None)
361+
device_data.pop(device.serialno, None)
362+
run_available.pop(device.serialno, None)
363363

364364
try:
365365
device.pyhackrf_close()

python_hackrf/pyhackrf_tools/pyhackrf_transfer.pyx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ def init_signals():
6969
def rx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_length: int, valid_length: int):
7070
global run_available, device_data
7171

72-
if not run_available[device.uuid]:
72+
if not run_available[device.serialno]:
7373
return -1
7474

75-
current_device_data = device_data[device.uuid]
75+
current_device_data = device_data[device.serialno]
7676

7777
current_device_data['byte_count'] += valid_length
7878
current_device_data['stream_power'] += np.sum(buffer[:valid_length].view(np.int8).astype(np.uint64, copy=False) ** 2)
@@ -91,7 +91,7 @@ def rx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
9191
current_device_data['rx_file'].write(accepted_data.tobytes())
9292

9393
if current_device_data['num_samples'] == 0:
94-
run_available[device.uuid] = False
94+
run_available[device.serialno] = False
9595
return -1
9696

9797
return 0
@@ -100,9 +100,9 @@ def rx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
100100
def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_length: int, valid_length: int):
101101
global run_available, device_data
102102

103-
current_device_data = device_data[device.uuid]
103+
current_device_data = device_data[device.serialno]
104104

105-
if current_device_data['tx_complete'] or not run_available[device.uuid]:
105+
if current_device_data['tx_complete'] or not run_available[device.serialno]:
106106
return -1, buffer, valid_length
107107

108108
to_write = buffer_length // 2
@@ -130,12 +130,12 @@ def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
130130
if current_device_data['tx_queue'].empty():
131131
if writed:
132132
current_device_data['tx_complete'] = True
133-
run_available[device.uuid] = False
133+
run_available[device.serialno] = False
134134
valid_length = writed * 2
135135
return 0, buffer, valid_length
136136
else:
137137
current_device_data['tx_complete'] = True
138-
run_available[device.uuid] = False
138+
run_available[device.serialno] = False
139139
return -1, buffer, valid_length
140140

141141
sent_data = current_device_data['tx_queue'].get_nowait()
@@ -157,7 +157,7 @@ def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
157157
writed = len(raw_data) // 8
158158
elif current_device_data['tx_file'].tell() < 1:
159159
# file is empty
160-
run_available[device.uuid] = False
160+
run_available[device.serialno] = False
161161
valid_length = 0
162162
return -1, buffer, valid_length
163163
else:
@@ -169,7 +169,7 @@ def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
169169
# limit samples
170170
if current_device_data['num_samples'] == 0:
171171
current_device_data['tx_complete'] = True
172-
run_available[device.uuid] = False
172+
run_available[device.serialno] = False
173173
valid_length = writed * 2
174174
return 0, buffer, valid_length
175175

@@ -181,7 +181,7 @@ def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
181181
# file is finished
182182
if not current_device_data['repeat_tx']:
183183
current_device_data['tx_complete'] = True
184-
run_available[device.uuid] = False
184+
run_available[device.serialno] = False
185185
valid_length = writed * 2
186186
return 0, buffer, valid_length
187187

@@ -193,7 +193,7 @@ def tx_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:], buffer_l
193193
rewrited = len(raw_data) // 8
194194
else:
195195
current_device_data['tx_complete'] = True
196-
run_available[device.uuid] = False
196+
run_available[device.serialno] = False
197197
valid_length = writed * 2
198198
return 0, buffer, valid_length
199199

@@ -209,10 +209,10 @@ def tx_complete_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:],
209209
global run_available, device_data
210210

211211
if not success:
212-
run_available[device.uuid] = False
212+
run_available[device.serialno] = False
213213
return
214214

215-
current_device_data = device_data[device.uuid]
215+
current_device_data = device_data[device.serialno]
216216

217217
current_device_data['byte_count'] += valid_length
218218
current_device_data['stream_power'] += np.sum(buffer[:valid_length].view(np.int8).astype(np.uint64, copy=False) ** 2)
@@ -221,12 +221,12 @@ def tx_complete_callback(device: pyhackrf.PyHackrfDevice, buffer: np.ndarray[:],
221221
def flush_callback(device: pyhackrf.PyHackrfDevice, success: int):
222222
global run_available, device_data
223223

224-
current_device_data = device_data[device.uuid]
224+
current_device_data = device_data[device.serialno]
225225

226226
if success:
227227
current_device_data['flush_complete'] = True
228228

229-
run_available[device.uuid] = False
229+
run_available[device.serialno] = False
230230

231231

232232
def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, baseband_filter_bandwidth: int = None, i_frequency: int = None, lo_frequency: int = None, image_reject: pyhackrf.py_rf_path_filter = pyhackrf.py_rf_path_filter.RF_PATH_FILTER_BYPASS,
@@ -245,7 +245,7 @@ def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, base
245245
else:
246246
device = pyhackrf.pyhackrf_open_by_serial(serial_number)
247247

248-
run_available[device.uuid] = True
248+
run_available[device.serialno] = True
249249

250250
sample_rate = int(sample_rate) if int(sample_rate) in AVAILABLE_SAMPLING_RATES else 10_000_000
251251
baseband_filter_bandwidth = int(baseband_filter_bandwidth) if baseband_filter_bandwidth in AVAILABLE_BASEBAND_FILTER_BANDWIDTHS else pyhackrf.pyhackrf_compute_baseband_filter_bw(int(sample_rate * .75))
@@ -266,7 +266,7 @@ def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, base
266266
'rx_queue': rx_queue,
267267
'tx_queue': tx_queue
268268
}
269-
device_data[device.uuid] = current_device_data
269+
device_data[device.serialno] = current_device_data
270270

271271
if (rx_queue is not None or rx_filename is not None) and (tx_queue is not None or tx_filename is not None):
272272
raise RuntimeError('HackRF cannot receive and send IQ samples at the same time.')
@@ -373,7 +373,7 @@ def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, base
373373

374374
time_start = time.time()
375375
time_prev = time.time()
376-
while run_available[device.uuid]:
376+
while run_available[device.serialno]:
377377
time.sleep(0.05)
378378
time_now = time.time()
379379
time_difference = time_now - time_prev
@@ -396,7 +396,7 @@ def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, base
396396

397397
time_now = time.time()
398398
if print_to_console:
399-
if not run_available[device.uuid]:
399+
if not run_available[device.serialno]:
400400
sys.stderr.write('\nExiting...\n')
401401
else:
402402
sys.stderr.write('\nExiting... [ pyhackrf streaming stopped ]\n')
@@ -427,8 +427,8 @@ def pyhackrf_transfer(frequency: int = None, sample_rate: int = 10_000_000, base
427427
except RuntimeError as e:
428428
sys.stderr.write(f'{e}\n')
429429

430-
device_data.pop(device.uuid, None)
431-
run_available.pop(device.uuid, None)
430+
device_data.pop(device.serialno, None)
431+
run_available.pop(device.serialno, None)
432432

433433
try:
434434
device.pyhackrf_close()

python_hackrf/pylibhackrf/__android.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from threading import Event
2424

2525
try:
26-
from jnius import ( # type: ignore
26+
from jnius import (
2727
autoclass,
2828
cast,
2929
)
@@ -35,7 +35,7 @@ def cast(item):
3535
raise RuntimeError('cast not available')
3636

3737
try:
38-
from android.broadcast import BroadcastReceiver # type: ignore
38+
from android.broadcast import BroadcastReceiver
3939
except ImportError:
4040
def BroadcastReceiver(item):
4141
raise RuntimeError('BroadcastReceiver not available')
@@ -45,18 +45,18 @@ def BroadcastReceiver(item):
4545

4646

4747
class USBBroadcastReceiver:
48-
def __init__(self, events):
48+
def __init__(self, events: dict) -> None:
4949
self.usb_action_permission = 'libusb.android.USB_PERMISSION'
5050
self.events = events
5151

52-
def start(self):
52+
def start(self) -> None:
5353
self.br = BroadcastReceiver(self.on_broadcast, actions=[self.usb_action_permission])
5454
self.br.start()
5555

56-
def stop(self):
56+
def stop(self) -> None:
5757
self.br.stop()
5858

59-
def on_broadcast(self, context, intent):
59+
def on_broadcast(self, context, intent) -> None:
6060
action = intent.getAction()
6161
UsbManager = autoclass('android.hardware.usb.UsbManager')
6262
if action == self.usb_action_permission:
@@ -72,9 +72,9 @@ def on_broadcast(self, context, intent):
7272
self.events[device_name]['event'].set()
7373

7474

75-
def get_usb_devices_info(num_devices: int = None) -> list:
75+
def get_hackrf_device_list(num_devices: int | None = None) -> list:
7676
events = {}
77-
devices_info = []
77+
hackrf_device_list = []
7878

7979
Context = autoclass('android.content.Context')
8080
PendingIntent = autoclass('android.app.PendingIntent')
@@ -97,7 +97,7 @@ def get_usb_devices_info(num_devices: int = None) -> list:
9797
if usb_manager.hasPermission(usb_device):
9898
usb_device_connection = usb_manager.openDevice(usb_device)
9999
file_descriptor = usb_device_connection.getFileDescriptor()
100-
devices_info.append((file_descriptor, usb_device.getProductId(), usb_device.getSerialNumber()))
100+
hackrf_device_list.append((file_descriptor, usb_device.getProductId(), usb_device.getSerialNumber()))
101101
else:
102102
permission_intent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, autoclass('android.content.Intent')(usb_action_permission), PendingIntent.FLAG_MUTABLE)
103103
events[device_name] = {'event': Event(), 'granted': False, 'device': None}
@@ -108,15 +108,15 @@ def get_usb_devices_info(num_devices: int = None) -> list:
108108

109109
if len(events):
110110
usb_broadcast_receiver.start()
111-
for device_name, info in events.items():
111+
for _, info in events.items():
112112
info['event'].wait()
113113
usb_broadcast_receiver.stop()
114114

115-
for device_name, info in events.items():
115+
for _, info in events.items():
116116
if info['granted']:
117117
usb_device = info['device']
118118
usb_device_connection = usb_manager.openDevice(usb_device)
119119
file_descriptor = usb_device_connection.getFileDescriptor()
120-
devices_info.append((file_descriptor, usb_device.getProductId(), usb_device.getSerialNumber()))
120+
hackrf_device_list.append((file_descriptor, usb_device.getProductId(), usb_device.getSerialNumber()))
121121

122-
return devices_info
122+
return hackrf_device_list

python_hackrf/pylibhackrf/pyhackrf.pyx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ from . cimport chackrf
3030
from enum import IntEnum
3131
cimport numpy as np
3232
import numpy as np
33-
import uuid
3433

3534
PY_BYTES_PER_BLOCK = chackrf.BYTES_PER_BLOCK
3635
PY_MAX_SWEEP_RANGES = chackrf.MAX_SWEEP_RANGES
@@ -144,16 +143,15 @@ cdef class PyHackRFDeviceList:
144143
def __get__(self):
145144
if self.__hackrf_device_list is not NULL:
146145
return self.__hackrf_device_list[0].devicecount
146+
return 0
147147

148148
property serial_numbers:
149149
def __get__(self):
150-
if self.__hackrf_device_list is not NULL:
151-
return [self.__hackrf_device_list[0].serial_numbers[i].decode('utf-8') for i in range(self.__hackrf_device_list[0].devicecount)]
150+
return [self.__hackrf_device_list[0].serial_numbers[i].decode('utf-8') for i in range(self.device_count)]
152151

153152
property usb_board_ids:
154153
def __get__(self):
155-
if self.__hackrf_device_list is not NULL:
156-
return [self.__hackrf_device_list[0].usb_board_ids[i] for i in range(self.__hackrf_device_list[0].devicecount)]
154+
return [self.__hackrf_device_list[0].usb_board_ids[i] for i in range(self.device_count)]
157155

158156
def pyhackrf_board_id_name(self, index: int) -> str:
159157
if self.__hackrf_device_list is not NULL:
@@ -163,10 +161,10 @@ cdef class PyHackrfDevice:
163161

164162
cdef chackrf.hackrf_device* __hackrf_device
165163
cdef list __pyoperacakes
166-
cdef public str uuid
164+
cdef public str serialno
167165

168166
def __cinit__(self):
169-
self.uuid = str(uuid.uuid4())
167+
self.serialno = self.pyhackrf_serialno_read()
170168
self.__hackrf_device = NULL
171169
self.__pyoperacakes = []
172170

0 commit comments

Comments
 (0)