kpal 0.2.2

An extensible and RESTful control system for physical computing
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Models represent the core abstractions of KPAL.
mod errors;

use std::{
    collections::{BTreeMap, HashMap},
    ffi::{CStr, CString},
    slice,
};

use libloading::Library as Dll;
use serde::{Deserialize, Serialize};

use kpal_plugin::Val as PluginValue;

pub use errors::ModelError;

/// A model represents one of the system's core abstractions.
pub trait Model {
    fn id(&self) -> usize;

    fn key() -> &'static str;
}

/// Attributes partially represent the complete state of a peripheral.
///
/// id and value are currenly the only fields that are deserialized because attributes are only
/// sent once during the lifetime of a peripheral before it is initialized. At this time, only id a
/// value are needed.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "variant")]
pub enum Attribute {
    #[serde(rename(serialize = "integer", deserialize = "integer"))]
    Int {
        id: usize,

        #[serde(default)]
        name: String,

        #[serde(default)]
        pre_init: bool,

        value: i32,
    },

    #[serde(rename(serialize = "double", deserialize = "double"))]
    Double {
        id: usize,

        #[serde(default)]
        name: String,

        #[serde(default)]
        pre_init: bool,

        value: f64,
    },

    #[serde(rename(serialize = "string", deserialize = "string"))]
    String {
        id: usize,

        #[serde(default)]
        name: String,

        #[serde(default)]
        pre_init: bool,

        value: String,
    },
    #[serde(rename(serialize = "unsigned_integer", deserialize = "unsigned_integer"))]
    Uint {
        id: usize,

        #[serde(default)]
        name: String,

        #[serde(default)]
        pre_init: bool,

        value: u32,
    },
}

impl Attribute {
    /// Returns the name of an attribute.
    pub fn name(&self) -> &str {
        match self {
            Attribute::Int { name, .. } => name,
            Attribute::Double { name, .. } => name,
            Attribute::String { name, .. } => name,
            Attribute::Uint { name, .. } => name,
        }
    }

    /// Indicates whether an attribute's value may be modified before peripheral initialization.
    pub fn pre_init(&self) -> bool {
        match self {
            Attribute::Int { pre_init, .. } => *pre_init,
            Attribute::Double { pre_init, .. } => *pre_init,
            Attribute::String { pre_init, .. } => *pre_init,
            Attribute::Uint { pre_init, .. } => *pre_init,
        }
    }

    /// Creates a new Attribute instance from a PluginValue.
    ///
    /// This function makes it easier to convert PluginValues, which are returned from the
    /// Peripheral's plugin API, to Attributes, which are passed across the REST API.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to assign to the new attribute
    /// * `id` - The numeric ID of the attribute
    /// * `name` - The attribute's name
    /// * `pre_init` - Detemines whether the attribute may be set before plugin initialization
    pub fn new(
        value: PluginValue,
        id: usize,
        name: String,
        pre_init: bool,
    ) -> Result<Attribute, ModelError> {
        match value {
            PluginValue::Int(value) => Ok(Attribute::Int {
                id,
                name,
                pre_init,
                value,
            }),
            PluginValue::Double(value) => Ok(Attribute::Double {
                id,
                name,
                pre_init,
                value,
            }),
            PluginValue::String(p_value, length) => {
                let value = unsafe {
                    let slice = slice::from_raw_parts(p_value, length);
                    let string = CStr::from_bytes_with_nul(slice)?.to_str()?;
                    string.to_owned()
                };
                Ok(Attribute::String {
                    id,
                    name,
                    pre_init,
                    value,
                })
            }
            PluginValue::Uint(value) => Ok(Attribute::Uint {
                id,
                name,
                pre_init,
                value,
            }),
        }
    }

    /// Returns a new value instance that is created from an attribute.
    pub fn to_value(&self) -> Result<Value, ModelError> {
        let value = match self {
            Attribute::Int { value, .. } => Value::Int { value: *value },
            Attribute::Double { value, .. } => Value::Double { value: *value },
            Attribute::String { value, .. } => {
                let c_string = CString::new(value.clone())?;
                Value::String { value: c_string }
            }
            Attribute::Uint { value, .. } => Value::Uint { value: *value },
        };

        Ok(value)
    }
}

impl Model for Attribute {
    fn id(&self) -> usize {
        match self {
            Attribute::Int { id, .. } => *id,
            Attribute::Double { id, .. } => *id,
            Attribute::String { id, .. } => *id,
            Attribute::Uint { id, .. } => *id,
        }
    }

    fn key() -> &'static str {
        "attributes"
    }
}

impl Eq for Attribute {}

impl PartialEq for Attribute {
    fn eq(&self, other: &Attribute) -> bool {
        match (self, other) {
            (
                Attribute::Int {
                    id: id1,
                    value: value1,
                    ..
                },
                Attribute::Int {
                    id: id2,
                    value: value2,
                    ..
                },
            ) => id1 == id2 && value1 == value2,
            (
                Attribute::Double {
                    id: id1,
                    value: value1,
                    ..
                },
                Attribute::Double {
                    id: id2,
                    value: value2,
                    ..
                },
            ) => id1 == id2 && value1 == value2,
            (
                Attribute::String {
                    id: id1,
                    value: value1,
                    ..
                },
                Attribute::String {
                    id: id2,
                    value: value2,
                    ..
                },
            ) => id1 == id2 && value1 == value2,
            (
                Attribute::Uint {
                    id: id1,
                    value: value1,
                    ..
                },
                Attribute::Uint {
                    id: id2,
                    value: value2,
                    ..
                },
            ) => id1 == id2 && value1 == value2,
            (_, _) => false,
        }
    }
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Library {
    id: usize,
    name: String,
    attributes: BTreeMap<usize, Attribute>,

    #[serde(skip)]
    library: Option<Dll>,
}

impl Clone for Library {
    /// Clones a library by ignoring any dynamic library owned by the model.
    fn clone(&self) -> Self {
        Library {
            id: self.id,
            name: self.name.clone(),
            attributes: self.attributes.clone(),
            library: None,
        }
    }
}

impl Library {
    pub fn new(id: usize, name: String, library: Option<Dll>) -> Library {
        let attributes: BTreeMap<usize, Attribute> = BTreeMap::new();
        Library {
            id,
            name,
            attributes,
            library,
        }
    }

    pub fn dll(&self) -> &Option<Dll> {
        &self.library
    }

    pub fn attributes(&self) -> &BTreeMap<usize, Attribute> {
        &self.attributes
    }

    pub fn set_attributes(&mut self, attributes: BTreeMap<usize, Attribute>) {
        self.attributes = attributes;
    }
}

impl Model for Library {
    fn id(&self) -> usize {
        self.id
    }

    fn key() -> &'static str {
        "libraries"
    }
}

#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct Peripheral {
    library_id: usize,
    name: String,

    #[serde(default, skip_serializing)]
    attributes: BTreeMap<usize, Attribute>,

    #[serde(default)]
    id: usize,

    #[serde(default, rename = "attributes")]
    links: Vec<HashMap<String, String>>,
}

impl Peripheral {
    pub fn attributes(&self) -> &BTreeMap<usize, Attribute> {
        &self.attributes
    }

    pub fn library_id(&self) -> usize {
        self.library_id
    }

    pub fn set_attribute(&mut self, id: usize, attribute: Attribute) {
        match self.attributes.get_mut(&id) {
            Some(old_attribute) => *old_attribute = attribute,
            None => {
                log::debug!("could not set attribute; index not valid: {}", id);
            }
        }
    }

    pub fn set_attributes(&mut self, attributes: BTreeMap<usize, Attribute>) {
        self.attributes = attributes;
    }

    pub fn set_attribute_from_value(
        &mut self,
        id: usize,
        value: PluginValue,
    ) -> Result<(), ModelError> {
        let attribute = self.attributes.get_mut(&id).unwrap();
        *attribute = Attribute::new(value, id, attribute.name().to_owned(), attribute.pre_init())?;
        Ok(())
    }

    pub fn set_attribute_links(&mut self) {
        let mut links: Vec<HashMap<String, String>> = Vec::new();
        for attr in self.attributes.values() {
            let mut link = HashMap::new();
            link.insert(
                "href".to_string(),
                format!("/api/v0/peripherals/{}/attributes/{}", self.id, attr.id()),
            );
            links.push(link);
        }

        self.links = links;
    }

    pub fn set_id(&mut self, id: usize) {
        self.id = id;
    }
}

impl Model for Peripheral {
    fn id(&self) -> usize {
        self.id
    }

    fn key() -> &'static str {
        "peripherals"
    }
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "variant")]
/// A Value represents the current value of an Attribute.
///
/// This enum is used to translate between values received from requests to update an attribute's
/// state and values understood by the plugin API.
pub enum Value {
    #[serde(rename(serialize = "integer", deserialize = "integer"))]
    Int { value: i32 },
    #[serde(rename(serialize = "double", deserialize = "double"))]
    Double { value: f64 },
    #[serde(rename(serialize = "string", deserialize = "string"))]
    String { value: CString },
    #[serde(rename(serialize = "unsigned_integer", deserialize = "unsigned_integer"))]
    Uint { value: u32 },
}

impl Value {
    pub fn as_val(&self) -> PluginValue {
        match self {
            Value::Int { value } => PluginValue::Int(*value),
            Value::Double { value } => PluginValue::Double(*value),
            Value::String { value } => {
                let slice = value.as_bytes_with_nul();
                PluginValue::String(slice.as_ptr(), slice.len())
            }
            Value::Uint { value } => PluginValue::Uint(*value),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::f64::consts::PI;

    use kpal_plugin::Val as PluginValue;

    #[test]
    fn test_attribute_new() {
        let context = set_up();
        let cases = vec![
            (
                PluginValue::Int(context.int_value),
                context.int_id,
                context.attributes.get(&context.int_id).unwrap(),
            ),
            (
                PluginValue::Double(context.float_value),
                context.float_id,
                context.attributes.get(&context.float_id).unwrap(),
            ),
        ];

        for (value, id, attr) in cases {
            let converted_attr =
                Attribute::new(value, id, context.name.clone(), context.pre_init).unwrap();
            assert_eq!(attr, &converted_attr);
        }
    }

    #[test]
    fn test_attribute_id() {
        let context = set_up();

        for (id, attr) in context.attributes {
            assert_eq!(id, attr.id());
        }
    }

    #[test]
    fn test_attribute_name() {
        let context = set_up();
        let cases = vec![
            (
                context.name.clone(),
                context.attributes.get(&context.int_id).unwrap(),
            ),
            (
                context.name.clone(),
                context.attributes.get(&context.float_id).unwrap(),
            ),
        ];

        for case in cases {
            let (name, attr) = case;
            assert_eq!(name, attr.name());
        }
    }

    #[test]
    fn test_library_new() {
        let context = set_up();
        let library = Library::new(0, context.name.clone(), None);

        assert_eq!(library.id, 0);
        assert_eq!(library.name, context.name);
        assert!(library.library.is_none());
    }

    #[test]
    fn test_library_dll() {
        let context = set_up();
        let library = Library {
            id: 0,
            name: context.name,
            attributes: context.attributes,
            library: None,
        };

        assert!(library.dll().is_none());
    }

    #[test]
    fn test_peripheral_attributes() {
        let context = set_up();
        assert_eq!(*context.peripheral.attributes(), context.attributes);
    }

    #[test]
    fn test_peripheral_library_id() {
        let context = set_up();
        assert_eq!(context.peripheral.library_id(), context.library_id);
    }

    #[test]
    fn test_peripheral_set_attribute() {
        let mut context = set_up();
        let new_attr = Attribute::Double {
            id: context.float_id,
            name: context.name,
            pre_init: context.pre_init,
            value: PI,
        };

        assert_ne!(
            context
                .peripheral
                .attributes
                .get(&context.float_id)
                .unwrap(),
            &new_attr
        );

        context
            .peripheral
            .set_attribute(context.float_id, new_attr.clone());
        assert_eq!(
            context
                .peripheral
                .attributes
                .get(&context.float_id)
                .unwrap(),
            &new_attr
        );
    }

    #[test]
    fn test_peripheral_set_attributes() {
        let mut context = set_up();
        let new_attr = Attribute::Double {
            id: context.float_id,
            name: context.name.clone(),
            pre_init: context.pre_init,
            value: PI,
        };
        let mut new_attrs = BTreeMap::new();
        new_attrs.insert(context.float_id, new_attr.clone());

        for attr in context.peripheral.attributes.clone().values() {
            assert_ne!(attr, &new_attr);
        }

        context.peripheral.set_attributes(new_attrs);
        for (id, attr) in context.peripheral.attributes {
            assert_eq!(attr, new_attr);
            assert_eq!(context.float_id, id);
        }
    }

    #[test]
    fn test_peripheral_set_attribute_from_value() {
        let mut context = set_up();
        let new_value = PluginValue::Double(PI);
        let new_attr = Attribute::Double {
            id: context.float_id,
            name: context.name.clone(),
            pre_init: context.pre_init,
            value: PI,
        };

        assert_ne!(context.peripheral.attributes.get(&0).unwrap(), &new_attr);

        context
            .peripheral
            .set_attribute_from_value(context.float_id, new_value)
            .unwrap();
        assert_eq!(
            context
                .peripheral
                .attributes
                .get(&context.float_id)
                .unwrap(),
            &new_attr
        );
    }

    struct Context {
        attributes: BTreeMap<usize, Attribute>,
        float_id: usize,
        float_value: f64,
        int_id: usize,
        int_value: i32,
        library_id: usize,
        name: String,
        peripheral: Peripheral,
        pre_init: bool,
    }

    fn set_up() -> Context {
        let (name, int_value, float_value) = (String::from("foo"), 42, 42.42);
        let (int_id, float_id) = (0, 1);
        let library_id = 1;
        let pre_init = false;
        let mut attributes: BTreeMap<usize, Attribute> = BTreeMap::new();
        attributes.insert(
            int_id,
            Attribute::Int {
                id: int_id,
                name: name.clone(),
                pre_init,
                value: int_value,
            },
        );
        attributes.insert(
            float_id,
            Attribute::Double {
                id: float_id,
                name: name.clone(),
                pre_init,
                value: float_value,
            },
        );

        let mut peripheral = Peripheral {
            library_id,
            name: name.clone(),
            attributes: attributes.clone(),
            id: 0,
            links: Vec::new(),
        };
        peripheral.set_attribute_links();

        Context {
            attributes,
            float_id,
            float_value,
            int_id,
            int_value,
            library_id,
            name,
            peripheral,
            pre_init,
        }
    }
}