let's try to wrap this up

This commit is contained in:
Robin Müller 2024-06-01 17:35:00 +02:00
parent 776da7b55e
commit d983ae1e84
Signed by: muellerr
GPG Key ID: A649FB78196E3849
10 changed files with 2084 additions and 1373 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// This file was auto-generated by the libcsp-cargo-build library
// This file was auto-generated by libcsp-cargo-build v0.1.0
#define CSP_POSIX 1
#define CSP_ZEPHYR 0

View File

@ -1,4 +1,6 @@
#include "csp/csp.h"
#include "csp/csp_hooks.h"
#include "csp_conn.h"
#include "csp/interfaces/csp_if_lo.h"
#include "csp/interfaces/csp_if_udp.h"
#include "csp_conn.h"

View File

@ -1,4 +1,4 @@
// This file was auto-generated by the libcsp-cargo-build library
// This file was auto-generated by libcsp-cargo-build v0.1.0
#define CSP_POSIX 1
#define CSP_ZEPHYR 0

View File

@ -1,4 +1,4 @@
// This file was auto-generated by the libcsp-cargo-build library
// This file was auto-generated by libcsp-cargo-build v0.1.0
pub const CSP_CONN_RXQUEUE_LEN: usize = 16;
pub const CSP_QFIFO_LEN: usize = 16;
pub const CSP_PORT_MAX_BIND: usize = 16;

View File

@ -10,9 +10,9 @@ use std::{
use libcsp::{
csp_accept_guarded, csp_bind, csp_buffer_get, csp_conn_dport, csp_conn_print_table,
csp_connect_guarded, csp_iflist_print, csp_init, csp_listen, csp_ping, csp_read_guarded,
csp_reboot, csp_route_work, csp_send, csp_service_handler, ConnectOpts, CspError, CspSocket,
MsgPriority, SocketFlags, CSP_ANY, CSP_LOOPBACK,
csp_connect_guarded, csp_init, csp_listen, csp_ping, csp_read_guarded, csp_reboot,
csp_route_work, csp_send, csp_service_handler, iflist::csp_iflist_print, ConnectOpts, CspError,
CspSocket, MsgPriority, SocketFlags, CSP_ANY, CSP_LOOPBACK,
};
const MY_SERVER_PORT: i32 = 10;

View File

@ -1,3 +1,6 @@
//! This crate provides a library to allow building the [`libcsp`](https://github.com/libcsp/libcsp)
//! C library with cargo. You can find some more high-level information and examples in the
//! [main repository](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust).
use std::{
io::{self, Write},
path::{Path, PathBuf},
@ -109,6 +112,17 @@ impl Default for Config {
}
}
/// Primary builder structure used to compile the `libcsp` C library.
///
/// The [Self::cfg] field can be used to configure the library build. The will also take care
/// of generating the autoconfig.h file required for the library configuration automatically based
/// on the build [Config]. An API is also provided to generate the autoconfig.rs file required
/// for compiling the [`libcsp-sys`](https://crates.io/crates/libcp-sys) Rust FFI bindings crate.
///
/// ## Example
///
/// The [example buildscript](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/examples/build.rs)
/// uses this builder structure to compile the library.
pub struct Builder {
generate_autoconf_file: bool,
libcsp_path: PathBuf,
@ -120,6 +134,7 @@ pub struct Builder {
}
impl Builder {
/// Create a new builder instance.
pub fn new(libcsp_path: PathBuf, out_dir: PathBuf) -> Self {
let mut libcsp_src_path_base = libcsp_path.clone();
libcsp_src_path_base.push("src");
@ -134,7 +149,13 @@ impl Builder {
}
}
pub fn cc(&mut self) -> &mut cc::Build {
/// Access to the underlying [cc::Build] builder object.
pub fn cc(&mut self) -> &cc::Build {
&self.build
}
/// Mutable access to the underlying [cc::Build] builder object.
pub fn cc_mut(&mut self) -> &mut cc::Build {
&mut self.build
}
@ -187,6 +208,8 @@ impl Builder {
self.build.cargo_warnings(self.compiler_warnings);
self.build.compile("csp");
// TODO: We could generate some sort of information file which contains the
// compilation information and the version of hte library.
Ok(())
}
@ -216,13 +239,21 @@ impl Builder {
}
}
/// Generate the autoconfig.h file which is required to build the C library. This file contains
/// important configuration defines.
pub fn generate_autoconf_header_file(out_dir: impl AsRef<Path>, cfg: &Config) -> io::Result<()> {
let out_dir = out_dir.as_ref();
let mut autoconf_file_string = String::new();
autoconf_file_string
.push_str("// This file was auto-generated by the libcsp-cargo-build library\n");
let version = env!("CARGO_PKG_VERSION");
let name = env!("CARGO_PKG_NAME");
autoconf_file_string.push_str(&format!(
"// This file was auto-generated by {} v{}\n",
name, version
));
#[cfg(unix)]
autoconf_file_string.push_str("#define CSP_POSIX 1\n");
#[cfg(not(unix))]
autoconf_file_string.push_str("#define CSP_POSIX 0\n");
autoconf_file_string.push_str("#define CSP_ZEPHYR 0\n");
autoconf_file_string.push('\n');
autoconf_file_string.push_str(&format!(
@ -320,11 +351,17 @@ pub fn generate_autoconf_header_file(out_dir: impl AsRef<Path>, cfg: &Config) ->
Ok(())
}
/// Generate the autoconfig.rs file which is required by the
/// [`libcsp-sys`](https://crates.io/crates/libcsp-sys) Rust bindings crate.
pub fn generate_autoconf_rust_file(out_dir: impl AsRef<Path>, cfg: &Config) -> io::Result<()> {
let out_dir = out_dir.as_ref();
let mut autoconf_file_string = String::new();
autoconf_file_string
.push_str("// This file was auto-generated by the libcsp-cargo-build library\n");
let version = env!("CARGO_PKG_VERSION");
let name = env!("CARGO_PKG_NAME");
autoconf_file_string.push_str(&format!(
"// This file was auto-generated by {} v{}\n",
name, version
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::CONN_RXQUEUE_LEN,

View File

@ -1,3 +1,19 @@
//! This crate provides FFI bindings for the [`libcsp` library](https://github.com/libcsp/libcsp).
//! Generally, you probably do not want to use this library directly and instead use the
//! [`libcsp`](https://crates.io/crates/libcsp) Rust crate which provides a safe and ergonomic Rust
//! API.
//!
//! You can find some more high-level information and examples in the
//! [main repository](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust).
//!
//! ## Compile-time configuration
//!
//! This library requires some compile-time configuration file to be included to work
//! properly. You can see an example version of the file for the workspace
//! [here](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/examples/autoconfig.rs).
//! The user has to provide the path to a directory containing this `autoconfig.rs` file using the
//! `CSP_CONFIG_DIR` environmental variable.
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
@ -214,6 +230,116 @@ pub struct csp_conn_s {
pub rdp: csp_rdp_t,
}
#[doc = " Interface Tx function.\n\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub type nexthop_t = ::core::option::Option<
unsafe extern "C" fn(
iface: *mut csp_iface_t,
via: u16,
packet: *mut csp_packet_t,
from_me: ::core::ffi::c_int,
) -> ::core::ffi::c_int,
>;
#[doc = " This struct is referenced in documentation.\n Update doc when you change this."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct csp_iface_s {
#[doc = "< Host address on this subnet"]
pub addr: u16,
#[doc = "< Subnet mask"]
pub netmask: u16,
#[doc = "< Name, max compare length is #CSP_IFLIST_NAME_MAX"]
pub name: *const ::core::ffi::c_char,
#[doc = "< Interface data, only known/used by the interface layer, e.g. state information."]
pub interface_data: *mut ::core::ffi::c_void,
#[doc = "< Driver data, only known/used by the driver layer, e.g. device/channel references."]
pub driver_data: *mut ::core::ffi::c_void,
#[doc = "< Next hop (Tx) function"]
pub nexthop: nexthop_t,
#[doc = "< Set default IF flag (CSP supports multiple defaults)"]
pub is_default: u8,
#[doc = "< Successfully transmitted packets"]
pub tx: u32,
#[doc = "< Successfully received packets"]
pub rx: u32,
#[doc = "< Transmit errors (packets)"]
pub tx_error: u32,
#[doc = "< Receive errors, e.g. too large message"]
pub rx_error: u32,
#[doc = "< Dropped packets"]
pub drop: u32,
#[doc = "< Authentication errors (packets)"]
pub autherr: u32,
#[doc = "< Frame format errors (packets)"]
pub frame: u32,
#[doc = "< Transmitted bytes"]
pub txbytes: u32,
#[doc = "< Received bytes"]
pub rxbytes: u32,
#[doc = "< Interrupts"]
pub irq: u32,
pub next: *mut csp_iface_s,
}
impl Default for csp_iface_s {
fn default() -> Self {
Self {
addr: Default::default(),
netmask: Default::default(),
name: core::ptr::null(),
interface_data: core::ptr::null_mut(),
driver_data: core::ptr::null_mut(),
nexthop: None,
is_default: Default::default(),
tx: Default::default(),
rx: Default::default(),
tx_error: Default::default(),
rx_error: Default::default(),
drop: Default::default(),
autherr: Default::default(),
frame: Default::default(),
txbytes: Default::default(),
rxbytes: Default::default(),
irq: Default::default(),
next: core::ptr::null_mut(),
}
}
}
#[doc = " Forward declaration of CSP interface, see #csp_iface_s for details."]
pub type csp_iface_t = csp_iface_s;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct csp_if_udp_conf_t {
pub host: *mut ::core::ffi::c_char,
pub lport: ::core::ffi::c_int,
pub rport: ::core::ffi::c_int,
pub server_handle: libc::pthread_t,
pub peer_addr: libc::sockaddr_in,
pub sockfd: ::core::ffi::c_int,
}
impl Default for csp_if_udp_conf_t {
fn default() -> Self {
Self {
host: core::ptr::null_mut(),
lport: Default::default(),
rport: Default::default(),
server_handle: Default::default(),
peer_addr: libc::sockaddr_in {
sin_family: Default::default(),
sin_port: Default::default(),
sin_addr: libc::in_addr {
s_addr: Default::default(),
},
sin_zero: Default::default(),
},
sockfd: Default::default(),
}
}
}
extern "C" {
#[doc = " Error counters"]
pub static mut csp_dbg_buffer_out: u8;
@ -286,6 +412,36 @@ extern "C" {
opts: u8,
) -> ::core::ffi::c_int;
#[doc = " Send a single ping/echo packet without waiting for reply.\n Payload is 1 byte.\n\n @param[in] node address of subsystem."]
pub fn csp_ping_noreply(node: u16);
#[doc = " Request process list.\n\n .. note:: This is currently only supported on FreeRTOS systems.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for replies. The function will not return until the timeout occurrs."]
pub fn csp_ps(node: u16, timeout: u32);
#[doc = " Request free memory.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply.\n @param[out] size free memory on subsystem.\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub fn csp_get_memfree(node: u16, timeout: u32, size: *mut u32) -> ::core::ffi::c_int;
#[doc = " Request free memory and print to stdout.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply."]
pub fn csp_memfree(node: u16, timeout: u32);
#[doc = " Request free buffers.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply.\n @param[out] size free buffers.\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub fn csp_get_buf_free(node: u16, timeout: u32, size: *mut u32) -> ::core::ffi::c_int;
#[doc = " Request free buffers and print to stdout.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply."]
pub fn csp_buf_free(node: u16, timeout: u32);
#[doc = " Reboot subsystem.\n If handled by the standard CSP service handler, the reboot handler set by csp_sys_set_reboot() on the subsystem, will be invoked.\n\n @param[in] node address of subsystem.\n"]
pub fn csp_reboot(node: u16);
#[doc = " Shutdown subsystem.\n If handled by the standard CSP service handler, the shutdown handler set by csp_sys_set_shutdown() on the subsystem, will be invoked.\n\n @param[in] node address of subsystem.\n"]
pub fn csp_shutdown(node: u16);
#[doc = " Request uptime and print to stdout.\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply.\n"]
pub fn csp_uptime(node: u16, timeout: u32);
#[doc = " Request uptime\n\n @param[in] node address of subsystem.\n @param[in] timeout timeout in mS to wait for reply.\n @param[out] uptime uptime in seconds.\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub fn csp_get_uptime(node: u16, timeout: u32, uptime: *mut u32) -> ::core::ffi::c_int;
#[doc = " Perform an entire request & reply transaction on an existing connection.\n Send \\a outbuf, wait for reply and copy reply to \\a inbuf.\n\n @param[in] conn connection\n @param[in] timeout timeout in mS to wait for a reply\n @param[in] outbuf outgoing data (request)\n @param[in] outlen length of data in \\a outbuf (request)\n @param[out] inbuf user provided buffer for receiving data (reply)\n @param[in] inlen length of expected reply, -1 for unknown size (inbuf MUST be large enough), 0 for no reply.\n @return 1 or reply size on success, 0 on failure (error, incoming length does not match, timeout)"]
pub fn csp_transaction_persistent(
conn: *mut csp_conn_t,
@ -296,9 +452,6 @@ extern "C" {
inlen: ::core::ffi::c_int,
) -> ::core::ffi::c_int;
#[doc = " Reboot subsystem.\n If handled by the standard CSP service handler, the reboot handler set by csp_sys_set_reboot() on the subsystem, will be invoked.\n\n @param[in] node address of subsystem.\n"]
pub fn csp_reboot(node: u16);
#[doc = " Establish outgoing connection.\n The call will return immediately, unless it is a RDP connection (#CSP_O_RDP) in which case it will wait until the other\n end acknowleges the connection (timeout is determined by the current connection timeout set by csp_rdp_set_opt()).\n\n @param[in] prio priority, see #csp_prio_t\n @param[in] dst Destination address\n @param[in] dst_port Destination port\n @param[in] timeout unused.\n @param[in] opts connection options, see @ref CSP_CONNECTION_OPTIONS.\n @return Established connection or NULL on failure (no free connections, timeout)."]
pub fn csp_connect(
prio: u8,
@ -308,8 +461,19 @@ extern "C" {
opts: u32,
) -> *mut csp_conn_t;
#[doc = " Close a socket, freeing it's RX queue and unbinding it from the associated\n port.\n\n @param[in] sock Socket\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub fn csp_socket_close(sock: *mut csp_socket_t) -> ::core::ffi::c_int;
#[doc = " Return destination port of connection.\n\n @param[in] conn connection\n @return destination port of an incoming connection"]
pub fn csp_conn_dport(conn: *const csp_conn_t) -> ::core::ffi::c_int;
#[doc = " Return source port of connection.\n\n @param[in] conn connection\n @return source port of an incoming connection"]
pub fn csp_conn_sport(conn: *const csp_conn_t) -> ::core::ffi::c_int;
#[doc = " Return destination address of connection.\n\n @param[in] conn connection\n @return destination address of an incoming connection"]
pub fn csp_conn_dst(conn: *const csp_conn_t) -> ::core::ffi::c_int;
#[doc = " Return source address of connection.\n\n @param[in] conn connection\n @return source address of an incoming connection"]
pub fn csp_conn_src(conn: *const csp_conn_t) -> ::core::ffi::c_int;
#[doc = " Return flags of connection.\n\n @param[in] conn connection\n @return flags of an incoming connection, see @ref CSP_HEADER_FLAGS"]
pub fn csp_conn_flags(conn: *const csp_conn_t) -> ::core::ffi::c_int;
#[doc = " Get free buffer from task context.\n\n @param[in] unused OBSOLETE ignored field, csp packets have a fixed size now\n @return Buffer pointer to #csp_packet_t or NULL if no buffers available"]
pub fn csp_buffer_get(unused: usize) -> *mut csp_packet_t;
@ -319,154 +483,65 @@ extern "C" {
#[doc = " Print connection table to stdout."]
pub fn csp_conn_print_table();
pub fn csp_iflist_print();
}
#[test]
fn bindgen_test_layout_csp_conn_s() {
const UNINIT: ::core::mem::MaybeUninit<csp_conn_s> = ::core::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::core::mem::size_of::<csp_conn_s>(),
280usize,
concat!("Size of: ", stringify!(csp_conn_s))
);
assert_eq!(
::core::mem::align_of::<csp_conn_s>(),
8usize,
concat!("Alignment of ", stringify!(csp_conn_s))
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).type_) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(type_)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).state) as usize - ptr as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(state)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).idin) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(idin)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).idout) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(idout)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).sport_outgoing) as usize - ptr as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(sport_outgoing)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue) as usize - ptr as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue_static) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue_static)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue_static_data) as usize - ptr as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue_static_data)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).callback) as usize - ptr as usize },
176usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(callback)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).dest_socket) as usize - ptr as usize },
184usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(dest_socket)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).timestamp) as usize - ptr as usize },
192usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(timestamp)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).opts) as usize - ptr as usize },
196usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(opts)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rdp) as usize - ptr as usize },
200usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rdp)
)
);
pub mod iflist {
use super::*;
extern "C" {
#[doc = " Add interface to the list.\n\n @param[in] iface The interface must remain valid as long as the application is running.\n @return #CSP_ERR_NONE on success, otherwise an error code."]
pub fn csp_iflist_add(iface: *mut csp_iface_t) -> ::core::ffi::c_int;
pub fn csp_iflist_print();
}
}
pub mod udp {
use super::*;
extern "C" {
#[doc = " Setup UDP peer\n\n RX task:\n A server task will attempt at binding to ip 0.0.0.0 port 9600\n If this fails, it is because another udp server is already running.\n The server task will continue attemting the bind and will not exit before the application is closed.\n\n TX peer:\n Outgoing CSP packets will be transferred to the peer specified by the host argument"]
pub fn csp_if_udp_init(iface: *mut csp_iface_t, ifconf: *mut csp_if_udp_conf_t);
}
}
/// Hook module for CSP.
///
/// You can override these methods by providing them with an implementation block in your
/// application for library with the function signature specified in this module.
pub mod hooks {
use super::*;
extern "C" {
pub fn csp_output_hook(
idout: *mut csp_id_t,
packet: *mut csp_packet_t,
iface: *mut csp_iface_t,
via: u16,
from_me: ::core::ffi::c_int,
);
pub fn csp_input_hook(iface: *mut csp_iface_t, packet: *mut csp_packet_t);
pub fn csp_reboot_hook();
pub fn csp_shutdown_hook();
pub fn csp_memfree_hook() -> u32;
pub fn csp_ps_hook(packet: *mut csp_packet_t) -> ::core::ffi::c_uint;
#[doc = " Implement these, if you use csp_if_tun"]
pub fn csp_crypto_decrypt(
ciphertext_in: *mut u8,
ciphertext_len: u8,
msg_out: *mut u8,
) -> ::core::ffi::c_int;
pub fn csp_crypto_encrypt(
msg_begin: *mut u8,
msg_len: u8,
ciphertext_out: *mut u8,
) -> ::core::ffi::c_int;
pub fn csp_clock_get_time(time: *mut csp_timestamp_t);
pub fn csp_clock_set_time(time: *const csp_timestamp_t) -> ::core::ffi::c_int;
}
}
#[cfg(test)]
@ -900,4 +975,423 @@ mod tests {
)
);
}
#[test]
fn bindgen_test_layout_csp_if_udp_conf_t() {
const UNINIT: ::core::mem::MaybeUninit<csp_if_udp_conf_t> =
::core::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::core::mem::size_of::<csp_if_udp_conf_t>(),
48usize,
concat!("Size of: ", stringify!(csp_if_udp_conf_t))
);
assert_eq!(
::core::mem::align_of::<csp_if_udp_conf_t>(),
8usize,
concat!("Alignment of ", stringify!(csp_if_udp_conf_t))
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).host) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(host)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).lport) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(lport)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rport) as usize - ptr as usize },
12usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(rport)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).server_handle) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(server_handle)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).peer_addr) as usize - ptr as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(peer_addr)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).sockfd) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(csp_if_udp_conf_t),
"::",
stringify!(sockfd)
)
);
}
#[test]
fn bindgen_test_layout_csp_conn_s() {
const UNINIT: ::core::mem::MaybeUninit<csp_conn_s> = ::core::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::core::mem::size_of::<csp_conn_s>(),
280usize,
concat!("Size of: ", stringify!(csp_conn_s))
);
assert_eq!(
::core::mem::align_of::<csp_conn_s>(),
8usize,
concat!("Alignment of ", stringify!(csp_conn_s))
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).type_) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(type_)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).state) as usize - ptr as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(state)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).idin) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(idin)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).idout) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(idout)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).sport_outgoing) as usize - ptr as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(sport_outgoing)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue) as usize - ptr as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue_static) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue_static)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_queue_static_data) as usize - ptr as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rx_queue_static_data)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).callback) as usize - ptr as usize },
176usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(callback)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).dest_socket) as usize - ptr as usize },
184usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(dest_socket)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).timestamp) as usize - ptr as usize },
192usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(timestamp)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).opts) as usize - ptr as usize },
196usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(opts)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rdp) as usize - ptr as usize },
200usize,
concat!(
"Offset of field: ",
stringify!(csp_conn_s),
"::",
stringify!(rdp)
)
);
}
#[test]
fn bindgen_test_layout_csp_iface_s() {
const UNINIT: ::core::mem::MaybeUninit<csp_iface_s> = ::core::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::core::mem::size_of::<csp_iface_s>(),
96usize,
concat!("Size of: ", stringify!(csp_iface_s))
);
assert_eq!(
::core::mem::align_of::<csp_iface_s>(),
8usize,
concat!("Alignment of ", stringify!(csp_iface_s))
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).addr) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(addr)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).netmask) as usize - ptr as usize },
2usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(netmask)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).name) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(name)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).interface_data) as usize - ptr as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(interface_data)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).driver_data) as usize - ptr as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(driver_data)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).nexthop) as usize - ptr as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(nexthop)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).is_default) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(is_default)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).tx) as usize - ptr as usize },
44usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(tx)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx) as usize - ptr as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(rx)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).tx_error) as usize - ptr as usize },
52usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(tx_error)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rx_error) as usize - ptr as usize },
56usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(rx_error)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).drop) as usize - ptr as usize },
60usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(drop)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).autherr) as usize - ptr as usize },
64usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(autherr)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).frame) as usize - ptr as usize },
68usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(frame)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).txbytes) as usize - ptr as usize },
72usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(txbytes)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).rxbytes) as usize - ptr as usize },
76usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(rxbytes)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).irq) as usize - ptr as usize },
80usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(irq)
)
);
assert_eq!(
unsafe { ::core::ptr::addr_of!((*ptr).next) as usize - ptr as usize },
88usize,
concat!(
"Offset of field: ",
stringify!(csp_iface_s),
"::",
stringify!(next)
)
);
}
}

View File

@ -2,20 +2,10 @@ libcsp-rust
========
This crate provides a (mostly) safe and ergonomic Rust API for the
[`libcsp` library](https://github.com/libcsp/libcsp) on top of the `libcsp-sys`
crate. You can find some more high-level information and examples in the
[`libcsp` library](https://github.com/libcsp/libcsp) on top of the
[`libcsp-sys`](https://crates.io/crates/libcsp-sys) crate.
You can find some more high-level information and examples in the
[main repository](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust).
The API documentation should provide all additional information required to use this library.
## Compile-time configuration of the `libcsp-rust` library
The `libcsp-rust` library requires some compile-time configuration file to be included to work
properly. You can see an example version of the file for the workspace
[here](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/examples/autoconfig.rs).
The user has to provide the path to a directory containing this `autoconfig.rs` file using the
`CSP_CONFIG_DIR` environmental variable.
It is recommended to read the [main workspace README](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust)
for more information to make the generation and specification of this auto-configuration file
as conveniently and easy as possible.

View File

@ -1,5 +1,10 @@
//! This crate provides a (mostly) safe and ergonomic Rust API for the
//! [`libcsp` C library](https://github.com/libcsp/libcsp) on top of the
//! [`libcsp-sys`](https://crates.io/crates/libcsp-sys) crate.
//!
//! You can find some more high-level information and examples in the
//! [main repository](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust).
#![no_std]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(any(feature = "std", test))]
@ -11,7 +16,7 @@ use num_enum::{IntoPrimitive, TryFromPrimitive};
use bitflags::bitflags;
use ffi::{csp_conn_s, csp_packet_s, csp_socket_s};
use libcsp_sys as ffi;
pub use libcsp_sys as ffi;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ReservedPort {
@ -291,6 +296,18 @@ pub fn csp_route_work() -> Result<(), CspError> {
#[derive(Debug, Copy, Clone)]
pub struct CspConnRef(*mut csp_conn_s);
impl CspConnRef {
pub fn inner(&mut self) -> Option<&csp_conn_s> {
// SAFETY: Raw pointer access, we return [None] if the pointers is NULL.
unsafe { self.0.as_ref() }
}
pub fn inner_mut(&mut self) -> Option<&mut csp_conn_s> {
// SAFETY: Raw pointer access, we return [None] if the pointers is NULL.
unsafe { self.0.as_mut() }
}
}
pub struct CspConnGuard(pub CspConnRef);
impl Drop for CspConnGuard {
@ -311,6 +328,57 @@ impl AsMut<CspConnRef> for CspConnGuard {
}
}
#[derive(Default)]
pub struct CspInterface(pub ffi::csp_iface_t);
impl CspInterface {
pub fn new(host_addr: u16, is_default: bool) -> Self {
Self(ffi::csp_iface_t {
addr: host_addr,
netmask: Default::default(),
name: core::ptr::null(),
interface_data: core::ptr::null_mut(),
driver_data: core::ptr::null_mut(),
nexthop: None,
is_default: is_default as u8,
tx: Default::default(),
rx: Default::default(),
tx_error: Default::default(),
rx_error: Default::default(),
drop: Default::default(),
autherr: Default::default(),
frame: Default::default(),
txbytes: Default::default(),
rxbytes: Default::default(),
irq: Default::default(),
next: core::ptr::null_mut(),
})
}
}
#[derive(Default)]
pub struct CspUdpConf(pub ffi::csp_if_udp_conf_t);
impl CspUdpConf {
pub fn new(addr: &'static str, lport: u16, rport: u16) -> Self {
Self(ffi::csp_if_udp_conf_t {
host: addr.as_ptr() as *mut i8,
lport: lport.into(),
rport: rport.into(),
server_handle: Default::default(),
peer_addr: libc::sockaddr_in {
sin_family: Default::default(),
sin_port: Default::default(),
sin_addr: libc::in_addr {
s_addr: Default::default(),
},
sin_zero: Default::default(),
},
sockfd: Default::default(),
})
}
}
pub fn csp_accept_guarded(socket: &mut CspSocket, timeout: Duration) -> Option<CspConnGuard> {
Some(CspConnGuard(csp_accept(socket, timeout)?))
}
@ -371,6 +439,41 @@ pub fn csp_conn_dport(conn: &CspConnRef) -> i32 {
unsafe { ffi::csp_conn_dport(conn.0) }
}
/// Rust wrapper for [ffi::csp_conn_sport].
pub fn csp_conn_sport(conn: &CspConnRef) -> i32 {
// SAFETY: FFI call.
unsafe { ffi::csp_conn_sport(conn.0) }
}
/// Rust wrapper for [ffi::csp_conn_dst].
pub fn csp_conn_dst(conn: &CspConnRef) -> i32 {
// SAFETY: FFI call.
unsafe { ffi::csp_conn_dst(conn.0) }
}
/// Rust wrapper for [ffi::csp_conn_src].
pub fn csp_conn_src(conn: &CspConnRef) -> i32 {
// SAFETY: FFI call.
unsafe { ffi::csp_conn_src(conn.0) }
}
/// Rust wrapper for [ffi::csp_conn_src].
pub fn csp_conn_flags(conn: &CspConnRef) -> i32 {
// SAFETY: FFI call.
unsafe { ffi::csp_conn_flags(conn.0) }
}
/// Rust wrapper for [ffi::csp_conn_src] which also tries to convert the options to
/// a [ConnectOpts] bitfield.
pub fn csp_conn_flags_typed(conn: &CspConnRef) -> Option<ConnectOpts> {
let flags_raw = csp_conn_flags(conn);
if flags_raw < 0 {
return None;
}
// SAFETY: FFI call.
ConnectOpts::from_bits(flags_raw as u32)
}
pub fn csp_service_handler(packet: CspPacketRef) {
// SAFETY: FFI call.
unsafe { ffi::csp_service_handler(&mut *packet.0) }
@ -482,12 +585,6 @@ pub fn csp_conn_print_table() {
unsafe { ffi::csp_conn_print_table() }
}
/// Rust wrapper for [ffi::csp_iflist_print].
pub fn csp_iflist_print() {
// SAFETY: FFI call.
unsafe { ffi::csp_iflist_print() }
}
/// Rust wrapper for [ffi::csp_buffer_free].
pub fn csp_buffer_free(packet: impl Into<CspPacketRef>) {
// SAFETY: FFI call and the Rust type system actually ensure the correct type
@ -557,3 +654,49 @@ pub fn csp_transaction_w_opts(
)
}
}
/// Calls [csp_transaction_w_opts] with [ConnectOpts::NONE].
pub fn csp_transaction(
prio: MsgPriority,
dst: u16,
dst_port: u8,
timeout: Duration,
out_data: &[u8],
in_data: &mut [u8],
in_len: Option<usize>,
) -> i32 {
csp_transaction_w_opts(
prio,
dst,
dst_port,
timeout,
out_data,
in_data,
in_len,
ConnectOpts::NONE,
)
}
pub mod udp {
use super::*;
/// Rust wrapper for [ffi::udp::csp_if_udp_init].
pub fn csp_if_udp_init(iface: &mut CspInterface, ifconf: &mut CspUdpConf) {
unsafe { ffi::udp::csp_if_udp_init(&mut iface.0, &mut ifconf.0) }
}
}
pub mod iflist {
use super::*;
/// Rust wrapper for [ffi::iflist::csp_iflist_print].
pub fn csp_iflist_print() {
// SAFETY: FFI call.
unsafe { ffi::iflist::csp_iflist_print() }
}
/// Rust wrapper for [ffi::iflist::csp_iflist_add].
pub fn csp_iflist_add(iface: &mut CspInterface) -> i32 {
unsafe { ffi::iflist::csp_iflist_add(&mut iface.0) }
}
}