diff --git a/Cargo.lock b/Cargo.lock index 044c4eb..455bb58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -223,6 +223,7 @@ dependencies = [ "bus", "downcast-rs", "num", + "once_cell", "postcard", "serde", "spacepackets", @@ -230,6 +231,13 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "fsrc-example" +version = "0.1.0" +dependencies = [ + "spacepackets", +] + [[package]] name = "hash32" version = "0.2.1" @@ -417,9 +425,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" [[package]] name = "parking_lot_core" diff --git a/Cargo.toml b/Cargo.toml index 87806fc..66610da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,5 @@ members = [ "fsrc-core", "spacepackets", + "fsrc-example" ] diff --git a/fsrc-core/Cargo.toml b/fsrc-core/Cargo.toml index f3c9cd2..9cc1e70 100644 --- a/fsrc-core/Cargo.toml +++ b/fsrc-core/Cargo.toml @@ -21,6 +21,7 @@ default-features = false postcard = { version = "1.0.1", features = ["use-std"] } serde = "1.0.143" zerocopy = "0.6.1" +once_cell = "1.13.1" [features] default = ["use_std"] diff --git a/fsrc-core/examples/tc_server.rs b/fsrc-core/examples/tc_server.rs deleted file mode 100644 index dfdced9..0000000 --- a/fsrc-core/examples/tc_server.rs +++ /dev/null @@ -1,23 +0,0 @@ -extern crate core; - -use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; -use std::thread; - -fn main() { - let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7301); - let socket = UdpSocket::bind(&server_addr.clone()).expect("Error opening UDP socket"); - let mut recv_buf = [0; 1024]; - let jh = thread::spawn(move || { - let dummy_data = [1, 2, 3, 4]; - let client = UdpSocket::bind("127.0.0.1:7300").expect("Connecting to UDP server failed"); - client - .send_to(&dummy_data, &server_addr) - .expect(&*format!("Sending to {:?} failed", server_addr)); - }); - let (num_bytes, src) = socket.recv_from(&mut recv_buf).expect("UDP Receive error"); - println!( - "Received {num_bytes} bytes from {src}: {:x?}", - &recv_buf[0..num_bytes] - ); - jh.join().expect("Joining thread failed"); -} diff --git a/fsrc-core/examples/test.rs b/fsrc-core/examples/test.rs deleted file mode 100644 index 49a7405..0000000 --- a/fsrc-core/examples/test.rs +++ /dev/null @@ -1,67 +0,0 @@ -use postcard::{from_bytes, to_stdvec}; -use serde::{Deserialize, Serialize}; -use zerocopy::byteorder::{I32, U16}; -use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned}; - -#[derive(AsBytes, FromBytes, Unaligned, Debug, Eq, PartialEq)] -#[repr(C, packed)] -struct ZeroCopyTest { - some_bool: u8, - some_u16: U16, - some_i32: I32, - some_float: [u8; 4], -} - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -struct PostcardTest { - some_bool: u8, - some_u16: u16, - some_i32: i32, - some_float: f32, -} - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -struct SliceSerTest<'slice> { - some_u8: u8, - some_u32: u32, - some_slice: &'slice [u8], -} - -fn main() { - let pc_test = PostcardTest { - some_bool: true as u8, - some_u16: 0x42, - some_i32: -200, - some_float: 7.7_f32, - }; - - let out = to_stdvec(&pc_test).unwrap(); - println!("{:#04x?}", out); - - let sample_hk = ZeroCopyTest { - some_bool: true as u8, - some_u16: U16::from(0x42), - some_i32: I32::from(-200), - some_float: 7.7_f32.to_be_bytes(), - }; - let mut slice = [0; 11]; - sample_hk.write_to(slice.as_mut_slice()); - println!("{:#04x?}", slice); - - let ser_vec; - { - let test_buf = [0, 1, 2, 3]; - let test_with_slice = SliceSerTest { - some_u8: 12, - some_u32: 1, - some_slice: test_buf.as_slice(), - }; - ser_vec = to_stdvec(&test_with_slice).unwrap(); - println!("{:#04x?}", out); - } - - { - let test_deser: SliceSerTest = from_bytes(ser_vec.as_slice()).unwrap(); - println!("{:?}", test_deser); - } -} diff --git a/fsrc-core/src/tmtc/ccsds_distrib.rs b/fsrc-core/src/tmtc/ccsds_distrib.rs index 689b956..dfe1238 100644 --- a/fsrc-core/src/tmtc/ccsds_distrib.rs +++ b/fsrc-core/src/tmtc/ccsds_distrib.rs @@ -151,7 +151,7 @@ impl CcsdsDistributor { /// This function can be used to retrieve a reference to the concrete instance of the APID /// handler after it was passed to the distributor. See the - /// [module documentation][crate::tmtc::ccsds_distrib] for an example. + /// [module documentation][crate::tmtc::ccsds_distrib] for an fsrc-example. pub fn apid_handler_ref>(&self) -> Option<&T> { self.apid_handler.downcast_ref::() } diff --git a/fsrc-core/src/tmtc/mod.rs b/fsrc-core/src/tmtc/mod.rs index 4b6fe16..0c9c02c 100644 --- a/fsrc-core/src/tmtc/mod.rs +++ b/fsrc-core/src/tmtc/mod.rs @@ -42,7 +42,7 @@ pub trait ReceivesTc { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error>; } -/// Generic trait for object which can receive CCSDS space packets, for example ECSS PUS packets +/// Generic trait for object which can receive CCSDS space packets, for fsrc-example ECSS PUS packets /// for CCSDS File Delivery Protocol (CFDP) packets. /// /// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the diff --git a/fsrc-example/Cargo.toml b/fsrc-example/Cargo.toml new file mode 100644 index 0000000..1ddb71a --- /dev/null +++ b/fsrc-example/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fsrc-example" +version = "0.1.0" +edition = "2021" +authors = ["Robin Mueller "] + +[dependencies.spacepackets] +path = "../spacepackets" diff --git a/fsrc-example/src/bin/client.rs b/fsrc-example/src/bin/client.rs new file mode 100644 index 0000000..5956570 --- /dev/null +++ b/fsrc-example/src/bin/client.rs @@ -0,0 +1,16 @@ +use fsrc_example::{OBSW_SERVER_ADDR, SERVER_PORT}; +use spacepackets::tc::PusTc; +use spacepackets::SpHeader; +use std::net::{IpAddr, SocketAddr, UdpSocket}; + +fn main() { + let mut buf = [0; 32]; + let addr = SocketAddr::new(IpAddr::V4(OBSW_SERVER_ADDR), SERVER_PORT); + let mut sph = SpHeader::tc(0x02, 0, 0).unwrap(); + let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true); + let client = UdpSocket::bind("127.0.0.1:7300").expect("Connecting to UDP server failed"); + let size = pus_tc.write_to(&mut buf).expect("Creating PUS TC failed"); + client + .send_to(&buf[0..size], &addr) + .expect(&*format!("Sending to {:?} failed", addr)); +} diff --git a/fsrc-example/src/bin/obsw.rs b/fsrc-example/src/bin/obsw.rs new file mode 100644 index 0000000..690cd3e --- /dev/null +++ b/fsrc-example/src/bin/obsw.rs @@ -0,0 +1,15 @@ +use fsrc_example::{OBSW_SERVER_ADDR, SERVER_PORT}; +use std::net::{IpAddr, SocketAddr, UdpSocket}; + +fn main() { + let mut recv_buf = [0; 1024]; + let addr = SocketAddr::new(IpAddr::V4(OBSW_SERVER_ADDR), SERVER_PORT); + let socket = UdpSocket::bind(&addr).expect("Error opening UDP socket"); + loop { + let (num_bytes, src) = socket.recv_from(&mut recv_buf).expect("UDP Receive error"); + println!( + "Received TM with len {num_bytes} from {src}: {:x?}", + &recv_buf[0..num_bytes] + ); + } +} diff --git a/fsrc-example/src/lib.rs b/fsrc-example/src/lib.rs new file mode 100644 index 0000000..b1bb894 --- /dev/null +++ b/fsrc-example/src/lib.rs @@ -0,0 +1,4 @@ +use std::net::Ipv4Addr; + +pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1); +pub const SERVER_PORT: u16 = 7301;