RTC cmdline function mostly working

seems there is some issue with de-registering/disconnecting
connections though, need to make simpler test cases to diagnose
This commit is contained in:
bunnie 2021-04-10 01:01:51 +08:00
parent 4957effe9c
commit f1d97fe428
6 changed files with 57 additions and 36 deletions

View file

@ -560,7 +560,7 @@ impl InputTracker {
p_tv.ellipsis = true;
p_tv.style = blitstr::GlyphStyle::Small;
write!(p_tv.text, "{}", pred_str).expect("can't write the prediction string");
log::trace!("****posting string with length {}", p_tv.text.as_str().unwrap().len());
log::trace!("posting string with length {}", p_tv.text.as_str().unwrap().len());
self.gam.post_textview(&mut p_tv).expect("couldn't post prediction text");
i += 1;
}
@ -597,11 +597,11 @@ fn handle_keyevents(keys: [char; 4]) {
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = true;
let dbglistener = false;
let debug1 = false;
let dbglistener = true;
let dbgcanvas = false;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Trace);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();

View file

@ -34,14 +34,19 @@ impl Rtc {
// tell my handler thread to quit
log::trace!("connect for unhook");
let cid = xous::connect(sid).expect("can't connect to CB server for disconnect message");
log::trace!("sending drop");
log::trace!("sending drop to conn {}", cid);
send_message(cid,
Message::new_scalar(Return::Drop.to_usize().unwrap(), 0, 0, 0, 0)
).unwrap();
log::trace!("disconnecting");
unsafe{xous::disconnect(cid).expect("can't disconnect from CB server");}
log::trace!("disconnecting unhook connection");
unsafe{
match xous::disconnect(cid) {
Ok(_) => log::trace!("disconnected unhook connection"),
Err(e) => log::error!("unhook rtc got error: {:?}", e),
};
}
}
log::trace!("nullifying");
log::trace!("nullifying local state");
self.callback_sid = None;
unsafe{RTC_CB = None};
Ok(())
@ -142,5 +147,6 @@ fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
}
}
}
log::trace!("rtc callback server exiting");
xous::destroy_server(sid).expect("can't destroy my server on exit!");
}

View file

@ -8,11 +8,15 @@ use xous_ipc::Buffer;
use api::{Return, Opcode, DateTime};
use xous::{CID, msg_scalar_unpack};
use core::sync::atomic::{AtomicU32, Ordering};
static CB_TO_MAIN_CONN: AtomicU32 = AtomicU32::new(0);
#[cfg(target_os = "none")]
mod implementation {
#![allow(dead_code)]
use bitflags::*;
use crate::CB_TO_MAIN_CONN;
use core::sync::atomic::Ordering;
use llio::{I2cStatus, I2cTransaction, Llio};
use crate::api::{Opcode, DateTime, Weekday};
use xous_ipc::Buffer;
@ -212,7 +216,8 @@ mod implementation {
fn i2c_callback(trans: I2cTransaction) {
if trans.status == I2cStatus::ResponseReadOk {
if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} {
let cb_to_main_conn = CB_TO_MAIN_CONN.load(Ordering::Relaxed);
if cb_to_main_conn != 0 {
// we expect this to be 0, as we don't set it
assert!(trans.callback_id == 0, "callback ID was incorrect!");
if let Some(rxbuf) = trans.rxbuf {
@ -457,6 +462,7 @@ mod implementation {
use crate::api::Weekday;
use chrono::prelude::*;
use crate::CB_TO_MAIN_CONN;
use core::sync::atomic::Ordering;
use num_traits::ToPrimitive;
fn rtc_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
@ -466,7 +472,8 @@ mod implementation {
let msg = xous::receive_message(sid).unwrap();
log::trace!("rtc callback got msg: {:?}", msg);
// we only have one purpose, and that's to send this message.
if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} {
let cb_to_main_conn = CB_TO_MAIN_CONN.load(Ordering::Relaxed);
if cb_to_main_conn != 0 {
log::trace!("rtc_get sending time to main server");
let now = Local::now();
let wday: Weekday = match now.weekday() {
@ -522,19 +529,18 @@ mod implementation {
}
}
static mut CB_TO_MAIN_CONN: Option<CID> = None;
#[xous::xous_main]
fn xmain() -> ! {
use crate::implementation::Rtc;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Trace);
log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
let rtc_sid = xns.register_name(api::SERVER_NAME_RTC).expect("can't register server");
log::trace!("registered with NS -- {:?}", rtc_sid);
unsafe{CB_TO_MAIN_CONN = Some(xous::connect(rtc_sid).unwrap())};
CB_TO_MAIN_CONN.store(xous::connect(rtc_sid).unwrap(), Ordering::Relaxed);
#[cfg(target_os = "none")]
let mut rtc = Rtc::new(&xns);
@ -599,20 +605,23 @@ fn xmain() -> ! {
let incoming_buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let dt = incoming_buffer.to_original::<DateTime, _>().unwrap();
log::trace!("ResponseDateTime received: {:?}", dt);
let outgoing_buf = Buffer::into_buf(dt).or(Err(xous::Error::InternalError)).unwrap();
for maybe_conn in dt_cb_conns.iter_mut() {
if let Some(conn) = maybe_conn {
//log::trace!("ResponeDateTime sending to {}", *conn);
let outgoing_buf = Buffer::into_buf(dt).or(Err(xous::Error::InternalError)).unwrap();
log::trace!("ResponeDateTime sending to {}", *conn);
match outgoing_buf.lend(*conn, Return::ReturnDateTime.to_u32().unwrap()) {
Err(xous::Error::ServerNotFound) => {
log::trace!("ServerNotFound, dropping connection");
*maybe_conn = None
},
Ok(_) => {},
Ok(_) => {
log::trace!("RespondeDateTime sent successfully");
},
_ => panic!("unhandled error or result in callback processing")
}
}
}
//log::trace!("ResponeDateTime done");
log::trace!("ResponeDateTime done");
},
Some(Opcode::RequestDateTime) => {
let mut sent = false;
@ -653,8 +662,9 @@ fn xmain() -> ! {
// clean up our program
log::trace!("main loop exit, destroying servers");
unsafe{
if let Some(cb)= CB_TO_MAIN_CONN {
xous::disconnect(cb).unwrap();
let cb_to_main_conn = CB_TO_MAIN_CONN.load(Ordering::Relaxed);
if cb_to_main_conn != 0 {
xous::disconnect(cb_to_main_conn).unwrap();
}
}
for entry in dt_cb_conns.iter_mut() {

View file

@ -8,7 +8,7 @@ pub trait ShellCmdApi<'a> {
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error>;
// called to process incoming messages that may have been origniated by the most recently issued command
fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
log::info!("SHCH: received unhandled message {:?}", msg);
log::info!("received unhandled message {:?}", msg);
Ok(None)
}
@ -62,7 +62,7 @@ mod echo; use echo::*;
//mod test; use test::*;
mod sleep; use sleep::*;
mod sensors; use sensors::*;
//mod callback; use callback::*;
mod callback; use callback::*;
mod rtc_cmd; use rtc_cmd::*;
#[derive(Debug)]
@ -73,7 +73,7 @@ pub struct CmdEnv {
//test_cmd: Test,
sleep_cmd: Sleep,
sensors_cmd: Sensors,
//callback_cmd: CallBack,
callback_cmd: CallBack,
rtc_cmd: RtcCmd,
}
impl CmdEnv {
@ -81,17 +81,17 @@ impl CmdEnv {
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
CmdEnv {
common_env: CommonEnv {
llio: llio::Llio::new(&xns).expect("CMD: couldn't connect to LLIO"),
com: com::Com::new(&xns).expect("CMD: could't connect to COM"),
llio: llio::Llio::new(&xns).expect("couldn't connect to LLIO"),
com: com::Com::new(&xns).expect("could't connect to COM"),
ticktimer: ticktimer,
gam: gam::Gam::new(&xns).expect("CMD: couldn't connect to GAM"),
gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"),
},
lastverb: String::<256>::new(),
///// 3. initialize your storage, by calling new()
//test_cmd: Test::new(),
sleep_cmd: Sleep::new(),
sensors_cmd: Sensors::new(),
//callback_cmd: CallBack::new(),
callback_cmd: CallBack::new(),
rtc_cmd: RtcCmd::new(&xns),
}
}
@ -106,7 +106,7 @@ impl CmdEnv {
//&mut self.test_cmd,
&mut self.sleep_cmd,
&mut self.sensors_cmd,
//&mut self.callback_cmd,
&mut self.callback_cmd,
&mut self.rtc_cmd,
];

View file

@ -1,14 +1,12 @@
use crate::{ShellCmdApi,CommonEnv};
use xous_ipc::String;
use core::sync::atomic::{AtomicU32, Ordering};
static SHELLCONN: AtomicU32 = AtomicU32::new(0);
pub fn dt_callback(dt: rtc::DateTime) {
let xns = xous_names::XousNames::new().unwrap();
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
let buf = xous_ipc::Buffer::into_buf(dt).or(Err(xous::Error::InternalError)).unwrap();
buf.lend(callback_conn, 0xdead_beef).unwrap(); // send an "unknown ID" so it's routed to the callback handler
unsafe{xous::disconnect(callback_conn).unwrap()};
log::trace!("SHELLCONN: {}", SHELLCONN.load(Ordering::Relaxed));
buf.send(SHELLCONN.load(Ordering::Relaxed), 0xdead_beef).unwrap(); // send an "unknown ID" so it's routed to the callback handler
}
#[derive(Debug)]
@ -17,6 +15,8 @@ pub struct RtcCmd {
}
impl RtcCmd {
pub fn new(xns: &xous_names::XousNames) -> Self {
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
SHELLCONN.store(callback_conn, Ordering::Relaxed);
let rtc = rtc::Rtc::new(&xns).unwrap();
RtcCmd {
rtc,
@ -112,7 +112,7 @@ impl<'a> ShellCmdApi<'a> for RtcCmd {
years: year,
weekday,
};
write!(ret, "Setting {}:{}:{}, {}/{}/{}, {:?}", hour, min, sec, month, day, year, weekday).unwrap();
write!(ret, "Setting {}:{}:{}, {}/{}/{}, {:?}", hour, min, sec, month, day, year, weekday).unwrap();
self.rtc.set_rtc(dt).unwrap();
}
},
@ -136,7 +136,6 @@ impl<'a> ShellCmdApi<'a> for RtcCmd {
let mut ret = String::<1024>::new();
write!(ret, "{}:{:02}:{:02}, {}/{}/{}, {:?}", dt.hours, dt.minutes, dt.seconds, dt.months, dt.days, dt.years, dt.weekday).unwrap();
// this currently doesn't work, under suspicion of perhaps one of the destroy/disconnect functions not working right
self.rtc.unhook_rtc_callback().expect("can't unhook callback after completion");
Ok(Some(ret))
}

View file

@ -114,6 +114,7 @@ impl Repl{
// take the input and pass it on to the various command parsers, and attach result
if let Some(mut local) = self.input {
log::trace!("processing line: {}", local);
if let Some(res) = self.env.dispatch(Some(&mut local), None).expect("command dispatch failed") {
let mut response = String::<1024>::new();
write!(response, "{}", res).expect("can't copy result to history");
@ -124,7 +125,8 @@ impl Repl{
self.circular_push(output_history);
}
} else if let Some(msg) = &self.msg {
if let Some(res) = self.env.dispatch(None, Some(msg)).expect("SCHC: callback failed") {
log::trace!("processing callback msg: {:?}", msg);
if let Some(res) = self.env.dispatch(None, Some(msg)).expect("callback failed") {
let mut response = String::<1024>::new();
write!(response, "{}", res).expect("can't copy result to history");
let output_history = History {
@ -279,12 +281,15 @@ fn xmain() -> ! {
update_repl = true; // set a flag, instead of calling here, so message can drop and calling server is released
}
Some(ShellOpcode::Redraw) => {
log::trace!("got Redraw");
repl.redraw().expect("REPL couldn't redraw");
}
Some(ShellOpcode::Quit) => {
log::trace!("got Quit");
break;
}
_ => {
log::trace!("got unknown message, treating as callback");
repl.msg(msg);
update_repl = true;
}
@ -293,6 +298,7 @@ fn xmain() -> ! {
repl.update().expect("REPL had problems updating");
update_repl = false;
}
log::trace!("reached bottom of main loop");
}
// clean up our program
log::trace!("main loop exit, destroying servers");