From 6454315cd23336aacdff283c9a44eeddae8e38c2 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 8 Jul 2020 19:24:26 +0800 Subject: [PATCH] config: refactor and share --- src/runtime/src/comms.rs | 10 +++------- src/runtime/src/config.rs | 34 +++++++++++++++++++++------------ src/runtime/src/main.rs | 11 +++++++++-- src/runtime/src/net_settings.rs | 20 +++++++++---------- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 8e67934..929d5a8 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -290,8 +290,8 @@ async fn handle_connection(stream: &TcpStream, control: Rc>; - let cfg = config::Config::new(); - startup_kernel = cfg.as_ref().ok().and_then(|cfg| cfg.read("startup").ok()); - Sockets::init(32); let control: Rc> = Rc::new(RefCell::new(kernel::Control::start())); - if let Some(buffer) = startup_kernel { + if let Ok(buffer) = cfg.read("startup") { info!("Loading startup kernel..."); if let Ok(()) = task::block_on(load_kernel(buffer, &control, None)) { info!("Starting startup kernel..."); diff --git a/src/runtime/src/config.rs b/src/runtime/src/config.rs index 6d7b225..bff5fb0 100644 --- a/src/runtime/src/config.rs +++ b/src/runtime/src/config.rs @@ -11,6 +11,7 @@ pub enum Error<'a> { IoError(io::Error), Utf8Error(FromUtf8Error), KeyNotFoundError(&'a str), + NoConfig, } pub type Result<'a, T> = core::result::Result>; @@ -22,6 +23,7 @@ impl<'a> fmt::Display for Error<'a> { Error::IoError(error) => write!(f, "I/O error: {}", error), Error::Utf8Error(error) => write!(f, "UTF-8 error: {}", error), Error::KeyNotFoundError(name) => write!(f, "Configuration key `{}` not found", name), + Error::NoConfig => write!(f, "Configuration not present"), } } } @@ -61,7 +63,7 @@ fn parse_config<'a>( } pub struct Config { - fs: fatfs::FileSystem, + fs: Option>, } impl Config { @@ -74,20 +76,28 @@ impl Config { let reader = sd_reader::SdReader::new(sd); let fs = reader.mount_fatfs(sd_reader::PartitionEntry::Entry1)?; - Ok(Config { fs }) + Ok(Config { fs: Some(fs) }) + } + + pub fn new_dummy() -> Self { + Config { fs: None } } pub fn read<'b>(&self, key: &'b str) -> Result<'b, Vec> { - let root_dir = self.fs.root_dir(); - let mut buffer: Vec = Vec::new(); - match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) { - Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?, - Err(_) => match root_dir.open_file("/CONFIG.TXT") { - Ok(f) => parse_config(key, &mut buffer, f)?, - Err(_) => return Err(Error::KeyNotFoundError(key)), - }, - }; - Ok(buffer) + if let Some(fs) = &self.fs { + let root_dir = fs.root_dir(); + let mut buffer: Vec = Vec::new(); + match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) { + Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?, + Err(_) => match root_dir.open_file("/CONFIG.TXT") { + Ok(f) => parse_config(key, &mut buffer, f)?, + Err(_) => return Err(Error::KeyNotFoundError(key)), + }, + }; + Ok(buffer) + } else { + Err(Error::NoConfig) + } } pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> { diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index fd6252c..6fe6af9 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -7,7 +7,7 @@ extern crate alloc; use core::{cmp, str}; -use log::info; +use log::{info, warn}; use libboard_zynq::{timer::GlobalTimer, logger, devc, slcr}; use libsupport_zynq::ram; @@ -93,5 +93,12 @@ pub fn main_core0() { pl::csr::rtio_core::reset_phy_write(1); } - comms::main(timer); + let cfg = match config::Config::new() { + Ok(cfg) => cfg, + Err(err) => { + warn!("config initialization failed: {}", err); + config::Config::new_dummy() + } + }; + comms::main(timer, &cfg); } diff --git a/src/runtime/src/net_settings.rs b/src/runtime/src/net_settings.rs index 6cf560b..9bef1ea 100644 --- a/src/runtime/src/net_settings.rs +++ b/src/runtime/src/net_settings.rs @@ -23,21 +23,19 @@ impl fmt::Display for NetAddresses { } } -pub fn get_adresses() -> NetAddresses { +pub fn get_adresses(cfg: &config::Config) -> NetAddresses { let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x52]); let mut ipv4_addr = IpAddress::v4(192, 168, 1, 52); let mut ipv6_addr = None; - if let Ok(cfg) = config::Config::new() { - if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) { - hardware_addr = addr; - } - if let Ok(Ok(addr)) = cfg.read_str("ip").map(|s| s.parse()) { - ipv4_addr = addr; - } - if let Ok(Ok(addr)) = cfg.read_str("ip6").map(|s| s.parse()) { - ipv6_addr = Some(addr); - } + if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) { + hardware_addr = addr; + } + if let Ok(Ok(addr)) = cfg.read_str("ip").map(|s| s.parse()) { + ipv4_addr = addr; + } + if let Ok(Ok(addr)) = cfg.read_str("ip6").map(|s| s.parse()) { + ipv6_addr = Some(addr); } let ipv6_ll_addr = IpAddress::v6(