Initial version of libcsp-rust

This commit is contained in:
Robin Müller 2024-05-29 00:25:05 +02:00 committed by Robin Mueller
commit cdf5441c41
42 changed files with 14049 additions and 0 deletions

4
.cargo/config.toml Normal file
View File

@ -0,0 +1,4 @@
[env]
# This is set for this repository so it does not always have to be set in the command line.
# It is required for building libcsp-rust.
CSP_CONFIG_DIR = { value = "examples", relative = true }

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

172
Cargo.lock generated Normal file
View File

@ -0,0 +1,172 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "cc"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "libcsp"
version = "0.1.0"
dependencies = [
"bitflags",
"libc",
"libcsp-sys",
"num_enum",
]
[[package]]
name = "libcsp-cargo-build"
version = "0.1.0"
dependencies = [
"cc",
]
[[package]]
name = "libcsp-rust-examples"
version = "0.1.0"
dependencies = [
"libcsp",
"libcsp-cargo-build",
]
[[package]]
name = "libcsp-sys"
version = "0.1.0"
dependencies = [
"libc",
]
[[package]]
name = "memchr"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "num_enum"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02339744ee7253741199f897151b38e72257d13802d4ee837285cc2990a90845"
dependencies = [
"num_enum_derive",
]
[[package]]
name = "num_enum_derive"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "681030a937600a36906c185595136d26abfebb4aa9c65701cefcaf8578bb982b"
dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro-crate"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284"
dependencies = [
"toml_edit",
]
[[package]]
name = "proc-macro2"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toml_datetime"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf"
[[package]]
name = "toml_edit"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"
dependencies = [
"indexmap",
"toml_datetime",
"winnow",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "winnow"
version = "0.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [
"memchr",
]

5
Cargo.toml Normal file
View File

@ -0,0 +1,5 @@
[workspace]
members = [
"libcsp-cargo-build", "libcsp", "libcsp-sys", "examples"
]
resolver = "2"

201
LICENSE-APACHE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

1
NOTICE Normal file
View File

@ -0,0 +1 @@
This software contains code developed at the University of Stuttgart's Institute of Space Systems.

95
README.md Normal file
View File

@ -0,0 +1,95 @@
libcsp-rust
=========
This project aims to provide libraries and tools to build and use
the [`libcsp`](https://github.com/libcsp/libcsp) C library in your Rust project.
It provides 3 crates for this:
- [`libcsp`](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/libcsp-rust)
provides a safe and ergonomic Rust interface on top of the `libcsp-sys` crate.
- [`libcsp-sys`](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/libcsp-sys)
provides the Rust bindings to [`libcsp`](https://github.com/libcsp/libcsp).
- [`libcsp-cargo-build`](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/libcsp-cargo-build)
provides an API to build the `libcsp` using `cargo` with the [`cc`](https://docs.rs/cc/latest/cc/) crate.
In addition, it provides a workspace to allow updating the `libcsp` C sources and the corresponding
bindings more easily inside the `clib` directory. Some of the examples `libcsp` provides were ported
to Rust and are showcased in the `examples` directory.
Please note that this is early-stage/experimental software. Important features might be missing.
PRs and improvement suggestions are welcome! This project was primarily tested on a Linux/POSIX
system so far.
## How it works
We assume that cargo should also take care of building the library.
1. Add the `libcsp-cargo-build` as a build dependency inside your `Cargo.toml`.
2. Add the `libcsp` as a regular dependency inside your `Cargo.toml`.
3. Create a custom `build.rs` script which takes care of building the `libcsp` C library using the
API provided by `libcsp-cargo-build`. You have to provide the source code for `libcsp` inside
some directory and pass the directory path to the builder API.
4. You can now write regular Rust code and use the Rust API provided by the `libcsp` crate to use
the `libcsp` C library.
It is recommended to have a look at the [example build script](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust/src/branch/main/examples/build.rs)
which should give you a general idea of how a build script which does the 4 steps above might look
like.
## Running the example
The example uses both the builder crate and the bindings and API crate and implements the
[server/client example](https://github.com/libcsp/libcsp/blob/develop/examples/csp_server_client.c)
in Rust. You can run the example using the following steps:
1. Clone/Copy `libcsp` into the `lib` folder, for example by using the provided `lib/clone-csp.sh`
script or adding `libcsp` as a git submodule.
2. You can now use `cargo run -p libcsp-rust-examples` to run the server/client example.
## Compile-time configuration of the `libcsp-sys` library
The `libcsp-sys` 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.
You can automatically generate this file when using `libcsp-cargo-build` by using the
[`generate_autoconf_rust_file`](here be link soon) method of the Builder object as done in the
example build script.
In this workspace, the `CSP_CONFIG_DIR` variable is hardcoded using the following `.cargo/config.toml`
configuration:
```toml
[env]
CSP_CONFIG_DIR = { value = "examples", relative = true }
```
## Generating and update the bindings using the `clib` folder
The `lib` folder in this repository serves as the staging directory for the `libcsp` library to
build. However, it can also be used to update the bindings provided in `libcsp-sys` by providing
some tools and helpers to auto-generate and update the bindings file `bindings.rs`.
If you want to do this, you should install `bindgen-cli` first:
```sh
cargo install bindgen-cli --locked
```
`bindgen` needs some additional information provided by the user to generate the bindings:
An `autoconfig.h` file which is used to configure `libcsp`. Normally, this file is generated
by the C build system. This file is located at `clib/cfg/csp` and is also updated automatically
when running the example application.
After cloning the repository, you can now run the following command to re-generate the bindings
file:
```sh
bindgen --use-core wrapper.h -- "-I./libcsp/include" "-I./cfg" "-I./libcsp/src" > bindings.rs
```
With the bindings file, you can now manually update the FFI bindings provided in
`libcsp-sys/src/lib.rs` or in your own CSP library.

1
clib/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/libcsp

7538
clib/bindings.rs Normal file

File diff suppressed because it is too large Load Diff

2418
clib/bindings.sh Normal file

File diff suppressed because it is too large Load Diff

25
clib/cfg/csp/autoconfig.h Normal file
View File

@ -0,0 +1,25 @@
// This file was auto-generated by libcsp-cargo-build v0.1.0
#define CSP_POSIX 1
#define CSP_ZEPHYR 0
#define CSP_HAVE_STDIO 1
#define CSP_ENABLE_CSP_PRINT 1
#define CSP_PRINT_STDIO 1
#define CSP_REPRODUCIBLE_BUILDS 0
#define CSP_QFIFO_LEN 16
#define CSP_PORT_MAX_BIND 16
#define CSP_CONN_RXQUEUE_LEN 16
#define CSP_CONN_MAX 8
#define CSP_BUFFER_SIZE 256
#define CSP_BUFFER_COUNT 15
#define CSP_RDP_MAX_WINDOW 5
#define CSP_RTABLE_SIZE 10
#define CSP_USE_RDP 1
#define CSP_USE_HMAC 1
#define CSP_USE_PROMISC 1
#define CSP_USE_RTABLE 0
#define CSP_HAVE_LIBSOCKETCAN 0
#define CSP_HAVE_LIBZMQ 0

3
clib/clone-csp.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
git clone https://github.com/us-irs/libcsp.git
git checkout const-correctness

4
clib/gen-bindings.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
bindgen_cmd="bindgen --use-core wrapper.h -- '-I./libcsp/include' '-I./cfg' '-I./libcsp/src' > bindings.rs"
echo "Running: $bindgen_cmd"
eval $bindgen_cmd

6
clib/wrapper.h Normal file
View File

@ -0,0 +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"

1
examples/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

10
examples/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "libcsp-rust-examples"
version = "0.1.0"
edition = "2021"
[dependencies]
libcsp = { path = "../libcsp" }
[build-dependencies]
libcsp-cargo-build = { path = "../libcsp-cargo-build" }

25
examples/autoconfig.h Normal file
View File

@ -0,0 +1,25 @@
// This file was auto-generated by libcsp-cargo-build v0.1.0
#define CSP_POSIX 1
#define CSP_ZEPHYR 0
#define CSP_HAVE_STDIO 1
#define CSP_ENABLE_CSP_PRINT 1
#define CSP_PRINT_STDIO 1
#define CSP_REPRODUCIBLE_BUILDS 0
#define CSP_QFIFO_LEN 16
#define CSP_PORT_MAX_BIND 16
#define CSP_CONN_RXQUEUE_LEN 16
#define CSP_CONN_MAX 8
#define CSP_BUFFER_SIZE 256
#define CSP_BUFFER_COUNT 15
#define CSP_RDP_MAX_WINDOW 5
#define CSP_RTABLE_SIZE 10
#define CSP_USE_RDP 1
#define CSP_USE_HMAC 1
#define CSP_USE_PROMISC 1
#define CSP_USE_RTABLE 0
#define CSP_HAVE_LIBSOCKETCAN 0
#define CSP_HAVE_LIBZMQ 0

8
examples/autoconfig.rs Normal file
View File

@ -0,0 +1,8 @@
// 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;
pub const CSP_CONN_MAX: usize = 8;
pub const CSP_BUFFER_SIZE: usize = 256;
pub const CSP_RDP_MAX_WINDOW: usize = 5;
pub const CSP_RTABLE_SIZE: usize = 10;

38
examples/build.rs Normal file
View File

@ -0,0 +1,38 @@
use std::{env, path::PathBuf};
use libcsp_cargo_build::{generate_autoconf_header_file, Builder};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap_or_default();
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let manifest_path = PathBuf::from(&manifest_dir);
let lib_cfg_dir = "../clib/cfg/csp";
let libcsp_path = "../clib/libcsp";
// This helper structure will take care of the majority of work to compile libcsp using the
// cc crate.
let mut csp_builder = Builder::new(PathBuf::from(libcsp_path), PathBuf::from(&out_dir));
// A lot of spam we are not interested in usually.
csp_builder.compiler_warnings = false;
// We always re-generate the header file.
generate_autoconf_header_file(manifest_path.clone(), &csp_builder.cfg)
.expect("generating header file failed");
// Copy the file to lib/csp/cfg as well for binding generation.
std::fs::copy(
manifest_path.join("autoconfig.h"),
PathBuf::from(&lib_cfg_dir).join("autoconfig.h"),
)
.expect("copying autoconfig.h failed");
// This file is required for the compile-time configuration of libcsp-rust.
csp_builder
.generate_autoconf_rust_file(manifest_path)
.expect("generating autoconfig.rs failed");
csp_builder.compile().expect("compiling libcsp failed");
// If we change the libcsp build configuration, we need to re-run the build.
println!("cargo::rerun-if-changed=build.rs");
}

205
examples/src/main.rs Normal file
View File

@ -0,0 +1,205 @@
use std::{
ffi::CStr,
sync::{
atomic::{AtomicBool, AtomicU32},
Arc,
},
thread,
time::Duration,
};
use libcsp::{
csp_accept_guarded, csp_bind, csp_buffer_get, csp_conn_dport, csp_conn_print_table,
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;
const TEST_MODE: bool = false;
const RUN_DURATION_IN_SECS: u32 = 3;
fn main() -> Result<(), u32> {
println!("CSP client/server example");
// SAFETY: We only call this once.
unsafe { csp_init() };
let stop_signal = Arc::new(AtomicBool::new(false));
let stop_signal_server = stop_signal.clone();
let stop_signal_client = stop_signal.clone();
let stop_signal_router = stop_signal.clone();
let server_received = Arc::new(AtomicU32::new(0));
let server_recv_copy = server_received.clone();
let csp_router_jh = thread::spawn(move || loop {
if stop_signal_router.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
if let Err(e) = csp_route_work() {
match e {
CspError::TimedOut => continue,
e => {
println!("CSP router error: {:?}", e);
break;
}
}
}
});
let csp_server_jh = thread::spawn(move || {
server(server_received, stop_signal_server);
});
let csp_client_jh = thread::spawn(move || {
client(stop_signal_client);
});
println!("CSP connection table");
csp_conn_print_table();
println!("CSP interfaces");
csp_iflist_print();
let mut app_result = Ok(());
// Wait for execution to end (ctrl+c)
loop {
std::thread::sleep(Duration::from_secs(RUN_DURATION_IN_SECS as u64));
if TEST_MODE {
// Test mode is intended for checking that host & client can exchange packets over loopback
let received_count = server_recv_copy.load(std::sync::atomic::Ordering::Relaxed);
println!("CSP: Server received {} packets", received_count);
if received_count < 5 {
app_result = Err(1);
}
stop_signal.store(true, std::sync::atomic::Ordering::Relaxed);
break;
}
}
csp_router_jh.join().unwrap();
csp_server_jh.join().unwrap();
csp_client_jh.join().unwrap();
app_result
}
fn server(server_received: Arc<AtomicU32>, stop_signal: Arc<AtomicBool>) {
println!("server task started");
// Create socket with no specific socket options, e.g. accepts CRC32, HMAC, etc. if enabled
// during compilation
let mut csp_socket = CspSocket::default();
// Bind socket to all ports, e.g. all incoming connections will be handled here
csp_bind(&mut csp_socket, CSP_ANY);
// Create a backlog of 10 connections, i.e. up to 10 new connections can be queued
csp_listen(&mut csp_socket, 10);
// Wait for connections and then process packets on the connection
loop {
if stop_signal.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
// Wait for a new connection, 10000 mS timeout
let conn = csp_accept_guarded(&mut csp_socket, Duration::from_millis(10000));
if conn.is_none() {
continue;
}
let mut conn = conn.unwrap();
// Read packets on connection, timout is 100 mS
loop {
if stop_signal.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
// Guarded packet is cleaned up automatically.
let packet = csp_read_guarded(&mut conn.0, Duration::from_millis(100));
if packet.is_none() {
break;
}
let packet = packet.unwrap();
match csp_conn_dport(&conn.0) {
MY_SERVER_PORT => {
server_received.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let cstr = CStr::from_bytes_with_nul(packet.as_ref().packet_data())
.expect("invalid packet data format, is not C string");
// Process packet here.
println!("packet received on MY_SERVER_PORT: {:?}", cstr);
}
_ => {
csp_service_handler(packet.take());
}
};
}
// No need to close, we accepted the connection with a guard.
}
}
fn client(stop_signal: Arc<AtomicBool>) {
println!("client task started");
let mut current_letter = 'A';
loop {
if stop_signal.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
if TEST_MODE {
thread::sleep(Duration::from_millis(20));
} else {
thread::sleep(Duration::from_millis(100));
}
// Send ping to server, timeout 1000 mS, ping size 20 bytes
if let Err(e) = csp_ping(
CSP_LOOPBACK,
Duration::from_millis(1000),
20,
SocketFlags::NONE,
) {
println!("ping error: {:?}", e);
}
// Send reboot request to server, the server has no actual implementation of
// csp_sys_reboot() and fails to reboot.
csp_reboot(CSP_LOOPBACK);
// Send data packet (string) to server
// 1. Connect to host on 'server_address', port MY_SERVER_PORT with regular UDP-like
// protocol and 1000 ms timeout.
let conn = csp_connect_guarded(
MsgPriority::Normal,
CSP_LOOPBACK,
MY_SERVER_PORT as u8,
Duration::from_millis(1000),
ConnectOpts::NONE,
);
if conn.is_none() {
println!("CSP client: connection failed");
return;
}
let mut conn = conn.unwrap();
// 2. Get packet buffer for message/data.
let packet_ref = csp_buffer_get();
if packet_ref.is_none() {
println!("CSP client: failed to get CSP buffer");
return;
}
let mut packet_mut = packet_ref.unwrap();
// 3. Copy data to packet.
let mut string_to_set = String::from("Hello world");
string_to_set.push(' ');
string_to_set.push(current_letter);
current_letter = (current_letter as u8 + 1) as char;
string_to_set.push('\0');
packet_mut.set_data(string_to_set.as_bytes());
// 4. Send data.
csp_send(&mut conn.0, packet_mut);
}
}

2
libcsp-cargo-build/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

View File

@ -0,0 +1,13 @@
Change Log
=======
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
# [v0.1.0] 2024-06-01
Initial release

View File

@ -0,0 +1,14 @@
[package]
name = "libcsp-cargo-build"
version = "0.1.0"
edition = "2021"
description = "Tools to build libcsp using cargo"
homepage = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
repository = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
license = "Apache-2.0"
keywords = ["no-std", "space", "aerospace", "ffi", "csp"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
categories = ["aerospace", "external-ffi-bindings", "no-std", "hardware-support", "embedded"]
[dependencies]
cc = "1"

View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1 @@
This software contains code developed at the University of Stuttgart's Institute of Space Systems.

View File

@ -0,0 +1,9 @@
libcsp-cargo-build
========
This crate provides a library to allow building the [`libcsp`](https://github.com/libcsp/libcsp)
with cargo. You can find some more high-level information and examples in the
[main workspace](https://egit.irs.uni-stuttgart.de/rust/libcsp-rust).
The API documentation should provide all additional advanced information you might require to tweak
the `libcsp` build.

View File

@ -0,0 +1,409 @@
//! 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},
};
pub mod cfg_keys {
pub const POSIX: &str = "CSP_POSIX";
pub const ZEPHYR: &str = "CSP_ZEPHYR";
pub const HAVE_STDIO: &str = "CSP_HAVE_STDIO";
pub const ENABLE_CSP_PRINT: &str = "CSP_ENABLE_CSP_PRINT";
pub const PRINT_STDIO: &str = "CSP_PRINT_STDIO";
pub const REPRODUCIBLE_BUILDS: &str = "CSP_REPRODUCIBLE_BUILDS";
pub const QFIFO_LEN: &str = "CSP_QFIFO_LEN";
pub const PORT_MAX_BIND: &str = "CSP_PORT_MAX_BIND";
pub const CONN_RXQUEUE_LEN: &str = "CSP_CONN_RXQUEUE_LEN";
pub const CONN_MAX: &str = "CSP_CONN_MAX";
pub const BUFFER_SIZE: &str = "CSP_BUFFER_SIZE";
pub const BUFFER_COUNT: &str = "CSP_BUFFER_COUNT";
pub const RDP_MAX_WINDOW: &str = "CSP_RDP_MAX_WINDOW";
pub const RTABLE_SIZE: &str = "CSP_RTABLE_SIZE";
pub const USE_RDP: &str = "CSP_USE_RDP";
pub const USE_HMAC: &str = "CSP_USE_HMAC";
pub const USE_PROMISC: &str = "CSP_USE_PROMISC";
pub const USE_RTABLE: &str = "CSP_USE_RTABLE";
pub const HAVE_LIBSOCKETCAN: &str = "CSP_HAVE_LIBSOCKETCAN";
pub const HAVE_LIBZMQ: &str = "CSP_HAVE_LIBZMQ";
}
const SRCS_LIST: &[&str] = &[
"csp_bridge.c",
"csp_buffer.c",
"csp_crc32.c",
"csp_debug.c",
"csp_id.c",
"csp_iflist.c",
"csp_conn.c",
"csp_init.c",
"csp_io.c",
"csp_port.c",
"csp_promisc.c",
"csp_qfifo.c",
"csp_port.c",
"csp_route.c",
"csp_dedup.c",
"csp_services.c",
"csp_service_handler.c",
"interfaces/csp_if_lo.c",
"interfaces/csp_if_kiss.c",
"interfaces/csp_if_tun.c",
"interfaces/csp_if_udp.c",
"crypto/csp_hmac.c",
"crypto/csp_sha1.c",
];
const ARCH_SRCS_UNIX: &[&str] = &[
"arch/posix/csp_clock.c",
"arch/posix/csp_semaphore.c",
"arch/posix/csp_system.c",
"arch/posix/csp_time.c",
"arch/posix/csp_queue.c",
"arch/posix/pthread_queue.c",
];
pub struct Config {
pub have_stdio: bool,
pub print_stdio: bool,
pub reproducible_builds: bool,
pub qfifo_len: u32,
pub port_max_bind: u32,
pub conn_rx_queue_len: u32,
pub conn_max: u32,
pub buffer_size: u32,
pub buffer_count: u32,
pub rdp_max_window: u32,
pub rtable_size: u32,
pub hmac: bool,
pub rtable: bool,
pub csp_print: bool,
pub promisc: bool,
pub rdp: bool,
pub yaml: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
have_stdio: true,
print_stdio: true,
reproducible_builds: false,
qfifo_len: 16,
port_max_bind: 16,
conn_rx_queue_len: 16,
conn_max: 8,
buffer_size: 256,
buffer_count: 15,
rdp_max_window: 5,
rtable_size: 10,
hmac: true,
rtable: false,
csp_print: true,
promisc: true,
rdp: true,
yaml: false,
}
}
}
/// 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,
libcsp_src_path_base: PathBuf,
out_dir: PathBuf,
pub cfg: Config,
pub compiler_warnings: bool,
build: cc::Build,
}
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");
Self {
generate_autoconf_file: true,
libcsp_path,
libcsp_src_path_base,
out_dir,
cfg: Default::default(),
compiler_warnings: true,
build: Default::default(),
}
}
/// 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
}
pub fn compile(&mut self) -> io::Result<()> {
if self.generate_autoconf_file {
self.generate_autoconf_header_file_default_location()?;
}
for src in SRCS_LIST {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push(src);
self.build.file(next_file);
}
if self.cfg.rdp {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_rdp.c");
self.build.file(next_file);
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_rdp_queue.c");
self.build.file(next_file);
}
if self.cfg.promisc {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_promisc.c");
self.build.file(next_file);
}
if self.cfg.csp_print {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_hex_dump.c");
self.build.file(next_file);
}
if self.cfg.yaml {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_yaml.c");
self.build.file(next_file);
}
if self.cfg.rtable {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push("csp_rtable_cidr.c");
self.build.file(next_file);
}
// TODO: UNIX does not necesarilly mean POSIX? Details to deal with later..
#[cfg(unix)]
self.posix_arch_files();
let mut inc_path = self.libcsp_path.clone();
inc_path.push("include");
self.build.include(inc_path);
self.build.include(&self.libcsp_src_path_base);
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(())
}
#[cfg(unix)]
fn posix_arch_files(&mut self) {
for src in ARCH_SRCS_UNIX {
let mut next_file = self.libcsp_src_path_base.clone();
next_file.push(src);
self.build.file(next_file);
}
}
pub fn generate_autoconf_header_file_default_location(&mut self) -> io::Result<()> {
let mut autoconf_dir = self.out_dir.join("cfg");
self.build.include(&autoconf_dir);
autoconf_dir.push("csp");
std::fs::create_dir_all(&autoconf_dir)?;
generate_autoconf_header_file(&autoconf_dir, &self.cfg)
}
pub fn generate_autoconf_header_file(&mut self, dir: impl AsRef<Path>) -> io::Result<()> {
generate_autoconf_header_file(dir, &self.cfg)
}
pub fn generate_autoconf_rust_file(&self, dir: impl AsRef<Path>) -> io::Result<()> {
generate_autoconf_rust_file(dir, &self.cfg)
}
}
/// 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();
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!(
"#define {} {}\n",
cfg_keys::HAVE_STDIO,
cfg.have_stdio as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::ENABLE_CSP_PRINT,
cfg.csp_print as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::PRINT_STDIO,
cfg.print_stdio as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::REPRODUCIBLE_BUILDS,
cfg.reproducible_builds as u32
));
autoconf_file_string.push('\n');
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::QFIFO_LEN,
cfg.qfifo_len
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::PORT_MAX_BIND,
cfg.port_max_bind
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::CONN_RXQUEUE_LEN,
cfg.conn_rx_queue_len
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::CONN_MAX,
cfg.conn_max
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::BUFFER_SIZE,
cfg.buffer_size
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::BUFFER_COUNT,
cfg.buffer_count
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::RDP_MAX_WINDOW,
cfg.rdp_max_window
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::RTABLE_SIZE,
cfg.rtable_size
));
autoconf_file_string.push('\n');
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::USE_RDP,
cfg.rdp as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::USE_HMAC,
cfg.hmac as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::USE_PROMISC,
cfg.promisc as u32
));
autoconf_file_string.push_str(&format!(
"#define {} {}\n",
cfg_keys::USE_RTABLE,
cfg.rtable as u32
));
autoconf_file_string.push('\n');
// TODO: Maybe those will be added at some point.. For now, they are hardcoded to 0.
autoconf_file_string.push_str(&format!("#define {} {}\n", cfg_keys::HAVE_LIBSOCKETCAN, 0));
autoconf_file_string.push_str(&format!("#define {} {}\n", cfg_keys::HAVE_LIBZMQ, 0));
let out_file = out_dir.join("autoconfig.h");
let mut file = std::fs::File::create(out_file)?;
file.write_all(autoconf_file_string.as_bytes())?;
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();
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,
cfg.conn_rx_queue_len
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::QFIFO_LEN,
cfg.qfifo_len
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::PORT_MAX_BIND,
cfg.port_max_bind
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::CONN_MAX,
cfg.conn_max
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::BUFFER_SIZE,
cfg.buffer_size
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::RDP_MAX_WINDOW,
cfg.rdp_max_window
));
autoconf_file_string.push_str(&format!(
"pub const {}: usize = {};\n",
cfg_keys::RTABLE_SIZE,
cfg.rtable_size
));
let out_file = out_dir.join("autoconfig.rs");
let mut file = std::fs::File::create(out_file)?;
file.write_all(autoconf_file_string.as_bytes())?;
Ok(())
}
#[cfg(test)]
mod tests {
// TODO: Unittest autoconf generators.
}

13
libcsp-sys/CHANGELOG.md Normal file
View File

@ -0,0 +1,13 @@
Change Log
=======
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
# [v0.1.0] 2024-06-01
Initial release

16
libcsp-sys/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "libcsp-sys"
version = "0.1.0"
edition = "2021"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
description = "FFI bindings for libcsp"
homepage = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
repository = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
license = "Apache-2.0"
keywords = ["no-std", "space", "aerospace", "ffi", "csp"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
categories = ["aerospace", "external-ffi-bindings", "no-std", "hardware-support", "embedded"]
links = "csp"
[dependencies]
libc = "0.2"

201
libcsp-sys/LICENSE-APACHE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

1
libcsp-sys/NOTICE Normal file
View File

@ -0,0 +1 @@
This software contains code developed at the University of Stuttgart's Institute of Space Systems.

21
libcsp-sys/README.md Normal file
View File

@ -0,0 +1,21 @@
libcsp-sys
========
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` 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 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.

31
libcsp-sys/build.rs Normal file
View File

@ -0,0 +1,31 @@
use std::{env, path::PathBuf};
pub const ENV_KEY_CSP_CONFIG_DIR: &str = "CSP_CONFIG_DIR";
fn main() {
println!("cargo:rustc-link-lib=csp");
let out_path = env::var("OUT_DIR").unwrap();
let csp_conf_dir = match env::var(ENV_KEY_CSP_CONFIG_DIR) {
Ok(conf_path) => conf_path,
Err(_e) => {
println!(
"cargo:warning={} not set, using CARGO_MANIFEST_DIR to search for autoconfig.rs",
ENV_KEY_CSP_CONFIG_DIR
);
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")
}
};
let mut csp_conf_path = PathBuf::new();
csp_conf_path.push(csp_conf_dir);
csp_conf_path.push("autoconfig.rs");
if !csp_conf_path.exists() {
panic!(
"autoconfig.rs not found at {:?}, is required for library build",
csp_conf_path
);
}
let out_path_full = PathBuf::from(&out_path).join("autoconfig.rs");
std::fs::copy(&csp_conf_path, out_path_full).expect("failed to copy autoconfig.rs to OUT_DIR");
println!("cargo::rerun-if-changed={:?}", &csp_conf_path);
}

3
libcsp-sys/src/config.rs Normal file
View File

@ -0,0 +1,3 @@
/// This environbment variable should be set by the user. If it is not set, the library will
/// try to find an autoconfig.rs file inside the CARGO_TARGET_DIR directory.
pub const ENV_KEY_CSP_CONFIG_DIR: &str = "CSP_CONFIG_DIR";

1399
libcsp-sys/src/lib.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
pub const CSP_CONN_RXQUEUE_LEN: usize = 16;
pub const CSP_QFIFO_LEN: usize = 15;
pub const CSP_PORT_MAX_BIND: usize = 16;
pub const CSP_CONN_MAX: usize = 8;
pub const CSP_BUFFER_SIZE: usize = 256;
pub const CSP_BUFFER_COUNT: usize = 15;
pub const CSP_RDP_MAX_WINDOW: usize = 5;
pub const CSP_RTABLE_SIZE: usize = 10;

13
libcsp/CHANGELOG.md Normal file
View File

@ -0,0 +1,13 @@
Change Log
=======
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
# [v0.1.0] 2024-06-01
Initial release

18
libcsp/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "libcsp"
version = "0.1.0"
edition = "2021"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
description = "Safe and ergonomic Rust API for libcsp on top on libcsp-sys"
homepage = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
repository = "https://egit.irs.uni-stuttgart.de/rust/libcsp-rust"
license = "Apache-2.0"
keywords = ["no-std", "space", "aerospace", "ffi", "csp"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
categories = ["aerospace", "external-ffi-bindings", "no-std", "hardware-support", "embedded"]
[dependencies]
bitflags = "2"
num_enum = "0.7"
libc = "0.2"
libcsp-sys = { path = "../libcsp-sys" }

201
libcsp/LICENSE-APACHE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

1
libcsp/NOTICE Normal file
View File

@ -0,0 +1 @@
This software contains code developed at the University of Stuttgart's Institute of Space Systems.

11
libcsp/README.md Normal file
View File

@ -0,0 +1,11 @@
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`](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.

702
libcsp/src/lib.rs Normal file
View File

@ -0,0 +1,702 @@
//! 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))]
extern crate std;
use core::time::Duration;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use bitflags::bitflags;
use ffi::{csp_conn_s, csp_packet_s, csp_socket_s};
pub use libcsp_sys as ffi;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ReservedPort {
Cmp = 0,
Ping = 1,
Ps = 2,
Memfree = 3,
Reboot = 4,
BufFree = 5,
Uptime = 6,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(i32)]
pub enum CspError {
None = 0,
NoMem = -1,
Inval = -2,
TimedOut = -3,
Used = -4,
NotSup = -5,
Busy = -6,
Already = -7,
Reset = -8,
NoBufs = -9,
Tx = -10,
Driver = -11,
Again = -12,
NoSys = -38,
Hmac = -100,
Crc32 = -102,
Sfp = -103,
}
/// Listen on all ports, primarily used with [csp_bind]
pub const CSP_ANY: u8 = 255;
pub const CSP_LOOPBACK: u16 = 0;
bitflags! {
pub struct SocketFlags: u32 {
const NONE = 0x0000;
/// RDP required.
const RDPREQ = 0x0001;
/// RDP prohibited.
const RDPPROHIB = 0x0002;
/// HMAC required
const HMACREQ = 0x0004;
/// HMAC prohibited.
const HMACPROHIB = 0x0008;
/// CRC32 required.
const CRC32REQ = 0x0040;
const CRC32PROHIB = 0x0080;
const CONN_LESS = 0x0100;
/// Copy opts from incoming packets. Only applies to [csp_sendto_reply]
const SAME = 0x8000;
// The source may set any bits
const _ = !0;
}
}
bitflags! {
pub struct ConnectOpts: u32 {
const NONE = SocketFlags::NONE.bits();
const RDP = SocketFlags::RDPREQ.bits();
const NORDP = SocketFlags::RDPPROHIB.bits();
const HMAC = SocketFlags::HMACREQ.bits();
const NOHMAC = SocketFlags::HMACPROHIB.bits();
const CRC32 = SocketFlags::CRC32REQ.bits();
const NOCRC32 = SocketFlags::CRC32PROHIB.bits();
const SAME = SocketFlags::SAME.bits();
// The source may set any bits
const _ = !0;
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum ConnState {
Closed = 0,
Open = 1,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum ConnType {
Client = 0,
Server = 1,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum RdpState {
Closed = 0,
SynSent = 1,
SynRcvd = 2,
Open = 3,
CloseWait = 4,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum MsgPriority {
Critical = 0,
High = 1,
Normal = 2,
Low = 3,
}
pub struct CspPacket(pub csp_packet_s);
pub struct CspPacketRef(*mut csp_packet_s);
pub struct CspPacketMut(*mut csp_packet_s);
impl From<CspPacketMut> for CspPacketRef {
fn from(value: CspPacketMut) -> Self {
Self(value.0)
}
}
impl CspPacketRef {
pub fn packet_data(&self) -> &[u8] {
unsafe { &(*self.0).packet_data_union.data[..self.packet_length()] }
}
pub fn whole_data(&self) -> &[u8; ffi::CSP_BUFFER_SIZE] {
unsafe { &(*self.0).packet_data_union.data }
}
pub fn packet_length(&self) -> usize {
unsafe { (*self.0).length.into() }
}
pub fn inner(&self) -> *const csp_packet_s {
self.0
}
}
pub struct CspPacketRefGuard(Option<CspPacketRef>);
impl Drop for CspPacketRefGuard {
fn drop(&mut self) {
if let Some(packet) = self.0.take() {
csp_buffer_free(packet)
}
}
}
impl CspPacketRefGuard {
/// Take the packet out of the guard, preventing it from being freed.
pub fn take(mut self) -> CspPacketRef {
self.0.take().unwrap()
}
}
impl AsRef<CspPacketRef> for CspPacketRefGuard {
fn as_ref(&self) -> &CspPacketRef {
self.0.as_ref().unwrap()
}
}
impl CspPacketMut {
pub fn packet_data(&self) -> &[u8] {
unsafe { &(*self.0).packet_data_union.data[..self.packet_length()] }
}
pub fn whole_data(&self) -> &[u8; ffi::CSP_BUFFER_SIZE] {
unsafe { &(*self.0).packet_data_union.data }
}
pub fn packet_length(&self) -> usize {
unsafe { (*self.0).length.into() }
}
pub fn inner(&self) -> *const csp_packet_s {
self.0
}
pub fn whole_data_mut(&mut self) -> &mut [u8; ffi::CSP_BUFFER_SIZE] {
unsafe { &mut (*self.0).packet_data_union.data }
}
pub fn set_data(&mut self, data: &[u8]) -> bool {
if data.len() > self.whole_data().len() {
return false;
}
self.whole_data_mut()[0..data.len()].copy_from_slice(data);
unsafe {
(*self.0).length = data.len() as u16;
}
true
}
pub fn inner_mut(&self) -> *mut csp_packet_s {
self.0
}
}
impl CspPacket {
pub fn new() -> Self {
Self::default()
}
}
impl Default for CspPacket {
fn default() -> Self {
Self(csp_packet_s {
packet_info: Default::default(),
length: Default::default(),
id: Default::default(),
next: core::ptr::null_mut(),
header: Default::default(),
packet_data_union: Default::default(),
})
}
}
#[derive(Default)]
pub struct CspSocket(pub csp_socket_s);
impl CspSocket {
pub fn inner_as_mut_ptr(&mut self) -> *mut csp_socket_s {
&mut self.0
}
}
/// Rust wrapper for [ffi::csp_init]. Initialize the CSP stack.
///
/// # Safety
///
/// - You must call this function only once.
pub unsafe fn csp_init() {
// SAFETY: FFI call
unsafe {
ffi::csp_init();
}
}
/// Rust wrapper for [ffi::csp_bind].
pub fn csp_bind(socket: &mut CspSocket, port: u8) {
// SAFETY: FFI call
unsafe {
ffi::csp_bind(socket.inner_as_mut_ptr(), port);
}
}
/// Rust wrapper for [ffi::csp_listen].
pub fn csp_listen(socket: &mut CspSocket, backlog: usize) {
// SAFETY: FFI call
unsafe {
ffi::csp_listen(socket.inner_as_mut_ptr(), backlog);
}
}
/// Rust wrapper for [ffi::csp_route_work].
pub fn csp_route_work_raw() -> i32 {
unsafe { ffi::csp_route_work() }
}
/// Rust wrapper for [ffi::csp_route_work] which also converts errors to the [CspError] type.
/// This function will panic if the returned error type is not among the known values of
/// [CspError].
///
/// [csp_route_work_raw] can be used if this is not acceptable.
pub fn csp_route_work() -> Result<(), CspError> {
let result = unsafe { ffi::csp_route_work() };
if result == CspError::None as i32 {
return Ok(());
}
Err(CspError::try_from(result)
.unwrap_or_else(|_| panic!("unexpected error value {} from csp_route_work", result)))
}
#[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 {
fn drop(&mut self) {
csp_close(self.0);
}
}
impl AsRef<CspConnRef> for CspConnGuard {
fn as_ref(&self) -> &CspConnRef {
&self.0
}
}
impl AsMut<CspConnRef> for CspConnGuard {
fn as_mut(&mut self) -> &mut CspConnRef {
&mut self.0
}
}
#[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)?))
}
/// Rust wrapper for [ffi::csp_accept].
pub fn csp_accept(socket: &mut CspSocket, timeout: Duration) -> Option<CspConnRef> {
let timeout_millis = timeout.as_millis();
if timeout_millis > u32::MAX as u128 {
return None;
}
Some(CspConnRef(unsafe {
let addr = ffi::csp_accept(socket.inner_as_mut_ptr(), timeout_millis as u32);
if addr.is_null() {
return None;
}
addr
}))
}
/// Rust wrapper for [ffi::csp_read].
pub fn csp_read(conn: &mut CspConnRef, timeout: Duration) -> Option<CspPacketRef> {
let timeout_millis = timeout.as_millis();
if timeout_millis > u32::MAX as u128 {
return None;
}
let opt_packet = unsafe { ffi::csp_read(conn.0, timeout_millis as u32) };
if opt_packet.is_null() {
return None;
}
// SAFETY: FFI pointer.
Some(CspPacketRef(unsafe { &mut *opt_packet }))
}
/// Rust wrapper for [ffi::csp_read] which returns a guarded packet reference. This packet
/// will cleaned up automatically with [csp_buffer_free] on drop.
pub fn csp_read_guarded(conn: &mut CspConnRef, timeout: Duration) -> Option<CspPacketRefGuard> {
Some(CspPacketRefGuard(Some(csp_read(conn, timeout)?)))
}
/// Rust wrapper for [ffi::csp_recvfrom].
pub fn csp_recvfrom(socket: &mut CspSocket, timeout: u32) -> Option<CspPacketRef> {
let opt_packet = unsafe { ffi::csp_recvfrom(&mut socket.0, timeout) };
if opt_packet.is_null() {
return None;
}
Some(CspPacketRef(unsafe { &mut *opt_packet }))
}
/// Rust wrapper for [ffi::csp_recvfrom] which returns a guarded packet reference. This packet
/// will cleaned up automatically with [csp_buffer_free] on drop.
pub fn csp_recvfrom_guarded(socket: &mut CspSocket, timeout: u32) -> Option<CspPacketRefGuard> {
Some(CspPacketRefGuard(Some(csp_recvfrom(socket, timeout)?)))
}
/// Rust wrapper for [ffi::csp_conn_dport].
pub fn csp_conn_dport(conn: &CspConnRef) -> i32 {
// SAFETY: FFI call.
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) }
}
/// Rust wrapper for [ffi::csp_close].
pub fn csp_close(conn: CspConnRef) -> i32 {
// SAFETY: FFI call.
unsafe { ffi::csp_close(conn.0) }
}
/// Rust wrapper for [ffi::csp_ping], returns the result code directly.
pub fn csp_ping_raw(node: u16, timeout: Duration, size: usize, opts: SocketFlags) -> i32 {
// SAFETY: FFI call.
unsafe {
ffi::csp_ping(
node,
timeout.as_millis() as u32,
size as u32,
opts.bits() as u8,
)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PingError;
/// Rust wrapper for [ffi::csp_ping].
pub fn csp_ping(
node: u16,
timeout: Duration,
size: usize,
opts: SocketFlags,
) -> Result<Duration, PingError> {
let result = csp_ping_raw(node, timeout, size, opts);
if result < 0 {
return Err(PingError);
}
Ok(Duration::from_millis(result as u64))
}
/// Rust wrapper for [ffi::csp_reboot].
pub fn csp_reboot(node: u16) {
// SAFETY: FFI call.
unsafe { ffi::csp_reboot(node) }
}
/// Rust wrapper for [ffi::csp_connect].
pub fn csp_connect(
prio: MsgPriority,
dst: u16,
dst_port: u8,
timeout: Duration,
opts: ConnectOpts,
) -> Option<CspConnRef> {
// SAFETY: FFI call.
let conn = unsafe {
ffi::csp_connect(
prio as u8,
dst,
dst_port,
timeout.as_millis() as u32,
opts.bits(),
)
};
if conn.is_null() {
return None;
}
// SAFETY: We checked that the pointer is valid.
Some(CspConnRef(conn))
}
/// Rust wrapper for [ffi::csp_connect] which returns a guard structure. The connection will be
/// be closed automatically when the guard structure is dropped.
pub fn csp_connect_guarded(
prio: MsgPriority,
dst: u16,
dst_port: u8,
timeout: Duration,
opts: ConnectOpts,
) -> Option<CspConnGuard> {
Some(CspConnGuard(csp_connect(
prio, dst, dst_port, timeout, opts,
)?))
}
/// Rust wrapper for [ffi::csp_buffer_get].
pub fn csp_buffer_get() -> Option<CspPacketMut> {
let packet_ref = unsafe {
// The size argument is unused
ffi::csp_buffer_get(0)
};
if packet_ref.is_null() {
return None;
}
// SAFETY: We checked that the pointer is valid.
Some(CspPacketMut(unsafe { &mut *packet_ref }))
}
/// Rust wrapper for [ffi::csp_send].
pub fn csp_send(conn: &mut CspConnRef, packet: impl Into<CspPacketRef>) {
// SAFETY: FFI call.
unsafe { ffi::csp_send(conn.0, packet.into().0) }
}
/// Rust wrapper for [ffi::csp_conn_print_table].
pub fn csp_conn_print_table() {
// SAFETY: FFI call.
unsafe { ffi::csp_conn_print_table() }
}
/// 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
// is free'd here, while also taking the packet by value.
unsafe { ffi::csp_buffer_free(packet.into().0 as *mut libc::c_void) }
}
/// Rust wrapper for [ffi::csp_transaction_persistent].
///
/// # Parameters
///
/// * `in_len`: Use [None] if the length is unknown, and the expected reply length otherwise.
///
/// # Returns
///
/// 1 or reply size on success, 0 otherwise.
pub fn csp_transaction_persistent(
conn: &mut CspConnRef,
timeout: Duration,
out_data: &[u8],
in_data: &mut [u8],
in_len: Option<usize>,
) -> i32 {
unsafe {
ffi::csp_transaction_persistent(
conn.0,
timeout.as_millis() as u32,
out_data.as_ptr() as *const core::ffi::c_void,
out_data.len() as i32,
in_data.as_ptr() as *mut core::ffi::c_void,
in_len.map(|v| v as i32).unwrap_or(-1),
)
}
}
/// Rust wrapper for [ffi::csp_transaction_w_opts].
///
/// # Parameters
///
/// * `in_len`: Use [None] if the length is unknown, and the expected reply length otherwise.
///
/// # Returns
///
/// 1 or reply size on success, 0 otherwise.
#[allow(clippy::too_many_arguments)]
pub fn csp_transaction_w_opts(
prio: MsgPriority,
dst: u16,
dst_port: u8,
timeout: Duration,
out_data: &[u8],
in_data: &mut [u8],
in_len: Option<usize>,
opts: ConnectOpts,
) -> i32 {
unsafe {
ffi::csp_transaction_w_opts(
prio as u8,
dst,
dst_port,
timeout.as_millis() as u32,
out_data.as_ptr() as *const core::ffi::c_void,
out_data.len() as i32,
in_data.as_ptr() as *mut core::ffi::c_void,
in_len.map(|v| v as i32).unwrap_or(-1),
opts.bits(),
)
}
}
/// 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) }
}
}