mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
refactor TRNG server to new API
This commit is contained in:
parent
2407f17267
commit
a3adf797b0
6 changed files with 58 additions and 57 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -1378,6 +1378,8 @@ dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
|
"num-derive",
|
||||||
|
"num-traits",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ log-server = { path = "../log-server" }
|
||||||
ticktimer-server = { path = "../ticktimer-server" }
|
ticktimer-server = { path = "../ticktimer-server" }
|
||||||
xous-names = { path = "../xous-names" }
|
xous-names = { path = "../xous-names" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
num-derive = {version = "0.3", default-features = false}
|
||||||
|
num-traits = {version = "0.2", default-features = false}
|
||||||
|
|
||||||
|
|
||||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,10 @@
|
||||||
use xous::{Message, ScalarMessage};
|
use xous::{Message, ScalarMessage};
|
||||||
|
|
||||||
|
pub(crate) const SERVER_NAME_TRNG: &str = "_TRNG manager_";
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||||
pub enum Opcode {
|
pub(crate) enum Opcode {
|
||||||
/// Get one or two 32-bit words of TRNG data
|
/// Get one or two 32-bit words of TRNG data
|
||||||
GetTrng(usize),
|
GetTrng,
|
||||||
}
|
|
||||||
|
|
||||||
impl core::convert::TryFrom<& Message> for Opcode {
|
|
||||||
type Error = &'static str;
|
|
||||||
fn try_from(message: & Message) -> Result<Self, Self::Error> {
|
|
||||||
match message {
|
|
||||||
Message::BlockingScalar(m) => match m.id {
|
|
||||||
0 => Ok(Opcode::GetTrng(m.arg1)),
|
|
||||||
_ => Err("TRNG api: unknown BlockingScalar ID"),
|
|
||||||
},
|
|
||||||
_ => Err("TRNG api: unhandled message type"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<Message> for Opcode {
|
|
||||||
fn into(self) -> Message {
|
|
||||||
match self {
|
|
||||||
Opcode::GetTrng(count) => Message::BlockingScalar(ScalarMessage {
|
|
||||||
id: 0,
|
|
||||||
arg1: count.into(),
|
|
||||||
arg2: 0,
|
|
||||||
arg3: 0,
|
|
||||||
arg4: 0,
|
|
||||||
}),
|
|
||||||
// _ => panic!("TRNG api: Opcode type not handled by Into(), refer to helper method"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,42 @@
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
use xous::{CID, send_message};
|
use xous::{CID, send_message};
|
||||||
|
use num_traits::ToPrimitive;
|
||||||
|
|
||||||
pub fn get_u32(cid: CID) -> Result<u32, xous::Error> {
|
pub struct Trng {
|
||||||
let response = send_message(cid, api::Opcode::GetTrng(1).into())?;
|
conn: CID,
|
||||||
|
}
|
||||||
|
impl Trng {
|
||||||
|
pub fn new(xns: xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||||
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_TRNG).expect("Can't connect to TRNG server");
|
||||||
|
Ok(Trng {
|
||||||
|
conn
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn get_u32(&self) -> Result<u32, xous::Error> {
|
||||||
|
let response = send_message(self.conn,
|
||||||
|
xous::Message::BlockingScalar(xous::ScalarMessage {
|
||||||
|
id: api::Opcode::GetTrng.to_usize().unwrap(),
|
||||||
|
arg1: 1 /* count */, arg2: 0, arg3: 0, arg4: 0
|
||||||
|
})
|
||||||
|
).expect("TRNG|LIB: can't get_u32");
|
||||||
if let xous::Result::Scalar2(trng, _) = response {
|
if let xous::Result::Scalar2(trng, _) = response {
|
||||||
Ok(trng as u32)
|
Ok(trng as u32)
|
||||||
} else {
|
} else {
|
||||||
panic!("unexpected return value: {:#?}", response);
|
panic!("unexpected return value: {:#?}", response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn get_u64(&self) -> Result<u64, xous::Error> {
|
||||||
pub fn get_u64(cid: CID) -> Result<u64, xous::Error> {
|
let response = send_message(self.conn,
|
||||||
let response = send_message(cid, api::Opcode::GetTrng(2).into())?;
|
xous::Message::BlockingScalar(xous::ScalarMessage {
|
||||||
|
id: api::Opcode::GetTrng.to_usize().unwrap(),
|
||||||
|
arg1: 2 /* count */, arg2: 0, arg3: 0, arg4: 0
|
||||||
|
})
|
||||||
|
).expect("TRNG|LIB: can't get_u32");
|
||||||
if let xous::Result::Scalar2(lo, hi) = response {
|
if let xous::Result::Scalar2(lo, hi) = response {
|
||||||
Ok( lo as u64 | ((hi as u64) << 32) )
|
Ok( lo as u64 | ((hi as u64) << 32) )
|
||||||
} else {
|
} else {
|
||||||
panic!("unexpected return value: {:#?}", response);
|
panic!("unexpected return value: {:#?}", response);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
|
||||||
use core::convert::TryFrom;
|
use num_traits::FromPrimitive;
|
||||||
|
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
|
|
||||||
|
|
@ -154,7 +154,7 @@ fn xmain() -> ! {
|
||||||
info!("TRNG: my PID is {}", xous::process::id());
|
info!("TRNG: my PID is {}", xous::process::id());
|
||||||
|
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
let trng_sid = xns.register_name(xous::names::SERVER_NAME_TRNG).expect("TRNG: can't register server");
|
let trng_sid = xns.register_name(api::SERVER_NAME_TRNG).expect("TRNG: can't register server");
|
||||||
info!("TRNG: registered with NS -- {:?}", trng_sid);
|
info!("TRNG: registered with NS -- {:?}", trng_sid);
|
||||||
|
|
||||||
#[cfg(target_os = "none")]
|
#[cfg(target_os = "none")]
|
||||||
|
|
@ -166,17 +166,20 @@ fn xmain() -> ! {
|
||||||
info!("TRNG: ready to accept requests");
|
info!("TRNG: ready to accept requests");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let envelope = xous::receive_message(trng_sid).unwrap();
|
let msg = xous::receive_message(trng_sid).unwrap();
|
||||||
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
match opcode {
|
Some(api::Opcode::GetTrng) => {
|
||||||
Opcode::GetTrng(count) => {
|
if let xous::Message::BlockingScalar(xous::ScalarMessage {
|
||||||
|
id: _, arg1: count, arg2: _, arg3: _, arg4: _
|
||||||
|
}) = msg.body {
|
||||||
let val: [u32; 2] = trng.get_trng(count);
|
let val: [u32; 2] = trng.get_trng(count);
|
||||||
xous::return_scalar2(envelope.sender, val[0] as _, val[1] as _)
|
xous::return_scalar2(msg.sender, val[0] as _, val[1] as _)
|
||||||
.expect("TRNG: couldn't return GetTrng request");
|
.expect("TRNG: couldn't return GetTrng request");
|
||||||
},
|
} else { log::error!("TRNG: GetTrng malformed message"); }
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
log::error!("TRNG: couldn't convert opcode")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
error!("KBD: couldn't convert opcode");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ pub const SERVER_NAME_LLIO: &str = "_Low Level I/O manager_";
|
||||||
pub const SERVER_NAME_SHELL: &str = "_Shell_";
|
pub const SERVER_NAME_SHELL: &str = "_Shell_";
|
||||||
pub const SERVER_NAME_GFX: &str = "_Graphics_";
|
pub const SERVER_NAME_GFX: &str = "_Graphics_";
|
||||||
pub const SERVER_NAME_KBD: &str = "_Matrix keyboard driver_";
|
pub const SERVER_NAME_KBD: &str = "_Matrix keyboard driver_";
|
||||||
pub const SERVER_NAME_TRNG: &str = "_TRNG manager_";
|
|
||||||
pub const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_";
|
pub const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_";
|
||||||
pub const SERVER_NAME_STATUS: &str = "_Status bar manager_";
|
pub const SERVER_NAME_STATUS: &str = "_Status bar manager_";
|
||||||
pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_";
|
pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue