Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cats/ham-cats
  • sam/ham-cats
  • Quantum_P/ham-cats
  • Reed/ham-cats
4 results
Show changes
Commits on Source (56)
Showing with 1803 additions and 74 deletions
image: "rust:latest"
before_script:
- rustup component add rustfmt
- rustup component add clippy
test:
script:
- cargo fmt -- --check
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test
[package]
name = "ham-cats"
version = "0.1.0"
version = "0.2.2"
edition = "2021"
license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
description = "Reference implementations for CATS, the ham radio protocol"
repository = "https://gitlab.scd31.com/cats/ham_cats"
homepage = "https://cats.radio"
categories = ["embedded", "encoding", "no-std", "no-std::no-alloc"]
[dependencies]
arrayvec = { version = "0.7.4", default-features = false }
bitvec = { version = "1.0.1", default-features = false }
crc = "3.0.1"
encoding_rs = { version = "0.8.33", default-features = false }
half = { version = "2.3.1", default-features = false }
snafu = "0.7.5"
labrador-ldpc = "1.2.1"
paste = "1.0.14"
snafu = { version = "0.7.5", default-features = false }
The MIT License (MIT)
=====================
Copyright © 2023 CATS
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
target
corpus
artifacts
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arbitrary"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2d098ff73c1ca148721f37baad5ea6a465a13f9573aba8641fbbbae8164a54e"
[[package]]
name = "arrayvec"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"jobserver",
"libc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crc"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86ec7a15cbe22e59248fc7eadb1907dab5ba09372595da4d73dd805ed4417dfe"
dependencies = [
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cace84e55f07e7301bae1c519df89cdad8cc3cd868413d3fdbdeca9ff3db484"
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "encoding_rs"
version = "0.8.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1"
dependencies = [
"cfg-if",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "half"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872"
dependencies = [
"cfg-if",
"crunchy",
]
[[package]]
name = "ham-cats"
version = "0.2.0"
dependencies = [
"arrayvec",
"bitvec",
"crc",
"encoding_rs",
"half",
"labrador-ldpc",
"paste",
"snafu",
]
[[package]]
name = "ham-cats-fuzz"
version = "0.0.0"
dependencies = [
"ham-cats",
"libfuzzer-sys",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "jobserver"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2"
dependencies = [
"libc",
]
[[package]]
name = "labrador-ldpc"
version = "1.1.1"
[[package]]
name = "libc"
version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "libfuzzer-sys"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a96cfd5557eb82f2b83fed4955246c988d331975a002961b07c81584d107e7f7"
dependencies = [
"arbitrary",
"cc",
"once_cell",
]
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "paste"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "proc-macro2"
version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "snafu"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4de37ad025c587a29e8f3f5605c00f70b98715ef90b9061a815b9e59e9042d6"
dependencies = [
"doc-comment",
"snafu-derive",
]
[[package]]
name = "snafu-derive"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "990079665f075b699031e9c08fd3ab99be5029b96f3b78dc0709e8f77e4efebf"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[package]
name = "ham-cats-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"
[package.metadata]
cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
[dependencies.ham-cats]
path = ".."
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "fuzz_target_1"
path = "fuzz_targets/fuzz_target_1.rs"
test = false
doc = false
[[bin]]
name = "fuzz_target_2"
path = "fuzz_targets/fuzz_target_2.rs"
test = false
doc = false
[[bin]]
name = "fuzz_target_3"
path = "fuzz_targets/fuzz_target_3.rs"
test = false
doc = false
[[bin]]
name = "fuzz_target_4"
path = "fuzz_targets/fuzz_target_4.rs"
test = false
doc = false
#![no_main]
use libfuzzer_sys::fuzz_target;
use ham_cats::packet::Packet;
fuzz_target!(|data: &[u8]| {
let mut buf = [0; 1024];
let _ = Packet::<1024>::fully_decode(data, &mut buf);
});
#![no_main]
use libfuzzer_sys::fuzz_target;
use ham_cats::{buffer::Buffer, packet::Packet};
fuzz_target!(|data: &[u8]| {
let mut buf = [0; 1024];
let mut buf = Buffer::new_empty(&mut buf);
if buf.try_extend_from_slice(data).is_ok() {
let _ = Packet::<1024>::semi_decode(buf);
}
});
#![no_main]
use libfuzzer_sys::fuzz_target;
use ham_cats::{buffer::Buffer, packet::Packet};
fuzz_target!(|data: &[u8]| {
let mut buf = [0; 1024];
let mut buf = Buffer::new_empty(&mut buf);
if buf.try_extend_from_slice(data).is_ok() {
let _ = Packet::<1024>::decode(buf);
}
});
#![no_main]
use libfuzzer_sys::fuzz_target;
use ham_cats::packet::Packet;
use std::convert::TryInto;
fuzz_target!(|data: &[u8]| {
// [u8] -> [i8]
let mut data: Vec<_> = data
.iter()
.map(|x| (u16::from(*x) as i16 - 128).try_into().unwrap())
.collect();
let mut buf = [0; 1024];
let _ = Packet::<1024>::fully_decode_soft::<8192, i8>(&mut data, &mut buf);
});
use core::ops::{Deref, DerefMut};
#[derive(Debug)]
pub struct BufferOverflow;
#[derive(Debug)]
pub struct Buffer<'a, const N: usize, T = u8> {
data: &'a mut [T; N],
i: usize,
}
impl<'a, const N: usize, T: Copy> Buffer<'a, N, T> {
/// Constructs a new `Buffer`.
/// `data` is the backing array.
/// `i` is the number of elements in `data` that contain data (and should thus be exposed by `Buffer`)
pub fn new(data: &'a mut [T; N], i: usize) -> Self {
assert!(i <= data.len());
Self { data, i }
}
pub fn new_full(data: &'a mut [T; N]) -> Self {
let i = data.len();
Self::new(data, i)
}
pub fn new_empty(data: &'a mut [T; N]) -> Self {
Self::new(data, 0)
}
pub const fn remaining_capacity(&self) -> usize {
N - self.i
}
pub fn try_push(&mut self, v: T) -> Result<(), BufferOverflow> {
if self.i == N {
return Err(BufferOverflow);
}
self.data[self.i] = v;
self.i += 1;
Ok(())
}
pub fn push(&mut self, v: T) {
self.try_push(v).unwrap();
}
pub fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), BufferOverflow> {
if self.remaining_capacity() < other.len() {
return Err(BufferOverflow);
}
self.data[self.i..(self.i + other.len())].copy_from_slice(other);
self.i += other.len();
Ok(())
}
pub fn extend(&mut self, other: &[T]) {
self.try_extend_from_slice(other).unwrap();
}
pub fn pop(&mut self) -> Option<T> {
if self.i == 0 {
return None;
}
self.i -= 1;
Some(self.data[self.i])
}
pub fn truncate(&mut self, new_len: usize) {
assert!(self.i >= new_len);
self.i = new_len;
}
pub fn drain(&mut self, start: usize, end: usize) {
assert!(end >= start);
assert!(end <= self.i);
let delta = end - start;
let surplus = self.len() - end;
for i in start..(start + surplus) {
self[i] = self[i + delta];
}
self.i -= delta;
}
pub fn clone_backing<'b>(&self, buf: &'b mut [T; N]) -> Buffer<'b, N, T> {
let mut out = Buffer::new_empty(buf);
out.extend(self);
out
}
}
impl<'a, const N: usize, T> From<&'a mut [T; N]> for Buffer<'a, N, T> {
fn from(data: &'a mut [T; N]) -> Self {
Self { data, i: 0 }
}
}
impl<const N: usize, T> Deref for Buffer<'_, N, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.data[..self.i]
}
}
impl<const N: usize, T> DerefMut for Buffer<'_, N, T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.data[..self.i]
}
}
......@@ -16,6 +16,12 @@ pub enum DecodeError {
#[snafu(display("CRC checksum mismatch"))]
CrcMismatch,
#[snafu(display("LDPC Decode error"))]
LdpcError,
#[snafu(display("Given data does not fit in the CATS packet"))]
Overflow,
}
#[derive(Debug, Snafu)]
......@@ -26,3 +32,57 @@ pub enum EncodeError {
#[snafu(display("Given data causes CATS packet to overflow"))]
CatsOverflow,
}
#[derive(Debug, Snafu)]
pub enum CommentError {
#[snafu(display("CATS packet does not have comment data"))]
NoComment,
#[snafu(display("Given buffer too small for comment data"))]
BufferOverflow,
}
#[derive(Debug, Snafu)]
pub enum DigipeatError {
#[snafu(display("No route"))]
NoRoute,
#[snafu(display("Identification is us"))]
Us,
#[snafu(display("Max hops hit"))]
MaxHops,
#[snafu(display("Already digipeated by this node"))]
AlreadyDigipeated,
#[snafu(display("Future hop(s) are already set"))]
SetDestiny,
}
#[derive(Debug, Snafu, PartialEq, Eq)]
pub enum AppendNodeError {
#[snafu(display("Future hop(s) already set to a different node"))]
SetFuture,
#[snafu(display("Max hops hit"))]
HopsOverflow,
#[snafu(display("Node already in route"))]
DuplicateNode,
#[snafu(display("Given data causes the route whisker to overflow"))]
RouteOverflow,
}
#[derive(Debug, Snafu, PartialEq, Eq)]
pub enum PacketRouteAppendError {
#[snafu(display("No route whisker on packet"))]
NoRouteWhisker,
#[snafu(display("Route error"))]
Route { error: AppendNodeError },
#[snafu(display("CATS packet overflow"))]
PacketOverflow,
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct Identity<'a> {
callsign: &'a str,
ssid: u8,
}
impl<'a> Identity<'a> {
pub fn new(callsign: &'a str, ssid: u8) -> Self {
Self { callsign, ssid }
}
pub fn callsign(&self) -> &'a str {
self.callsign
}
pub fn ssid(&self) -> u8 {
self.ssid
}
}
use bitvec::prelude::*;
use labrador_ldpc::decoder::DecodeFrom;
use crate::{
buffer::{Buffer, BufferOverflow},
error::EncodeError,
};
pub(crate) fn interleave<const N: usize>(
data: &[u8],
out: &mut Buffer<N>,
) -> Result<(), EncodeError> {
let bv = data.view_bits::<Msb0>();
let initial_length = out.len();
for _ in 0..data.len() {
out.try_push(0).map_err(|_| EncodeError::CatsOverflow)?;
}
let bv_out = out[initial_length..].view_bits_mut::<Msb0>();
let mut out_i = 0;
for i in 0..32 {
for j in (0..bv.len()).step_by(32) {
if i + j >= bv.len() {
continue;
}
bv_out.set(out_i, bv[i + j]);
out_i += 1;
}
}
Ok(())
}
pub(crate) fn uninterleave<const N: usize>(
data: &[u8],
out: &mut Buffer<N>,
) -> Result<(), BufferOverflow> {
let bv = data.view_bits::<Msb0>();
for _ in 0..data.len() {
out.try_push(0)?;
}
let bv_out = out.view_bits_mut::<Msb0>();
let mut out_i = 0;
for i in 0..32 {
for j in (0..bv.len()).step_by(32) {
if i + j >= bv.len() {
continue;
}
bv_out.set(i + j, bv[out_i]);
out_i += 1;
}
}
Ok(())
}
pub(crate) fn uninterleave_soft<const N: usize, T: DecodeFrom>(
data: &[T],
out: &mut Buffer<N, T>,
) -> Result<(), BufferOverflow> {
for _ in 0..data.len() {
out.try_push(T::zero())?;
}
let mut out_i = 0;
for i in 0..32 {
for j in (0..data.len()).step_by(32) {
if i + j >= data.len() {
continue;
}
out[i + j] = data[out_i];
out_i += 1;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::soft_bit::FromHardBit;
#[test]
fn interleaver_works() {
let mut data = [0x84, 0x73, 0x12, 0xA3, 0xFF, 0x00, 0xC2, 0x1B, 0x77];
let orig = Buffer::new_full(&mut data);
let mut interleaved = [0; 10];
let mut interleaved = Buffer::new(&mut interleaved, 0);
interleaved.push(b'H');
interleave(&orig, &mut interleaved).unwrap();
let expected = [b'H', 0xCD, 0xB5, 0xDB, 0x2A, 0x0A, 0x52, 0x0C, 0x89, 0x4F];
assert_eq!(expected, interleaved[..]);
let mut uninterleaved = [0; 10];
let mut uninterleaved = Buffer::new(&mut uninterleaved, 0);
uninterleave(&interleaved[1..], &mut uninterleaved).unwrap();
assert_eq!(*orig, *uninterleaved);
}
#[test]
fn hard_interleave_soft_uninterleave() {
let mut data = [0x84, 0x73, 0x12, 0xA3, 0xFF, 0x00, 0xC2, 0x1B, 0x77];
let orig = Buffer::new_full(&mut data);
let mut interleaved = [0; 10];
let mut interleaved = Buffer::new(&mut interleaved, 0);
interleaved.push(b'H');
interleave(&orig, &mut interleaved).unwrap();
let mut soft_interleaved = [0.0; 10 * 8];
for (i, b) in interleaved.iter().enumerate() {
for j in 0..8 {
soft_interleaved[8 * i + j] = f32::from_hard_bit(b & (1 << (7 - j)) > 0);
}
}
let mut uninterleaved = [0.0; 10 * 8];
let mut uninterleaved = Buffer::new(&mut uninterleaved, 0);
uninterleave_soft(&soft_interleaved[8..], &mut uninterleaved).unwrap();
assert_eq!(orig.len() * 8, uninterleaved.len());
for (i, b) in orig.iter().enumerate() {
for j in 0..8 {
assert_eq!(uninterleaved[8 * i + j].hard_bit(), *b & (1 << (7 - j)) > 0);
}
}
}
}
use labrador_ldpc::{decoder::DecodeFrom, LDPCCode};
use crate::{buffer::Buffer, error::EncodeError};
macro_rules! enc_chunk {
($d:ident, $i:ident, $t:ident, $n:literal) => {
::paste::paste! {
let mut codeword = [0; $n * 2];
codeword[0..$n].copy_from_slice(&$d[$i..($i + $n)]);
labrador_ldpc::LDPCCode::[<$t>].encode(&mut codeword);
$d.try_extend_from_slice(&codeword[$n..])
.map_err(|_| EncodeError::CatsOverflow)?;
$i += $n;
}
};
}
macro_rules! dec_chunk {
($d:ident, $p:ident, $w:ident, $t:ident, $n:literal) => {
::paste::paste! {
let code_data = &mut $d[..$n];
let code_parity = &$p.get_mut(..$n)?;
let mut input = [0; $n * 2];
input[..$n].copy_from_slice(code_data);
input[$n..].copy_from_slice(code_parity);
const CODE: LDPCCode = LDPCCode::[<$t>];
let mut out = [0; CODE.output_len()];
CODE.decode_bf(&input, &mut out, &mut $w[..CODE.decode_bf_working_len()], 16);
code_data.copy_from_slice(&out[..$n]);
$d = &mut $d[$n..];
$p = &mut $p[$n..];
}
};
}
macro_rules! dec_chunk_soft {
($d:ident, $p:ident, $w:ident, $w_u8:ident, $out:ident, $t:ident, $n:literal) => {
::paste::paste! {
let code_data = &mut $d[..$n];
let code_parity = &$p.get_mut(..$n)?;
let mut input = [T::zero(); $n * 2];
input[..$n].copy_from_slice(code_data);
input[$n..].copy_from_slice(code_parity);
const CODE: LDPCCode = LDPCCode::[<$t>];
let mut out_tmp = [0; CODE.output_len()];
CODE.decode_ms(&input, &mut out_tmp, &mut $w[..CODE.decode_ms_working_len()], &mut $w_u8[..CODE.decode_ms_working_u8_len()], 16);
$out.try_extend_from_slice(&out_tmp[..$n/8]).ok()?;
$d = &mut $d[$n..];
$p = &mut $p[$n..];
}
};
}
// On failure this still modifies the data array!
pub(crate) fn encode<const N: usize>(data: &mut Buffer<N>) -> Result<(), EncodeError> {
let mut i = 0;
let n = data.len();
loop {
match n - i {
512.. => {
enc_chunk!(data, i, TM8192, 512);
}
128.. => {
enc_chunk!(data, i, TM2048, 128);
}
32.. => {
enc_chunk!(data, i, TC512, 32);
}
16.. => {
enc_chunk!(data, i, TC256, 16);
}
8.. => {
enc_chunk!(data, i, TC128, 8);
}
0 => break,
_ => {
let mut codeword = [0xAA; 16];
codeword[0..(n - i)].copy_from_slice(&data[i..n]);
LDPCCode::TC128.encode(&mut codeword);
data.try_extend_from_slice(&codeword[8..])
.map_err(|_| EncodeError::CatsOverflow)?;
i = n;
}
}
}
data.try_extend_from_slice(&u16::try_from(n).unwrap().to_le_bytes())
.map_err(|_| EncodeError::CatsOverflow)?;
Ok(())
}
pub(crate) fn decode<const N: usize>(data_av: &mut Buffer<N>) -> Option<()> {
if data_av.len() < 2 {
return None;
}
let len = [data_av[data_av.len() - 2], data_av[data_av.len() - 1]];
let len = u16::from_le_bytes(len).into();
if len >= data_av.len() {
return None;
}
let (mut data, parity) = data_av.split_at_mut(len);
let mut parity = parity.get_mut(..parity.len().checked_sub(2)?)?;
let mut working = [0; LDPCCode::TM8192.decode_bf_working_len()];
loop {
match data.len() {
512.. => {
dec_chunk!(data, parity, working, TM8192, 512);
}
128.. => {
dec_chunk!(data, parity, working, TM2048, 128);
}
32.. => {
dec_chunk!(data, parity, working, TC512, 32);
}
16.. => {
dec_chunk!(data, parity, working, TC256, 16);
}
8.. => {
dec_chunk!(data, parity, working, TC128, 8);
}
0 => break,
_ => {
let mut code_data = [0xAA; 8];
code_data[..data.len()].copy_from_slice(data);
let code_parity = &parity.get_mut(..8)?;
let mut input = [0; 16];
input[..8].copy_from_slice(&code_data);
input[8..].copy_from_slice(code_parity);
let mut out = [0; LDPCCode::TC128.output_len()];
LDPCCode::TC128.decode_bf(
&input,
&mut out,
&mut working[..LDPCCode::TC128.decode_bf_working_len()],
16,
);
data.copy_from_slice(&out[..data.len()]);
data = &mut data[..0];
parity = &mut parity[..0];
}
}
}
// remove the parity data
data_av.truncate(len);
Some(())
}
pub(crate) fn decode_soft<const N: usize, const M: usize, T: DecodeFrom>(
data_av: &mut Buffer<N, T>,
out: &mut Buffer<M>,
) -> Option<()> {
if data_av.len() % 8 != 0 {
return None;
}
if data_av.len() < 16 {
return None;
}
let len: usize = len_from_soft(data_av[(data_av.len() - 16)..].try_into().unwrap()).into();
if len * 8 + 16 >= data_av.len() {
return None;
}
let data_len = data_av.len().checked_sub(16)?;
let data_av = data_av.get_mut(..data_len)?;
let (mut data, mut parity) = data_av.split_at_mut(len * 8);
let mut working = [T::zero(); LDPCCode::TM8192.decode_ms_working_len()];
let mut working_u8 = [0; LDPCCode::TM8192.decode_ms_working_u8_len()];
loop {
match data.len() {
4096.. => {
dec_chunk_soft!(data, parity, working, working_u8, out, TM8192, 4096);
}
1024.. => {
dec_chunk_soft!(data, parity, working, working_u8, out, TM2048, 1024);
}
256.. => {
dec_chunk_soft!(data, parity, working, working_u8, out, TC512, 256);
}
128.. => {
dec_chunk_soft!(data, parity, working, working_u8, out, TC256, 128);
}
64.. => {
dec_chunk_soft!(data, parity, working, working_u8, out, TC128, 64);
}
0 => break,
_ => {
// Extra bits are padded with 0xAA
// We need to tell the soft decoder that these bits can't have flipped
// So we use T::maxval
let mut code_data = [
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
]
.map(|x| if x > 0 { -T::maxval() } else { T::maxval() });
code_data[..data.len()].copy_from_slice(data);
let code_parity = &parity.get_mut(..64)?;
let mut input = [T::zero(); 128];
input[..64].copy_from_slice(&code_data);
input[64..].copy_from_slice(code_parity);
let mut tmp_out = [0; LDPCCode::TC128.output_len()];
LDPCCode::TC128.decode_ms(
&input,
&mut tmp_out,
&mut working[..LDPCCode::TC128.decode_ms_working_len()],
&mut working_u8[..LDPCCode::TC128.decode_ms_working_u8_len()],
16,
);
out.try_extend_from_slice(&tmp_out[..(data.len() / 8)])
.ok()?;
data = &mut data[..0];
parity = &mut parity[..0];
}
}
}
Some(())
}
fn len_from_soft<T: DecodeFrom>(bits: &[T; 16]) -> u16 {
let mut upper = 0;
for b in &bits[0..8] {
upper <<= 1;
upper |= u8::from(b.hard_bit());
}
let mut lower = 0;
for b in &bits[8..] {
lower <<= 1;
lower |= u8::from(b.hard_bit());
}
u16::from_le_bytes([upper, lower])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::soft_bit::FromHardBit;
use bitvec::{order::Msb0, view::BitView};
#[test]
fn len_test() {
// from the example in the docs
let mut buf = [0; 8191];
let mut data = Buffer::new_empty(&mut buf);
for _ in 0..41 {
data.extend(b"Example packet data wueirpqwerwrywqoeiruy29346129384761");
}
data.extend(b"Example packet data wueirpqwerwrywqoeiru346129384761");
assert_eq!(2349, data.len());
encode(&mut data).unwrap();
assert_eq!(4703, data.len());
}
#[test]
fn basic_encode_decode_short() {
let mut buf = [0; 32];
let mut buf2 = [0; 32];
let mut data = Buffer::new_empty(&mut buf);
data.try_extend_from_slice(b"Hello world!").unwrap();
let orig = data.clone_backing(&mut buf2);
encode(&mut data).unwrap();
decode(&mut data).unwrap();
assert_eq!(*orig, *data);
}
#[test]
fn basic_encode_decode() {
let mut buf = [0; 8191];
let mut buf2 = [0; 8191];
let mut data = Buffer::new_empty(&mut buf);
for _ in 0..50 {
data.extend(b"This is a test packet. jsalksjd093809324JASLD:LKD*#$)(*#@)");
}
let orig = data.clone_backing(&mut buf2);
encode(&mut data).unwrap();
assert_ne!(*orig, *data);
decode(&mut data).unwrap();
assert_eq!(*orig, *data);
}
#[test]
fn encode_decode_with_bit_flips() {
let mut buf = [0; 8191];
let mut buf2 = [0; 8191];
let mut data = Buffer::new_empty(&mut buf);
for _ in 0..50 {
data.extend(b"jsalksjd093809324JASLD:LKD*#$)(*#@) Another test packet");
}
let orig = data.clone_backing(&mut buf2);
encode(&mut data).unwrap();
assert_ne!(*orig, *data);
data[234] ^= 0x55;
data[0] ^= 0xAA;
data[999] ^= 0x43;
decode(&mut data).unwrap();
assert_eq!(*orig, *data);
}
#[test]
fn basic_encode_decode_soft() {
let mut buf = [0; 8191];
let mut buf2 = [0; 8191];
let mut data = Buffer::new_empty(&mut buf);
for _ in 0..50 {
data.extend(b"This is a test packet. jsalksjd093809324JASLD:LKD*#$)(*#@)");
}
let orig = data.clone_backing(&mut buf2);
encode(&mut data).unwrap();
assert_ne!(*orig, *data);
let mut soft = [0.0; 8191 * 8];
let mut soft = Buffer::new_empty(&mut soft);
for b in data.view_bits::<Msb0>() {
soft.push(f32::from_hard_bit(*b));
}
let mut out = [0; 8191];
let mut out = Buffer::new_empty(&mut out);
decode_soft(&mut soft, &mut out).unwrap();
assert_eq!(*orig, *out);
}
}
#![no_std]
#![cfg_attr(not(test), no_std)]
pub mod buffer;
pub mod error;
pub mod identity;
pub mod interleaver;
pub mod ldpc;
pub mod packet;
pub mod soft_bit;
pub mod whisker;
pub mod whitener;
mod utf8;
use core::fmt::Debug;
use arrayvec::{ArrayVec, CapacityError};
use crc::{Crc, CRC_16_IBM_SDLC};
use labrador_ldpc::decoder::DecodeFrom;
use crate::{
error::{DecodeError, EncodeError},
utf8,
buffer::{Buffer, BufferOverflow},
error::{CommentError, DecodeError, DigipeatError, EncodeError, PacketRouteAppendError},
identity::Identity,
interleaver, ldpc, utf8,
whisker::{
Comment, Identification, ValidatedWhiskerIter, Whisker, WhiskerIter, COMMENT_TYPE,
IDENT_TYPE,
Arbitrary, Comment, Destination, Gps, Identification, NodeInfo, PastHop, Route, RouteHop,
Timestamp, ValidatedWhiskerIter, Whisker, WhiskerIter, COMMENT_TYPE,
},
whitener,
};
const X25: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
macro_rules! uniq_whisker {
($t:meta) => {
::paste::paste! {
pub fn [<$t:snake:lower>](&self) -> Option<$t> {
self.iter().find_map(|w| match w {
Whisker::$t(x) => Some(x),
_ => None,
})
}
pub fn [<add_ $t:snake:lower>](&mut self, w: $t) -> Result<(), EncodeError> {
if self.[<$t:snake:lower>]().is_some() {
return Err(EncodeError::DuplicateData);
}
try_lock(&mut self.buf, |data| {
let mut buf = [0; 256];
// safe to unwrap since we know we have enough space
let out = w.encode(&mut buf).unwrap();
data.try_push(crate::whisker::[<$t:snake:upper _TYPE>]).ok().ok_or(EncodeError::CatsOverflow)?;
data.try_extend_from_slice(out).ok().ok_or(EncodeError::CatsOverflow)?;
Ok(())
})?;
Ok(())
}
pub fn [<clear_ $t:snake:lower>](&mut self) {
self.clear_by_type(crate::whisker::[<$t:snake:upper _TYPE>], false);
}
}
};
}
macro_rules! poly_whisker {
($t: meta) => {
::paste::paste! {
pub fn [<$t:lower _iter>](&self) -> core::iter::FilterMap<ValidatedWhiskerIter, fn(Whisker) -> Option<$t>> {
fn filt(w: Whisker) -> Option<$t> {
match w {
Whisker::$t(x) => Some(x),
_ => None
}
}
self.iter().filter_map(filt)
}
pub fn [<add_ $t:lower>](&mut self, w: $t) -> Result<(), EncodeError> {
try_lock(&mut self.buf, |data| {
let mut buf = [0; 256];
// safe to unwrap since we know we have enough space
let out = w.encode(&mut buf).unwrap();
data.try_push(crate::whisker::[<$t:upper _TYPE>]).ok().ok_or(EncodeError::CatsOverflow)?;
data.try_extend_from_slice(out).ok().ok_or(EncodeError::CatsOverflow)?;
Ok(())
})?;
Ok(())
}
pub fn [<clear_ $t:lower>](&mut self) {
self.clear_by_type(crate::whisker::[<$t:upper _TYPE>], true);
}
}
};
}
/// N is the maximum packet size we can handle
/// Panics if N >= 8191
#[derive(Default, Clone)]
pub struct Packet<const N: usize> {
data: ArrayVec<u8, N>,
pub struct Packet<'a, const N: usize> {
buf: Buffer<'a, N>,
}
// TODO need to have methods for removing whiskers from the packet
impl<const N: usize> Packet<N> {
pub fn decode(data: ArrayVec<u8, N>) -> Result<Self, DecodeError> {
impl<'a, const N: usize> Packet<'a, N> {
pub fn new(buf: &'a mut [u8; N]) -> Self {
Self { buf: buf.into() }
}
pub fn clone_backing<'b>(&self, buf: &'b mut [u8; N]) -> Packet<'b, N> {
Packet {
buf: self.buf.clone_backing(buf),
}
}
/// Expects bytes in the `buf`
/// `buf` is used as the backing buffer for the Packet
pub fn decode(buf: Buffer<'a, N>) -> Result<Self, DecodeError> {
assert!(N <= 8191);
// validate the data
for w in WhiskerIter::new(&data) {
for w in WhiskerIter::new(&buf) {
w?;
}
let comment_iter = WhiskerIter::new(&data)
let comment_iter = WhiskerIter::new(&buf)
.filter_map(|w| match w.unwrap() {
Whisker::Comment(c) => Some(c.internal_data().clone()),
_ => None,
......@@ -42,18 +127,18 @@ impl<const N: usize> Packet<N> {
return Err(DecodeError::InvalidComment);
}
Ok(Self { data })
Ok(Self { buf })
}
pub fn encode(&self) -> &[u8] {
&self.data
&self.buf
}
/// Directly after the CRC block in The Pipeline
pub fn semi_encode(mut self) -> Result<ArrayVec<u8, N>, (EncodeError, Self)> {
let crc = X25.checksum(&self.data).to_le_bytes();
pub fn semi_encode(mut self) -> Result<Buffer<'a, N>, (EncodeError, Self)> {
let crc = X25.checksum(&self.buf).to_le_bytes();
let res: Result<(), CapacityError<u8>> = try_lock(&mut self.data, |data| {
let res: Result<(), BufferOverflow> = try_lock(&mut self.buf, |data| {
data.try_push(crc[0])?;
data.try_push(crc[1])?;
......@@ -61,77 +146,107 @@ impl<const N: usize> Packet<N> {
});
match res {
Ok(()) => Ok(self.data),
Ok(()) => Ok(self.buf),
Err(_) => Err((EncodeError::CatsOverflow, self)),
}
}
/// Directly after the CRC block in The Pipeline
pub fn semi_decode(mut data: ArrayVec<u8, N>) -> Result<Self, DecodeError> {
let crc1 = data.pop().ok_or(DecodeError::UnexpectedEndOfInput)?;
let crc0 = data.pop().ok_or(DecodeError::UnexpectedEndOfInput)?;
/// Expects bytes in the `buf`
/// `buf` is used as the backing buffer for the Packet
pub fn semi_decode(mut buf: Buffer<'a, N>) -> Result<Self, DecodeError> {
let crc1 = buf.pop().ok_or(DecodeError::UnexpectedEndOfInput)?;
let crc0 = buf.pop().ok_or(DecodeError::UnexpectedEndOfInput)?;
let crc_expected = u16::from_le_bytes([crc0, crc1]);
let crc_actual = X25.checksum(&data);
let crc_actual = X25.checksum(&buf);
if crc_expected != crc_actual {
return Err(DecodeError::CrcMismatch);
}
Self::decode(data)
Self::decode(buf)
}
pub fn iter(&self) -> ValidatedWhiskerIter {
ValidatedWhiskerIter::new(&self.data)
}
pub fn identification(&self) -> Option<Identification> {
self.iter().find_map(|w| match w {
Whisker::Identification(x) => Some(x),
_ => None,
})
}
/// Encodes packet for transmission on the air.
/// Includes the data length L, but does not include the preamble or sync word.
pub fn fully_encode<const M: usize>(self, out: &mut Buffer<M>) -> Result<(), EncodeError> {
let mut data = self.semi_encode().map_err(|(err, _)| err)?;
whitener::whiten(&mut data);
ldpc::encode(&mut data)?;
pub fn add_identification(
&mut self,
identification: Identification,
) -> Result<(), EncodeError> {
if self.identification().is_some() {
return Err(EncodeError::DuplicateData);
}
// safe to unwrap - length must be below 8191
out.try_extend_from_slice(&u16::try_from(data.len()).unwrap().to_le_bytes())
.map_err(|_| EncodeError::CatsOverflow)?;
interleaver::interleave(&data, out)?;
let mut buf = [0; 256];
// safe to unwrap since we know we have enough space
let out = identification.encode(&mut buf).unwrap();
Ok(())
}
try_lock(&mut self.data, |data| {
data.try_push(IDENT_TYPE)
.map_err(|_| EncodeError::CatsOverflow)?;
data.try_extend_from_slice(out)
.map_err(|_| EncodeError::CatsOverflow)?;
/// Decodes packet that was received over the air.
/// Packet shouldn't have preamble, sync word, or data langth L.
/// Expects bytes in `data`
/// `buf` is used as the backing buffer for the Packet
pub fn fully_decode(data: &[u8], buf: &'a mut [u8; N]) -> Result<Self, DecodeError> {
let mut buf = Buffer::new_empty(buf);
interleaver::uninterleave(data, &mut buf).map_err(|_| DecodeError::Overflow)?;
ldpc::decode(&mut buf).ok_or(DecodeError::LdpcError)?;
whitener::whiten(&mut buf);
Self::semi_decode(buf)
}
Ok(())
})?;
/// Expects soft bits in `data`. Bits should be LLR, with positive numbers more likely to be 0.
/// Returns `DecodeError::Overflow` if `M` is less than `data.len()`.
pub fn fully_decode_soft<const M: usize, T: DecodeFrom>(
data: &mut [T],
buf: &'a mut [u8; N],
) -> Result<Self, DecodeError> {
let mut out = [T::zero(); M];
let mut out = Buffer::new_empty(&mut out);
interleaver::uninterleave_soft(data, &mut out).map_err(|_| DecodeError::Overflow)?;
let mut buf = Buffer::new_empty(buf);
ldpc::decode_soft(&mut out, &mut buf).ok_or(DecodeError::LdpcError)?;
whitener::whiten(&mut buf);
Self::semi_decode(buf)
}
Ok(())
pub fn iter(&self) -> ValidatedWhiskerIter {
ValidatedWhiskerIter::new(&self.buf)
}
/// Returns None if given buffer is not big enough
pub fn comment<'a>(&self, buf: &'a mut [u8]) -> Option<&'a str> {
uniq_whisker!(Identification);
uniq_whisker!(Timestamp);
uniq_whisker!(Gps);
uniq_whisker!(Route);
uniq_whisker!(NodeInfo);
poly_whisker!(Destination);
poly_whisker!(Arbitrary);
pub fn comment<'b>(&self, buf: &'b mut [u8]) -> Result<&'b str, CommentError> {
let iter = self.iter().filter_map(|w| match w {
Whisker::Comment(c) => Some(c),
_ => None,
});
let mut i = 0;
let mut has_comment = false;
for c in iter {
has_comment = true;
let data = c.internal_data();
buf.get_mut(i..(i + data.len()))?.copy_from_slice(data);
buf.get_mut(i..(i + data.len()))
.ok_or(CommentError::BufferOverflow)?
.copy_from_slice(data);
i += data.len();
}
if !has_comment {
return Err(CommentError::NoComment);
}
// Safe to unwrap since the comment was pre-validated
Some(core::str::from_utf8(&buf[0..i]).unwrap())
Ok(core::str::from_utf8(&buf[0..i]).unwrap())
}
pub fn add_comment(&mut self, comment: &str) -> Result<(), EncodeError> {
......@@ -140,7 +255,7 @@ impl<const N: usize> Packet<N> {
return Err(EncodeError::DuplicateData);
}
try_lock(&mut self.data, |data| {
try_lock(&mut self.buf, |data| {
let mut comment = comment.as_bytes();
while comment.len() > 255 {
......@@ -170,28 +285,120 @@ impl<const N: usize> Packet<N> {
Ok(())
}
pub fn clear_comment(&mut self) {
self.clear_by_type(COMMENT_TYPE, true);
}
/// Given the callsign and ssid of a node, should it digipeat this packet?
/// Takes into account things such as if we've digipeated it already, the max hops, etc.
pub fn should_digipeat(&self, identity: Identity) -> Result<(), DigipeatError> {
let route = match self.route() {
Some(x) => x,
None => {
return Err(DigipeatError::NoRoute);
}
};
if let Some(ident) = self.identification() {
if &ident.callsign == identity.callsign() && ident.ssid == identity.ssid() {
return Err(DigipeatError::Us);
}
}
let max_hops: usize = route.max_hops.into();
let cur_hops = route
.iter()
.filter(|r| match r {
RouteHop::Internet => false,
RouteHop::Past(_) => true,
RouteHop::Future(_) => false,
})
.count();
if max_hops <= cur_hops {
return Err(DigipeatError::MaxHops);
}
let already_digipeated = route.iter().any(|r| match r {
RouteHop::Internet => false,
RouteHop::Past(past_hop) => past_hop.identity() == identity,
RouteHop::Future(_) => false,
});
if already_digipeated {
return Err(DigipeatError::AlreadyDigipeated);
}
let next_node = route.iter().find_map(|r| match r {
RouteHop::Future(x) => Some(x),
_ => None,
});
match next_node {
Some(ident) if ident != identity => Err(DigipeatError::SetDestiny),
_ => Ok(()),
}
}
/// Note that if this fails due to a CATS overflow, it will wipe the route off of the packet
pub fn append_to_route(
&mut self,
callsign: &str,
ssid: u8,
rssi: Option<f64>,
) -> Result<(), PacketRouteAppendError> {
let mut route = self.route().ok_or(PacketRouteAppendError::NoRouteWhisker)?;
route
.append_hop(PastHop::new(Identity::new(callsign, ssid), rssi))
.map_err(|error| PacketRouteAppendError::Route { error })?;
self.clear_route();
self.add_route(route)
.map_err(|_| PacketRouteAppendError::PacketOverflow)?;
Ok(())
}
fn clear_by_type(&mut self, whisker_type: u8, all: bool) {
let mut i = 0;
while i < self.buf.len() {
let t = self.buf[i];
let step = usize::from(self.buf[i + 1]) + 2;
if t == whisker_type {
self.buf.drain(i, i + step);
if !all {
return;
}
} else {
i += step;
}
}
}
}
impl<const N: usize> Debug for Packet<N> {
impl<const N: usize> Debug for Packet<'_, N> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_list()
.entries(ValidatedWhiskerIter::new(&self.data))
.entries(ValidatedWhiskerIter::new(&self.buf))
.finish()
}
}
// if the function returns an error, we roll back the array
// by rolling it back, we really just pop bytes off the end until the length matches
fn try_lock<const N: usize, T, E, F: Fn(&mut ArrayVec<u8, N>) -> Result<T, E>>(
arr: &mut ArrayVec<u8, N>,
fn try_lock<'a, const N: usize, T, E, F: Fn(&mut Buffer<'a, N>) -> Result<T, E>>(
buf: &mut Buffer<'a, N>,
f: F,
) -> Result<T, E> {
let len = arr.len();
let len = buf.len();
match f(arr) {
match f(buf) {
Ok(x) => Ok(x),
Err(e) => {
arr.truncate(len);
buf.truncate(len);
Err(e)
}
......@@ -200,15 +407,56 @@ fn try_lock<const N: usize, T, E, F: Fn(&mut ArrayVec<u8, N>) -> Result<T, E>>(
#[cfg(test)]
mod tests {
use super::*;
use crate::soft_bit::FromHardBit;
use crate::whisker::NodeInfoBuilder;
use arrayvec::ArrayString;
use bitvec::{order::Msb0, view::BitView};
use super::*;
#[test]
fn dest() {
let d1 = Destination::new(false, 7, "CALL1", 23).unwrap();
let d2 = Destination::new(true, 23, "CALL2", 2).unwrap();
let mut buf = [0; 1024];
let mut packet: Packet<1024> = Packet::new(&mut buf);
packet.add_destination(d1.clone()).unwrap();
packet.add_destination(d2.clone()).unwrap();
let mut dests = packet.destination_iter();
assert_eq!(d1, dests.next().unwrap());
assert_eq!(d2, dests.next().unwrap());
assert_eq!(None, dests.next());
}
#[test]
fn route_clear() {
let mut buf = [0; 1024];
let mut p: Packet<1024> = Packet::new(&mut buf);
p.add_identification(Identification::new("call", 43, 123).unwrap())
.unwrap();
let mut r = Route::new(8);
r.push_internet().unwrap();
p.add_route(r).unwrap();
p.add_comment("This is a comment").unwrap();
p.clear_route();
assert_eq!(
Identification::new("call", 43, 123).unwrap(),
p.identification().unwrap()
);
assert_eq!(None, p.route());
let mut buf = [0; 32];
assert_eq!("This is a comment", p.comment(&mut buf).unwrap());
}
#[test]
fn semi_e2e() {
let comment = "Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.";
let mut packet = Packet::<2048>::default();
let mut buf = [0; 2048];
let mut packet = Packet::new(&mut buf);
packet
.add_identification(Identification {
icon: 123,
......@@ -241,4 +489,330 @@ mod tests {
let mut buf = [0; 1024];
assert_eq!(comment, packet2.comment(&mut buf).unwrap());
}
#[test]
fn full_e2e() {
let comment = "Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.";
let mut buf = [0; 4096];
let mut packet = Packet::new(&mut buf);
packet
.add_identification(Identification {
icon: 123,
callsign: ArrayString::from("ABCXYZ_LONG_CALL").unwrap(),
ssid: 43,
})
.unwrap();
packet.add_comment(comment).unwrap();
let res = packet.add_identification(Identification {
icon: 456,
callsign: ArrayString::from("NOPE").unwrap(),
ssid: 0,
});
assert!(matches!(res, Err(EncodeError::DuplicateData)));
let mut buf2 = [0; 4096];
let mut fully = Buffer::new_empty(&mut buf2);
packet.fully_encode(&mut fully).unwrap();
fully[40] ^= 0x55;
fully[844] ^= 0x7B;
// exclude length
let mut buf3 = [0; 8191];
let packet2: Packet<8191> = Packet::fully_decode(&fully[2..], &mut buf3).unwrap();
assert_eq!(
Identification {
icon: 123,
callsign: ArrayString::from("ABCXYZ_LONG_CALL").unwrap(),
ssid: 43,
},
packet2.identification().unwrap()
);
let mut buf = [0; 1024];
assert_eq!(comment, packet2.comment(&mut buf).unwrap());
}
#[test]
fn full_e2e_soft_decode() {
let comment = "Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.Hello world! This is a comment. It's long so that it needs to be split across more than one whisker.";
let mut buf = [0; 4096];
let mut packet = Packet::new(&mut buf);
packet
.add_identification(Identification {
icon: 123,
callsign: ArrayString::from("ABCXYZ_LONG_CALL").unwrap(),
ssid: 43,
})
.unwrap();
packet.add_comment(comment).unwrap();
let res = packet.add_identification(Identification {
icon: 456,
callsign: ArrayString::from("NOPE").unwrap(),
ssid: 0,
});
assert!(matches!(res, Err(EncodeError::DuplicateData)));
let mut buf2 = [0; 4096];
let mut fully = Buffer::new_empty(&mut buf2);
packet.fully_encode(&mut fully).unwrap();
fully[40] ^= 0x55;
fully[844] ^= 0x7B;
let mut soft = [0.0; 8191 * 8];
let mut soft = Buffer::new_empty(&mut soft);
for b in fully.view_bits::<Msb0>().iter() {
soft.push(f32::from_hard_bit(*b));
}
let soft = &mut soft[16..];
let mut buf3 = [0; 8191];
// exclude length
let packet2: Packet<8191> =
Packet::fully_decode_soft::<{ 8191 * 8 }, _>(soft, &mut buf3).unwrap();
assert_eq!(
Identification {
icon: 123,
callsign: ArrayString::from("ABCXYZ_LONG_CALL").unwrap(),
ssid: 43,
},
packet2.identification().unwrap()
);
let mut buf = [0; 1024];
assert_eq!(comment, packet2.comment(&mut buf).unwrap());
}
#[test]
fn node_info_e2e() {
let mut buf = [0; 4096];
let mut packet = Packet::new(&mut buf);
packet
.add_node_info(
NodeInfoBuilder::default()
.hardware_id(0xBEEF)
.software_id(0xBC)
.uptime(2304)
.antenna_height(5)
.antenna_gain(3.0)
.tx_power(30.0)
.voltage(12.6)
.xcvr_temperature(-15)
.battery_charge(65.0)
.build(),
)
.unwrap();
let mut buf2 = [0; 4096];
let mut encoded = Buffer::new_empty(&mut buf2);
packet.fully_encode(&mut encoded).unwrap();
let mut buf3 = [0; 4096];
let packet2 = Packet::fully_decode(&encoded[2..], &mut buf3).unwrap();
let node_info = packet2.node_info().unwrap();
assert_eq!(0xBEEF, node_info.hardware_id().unwrap());
assert_eq!(0xBC, node_info.software_id().unwrap());
assert_eq!(2304, node_info.uptime().unwrap());
assert_eq!(5, node_info.antenna_height().unwrap());
assert_eq!(3.0, node_info.antenna_gain().unwrap());
assert_eq!(30.0, node_info.tx_power().unwrap());
assert_eq!(12.6, node_info.voltage().unwrap());
assert_eq!(-15, node_info.xcvr_temperature().unwrap());
assert_eq!(64.70588235294117, node_info.battery_charge().unwrap());
}
#[test]
fn node_info_e2e_some_unpopulated() {
let mut buf = [0; 4096];
let mut packet = Packet::new(&mut buf);
packet
.add_node_info(
NodeInfoBuilder::default()
.software_id(0xBC)
.uptime(2304)
.antenna_gain(3.0)
.voltage(12.6)
.xcvr_temperature(-15)
.build(),
)
.unwrap();
let mut buf2 = [0; 4096];
let mut encoded = Buffer::new_empty(&mut buf2);
packet.fully_encode(&mut encoded).unwrap();
let mut buf3 = [0; 4096];
let packet2 = Packet::fully_decode(&encoded[2..], &mut buf3).unwrap();
let node_info = packet2.node_info().unwrap();
assert_eq!(None, node_info.hardware_id());
assert_eq!(0xBC, node_info.software_id().unwrap());
assert_eq!(2304, node_info.uptime().unwrap());
assert_eq!(None, node_info.antenna_height());
assert_eq!(3.0, node_info.antenna_gain().unwrap());
assert_eq!(None, node_info.tx_power());
assert_eq!(12.6, node_info.voltage().unwrap());
assert_eq!(-15, node_info.xcvr_temperature().unwrap());
assert_eq!(None, node_info.battery_charge());
}
#[test]
fn fully_decode_fuzz_tests() {
let data = [
112, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 74, 0, 0, 0, 0, 41, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 114, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 10, 112, 0, 0, 0, 0, 0, 0, 2, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 74, 0, 0, 0, 0, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39,
114, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 145, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
59, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7,
0, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 59, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 228, 0, 0, 0, 64, 0, 65, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 7, 0, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 1, 0, 0, 10, 118, 118, 118, 118, 118, 118, 118, 118, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 228, 0, 0, 0, 64,
0, 65, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 7, 0, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 1, 0, 0, 10, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 1, 2, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 0, 0, 118, 118, 118, 118,
118, 118, 118, 118, 118, 118, 118, 0, 0, 0, 0, 96, 96,
];
let mut buf = [0; 1024];
let _ = Packet::<1024>::fully_decode(&data, &mut buf);
}
#[test]
fn semi_decode_fuzz_tests() {
let cases = [
&[
42, 64, 64, 64, 229, 40, 64, 64, 0, 0, 173, 173, 173, 173, 173, 173, 173, 173, 64,
64, 0, 0, 173, 187, 187, 187, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 187, 187,
187, 101, 157, 138, 74, 101, 157, 138, 74, 0, 0, 0, 0, 1, 126, 255, 255, 255, 187,
187, 187, 187, 187, 187, 187, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 187, 101, 157, 138, 74, 101,
157, 138, 106, 0, 0, 0, 0, 1, 126, 255, 255, 255, 0, 212, 0, 0, 0, 0, 1, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 173, 187, 187, 187, 101,
157, 138, 74, 101, 157, 138, 74, 0, 0, 0, 0, 1, 126, 255, 255, 255, 0, 212, 0, 0,
0, 0, 1, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 157, 138, 74, 0, 0, 0,
0, 1, 126, 255, 255, 255, 0, 212, 0, 0, 0, 0, 1, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 173, 187, 187, 64, 187, 101, 157, 138, 74, 101, 157,
50, 138, 74, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 1, 126, 255, 255, 255, 0, 212, 0, 0, 0, 0, 1, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 64, 64, 0, 0, 173, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 187, 187, 187, 101, 157, 138, 74, 101, 157, 138, 74, 0, 0, 0, 0, 1, 126, 255,
255, 255, 0, 212, 0, 0, 0, 0, 1, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 173, 187, 187, 187, 101, 157, 138, 74, 101, 157, 138, 74, 0, 0, 0,
0, 1, 126, 255, 255, 255, 0, 212, 0, 0, 0, 0, 1, 187, 187, 0, 0, 192, 192, 0, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 64, 64, 0, 0, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 187, 187, 187, 101,
157, 138, 74, 101, 157, 138, 74, 0, 0, 0, 0, 1, 126, 255, 255, 255, 0, 212, 0, 0,
0, 0, 1, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187,
187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 126,
][..],
&[
48, 5, 4, 255, 5, 5, 5, 5, 5, 5, 5, 37, 5, 7, 5, 5, 35, 5, 5, 5, 5, 4, 5, 7, 5, 5,
5, 5, 5, 5, 5, 5, 11, 126, 3, 101, 5, 3, 3, 96, 192, 128, 192, 192,
][..],
];
for data in cases {
let mut buf = [0; 1024];
let mut buf = Buffer::new_empty(&mut buf);
buf.extend(data);
let _ = Packet::<1024>::semi_decode(buf);
}
}
#[test]
fn decode_fuzz_tests() {
let cases = [&[4, 0, 0, 0][..], &[4, 5, 0, 0, 0, 10, 255, 255, 0, 0][..]];
for data in cases {
let mut buf = [0; 1024];
let mut buf = Buffer::new_empty(&mut buf);
buf.extend(data);
let _ = Packet::<1024>::decode(buf);
}
}
#[test]
fn fully_decode_soft_fuzz_tests() {
// When adding to this, don't forget to do the u8 -> i8 conversion
let cases = [
&mut [
-39, -39, -39, -118, -58, -58, -58, -58, -89, -39, -118, -58, -58, -58, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, -58, -58,
127, 81, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, -86, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, -58, -58, 127,
81, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, -58, -58, 127, 81, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, -128, -128, -128, -128, -128, -128, -128, -128, -128,
-128, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, -128, -128,
][..],
&mut [
-73, -73, -73, -73, -75, -76, -73, -73, -73, -73, -73, -73, -73, -73, 73, 72, 72,
72, 72, 72, 72, 62, -73, -118, 120, 127, 127, 121, 127, 112, 127, 127,
],
];
for data in cases {
let mut buf = [0; 1024];
let _ = Packet::<1024>::fully_decode_soft::<8192, i8>(data, &mut buf);
}
}
}
pub use labrador_ldpc::decoder::DecodeFrom;
// Less than zero = 1 bit
// Greater than zero = 0 bit
pub trait FromHardBit {
fn from_hard_bit(bit: bool) -> Self;
}
impl<T: DecodeFrom> FromHardBit for T {
fn from_hard_bit(bit: bool) -> Self {
if bit {
-Self::one()
} else {
Self::one()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn e2e() {
assert!(f32::from_hard_bit(true).hard_bit());
assert!(!f32::from_hard_bit(false).hard_bit());
assert!(f64::from_hard_bit(true).hard_bit());
assert!(!f64::from_hard_bit(false).hard_bit());
assert!(i8::from_hard_bit(true).hard_bit());
assert!(!i8::from_hard_bit(false).hard_bit());
assert!(i16::from_hard_bit(true).hard_bit());
assert!(!i16::from_hard_bit(false).hard_bit());
assert!(i32::from_hard_bit(true).hard_bit());
assert!(!i32::from_hard_bit(false).hard_bit());
}
}
use arrayvec::ArrayVec;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Arbitrary(pub ArrayVec<u8, 255>);
impl Arbitrary {
......
......@@ -2,7 +2,7 @@ use core::str::FromStr;
use arrayvec::ArrayString;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Destination {
ack: u8,
callsign: ArrayString<253>,
......@@ -36,7 +36,7 @@ impl Destination {
}
pub fn ack_num(&self) -> u8 {
self.ack
self.ack & !(1 << 7)
}
pub fn callsign(&self) -> &str {
......