def test_pack_complex(self): hkrf = HackRF(1, 1, 1, 1) print( hkrf.pack_complex( np.array( [complex(0.1, 0.1), complex(0.5, 0.1)], dtype=np.complex64)))
def test_hackrf_class_send(self): hfc = HackRF(433.92e6, 1e6, 1e6, 20) hfc.start_tx_mode(np.fromfile("/tmp/hackrf.complex", dtype=np.complex64), repeats=1) while not hfc.sending_finished: print("Repeat: {0} Current Sample: {1}/{2}".format(hfc.current_sending_repeat+1, hfc.current_sent_sample, len(hfc.samples_to_send))) time.sleep(1) hfc.stop_tx_mode("Test finished")
def test_hackrf_class_send(self): hfc = HackRF(1e6, 433.92e6, 20, 1e6) hfc.open() hfc.start_tx_mode(np.fromfile("/tmp/hackrf.complex", dtype=np.complex64), repeats=1) while not hfc.sending_finished: print("Repeat: {0} Current Sample: {1}".format( hfc.current_sending_repeat + 1, hfc.current_sent_sample)) time.sleep(1) hfc.stop_tx_mode("Test finished") hfc.close()
def test_hackrf_class_recv(self): hfc = HackRF(433.92e6, 1e6, 1e6, 20) hfc.start_rx_mode() i = 0 TIME_TOTAL = 5 while i <TIME_TOTAL: print("{0}/{1}".format(i+1, TIME_TOTAL)) time.sleep(1) i+=1 print("{0:,}".format(hfc.current_recv_index)) hfc.received_data.tofile("/tmp/hackrf.complex") print("Wrote Data") hfc.stop_rx_mode("Finished test")
def test_hackrf_class_recv(self): hfc = HackRF(1e6, 433.92e6, 20, 1e6) hfc.open() hfc.start_rx_mode() i = 0 TIME_TOTAL = 5 while i < TIME_TOTAL: print("{0}/{1}".format(i + 1, TIME_TOTAL)) time.sleep(1) i += 1 print("{0:,}".format(hfc.current_recv_index)) #print(hfc.received_data) #hfc.unpack_complex(len(hfc.byte_buffer)//2).tofile("/tmp/hackrf.complex") hfc.received_data.tofile("/tmp/hackrf.complex") print("Wrote Data") hfc.stop_rx_mode("Finished test") hfc.close()
def test_hackrf_pack_unpack(self): arr = np.array([-128, -128, -0.5, -0.5, -3, -3, 127, 127], dtype=np.int8) self.assertEqual(arr[0], -128) self.assertEqual(arr[1], -128) self.assertEqual(arr[-1], 127) received = arr.tostring() self.assertEqual(len(received), len(arr)) self.assertEqual(np.int8(received[0]), -128) self.assertEqual(np.int8(received[1]), -128) unpacked = HackRF.unpack_complex(received, len(received) // 2) self.assertEqual(unpacked[0], complex(-1, -1)) self.assertAlmostEqual(unpacked[1], complex(0, 0), places=1) self.assertAlmostEqual(unpacked[2], complex(0, 0), places=1) self.assertEqual(unpacked[3], complex(1, 1)) packed = HackRF.pack_complex(unpacked) self.assertEqual(received, packed)
def test_hackrf_class_send(self): hfc = HackRF(433.92e6, 1e6, 1e6, 20) hfc.start_tx_mode(np.fromfile(os.path.join(tempfile.gettempdir(), "hackrf.complex"), dtype=np.complex64), repeats=1) while not hfc.sending_finished: print("Repeat: {0} Current Sample: {1}/{2}".format( hfc.current_sending_repeat + 1, hfc.current_sent_sample, len(hfc.samples_to_send))) time.sleep(1) hfc.stop_tx_mode("Test finished")
def __init__(self, backend_handler, name: str, mode: Mode, freq=None, sample_rate=None, bandwidth=None, gain=None, if_gain=None, baseband_gain=None, samples_to_send=None, device_ip=None, sending_repeats=1, parent=None, resume_on_full_receive_buffer=False, raw_mode=True, portnumber=1234): super().__init__(parent) self.name = name self.mode = mode self.backend_handler = backend_handler freq = config.DEFAULT_FREQUENCY if freq is None else freq sample_rate = config.DEFAULT_SAMPLE_RATE if sample_rate is None else sample_rate bandwidth = config.DEFAULT_BANDWIDTH if bandwidth is None else bandwidth gain = config.DEFAULT_GAIN if gain is None else gain if_gain = config.DEFAULT_IF_GAIN if if_gain is None else if_gain baseband_gain = config.DEFAULT_BB_GAIN if baseband_gain is None else baseband_gain resume_on_full_receive_buffer = self.mode == Mode.spectrum or resume_on_full_receive_buffer if self.name == NetworkSDRInterfacePlugin.NETWORK_SDR_NAME: self.backend = Backends.network else: try: self.backend = self.backend_handler.device_backends[ name.lower()].selected_backend except KeyError: logger.warning("Invalid device name: {0}".format(name)) self.backend = Backends.none self.__dev = None return if self.backend == Backends.grc: if mode == Mode.receive: from urh.dev.gr.ReceiverThread import ReceiverThread self.__dev = ReceiverThread( freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent, resume_on_full_receive_buffer=resume_on_full_receive_buffer ) elif mode == Mode.send: from urh.dev.gr.SenderThread import SenderThread self.__dev = SenderThread(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent) self.__dev.data = samples_to_send self.__dev.samples_per_transmission = len( samples_to_send) if samples_to_send is not None else 2**15 elif mode == Mode.spectrum: from urh.dev.gr.SpectrumThread import SpectrumThread self.__dev = SpectrumThread(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent) else: raise ValueError("Unknown mode") self.__dev.device = name self.__dev.started.connect(self.emit_started_signal) self.__dev.stopped.connect(self.emit_stopped_signal) self.__dev.sender_needs_restart.connect( self.emit_sender_needs_restart) elif self.backend == Backends.native: name = self.name.lower() if name in map(str.lower, BackendHandler.DEVICE_NAMES): if name == "hackrf": from urh.dev.native.HackRF import HackRF self.__dev = HackRF(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, resume_on_full_receive_buffer) elif name.replace("-", "") == "rtlsdr": from urh.dev.native.RTLSDR import RTLSDR self.__dev = RTLSDR(freq, gain, sample_rate, device_number=0, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name.replace("-", "") == "rtltcp": from urh.dev.native.RTLSDRTCP import RTLSDRTCP self.__dev = RTLSDRTCP(freq, gain, sample_rate, bandwidth, device_number=0, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name == "limesdr": from urh.dev.native.LimeSDR import LimeSDR self.__dev = LimeSDR(freq, gain, sample_rate, bandwidth, gain, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name.startswith("airspy"): from urh.dev.native.AirSpy import AirSpy self.__dev = AirSpy(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name.startswith("usrp"): from urh.dev.native.USRP import USRP self.__dev = USRP(freq, gain, sample_rate, bandwidth, gain, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name.startswith("sdrplay"): from urh.dev.native.SDRPlay import SDRPlay self.__dev = SDRPlay(freq, gain, bandwidth, gain, if_gain=if_gain, resume_on_full_receive_buffer= resume_on_full_receive_buffer) elif name == "soundcard": from urh.dev.native.SoundCard import SoundCard self.__dev = SoundCard(sample_rate, resume_on_full_receive_buffer= resume_on_full_receive_buffer) else: raise NotImplementedError( "Native Backend for {0} not yet implemented".format( name)) elif name == "test": # For Unittests Only self.__dev = Device(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, resume_on_full_receive_buffer) else: raise ValueError("Unknown device name {0}".format(name)) self.__dev.portnumber = portnumber self.__dev.device_ip = device_ip if mode == Mode.send: self.__dev.init_send_parameters(samples_to_send, sending_repeats) elif self.backend == Backends.network: self.__dev = NetworkSDRInterfacePlugin( raw_mode=raw_mode, resume_on_full_receive_buffer=resume_on_full_receive_buffer, spectrum=self.mode == Mode.spectrum, sending=self.mode == Mode.send) self.__dev.send_connection_established.connect( self.emit_ready_for_action) self.__dev.receive_server_started.connect( self.emit_ready_for_action) self.__dev.error_occurred.connect(self.emit_fatal_error_occurred) self.__dev.samples_to_send = samples_to_send elif self.backend == Backends.none: self.__dev = None else: raise ValueError("Unsupported Backend") if hasattr(self.__dev, "data_received"): self.__dev.data_received.connect(self.data_received.emit) if mode == Mode.spectrum: self.__dev.is_in_spectrum_mode = True
def __init__(self, backend_handler, name: str, mode: Mode, bw, freq, gain, samp_rate, samples_to_send=None, device_ip=None, sending_repeats=1, parent=None, is_ringbuffer=False, raw_mode=True, portnumber=1234): super().__init__(parent) self.name = name self.mode = mode self.backend_handler = backend_handler if self.name == NetworkSDRInterfacePlugin.NETWORK_SDR_NAME: self.backend = Backends.network else: try: self.backend = self.backend_handler.device_backends[ name.lower()].selected_backend except KeyError: logger.warning("Invalid device name: {0}".format(name)) self.backend = Backends.none self.__dev = None return if self.backend == Backends.grc: if mode == Mode.receive: self.__dev = ReceiverThread(samp_rate, freq, gain, bw, parent=parent, is_ringbuffer=is_ringbuffer) self.__dev.index_changed.connect(self.emit_index_changed) elif mode == Mode.send: self.__dev = SenderThread(samp_rate, freq, gain, bw, parent=parent) self.__dev.data = samples_to_send self.__dev.samples_per_transmission = len(samples_to_send) elif mode == Mode.spectrum: self.__dev = SpectrumThread(samp_rate, freq, gain, bw, parent=parent) else: raise ValueError("Unknown mode") self.__dev.usrp_ip = device_ip self.__dev.device = name self.__dev.started.connect(self.emit_started_signal) self.__dev.stopped.connect(self.emit_stopped_signal) self.__dev.sender_needs_restart.connect( self.emit_sender_needs_restart) elif self.backend == Backends.native: name = self.name.lower() if name in map(str.lower, BackendHandler.DEVICE_NAMES): is_ringbuffer = self.mode == Mode.spectrum or is_ringbuffer if name == "hackrf": from urh.dev.native.HackRF import HackRF self.__dev = HackRF(bw, freq, gain, samp_rate, is_ringbuffer=is_ringbuffer) elif name.replace("-", "") == "rtlsdr": from urh.dev.native.RTLSDR import RTLSDR self.__dev = RTLSDR(freq, gain, samp_rate, device_number=0, is_ringbuffer=is_ringbuffer) elif name.replace("-", "") == "rtltcp": from urh.dev.native.RTLSDRTCP import RTLSDRTCP self.__dev = RTLSDRTCP(freq, gain, samp_rate, device_number=0, is_ringbuffer=is_ringbuffer) else: raise NotImplementedError( "Native Backend for {0} not yet implemented".format( name)) else: raise ValueError("Unknown device name {0}".format(name)) self.__dev.portnumber = portnumber self.__dev.device_ip = device_ip self.__dev.rcv_index_changed.connect(self.emit_index_changed) if mode == Mode.send: self.__dev.init_send_parameters(samples_to_send, sending_repeats, skip_device_parameters=True) elif self.backend == Backends.network: self.__dev = NetworkSDRInterfacePlugin(raw_mode=raw_mode) self.__dev.rcv_index_changed.connect(self.emit_index_changed) self.__dev.samples_to_send = samples_to_send elif self.backend == Backends.none: self.__dev = None else: raise ValueError("Unsupported Backend")
def test_hackrf_class_send_recv(self): while True: t = time.time() hfc = HackRF(1e6, 433.92e6, 20, 1e6) hfc.open() print("opening time:", time.time() - t) hfc.start_rx_mode() i = 0 t = time.time() while i < 1: #print("{0}/{1}".format(i+1, 5)) time.sleep(1) i += 1 print("{0:,}".format(hfc.current_recv_index)) rcv_data = hfc.received_data print("Rcv done {0}".format(time.time() - t)) t = time.time() hfc.stop_rx_mode("Finished test") print(" Time stopping rx mode: {0}".format( 1000 * (time.time() - t))) hfc.open(init=False) print(" Reopen device: {0}".format(1000 * (time.time() - t))) hfc.start_tx_mode(rcv_data, repeats=1) print(" Switch time:", 1000 * (time.time() - t), "ms") t = time.time() while not hfc.sending_finished: #print("Repeat: {0} Current Sample: {1}".format(hfc.current_sending_repeat + 1, hfc.current_sent_sample)) time.sleep(0.01) print("Send time", time.time() - t) t = time.time() hfc.stop_tx_mode("Test finished") #hfc.close() print("Close time", time.time() - t) hfc.close()
# import pyximport # # # pyximport.install() from urh.dev.native.lib import hackrf from urh.dev.native.HackRF import HackRF hfc = HackRF(433.92e6, 1e6, 1e6, 20) hfc.start_rx_mode() # # from urh.dev.native.RTLSDR import RTLSDR # rtlsdr = RTLSDR(433.92e6, 20, 2e6, device_number=0) # rtlsdr.start_rx_mode()
def __init__(self, backend_handler, name: str, mode: Mode, freq=None, sample_rate=None, bandwidth=None, gain=None, if_gain=None, baseband_gain=None, samples_to_send=None, device_ip=None, sending_repeats=1, parent=None, is_ringbuffer=False, raw_mode=True, portnumber=1234): super().__init__(parent) self.name = name self.mode = mode self.backend_handler = backend_handler freq = config.DEFAULT_FREQUENCY if freq is None else freq sample_rate = config.DEFAULT_SAMPLE_RATE if sample_rate is None else sample_rate bandwidth = config.DEFAULT_BANDWIDTH if bandwidth is None else bandwidth gain = config.DEFAULT_GAIN if gain is None else gain if_gain = config.DEFAULT_IF_GAIN if if_gain is None else if_gain baseband_gain = config.DEFAULT_BB_GAIN if baseband_gain is None else baseband_gain if self.name == NetworkSDRInterfacePlugin.NETWORK_SDR_NAME: self.backend = Backends.network else: try: self.backend = self.backend_handler.device_backends[name.lower()].selected_backend except KeyError: logger.warning("Invalid device name: {0}".format(name)) self.backend = Backends.none self.__dev = None return if self.backend == Backends.grc: if mode == Mode.receive: from urh.dev.gr.ReceiverThread import ReceiverThread self.__dev = ReceiverThread(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent, is_ringbuffer=is_ringbuffer) self.__dev.index_changed.connect(self.emit_index_changed) elif mode == Mode.send: from urh.dev.gr.SenderThread import SenderThread self.__dev = SenderThread(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent) self.__dev.data = samples_to_send self.__dev.samples_per_transmission = len(samples_to_send) elif mode == Mode.spectrum: from urh.dev.gr.SpectrumThread import SpectrumThread self.__dev = SpectrumThread(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, parent=parent) else: raise ValueError("Unknown mode") self.__dev.device = name self.__dev.started.connect(self.emit_started_signal) self.__dev.stopped.connect(self.emit_stopped_signal) self.__dev.sender_needs_restart.connect(self.emit_sender_needs_restart) elif self.backend == Backends.native: name = self.name.lower() if name in map(str.lower, BackendHandler.DEVICE_NAMES): is_ringbuffer = self.mode == Mode.spectrum or is_ringbuffer if name == "hackrf": from urh.dev.native.HackRF import HackRF self.__dev = HackRF(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, is_ringbuffer) elif name.replace("-", "") == "rtlsdr": from urh.dev.native.RTLSDR import RTLSDR self.__dev = RTLSDR(freq, gain, sample_rate, device_number=0, is_ringbuffer=is_ringbuffer) elif name.replace("-", "") == "rtltcp": from urh.dev.native.RTLSDRTCP import RTLSDRTCP self.__dev = RTLSDRTCP(freq, gain, sample_rate, device_number=0, is_ringbuffer=is_ringbuffer) else: raise NotImplementedError("Native Backend for {0} not yet implemented".format(name)) elif name == "test": # For Unittests Only self.__dev = Device(freq, sample_rate, bandwidth, gain, if_gain, baseband_gain, is_ringbuffer) self.__dev.BYTES_PER_SAMPLE = 4 else: raise ValueError("Unknown device name {0}".format(name)) self.__dev.portnumber = portnumber self.__dev.device_ip = device_ip self.__dev.rcv_index_changed.connect(self.emit_index_changed) if mode == Mode.send: self.__dev.init_send_parameters(samples_to_send, sending_repeats, skip_device_parameters=True) elif self.backend == Backends.network: self.__dev = NetworkSDRInterfacePlugin(raw_mode=raw_mode, spectrum=self.mode == Mode.spectrum) self.__dev.rcv_index_changed.connect(self.emit_index_changed) self.__dev.samples_to_send = samples_to_send elif self.backend == Backends.none: self.__dev = None else: raise ValueError("Unsupported Backend")