2021-01-13 07:04:14 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
|
use xous::{CID, send_message};
|
2021-04-01 09:20:36 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2021-01-13 07:04:14 +00:00
|
|
|
|
2021-04-01 09:20:36 +00:00
|
|
|
pub struct Trng {
|
|
|
|
|
conn: CID,
|
2021-01-13 07:04:14 +00:00
|
|
|
}
|
2021-04-01 09:20:36 +00:00
|
|
|
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 {
|
|
|
|
|
Ok(trng as u32)
|
|
|
|
|
} else {
|
|
|
|
|
panic!("unexpected return value: {:#?}", response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn get_u64(&self) -> Result<u64, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
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");
|
2021-01-13 07:04:14 +00:00
|
|
|
if let xous::Result::Scalar2(lo, hi) = response {
|
2021-04-01 09:20:36 +00:00
|
|
|
Ok( lo as u64 | ((hi as u64) << 32) )
|
|
|
|
|
} else {
|
|
|
|
|
panic!("unexpected return value: {:#?}", response);
|
|
|
|
|
}
|
2021-01-13 07:04:14 +00:00
|
|
|
}
|
|
|
|
|
}
|