Converting python library UHD to hackrf

54 Views Asked by At

I'm working on a project based off of: https://github.com/RUB-SysSec/DroneSecurity.

The live receiver aspect uses UHD drivers from higher-end Ettus SDRs. I only have a HackRF ONE and am working with the pyhackrf library to try to re-create it. Without a Ettus SDR, I can't test to see what kind of information is being retrieved to recreate in pyhackrf.

The following function is from ./src/droneid_receiver_live.py of the above git project.

def set_sdr(usrp, sample_rate=50e6, duration_s=1.3, gain=None):
    ###### dev config (UHD b200) #####
    # RX2 port for 2.4 GHz antenna
    usrp.set_rx_antenna("RX2",0)
    if gain:
        usrp.set_rx_gain(gain, 0)
    else:
        usrp.set_rx_agc(True, 0)

    num_samps = duration_s*sample_rate

    usrp.set_rx_rate(sample_rate, 0)
    dev_samp_rate = usrp.get_rx_rate()

    # Set up the stream and receive buffer
    st_args = uhd.usrp.StreamArgs("fc32","sc16")
    st_args.channels = [0]
    _metadata = uhd.types.RXMetadata()
    _streamer = usrp.get_rx_stream(st_args)
    _recv_buffer = np.zeros((1, RECV_BUFFER_LEN), dtype=np.complex64)

    return num_samps, _metadata, _streamer, _recv_buffer

The below code is my attempted recreation but in the pyhackrf library, you can't directly set the receive buffer or receive frame length as you would with other UHD. Instead, pyhackrf gets samples continuously, and you have process them in real-time using a callback function (defined below).

def rx_callback(data):
    # Convert the received data to a numpy array
    sample_array = ctypes.cast(data.contents.buffer, ctypes.POINTER(
        ctypes.c_int16 * data.contents.valid_length))
    samples = np.frombuffer(sample_array.contents, dtype=np.complex64)

    # Process received samples
    num_frames = len(samples) // FRAME_SIZE
    buffer_index = 0
    for _ in range(num_frames):
        frame = samples[buffer_index:buffer_index + FRAME_SIZE]
        print("Received frame:", frame)
        # Process each frame here
        buffer_index += FRAME_SIZE

Has anyone tried to recreate uhd scripts using the hackrf library?

Tried to convert UHD python code to work with pyhackrf library.

0

There are 0 best solutions below