refactor COM to use generic name registrations for event listeners

This commit is contained in:
bunnie 2021-01-12 18:44:02 +08:00
parent 7cf70892c1
commit 2eafe825b8
8 changed files with 74 additions and 43 deletions

View file

@ -4,6 +4,10 @@ use xous::{Message, ScalarMessage};
// It's just a convenient abuse of already-defined constants. However, it's intended that
// the COM server on the SoC side abstracts much of the EC bus complexity away.
use com_rs::*;
// subtype constants for registering service listeners from the COM
pub const SUBTYPE_REGISTER_BATTSTATS_LISTENER: u16 = 0;
#[derive(Debug, Default)]
pub struct BattStats {
/// instantaneous voltage in mV
@ -98,6 +102,9 @@ pub enum Opcode<'a> {
/// Send Rx stats to fcc-agent
RxStatsAgent,
/// request for a listener to BattStats events
RegisterBattStatsListener(xous_names::api::Registration),
}
impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> {
@ -146,8 +153,12 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> {
)
};
Ok(Opcode::Wf200PdsLine(core::str::from_utf8(s).unwrap()))
} else if xous_names::api::Registration::match_subtype(m.id, SUBTYPE_REGISTER_BATTSTATS_LISTENER) {
Ok(Opcode::RegisterBattStatsListener({
unsafe { *( (m.buf.as_mut_ptr()) as *mut xous_names::api::Registration) }
}))
} else {
Err("unrecognized opcode")
Err("COM: unknown borrow ID")
}
}
_ => Err("unhandled message type"),
@ -221,14 +232,10 @@ impl<'a> Into<Message> for Opcode<'a> {
id: ComState::WFX_FW_REV_GET.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
Opcode::EcGitRev => Message::BlockingScalar(ScalarMessage {
id: ComState::EC_GIT_REV.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
// we use the direct string "lend" API -- the code below actually doesn't work
/*Opcode::Wf200PdsLine(pdsline) => {
let data = xous::carton::Carton::from_bytes(pdsline);
Message::Borrow(data.into_message(ComState::WFX_PDS_LINE_SET.verb as _))
},*/
// Wf200PdsLine() uses the direct string "lend" API
Opcode::RxStatsAgent => Message::Scalar(ScalarMessage {
id: ComState::WFX_RXSTAT_GET.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
_ => todo!("message type not yet implemented")
_ => panic!("opcode not handled -- maybe you meant to use one of the direct APIs")
}
}
}

View file

@ -58,3 +58,11 @@ pub fn send_pds_line(cid: CID, s: &xous::String) -> Result<(), Error> {
pub fn get_rx_stats_agent(cid: CID) -> Result<(), Error> {
send_message(cid, api::Opcode::RxStatsAgent.into()).map(|_| ())
}
// event relay request API
pub fn request_battstat_events(name: &str, com_conn: xous::CID) -> Result<xous::Result, xous::Error> {
xous_names::request_core(name, com_conn, api::SUBTYPE_REGISTER_BATTSTATS_LISTENER)
}
// note to future self: add other event listener registrations (such as network events) here

View file

@ -234,14 +234,14 @@ mod implementation {
#[xous::xous_main]
fn xmain() -> ! {
use crate::implementation::XousCom;
use heapless::Vec;
use heapless::consts::*;
log_server::init_wait().unwrap();
let com_sid = xous_names::register_name(xous::names::SERVER_NAME_COM).expect("COM: can't register server");
info!("COM: registered with NS -- {:?}", com_sid);
let shell_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_SHELL).expect("COM: couldn't establish shell connection");
let agent_conn: usize;
if cfg!(feature = "fccagent") {
agent_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_FCCAGENT).expect("FCCAGENT: can't connect to COM");
@ -252,6 +252,10 @@ fn xmain() -> ! {
// Create a new com object
let mut com = XousCom::new();
// create a Vec to track return connections for battery stats
let mut battstats_conns: Vec<xous::CID, U64> = Vec::new();
// other future notification vectors shall go here
info!("COM: starting main loop");
loop {
let envelope = xous::receive_message(com_sid).unwrap();
@ -272,12 +276,14 @@ fn xmain() -> ! {
info!("COM: done returning batt stats request");
}
Opcode::BattStatsNb => {
com.workqueue
.push(WorkRequest {
work: ComState::STAT,
sender: shell_conn,
})
.unwrap();
for &conn in battstats_conns.iter() {
com.workqueue
.push(WorkRequest {
work: ComState::STAT,
sender: conn,
})
.unwrap();
}
}
Opcode::Wf200Rev => {
com.txrx(ComState::WFX_FW_REV_GET.verb);
@ -326,6 +332,7 @@ fn xmain() -> ! {
}
Opcode::RxStatsAgent => {
if cfg!(feature = "fccagent") {
// note -- this code never worked, but wasn't needed. just hanging out as bread crumbs for future work in case this is needed.
let mut stats: [u8; (ComState::WFX_RXSTAT_GET.r_words*2) as usize] = [0; (ComState::WFX_RXSTAT_GET.r_words*2) as usize];
com.txrx(ComState::WFX_RXSTAT_GET.verb);
for i in 0..ComState::WFX_RXSTAT_GET.r_words as usize {
@ -339,6 +346,10 @@ fn xmain() -> ! {
xous::send_message(agent_conn, m).expect("Can't send RxStat message to FCC agent!");
}
}
Opcode::RegisterBattStatsListener(registration) => {
let cid = xous_names::request_connection_blocking(registration.name.to_str()).expect("COM: can't connect to requested listener for reporting events");
battstats_conns.push(cid).expect("COM: probably ran out of slots for battstats event reporting");
}
_ => error!("unknown opcode"),
}
} else {

View file

@ -2,8 +2,8 @@ use xous::{Message, ScalarMessage};
use heapless::Vec;
use heapless::consts::*;
pub const SUBTYPE_REGISTER_BASIC_LISTENER: u8 = 0;
pub const SUBTYPE_REGISTER_RAW_LISTENER: u8 = 1;
pub const SUBTYPE_REGISTER_BASIC_LISTENER: u16 = 0;
pub const SUBTYPE_REGISTER_RAW_LISTENER: u16 = 1;
#[derive(Debug, Default, Copy, Clone)]
pub struct ScanCode {
@ -154,11 +154,11 @@ impl core::convert::TryFrom<& Message> for Opcode {
_ => Err("KBD api: unknown Scalar ID"),
},
Message::Borrow(m) => {
if (m.id & 0xFF) as u8 == SUBTYPE_REGISTER_BASIC_LISTENER {
if xous_names::api::Registration::match_subtype(m.id, SUBTYPE_REGISTER_BASIC_LISTENER) {
Ok(Opcode::RegisterListener({
unsafe { *( (m.buf.as_mut_ptr()) as *mut xous_names::api::Registration) }
}))
} else if (m.id & 0xFF) as u8 == SUBTYPE_REGISTER_RAW_LISTENER {
} else if xous_names::api::Registration::match_subtype(m.id, SUBTYPE_REGISTER_RAW_LISTENER) {
Ok(Opcode::RegisterRawListener({
unsafe { *( (m.buf.as_mut_ptr()) as *mut xous_names::api::Registration) }
}))

View file

@ -2,24 +2,10 @@
pub mod api;
use xous::ipc::*;
use core::fmt::Write;
fn request_core(name: &str, kbd_conn: xous::CID, subtype: u8) -> Result<xous::Result, xous::Error> {
let registration = xous_names::api::Registration::new();
let mut sendable_registration = Sendable::new(registration)
.expect("can't create sendable registration structure");
sendable_registration.set_subtype(subtype);
write!(sendable_registration.name, "{}", name).unwrap();
sendable_registration.lend(kbd_conn, sendable_registration.mid()).expect("keyboard event request registration failure!");
Ok(xous::Result::Ok)
}
pub fn request_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> {
request_core(name, kbd_conn, api::SUBTYPE_REGISTER_BASIC_LISTENER)
xous_names::request_core(name, kbd_conn, api::SUBTYPE_REGISTER_BASIC_LISTENER)
}
pub fn request_raw_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> {
request_core(name, kbd_conn, api::SUBTYPE_REGISTER_RAW_LISTENER)
xous_names::request_core(name, kbd_conn, api::SUBTYPE_REGISTER_RAW_LISTENER)
}

View file

@ -110,7 +110,10 @@ fn event_thread(_arg: usize) {
let shell_server = xous_names::register_name(xous::names::SERVER_NAME_SHELL).expect("SHELL: can't register server");
let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD).expect("SHELL|event_thread: can't connect to KBD");
keyboard::request_events(xous::names::SERVER_NAME_SHELL, kbd_conn).expect("SHELL:event_thread: couldn't request events from keyboard");
keyboard::request_events(xous::names::SERVER_NAME_SHELL, kbd_conn).expect("SHELL|event_thread: couldn't request events from keyboard");
let com_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_COM).expect("SHELL|event_thread: can't connect to COM");
com::request_battstat_events(xous::names::SERVER_NAME_SHELL, com_conn).expect("SHELL|event_thread: couldn't request events from COM");
info!("SHELL|event_thread: starting COM response handler thread");
loop {

View file

@ -5,10 +5,10 @@ use hash32::{Hash, Hasher};
use core::cmp::Eq;
// bottom 8 bits are reserved for structure re-use by other servers
pub const ID_REGISTER_NAME: usize = 0x0_00;
pub const ID_LOOKUP_NAME: usize = 0x1_00;
pub const ID_AUTHENTICATE: usize = 0x2_00;
// bottom 16 bits are reserved for structure re-use by other servers
pub const ID_REGISTER_NAME: usize = 0x1_0000;
pub const ID_LOOKUP_NAME: usize = 0x2_0000;
pub const ID_AUTHENTICATE: usize = 0x3_0000;
pub const AUTHENTICATE_TIMEOUT: usize = 10_000; // time in ms that a process has to respond to an authentication request
@ -122,8 +122,12 @@ pub struct Registration {
impl Registration {
pub fn mid(&self) -> usize { self.mid }
pub fn get_subtype(&self) -> u8 { (self.mid & 0xFF) as u8 }
pub fn set_subtype(&mut self, subtype: u8) { self.mid = (self.mid & 0xFFFF_FF_00) | (subtype as usize); }
pub fn get_subtype(&self) -> u16 { (self.mid & 0xFFFF) as u16 }
pub fn set_subtype(&mut self, subtype: u16) { self.mid = (self.mid & 0xFFFF_0000) | (subtype as usize); }
pub fn match_subtype(id: usize, subtype: u16) -> bool {
((id & 0xFFFF) as u16 == subtype) && ((id & 0xFFFF_0000) == (ID_REGISTER_NAME & 0xFFFF_0000))
}
pub fn new() -> Self {
Registration {

View file

@ -69,4 +69,16 @@ pub fn request_connection_blocking(name: &str) -> Result<xous::CID, xous::Error>
}
}
}
}
}
/// API for other processes to re-use the Registration record for brokering requests
pub fn request_core(name: &str, conn: xous::CID, subtype: u16) -> Result<xous::Result, xous::Error> {
let registration = Registration::new();
let mut sendable_registration = Sendable::new(registration)
.expect("can't create sendable registration structure");
sendable_registration.set_subtype(subtype);
write!(sendable_registration.name, "{}", name).unwrap();
sendable_registration.lend(conn, sendable_registration.mid()).expect("event request registration failure!");
Ok(xous::Result::Ok)
}