Skip to content
This repository has been archived by the owner on Jul 4, 2021. It is now read-only.

Commit

Permalink
Started custom errors
Browse files Browse the repository at this point in the history
Started adding custom errors. Still need to figure out how to chain errors.
  • Loading branch information
mattcburns committed Aug 15, 2020
1 parent 41f30f5 commit 8a7dcbb
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 56 deletions.
36 changes: 36 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::fmt;

#[derive(Debug)]
pub struct RegistrationError {
pub details: String
}

impl RegistrationError {
pub fn new(msg: &str) -> RegistrationError {
RegistrationError{details: msg.to_string()}
}
}

impl fmt::Display for RegistrationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}

#[derive(Debug)]
pub struct SerialError {
pub details: String
}

impl SerialError {
pub fn new(msg: &str) -> SerialError {
SerialError{details: msg.to_string()}
}
}

impl fmt::Display for SerialError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}

62 changes: 6 additions & 56 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
use std::error::Error;
mod errors;
mod register;

use rustbus::{RpcConn, MessageBuilder, client_conn::Timeout, params::Container};
use rustbus::params::{Param, Base};

fn main() -> Result<(), Box<dyn Error>> {
println!("Serial: {:?}", get_serial_number()?);
Ok(())
}

fn get_serial_number() -> Result<String, Box<dyn Error>> {
let mut rpc_con = match RpcConn::system_conn(Timeout::Infinite) {
Ok(rpc_con) => rpc_con,
Err(e) => panic!(e),
};


let mut sig = MessageBuilder::new()
.call("Get".into())
.on("/xyz/openbmc_project/inventory/system".into())
.with_interface("org.freedesktop.DBus.Properties".into())
.at("xyz.openbmc_project.Inventory.Manager".into())
.build();

// Additional params to retrieve the AssetTag properties.
sig.body.push_param2(
String::from("xyz.openbmc_project.Inventory.Decorator.AssetTag").as_str(),
String::from("AssetTag").as_str())
.expect("Unable to push_param2");
let serial = match rpc_con.send_message(&mut sig, Timeout::Infinite){
Ok(serial) => serial,
Err(e) => panic!(e),
};
let resp = match rpc_con.wait_response(serial, Timeout::Infinite){
Ok(resp) => resp,
Err(e) => panic!(e),
fn main() {
match register::register_bmc() {
Ok(()) => println!("Registered!"),
Err(e) => println!("Unable to register: {:?}", e),
};

let message = match resp.unmarshall_all(){
Ok(message) => message,
Err(e) => panic!(e),
};

let first_param = &message.params[0];


// This is super ugly, need to figure out how to improve
if let Param::Container(v) = first_param {
if let Container::Variant(x) = v {
if let Param::Base(y) = &x.value {
if let Base::String(z) = &y {
return Ok(z.to_string());
}
}
}
}

panic!("Unable to parse serial");
}
76 changes: 76 additions & 0 deletions src/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use rustbus::{RpcConn, MessageBuilder, client_conn::Timeout, params::Container};
use rustbus::params::{Param, Base};

use crate::errors::{RegistrationError, SerialError};

pub fn register_bmc() -> Result<(), RegistrationError> {
let mut conn = match openconn() {
Ok(conn) => conn,
Err(_) => return Err(RegistrationError::new("Unable to open connection.")),
};

let serial = match get_serial_number(&mut conn) {
Ok(serial) => serial,
Err(_) => return Err(RegistrationError::new("Unable to read serial number.")),
};
println!("{:?}", serial);

Ok(())

}

fn openconn() -> Result<RpcConn, rustbus::client_conn::Error> {
match RpcConn::system_conn(Timeout::Infinite) {
Ok(rpc_conn) => return Ok(rpc_conn),
Err(e) => return Err(e),
};
}

fn get_serial_number(rpc_conn: &mut RpcConn) -> Result<String, SerialError> {


let mut sig = MessageBuilder::new()
.call("Get".into())
.on("/xyz/openbmc_project/inventory/system".into())
.with_interface("org.freedesktop.DBus.Properties".into())
.at("xyz.openbmc_project.Inventory.Manager".into())
.build();

// Additional params to retrieve the AssetTag properties.
match sig.body.push_param2(
String::from("xyz.openbmc_project.Inventory.Decorator.AssetTag").as_str(),
String::from("AssetTag").as_str()) {
Ok(()) => {},
Err(_) => return Err(SerialError::new("Unable to push params.")),
};

let serial = match rpc_conn.send_message(&mut sig, Timeout::Infinite){
Ok(serial) => serial,
Err(_) => return Err(SerialError::new("Unable to send message to bus.")),
};
let resp = match rpc_conn.wait_response(serial, Timeout::Infinite){
Ok(resp) => resp,
Err(_) => return Err(SerialError::new("Never got a response from bus.")),
};

let message = match resp.unmarshall_all(){
Ok(message) => message,
Err(_) => return Err(SerialError::new("Unable to unmarshall message")),
};

let first_param = &message.params[0];


// This is super ugly, need to figure out how to improve
if let Param::Container(v) = first_param {
if let Container::Variant(x) = v {
if let Param::Base(y) = &x.value {
if let Base::String(z) = &y {
return Ok(z.to_string());
}
}
}
}

Err(SerialError::new("Unable to parse serial"))
}

0 comments on commit 8a7dcbb

Please sign in to comment.