mirror of
https://github.com/chirpstack/chirpstack.git
synced 2025-03-18 18:15:16 +00:00
lrwn: Implement applayer v1 fragmentation encoding func.
This commit is contained in:
parent
87c498544d
commit
08c3ef00f9
@ -487,6 +487,65 @@ pub struct DataFragmentPayloadIndexAndN {
|
||||
pub frag_index: u8,
|
||||
}
|
||||
|
||||
// Encode the given slice of bytes to fragments including forward error correction.
|
||||
// This is based on the proposed FEC code from the Fragmented Data Block Transport over
|
||||
// LoRaWAN recommendation.
|
||||
pub fn encode(payload: &[u8], fragment_size: usize, redundancy: usize) -> Result<Vec<Vec<u8>>> {
|
||||
if payload.len() % fragment_size != 0 {
|
||||
return Err(anyhow!("Payload size must be a multiple of fragment_size"));
|
||||
}
|
||||
|
||||
// fragment the data into rows
|
||||
let mut data_rows: Vec<Vec<u8>> = payload.chunks(fragment_size).map(|v| v.to_vec()).collect();
|
||||
let w = data_rows.len();
|
||||
|
||||
for y in 0..redundancy {
|
||||
let mut s = vec![0; fragment_size];
|
||||
let a = matrix_line(y + 1, w);
|
||||
|
||||
for x in 0..w {
|
||||
if a[x] == 1 {
|
||||
for (m, s_val) in s.iter_mut().enumerate() {
|
||||
*s_val ^= data_rows[x][m];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data_rows.push(s);
|
||||
}
|
||||
|
||||
Ok(data_rows)
|
||||
}
|
||||
|
||||
fn prbs23(x: usize) -> usize {
|
||||
let b0 = x & 1;
|
||||
let b1 = (x & 32) / 32;
|
||||
(x / 2) + (b0 ^ b1) * (1 << 22)
|
||||
}
|
||||
|
||||
fn is_power_2(num: usize) -> bool {
|
||||
num != 0 && (num & (num - 1)) == 0
|
||||
}
|
||||
|
||||
fn matrix_line(n: usize, m: usize) -> Vec<usize> {
|
||||
let mut line = vec![0; m];
|
||||
|
||||
let mm = if is_power_2(m) { 1 } else { 0 };
|
||||
|
||||
let mut x = 1 + (1001 * n);
|
||||
|
||||
for _nb_coeff in 0..(m / 2) {
|
||||
let mut r = 1 << 16;
|
||||
while r >= m {
|
||||
x = prbs23(x);
|
||||
r = x % (m + mm);
|
||||
}
|
||||
line[r] = 1;
|
||||
}
|
||||
|
||||
line
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
@ -759,6 +818,98 @@ mod test {
|
||||
run_tests_decode(&decode_tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode() {
|
||||
struct EncodeTest {
|
||||
name: String,
|
||||
data: Vec<u8>,
|
||||
fragment_size: usize,
|
||||
redundancy: usize,
|
||||
expected_fragments: Vec<Vec<u8>>,
|
||||
expected_error: Option<String>,
|
||||
}
|
||||
|
||||
let mut data = vec![0; 100];
|
||||
for (i, v) in data.iter_mut().enumerate() {
|
||||
*v = i as u8;
|
||||
}
|
||||
|
||||
let tests = [
|
||||
EncodeTest {
|
||||
name: "invalid fragment size".into(),
|
||||
data: vec![0; 5],
|
||||
fragment_size: 10,
|
||||
redundancy: 0,
|
||||
expected_fragments: vec![],
|
||||
expected_error: Some("Payload size must be a multiple of fragment_size".into()),
|
||||
},
|
||||
EncodeTest {
|
||||
name: "fragment size 10, redundancy 10".into(),
|
||||
data: data.clone(),
|
||||
fragment_size: 10,
|
||||
redundancy: 10,
|
||||
expected_fragments: vec![
|
||||
vec![0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9],
|
||||
vec![0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13],
|
||||
vec![0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d],
|
||||
vec![0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27],
|
||||
vec![0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31],
|
||||
vec![0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b],
|
||||
vec![0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45],
|
||||
vec![0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f],
|
||||
vec![0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59],
|
||||
vec![0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63],
|
||||
vec![0x26, 0x26, 0x22, 0x22, 0x2e, 0x2e, 0x22, 0x22, 0x26, 0x26],
|
||||
vec![0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d],
|
||||
vec![0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5],
|
||||
vec![0x36, 0x36, 0x32, 0x32, 0x3e, 0x3e, 0x22, 0x22, 0x36, 0x36],
|
||||
vec![0x3a, 0x3a, 0xe, 0xe, 0xa, 0xa, 0x6, 0x6, 0xa, 0xa],
|
||||
vec![0xe, 0xe, 0x32, 0x32, 0x36, 0x36, 0x22, 0x22, 0x3e, 0x3e],
|
||||
vec![0x2, 0x2, 0xe, 0xe, 0x72, 0x72, 0x76, 0x76, 0x62, 0x62],
|
||||
vec![0x1e, 0x1e, 0x1a, 0x1a, 0x66, 0x66, 0x5a, 0x5a, 0x4e, 0x4e],
|
||||
vec![0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x30, 0x31, 0x22, 0x23],
|
||||
vec![0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x18, 0x19, 0xa, 0xb],
|
||||
],
|
||||
expected_error: None,
|
||||
},
|
||||
EncodeTest {
|
||||
name: "fragment size 10, redundancy 5".into(),
|
||||
data: data.clone(),
|
||||
fragment_size: 10,
|
||||
redundancy: 5,
|
||||
expected_fragments: vec![
|
||||
vec![0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9],
|
||||
vec![0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13],
|
||||
vec![0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d],
|
||||
vec![0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27],
|
||||
vec![0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31],
|
||||
vec![0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b],
|
||||
vec![0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45],
|
||||
vec![0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f],
|
||||
vec![0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59],
|
||||
vec![0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63],
|
||||
vec![0x26, 0x26, 0x22, 0x22, 0x2e, 0x2e, 0x22, 0x22, 0x26, 0x26],
|
||||
vec![0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d],
|
||||
vec![0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5],
|
||||
vec![0x36, 0x36, 0x32, 0x32, 0x3e, 0x3e, 0x22, 0x22, 0x36, 0x36],
|
||||
vec![0x3a, 0x3a, 0xe, 0xe, 0xa, 0xa, 0x6, 0x6, 0xa, 0xa],
|
||||
],
|
||||
expected_error: None,
|
||||
},
|
||||
];
|
||||
|
||||
for tst in &tests {
|
||||
println!("> {}", tst.name);
|
||||
|
||||
let res = encode(&tst.data, tst.fragment_size, tst.redundancy);
|
||||
if let Some(e) = &tst.expected_error {
|
||||
assert_eq!(e, &res.err().unwrap().to_string());
|
||||
} else {
|
||||
assert_eq!(tst.expected_fragments, res.unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_tests_encode(tests: &[CommandTest]) {
|
||||
for tst in tests {
|
||||
println!("> {}", tst.name);
|
||||
|
Loading…
x
Reference in New Issue
Block a user