// // AirSpyHF.swift // PrySDR // // Created by Jacky Jack on 25/10/2024. // import libairspyhf enum AirspyHFError: Error { case InvalidDevice } /// Wrapper for libairspyhf library class AirSpyHF { var dev:UnsafeMutablePointer? = .allocate(capacity: 1) /* init() { }*/ /// Initialise by index init(idx: Int32) throws { let ndev = airspyhf_list_devices(nil, 0) if ((idx < 0) || (idx >= ndev)) { throw AirspyHFError.InvalidDevice } let serialPtr: UnsafeMutablePointer = .allocate(capacity: 1) airspyhf_list_devices(serialPtr, idx+1) if serialPtr.pointee == 0 { print("Cant get serial number of device index \(idx)") throw AirspyHFError.InvalidDevice } try openSN(serial: serialPtr.pointee) } func openSN(serial: UInt64) throws { let ret = airspyhf_open_sn(&dev, serial) print("ret=\(ret)") if (ret != AIRSPYHF_SUCCESS.rawValue) { print("Cant open device with serial numner \(serial)") throw AirspyHFError.InvalidDevice } } /* /// Initialise by serial number init(serial: UInt64) { }*/ func getSampleRates() -> [UInt32] { var sample_rates:[UInt32] = [] var ret: Int32 let nsrate = UnsafeMutablePointer.allocate(capacity: 1) ret = airspyhf_get_samplerates(dev, nsrate, 0) if ret == AIRSPYHF_ERROR.rawValue { print("Error uptaining the sample number") return [] } let sampleBuffer = UnsafeMutableBufferPointer.allocate(capacity: Int(nsrate.pointee)) let rawPointer = UnsafeMutablePointer?(sampleBuffer.baseAddress!) ret = airspyhf_get_samplerates(dev, rawPointer, nsrate.pointee) if (ret == AIRSPYHF_ERROR.rawValue) { print("Error geting sample rates. Not processed") } print("Found \(nsrate.pointee) samplerates") for idx in 0.. Int32 { let ret = airspyhf_set_samplerate(dev, samplerate) return ret } func setHfAgc(_ flag: UInt8) -> Int32{ let ret = airspyhf_set_hf_agc(dev, flag) return ret } func setHfAgcThreshold(_ flag: UInt8) -> Int32 { let ret = airspyhf_set_hf_agc_threshold(dev, flag) return ret } func setHfLNA(_ flag: UInt8) -> Int32 { let ret = airspyhf_set_hf_lna(dev, flag) return ret } func start(_ callback: airspyhf_sample_block_cb_fn) -> Int32 { let ret = airspyhf_start(dev, callback, nil) return ret } func stop() -> Int32 { let ret = airspyhf_stop(dev) return ret } func setFreq(_ frequency: UInt32) -> Int32 { let ret = airspyhf_set_freq(dev, frequency) return ret } func isStreaming() -> Int32 { let ret = airspyhf_is_streaming(dev) return ret } func close() { airspyhf_close(dev) } }