summaryrefslogtreecommitdiff
path: root/Radio/HW/AirSpyHF/AirSpyHF.swift
blob: ea835afe0f20edc12269caf40865164d39e1ca2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//  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<airspyhf_device_t>? = .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<UInt64> = .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<UInt32>.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<UInt32>.allocate(capacity: Int(nsrate.pointee))
        let rawPointer = UnsafeMutablePointer<UInt32>?(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..<nsrate.pointee {
            sample_rates.append(sampleBuffer[Int(idx)])
        }
        
        return sample_rates
    }
    
    func setSampleRate(_ samplerate:UInt32) -> 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)
    }
}