xous-core/services/shellchat/src/cmds/ssid.rs

63 lines
2 KiB
Rust
Raw Normal View History

2021-04-16 15:07:31 +00:00
use crate::{ShellCmdApi, CommonEnv};
use xous_ipc::String;
use core::fmt::Write;
use xous::MessageEnvelope;
2021-04-16 15:07:31 +00:00
#[derive(Debug)]
pub struct Ssid {
cb_id: Option<xous::CID>,
2021-04-16 15:07:31 +00:00
}
impl Ssid {
pub fn new() -> Ssid {
Ssid {
cb_id: None,
2021-04-16 15:07:31 +00:00
}
}
}
impl<'a> ShellCmdApi<'a> for Ssid {
cmd_api!(ssid);
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
let mut ret = String::<1024>::new();
let helpstring = "ssid [scan]";
let mut tokens = args.as_str().unwrap().split(' ');
if self.cb_id.is_none() {
self.cb_id = Some(
env.register_handler(String::<256>::from_str(self.verb()))
);
}
2021-04-16 15:07:31 +00:00
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
THE BALDEST YAK: keyboard injection now working I wrote in a commit yesterday how there was no ROI on doing this yet somehow I kept on doing it. I blame poor impulse control. Turns out there was a series of race conditions and bugs that caused this thing to "mostly work" on the first go at it but really hard to debug and get very right. 1. Litex UART has a weird undocumented API. You advance the Rx FIFO by writing `1` to the PENDING bit, and no other way. However, you determine if there are any more characters to receive by reading the EMPTY bit and seeing if it is 1. The previous method wrote `1` to the PENDING bit and then used the PENDING bit to see if a character was available, under the theory that the PENDING bit would be set again if there was still a character. This is somewhat true, except that the PENDING bit is set only some amount of time later which is about comparable to a few CPU instructions, so if you had a fast loop path the PENDING bit would show as cleared and you'd exit, which would cause no further interrupts to fire because the interrupt was effecitvely not acknowledged. This isn't written up anywhere but I figured this out by just reading the source code for Litex UART and BIOS: https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279 https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44 2. I had the Key Injector IP block in the `sys` domain, which would have its clock cut when the CPU went to sleep, which would cause input characters to disappear when the CPU went into power saving. moving this to "always on" domain fixes that. 3. I had the UART PHY in the always on block (so you could send or receive exactly one character), but not the separate UART block (which contained the FIFOs). The UART block would also go into power savings, and this would cause FIFO entries to go missing. 4. Escape sequences get sent as a burst of characters. The Key Injector originally did not have a FIFO of its own, so it would only remember the *last* key of the sequence that was written, causing some keys to be dropped occassionally. Adding a 16-deep FIFO to the injector fixes this. 5. The code I had to read out the injector's results was designed with the "strobe" bit (to indicate that the FIFO had characters) in the same regsiter as the character itself. Thus the loop that 1. checked if the fifo was empty then 2. read and forwarded the character would actually perform two reads from the FIFO. However, this "mostly worked" I think because the FIFO retains the last value that was written even when it was empty, so if only one character was read at a time everything was fine. It only didn't work when you stuffed two characters in there, as you'd drop the first one. Reading the FIFO once and then interpreting the code with a bitmask fixes this. 6. The escape sequences, in their full glory with no dropped characters, required a refactored parsing routine, which I have now added. 7. backspace->delete mappings, the bane of computer engineers since 1960. OK. very bald yak. Here I lay down the shears next to several mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
"force" => {
env.com.set_ssid_scanning(true).unwrap();
write!(ret, "Forcing SSID scan. Warning: could put the connection manager in an inconsistent state.").unwrap();
}
2021-04-16 15:07:31 +00:00
"scan" => {
THE BALDEST YAK: keyboard injection now working I wrote in a commit yesterday how there was no ROI on doing this yet somehow I kept on doing it. I blame poor impulse control. Turns out there was a series of race conditions and bugs that caused this thing to "mostly work" on the first go at it but really hard to debug and get very right. 1. Litex UART has a weird undocumented API. You advance the Rx FIFO by writing `1` to the PENDING bit, and no other way. However, you determine if there are any more characters to receive by reading the EMPTY bit and seeing if it is 1. The previous method wrote `1` to the PENDING bit and then used the PENDING bit to see if a character was available, under the theory that the PENDING bit would be set again if there was still a character. This is somewhat true, except that the PENDING bit is set only some amount of time later which is about comparable to a few CPU instructions, so if you had a fast loop path the PENDING bit would show as cleared and you'd exit, which would cause no further interrupts to fire because the interrupt was effecitvely not acknowledged. This isn't written up anywhere but I figured this out by just reading the source code for Litex UART and BIOS: https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279 https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44 2. I had the Key Injector IP block in the `sys` domain, which would have its clock cut when the CPU went to sleep, which would cause input characters to disappear when the CPU went into power saving. moving this to "always on" domain fixes that. 3. I had the UART PHY in the always on block (so you could send or receive exactly one character), but not the separate UART block (which contained the FIFOs). The UART block would also go into power savings, and this would cause FIFO entries to go missing. 4. Escape sequences get sent as a burst of characters. The Key Injector originally did not have a FIFO of its own, so it would only remember the *last* key of the sequence that was written, causing some keys to be dropped occassionally. Adding a 16-deep FIFO to the injector fixes this. 5. The code I had to read out the injector's results was designed with the "strobe" bit (to indicate that the FIFO had characters) in the same regsiter as the character itself. Thus the loop that 1. checked if the fifo was empty then 2. read and forwarded the character would actually perform two reads from the FIFO. However, this "mostly worked" I think because the FIFO retains the last value that was written even when it was empty, so if only one character was read at a time everything was fine. It only didn't work when you stuffed two characters in there, as you'd drop the first one. Reading the FIFO once and then interpreting the code with a bitmask fixes this. 6. The escape sequences, in their full glory with no dropped characters, required a refactored parsing routine, which I have now added. 7. backspace->delete mappings, the bane of computer engineers since 1960. OK. very bald yak. Here I lay down the shears next to several mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
// SSID scanning is automatically initiated by the connection manager, so we don't initiate it explicitly, just report results
let ssid_list = env.netmgr.wifi_get_ssid_list().unwrap();
write!(ret, "RSSI reported in dBm:\n").unwrap();
for ssid in ssid_list {
if name.len() > 0 {
write!(ret, "-{} {}\n", ssid.rssi, &ssid.name.as_str().unwrap_or("UTF-8 error")).unwrap();
}
}
2021-04-16 15:07:31 +00:00
}
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
} else {
write!(ret, "{}", helpstring).unwrap();
}
Ok(Some(ret))
}
fn callback(&mut self, _msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
let ret = String::<1024>::new();
2021-04-16 15:07:31 +00:00
Ok(Some(ret))
}
}