2021-01-04 15:34:36 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
2021-01-04 20:40:34 +00:00
|
|
|
extern crate hash32_derive;
|
|
|
|
|
|
2021-01-04 15:34:36 +00:00
|
|
|
mod api;
|
2021-03-17 19:54:48 +00:00
|
|
|
use api::*;
|
2021-01-04 15:34:36 +00:00
|
|
|
|
2021-03-28 21:20:21 +00:00
|
|
|
use num_traits::FromPrimitive;
|
2021-03-28 20:35:11 +00:00
|
|
|
use xous_ipc::{String, Buffer};
|
|
|
|
|
|
2021-01-04 15:34:36 +00:00
|
|
|
use log::{error, info};
|
|
|
|
|
|
2021-04-26 16:39:18 +00:00
|
|
|
#[cfg(target_os = "none")]
|
|
|
|
|
mod implementation {
|
|
|
|
|
use utralib::generated::*;
|
2021-01-07 08:58:13 +00:00
|
|
|
|
2021-04-26 16:39:18 +00:00
|
|
|
pub struct D11cTimeout {
|
|
|
|
|
d11t_csr: utralib::CSR<u32>,
|
|
|
|
|
}
|
|
|
|
|
impl D11cTimeout {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
let csr = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::d11ctime::HW_D11CTIME_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map D11cTimeout CSR range");
|
|
|
|
|
|
|
|
|
|
D11cTimeout {
|
|
|
|
|
d11t_csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn deterministic_busy_wait(&self) {
|
|
|
|
|
let phase = self.d11t_csr.rf(utra::d11ctime::HEARTBEAT_BEAT);
|
|
|
|
|
while phase == self.d11t_csr.rf(utra::d11ctime::HEARTBEAT_BEAT) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-15 17:22:25 +00:00
|
|
|
pub fn hosted_delay(&self) {
|
|
|
|
|
// this is a delay used in hosted mode to prevent threads from thrashing. We don't need this on
|
|
|
|
|
// raw hardware because we aren't multi-core, and thus a yield_slice() will always schedule
|
|
|
|
|
// a different process to run, whereas a yield_slice() on hosted/emulation can immediately return
|
|
|
|
|
// and cause troubles for other processes.
|
|
|
|
|
}
|
2021-04-26 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "none"))]
|
|
|
|
|
mod implementation {
|
|
|
|
|
pub struct D11cTimeout {}
|
|
|
|
|
impl D11cTimeout {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
D11cTimeout {}
|
|
|
|
|
}
|
|
|
|
|
pub fn deterministic_busy_wait(&self) {
|
|
|
|
|
// don't do anything for hosted mode
|
|
|
|
|
}
|
2021-05-15 17:22:25 +00:00
|
|
|
pub fn hosted_delay(&self) {
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
|
}
|
2021-04-26 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-28 20:35:11 +00:00
|
|
|
|
2021-05-08 15:34:08 +00:00
|
|
|
/*
|
|
|
|
|
SlowMap is a stand-in implementation for a HashMap from the Heapless crate that has proven to be unsafe,
|
|
|
|
|
and leaking data between entries. It's called "SlowMap" because it's slow: accesses are O(N). That
|
|
|
|
|
being said, it's 100% safe, and xous-names accesses are once-in-a-blue-moon type of things, so
|
|
|
|
|
I'll take safety over speed in this case.
|
|
|
|
|
|
|
|
|
|
Eventually, we shall endeavor to remove Heapless entirely, once we have a `libstd` in place
|
|
|
|
|
and we can use heap-allocated Rust primitives...
|
|
|
|
|
*/
|
|
|
|
|
struct SlowMap {
|
|
|
|
|
pub map: [Option<(XousServerName, xous::SID)>; 128],
|
|
|
|
|
}
|
|
|
|
|
impl SlowMap {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
SlowMap {
|
|
|
|
|
map: [None; 128],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn insert(&mut self, name: XousServerName, sid: xous::SID) -> Result<(), xous::Error> {
|
|
|
|
|
let mut ok = false;
|
|
|
|
|
for entry in self.map.iter_mut() {
|
|
|
|
|
if entry.is_none() {
|
|
|
|
|
*entry = Some((name, sid));
|
|
|
|
|
ok = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
Err(xous::Error::OutOfMemory)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn contains_key(&self, name: &XousServerName) -> bool {
|
|
|
|
|
for maybe_entry in self.map.iter() {
|
|
|
|
|
if let Some(entry) = maybe_entry {
|
|
|
|
|
let (key, _value) = entry;
|
|
|
|
|
if name == key {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
pub fn get(&self, name: &XousServerName) -> Option<&xous::SID> {
|
|
|
|
|
for maybe_entry in self.map.iter() {
|
|
|
|
|
if let Some(entry) = maybe_entry {
|
|
|
|
|
let (key, value) = entry;
|
|
|
|
|
if name == key {
|
|
|
|
|
return Some(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 15:34:36 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
2021-04-26 16:39:18 +00:00
|
|
|
use implementation::*;
|
2021-05-08 15:34:08 +00:00
|
|
|
let debug1 = false;
|
2021-01-04 15:34:36 +00:00
|
|
|
log_server::init_wait().unwrap();
|
2021-05-08 15:34:08 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
2021-01-04 15:34:36 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
let name_server = xous::create_server_with_address(b"xous-name-server")
|
|
|
|
|
.expect("Couldn't create xousnames-server");
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-04-26 16:39:18 +00:00
|
|
|
let d11ctimeout = D11cTimeout::new();
|
2021-01-04 20:40:34 +00:00
|
|
|
|
|
|
|
|
// this limits the number of available servers to be requested to 128...!
|
2021-05-08 15:34:08 +00:00
|
|
|
//let mut name_table = FnvIndexMap::<XousServerName, xous::SID, 128>::new();
|
|
|
|
|
let mut name_table = SlowMap::new();
|
2021-01-04 15:34:36 +00:00
|
|
|
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("started");
|
2021-01-04 15:34:36 +00:00
|
|
|
loop {
|
2021-03-28 20:35:11 +00:00
|
|
|
let mut msg = xous::receive_message(name_server).unwrap();
|
2021-04-01 19:10:13 +00:00
|
|
|
if debug1{info!("received message");}
|
2021-03-28 20:35:11 +00:00
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(api::Opcode::Register) => {
|
2021-03-28 21:20:21 +00:00
|
|
|
let mem = msg.body.memory_message_mut().unwrap();
|
2021-03-28 20:35:11 +00:00
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) };
|
2021-05-08 10:44:23 +00:00
|
|
|
let xous_string = buffer.to_original::<String::<64>, _>().unwrap();
|
|
|
|
|
let name = XousServerName::from_str(xous_string.as_str().expect("couldn't convert server name to string"));
|
2021-03-28 20:35:11 +00:00
|
|
|
|
2021-03-28 21:20:21 +00:00
|
|
|
let response: api::Return;
|
2021-03-28 20:35:11 +00:00
|
|
|
|
2021-04-01 19:10:13 +00:00
|
|
|
if debug1{info!("registration request for '{}'", name);}
|
2021-03-28 20:35:11 +00:00
|
|
|
if !name_table.contains_key(&name) {
|
|
|
|
|
let new_sid =
|
2021-04-01 19:10:13 +00:00
|
|
|
xous::create_server_id().expect("create server failed, maybe OOM?");
|
2021-03-28 20:35:11 +00:00
|
|
|
name_table
|
|
|
|
|
.insert(name, new_sid)
|
2021-04-01 19:10:13 +00:00
|
|
|
.expect("register name failure, maybe out of HashMap capacity?");
|
|
|
|
|
if debug1{info!("request successful, SID is {:?}", new_sid);}
|
2021-03-28 20:35:11 +00:00
|
|
|
|
|
|
|
|
response = api::Return::SID(new_sid.into());
|
|
|
|
|
} else {
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("request failed, waiting for deterministic timeout");
|
2021-04-26 16:39:18 +00:00
|
|
|
d11ctimeout.deterministic_busy_wait();
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("deterministic timeout done");
|
2021-03-28 20:35:11 +00:00
|
|
|
response = api::Return::Failure
|
2021-01-04 20:40:34 +00:00
|
|
|
}
|
2021-04-26 16:52:52 +00:00
|
|
|
buffer.replace(response).expect("Register can't serialize return value");
|
2021-03-28 20:35:11 +00:00
|
|
|
}
|
|
|
|
|
Some(api::Opcode::Lookup) => {
|
2021-03-28 21:20:21 +00:00
|
|
|
let mem = msg.body.memory_message_mut().unwrap();
|
2021-03-28 20:35:11 +00:00
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) };
|
2021-05-08 10:44:23 +00:00
|
|
|
let name_string = buffer.to_original::<String::<64>, _>().unwrap();
|
|
|
|
|
let name = XousServerName::from_str(name_string.as_str().expect("couldn't convert server name to string"));
|
2021-04-01 19:10:13 +00:00
|
|
|
if debug1{info!("Lookup request for '{}'", name);}
|
2021-03-28 21:20:21 +00:00
|
|
|
let response: api::Return;
|
2021-03-28 20:35:11 +00:00
|
|
|
if let Some(server_sid) = name_table.get(&name) {
|
|
|
|
|
let sender_pid = msg
|
|
|
|
|
.sender
|
|
|
|
|
.pid()
|
2021-04-01 19:10:13 +00:00
|
|
|
.expect("can't extract sender PID on Lookup");
|
2021-03-28 20:35:11 +00:00
|
|
|
match xous::connect_for_process(sender_pid, *server_sid)
|
2021-04-01 19:10:13 +00:00
|
|
|
.expect("can't broker connection")
|
2021-03-28 20:35:11 +00:00
|
|
|
{
|
|
|
|
|
xous::Result::ConnectionID(connection_id) => {
|
2021-04-01 19:10:13 +00:00
|
|
|
if debug1{info!("lookup success, returning connection {}", connection_id);}
|
2021-03-28 20:35:11 +00:00
|
|
|
response = api::Return::CID(connection_id)
|
2021-01-08 18:12:01 +00:00
|
|
|
}
|
2021-03-28 20:35:11 +00:00
|
|
|
_ => {
|
2021-05-15 17:22:25 +00:00
|
|
|
log::debug!(
|
2021-04-01 19:10:13 +00:00
|
|
|
"Can't find request '{}' in table, dumping table:",
|
2021-03-28 20:35:11 +00:00
|
|
|
name
|
|
|
|
|
);
|
2021-05-08 15:34:08 +00:00
|
|
|
for kv_tuple in name_table.map.iter() {
|
|
|
|
|
if let Some((key, val)) = kv_tuple {
|
2021-05-15 17:22:25 +00:00
|
|
|
log::debug!("name: '{}', sid: '{:?}'", key, val);
|
2021-05-08 15:34:08 +00:00
|
|
|
}
|
2021-03-28 20:35:11 +00:00
|
|
|
}
|
2021-05-15 17:22:25 +00:00
|
|
|
d11ctimeout.hosted_delay();
|
2021-03-28 20:35:11 +00:00
|
|
|
response = api::Return::Failure
|
2021-02-10 07:02:48 +00:00
|
|
|
}
|
2021-01-07 10:51:14 +00:00
|
|
|
}
|
2021-03-28 20:35:11 +00:00
|
|
|
} else {
|
2021-05-15 17:22:25 +00:00
|
|
|
log::debug!(
|
2021-04-01 19:10:13 +00:00
|
|
|
"Can't find request '{}' in table, dumping table:",
|
2021-03-28 20:35:11 +00:00
|
|
|
name
|
|
|
|
|
);
|
2021-05-08 15:34:08 +00:00
|
|
|
for kv_tuple in name_table.map.iter() {
|
|
|
|
|
if let Some((key, val)) = kv_tuple {
|
2021-05-15 17:22:25 +00:00
|
|
|
log::debug!("name: '{}', sid: '{:?}'", key, val);
|
2021-05-08 15:34:08 +00:00
|
|
|
}
|
2021-03-28 20:35:11 +00:00
|
|
|
}
|
|
|
|
|
// no authenticate remedy currently supported, but we'd put that code somewhere around here eventually.
|
2021-03-28 20:44:03 +00:00
|
|
|
let (c1, c2, c3, c4) = xous::create_server_id().unwrap().to_u32();
|
2021-03-28 20:35:11 +00:00
|
|
|
let auth_request = AuthenticateRequest {
|
2021-05-08 10:44:23 +00:00
|
|
|
name: String::<64>::from_str(name_string.as_str().expect("couldn't convert server name to string")),
|
2021-03-28 20:35:11 +00:00
|
|
|
pubkey_id: [0; 20], // placeholder
|
2021-03-28 20:44:03 +00:00
|
|
|
challenge: [c1, c2, c3, c4],
|
2021-03-28 20:35:11 +00:00
|
|
|
};
|
2021-05-15 17:22:25 +00:00
|
|
|
d11ctimeout.hosted_delay();
|
2021-03-28 20:35:11 +00:00
|
|
|
response = api::Return::AuthenticateRequest(auth_request) // this code just exists to exercise the return path
|
2021-01-07 10:51:14 +00:00
|
|
|
}
|
2021-04-26 16:52:52 +00:00
|
|
|
buffer.replace(response).expect("Lookup can't serialize return value");
|
2021-03-28 20:35:11 +00:00
|
|
|
}
|
|
|
|
|
Some(api::Opcode::AuthenticatedLookup) => {
|
2021-03-28 21:20:21 +00:00
|
|
|
let mem = msg.body.memory_message_mut().unwrap();
|
2021-03-28 20:35:11 +00:00
|
|
|
let buffer = unsafe { Buffer::from_memory_message_mut(mem) };
|
2021-04-01 08:43:22 +00:00
|
|
|
let auth_lookup: AuthenticatedLookup = buffer.to_original().unwrap();
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("AuthenticatedLookup request {:?}", auth_lookup);
|
|
|
|
|
error!("AuthenticatedLookup not yet implemented");
|
|
|
|
|
unimplemented!("AuthenticatedLookup not yet implemented");
|
2021-03-28 20:35:11 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
None => {error!("couldn't decode message: {:?}", msg); break}
|
2021-01-04 15:34:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
xous::destroy_server(name_server).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
2021-05-20 03:01:00 +00:00
|
|
|
xous::terminate_process(0);
|
2021-01-04 15:34:36 +00:00
|
|
|
}
|