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
use soapysdr_sys::*;
use std::slice;

use std::ffi::CStr;
use std::os::raw::c_char;

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ArgType {
    Bool,
    Float,
    Int,
    String,
    __Nonexhaustive,
}

impl From<SoapySDRArgInfoType> for ArgType {
    #[allow(non_upper_case_globals)]
    fn from(arg_info_type: SoapySDRArgInfoType) -> Self {
        match arg_info_type {
            SoapySDRArgInfoType_SOAPY_SDR_ARG_INFO_BOOL => ArgType::Bool,
            SoapySDRArgInfoType_SOAPY_SDR_ARG_INFO_FLOAT => ArgType::Float,
            SoapySDRArgInfoType_SOAPY_SDR_ARG_INFO_INT => ArgType::Int,
            SoapySDRArgInfoType_SOAPY_SDR_ARG_INFO_STRING => ArgType::String,
            _ => ArgType::__Nonexhaustive,
        }
    }
}

/// Metadata about supported arguments.
pub struct ArgInfo {
    /// The key used to identify the argument
    pub key: String,

    /// The default value of the argument when not specified
    pub value: String,

    /// The displayable name of the argument
    pub name: Option<String>,

    ///  A brief description about the argument
    pub description: Option<String>,

    /// The units of the argument: dB, Hz, etc
    pub units: Option<String>,

    /// The data type of the argument
    pub data_type: ArgType,

    /// A discrete list of possible values.
    ///
    /// When specified, the argument should be restricted to this options set.
    pub options: Vec<(String, Option<String>)>,
}

unsafe fn required_string(s: *mut c_char) -> String {
    assert!(!s.is_null(), "Null string from SoapySDR");
    CStr::from_ptr(s).to_string_lossy().into()
}

unsafe fn optional_string(s: *mut c_char) -> Option<String> {
    if !s.is_null() {
        Some(CStr::from_ptr(s).to_string_lossy().into())
    } else {
        None
    }
}

pub unsafe fn arg_info_from_c(c: &SoapySDRArgInfo) -> ArgInfo {
    ArgInfo {
        key:         required_string(c.key),
        value:       required_string(c.value),
        name:        optional_string(c.name),
        description: optional_string(c.description),
        units:       optional_string(c.units),
        data_type:   c.type_.into(),
        options: {
            let option_vals = slice::from_raw_parts(c.options, c.numOptions);
            let option_names = slice::from_raw_parts(c.optionNames, c.numOptions);
            option_vals.iter().zip(option_names.iter()).map(|(&name, &val)| {
                (required_string(name), optional_string(val))
            }).collect()
        }
    }
}