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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#![deny(missing_docs)]

//! The data structures for representing a XIO hardware database.

#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate serde_hex;

#[cfg(test)]
#[macro_use]
extern crate serde_json;

#[cfg(not(test))]
extern crate serde_json;

extern crate indexmap;
extern crate ommui_file_loading;
extern crate ommui_string_patterns;
extern crate serde;
extern crate uuid;
extern crate xio_base_datatypes;

use ommui_string_patterns::{freetextstring, idstring};
use std::collections::BTreeMap;
use uuid::Uuid;
use xio_base_datatypes as base;

mod hexnumber;

/// Functionality for loading a XIO hardware database from files.
pub mod filesystem;

/// The description of a device.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DeviceDescription {
    /// Description of the FTDI flashing chip.
    pub ftdi: FtdiDescription,

    /// Description of the XIO properties.
    pub xio: Option<XioDescription>,

    /// Description of the system.
    pub system: SystemDescription,

    /// Summary of the device status.
    pub summary: SummaryDescription,
}

/// The description of a connected device recognized by FTDI chip.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FtdiDescription {
    /// The string identifier stored in the FTDI EEPROM.
    pub id: String,

    /// The vendor string stored in the FTDI EEPROM.
    pub vendor: String,

    /// The product string stored in the FTDI EEPROM.
    pub product: String,

    /// The uuid stored in the FTDI EEPROM.
    pub uuid: Uuid,

    /// A flag indicating if the device is recognized.
    pub recognized: bool,

    /// A flag indicating if the device is programmed (a.k.a. flashed).
    pub programmed: bool,

    /// Board identifier string.
    pub board: String,

    /// Device path.
    pub device: String,

    /// Named symlink to the device.
    pub symlink: String,
}

/// The description of a connected device recognized through USB.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct XioDescription {
    /// Id of the device.
    pub id: String,

    /// USB vendor string of the device.
    pub vendor: String,

    /// USB product string of the device.
    pub product: String,

    /// Device uuid.
    pub uuid: Uuid,

    /// Flag indicating if the device is recognized.
    pub recognized: bool,

    /// Flag indicating if the device is programmed (a.k.a. flashed).
    pub programmed: bool,

    /// Board identifier string.
    pub board: String,

    /// Device path.
    pub device: String,

    /// Named symlink to the device.
    pub symlink: String,

    /// Version string of the firmware running on the device.
    pub version: String,
}

/// Systems description of a XIO device.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SystemDescription {
    /// Version of the device.
    pub version: String,

    /// Version string of the firmware running on the device.
    pub firmware: String,
}

/// Summary information of a XIO device.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SummaryDescription {
    /// Flag indicating if the device is ready.
    pub ready: bool,
}

/// Description of a XIO device hardware board type.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HardwareBoardDescription {
    /// Name of the board.
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    /// Generic description of the board.
    pub description: GenericDescription,

    /// Capabilities provided by the board.
    pub capabilities: BTreeMap<String, Capability>,
}

/// Generic description of some device properties.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GenericDescription {
    /// Vendor of the device.
    #[serde(with = "freetextstring")]
    pub vendor: String,

    /// Model name of the device.
    #[serde(with = "freetextstring")]
    pub model: String,
}

/// A capability implemented in a XIO device.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Capability {
    /// Flag indicating if the capability is built into the device.
    pub builtin: bool,

    /// String identifier of the module type.
    ///
    /// The module must be present in the modules collection that
    /// can be loaded through the `filesystem::HardwareDatabase`.
    #[serde(with = "idstring")]
    pub module: String,

    /// The index number of the capability within the XIO device.
    #[serde(with = "hexnumber")]
    pub id: u16,
}

#[cfg(test)]
mod capability_tests {
    extern crate serde_json;
    use super::Capability;

    #[test]
    fn deserialize_hex() {
        let json =
            json!({ "builtin": true, "module": "xyz", "id": "0x1234"});
        let cap: Capability =
            serde_json::from_str(&json.to_string()).unwrap();
        assert_eq!(
            cap,
            Capability {
                builtin: true,
                module: "xyz".to_string(),
                id: 0x1234,
            }
        );
        assert_eq!(serde_json::to_value(cap).unwrap(), json);
    }

    #[test]
    fn deserialize_hex_zero() {
        let json =
            json!({ "builtin": true, "module": "xyz", "id": "0x0000"});
        let cap: Capability =
            serde_json::from_str(&json.to_string()).unwrap();
        assert_eq!(
            cap,
            Capability {
                builtin: true,
                module: "xyz".to_string(),
                id: 0,
            }
        );
        assert_eq!(serde_json::to_value(cap).unwrap(), json);
    }

    #[test]
    fn deserialize() {
        let json = json!({ "builtin": true, "module": "xyz", "id": 1234});
        let cap: Capability =
            serde_json::from_str(&json.to_string()).unwrap();
        assert_eq!(
            cap,
            Capability {
                builtin: true,
                module: "xyz".to_string(),
                id: 1234,
            }
        );
        let json_hex =
            json!({ "builtin": true, "module": "xyz", "id": "0x04d2"});
        assert_eq!(serde_json::to_value(cap).unwrap(), json_hex);
    }
}

/// Description of a XIO module.
///
/// This describes the functionality that can be provided by a
/// module. A XIO board can have multiple capabilities referencing
/// the same module description. If a board has e.g. two A/D converters
/// of the same type, there are two capabilities which reference the
/// same module id.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Module {
    /// Caption of the module.
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    /// Generic description of the module.
    pub description: GenericDescription,

    /// List of channels provided by the module.
    pub channels: Vec<Channel>,
}

/// Description of a channel available in a XIO module.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Channel {
    /// String identifier of the module.
    ///
    /// Must be unique among all channels that are contained in a module.
    #[serde(with = "idstring")]
    pub id: String,

    /// Caption of the channel.
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    /// Dataype of the channel value.
    #[serde(rename = "type")]
    pub channel_type: base::DataType,

    /// Access permissions of the channel.
    pub permission: base::ChannelPermission,
}