From 1ceebcce17fc7ececedebfb39c070261fa54e533 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 3 Jul 2023 15:55:49 -0600 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ Cargo.toml | 9 +++++ src/lib.rs | 4 +++ src/packet/mod.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/packet/mod.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8eb581d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/Cargo.lock +/.idea diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c962aeb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bone_rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bitfield = "0.14.0" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..51f6f4b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +#![cfg_attr(not(test), no_std)] + +pub mod packet; + diff --git a/src/packet/mod.rs b/src/packet/mod.rs new file mode 100644 index 0000000..74dada6 --- /dev/null +++ b/src/packet/mod.rs @@ -0,0 +1,88 @@ +use bitfield::bitfield; + +#[derive(Debug)] +pub enum BonePacketError { + SourceBufferTooSmall, +} + +bitfield! { + #[derive(PartialEq, Clone)] + pub struct BonePacketHeader(u64); + impl Debug; + u16; + version, set_version: 2, 0; + sec_header_count, set_sec_header_count: 7, 3; + category, set_category: 23, 8; + unique_id, set_unique_id: 39, 24; + seq_type, set_seq_type: 42, 40; + seq_count, set_seq_count: 47, 43; + length, set_length: 63, 48; +} + +#[derive(Debug, Clone)] +pub struct BonePacket<'a> { + header: BonePacketHeader, + data: &'a [u8] +} + +impl<'a> TryFrom<&'a [u8]> for BonePacket<'a> { + type Error = BonePacketError; + + fn try_from(buf: &'a [u8]) -> Result { + if buf.len() < 8 { + return Err(BonePacketError::SourceBufferTooSmall) + } + + let header: u64 = (buf[7] as u64) << 56 | (buf[6] as u64) << 48 | (buf[5] as u64) << 40 | (buf[4] as u64) << 32 | (buf[3] as u64) << 24 | (buf[2] as u64) << 16 | (buf[1] as u64) << 8 | buf[0] as u64; + + let data = &buf[8..]; + + Ok(BonePacket { + header: BonePacketHeader(header), + data + }) + } +} + +#[cfg(test)] +mod tests { + use crate::packet::{BonePacket, BonePacketHeader}; + + #[test] + fn test_header_to_from_bytes() { + let mut header = BonePacketHeader(0x0); + header.set_version(0); + header.set_sec_header_count(1); + header.set_category(5); + header.set_unique_id(42069); + header.set_seq_type(1); + header.set_seq_count(100); + header.set_length(500); + + let buf = header.0.clone(); + + let header2 = BonePacketHeader(buf); + + assert_eq!(header, header2) + } + + #[test] + fn test_from_array() { + let mut header = BonePacketHeader(0x0); + header.set_version(0); + header.set_sec_header_count(1); + header.set_category(5); + header.set_unique_id(42069); + header.set_seq_type(1); + header.set_seq_count(100); + header.set_length(500); + + let buf = header.0.to_ne_bytes(); + + println!("{:?}", buf); + let packet = BonePacket::try_from(buf.as_slice()).unwrap(); + + assert_eq!(header, packet.header); + assert_eq!(0, packet.data.len()); + } +} \ No newline at end of file