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
285
286
287
288
289
#[macro_use]
extern crate serde_derive;

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

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

extern crate failure;
extern crate ommui_file_loading;
extern crate ommui_string_patterns;
extern crate serde;
extern crate uuid;

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

mod hexnumber;

pub mod filesystem;

pub type InstructionMap = BTreeMap<String, Instruction>;
type Result<T> = std::result::Result<T, failure::Error>;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Instruction {
    pub code: u16,

    #[serde(with = "freetextstring")]
    pub description: String,

    #[serde(with = "freetextstring")]
    pub format_string: String,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<InstructionParameter>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct InstructionParameter {
    #[serde(with = "idstring")]
    pub id: String,

    #[serde(rename = "type")]
    pub parameter_type: Vec<DataType>,

    pub storage: StorageTypeWithMixed,

    #[serde(with = "freetextstring")]
    pub description: String,

    #[serde(with = "freetextstring")]
    pub format_string: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
#[repr(u8)]
pub enum DataType {
    Boolean = 0x00,
    Int8 = 0x01,
    Int16 = 0x02,
    Int32 = 0x03,
    Int64 = 0x04,
    UInt8 = 0x05,
    UInt16 = 0x06,
    UInt32 = 0x07,
    UInt64 = 0x08,
    ParameterMask = 0x09,
    Invalid = 0xFF,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[repr(u8)]
pub enum StorageType {
    Channel = 0b01,
    Fixed = 0b10,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[repr(u8)]
pub enum StorageTypeWithMixed {
    Channel = 0b01,
    Fixed = 0b10,
    ChannelOrFixed = 0b11,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Job {
    pub commands: Vec<Command>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Command {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub command_type: String,

    // TODO: verify that the key is an idstring
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, String>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conditions: Vec<Condition>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Condition {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub command_type: String,

    pub exit_job: bool,

    // TODO: verify that the key is an idstring
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DeviceDescription {
    pub ftdi: FtdiDescription,
    pub xio: Option<XioDescription>,
    pub system: SystemDescription,
    pub summary: SummaryDescription,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FtdiDescription {
    pub id: String,
    pub vendor: String,
    pub product: String,
    pub uuid: Uuid,
    pub recognized: bool,
    pub programmed: bool,
    pub board: String,
    pub device: String,
    pub symlink: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct XioDescription {
    pub id: String,
    pub vendor: String,
    pub product: String,
    pub uuid: Uuid,
    pub recognized: bool,
    pub programmed: bool,
    pub board: String,
    pub device: String,
    pub symlink: String,
    pub version: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SystemDescription {
    pub version: String,
    pub firmware: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SummaryDescription {
    pub ready: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HardwareBoardDescription {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    pub description: GenericDescription,

    // TODO: verify that the key is an idstring
    pub capabilities: BTreeMap<String, Capability>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GenericDescription {
    #[serde(with = "freetextstring")]
    pub vendor: String,

    #[serde(with = "freetextstring")]
    pub model: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Capability {
    pub builtin: bool,

    #[serde(with = "idstring")]
    pub module: String,

    #[serde(with = "hexnumber")]
    pub id: u16,
}

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

    #[test]
    fn deserialize() {
        #[cfg_attr(rustfmt, rustfmt_skip)]
        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,
            }
        );
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Module {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    pub description: GenericDescription,

    pub channels: Vec<Channel>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Channel {
    #[serde(with = "idstring")]
    pub id: String,

    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub channel_type: DataType,

    pub permission: ChannelPermission,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[repr(u8)]
pub enum ChannelPermission {
    ReadOnly,
    ReadOnlyAfterStart,
    ReadWrite,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", untagged)]
pub enum ParameterValue {
    Boolean(bool),
    UInt8(u8),
    UInt16(u16),
    UInt32(u32),
    UInt64(u64),
    Int8(i8),
    Int16(i16),
    Int32(i32),
    Int64(i64),
    ParameterMask(u64),
}