diff --git a/satrs-core/src/parsers/ccsds.rs b/satrs-core/src/encoding/ccsds.rs similarity index 100% rename from satrs-core/src/parsers/ccsds.rs rename to satrs-core/src/encoding/ccsds.rs diff --git a/satrs-core/src/parsers/cobs.rs b/satrs-core/src/encoding/cobs.rs similarity index 92% rename from satrs-core/src/parsers/cobs.rs rename to satrs-core/src/encoding/cobs.rs index 5d906a8..0b0e04a 100644 --- a/satrs-core/src/parsers/cobs.rs +++ b/satrs-core/src/encoding/cobs.rs @@ -1,5 +1,17 @@ use crate::tmtc::ReceivesTcCore; -use cobs::decode_in_place; +use cobs::{decode_in_place, encode}; + +/// This function encodes the given packet with COBS and also wraps the encoded packet with +/// the sentinel value 0. It can be used repeatedly on the same encoded buffer by expecting +/// and incrementing the mutable reference of the current packet index. This is also used +/// to retrieve the total encoded size. +pub fn encode_packet_with_cobs(packet: &[u8], encoded_buf: &mut [u8], current_idx: &mut usize) { + encoded_buf[*current_idx] = 0; + *current_idx += 1; + *current_idx += encode(packet, &mut encoded_buf[*current_idx..]); + encoded_buf[*current_idx] = 0; + *current_idx += 1; +} /// This function parses a given buffer for COBS encoded packets. The packet structure is /// expected to be like this, assuming a sentinel value of 0 as the packet delimiter: @@ -58,7 +70,7 @@ pub(crate) mod tests { use cobs::encode; use crate::{ - parsers::tests::{encode_simple_packet, INVERTED_PACKET, SIMPLE_PACKET}, + encoding::tests::{encode_simple_packet, INVERTED_PACKET, SIMPLE_PACKET}, tmtc::ReceivesTcCore, }; diff --git a/satrs-core/src/encoding/mod.rs b/satrs-core/src/encoding/mod.rs new file mode 100644 index 0000000..33d2727 --- /dev/null +++ b/satrs-core/src/encoding/mod.rs @@ -0,0 +1,22 @@ +pub mod ccsds; +pub mod cobs; + +pub use crate::encoding::ccsds::parse_buffer_for_ccsds_space_packets; +pub use crate::encoding::cobs::parse_buffer_for_cobs_encoded_packets; + +#[cfg(test)] +pub(crate) mod tests { + use super::cobs::encode_packet_with_cobs; + + pub(crate) const SIMPLE_PACKET: [u8; 5] = [1, 2, 3, 4, 5]; + pub(crate) const INVERTED_PACKET: [u8; 5] = [5, 4, 3, 2, 1]; + + pub(crate) fn encode_simple_packet(encoded_buf: &mut [u8], current_idx: &mut usize) { + encode_packet_with_cobs(&SIMPLE_PACKET, encoded_buf, current_idx) + } + + #[allow(dead_code)] + pub(crate) fn encode_inverted_packet(encoded_buf: &mut [u8], current_idx: &mut usize) { + encode_packet_with_cobs(&INVERTED_PACKET, encoded_buf, current_idx) + } +} diff --git a/satrs-core/src/hal/std/tcp_with_cobs_server.rs b/satrs-core/src/hal/std/tcp_with_cobs_server.rs index ec080e2..357b07d 100644 --- a/satrs-core/src/hal/std/tcp_with_cobs_server.rs +++ b/satrs-core/src/hal/std/tcp_with_cobs_server.rs @@ -8,7 +8,7 @@ use std::net::TcpListener; use std::net::TcpStream; use std::vec::Vec; -use crate::parsers::parse_buffer_for_cobs_encoded_packets; +use crate::encoding::parse_buffer_for_cobs_encoded_packets; use crate::tmtc::ReceivesTc; use crate::tmtc::TmPacketSource; @@ -171,8 +171,8 @@ mod tests { }; use crate::{ + encoding::tests::{INVERTED_PACKET, SIMPLE_PACKET}, hal::std::tcp_server::ServerConfig, - parsers::tests::{INVERTED_PACKET, SIMPLE_PACKET}, tmtc::{ReceivesTcCore, TmPacketSourceCore}, }; use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec::Vec}; diff --git a/satrs-core/src/lib.rs b/satrs-core/src/lib.rs index 50e9d42..8ae56e5 100644 --- a/satrs-core/src/lib.rs +++ b/satrs-core/src/lib.rs @@ -20,6 +20,7 @@ extern crate downcast_rs; #[cfg(any(feature = "std", test))] extern crate std; +pub mod encoding; pub mod error; #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] @@ -33,7 +34,6 @@ pub mod hk; pub mod mode; pub mod objects; pub mod params; -pub mod parsers; pub mod pool; pub mod power; pub mod pus; diff --git a/satrs-core/src/parsers/mod.rs b/satrs-core/src/parsers/mod.rs deleted file mode 100644 index c25060a..0000000 --- a/satrs-core/src/parsers/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -pub mod ccsds; -pub mod cobs; - -pub use crate::parsers::ccsds::parse_buffer_for_ccsds_space_packets; -pub use crate::parsers::cobs::parse_buffer_for_cobs_encoded_packets; - -#[cfg(test)] -pub(crate) mod tests { - use cobs::encode; - - pub(crate) const SIMPLE_PACKET: [u8; 5] = [1, 2, 3, 4, 5]; - pub(crate) const INVERTED_PACKET: [u8; 5] = [5, 4, 3, 2, 1]; - - pub(crate) fn encode_simple_packet(encoded_buf: &mut [u8], current_idx: &mut usize) { - encoded_buf[*current_idx] = 0; - *current_idx += 1; - *current_idx += encode(&SIMPLE_PACKET, &mut encoded_buf[*current_idx..]); - encoded_buf[*current_idx] = 0; - *current_idx += 1; - } -}