Skip to content

Commit

Permalink
refactor: use CrosEcCmd enum in ec_command function
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Mar 11, 2024
1 parent 5a2bb9a commit 93b561f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/commands/get_chip_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::CrosEcCmds;
use crate::commands::CrosEcCmd;
use crate::crosec::dev::ec_command;
use crate::crosec::EcCmdResult;
use std::mem::size_of;
Expand All @@ -22,7 +22,7 @@ pub fn ec_cmd_get_chip_info() -> EcCmdResult<(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)?;
let result = ec_command(CrosEcCmd::GetChipInfo, 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(""));
Expand Down
4 changes: 2 additions & 2 deletions src/commands/hello.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::CrosEcCmds;
use crate::commands::CrosEcCmd;
use crate::crosec::dev::ec_command;
use crate::crosec::EcCmdResult;
use std::mem::size_of;
Expand All @@ -24,7 +24,7 @@ pub fn ec_cmd_hello() -> EcCmdResult<bool> {
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)?;
let result = ec_command(CrosEcCmd::Hello, 0, params_slice)?;
let response: EcResponseHello = unsafe { std::ptr::read(result.as_ptr() as *const _) };

Ok(response.out_data == EXPECTED_OUTPUT)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub enum CrosEcCmds {
#[repr(u32)]
pub enum CrosEcCmd {
Hello = 0x0001,
Version = 0x0002,
GetBuildInfo = 0x0004,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::CrosEcCmds;
use crate::commands::CrosEcCmd;
use crate::crosec::dev::ec_command;
use crate::crosec::dev::BUF_SIZE;
use crate::crosec::EcCmdResult;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn ec_cmd_version() -> EcCmdResult<(String, String, String, String, String)>
let params_slice =
unsafe { slice::from_raw_parts(params_ptr, size_of::<EcResponseVersionV1>()) };

let result = ec_command(CrosEcCmds::Version as u32, 0, params_slice)?;
let result = ec_command(CrosEcCmd::Version, 0, params_slice)?;
let response: EcResponseVersionV1 = unsafe { std::ptr::read(result.as_ptr() as *const _) };

let ro_ver = String::from_utf8(response.version_string_ro.to_vec()).unwrap_or(String::from(""));
Expand All @@ -59,7 +59,7 @@ pub fn ec_cmd_version() -> EcCmdResult<(String, String, String, String, String)>
let build_string_ptr = &build_string as *const _ as *const u8;
let build_string_slice = unsafe { slice::from_raw_parts(build_string_ptr, BUF_SIZE) };

let result = ec_command(CrosEcCmds::GetBuildInfo as u32, 0, build_string_slice)?;
let result = ec_command(CrosEcCmd::GetBuildInfo, 0, build_string_slice)?;
let response: [u8; BUF_SIZE] = unsafe { std::ptr::read(result.as_ptr() as *const _) };

let build_info = String::from_utf8(response.to_vec()).unwrap_or(String::from(""));
Expand Down
5 changes: 3 additions & 2 deletions src/crosec/dev.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::commands::CrosEcCmd;
use crate::crosec::EcCmdResult;
use crate::crosec::EcError;
use nix::ioctl_readwrite;
Expand All @@ -21,7 +22,7 @@ struct _CrosEcCommandV2 {
#[repr(C)]
struct CrosEcCommandV2 {
version: u32,
command: u32,
command: CrosEcCmd,
outsize: u32,
insize: u32,
result: u32,
Expand All @@ -46,7 +47,7 @@ fn init() {
};
}

pub fn ec_command(command: u32, command_version: u8, data: &[u8]) -> EcCmdResult<Vec<u8>> {
pub fn ec_command(command: CrosEcCmd, command_version: u8, data: &[u8]) -> EcCmdResult<Vec<u8>> {
init();

let size = std::cmp::min(IN_SIZE, data.len());
Expand Down

0 comments on commit 93b561f

Please sign in to comment.