diff --git a/boards/communication/src/data_manager.rs b/boards/communication/src/data_manager.rs index 466a901..11be630 100644 --- a/boards/communication/src/data_manager.rs +++ b/boards/communication/src/data_manager.rs @@ -2,13 +2,17 @@ use heapless::{HistoryBuffer, Vec}; use messages::command::RadioRate; use messages::state::StateData; use messages::Message; +use messages::{ + MAX_COMMAND_SIZE, MAX_HEALTH_SIZE, MAX_LOG_SIZE, MAX_SENSOR_SIZE, MAX_SIZE, MAX_STATE_SIZE, +}; +use postcard; const MAX_RADIO_MSG: u8 = 255; -#[derive(Clone)] pub struct DataManager { pub message_queue: HistoryBuffer, pub logging_rate: Option, + pub state: Option, } impl DataManager { @@ -16,6 +20,7 @@ impl DataManager { Self { message_queue: HistoryBuffer::new(), logging_rate: Some(RadioRate::Slow), // start slow. + state: None, } } @@ -29,22 +34,54 @@ impl DataManager { return RadioRate::Slow; } - pub fn stuff_messages(&mut self) -> Option> { + pub fn stuff_messages(&mut self) -> Result, postcard::Error> { let mut bytes: Vec = Vec::new(); for el in self.message_queue.oldest_ordered() { - bytes.extend_from_slice(el.to_bytes()) + match el.data { + messages::Data::Command(_) => { + if bytes.len() + MAX_COMMAND_SIZE <= MAX_SIZE { + bytes.extend(postcard::to_vec::(el)?); + } else { + break; + } + } + messages::Data::Health(_) => { + if bytes.len() + MAX_HEALTH_SIZE <= MAX_SIZE { + bytes.extend(postcard::to_vec::(el)?); + } else { + break; + } + } + messages::Data::Sensor(_) => { + if bytes.len() + MAX_SENSOR_SIZE <= MAX_SIZE { + bytes.extend(postcard::to_vec::(el)?); + } else { + break; + } + } + messages::Data::State(_) => { + if bytes.len() + MAX_STATE_SIZE <= MAX_SIZE { + bytes.extend(postcard::to_vec::(el)?); + } else { + break; + } + } + messages::Data::Log(_) => { + if bytes.len() + MAX_LOG_SIZE <= MAX_SIZE { + bytes.extend(postcard::to_vec::(el)?); + } else { + break; + } + } + } } if bytes.len() > 0 { - return Some(bytes); + return Ok(bytes); } - None + return Err(postcard::Error::WontImplement); } - pub fn clone_states(&self) -> [Option; 1] { - [self.state.clone()] - } pub fn handle_data(&mut self, data: Message) { - self.message_queue.write(data); match data.data { messages::Data::Command(command) => match command.data { messages::command::CommandData::RadioRateChange(command_data) => { @@ -54,7 +91,9 @@ impl DataManager { messages::command::CommandData::DeployMain(_) => {} messages::command::CommandData::PowerDown(_) => {} }, - _ => {} + _ => { + self.message_queue.write(data); + } } } } diff --git a/boards/communication/src/main.rs b/boards/communication/src/main.rs index 29faeb3..db03f29 100644 --- a/boards/communication/src/main.rs +++ b/boards/communication/src/main.rs @@ -221,11 +221,10 @@ mod app { }); } - /// Receives a log message from the custom logger so that it can be sent over the radio. pub fn queue_gs_message(d: impl Into) { let message = Message::new(&monotonics::now(), COM_ID, d.into()); - - send_gs::spawn(message).ok(); + let bytes = postcard::to_vec(&message).unwrap(); + spawn!(send_gs, bytes).ok(); } /** @@ -245,7 +244,7 @@ mod app { fn sd_dump(cx: sd_dump::Context, m: Vec) { let manager = cx.local.sd_manager; cx.shared.em.run(|| { - let mut buf: [u8; 255] = m.into_array()?; + let mut buf: [u8; 255] = [0; 255]; let msg_ser = postcard::to_slice_cobs(&m, &mut buf)?; if let Some(mut file) = manager.file.take() { manager.write(&mut file, &msg_ser)?; @@ -263,7 +262,7 @@ mod app { */ #[task(shared = [data_manager, &em])] fn sensor_send(mut cx: sensor_send::Context) { - let (sensors, logging_rate) = cx.shared.data_manager.lock(|data_manager| { + let (stuffed_messages, logging_rate) = cx.shared.data_manager.lock(|data_manager| { ( data_manager.stuff_messages(), data_manager.get_logging_rate(), @@ -271,8 +270,9 @@ mod app { }); cx.shared.em.run(|| { - spawn!(send_gs, sensors.clone())?; - spawn!(sd_dump, sensors)?; + let bytes = postcard::to_vec(&stuffed_messages?)?; + spawn!(send_gs, bytes.clone())?; + spawn!(sd_dump, bytes)?; Ok(()) }); match logging_rate { @@ -294,7 +294,8 @@ mod app { cx.shared.em.run(|| { if let Some(x) = state_data { let message = Message::new(&monotonics::now(), COM_ID, State::new(x)); - spawn!(send_gs, message)?; + let bytes = postcard::to_vec(&message).unwrap(); + spawn!(send_gs, bytes)?; } // if there is none we still return since we simply don't have data yet. Ok(()) }); @@ -315,7 +316,8 @@ mod app { Health::new(health_manager.monitor.data.clone(), state), ) }); - spawn!(send_gs, msg)?; + let bytes = postcard::to_vec(&msg).unwrap(); + spawn!(send_gs, bytes)?; spawn_after!(report_health, ExtU64::secs(5))?; Ok(()) });