// // AirSpy.swift // PrySDR // // Created by Jacky Jack on 25/10/2024. // import libairspy enum AirspyHFError: Error { case InvalidDevice } /// Wrapper for libairspy library class AirSpy { var dev:UnsafeMutablePointer? = .allocate(capacity: 1) var sample_rates:[UInt32] = [] init(serialno: UInt64) throws { let ret = airspy_open_sn(&dev, serialno) if (ret != AIRSPY_SUCCESS.rawValue) { throw AirspyHFError.InvalidDevice } } /// will open first device it can find init() throws { let ret = airspy_open(&dev) if ret != AIRSPY_SUCCESS.rawValue { print("Couldnt open airspy device") throw AirspyHFError.InvalidDevice } } func getSampleRates() -> [UInt32] { var ret: Int32 = 0 if sample_rates.count == 0 { let nscount: UnsafeMutablePointer = .allocate(capacity: 1) ret = airspy_get_samplerates(dev, nscount, 0) let samplerates_buf:UnsafeMutableBufferPointer = .allocate(capacity: Int(nscount.pointee)) let rawPointer = UnsafeMutablePointer?(samplerates_buf.baseAddress!) ret = airspy_get_samplerates(dev, rawPointer, nscount.pointee) sample_rates = [] for i in 0.. Int32 { return airspy_set_samplerate(dev, samplerate) } func rfBias(_ rfbias: UInt8) -> Int32 { return airspy_set_rf_bias(dev, rfbias) } func VGAGain(_ vgagain: UInt8) -> Int32 { return airspy_set_vga_gain(dev, vgagain) } func mixerGain(_ gain: UInt8) -> Int32 { return airspy_set_mixer_gain(dev, gain) } func lnaGain(_ gain: UInt8) -> Int32 { return airspy_set_lna_gain(dev, gain) } func startRx(_ callback: airspy_sample_block_cb_fn) -> Int32 { return airspy_start_rx(dev, callback, nil) } func stopRx() -> Int32 { return airspy_stop_rx(dev) } func setFrequency(_ freq: UInt32) -> Int32 { return airspy_set_freq(dev, freq) } func isStreaming() -> Int32 { return airspy_is_streaming(dev) } func close() -> Int32 { return airspy_close(dev) } }