Skip to content

Commit

Permalink
feat: add actual error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Mar 11, 2024
1 parent f75b261 commit 5a2bb9a
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 23 deletions.
227 changes: 227 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
color-eyre = "0.6.2"
nix = { version = "0.27.1", features = ["ioctl"] }
num-derive = "0.4.2"
num-traits = "0.2.18"
thiserror = "1.0.57"
13 changes: 9 additions & 4 deletions src/bin/ectool.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use color_eyre::eyre::Result;
use crosec_rs::commands::get_chip_info::ec_cmd_get_chip_info;
use crosec_rs::commands::hello::ec_cmd_hello;
use crosec_rs::commands::version::ec_cmd_version;

fn main() {
fn main() -> Result<()> {
color_eyre::install()?;

println!("Hello command");
let status = ec_cmd_hello();
let status = ec_cmd_hello()?;
if status {
println!("EC says hello!");
} else {
println!("EC did not say hello :(");
}

println!("Version command");
let (ro_ver, rw_ver, firmware_copy, build_info, tool_version) = ec_cmd_version();
let (ro_ver, rw_ver, firmware_copy, build_info, tool_version) = ec_cmd_version()?;
println!("RO version: {ro_ver}");
println!("RW version: {rw_ver}");
println!("Firmware copy: {firmware_copy}");
println!("Build info: {build_info}");
println!("Tool version: {tool_version}");

println!("Chip info command");
let (vendor, name, revision) = ec_cmd_get_chip_info();
let (vendor, name, revision) = ec_cmd_get_chip_info()?;
println!("Chip info:");
println!(" vendor: {vendor}");
println!(" name: {name}");
println!(" revision: {revision}");

Ok(())
}
8 changes: 4 additions & 4 deletions src/commands/get_chip_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::commands::CrosEcCmds;
use crate::crosec::dev::ec_command;
use crate::crosec::EcCmdResult;
use std::mem::size_of;
use std::slice;

Expand All @@ -10,7 +11,7 @@ struct EcResponseGetChipInfo {
revision: [u8; 32],
}

pub fn ec_cmd_get_chip_info() -> (String, String, String) {
pub fn ec_cmd_get_chip_info() -> EcCmdResult<(String, String, String)> {
let params = EcResponseGetChipInfo {
vendor: [0; 32],
name: [0; 32],
Expand All @@ -21,13 +22,12 @@ pub fn ec_cmd_get_chip_info() -> (String, String, String) {
let params_slice =
unsafe { slice::from_raw_parts(params_ptr, size_of::<EcResponseGetChipInfo>()) };

let result = ec_command(CrosEcCmds::GetChipInfo as u32, 0, params_slice)
.unwrap_or_else(|error| panic!("EC error: {error:?}"));
let result = ec_command(CrosEcCmds::GetChipInfo as u32, 0, params_slice)?;
let response: EcResponseGetChipInfo = unsafe { std::ptr::read(result.as_ptr() as *const _) };

let vendor = String::from_utf8(response.vendor.to_vec()).unwrap_or(String::from(""));
let name = String::from_utf8(response.name.to_vec()).unwrap_or(String::from(""));
let revision = String::from_utf8(response.revision.to_vec()).unwrap_or(String::from(""));

(vendor, name, revision)
Ok((vendor, name, revision))
}
9 changes: 5 additions & 4 deletions src/commands/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::commands::CrosEcCmds;
use crate::crosec::dev::ec_command;
use crate::crosec::EcCmdResult;
use std::mem::size_of;
use std::slice;

Expand All @@ -16,15 +17,15 @@ struct EcResponseHello {
out_data: u32,
}

pub fn ec_cmd_hello() -> bool {
pub fn ec_cmd_hello() -> EcCmdResult<bool> {
let params = EcParamsHello {
in_data: INPUT_DATA,
};
let params_ptr = &params as *const _ as *const u8;
let params_slice = unsafe { slice::from_raw_parts(params_ptr, size_of::<EcParamsHello>()) };

let result = ec_command(CrosEcCmds::Hello as u32, 0, params_slice)
.unwrap_or_else(|error| panic!("EC error: {error:?}"));
let result = ec_command(CrosEcCmds::Hello as u32, 0, params_slice)?;
let response: EcResponseHello = unsafe { std::ptr::read(result.as_ptr() as *const _) };
response.out_data == EXPECTED_OUTPUT

Ok(response.out_data == EXPECTED_OUTPUT)
}
Loading

0 comments on commit 5a2bb9a

Please sign in to comment.