Skip to content
File

Blob: archive/fft-benchmark/firmware/crates/esp32-radio/src/console.rs

rust42 lines
1//! USB maintenance only. Browser commands cannot reach this parser.
2use crate::{error::Result, platform};
3use serde::Deserialize;
4use std::{thread, time::Duration};
5 
6#[derive(Deserialize)]
7#[serde(tag = "cmd", rename_all = "snake_case", deny_unknown_fields)]
8enum Maintenance {
9 Ping,
10 RestartDevice,
11}
12 
13pub(crate) fn run() -> Result<()> {
14 let mut line = Vec::with_capacity(128);
15 let mut overflow = false;
16 loop {
17 match platform::console_byte() {
18 -1 => thread::sleep(Duration::from_millis(10)),
19 10 => {
20 if !overflow {
21 match serde_json::from_slice::<Maintenance>(&line) {
22 Ok(Maintenance::RestartDevice) => platform::restart(),
23 Ok(Maintenance::Ping) => println!(
24 "PROBE {{\"event\":\"command_result\",\"cmd\":\"ping\",\"result\":0,\"firmware\":\"rust\"}}"
25 ),
26 Err(_) => {}
27 }
28 }
29 line.clear();
30 overflow = false;
31 }
32 byte => {
33 if line.len() < 128 {
34 line.push(byte as u8);
35 } else {
36 overflow = true;
37 }
38 }
39 }
40 }
41}