Add URH
This commit is contained in:
		
							
								
								
									
										130
									
								
								Software/urh/tests/device/HackRFTests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								Software/urh/tests/device/HackRFTests.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,130 @@
 | 
			
		||||
import time
 | 
			
		||||
import unittest
 | 
			
		||||
import os
 | 
			
		||||
import tempfile
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
 | 
			
		||||
from urh.util import util
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import hackrf
 | 
			
		||||
from urh.dev.native.HackRF import HackRF
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestHackRF(unittest.TestCase):
 | 
			
		||||
    def callback_fun(self, buffer):
 | 
			
		||||
        print(buffer)
 | 
			
		||||
        for i in range(0, len(buffer), 4):
 | 
			
		||||
            try:
 | 
			
		||||
                r = np.fromstring(buffer[i:i + 2], dtype=np.float16) / 32767.5
 | 
			
		||||
                i = np.fromstring(buffer[i + 2:i + 4], dtype=np.float16) / 32767.5
 | 
			
		||||
            except ValueError:
 | 
			
		||||
                continue
 | 
			
		||||
            if r and i:
 | 
			
		||||
                print(r, i)
 | 
			
		||||
                # out.append(complex(float(buffer[i:i+1])/32767.5, float(buffer[i+2:i+3])/32767.5))
 | 
			
		||||
 | 
			
		||||
        return 0
 | 
			
		||||
 | 
			
		||||
    def test_fromstring(self):
 | 
			
		||||
        buffer = b'\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xff\xfd\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe\xfd\xfe\xff\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe'
 | 
			
		||||
        r = np.empty(len(buffer) // 2, dtype=np.float32)
 | 
			
		||||
        i = np.empty(len(buffer) // 2, dtype=np.float32)
 | 
			
		||||
        c = np.empty(len(buffer) // 2, dtype=np.complex64)
 | 
			
		||||
 | 
			
		||||
        # dtype  =
 | 
			
		||||
        unpacked = np.frombuffer(buffer, dtype=[('r', np.uint8), ('i', np.uint8)])
 | 
			
		||||
        ru = unpacked['r'] / 128.0
 | 
			
		||||
        iu = unpacked['i'] / 128.0
 | 
			
		||||
 | 
			
		||||
        # for j in range(0, len(buffer)-1, 2):
 | 
			
		||||
        #    r[j//2] = np.frombuffer(buffer[j:j + 1], dtype=np.int8) / 128.0
 | 
			
		||||
        #    i[j//2] = np.frombuffer(buffer[j + 1:j + 2], dtype=np.int8) / 128.0
 | 
			
		||||
        # r2 = np.fromstring(buffer[], dtype=np.float16) / 32767.5
 | 
			
		||||
        c.real = ru
 | 
			
		||||
        c.imag = iu
 | 
			
		||||
        print(c)
 | 
			
		||||
        # x,y = np.frombuffer(buffer, dtype=[('x', np.float16), ('y', np.float16)])
 | 
			
		||||
 | 
			
		||||
    def test_fromstring2(self):
 | 
			
		||||
        buffer = b'\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xff\xfd\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe\xfd\xfe\xff\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe'
 | 
			
		||||
        c = np.empty(len(buffer) // 2, dtype=np.complex64)
 | 
			
		||||
 | 
			
		||||
        # dtype  =
 | 
			
		||||
        unpacked = np.frombuffer(buffer, dtype="<h") # cast in short
 | 
			
		||||
        print(unpacked)
 | 
			
		||||
        f = 1.0/32767.5
 | 
			
		||||
        for i in range(0, len(unpacked)-1,2):
 | 
			
		||||
            c[i] = complex(float(unpacked[i]*f), float(unpacked[i+1]*f))
 | 
			
		||||
 | 
			
		||||
        print(c)
 | 
			
		||||
        # x,y = np.frombuffer(buffer, dtype=[('x', np.float16), ('y', np.float16)])
 | 
			
		||||
 | 
			
		||||
    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(os.path.join(tempfile.gettempdir(), "hackrf.complex"))
 | 
			
		||||
        print("Wrote Data")
 | 
			
		||||
        hfc.stop_rx_mode("Finished test")
 | 
			
		||||
 | 
			
		||||
    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 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.bytes_to_iq(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.iq_to_bytes(unpacked)
 | 
			
		||||
        self.assertEqual(received, packed)
 | 
			
		||||
 | 
			
		||||
    def test_c_api(self):
 | 
			
		||||
        def callback(n):
 | 
			
		||||
            print("called")
 | 
			
		||||
            return np.array([1], dtype=np.complex64)
 | 
			
		||||
 | 
			
		||||
        print("init", hackrf.init())
 | 
			
		||||
        print("open", hackrf.open())
 | 
			
		||||
 | 
			
		||||
        print("start_tx", hackrf.start_tx_mode(callback))
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
 | 
			
		||||
        print("stop_tx", hackrf.stop_tx_mode())
 | 
			
		||||
 | 
			
		||||
        print("close", hackrf.close())
 | 
			
		||||
        print("exit", hackrf.exit())
 | 
			
		||||
 | 
			
		||||
    def test_device_list(self):
 | 
			
		||||
        print(hackrf.get_device_list())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										56
									
								
								Software/urh/tests/device/TestAirSpy.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Software/urh/tests/device/TestAirSpy.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
from multiprocessing import Queue, Pipe
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import airspy
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestAirSpy(unittest.TestCase):
 | 
			
		||||
    def test_cython_wrapper(self):
 | 
			
		||||
        result = airspy.open()
 | 
			
		||||
        print("Open:", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        sample_rates = airspy.get_sample_rates()
 | 
			
		||||
        print("Samples rates:", sample_rates)
 | 
			
		||||
 | 
			
		||||
        result = airspy.set_sample_rate(10**6)
 | 
			
		||||
        print("Set sample rate", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        result = airspy.set_center_frequency(int(433.92e6))
 | 
			
		||||
        print("Set center frequency", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        result = airspy.set_if_rx_gain(5)
 | 
			
		||||
        print("Set lna gain", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        result = airspy.set_rf_gain(8)
 | 
			
		||||
        print("Set mixer gain", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        result = airspy.set_baseband_gain(10)
 | 
			
		||||
        print("Set vga gain", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
 | 
			
		||||
        result = airspy.start_rx(child_conn.send_bytes)
 | 
			
		||||
        print("Set start rx", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        time.sleep(0.01)
 | 
			
		||||
        print(np.fromstring(parent_conn.recv_bytes(8*65536), dtype=np.complex64))
 | 
			
		||||
 | 
			
		||||
        print("Closing")
 | 
			
		||||
 | 
			
		||||
        parent_conn.close()
 | 
			
		||||
        child_conn.close()
 | 
			
		||||
 | 
			
		||||
        result = airspy.stop_rx()
 | 
			
		||||
        print("Set stop rx", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
        result = airspy.close()
 | 
			
		||||
        print("Close:", airspy.error_name(result), result)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										69
									
								
								Software/urh/tests/device/TestBladeRF.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								Software/urh/tests/device/TestBladeRF.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
import time
 | 
			
		||||
from multiprocessing.connection import Pipe
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.BladeRF import BladeRF
 | 
			
		||||
from urh.util import util
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import bladerf
 | 
			
		||||
 | 
			
		||||
class TestBladeRF(unittest.TestCase):
 | 
			
		||||
    def test_version(self):
 | 
			
		||||
        bladerf.get_api_version()
 | 
			
		||||
 | 
			
		||||
    def test_cython_wrapper(self):
 | 
			
		||||
        serials = bladerf.get_device_list()
 | 
			
		||||
        print("Connected serials", serials)
 | 
			
		||||
 | 
			
		||||
        bladerf.open()
 | 
			
		||||
 | 
			
		||||
        bladerf.set_tx(True)
 | 
			
		||||
        bladerf.set_channel(0)
 | 
			
		||||
        print("set gain", bladerf.set_gain(20))
 | 
			
		||||
        print("set gain", bladerf.set_gain(21))
 | 
			
		||||
 | 
			
		||||
        bladerf.set_tx(False)
 | 
			
		||||
        bladerf.set_channel(1)
 | 
			
		||||
        print("Sample Rate", bladerf.get_sample_rate())
 | 
			
		||||
        print("Set sample rate to 2e6", bladerf.set_sample_rate(int(2e6)))
 | 
			
		||||
        print("sample rate", bladerf.get_sample_rate())
 | 
			
		||||
        print("Set sample rate to 40e6", bladerf.set_sample_rate(int(40e6)))
 | 
			
		||||
        print("sample rate", bladerf.get_sample_rate())
 | 
			
		||||
        print("Set sample rate to 200e6", bladerf.set_sample_rate(int(200e6)))
 | 
			
		||||
        print("sample rate", bladerf.get_sample_rate())
 | 
			
		||||
 | 
			
		||||
        bladerf.set_tx(True)
 | 
			
		||||
        bladerf.set_channel(1)
 | 
			
		||||
        print("Bandwidth", bladerf.get_bandwidth())
 | 
			
		||||
        print("Set Bandwidth to 2e6", bladerf.set_bandwidth(int(2e6)))
 | 
			
		||||
        print("Bandwidth", bladerf.get_bandwidth())
 | 
			
		||||
 | 
			
		||||
        bladerf.set_tx(False)
 | 
			
		||||
        bladerf.set_channel(0)
 | 
			
		||||
        print("Frequency", bladerf.get_center_freq())
 | 
			
		||||
        print("Set Frequency to 433.92e6", bladerf.set_center_freq(int(433.92e6)))
 | 
			
		||||
        print("Frequency", bladerf.get_center_freq())
 | 
			
		||||
 | 
			
		||||
        bladerf.prepare_sync()
 | 
			
		||||
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
 | 
			
		||||
        for i in range(3):
 | 
			
		||||
            bladerf.receive_sync(child_conn, 4096)
 | 
			
		||||
            data = parent_conn.recv_bytes()
 | 
			
		||||
            print(data)
 | 
			
		||||
 | 
			
		||||
        bladerf.close()
 | 
			
		||||
 | 
			
		||||
        bladerf.open()
 | 
			
		||||
        bladerf.set_tx(True)
 | 
			
		||||
        bladerf.set_channel(0)
 | 
			
		||||
        bladerf.prepare_sync()
 | 
			
		||||
 | 
			
		||||
        for i in range(10):
 | 
			
		||||
            print("Send", bladerf.send_sync(np.fromstring(data, dtype=np.int16)))
 | 
			
		||||
        bladerf.close()
 | 
			
		||||
							
								
								
									
										112
									
								
								Software/urh/tests/device/TestLimeSDR.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Software/urh/tests/device/TestLimeSDR.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
import unittest
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
from multiprocessing import Pipe
 | 
			
		||||
f = os.readlink(__file__) if os.path.islink(__file__) else __file__
 | 
			
		||||
path = os.path.realpath(os.path.join(f, "..", "..", "src"))
 | 
			
		||||
 | 
			
		||||
if path not in sys.path:
 | 
			
		||||
    sys.path.insert(0, path)
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
 | 
			
		||||
from urh.util import util
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import limesdr
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestLimeSDR(unittest.TestCase):
 | 
			
		||||
    def test_cython_wrapper(self):
 | 
			
		||||
        print("Devices:", limesdr.get_device_list())
 | 
			
		||||
        # print("Open:", limesdr.open("LimeSDR-USB, media=USB 3.0, module=STREAM, addr=1d50:6108, serial=0009060B0049180A"))
 | 
			
		||||
        print("Open:", limesdr.open())
 | 
			
		||||
        print("-" * 20)
 | 
			
		||||
 | 
			
		||||
        print("Init", limesdr.init())
 | 
			
		||||
        limesdr.set_tx(True)
 | 
			
		||||
        self.assertTrue(limesdr.get_tx())
 | 
			
		||||
        #print(limesdr.IS_TX)
 | 
			
		||||
        print("Num Channels TX:", limesdr.get_num_channels())
 | 
			
		||||
        print("TX antennas", limesdr.get_antenna_list())
 | 
			
		||||
        limesdr.set_tx(False)
 | 
			
		||||
        self.assertFalse(limesdr.get_tx())
 | 
			
		||||
 | 
			
		||||
        print("Num Channels RX:", limesdr.get_num_channels())
 | 
			
		||||
        limesdr.CHANNEL = 0
 | 
			
		||||
        print("Enable RX Channel 0:", limesdr.enable_channel(True, False, 0))
 | 
			
		||||
 | 
			
		||||
        #path = os.path.realpath(os.path.join(__file__, "..", "..", "src", "urh", "dev", "native", "lime.ini"))
 | 
			
		||||
        #print(path)
 | 
			
		||||
        #limesdr.load_config(path)
 | 
			
		||||
        #limesdr.save_config("/tmp/lime_test.ini")
 | 
			
		||||
 | 
			
		||||
        clocks = ["LMS_CLOCK_REF", "LMS_CLOCK_SXR", "LMS_CLOCK_SXT", "LMS_CLOCK_CGEN", "LMS_CLOCK_RXTSP", "LMS_CLOCK_TXTSP"]
 | 
			
		||||
 | 
			
		||||
        for i, clock in enumerate(clocks):
 | 
			
		||||
            print(clock, limesdr.get_clock_freq(i))
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        print("RX Sample Rate Range:", limesdr.get_sample_rate_range())
 | 
			
		||||
        print("RX Channel 0 Sample Rate:", limesdr.get_sample_rate())
 | 
			
		||||
        print("Set Sample Rate:", limesdr.set_sample_rate(2e6))
 | 
			
		||||
        print("RX Channel 0 Sample Rate:", limesdr.get_sample_rate())
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        print("RX Frequency Range:", limesdr.get_center_frequency_range())
 | 
			
		||||
        print("RX 0 center freq:", limesdr.get_center_frequency())
 | 
			
		||||
        print("RX 0 set center freq:", limesdr.set_center_frequency(433.92e6))
 | 
			
		||||
        print("RX 0 center freq:", limesdr.get_center_frequency())
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        print("RX 0 gain", limesdr.get_normalized_gain())
 | 
			
		||||
        print("RX 0 set gain", limesdr.set_normalized_gain(0.5))
 | 
			
		||||
        print("RX 0 gain", limesdr.get_normalized_gain())
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        print("RX Bandwidth Range", limesdr.get_lpf_bandwidth_range())
 | 
			
		||||
        print("RX 0 Bandwidth", limesdr.get_lpf_bandwidth())
 | 
			
		||||
        print("RX 0 set Bandwidth", limesdr.set_lpf_bandwidth(20e6))
 | 
			
		||||
        print("RX 0 Bandwidth", limesdr.get_lpf_bandwidth())
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        print("RX 0 calibrate:", limesdr.calibrate(20e6))
 | 
			
		||||
 | 
			
		||||
        limesdr.print_last_error()
 | 
			
		||||
        antenna_list = limesdr.get_antenna_list()
 | 
			
		||||
        print("RX 0 antenna list", antenna_list)
 | 
			
		||||
        print("RX 0 current antenna", limesdr.get_antenna(), antenna_list[limesdr.get_antenna()])
 | 
			
		||||
        print("RX 0 current antenna BW", limesdr.get_antenna_bw(limesdr.get_antenna()))
 | 
			
		||||
 | 
			
		||||
        print("Chip Temperature", limesdr.get_chip_temperature())
 | 
			
		||||
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
 | 
			
		||||
        for _ in range(2):
 | 
			
		||||
            limesdr.print_last_error()
 | 
			
		||||
            print("Setup stream", limesdr.setup_stream(1000))
 | 
			
		||||
            print("Start stream", limesdr.start_stream())
 | 
			
		||||
            limesdr.recv_stream(child_conn, 1000, 100)
 | 
			
		||||
            print("Stop stream", limesdr.stop_stream())
 | 
			
		||||
            print("Destroy stream", limesdr.destroy_stream())
 | 
			
		||||
 | 
			
		||||
            print(parent_conn.recv_bytes())
 | 
			
		||||
 | 
			
		||||
        limesdr.set_tx(True)
 | 
			
		||||
        self.assertTrue(limesdr.get_tx())
 | 
			
		||||
        samples_to_send = np.ones(32768, dtype=np.complex64)
 | 
			
		||||
        for _ in range(2):
 | 
			
		||||
            limesdr.print_last_error()
 | 
			
		||||
            print("Setup stream", limesdr.setup_stream(4000000000))
 | 
			
		||||
            print("Start stream", limesdr.start_stream())
 | 
			
		||||
            print("Send samples", limesdr.send_stream(samples_to_send.view(np.float32), 100))
 | 
			
		||||
            print("Stop stream", limesdr.stop_stream())
 | 
			
		||||
            print("Destroy stream", limesdr.destroy_stream())
 | 
			
		||||
 | 
			
		||||
        print("-" * 20)
 | 
			
		||||
        print("Close:", limesdr.close())
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										47
									
								
								Software/urh/tests/device/TestPlutoSDR.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								Software/urh/tests/device/TestPlutoSDR.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
from multiprocessing import Pipe
 | 
			
		||||
 | 
			
		||||
from urh.util import util
 | 
			
		||||
import numpy as np
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import plutosdr
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_cython_wrapper():
 | 
			
		||||
    descs, uris = plutosdr.scan_devices()
 | 
			
		||||
    plutosdr.set_tx(False)
 | 
			
		||||
    print("Devices", descs)
 | 
			
		||||
    print("Open", plutosdr.open(uris[0]))
 | 
			
		||||
    print("Set Freq to 433.92e6", plutosdr.set_center_freq(int(433.92e6)))
 | 
			
		||||
    print("Set Sample Rate to 2M", plutosdr.set_sample_rate(int(2.5e6)))
 | 
			
		||||
    print("Set bandwidth to 4M", plutosdr.set_bandwidth(int(4e6)))
 | 
			
		||||
    print("Set gain to 10", plutosdr.set_rf_gain(10))
 | 
			
		||||
 | 
			
		||||
    print("prepare rx", plutosdr.setup_rx())
 | 
			
		||||
 | 
			
		||||
    parent_conn, child_conn = Pipe()
 | 
			
		||||
 | 
			
		||||
    for i in range(10):
 | 
			
		||||
        plutosdr.receive_sync(child_conn)
 | 
			
		||||
        data = parent_conn.recv_bytes()
 | 
			
		||||
        print(np.frombuffer(data, dtype=np.int16))
 | 
			
		||||
 | 
			
		||||
    print(plutosdr.get_tx())
 | 
			
		||||
    print("Close", plutosdr.close())
 | 
			
		||||
 | 
			
		||||
    plutosdr.set_tx(True)
 | 
			
		||||
 | 
			
		||||
    print("Open", plutosdr.open(uris[0]))
 | 
			
		||||
    print("Setup tx", plutosdr.setup_tx())
 | 
			
		||||
    print("Set Freq to 433.92e6", plutosdr.set_center_freq(int(433.92e6)))
 | 
			
		||||
    print("Set Sample Rate to 2M", plutosdr.set_sample_rate(int(2.5e6)))
 | 
			
		||||
    print("Set bandwidth to 4M", plutosdr.set_bandwidth(int(4e6)))
 | 
			
		||||
    print("Set gain to 10", plutosdr.set_rf_gain(-89))
 | 
			
		||||
 | 
			
		||||
    print("Send", plutosdr.send_sync(np.zeros(4096, dtype=np.int16)))
 | 
			
		||||
 | 
			
		||||
    print("Close", plutosdr.close())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    test_cython_wrapper()
 | 
			
		||||
							
								
								
									
										84
									
								
								Software/urh/tests/device/TestRTLSDR.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								Software/urh/tests/device/TestRTLSDR.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
import unittest
 | 
			
		||||
import sys
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
 | 
			
		||||
from urh.util import util
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.RTLSDR import RTLSDR
 | 
			
		||||
from urh.dev.native.lib import rtlsdr
 | 
			
		||||
 | 
			
		||||
class TestRTLSDR(unittest.TestCase):
 | 
			
		||||
    def test_cython_wrapper(self):
 | 
			
		||||
 | 
			
		||||
        print("Device count:", rtlsdr.get_device_count())
 | 
			
		||||
        print("Device name:", rtlsdr.get_device_name(0))
 | 
			
		||||
        manufact, product, serial = rtlsdr.get_device_usb_strings(0)
 | 
			
		||||
        print("Manufacturer:", manufact)
 | 
			
		||||
        print("Product:", product)
 | 
			
		||||
        print("Serial", serial)
 | 
			
		||||
        print("Index by serial", rtlsdr.get_index_by_serial(serial))
 | 
			
		||||
        print("Open:", rtlsdr.open(0))
 | 
			
		||||
        print("Reset Buffer:", rtlsdr.reset_buffer())  # IMPORTANT
 | 
			
		||||
        print("XTAL Freq:", rtlsdr.get_xtal_freq())
 | 
			
		||||
        print("USB device strings", rtlsdr.get_usb_strings())
 | 
			
		||||
        print("Center Freq:", rtlsdr.get_center_freq())
 | 
			
		||||
        print("Set center freq to 433MHz", rtlsdr.set_center_freq(int(433e6)))
 | 
			
		||||
        print("Center Freq:", rtlsdr.get_center_freq())
 | 
			
		||||
        print("Freq Correction", rtlsdr.get_freq_correction())
 | 
			
		||||
        print("Set Freq Correction to 10", rtlsdr.set_freq_correction(10))
 | 
			
		||||
        print("Freq Correction", rtlsdr.get_freq_correction())
 | 
			
		||||
        print("tuner type", rtlsdr.get_tuner_type())
 | 
			
		||||
        print("tuner_gains", rtlsdr.get_tuner_gains())
 | 
			
		||||
        print("set_manual_gain_mode", rtlsdr.set_tuner_gain_mode(1))
 | 
			
		||||
        print("tuner gain", rtlsdr.get_tuner_gain())
 | 
			
		||||
        print("set gain to 338", rtlsdr.set_tuner_gain(338))
 | 
			
		||||
        print("tuner gain", rtlsdr.get_tuner_gain())
 | 
			
		||||
        print("set tuner if gain", rtlsdr.set_tuner_if_gain(1, 10))
 | 
			
		||||
        print("Sample Rate", rtlsdr.get_sample_rate())
 | 
			
		||||
        print("Set Sample Rate to 300k", rtlsdr.set_sample_rate(300 * 10 ** 3))
 | 
			
		||||
        print("Sample Rate", rtlsdr.get_sample_rate())
 | 
			
		||||
        read_samples = rtlsdr.read_sync(1024)
 | 
			
		||||
        print(read_samples)
 | 
			
		||||
        rtlsdr.close()
 | 
			
		||||
 | 
			
		||||
    def test_receive(self):
 | 
			
		||||
        rtlsdr_class = RTLSDR(433.92e6, 20, 2e6, device_number=0)
 | 
			
		||||
        self.assertEqual(rtlsdr_class.current_recv_index, 0)
 | 
			
		||||
        rtlsdr_class.start_rx_mode()
 | 
			
		||||
        time.sleep(2)
 | 
			
		||||
        rtlsdr_class.stop_rx_mode("Finished")
 | 
			
		||||
        index = rtlsdr_class.current_recv_index
 | 
			
		||||
        print(rtlsdr_class.current_recv_index)
 | 
			
		||||
        self.assertGreater(rtlsdr_class.current_recv_index, 0)
 | 
			
		||||
        time.sleep(2)
 | 
			
		||||
        self.assertEqual(index, rtlsdr_class.current_recv_index)
 | 
			
		||||
        rtlsdr_class.start_rx_mode()
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
        self.assertGreater(rtlsdr_class.current_recv_index, index)
 | 
			
		||||
 | 
			
		||||
    def test_bytes_to_iq(self):
 | 
			
		||||
        arr = np.array([0, 0, 127.5, 127.5, 255, 255], dtype=np.uint8)
 | 
			
		||||
        self.assertEqual(arr[0], 0)
 | 
			
		||||
        self.assertEqual(arr[1], 0)
 | 
			
		||||
        self.assertEqual(arr[-1], 255)
 | 
			
		||||
 | 
			
		||||
        received = arr.tostring()
 | 
			
		||||
        self.assertEqual(len(received), len(arr))
 | 
			
		||||
        self.assertEqual(np.int8(received[0]), 0)
 | 
			
		||||
        self.assertEqual(np.int8(received[1]), 0)
 | 
			
		||||
        unpacked = RTLSDR.bytes_to_iq(received, len(received) // 2)
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(unpacked[0], complex(-1, -1))
 | 
			
		||||
        self.assertAlmostEqual(unpacked[1], complex(0, 0), places=1)
 | 
			
		||||
        self.assertEqual(unpacked[2], complex(1, 1))
 | 
			
		||||
 | 
			
		||||
        packed = RTLSDR.iq_to_bytes(unpacked)
 | 
			
		||||
        self.assertEqual(received, packed)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										112
									
								
								Software/urh/tests/device/TestRTLSDRPipe.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Software/urh/tests/device/TestRTLSDRPipe.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
import unittest
 | 
			
		||||
from multiprocessing import Pipe
 | 
			
		||||
 | 
			
		||||
from multiprocessing import Process
 | 
			
		||||
from threading import Thread
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import rtlsdr
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
from urh.util.Logger import logger
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def callback_recv(buffer):
 | 
			
		||||
    try:
 | 
			
		||||
        print(len(buffer))
 | 
			
		||||
    except BrokenPipeError:
 | 
			
		||||
        pass
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def receive_async(callback, connection):
 | 
			
		||||
    rtlsdr.open(0)
 | 
			
		||||
    rtlsdr.reset_buffer()
 | 
			
		||||
    rtlsdr.read_async(callback, connection)
 | 
			
		||||
    connection.close()
 | 
			
		||||
 | 
			
		||||
def receive_sync(connection):
 | 
			
		||||
    rtlsdr.open(0)
 | 
			
		||||
    rtlsdr.reset_buffer()
 | 
			
		||||
    exit_requested = False
 | 
			
		||||
 | 
			
		||||
    while not exit_requested:
 | 
			
		||||
        while connection.poll():
 | 
			
		||||
            result = process_command(connection.recv())
 | 
			
		||||
            if result == "stop":
 | 
			
		||||
                exit_requested = True
 | 
			
		||||
                break
 | 
			
		||||
 | 
			
		||||
        if not exit_requested:
 | 
			
		||||
            connection.send_bytes(rtlsdr.read_sync())
 | 
			
		||||
 | 
			
		||||
    connection.close()
 | 
			
		||||
 | 
			
		||||
def process_command(command):
 | 
			
		||||
    if command == "stop":
 | 
			
		||||
        return "stop"
 | 
			
		||||
 | 
			
		||||
    tag, value = command.split(":")
 | 
			
		||||
    if tag == "center_freq":
 | 
			
		||||
        logger.info("[RTLSDR] setting center freq to {}".format(int(value)))
 | 
			
		||||
        rtlsdr.set_center_freq(int(value))
 | 
			
		||||
    elif tag == "tuner_gain":
 | 
			
		||||
        logger.info("[RTLSDR] setting tuner_gain to {}".format(int(value)))
 | 
			
		||||
        rtlsdr.set_tuner_gain(int(value))
 | 
			
		||||
    elif tag == "sample_rate":
 | 
			
		||||
        logger.info("[RTLSDR] setting sample rate to {}".format(int(value)))
 | 
			
		||||
        rtlsdr.set_sample_rate(int(value))
 | 
			
		||||
 | 
			
		||||
def read_connection(connection):
 | 
			
		||||
    while True:
 | 
			
		||||
        try:
 | 
			
		||||
            received_bytes = connection.recv_bytes()
 | 
			
		||||
            print(received_bytes[0:100])
 | 
			
		||||
        except EOFError:
 | 
			
		||||
            break
 | 
			
		||||
 | 
			
		||||
def f(child_conn):
 | 
			
		||||
    ctrl_command = b""
 | 
			
		||||
    while ctrl_command != b"stop":
 | 
			
		||||
        child_conn.send_bytes(bytearray([1, 2, 3, 4, 5]))
 | 
			
		||||
        time.sleep(0.1)
 | 
			
		||||
        if child_conn.poll():
 | 
			
		||||
            ctrl_command = child_conn.recv_bytes()
 | 
			
		||||
            print("Got from server", ctrl_command)
 | 
			
		||||
 | 
			
		||||
    print("Stopping....")
 | 
			
		||||
    child_conn.send("goodbye")
 | 
			
		||||
    child_conn.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestPipe(unittest.TestCase):
 | 
			
		||||
    def test_multiprocessing_pipe(self):
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
        p = Process(target=f, args=(child_conn,))
 | 
			
		||||
        p.start()
 | 
			
		||||
        for _ in range(5):
 | 
			
		||||
            while parent_conn.poll():
 | 
			
		||||
                print("Got from client", parent_conn.recv_bytes())  # prints "[42, None, 'hello']"
 | 
			
		||||
            time.sleep(1)
 | 
			
		||||
        parent_conn.send_bytes(b"stop")
 | 
			
		||||
        p.join()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def test_rtl_sdr_with_pipe(self):
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
        p = Process(target=receive_sync, args=(child_conn, ))
 | 
			
		||||
        t = Thread(target=read_connection, args=(parent_conn, ))
 | 
			
		||||
        t.daemon = True
 | 
			
		||||
        p.daemon = True
 | 
			
		||||
        t.start()
 | 
			
		||||
        p.start()
 | 
			
		||||
        time.sleep(2)
 | 
			
		||||
        print("Sending set freq command")
 | 
			
		||||
        parent_conn.send("center_freq:{}".format(int(433.92e6)))
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
        parent_conn.send("tuner_gain:{}".format(int(20)))
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
        parent_conn.send("sample_rate:{}".format(int(2e6)))
 | 
			
		||||
        print("Sending stop command")
 | 
			
		||||
        parent_conn.send("stop")
 | 
			
		||||
        p.join()
 | 
			
		||||
        time.sleep(2)
 | 
			
		||||
							
								
								
									
										29
									
								
								Software/urh/tests/device/TestRTLSDRTCP.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								Software/urh/tests/device/TestRTLSDRTCP.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.RTLSDRTCP import RTLSDRTCP
 | 
			
		||||
 | 
			
		||||
class TestRTLSDRTCP(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
    def test_device_communication(self):
 | 
			
		||||
        error = 0
 | 
			
		||||
        sdr = RTLSDRTCP(0, 0, 0, device_number=0)
 | 
			
		||||
        sdr.open(sdr.child_ctrl_conn)
 | 
			
		||||
        if sdr.socket_is_open == False:
 | 
			
		||||
            error += 1
 | 
			
		||||
        if sdr.set_parameter("centerFreq", 927000000, sdr.child_ctrl_conn):
 | 
			
		||||
            error += 1
 | 
			
		||||
        if sdr.set_parameter("sampleRate", 2000000, sdr.child_ctrl_conn):
 | 
			
		||||
            error += 1
 | 
			
		||||
        if sdr.set_parameter("bandwidth", 2000000, sdr.child_ctrl_conn):
 | 
			
		||||
            error += 1
 | 
			
		||||
        if sdr.set_parameter("tunerGain", 200, sdr.child_ctrl_conn):
 | 
			
		||||
            error += 1
 | 
			
		||||
        data = sdr.read_sync()
 | 
			
		||||
        if len(data) < 1:
 | 
			
		||||
            error += 1
 | 
			
		||||
        sdr.close()
 | 
			
		||||
        self.assertEqual(error, 0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										49
									
								
								Software/urh/tests/device/TestSDRPlay.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								Software/urh/tests/device/TestSDRPlay.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
import time
 | 
			
		||||
from multiprocessing.connection import Connection, Pipe
 | 
			
		||||
 | 
			
		||||
import numpy as np
 | 
			
		||||
from multiprocessing import Process
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.SDRPlay import SDRPlay
 | 
			
		||||
from urh.util import util
 | 
			
		||||
import ctypes
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.lib import sdrplay
 | 
			
		||||
 | 
			
		||||
def recv(conn: Connection):
 | 
			
		||||
    while True:
 | 
			
		||||
        t = time.time()
 | 
			
		||||
        result = SDRPlay.bytes_to_iq(conn.recv_bytes())
 | 
			
		||||
        print("UNPACK", time.time()-t)
 | 
			
		||||
 | 
			
		||||
class TestSDRPlay(unittest.TestCase):
 | 
			
		||||
    def test_c_wrapper(self):
 | 
			
		||||
        def pycallback(data):
 | 
			
		||||
            arr = np.asarray(data)
 | 
			
		||||
            #result = np.empty(len(arr) // 2, dtype=np.complex64)
 | 
			
		||||
            #result.real = (arr[::2] + 0.5) / 32767.5
 | 
			
		||||
            #result.imag = (arr[1::2] + 0.5) / 32767.5
 | 
			
		||||
 | 
			
		||||
        print(sdrplay.get_api_version())
 | 
			
		||||
        print(sdrplay.get_devices())
 | 
			
		||||
        print(sdrplay.set_device_index(0))
 | 
			
		||||
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
        p = Process(target=recv, args=(parent_conn,))
 | 
			
		||||
        p.daemon = True
 | 
			
		||||
        p.start()
 | 
			
		||||
 | 
			
		||||
        null_ptr = ctypes.POINTER(ctypes.c_voidp)()
 | 
			
		||||
        print("Init stream", sdrplay.init_stream(50, 2e6, 433.92e6, 2e6, 500, child_conn))
 | 
			
		||||
 | 
			
		||||
        time.sleep(2)
 | 
			
		||||
        print("settings sample rate")
 | 
			
		||||
        print("Set sample rate", sdrplay.set_sample_rate(2e6))
 | 
			
		||||
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
        p.terminate()
 | 
			
		||||
        p.join()
 | 
			
		||||
							
								
								
									
										105
									
								
								Software/urh/tests/device/TestSoundCard.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								Software/urh/tests/device/TestSoundCard.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,105 @@
 | 
			
		||||
import numpy as np
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_sounddevice_lib():
 | 
			
		||||
    import time
 | 
			
		||||
 | 
			
		||||
    import numpy as np
 | 
			
		||||
    from sounddevice import InputStream, OutputStream, sleep as sd_sleep
 | 
			
		||||
    """ 
 | 
			
		||||
    if no portaudio installed:
 | 
			
		||||
    Traceback (most recent call last):
 | 
			
		||||
  File "TestSoundCard.py", line 42, in <module>
 | 
			
		||||
    test_sounddevice_lib()
 | 
			
		||||
  File "TestSoundCard.py", line 5, in test_sounddevice_lib
 | 
			
		||||
    import sounddevice as sd
 | 
			
		||||
  File "/usr/lib/python3.6/site-packages/sounddevice.py", line 64, in <module>
 | 
			
		||||
    raise OSError('PortAudio library not found')
 | 
			
		||||
  OSError: PortAudio library not found
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    duration = 2.5  # seconds
 | 
			
		||||
 | 
			
		||||
    rx_buffer = np.ones((10 ** 6, 2), dtype=np.float32)
 | 
			
		||||
    global current_rx, current_tx
 | 
			
		||||
    current_rx = 0
 | 
			
		||||
    current_tx = 0
 | 
			
		||||
 | 
			
		||||
    def rx_callback(indata: np.ndarray, frames: int, time, status):
 | 
			
		||||
        global current_rx
 | 
			
		||||
        if status:
 | 
			
		||||
            print(status)
 | 
			
		||||
 | 
			
		||||
        rx_buffer[current_rx:current_rx + frames] = indata
 | 
			
		||||
        current_rx += frames
 | 
			
		||||
 | 
			
		||||
    def tx_callback(outdata: np.ndarray, frames: int, time, status):
 | 
			
		||||
        global current_tx
 | 
			
		||||
        if status:
 | 
			
		||||
            print(status)
 | 
			
		||||
 | 
			
		||||
        outdata[:] = rx_buffer[current_tx:current_tx + frames]
 | 
			
		||||
        current_tx += frames
 | 
			
		||||
 | 
			
		||||
    with InputStream(channels=2, callback=rx_callback):
 | 
			
		||||
        sd_sleep(int(duration * 1000))
 | 
			
		||||
 | 
			
		||||
    print("Current rx", current_rx)
 | 
			
		||||
 | 
			
		||||
    with OutputStream(channels=2, callback=tx_callback):
 | 
			
		||||
        sd_sleep(int(duration * 1000))
 | 
			
		||||
 | 
			
		||||
    print("Current tx", current_tx)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_pyaudio():
 | 
			
		||||
    import pyaudio
 | 
			
		||||
 | 
			
		||||
    CHUNK = 1024
 | 
			
		||||
    p = pyaudio.PyAudio()
 | 
			
		||||
 | 
			
		||||
    stream = p.open(format=pyaudio.paFloat32,
 | 
			
		||||
                    channels=2,
 | 
			
		||||
                    rate=48000,
 | 
			
		||||
                    input=True,
 | 
			
		||||
                    frames_per_buffer=CHUNK)
 | 
			
		||||
 | 
			
		||||
    print("* recording")
 | 
			
		||||
 | 
			
		||||
    frames = []
 | 
			
		||||
 | 
			
		||||
    for i in range(0, 100):
 | 
			
		||||
        data = stream.read(CHUNK)
 | 
			
		||||
        frames.append(data)
 | 
			
		||||
 | 
			
		||||
    print("* done recording")
 | 
			
		||||
 | 
			
		||||
    stream.stop_stream()
 | 
			
		||||
    stream.close()
 | 
			
		||||
    p.terminate()
 | 
			
		||||
    data = b''.join(frames)
 | 
			
		||||
 | 
			
		||||
    print("* playing")
 | 
			
		||||
 | 
			
		||||
    p = pyaudio.PyAudio()
 | 
			
		||||
    stream = p.open(format=pyaudio.paFloat32,
 | 
			
		||||
                    channels=2,
 | 
			
		||||
                    rate=48000,
 | 
			
		||||
                    output=True,
 | 
			
		||||
                    )
 | 
			
		||||
 | 
			
		||||
    for i in range(0, len(data), CHUNK):
 | 
			
		||||
        stream.write(data[i:i+CHUNK])
 | 
			
		||||
 | 
			
		||||
    stream.stop_stream()
 | 
			
		||||
    stream.close()
 | 
			
		||||
 | 
			
		||||
    p.terminate()
 | 
			
		||||
 | 
			
		||||
    print("* done playing")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    # test_sounddevice_lib()
 | 
			
		||||
    test_pyaudio()
 | 
			
		||||
							
								
								
									
										64
									
								
								Software/urh/tests/device/TestUSRP.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								Software/urh/tests/device/TestUSRP.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
from multiprocessing.connection import Pipe
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
from urh.util import util
 | 
			
		||||
 | 
			
		||||
util.set_shared_library_path()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
from urh.dev.native.USRP import USRP
 | 
			
		||||
from urh.dev.native.lib import usrp
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
class TestUSRP(unittest.TestCase):
 | 
			
		||||
    def test_cython_wrapper(self):
 | 
			
		||||
        print(usrp.find_devices(""))
 | 
			
		||||
 | 
			
		||||
        usrp.set_tx(False)
 | 
			
		||||
 | 
			
		||||
        return_code = usrp.open("addr=192.168.10.2")
 | 
			
		||||
        print("open", return_code)
 | 
			
		||||
 | 
			
		||||
        usrp.setup_stream()
 | 
			
		||||
        print("Made rx_streame handler")
 | 
			
		||||
 | 
			
		||||
        print(usrp.get_device_representation())
 | 
			
		||||
 | 
			
		||||
        print("Set sample rate", usrp.set_sample_rate(2e6))
 | 
			
		||||
        print("Set freq", usrp.set_center_freq(433.92e6))
 | 
			
		||||
        print("Set bandwidth", usrp.set_bandwidth(1e6))
 | 
			
		||||
        print("Set gain", usrp.set_rf_gain(0.5))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        buffer = bytearray()
 | 
			
		||||
 | 
			
		||||
        num_samples = 32768 // 2
 | 
			
		||||
 | 
			
		||||
        usrp.start_stream(num_samples)
 | 
			
		||||
        parent_conn, child_conn = Pipe()
 | 
			
		||||
 | 
			
		||||
        for i in range(500):
 | 
			
		||||
            usrp.recv_stream(child_conn, num_samples)
 | 
			
		||||
            received_bytes = parent_conn.recv_bytes()
 | 
			
		||||
            #print(received_bytes)
 | 
			
		||||
            print(i)
 | 
			
		||||
            buffer.extend(received_bytes)
 | 
			
		||||
            #print(USRP.bytes_to_iq(received_bytes, len(received_bytes) // 8))
 | 
			
		||||
 | 
			
		||||
        f = open("/tmp/test.complex", "wb")
 | 
			
		||||
        f.write(buffer)
 | 
			
		||||
        f.close()
 | 
			
		||||
 | 
			
		||||
        usrp.destroy_stream()
 | 
			
		||||
        print("Freed rx streamer handler")
 | 
			
		||||
 | 
			
		||||
        return_code = usrp.close()
 | 
			
		||||
        print("close", return_code)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        #self.assertTrue(True)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    unittest.main()
 | 
			
		||||
							
								
								
									
										0
									
								
								Software/urh/tests/device/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								Software/urh/tests/device/__init__.py
									
									
									
									
									
										Normal file
									
								
							
		Reference in New Issue
	
	Block a user