From 93af26ef131bb4171a13271393aea9b40827e385 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sun, 22 Aug 2021 10:29:31 +0200 Subject: [PATCH] Implementation of M17 decorrelator, polynomial interleaver and Golay(24,12) encoder --- .../include/protocols/M17/M17Decorrelator.h | 63 +++++++++++++++++++ openrtx/include/protocols/M17/M17Golay.h | 63 +++++++++++++++++++ .../include/protocols/M17/M17Interleaver.h | 56 +++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 openrtx/include/protocols/M17/M17Decorrelator.h create mode 100644 openrtx/include/protocols/M17/M17Golay.h create mode 100644 openrtx/include/protocols/M17/M17Interleaver.h diff --git a/openrtx/include/protocols/M17/M17Decorrelator.h b/openrtx/include/protocols/M17/M17Decorrelator.h new file mode 100644 index 00000000..23d7c4f2 --- /dev/null +++ b/openrtx/include/protocols/M17/M17Decorrelator.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * Adapted from original code written by Rob Riggs, Mobilinkd LLC * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef M17_DECORRELATOR_H +#define M17_DECORRELATOR_H + +#ifndef __cplusplus +#error This header is C++ only! +#endif + +#include +#include +#include + + +/** + * Decorrelator sequence for data randomisation. + */ +static constexpr std::array< uint8_t, 46 > sequence = +{ + 0xd6, 0xb5, 0xe2, 0x30, 0x82, 0xFF, 0x84, 0x62, + 0xba, 0x4e, 0x96, 0x90, 0xd8, 0x98, 0xdd, 0x5d, + 0x0c, 0xc8, 0x52, 0x43, 0x91, 0x1d, 0xf8, 0x6e, + 0x68, 0x2F, 0x35, 0xda, 0x14, 0xea, 0xcd, 0x76, + 0x19, 0x8d, 0xd5, 0x80, 0xd1, 0x33, 0x87, 0x13, + 0x57, 0x18, 0x2d, 0x29, 0x78, 0xc3 +}; + + +/** + * Apply M17 decorrelation scheme to a byte array. + * + * \param data: byte array to be decorrelated.. + */ +template +inline void decorrelate(std::array< uint8_t, N >& data) +{ + for (size_t i = 0; i < N; i++) + { + data[i] ^= sequence[i]; + } +} + +#endif /* M17_DECORRELATOR_H */ diff --git a/openrtx/include/protocols/M17/M17Golay.h b/openrtx/include/protocols/M17/M17Golay.h new file mode 100644 index 00000000..cc50f866 --- /dev/null +++ b/openrtx/include/protocols/M17/M17Golay.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * Adapted from original code written by Rob Riggs, Mobilinkd LLC * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef M17_GOLAY_H +#define M17_GOLAY_H + +#ifndef __cplusplus +#error This header is C++ only! +#endif + +#include + +/** + * Compute the Golay(24,12) codeword of a given block of data using the M17 + * Golay polynomial. + * Result is composed as follows: + * + * +--------+----------+--------+ + * | parity | checksum | data | + * +--------+----------+--------+ + * | 1 bit | 11 bit | 12 bit | + * +--------+----------+--------+ + * + * \param data: input data, upper four bits are discarded. + * \return resulting 24 bit codeword. + */ +static uint32_t golay_encode24(const uint16_t& data) +{ + // Compute [23,12] Golay codeword + uint32_t codeword = data & 0x0FFFF; + for(size_t i = 0; i < 12; i++) + { + if(codeword & 0x01) codeword ^= 0x0C75; + codeword >>= 1; + } + + // Append data to codeword, result is checkbits(11) | data(12) + codeword |= (data << 11); + + // Compute parity and append it to codeword + return (codeword << 1) | (__builtin_popcount(codeword) & 0x01); +} + +#endif /* M17_GOLAY_H */ diff --git a/openrtx/include/protocols/M17/M17Interleaver.h b/openrtx/include/protocols/M17/M17Interleaver.h new file mode 100644 index 00000000..b988c226 --- /dev/null +++ b/openrtx/include/protocols/M17/M17Interleaver.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * Adapted from original code written by Rob Riggs, Mobilinkd LLC * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef M17_INTERLEAVER_H +#define M17_INTERLEAVER_H + +#ifndef __cplusplus +#error This header is C++ only! +#endif + +#include "M17Utils.h" + +/** + * Interleave a block of data using the quadratic permutation polynomial from + * M17 protocol specification. Polynomial used is P(x) = 45*x + 92*x^2. + * + * \param data: input byte array. + */ +template < size_t N > +void interleave(std::array< uint8_t, N >& data) +{ + std::array< uint8_t, N > interleaved; + + static constexpr size_t F1 = 45; + static constexpr size_t F2 = 92; + static constexpr size_t NB = N*8; + + for(size_t i = 0; i < NB; i++) + { + size_t index = ((F1 * i) + (F2 * i * i)) % NB; + setBit(interleaved, index, getBit(data, i)); + } + + std::copy(interleaved.begin(), interleaved.end(), data.begin()); +} + +#endif /* M17_INTERLEAVER_H */