|
| 1 | +use crate::os::xous::ffi::Connection; |
| 2 | +use core::sync::atomic::{AtomicU32, Ordering}; |
| 3 | + |
| 4 | +mod log; |
| 5 | +pub(crate) use log::*; |
| 6 | + |
| 7 | +mod systime; |
| 8 | +pub(crate) use systime::*; |
| 9 | + |
| 10 | +mod ticktimer; |
| 11 | +pub(crate) use ticktimer::*; |
| 12 | + |
| 13 | +mod ns { |
| 14 | + const NAME_MAX_LENGTH: usize = 64; |
| 15 | + use crate::os::xous::ffi::{lend_mut, Connection}; |
| 16 | + // By making this repr(C), the layout of this struct becomes well-defined |
| 17 | + // and no longer shifts around. |
| 18 | + // By marking it as `align(4096)` we define that it will be page-aligned, |
| 19 | + // meaning it can be sent between processes. We make sure to pad out the |
| 20 | + // entire struct so that memory isn't leaked to the name server. |
| 21 | + #[repr(C, align(4096))] |
| 22 | + struct ConnectRequest { |
| 23 | + data: [u8; 4096], |
| 24 | + } |
| 25 | + |
| 26 | + impl ConnectRequest { |
| 27 | + pub fn new(name: &str) -> Self { |
| 28 | + let mut cr = ConnectRequest { data: [0u8; 4096] }; |
| 29 | + let name_bytes = name.as_bytes(); |
| 30 | + |
| 31 | + // Copy the string into our backing store. |
| 32 | + for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.data[0..NAME_MAX_LENGTH]) { |
| 33 | + *dest_byte = src_byte; |
| 34 | + } |
| 35 | + |
| 36 | + // Set the string length to the length of the passed-in String, |
| 37 | + // or the maximum possible length. Which ever is smaller. |
| 38 | + for (&src_byte, dest_byte) in (name.len().min(NAME_MAX_LENGTH) as u32) |
| 39 | + .to_le_bytes() |
| 40 | + .iter() |
| 41 | + .zip(&mut cr.data[NAME_MAX_LENGTH..]) |
| 42 | + { |
| 43 | + *dest_byte = src_byte; |
| 44 | + } |
| 45 | + cr |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn connect_with_name_impl(name: &str, blocking: bool) -> Option<Connection> { |
| 50 | + let mut request = ConnectRequest::new(name); |
| 51 | + let opcode = if blocking { |
| 52 | + 6 /* BlockingConnect */ |
| 53 | + } else { |
| 54 | + 7 /* TryConnect */ |
| 55 | + }; |
| 56 | + let cid = if blocking { super::name_server() } else { super::try_name_server()? }; |
| 57 | + |
| 58 | + lend_mut(cid, opcode, &mut request.data, 0, name.len().min(NAME_MAX_LENGTH)) |
| 59 | + .expect("unable to perform lookup"); |
| 60 | + |
| 61 | + // Read the result code back from the nameserver |
| 62 | + let result = u32::from_le_bytes(request.data[0..4].try_into().unwrap()); |
| 63 | + if result == 0 { |
| 64 | + // If the result was successful, then the CID is stored in the next 4 bytes |
| 65 | + Some(u32::from_le_bytes(request.data[4..8].try_into().unwrap()).into()) |
| 66 | + } else { |
| 67 | + None |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pub fn connect_with_name(name: &str) -> Option<Connection> { |
| 72 | + connect_with_name_impl(name, true) |
| 73 | + } |
| 74 | + |
| 75 | + pub fn try_connect_with_name(name: &str) -> Option<Connection> { |
| 76 | + connect_with_name_impl(name, false) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Attempt to connect to a server by name. If the server does not exist, this will |
| 81 | +/// block until the server is created. |
| 82 | +/// |
| 83 | +/// Note that this is different from connecting to a server by address. Server |
| 84 | +/// addresses are always 16 bytes long, whereas server names are arbitrary-length |
| 85 | +/// strings up to 64 bytes in length. |
| 86 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 87 | +pub fn connect(name: &str) -> Option<Connection> { |
| 88 | + ns::connect_with_name(name) |
| 89 | +} |
| 90 | + |
| 91 | +/// Attempt to connect to a server by name. If the server does not exist, this will |
| 92 | +/// immediately return `None`. |
| 93 | +/// |
| 94 | +/// Note that this is different from connecting to a server by address. Server |
| 95 | +/// addresses are always 16 bytes long, whereas server names are arbitrary-length |
| 96 | +/// strings. |
| 97 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 98 | +pub fn try_connect(name: &str) -> Option<Connection> { |
| 99 | + ns::try_connect_with_name(name) |
| 100 | +} |
| 101 | + |
| 102 | +static NAME_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0); |
| 103 | + |
| 104 | +/// Return a `Connection` to the name server. If the name server has not been started, |
| 105 | +/// then this call will block until the name server has been started. The `Connection` |
| 106 | +/// will be shared among all connections in a process, so it is safe to call this |
| 107 | +/// multiple times. |
| 108 | +pub(crate) fn name_server() -> Connection { |
| 109 | + let cid = NAME_SERVER_CONNECTION.load(Ordering::Relaxed); |
| 110 | + if cid != 0 { |
| 111 | + return cid.into(); |
| 112 | + } |
| 113 | + |
| 114 | + let cid = crate::os::xous::ffi::connect("xous-name-server".try_into().unwrap()).unwrap(); |
| 115 | + NAME_SERVER_CONNECTION.store(cid.into(), Ordering::Relaxed); |
| 116 | + cid |
| 117 | +} |
| 118 | + |
| 119 | +fn try_name_server() -> Option<Connection> { |
| 120 | + let cid = NAME_SERVER_CONNECTION.load(Ordering::Relaxed); |
| 121 | + if cid != 0 { |
| 122 | + return Some(cid.into()); |
| 123 | + } |
| 124 | + |
| 125 | + if let Ok(Some(cid)) = crate::os::xous::ffi::try_connect("xous-name-server".try_into().unwrap()) |
| 126 | + { |
| 127 | + NAME_SERVER_CONNECTION.store(cid.into(), Ordering::Relaxed); |
| 128 | + Some(cid) |
| 129 | + } else { |
| 130 | + None |
| 131 | + } |
| 132 | +} |
0 commit comments