From b999f85d83728bd7034e85f2e038bb9a6454b16a Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Sun, 22 Dec 2024 09:45:59 +0000 Subject: BladeRF: seems to receive data --- Radio/Utils/BladeRFIQ/main.swift | 106 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) (limited to 'Radio/Utils/BladeRFIQ') diff --git a/Radio/Utils/BladeRFIQ/main.swift b/Radio/Utils/BladeRFIQ/main.swift index a4f2ffe..cc748f2 100644 --- a/Radio/Utils/BladeRFIQ/main.swift +++ b/Radio/Utils/BladeRFIQ/main.swift @@ -9,6 +9,110 @@ import Foundation import ArgumentParser import libbladerf -print("Hello, World!") +//set the command line arguments +struct CommandLineArgs: ParsableCommand { + @Argument var file:String = "" + @Option() var serial: UInt64 = 0 + @Option(name:.shortAndLong) var samplerate: Int = 200000 + @Option(name:.shortAndLong) var gain: Int = 0 + @Option(name:.shortAndLong) var frequency: Int = 100000000 + @Option(name:.shortAndLong) var nsamples: Int = 65536 + @Flag(help:"Version \(software_version)") var version: Bool = false + @Flag(name: .shortAndLong) var verbose: Bool = false +} +let args = CommandLineArgs.parseOrExit() +if (args.version) { + print("AirSpyIQ version \(software_version)") + exit(0) +} + +//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() +} + +var version: bladerf_version = bladerf_version() +bladerf_version(&version) +print(version.describe!) + +var device: OpaquePointer? + +var ret: Int32 + +ret = bladerf_open(&device,nil) +print("ret=\(ret)") + +var actual_samplerate: UnsafeMutablePointer = .allocate(capacity: 1) + +ret = bladerf_set_sample_rate(device, 0, UInt32(args.samplerate), actual_samplerate) +print("ret=\(ret)") + +ret = bladerf_set_frequency(device, 0, UInt64(args.frequency)) +print("ret=\(ret)") + + +let NUM_SAMPLES=8192 +let SYNC_TIMEOUT=500 +ret = bladerf_sync_config(device, bladerf_channel_layout(0), BLADERF_FORMAT_SC16_Q11, 16, UInt32(NUM_SAMPLES), 8, UInt32(SYNC_TIMEOUT)) +print("ret=\(ret)") + +ret = bladerf_enable_module(device, 0, true) +print("ret=\(ret)") + + + +let buf_ptr = UnsafeMutableRawPointer.allocate(byteCount: NUM_SAMPLES, alignment: 1) + +var total_samples:Int32=0 +//var count=100 +var done=false +while (!done) { + ret = bladerf_sync_rx(device, buf_ptr, UInt32(NUM_SAMPLES), nil, UInt32(SYNC_TIMEOUT)) + if (ret != 0) { + print("RX failed: \(ret)") + done=true + } else { + print("Received \(NUM_SAMPLES) bytes") + + if let file = fileHandle { + let convertedData = Data(bytes: buf_ptr, count: NUM_SAMPLES) + do { + try file.write(contentsOf: convertedData) + } catch { + print("Cant dump data to file") + } + } + + total_samples += Int32(NUM_SAMPLES) + if total_samples > args.nsamples { + done=true + } + } + //count -= 1 + print("ret=\(ret)") +} + +ret = bladerf_enable_module(device, 0, false) +print("bladerf_enable_module ret=\(ret)") + + +bladerf_close(device) -- cgit v1.2.3