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
#![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};
#[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 problably 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 problably 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> {
loop {
match self.request_connection(name) {
Ok(val) => return Ok(val),
Err(xous::Error::AccessDenied) => return Err(xous::Error::AccessDenied),
_ => (),
}
log::info!("connection to {} could not be established, retrying", name);
xous::yield_slice();
}
}
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();
}
}
}
}