|
1 | 1 | // Copyright (C) 2016, Paul Osborne <[email protected]>
|
| 2 | + |
| 3 | +use toml; |
| 4 | +use rustc_serialize::{Decodable}; |
| 5 | +use std::io; |
| 6 | +use std::path::Path; |
| 7 | +use std::collections::HashMap; |
| 8 | +use sysfs_gpio::Direction; |
| 9 | + |
| 10 | +#[derive(RustcDecodable, Debug)] |
| 11 | +pub struct PinConfig { |
| 12 | + num: u64, |
| 13 | + direction: Option<String>, |
| 14 | + aliases: Option<Vec<String>>, |
| 15 | + export: Option<bool>, |
| 16 | + active_low: Option<bool>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(RustcDecodable, Debug)] |
| 20 | +pub struct GpioConfig { |
| 21 | + pins: HashMap<String, PinConfig>, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug)] |
| 25 | +pub enum Error { |
| 26 | + IoError(io::Error), |
| 27 | + ParseError, |
| 28 | + NoConfigFound, |
| 29 | +} |
| 30 | + |
| 31 | +fn to_direction(dirstr: &str) -> Option<Direction> { |
| 32 | + match dirstr { |
| 33 | + "in" => Some(Direction::In), |
| 34 | + "out" => Some(Direction::Out), |
| 35 | + "high" => Some(Direction::High), |
| 36 | + "low" => Some(Direction::Low), |
| 37 | + _ => None |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl GpioConfig { |
| 42 | + |
| 43 | + /// Load a GPIO Config from the system |
| 44 | + /// |
| 45 | + /// This function will load the GPIO configuration from standard system |
| 46 | + /// locations as well as from the additional configs passed in via the |
| 47 | + /// `configs` parameter. Each parameter is expected to be a path to a |
| 48 | + /// config file in disk. |
| 49 | + /// |
| 50 | + /// Under the covers, this function will attempt to discover configuration |
| 51 | + /// files in the following standard locations in order: |
| 52 | + /// |
| 53 | + /// - `/etc/gpio.toml` |
| 54 | + /// - `/etc/gpio.d/*.toml` |
| 55 | + /// - `configs` (parameter) |
| 56 | + /// |
| 57 | + /// Each config file found in these locations will be loaded and then they |
| 58 | + /// will be pulled together to form a unified configuration via the |
| 59 | + /// `combine` method. |
| 60 | + pub fn load(configs: Vec<String>) -> Result<GpioConfig, Error> { |
| 61 | + Err(Error::NoConfigFound) |
| 62 | + } |
| 63 | + |
| 64 | + /// Load a GPIO configuration for the provided toml string |
| 65 | + pub fn from_str(config: &str) -> Result<GpioConfig, Error> { |
| 66 | + let root = toml::Parser::new(config).parse().unwrap(); |
| 67 | + let mut d = toml::Decoder::new(toml::Value::Table(root)); |
| 68 | + Ok(Decodable::decode(&mut d).unwrap()) |
| 69 | + } |
| 70 | + |
| 71 | + /// Load a GPIO config from the specified path |
| 72 | + pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GpioConfig, Error> { |
| 73 | + Err(Error::NoConfigFound) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod test { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_parse_basic() { |
| 83 | + let configstr = r#" |
| 84 | +# Basic Form |
| 85 | +[pins.reset_button] |
| 86 | +num = 73 # required |
| 87 | +direction = "in" # default: in |
| 88 | +active_low = true # default: false |
| 89 | +export = true # default: true |
| 90 | +
|
| 91 | +[pins.status_led] |
| 92 | +num = 37 |
| 93 | +aliases = ["A27", "green_led"] |
| 94 | +direction = "out" |
| 95 | +
|
| 96 | +# Compact Form |
| 97 | +[pins] |
| 98 | +error_led = { num = 11, direction = "in", export = false} |
| 99 | +"#; |
| 100 | + let config = GpioConfig::from_str(configstr).unwrap(); |
| 101 | + let status_led = config.pins.get("status_led").unwrap(); |
| 102 | + assert_eq!(status_led.num, 37); |
| 103 | + assert_eq!(status_led.aliases, |
| 104 | + Some(vec!(String::from("A27"), |
| 105 | + String::from("green_led")))); |
| 106 | + assert_eq!(status_led.direction, Some(String::from("out"))); |
| 107 | + assert_eq!(status_led.active_low, None); |
| 108 | + assert_eq!(status_led.export, None); |
| 109 | + } |
| 110 | +} |
0 commit comments