summaryrefslogtreecommitdiff
path: root/Radio/HW/RtlSdr/r820/r820.swift
blob: 3f6d4a9bd261834966206e25b952dfc4ebccd446 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
//  r820.swift
//  r820sdr-init
//
//  Created by Jacky Jack on 15/03/2022.
//

//https://www.kodeco.com/7181017-unsafe-swift-using-pointers-and-interacting-with-c
// THis is just a wrapper for fun


import Foundation
import libr820


/// Gets the number of rtlsdr's detected in system
/// - Returns: return number of devices detected
///
func getDeviceCount() -> UInt32 {
    return rtlsdr_get_device_count()
}

/// Search for device index from serial number
/// - Parameters:
///   - serial: serial string of device
/// - Returns: return the index number if found, -1 if not found
///
func getIndexBySerial(serial: String) -> Int32
{
    return rtlsdr_get_index_by_serial(serial);//is this ok?
}

//TODO: investigate getDeviceName
/*
func getDeviceName(index: UInt32) -> String {
    return rtlsdr_get_device_name(index)
}*/

//TODO: investigate getDeviceUsbString

/*
 func getDeviceUsbString() {
 return rtlsdr_get_device_usb_strings();
 }*/

/// Wrapper for librtlsdr functions that's not operating on device
public class R820API {
    func R820() {
        print("Start device API")
    }
    
    func getDeviceCount() -> UInt32 {
        return rtlsdr_get_device_count()
    }
}

/// Wrapper for rtlsdr that works with device API
public class R820Tuner {
    var deviceInitalized:Bool = false
    var dev:       OpaquePointer?  = nil
    
    /**
     Function to initialise tunner, do nothing at the moment
     */
    func R820Tuner() {
        print("Initialise tunner")
    }
    
    /// open device by Index, checks if device allready were initialised within class
    /// - Parameters:
    ///   - index: device index to open
    func open(index devId: UInt32) {
        let err = rtlsdr_open(&dev,devId)
        if (err != 0) {
            print("Error couldnt open device")
            return
        }
        deviceInitalized = true
    }
    
    /// close device if it was opened before
    func close() {
        if self.deviceInitalized {
            rtlsdr_close(dev);
            dev = nil
        }
    }
    
    func setXtalFreq(rtlFreq: UInt32, tunnerFreq: UInt32) -> Int32 {
        return rtlsdr_set_xtal_freq(dev, rtlFreq, tunnerFreq);
    }
    
    func getXtalFreq(rtlFreq: UnsafeMutablePointer<UInt32>?, tunnerFreq: UnsafeMutablePointer<UInt32>?)->Int32{
        return rtlsdr_get_xtal_freq(dev, rtlFreq, tunnerFreq)
    }
    
    func getUsbStrings(manufact: UnsafeMutablePointer<CChar>?, product: UnsafeMutablePointer<CChar>?, serial: UnsafeMutablePointer<CChar>?) -> Int32{
        return rtlsdr_get_usb_strings(dev, manufact, product, serial)
    }
    
    func writeEeprom(data: UnsafeMutablePointer<UInt8>?,offset: UInt8, len: UInt16) -> Int32 {
        return rtlsdr_write_eeprom(dev, data, offset, len)
    }
    
    func readEeprom(data: UnsafeMutablePointer<UInt8>?,offset: UInt8, len: UInt16) -> Int32 {
        return rtlsdr_read_eeprom(dev, data, offset, len)
    }
    
    func setCenterFreq(freq: UInt32) -> Int32{
        return rtlsdr_set_center_freq(dev, freq);
    }
    
    func getCenterFreq()->UInt32 {
        return rtlsdr_get_center_freq(dev)
    }
    
    func setFreqCorrection(ppm: Int32) -> Int32 {
        return rtlsdr_set_freq_correction(dev, ppm)
    }
    
    func getFreqCorrection() -> Int32 {
        return rtlsdr_get_freq_correction(dev)
    }
    
    
    func getTunerType() -> rtlsdr_tuner {
        return rtlsdr_get_tuner_type(dev)
    }
    
    func getTunerGains(gains: UnsafeMutablePointer<Int32>?) -> Int32 {
        return rtlsdr_get_tuner_gains(dev, gains)
    }
    
    func setTunerGain(gain: Int32) -> Int32 {
        return rtlsdr_set_tuner_gain(dev,gain);
    }
    
    func setTunnerBandwidth(bw: UInt32) -> Int32 {
        return rtlsdr_set_tuner_bandwidth(dev, bw)
    }
    
    func setTunerIfGain(stage: Int32, gain: Int32) -> Int32 {
        return rtlsdr_set_tuner_if_gain(dev,stage,gain)
    }
    
    func setTunerGainMode(manual: Int32) -> Int32 {
        return rtlsdr_set_tuner_gain_mode(dev, manual)
    }
    
    func setSampleRate(samplerate: UInt32) -> Int32 {
        return rtlsdr_set_sample_rate(dev, samplerate)
    }
    
    func getSampleRate() -> UInt32 {
        return rtlsdr_get_sample_rate(dev)
    }
    
    func setTestmode(on: Int32) -> Int32 {
        return rtlsdr_set_testmode(dev, on);
    }
    
    func setAgcMode(on: Int32) -> Int32 {
        return rtlsdr_set_agc_mode(dev, on);
    }
    
    func setDirectSampling(on: Int32) -> Int32 {
        return rtlsdr_set_direct_sampling(dev, on)
    }
    
    func getDirectSampling() -> Int32 {
        return rtlsdr_get_direct_sampling(dev)
    }
    
    func setOffsetTuning(on: Int32) -> Int32 {
        return rtlsdr_set_offset_tuning(dev, on)
    }
    
    func getOffsetTuning() -> Int32 {
        return rtlsdr_get_offset_tuning(dev)
    }
    
    func resetBuffer() -> Int32 {
        return rtlsdr_reset_buffer(dev)
    }
    
    /// Read the data from buffer in sync manner
    /// - Parameters:
    ///  - buf: input buffer
    ///  - len: input buffer length
    ///  - n_read: number of bytes received
    /// - Returns: -1 if there is no device, 0 on success or one of LIBUSB error
    ///  LIBUSB_ERROR_TIMEOUT, LIBUSB_ERROR_PIPE,LIBUSB_ERROR_OVERFLOW
    /// LIBUSB_ERROR_NO_DEVICE, LIBUSB_ERROR_BUSY LIBUSB_ERROR_INVALID_PARAM
    ///
    func readSync(buf:UnsafeMutableRawPointer?,len: Int32,n_read:UnsafeMutablePointer<Int32>?) -> Int32 {
        return rtlsdr_read_sync(dev, buf, len, n_read);
    }
    
    func waitAsync(cb: rtlsdr_read_async_cb_t?, ctx:UnsafeMutableRawPointer?) -> Int32{
        return rtlsdr_wait_async(dev,cb, ctx);
    }
    
    func readAsync(cb: rtlsdr_read_async_cb_t,ctx:UnsafeMutableRawPointer?, buf_num: UInt32, buf_len: UInt32) -> Int32 {
        return rtlsdr_read_async(dev, cb, ctx, buf_num, buf_len)
    }
    
    /// Cancel async operation for sighandlders
    /// to exit from callbacks
    func cancelAsync() -> Int32 {
        return rtlsdr_cancel_async(dev);
    }
    
    func setBiasTee(on: Int32) -> Int32 {
        return rtlsdr_set_bias_tee(dev, on)
    }
    
    func setBiasTeeGpio(gpio: Int32, on: Int32) -> Int32 {
        return rtlsdr_set_bias_tee_gpio(dev,gpio, on)
    }
    
}