summaryrefslogtreecommitdiff
path: root/Radio/Utils/RtlSdrIQ/main.swift
blob: 227f22248ece5823328c6f53d05201a2cc6a6778 (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
//
//  main.swift
//  RtlSdrIQ
//
//  Created by Jacky Jack on 27/11/2024.
//

import Foundation
import libr820
import ArgumentParser

let NUM_SAMPLES=1024

//set the command line arguments
struct CommandLineArgs: ParsableCommand {
    @Argument var file:String = ""
    @Option(name:.shortAndLong) var device_idx: Int = 0
    @Option(name:.shortAndLong) var samplerate: Int = 2048000
    @Option(name:.shortAndLong) var gain: Int = 0
    @Option(name:.shortAndLong) var frequency: Int = 100000000
    @Option(name:.shortAndLong) var nsamples: Int = 1024
    @Flag(help:"Version \(software_version)") var version: Bool = false
    @Flag(name: .shortAndLong) var verbose: Bool = false
    @Flag(name: .shortAndLong) var async: Bool = false
}

let args = CommandLineArgs.parseOrExit()

//check that there is any devices
let count = getDeviceCount()
if count < 1 {
    print("There is not R820 tunner found on this machine")
    exit(0)
}

//check that argument index is withing range
if (args.device_idx < 0) || (args.device_idx > count-1) {
    print("Index is out of range, devices found \(count)")
    exit(0)
}

//initialise structure without a connected driver
let device = R820Tuner()
device.open(index: UInt32(args.device_idx))

//prepare dongle to receive some data
let _ = device.setSampleRate(samplerate: UInt32(args.samplerate))
let _ = device.setCenterFreq(freq: UInt32(args.frequency))
if args.gain == 0 {
    let _ = device.setAgcMode(on: 1)
} else {
    let _ = device.setTunerGain(gain: Int32(args.gain))
}
let _ = device.resetBuffer()

//prepare file descriptor if args specify that
let currentExePath = Process().currentDirectoryPath
var fileDescriptor = FileManager.default
var fileArgUrl:URL?
var fileHandle:FileHandle?
if args.file != "" {
    fileArgUrl = URL(fileURLWithFileSystemRepresentation: args.file, isDirectory: false, relativeTo: nil)
    if (checkIfFileExists(args.file)) {
        //remove file
        do {
            try fileDescriptor.removeItem(atPath: fileArgUrl!.path())
        } catch {
            print("Couldn't delete file that exists \(fileArgUrl!.path())")
        }
    }
    
    //create file
    fileDescriptor.createFile(atPath: fileArgUrl!.path(), contents: nil)
    try fileHandle = FileHandle(forWritingTo: fileArgUrl!)
    try fileHandle?.seekToEnd()
}

//prepare loop buffers to process data
let buf_ptr = UnsafeMutableRawPointer.allocate(byteCount: NUM_SAMPLES, alignment: 1)
var nbytes:Int32 = 0
var total_bytes:Int32 = 0
if (args.async == false) {
    while (total_bytes < args.nsamples) {
        let ret = device.readSync(buf: buf_ptr, len: Int32(NUM_SAMPLES), n_read: &nbytes)
        if ret<0 {
            print("data read sync returned <0 = \(ret)")
            break;
        }
        print("Reading samples read \(nbytes) bytes")
        total_bytes += nbytes
        
        let dataU8 = buf_ptr.bindMemory(to: UInt8.self, capacity: NUM_SAMPLES)
        let bufferU8 = UnsafeBufferPointer(start: dataU8, count: NUM_SAMPLES)
        let convertedData = Data(bufferU8)
        /*for i in 0..<Int(nbytes) {
            let offsetByte = buf_ptr + i
            print("\(String(format:"%02hhX ",offsetByte.load(as: UInt8.self)))",terminator: "")
        }*/
        if (args.verbose) {
            for i in 0..<bufferU8.count {
                print("\(String(format: "%02hhX", bufferU8[i]))", terminator: "")
            }
        }
        
        //dump data to file
        
        if let file = fileHandle {
            //print(convertedData)
            do {
                //try convertedData.write(to: fileUrl)
                try file.write(contentsOf: convertedData)
            } catch {
                print("Cant dump data to file")
            }
        }
    }
} else {
    print("ASYNC not implemented")
}

//clean after use all structures
device.close()
buf_ptr.deallocate()