Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick to earlgrey_1.0.0: [opentitanlib] Refactor RPC module to enable supporting future console interfaces (e.g., SPI) #24840

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sw/host/opentitanlib/src/test_utils/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{UartRecv, UartSend};
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
Expand Down
2 changes: 1 addition & 1 deletion sw/host/opentitanlib/src/test_utils/i2c_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{UartRecv, UartSend};
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
Expand Down
2 changes: 1 addition & 1 deletion sw/host/opentitanlib/src/test_utils/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;

use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{UartRecv, UartSend};
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
Expand Down
2 changes: 1 addition & 1 deletion sw/host/opentitanlib/src/test_utils/pinmux_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::Duration;
use crate::chip::autogen::earlgrey::{PinmuxInsel, PinmuxMioOut, PinmuxOutsel, PinmuxPeripheralIn};
use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{UartRecv, UartSend};
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
Expand Down
51 changes: 33 additions & 18 deletions sw/host/opentitanlib/src/test_utils/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,59 @@ use serde::Serialize;
use std::io::Write;
use std::time::Duration;

use crate::io::uart::{Uart, UartError};
use crate::io::console::{ConsoleDevice, ConsoleError};
use crate::test_utils::status::Status;
use crate::uart::console::{ExitStatus, UartConsole};

// Bring in the auto-generated sources.
include!(env!("ottf"));

pub trait UartSend {
fn send(&self, uart: &dyn Uart) -> Result<()>;

fn send_with_crc(&self, uart: &dyn Uart) -> Result<()>;
pub trait ConsoleSend<T>
where
T: ConsoleDevice + ?Sized,
{
fn send(&self, device: &T) -> Result<()>;
fn send_with_crc(&self, device: &T) -> Result<()>;
}

impl<T: Serialize> UartSend for T {
fn send(&self, uart: &dyn Uart) -> Result<()> {
impl<T, U> ConsoleSend<T> for U
where
T: ConsoleDevice + ?Sized,
U: Serialize,
{
fn send(&self, device: &T) -> Result<()> {
let s = serde_json::to_string(self)?;
log::info!("Sending: {}", s);
uart.write(s.as_bytes())?;
device.console_write(s.as_bytes())?;
Ok(())
}

fn send_with_crc(&self, uart: &dyn Uart) -> Result<()> {
fn send_with_crc(&self, device: &T) -> Result<()> {
let s = serde_json::to_string(self)?;
log::info!("Sending: {}", s);
uart.write(s.as_bytes())?;
device.console_write(s.as_bytes())?;
let actual_crc = OttfCrc {
crc: Crc::<u32>::new(&CRC_32_ISO_HDLC).checksum(s.as_bytes()),
};
actual_crc.send(uart)
actual_crc.send(device)
}
}

pub trait UartRecv {
fn recv(uart: &dyn Uart, timeout: Duration, quiet: bool) -> Result<Self>
pub trait ConsoleRecv<T>
where
T: ConsoleDevice + ?Sized,
{
fn recv(device: &T, timeout: Duration, quiet: bool) -> Result<Self>
where
Self: Sized;
}

impl<T: DeserializeOwned> UartRecv for T {
fn recv(uart: &dyn Uart, timeout: Duration, quiet: bool) -> Result<Self>
impl<T, U> ConsoleRecv<T> for U
where
T: ConsoleDevice + ?Sized,
U: DeserializeOwned,
{
fn recv(device: &T, timeout: Duration, quiet: bool) -> Result<Self>
where
Self: Sized,
{
Expand All @@ -67,7 +80,7 @@ impl<T: DeserializeOwned> UartRecv for T {
} else {
None
};
let result = console.interact(uart, None, out)?;
let result = console.interact(device, None, out)?;
println!();
match result {
ExitStatus::ExitSuccess => {
Expand All @@ -89,7 +102,7 @@ impl<T: DeserializeOwned> UartRecv for T {
let err = serde_json::from_str::<Status>(json_str)?;
Err(err.into())
}
ExitStatus::Timeout => Err(UartError::GenericError("Timed Out".into()).into()),
ExitStatus::Timeout => Err(ConsoleError::GenericError("Timed Out".into()).into()),
_ => Err(anyhow!("Impossible result: {:?}", result)),
}
}
Expand All @@ -99,7 +112,9 @@ fn check_crc(json_str: &str, crc_str: &str) -> Result<()> {
let crc = crc_str.parse::<u32>()?;
let actual_crc = Crc::<u32>::new(&CRC_32_ISO_HDLC).checksum(json_str.as_bytes());
if crc != actual_crc {
return Err(UartError::GenericError("CRC didn't match received json body.".into()).into());
return Err(
ConsoleError::GenericError("CRC didn't match received json body.".into()).into(),
);
}
Ok(())
}
2 changes: 1 addition & 1 deletion sw/host/opentitanlib/src/test_utils/spi_passthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{UartRecv, UartSend};
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
Expand Down
2 changes: 1 addition & 1 deletion sw/host/provisioning/cp_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use opentitanlib::test_utils::lc_transition::trigger_lc_transition;
use opentitanlib::test_utils::load_sram_program::{
ExecutionMode, ExecutionResult, SramProgramParams,
};
use opentitanlib::test_utils::rpc::UartSend;
use opentitanlib::test_utils::rpc::ConsoleSend;
use opentitanlib::uart::console::UartConsole;
use ujson_lib::provisioning_data::ManufCpProvisioningData;

Expand Down
2 changes: 1 addition & 1 deletion sw/host/provisioning/ft_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use opentitanlib::test_utils::lc_transition::trigger_lc_transition;
use opentitanlib::test_utils::load_sram_program::{
ExecutionMode, ExecutionResult, SramProgramParams,
};
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;
use ot_certs::x509::parse_certificate;
use perso_tlv_lib::perso_tlv_get_field;
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/chip/i2c_target/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use opentitanlib::test_utils;
use opentitanlib::test_utils::e2e_command::TestCommand;
use opentitanlib::test_utils::i2c_target::{I2cTargetAddress, I2cTestConfig, I2cTransferStart};
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::test_utils::status::Status;
use opentitanlib::uart::console::UartConsole;
use std::borrow::Borrow;
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/aes_nist_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cryptotest_commands::commands::CryptotestCommand;
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/drbg_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use cryptotest_commands::drbg_commands::{CryptotestDrbgInput, CryptotestDrbgOutp
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/ecdh_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cryptotest_commands::ecdh_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;
use p256::elliptic_curve::scalar::ScalarPrimitive as ScalarPrimitiveP256;
use p256::U256;
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/ecdsa_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use cryptotest_commands::ecdsa_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/hash_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cryptotest_commands::hash_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/hmac_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cryptotest_commands::hmac_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/kmac_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cryptotest_commands::kmac_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/crypto/sphincsplus_kat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use cryptotest_commands::sphincsplus_commands::{
use opentitanlib::app::TransportWrapper;
use opentitanlib::execute_test;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;

#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/manuf/cp_provision_functest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use opentitanlib::test_utils::lc_transition;
use opentitanlib::test_utils::load_sram_program::{
ExecutionMode, ExecutionResult, SramProgramParams,
};
use opentitanlib::test_utils::rpc::UartSend;
use opentitanlib::test_utils::rpc::ConsoleSend;
use opentitanlib::uart::console::UartConsole;
use ujson_lib::provisioning_data::ManufCpProvisioningData;
use util_lib::hash_lc_token;
Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/manuf/personalize_functest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use opentitanlib::io::jtag::JtagTap;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::lc::read_lc_state;
use opentitanlib::test_utils::lc_transition::trigger_lc_transition;
use opentitanlib::test_utils::rpc::UartSend;
use opentitanlib::test_utils::rpc::ConsoleSend;
use opentitanlib::uart::console::UartConsole;
use util_lib::hash_lc_token;

Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/rom/e2e_chip_specific_startup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use opentitanlib::test_utils::e2e_command::TestCommand;
use opentitanlib::test_utils::epmp::constants::*;
use opentitanlib::test_utils::epmp::{Epmp, EpmpAddressRange, EpmpEntry, EpmpRegionKind};
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::uart::console::UartConsole;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion sw/host/tests/rom/sw_strap_value/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use opentitanlib::execute_test;
use opentitanlib::io::gpio::{PinMode, PullMode};
use opentitanlib::test_utils::e2e_command::TestCommand;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::test_utils::rpc::{UartRecv, UartSend};
use opentitanlib::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use opentitanlib::test_utils::status::Status;
use opentitanlib::uart::console::UartConsole;
use std::time::Duration;
Expand Down
Loading