diff --git a/networking/firmware_protocol/src/lib.rs b/networking/firmware_protocol/src/lib.rs index 26d8caf5..504cbdfb 100644 --- a/networking/firmware_protocol/src/lib.rs +++ b/networking/firmware_protocol/src/lib.rs @@ -3,7 +3,7 @@ extern crate alloc; mod clientbound; -mod sansio; +pub mod sansio; mod serverbound; pub use clientbound::*; diff --git a/networking/firmware_protocol/src/sansio/mod.rs b/networking/firmware_protocol/src/sansio/mod.rs index d3fae140..c9ab6ebe 100644 --- a/networking/firmware_protocol/src/sansio/mod.rs +++ b/networking/firmware_protocol/src/sansio/mod.rs @@ -2,4 +2,48 @@ //! //! sans-io means that it performs no io and can be used in async or non async code. +use crate::{CbPacket, SbPacket}; + mod serialization; + +// TODO: All of this code operates on deku stuff, can we make it generic? + +pub enum ProtocolState { + Disconnected(Disconnected), + Connected(Connected), +} +impl ProtocolState { + pub const fn new() -> Self { + Self::Disconnected(Disconnected {}) + } + pub fn received_msg(&mut self, m: CbPacket) -> Option { + // We are going to pass ownership of self, by stealing it out of the enum + // temporarily, and putting it back in later. + + let mut taken = Self::new(); + core::mem::swap(self, &mut taken); + let (mut taken, packet) = match taken { + ProtocolState::Disconnected(s) => s.received_msg(m), + ProtocolState::Connected(s) => s.received_msg(m), + }; + core::mem::swap(self, &mut taken); + packet + } +} + +pub struct Disconnected {} +impl Disconnected { + pub fn received_msg(self, m: CbPacket) -> (ProtocolState, Option) { + todo!() + } +} + +pub struct Connected {} +impl Connected { + pub fn received_msg(self, m: CbPacket) -> (ProtocolState, Option) { + todo!() + } + pub fn send_imu(&mut self, x: f32, y: f32, z: f32, w: f32) -> SbPacket { + todo!() + } +} diff --git a/networking/firmware_protocol/src/sansio/serialization.rs b/networking/firmware_protocol/src/sansio/serialization.rs index e7109116..cdafc257 100644 --- a/networking/firmware_protocol/src/sansio/serialization.rs +++ b/networking/firmware_protocol/src/sansio/serialization.rs @@ -20,6 +20,6 @@ pub trait SerializeExact { /// `f`'s argument. fn serialize_exact<'a, 'b>( &'a mut self, - f: impl FnOnce(usize) -> nb::Result<&'b mut [u8], Self::Error>, // TODO: This might not work - ) -> nb::Result<(), Self::Error>; + f: impl FnOnce(usize) -> Result<&'b mut [u8], Self::Error>, + ) -> Result<(), Self::Error>; }