1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use api::Disconnect;
use core::fmt::Write;
use num_traits::ToPrimitive;
use xous_ipc::{Buffer, String};
#[repr(C, align(4096))]
struct ConnectRequest {
name: [u8; 64],
len: u32,
_padding: [u8; 4096 - 4 - 64],
}
impl Default for ConnectRequest {
fn default() -> Self {
ConnectRequest { name: [0u8; 64], len: 0, _padding: [0u8; 4096 - 4 - 64] }
}
}
#[doc = include_str!("../README.md")]
#[derive(Debug)]
pub struct XousNames {
conn: xous::CID,
}
impl XousNames {
pub fn new() -> Result<Self, xous::Error> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let conn = xous::connect(xous::SID::from_bytes(b"xous-name-server").unwrap())
.expect("Couldn't connect to XousNames");
Ok(XousNames { conn })
}
pub fn unregister_server(&self, sid: xous::SID) -> Result<(), xous::Error> {
let s = sid.to_array();
let response = xous::send_message(
self.conn,
xous::Message::new_blocking_scalar(
api::Opcode::Unregister.to_usize().unwrap(),
s[0] as usize,
s[1] as usize,
s[2] as usize,
s[3] as usize,
),
)
.expect("unregistration failed");
if let xous::Result::Scalar1(result) = response {
if result != 0 {
Ok(())
} else {
Err(xous::Error::ServerNotFound)
}
} else {
Err(xous::Error::InternalError)
}
}
pub fn register_name(
&self,
name: &str,
max_conns: Option<u32>,
) -> Result<xous::SID, xous::Error> {
let mut registration = api::Registration {
name: String::<64>::new(),
conn_limit: max_conns,
};
write!(registration.name, "{}", name).expect("name probably too long");
let mut buf = Buffer::into_buf(registration).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::Register.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::SID(sid_raw) => {
let sid = sid_raw.into();
xous::create_server_with_sid(sid).expect("can't auto-register server");
Ok(sid)
}
api::Return::Failure => Err(xous::Error::InternalError),
_ => unimplemented!("unimplemented return codes"),
}
}
pub fn request_connection_with_token(
&self,
name: &str,
) -> Result<(xous::CID, Option<[u32; 4]>), xous::Error> {
let mut lookup_name = xous_ipc::String::<64>::new();
write!(lookup_name, "{}", name).expect("name probably too long");
let mut buf = Buffer::into_buf(lookup_name).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::Lookup.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::CID((cid, token)) => Ok((cid, token)),
_ => Err(xous::Error::ServerNotFound),
}
}
pub fn disconnect_with_token(&self, name: &str, token: [u32; 4]) -> Result<(), xous::Error> {
let disconnect = Disconnect {
name: String::<64>::from_str(name),
token,
};
let mut buf = Buffer::into_buf(disconnect).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::Disconnect.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::Success => Ok(()),
_ => Err(xous::Error::ServerNotFound),
}
}
pub fn request_connection(&self, name: &str) -> Result<xous::CID, xous::Error> {
let mut lookup_name = xous_ipc::String::<64>::new();
write!(lookup_name, "{}", name).expect("name probably too long");
let mut buf = Buffer::into_buf(lookup_name).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::Lookup.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
api::Return::CID((cid, _)) => Ok(cid),
_ => Err(xous::Error::ServerNotFound),
}
}
pub fn request_connection_blocking(&self, name: &str) -> Result<xous::CID, xous::Error> {
match self.request_connection(name) { Ok(val) => Ok(val),
Err(xous::Error::AccessDenied) => Err(xous::Error::AccessDenied),
_ => {
xous::yield_slice();
let mut cr: ConnectRequest = Default::default();
let name_bytes = name.as_bytes();
cr.len = cr.name.len().min(name_bytes.len()) as u32;
for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.name) {
*dest_byte = src_byte;
}
log::info!("connection requested {}", name);
let msg = xous::MemoryMessage {
id: api::Opcode::BlockingConnect.to_usize().unwrap(),
buf: unsafe{ xous::MemoryRange::new(&mut cr as *mut _ as *mut u8 as usize, core::mem::size_of::<ConnectRequest>())?
},
offset: None,
valid: xous::MemorySize::new(cr.len as usize),
};
xous::send_message(self.conn, xous::Message::MutableBorrow(msg))?;
let response_ptr = &cr as *const ConnectRequest as *const u32;
let result = unsafe { response_ptr.read() }; if result == 0 {
let cid = unsafe { response_ptr.add(1).read() }.into(); log::info!("connected to {}:{}", name, cid);
Ok(cid)
} else {
Err(xous::Error::InternalError)
}
}
}
}
pub fn trusted_init_done(&self) -> Result<bool, xous::Error> {
let response = xous::send_message(
self.conn,
xous::Message::new_blocking_scalar(
api::Opcode::TrustedInitDone.to_usize().unwrap(),
0,
0,
0,
0,
),
)
.expect("couldn't query trusted_init_done");
if let xous::Result::Scalar1(result) = response {
if result == 1 {
Ok(true)
} else {
Ok(false)
}
} else {
Err(xous::Error::InternalError)
}
}
}
use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
impl Drop for XousNames {
fn drop(&mut self) {
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe {
xous::disconnect(self.conn).unwrap();
}
}
}
}