this code works in hosted mode but fails on hardware

This commit is contained in:
bunnie 2021-01-05 15:48:51 +08:00
parent ec675eefb4
commit 0d869870ae
5 changed files with 88 additions and 17 deletions

View file

@ -198,6 +198,10 @@ mod implementation {
0xDEAD as u16
}
pub fn wait_txrx(&mut self, tx: u16, timeout: Option<u32>) -> u16 {
0xDEAD as u16
}
pub fn get_battstats(&mut self) -> BattStats {
BattStats {
voltage: 3700,

View file

@ -356,8 +356,7 @@ fn shell_main() -> ! {
let registration = Registration::new();
let mut sendable_registration = Sendable::new(registration)
.expect("can't create sendable registration structure");
let test_name = b"A test Name!";
sendable_registration.name.name[..test_name.len()].clone_from_slice(test_name);
write!(sendable_registration.name, "A test Name!");
info!("namserver registration input: {:?}", sendable_registration.success);
sendable_registration.lend_mut(ns_conn, sendable_registration.mid()).expect("nameserver registration failure!");
info!("nameserver registration result: {:?}, {:?}", sendable_registration.success, sendable_registration.sid);
@ -368,9 +367,7 @@ fn shell_main() -> ! {
let mut sendable_lookup = Sendable::new(lookup)
.expect("can't create sendable lookup structure");
info!("making a test name");
let another_name = b"A test Name!";
info!("cloning into slice");
sendable_lookup.name.name[..another_name.len()].clone_from_slice(another_name);
write!(sendable_lookup.name, "A test Name!");
info!("nameserver lookup input: {:?}", sendable_lookup.success);
sendable_lookup.lend_mut(ns_conn, sendable_lookup.mid()).expect("nameserver lookup failure!");
info!("nameserver lookup result: {:?}, {:?}", sendable_lookup.success, sendable_lookup.cid);

View file

@ -11,19 +11,90 @@ pub const ID_AUTHENTICATE: usize = 2;
pub const AUTHENTICATE_TIMEOUT: usize = 10_000; // time in ms that a process has to respond to an authentication request
//////////////////////// handle throwing strings across IPC boundary with hash comparison
#[derive(Hash32, Debug, Copy, Clone)]
pub struct XousServerName{pub name: [u8; 32]}
#[repr(C)]
pub struct XousServerName {
pub name: [u8; 32],
pub length: usize,
}
impl XousServerName {
pub fn new() -> Self {
Default::default()
}
pub fn to_str(&self) -> &str {
core::str::from_utf8(unsafe {
core::slice::from_raw_parts(self.name.as_ptr(), self.length)
})
.unwrap()
}
}
impl Default for XousServerName {
fn default() -> Self { XousServerName{name: [0; 32]} }
fn default() -> Self {
XousServerName {
name: [0; 32],
length: 0,
}
}
}
impl PartialEq for XousServerName {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
self.name[..self.length] == other.name[..other.length] && self.length == other.length
}
}
impl Eq for XousServerName {}
// Allow using the `write!()` macro to write into a `&XousServerName`
impl core::fmt::Write for XousServerName {
fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> {
self.length = 0;
let b = s.bytes();
// Ensure the length is acceptable
if b.len() > self.name.len() {
Err(core::fmt::Error)?;
}
self.length = b.len();
// Copy the string into this variable
for (dest, src) in self.name.iter_mut().zip(s.bytes()) {
*dest = src;
}
// Attempt to convert the string to UTF-8 to validate it's correct UTF-8
core::str::from_utf8(unsafe {
core::slice::from_raw_parts(self.name.as_ptr(), self.length)
})
.map_err(|_| core::fmt::Error)?;
Ok(())
}
}
// Allow a `&XousServerName` to be printed out
impl core::fmt::Display for XousServerName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_str())
}
}
// Allow a `&XousServerName` to be used anywhere that expects a `&str`
impl AsRef<str> for XousServerName {
fn as_ref(&self) -> &str {
self.to_str()
}
}
//////////////////////// end server name string implementation functions
#[derive(Debug)]
#[repr(C)]
pub struct Registration {
mid: usize,
pub sid: SID, // query: do we even want to return this to the registering process??
@ -43,7 +114,7 @@ impl Registration {
pub fn new() -> Self {
Registration {
mid: ID_REGISTER_NAME,
sid: xous::SID::from_u32(0,0,0,0),
sid: SID::from_u32(0,0,0,0),
name: XousServerName::default(),
success: false,
}
@ -51,6 +122,7 @@ impl Registration {
}
#[derive(Debug)]
#[repr(C)]
pub struct Lookup {
mid: usize,
pub cid: CID,
@ -78,6 +150,7 @@ impl Lookup {
}
#[derive(Debug)]
#[repr(C)]
pub struct Authenticate {
mid: usize,
pub cid: CID,

View file

@ -35,9 +35,7 @@ fn xmain() -> ! {
let registration: &mut Registration = unsafe {
&mut *(m.buf.as_mut_ptr() as *mut Registration)
};
let namevec: Vec<u8, U32> = Vec::from_slice(&registration.name.name).unwrap();
let name: String<U32> = String::from_utf8(namevec).unwrap();
info!("NS: registration request for {}", name);
info!("NS: registration request for {}", registration.name);
if !name_table.contains_key(&registration.name) {
let new_sid = xous::create_server().expect("NS: create server failed, maybe OOM?");
name_table.insert(registration.name, new_sid).expect("NS: register name failure, maybe out of HashMap capacity?");
@ -59,10 +57,8 @@ fn xmain() -> ! {
let lookup: &mut Lookup = unsafe {
&mut *(m.buf.as_mut_ptr() as *mut Lookup)
};
let namevec: Vec<u8, U32> = Vec::from_slice(&lookup.name.name).unwrap();
let name: String<U32> = String::from_utf8(namevec).unwrap();
info!("NS: Lookup request for {}", name);
lookup.cid = 1337;
info!("NS: Lookup request for {}", lookup.name);
// lookup.cid = 1337;
lookup.success = true;
} else {
error!("NS: unknown message ID received");

View file

@ -183,7 +183,8 @@ fn run(debug: bool) -> Result<(), DynError> {
"graphics-server",
"ticktimer-server",
"com",
]; // , "xous-names"
"xous-names",
];
// let mut init_paths = vec![];
for pkg in &init {