2016-04-11 20:26:57 +02:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016, Libre Space Foundation <http://librespacefoundation.org/>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gnuradio/io_signature.h>
|
|
|
|
#include <satnogs/whitening.h>
|
|
|
|
#include <satnogs/utils.h>
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
namespace gr {
|
|
|
|
namespace satnogs {
|
2016-04-11 20:26:57 +02:00
|
|
|
|
2018-12-01 23:47:56 +01:00
|
|
|
int whitening::base_unique_id = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data whitening and de-whitening class
|
|
|
|
* @param mask the polynomial mask
|
|
|
|
* @param seed the initial seed
|
|
|
|
* @param order the order of the shift register. This is equal to the
|
|
|
|
* number of memory stages.
|
|
|
|
*/
|
2018-12-03 00:42:14 +01:00
|
|
|
whitening::whitening_sptr
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::make(uint32_t mask, uint32_t seed, uint32_t order)
|
2018-12-01 23:47:56 +01:00
|
|
|
{
|
2018-12-03 00:42:14 +01:00
|
|
|
return whitening::whitening_sptr(new whitening(mask, seed, order));
|
2018-12-01 23:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-16 14:15:51 +02:00
|
|
|
/**
|
|
|
|
* CCSDS compliant data whitening
|
|
|
|
* @return shared pointer to a CCSDS compliant scrambler/descrambler
|
|
|
|
*/
|
|
|
|
whitening::whitening_sptr
|
|
|
|
whitening::make_ccsds()
|
|
|
|
{
|
|
|
|
return make(0xA9, 0xFF, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
/**
|
|
|
|
* Data whitening and de-whitening class
|
|
|
|
* @param mask the polynomial mask
|
|
|
|
* @param seed the initial seed
|
|
|
|
* @param order the order of the shift register. This is equal to the
|
|
|
|
* number of memory stages.
|
|
|
|
*/
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::whitening(uint32_t mask, uint32_t seed, uint32_t order) :
|
|
|
|
d_lfsr(mask, seed, order),
|
|
|
|
d_id(0)
|
2018-12-01 23:47:56 +01:00
|
|
|
{
|
|
|
|
d_id = base_unique_id++;
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::~whitening()
|
2018-12-03 00:42:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-01 23:47:56 +01:00
|
|
|
int
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::unique_id()
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
2018-12-01 23:47:56 +01:00
|
|
|
return d_id;
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
2016-04-11 20:26:57 +02:00
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
/**
|
|
|
|
* Resets the scrambler (or the descrambler) to the initial stage and
|
|
|
|
* the initial seed.
|
|
|
|
*/
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::reset()
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
d_lfsr.reset();
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
2016-04-11 20:26:57 +02:00
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
/**
|
|
|
|
* Performs data scrambling
|
|
|
|
* @param out the output buffer
|
|
|
|
* @param in the input buffer
|
|
|
|
* @param len the number of the bytes to be scrambled
|
|
|
|
* @param msb if set to true, the descrambler starts from the msb
|
|
|
|
*/
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::scramble(uint8_t *out, const uint8_t *in, size_t len, bool msb)
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
uint8_t b;
|
2019-09-12 15:25:10 +02:00
|
|
|
if (msb) {
|
2018-08-09 18:29:47 +02:00
|
|
|
for (i = 0; i < len; i++) {
|
2019-09-12 15:25:10 +02:00
|
|
|
b = d_lfsr.next_bit() << 7;
|
|
|
|
b |= d_lfsr.next_bit() << 6;
|
|
|
|
b |= d_lfsr.next_bit() << 5;
|
|
|
|
b |= d_lfsr.next_bit() << 4;
|
|
|
|
b |= d_lfsr.next_bit() << 3;
|
|
|
|
b |= d_lfsr.next_bit() << 2;
|
|
|
|
b |= d_lfsr.next_bit() << 1;
|
|
|
|
b |= d_lfsr.next_bit();
|
2018-08-09 18:29:47 +02:00
|
|
|
out[i] = in[i] ^ b;
|
2016-04-11 20:26:57 +02:00
|
|
|
}
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
else {
|
2018-08-09 18:29:47 +02:00
|
|
|
for (i = 0; i < len; i++) {
|
2019-09-12 15:25:10 +02:00
|
|
|
b = d_lfsr.next_bit();
|
|
|
|
b |= d_lfsr.next_bit() << 1;
|
|
|
|
b |= d_lfsr.next_bit() << 2;
|
|
|
|
b |= d_lfsr.next_bit() << 3;
|
|
|
|
b |= d_lfsr.next_bit() << 4;
|
|
|
|
b |= d_lfsr.next_bit() << 5;
|
|
|
|
b |= d_lfsr.next_bit() << 6;
|
|
|
|
b |= d_lfsr.next_bit() << 7;
|
2018-08-09 18:29:47 +02:00
|
|
|
out[i] = in[i] ^ b;
|
2016-04-11 20:26:57 +02:00
|
|
|
}
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-11 20:26:57 +02:00
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
/**
|
|
|
|
* Performs data de-scrambling
|
|
|
|
* @param out the output buffer
|
|
|
|
* @param in the input buffer
|
|
|
|
* @param len the number of the bytes to be de-scrambled
|
|
|
|
* @param msb if set to true, the descrambler starts from the msb
|
|
|
|
*/
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::descramble(uint8_t *out, const uint8_t *in, size_t len,
|
|
|
|
bool msb)
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
scramble(out, in, len, msb);
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
2016-06-22 14:37:27 +02:00
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
/**
|
|
|
|
* Performs data scrambling. The input and output buffer
|
|
|
|
* contain one bit per byte
|
|
|
|
* @param out the output buffer
|
|
|
|
* @param in the input buffer
|
|
|
|
* @param bits_num the number of bits to be scrambled
|
|
|
|
*/
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::scramble_one_bit_per_byte(uint8_t *out, const uint8_t *in,
|
|
|
|
size_t bits_num)
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < bits_num; i++) {
|
2019-09-12 15:25:10 +02:00
|
|
|
out[i] = in[i] ^ d_lfsr.next_bit();
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs data descrambling. The input and output buffer
|
|
|
|
* contain one bit per byte
|
|
|
|
* @param out the output buffer
|
|
|
|
* @param in the input buffer
|
|
|
|
* @param bits_num the number of bits to be descrambled
|
|
|
|
*/
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
whitening::descramble_one_bit_per_byte(uint8_t *out, const uint8_t *in,
|
|
|
|
size_t bits_num)
|
2018-08-09 18:29:47 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
scramble_one_bit_per_byte(out, in, bits_num);
|
2018-08-09 18:29:47 +02:00
|
|
|
}
|
2016-06-22 14:37:27 +02:00
|
|
|
|
2018-08-09 18:29:47 +02:00
|
|
|
} /* namespace satnogs */
|
2016-04-11 20:26:57 +02:00
|
|
|
} /* namespace gr */
|