mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
services: allow for multiple callbacks
This means we can have multiple clients requesting a callback. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
561a0c3e27
commit
c1fe20c06b
1 changed files with 36 additions and 9 deletions
|
|
@ -77,8 +77,16 @@ fn handle_log_string(mem: &xous::MemoryMessage) {
|
||||||
fn double_string(mem: &mut xous::MemoryMessage) {
|
fn double_string(mem: &mut xous::MemoryMessage) {
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
||||||
let mut response = api::StringDoubler { value: xous::String::new() };
|
let mut response = api::StringDoubler {
|
||||||
for ch in buffer.try_into::<api::StringDoubler, _>().unwrap().value.as_str().chars() {
|
value: xous::String::new(),
|
||||||
|
};
|
||||||
|
for ch in buffer
|
||||||
|
.try_into::<api::StringDoubler, _>()
|
||||||
|
.unwrap()
|
||||||
|
.value
|
||||||
|
.as_str()
|
||||||
|
.chars()
|
||||||
|
{
|
||||||
write!(response.value, "{}{}", ch, ch).ok();
|
write!(response.value, "{}{}", ch, ch).ok();
|
||||||
}
|
}
|
||||||
buffer.serialize_from(response).unwrap();
|
buffer.serialize_from(response).unwrap();
|
||||||
|
|
@ -86,7 +94,6 @@ fn double_string(mem: &mut xous::MemoryMessage) {
|
||||||
|
|
||||||
#[xous::xous_main]
|
#[xous::xous_main]
|
||||||
fn test_main() -> ! {
|
fn test_main() -> ! {
|
||||||
let mut callback_conn = None;
|
|
||||||
log_server::init_wait().unwrap();
|
log_server::init_wait().unwrap();
|
||||||
|
|
||||||
log::info!(
|
log::info!(
|
||||||
|
|
@ -95,6 +102,8 @@ fn test_main() -> ! {
|
||||||
);
|
);
|
||||||
let sid = xous_names::register_name(api::SERVER_NAME).unwrap();
|
let sid = xous_names::register_name(api::SERVER_NAME).unwrap();
|
||||||
|
|
||||||
|
let mut logstring_callback_connections = [None; 32];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut msg = xous::receive_message(sid).unwrap();
|
let mut msg = xous::receive_message(sid).unwrap();
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
|
@ -106,15 +115,23 @@ fn test_main() -> ! {
|
||||||
}
|
}
|
||||||
Some(api::Opcode::LogString) => {
|
Some(api::Opcode::LogString) => {
|
||||||
let memory = msg.body.memory_message().unwrap();
|
let memory = msg.body.memory_message().unwrap();
|
||||||
if let Some(callback_sid) = callback_conn {
|
// If a callback exists, first pass this message to the callback server.
|
||||||
let buffer = unsafe { buffer::Buffer::from_memory_message(memory) };
|
for callback_conn in logstring_callback_connections.iter() {
|
||||||
buffer
|
if let Some(callback_sid) = callback_conn {
|
||||||
.lend(callback_sid, api::CallbackType::LogString.to_u32().unwrap())
|
let buffer = unsafe { buffer::Buffer::from_memory_message(memory) };
|
||||||
.unwrap();
|
buffer
|
||||||
|
.lend(
|
||||||
|
*callback_sid,
|
||||||
|
api::CallbackType::LogString.to_u32().unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handle_log_string(msg.body.memory_message().unwrap())
|
handle_log_string(msg.body.memory_message().unwrap())
|
||||||
}
|
}
|
||||||
Some(api::Opcode::AddLogStringCallback) => {
|
Some(api::Opcode::AddLogStringCallback) => {
|
||||||
|
// The Log String Callback provides us a SID. Connect to that SID
|
||||||
|
// and add it to the list of connections available.
|
||||||
if let xous::Message::Scalar(xous::ScalarMessage {
|
if let xous::Message::Scalar(xous::ScalarMessage {
|
||||||
id: _id,
|
id: _id,
|
||||||
arg1,
|
arg1,
|
||||||
|
|
@ -124,7 +141,17 @@ fn test_main() -> ! {
|
||||||
}) = msg.body
|
}) = msg.body
|
||||||
{
|
{
|
||||||
let sid = xous::SID::from_u32(arg1 as _, arg2 as _, arg3 as _, arg4 as _);
|
let sid = xous::SID::from_u32(arg1 as _, arg2 as _, arg3 as _, arg4 as _);
|
||||||
callback_conn = Some(xous::connect(sid).unwrap());
|
let cb_conn = Some(xous::connect(sid).unwrap());
|
||||||
|
// Add this callback connection to the list of callbacks if it doesn't
|
||||||
|
// already exist.
|
||||||
|
if !logstring_callback_connections.contains(&cb_conn) {
|
||||||
|
for entry in logstring_callback_connections.iter_mut() {
|
||||||
|
if *entry == None {
|
||||||
|
*entry = cb_conn;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => (),
|
None => (),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue