xous-core/services/xous-names/src/main.rs
2021-03-11 04:12:14 +08:00

118 lines
5.4 KiB
Rust

#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
extern crate hash32_derive;
mod api;
use xous::buffer;
// use heapless::String;
use heapless::consts::*;
use heapless::FnvIndexMap;
use core::convert::TryInto;
use log::{error, info};
const FAIL_TIMEOUT_MS: u64 = 100;
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = false;
log_server::init_wait().unwrap();
info!("NS: my PID is {}", xous::process::id());
let name_server = xous::create_server_with_address(b"xous-name-server")
.expect("Couldn't create xousnames-server");
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap();
// this limits the number of available servers to be requested to 128...!
let mut name_table = FnvIndexMap::<_, _, U128>::new();
info!("NS: started");
use core::pin::Pin;
use rkyv::{archived_value_mut, Unarchive};
loop {
let envelope = xous::receive_message(name_server).unwrap();
if debug1{info!("NS: received message");}
if let xous::Message::MutableBorrow(m) = &envelope.body {
let mut buf = unsafe { buffer::XousBuffer::from_memory_message(m) };
let value = unsafe {
archived_value_mut::<api::Request>(Pin::new(buf.as_mut()), m.id.try_into().unwrap())
};
let new_value = match &*value {
rkyv::Archived::<api::Request>::Register(registration_name) => {
use rkyv::Unarchive;
let name = registration_name.unarchive();
if debug1{info!("NS: registration request for '{}'", name);}
if !name_table.contains_key(&name) {
let new_sid =
xous::create_server_id().expect("NS: create server failed, maybe OOM?");
name_table
.insert(name, new_sid)
.expect("NS: register name failure, maybe out of HashMap capacity?");
if debug1{info!("NS: request successful, SID is {:?}", new_sid);}
rkyv::Archived::<api::Request>::SID(new_sid.into())
} else {
// compute the next interval, rounded to a multiple of FAIL_TIMEOUT_MS to reduce timing side channels
let target_time: u64 = ((ticktimer_server::elapsed_ms(ticktimer_conn)
.unwrap()
/ FAIL_TIMEOUT_MS)
+ 1)
* FAIL_TIMEOUT_MS;
info!("NS: request failed, waiting for deterministic timeout");
while ticktimer_server::elapsed_ms(ticktimer_conn).unwrap() < target_time {
xous::yield_slice();
}
info!("NS: deterministic timeout done");
rkyv::Archived::<api::Request>::Failure
}
}
rkyv::Archived::<api::Request>::Lookup(lookup_name) => {
if debug1{info!("NS: Lookup request for '{}'", lookup_name);}
let name = lookup_name.unarchive();
if let Some(server_sid) = name_table.get(&name) {
let sender_pid = envelope
.sender
.pid()
.expect("NS: can't extract sender PID on Lookup");
match xous::connect_for_process(sender_pid, *server_sid)
.expect("NS: can't broker connection")
{
xous::Result::ConnectionID(connection_id) => {
if debug1{info!("NS: lookup success, returning connection {}", connection_id);}
rkyv::Archived::<api::Request>::CID(connection_id)
}
_ => {
info!(
"NS: Can't find request '{}' in table, dumping table:",
lookup_name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
rkyv::Archived::<api::Request>::Failure
}
}
} else {
info!(
"NS: Can't find request '{}' in table, dumping table:",
lookup_name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
// no authenticate remedy currently supported, but we'd put that code somewhere around here eventually.
rkyv::Archived::<api::Request>::Failure
}
}
_ => panic!("Invalid response from the server -- corruption occurred"),
};
unsafe { *value.get_unchecked_mut() = new_value };
} else {
error!("NS: couldn't convert opcode");
}
}
}