Initial commit

main
Joey Hines 2023-07-03 15:55:49 -06:00
commit 1ceebcce17
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
4 changed files with 104 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
/target
/Cargo.lock
/.idea

9
Cargo.toml 100644
View File

@ -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"

4
src/lib.rs 100644
View File

@ -0,0 +1,4 @@
#![cfg_attr(not(test), no_std)]
pub mod packet;

88
src/packet/mod.rs 100644
View File

@ -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<Self, Self::Error> {
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());
}
}