Skip to content

Commit

Permalink
Added temporary protocol structs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Jan 16, 2023
1 parent 466a688 commit 6df3d4c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion networking/firmware_protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

mod clientbound;
mod sansio;
pub mod sansio;
mod serverbound;

pub use clientbound::*;
Expand Down
44 changes: 44 additions & 0 deletions networking/firmware_protocol/src/sansio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SbPacket> {
// 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<SbPacket>) {
todo!()
}
}

pub struct Connected {}
impl Connected {
pub fn received_msg(self, m: CbPacket) -> (ProtocolState, Option<SbPacket>) {
todo!()
}
pub fn send_imu(&mut self, x: f32, y: f32, z: f32, w: f32) -> SbPacket {
todo!()
}
}
4 changes: 2 additions & 2 deletions networking/firmware_protocol/src/sansio/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

0 comments on commit 6df3d4c

Please sign in to comment.