mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +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) {
|
||||
use core::fmt::Write;
|
||||
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
||||
let mut response = api::StringDoubler { value: xous::String::new() };
|
||||
for ch in buffer.try_into::<api::StringDoubler, _>().unwrap().value.as_str().chars() {
|
||||
let mut response = api::StringDoubler {
|
||||
value: xous::String::new(),
|
||||
};
|
||||
for ch in buffer
|
||||
.try_into::<api::StringDoubler, _>()
|
||||
.unwrap()
|
||||
.value
|
||||
.as_str()
|
||||
.chars()
|
||||
{
|
||||
write!(response.value, "{}{}", ch, ch).ok();
|
||||
}
|
||||
buffer.serialize_from(response).unwrap();
|
||||
|
|
@ -86,7 +94,6 @@ fn double_string(mem: &mut xous::MemoryMessage) {
|
|||
|
||||
#[xous::xous_main]
|
||||
fn test_main() -> ! {
|
||||
let mut callback_conn = None;
|
||||
log_server::init_wait().unwrap();
|
||||
|
||||
log::info!(
|
||||
|
|
@ -95,6 +102,8 @@ fn test_main() -> ! {
|
|||
);
|
||||
let sid = xous_names::register_name(api::SERVER_NAME).unwrap();
|
||||
|
||||
let mut logstring_callback_connections = [None; 32];
|
||||
|
||||
loop {
|
||||
let mut msg = xous::receive_message(sid).unwrap();
|
||||
match FromPrimitive::from_usize(msg.body.id()) {
|
||||
|
|
@ -106,15 +115,23 @@ fn test_main() -> ! {
|
|||
}
|
||||
Some(api::Opcode::LogString) => {
|
||||
let memory = msg.body.memory_message().unwrap();
|
||||
if let Some(callback_sid) = callback_conn {
|
||||
let buffer = unsafe { buffer::Buffer::from_memory_message(memory) };
|
||||
buffer
|
||||
.lend(callback_sid, api::CallbackType::LogString.to_u32().unwrap())
|
||||
.unwrap();
|
||||
// If a callback exists, first pass this message to the callback server.
|
||||
for callback_conn in logstring_callback_connections.iter() {
|
||||
if let Some(callback_sid) = callback_conn {
|
||||
let buffer = unsafe { buffer::Buffer::from_memory_message(memory) };
|
||||
buffer
|
||||
.lend(
|
||||
*callback_sid,
|
||||
api::CallbackType::LogString.to_u32().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
handle_log_string(msg.body.memory_message().unwrap())
|
||||
}
|
||||
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 {
|
||||
id: _id,
|
||||
arg1,
|
||||
|
|
@ -124,7 +141,17 @@ fn test_main() -> ! {
|
|||
}) = msg.body
|
||||
{
|
||||
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 => (),
|
||||
|
|
|
|||
Loading…
Reference in a new issue