2021-03-03 06:53:33 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
use ime_plugin_api::*;
|
|
|
|
|
use core::convert::TryFrom;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
use heapless::spsc::Queue;
|
2021-03-07 20:25:58 +00:00
|
|
|
use heapless::consts::U8;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
2021-03-03 15:27:30 +00:00
|
|
|
use rkyv::Unarchive;
|
|
|
|
|
use core::pin::Pin;
|
|
|
|
|
use rkyv::{archived_value, archived_value_mut};
|
|
|
|
|
|
2021-03-03 06:53:33 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
let debug1 = true;
|
|
|
|
|
log_server::init_wait().unwrap();
|
|
|
|
|
info!("IME_SH: my PID is {}", xous::process::id());
|
|
|
|
|
|
|
|
|
|
let ime_sh_sid = xous_names::register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IME_SH: can't register server");
|
2021-03-04 08:02:42 +00:00
|
|
|
if debug1{info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);}
|
2021-03-03 06:53:33 +00:00
|
|
|
|
2021-03-07 20:25:58 +00:00
|
|
|
let mut history: Queue<xous::String<64>, U4> = Queue::new(); // this has 2^4 elements = 16??? or does it just have 4 elements.
|
|
|
|
|
let history_max = 4;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
let mytriggers = PredictionTriggers {
|
2021-03-07 16:57:50 +00:00
|
|
|
newline: true,
|
|
|
|
|
punctuation: false,
|
|
|
|
|
whitespace: false,
|
2021-03-03 19:14:26 +00:00
|
|
|
};
|
|
|
|
|
|
2021-03-04 08:02:42 +00:00
|
|
|
info!("IME_SH: ready to accept requests");
|
2021-03-03 06:53:33 +00:00
|
|
|
loop {
|
|
|
|
|
let envelope = xous::receive_message(ime_sh_sid).unwrap();
|
2021-03-03 19:14:26 +00:00
|
|
|
if debug1{info!("IME_SH: received message {:?}", envelope);}
|
2021-03-03 15:27:30 +00:00
|
|
|
if let xous::Message::Borrow(m) = &envelope.body {
|
2021-03-03 06:53:33 +00:00
|
|
|
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let bytes = Pin::new(buf.as_ref());
|
|
|
|
|
let value = unsafe {
|
2021-03-03 19:14:26 +00:00
|
|
|
archived_value::<Opcode>(&bytes, m.id as usize)
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
|
|
|
|
match &*value {
|
2021-03-03 19:14:26 +00:00
|
|
|
rkyv::Archived::<Opcode>::Input(_rkyv_s) => {
|
|
|
|
|
// shell does nothing with the input, it only keeps track of
|
|
|
|
|
// the picked results
|
|
|
|
|
},
|
|
|
|
|
rkyv::Archived::<Opcode>::Picked(rkyv_s) => {
|
2021-03-07 20:25:58 +00:00
|
|
|
let s: xous::String<4000> = rkyv_s.unarchive();
|
|
|
|
|
let mut local_s: xous::String<64> = xous::String::new();
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
write!(local_s, "{:32}", s).expect("IME_SH: overflowed history variable");
|
|
|
|
|
if debug1{info!("IME_SH: storing history value | {}", s);}
|
2021-03-03 06:53:33 +00:00
|
|
|
if history.len() == history_max {
|
2021-03-03 15:27:30 +00:00
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue history");
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
2021-03-07 20:25:58 +00:00
|
|
|
history.enqueue(local_s).expect("IME_SH: couldn't store history");
|
|
|
|
|
if debug1{info!("IME_SH: history has length {}", history.len());}
|
2021-03-03 06:53:33 +00:00
|
|
|
},
|
2021-03-03 15:27:30 +00:00
|
|
|
_ => error!("IME_SH: unknown Borrow message")
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
2021-03-03 15:27:30 +00:00
|
|
|
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
|
2021-03-03 06:53:33 +00:00
|
|
|
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let value = unsafe {
|
2021-03-03 19:14:26 +00:00
|
|
|
archived_value_mut::<Opcode>(Pin::new(buf.as_mut()), m.id as usize)
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
|
|
|
|
match &*value {
|
2021-03-03 19:14:26 +00:00
|
|
|
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
|
|
|
|
|
let mut prediction: Prediction = pred_r.unarchive();
|
2021-03-07 20:25:58 +00:00
|
|
|
if debug1{info!("IME_SH: querying prediction index {}", prediction.index);}
|
|
|
|
|
if debug1{info!("IME_SH: {:?}", prediction);}
|
|
|
|
|
if history.len() > 0 && ((prediction.index as usize) < history.len()) {
|
2021-03-03 06:53:33 +00:00
|
|
|
let mut index = prediction.index;
|
2021-03-03 15:27:30 +00:00
|
|
|
if index >= history.len() as u32 {
|
|
|
|
|
index = history.len() as u32 - 1;
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
2021-03-03 15:27:30 +00:00
|
|
|
let mut i = history.len() as u32;
|
|
|
|
|
for &s in history.iter() {
|
2021-03-03 06:53:33 +00:00
|
|
|
// iterator is from oldest to newest, so do some math to go from newest to oldest
|
2021-03-03 15:27:30 +00:00
|
|
|
if (history.len() as u32 - i) == index {
|
2021-03-07 20:25:58 +00:00
|
|
|
// decompose the string into a character-by-character sequence
|
|
|
|
|
// and then stuff byte-by-byte, as fits, into the return array
|
|
|
|
|
prediction.len = 0;
|
|
|
|
|
for ch in s.as_str().unwrap().chars() {
|
|
|
|
|
match ch.len_utf8() {
|
|
|
|
|
1 => {
|
|
|
|
|
if prediction.len < prediction.string.len() as u32 {
|
|
|
|
|
prediction.string[prediction.len as usize] = ch as u8;
|
|
|
|
|
prediction.len += 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
let mut data: [u8; 4] = [0; 4];
|
|
|
|
|
let subslice = ch.encode_utf8(&mut data);
|
|
|
|
|
if prediction.len + (subslice.len() as u32) < prediction.string.len() as u32 {
|
|
|
|
|
for c in subslice.bytes() {
|
|
|
|
|
prediction.string[prediction.len as usize] = c;
|
|
|
|
|
prediction.len += 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
prediction.valid = true;
|
2021-03-03 06:53:33 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2021-03-07 20:25:58 +00:00
|
|
|
i = i - 1;
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
|
|
|
|
} else { // there is no history
|
2021-03-07 20:25:58 +00:00
|
|
|
prediction.valid = false;
|
|
|
|
|
if debug1{info!("IME_SH: no prediction found");}
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
2021-03-07 20:25:58 +00:00
|
|
|
if debug1{info!("IME_SH: returning index {} string {:?}", prediction.index, prediction.string);}
|
|
|
|
|
|
|
|
|
|
// pack our data back into the buffer to return
|
|
|
|
|
use rkyv::Write;
|
|
|
|
|
let ret_op = Opcode::Prediction(prediction);
|
|
|
|
|
let mut writer = rkyv::ArchiveBuffer::new(buf);
|
|
|
|
|
let _pos = writer.archive(&ret_op).expect("IME_SH: Prediction query couldn't re-archive return value");
|
|
|
|
|
if debug1{info!("IME_SH: archived with pos {}", _pos);}
|
2021-03-03 15:27:30 +00:00
|
|
|
},
|
2021-03-07 20:25:58 +00:00
|
|
|
_ => error!("IME_SH: Got invalid MutableBorrow")
|
|
|
|
|
};
|
2021-03-03 19:14:26 +00:00
|
|
|
} else if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
Opcode::Unpick => {
|
|
|
|
|
if history.len() == 1 {
|
|
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (1)");
|
|
|
|
|
} else if history.len() > 1 {
|
|
|
|
|
// rotate everything around except the last entry
|
|
|
|
|
for _ in 0 .. history.len() - 1 {
|
|
|
|
|
let s = history.dequeue().expect("IME_SH: couldn't dequee in Unpick (>1)");
|
|
|
|
|
history.enqueue(s).expect("IME_SH: couldn't enqueue in Unpick");
|
|
|
|
|
}
|
|
|
|
|
// discard the last entry
|
|
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (>1 last)");
|
|
|
|
|
}
|
|
|
|
|
// in case of 0 length, do nothing
|
|
|
|
|
},
|
|
|
|
|
Opcode::GetPredictionTriggers => {
|
|
|
|
|
xous::return_scalar(envelope.sender, mytriggers.into()).expect("IME_SH: couldn't return GetPredictionTriggers");
|
|
|
|
|
},
|
|
|
|
|
_ => error!("IME_SH: unknown Opcode"),
|
|
|
|
|
}
|
2021-03-03 06:53:33 +00:00
|
|
|
} else {
|
2021-03-03 15:27:30 +00:00
|
|
|
info!("IME_SH: received unknown message type {:?}", envelope);
|
2021-03-03 06:53:33 +00:00
|
|
|
panic!("IME_SH: received unknown message type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|