2019-09-01 22:32:59 +02:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019, Libre Space Foundation <http://libre.space>
|
|
|
|
*
|
|
|
|
* 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/amsat_duv_decoder.h>
|
|
|
|
#include <satnogs/metadata.h>
|
|
|
|
#include <gnuradio/blocks/count_bits.h>
|
|
|
|
|
|
|
|
extern "C" {
|
2019-09-02 19:20:21 +02:00
|
|
|
#include <fec.h>
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
namespace gr {
|
|
|
|
namespace satnogs {
|
2019-09-01 22:32:59 +02:00
|
|
|
|
2019-09-02 19:20:21 +02:00
|
|
|
/**
|
|
|
|
* Actual frame size without RS padding and parity.
|
|
|
|
* 6 bytes are header, 58 payload
|
|
|
|
*/
|
|
|
|
const size_t amsat_duv_decoder::amsat_fox_duv_frame_size = 6 + 58;
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
const uint8_t amsat_duv_decoder::amsat_fox_spacecraft_id[] {
|
|
|
|
0x1 /* FOX-1A */,
|
|
|
|
0x2 /* FOX-1B */,
|
|
|
|
0x3 /* FOX-1C */,
|
|
|
|
0x4 /* FOX-1D */,
|
|
|
|
0x5 /* FOX-1E */
|
|
|
|
};
|
2019-09-02 19:20:21 +02:00
|
|
|
|
2019-09-01 22:32:59 +02:00
|
|
|
/**
|
|
|
|
* Creates a shared pointer to a amsat_duv_decoder object
|
|
|
|
* @param control_symbol the control symbol indicating the start of a frame
|
|
|
|
* @param max_frame_len the maximum frame length
|
|
|
|
* @return a shared pointer to a amsat_duv_decoder object
|
|
|
|
*/
|
|
|
|
decoder::decoder_sptr
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::make(const std::string &control_symbol,
|
|
|
|
size_t max_frame_len)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
return decoder::decoder_sptr(
|
|
|
|
new amsat_duv_decoder(control_symbol, max_frame_len));
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Creates a decoder for the 80b10 DUV coding scheme of the AMSAT FOX satellites
|
|
|
|
* @param control_symbol the control symbol indicating the start of a frame
|
|
|
|
* @param max_frame_len the maximum frame length
|
|
|
|
*/
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::amsat_duv_decoder(const std::string &control_symbol,
|
|
|
|
size_t max_frame_len) :
|
|
|
|
decoder(1, max_frame_len),
|
|
|
|
d_erasure_cnt(0),
|
|
|
|
d_control_symbol_pos(0),
|
|
|
|
d_control_symbol_neg(0),
|
|
|
|
d_data_reg(0),
|
|
|
|
d_wrong_bits(0),
|
|
|
|
d_wrong_bits_neg(0),
|
|
|
|
d_nwrong(0),
|
|
|
|
d_nwrong_neg(0),
|
|
|
|
d_word_cnt(0),
|
|
|
|
d_bitstream_idx(0),
|
|
|
|
d_state(SEARCH_SYNC)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
|
|
|
d_8b_words = new uint8_t[max_frame_len];
|
|
|
|
d_erasures_indexes = new int[max_frame_len];
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
if (!set_access_code(control_symbol)) {
|
|
|
|
throw std::out_of_range("control_symbol is not 10 bits");
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::set_access_code(const std::string &control_symbol)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
unsigned len = control_symbol.length(); // # of bytes in string
|
2019-09-01 22:32:59 +02:00
|
|
|
|
|
|
|
/* if the control sequence is not 10-bit then throw exception */
|
|
|
|
if (len != 10) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
d_control_symbol_pos = (d_control_symbol_pos << 1)
|
2019-09-12 15:25:10 +02:00
|
|
|
| (control_symbol[i] & 0x1);
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
d_control_symbol_neg = (~d_control_symbol_pos) & 0x3FF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::~amsat_duv_decoder()
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
|
|
|
delete[] d_8b_words;
|
|
|
|
delete[] d_erasures_indexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::process_10b(uint16_t word, size_t write_pos)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
|
|
|
uint16_t diff_bits = 0;
|
|
|
|
uint8_t min_pos = 0;
|
|
|
|
uint8_t min_dist = 11;
|
|
|
|
uint8_t curr_dist = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
/* Check for the disparity -1 */
|
|
|
|
while ((i < 256) && (min_dist > 0)) {
|
|
|
|
|
|
|
|
diff_bits = (word ^ (d_lookup_8b10b[0][i])) & 0x3FF;
|
2019-09-12 15:25:10 +02:00
|
|
|
curr_dist = gr::blocks::count_bits16(diff_bits);
|
2019-09-01 22:32:59 +02:00
|
|
|
|
|
|
|
if (curr_dist < min_dist) {
|
|
|
|
min_dist = curr_dist;
|
|
|
|
min_pos = i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the disparity +1 */
|
|
|
|
i = 0;
|
|
|
|
while ((i < 256) && (min_dist > 0)) {
|
|
|
|
|
|
|
|
diff_bits = (word ^ (d_lookup_8b10b[1][i])) & 0x3FF;
|
2019-09-12 15:25:10 +02:00
|
|
|
curr_dist = gr::blocks::count_bits16(diff_bits);
|
2019-09-01 22:32:59 +02:00
|
|
|
|
|
|
|
if (curr_dist < min_dist) {
|
|
|
|
min_dist = curr_dist;
|
|
|
|
min_pos = i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report that there is erasure to this 10 bits */
|
|
|
|
d_8b_words[write_pos] = min_pos;
|
|
|
|
|
|
|
|
/* If we did not found a perfect match, mark this index as erasure */
|
|
|
|
if (min_dist != 0) {
|
|
|
|
d_erasures_indexes[d_erasure_cnt++] = write_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:20:21 +02:00
|
|
|
inline uint16_t
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::pack_10b_word(size_t idx)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
2019-09-02 19:20:21 +02:00
|
|
|
return (((uint16_t) d_bitstream[idx] & 0x1) << 9)
|
2019-09-12 15:25:10 +02:00
|
|
|
| (((uint16_t) d_bitstream[idx + 1] & 0x1) << 8)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 2] & 0x1) << 7)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 3] & 0x1) << 6)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 4] & 0x1) << 5)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 5] & 0x1) << 4)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 6] & 0x1) << 3)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 7] & 0x1) << 2)
|
|
|
|
| (((uint16_t) d_bitstream[idx + 8] & 0x1) << 1)
|
|
|
|
| (d_bitstream[idx + 9] & 0x1);
|
2019-09-02 19:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
2019-09-12 15:25:10 +02:00
|
|
|
is_spacecraft_valid(uint8_t id)
|
2019-09-02 19:20:21 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(amsat_duv_decoder::amsat_fox_spacecraft_id);
|
2019-09-12 15:25:10 +02:00
|
|
|
i++) {
|
2019-09-02 19:20:21 +02:00
|
|
|
if (amsat_duv_decoder::amsat_fox_spacecraft_id[i] == id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder_status_t
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::decode(const void *in, int len)
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
const uint8_t *input = (const uint8_t *) in;
|
2019-09-01 22:32:59 +02:00
|
|
|
decoder_status_t status;
|
|
|
|
int ret;
|
|
|
|
uint16_t word;
|
2019-09-02 19:20:21 +02:00
|
|
|
size_t available;
|
2019-09-01 22:32:59 +02:00
|
|
|
|
2019-09-02 19:20:21 +02:00
|
|
|
/* Due to internal buffering we consume all the availabele symbols */
|
|
|
|
for (int i = 0; i < len; i++) {
|
2019-09-12 15:25:10 +02:00
|
|
|
d_bitstream.push_back(input[i]);
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
2019-09-02 19:20:21 +02:00
|
|
|
status.consumed = len;
|
2019-09-01 22:32:59 +02:00
|
|
|
|
2019-09-02 19:20:21 +02:00
|
|
|
while (1) {
|
|
|
|
bool cont = false;
|
2019-09-12 15:25:10 +02:00
|
|
|
if (d_bitstream.size() < 11) {
|
2019-09-01 22:32:59 +02:00
|
|
|
return status;
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
switch (d_state) {
|
|
|
|
case SEARCH_SYNC:
|
|
|
|
for (size_t i = 0; i < d_bitstream.size(); i++) {
|
|
|
|
d_data_reg = (d_data_reg << 1) | (d_bitstream[i] & 0x1);
|
|
|
|
d_wrong_bits = (d_data_reg ^ d_control_symbol_pos) & 0x3FF;
|
|
|
|
d_wrong_bits_neg = (d_data_reg ^ d_control_symbol_neg) & 0x3FF;
|
|
|
|
d_nwrong = gr::blocks::count_bits16(d_wrong_bits);
|
|
|
|
d_nwrong_neg = gr::blocks::count_bits16(d_wrong_bits_neg);
|
|
|
|
|
|
|
|
/* we found the controls symbol */
|
|
|
|
if ((d_nwrong == 0) || (d_nwrong_neg == 0)) {
|
|
|
|
d_erasure_cnt = 0;
|
|
|
|
d_word_cnt = 0;
|
|
|
|
d_state = DECODING;
|
|
|
|
if (i > 10) {
|
|
|
|
d_bitstream_idx = 9;
|
|
|
|
d_bitstream.erase(d_bitstream.begin(),
|
|
|
|
d_bitstream.begin() + i + 1 - 9);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d_bitstream_idx = i;
|
|
|
|
d_bitstream.pop_front();
|
2019-09-02 19:20:21 +02:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
}
|
|
|
|
/* No SYNC found on the entire buffer. Clear it and return */
|
|
|
|
d_bitstream.clear();
|
|
|
|
return status;
|
|
|
|
case DECODING:
|
|
|
|
available = d_bitstream.size() - d_bitstream_idx;
|
|
|
|
if (available < 10) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* From now one, we have a SYNC so we process the data
|
|
|
|
* in chunks of 10 bits
|
|
|
|
*/
|
|
|
|
for (size_t i = 0; i < available / 10; i++, d_bitstream_idx += 10) {
|
|
|
|
word = pack_10b_word(d_bitstream_idx);
|
|
|
|
|
|
|
|
/* Revert 10b to 8b and accumulate! */
|
|
|
|
process_10b(word, d_word_cnt);
|
|
|
|
d_word_cnt++;
|
|
|
|
|
|
|
|
if (d_word_cnt == max_frame_len()) {
|
|
|
|
d_state = SEARCH_SYNC;
|
|
|
|
d_data_reg = 0;
|
|
|
|
|
|
|
|
if (d_erasure_cnt > 0) {
|
|
|
|
ret = decode_rs_8(d_8b_words, d_erasures_indexes, d_erasure_cnt,
|
|
|
|
255 - max_frame_len());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = decode_rs_8(d_8b_words, NULL, 0, 255 - max_frame_len());
|
|
|
|
}
|
|
|
|
if (ret > -1) {
|
|
|
|
uint8_t fox_id = d_8b_words[0] & 0x7;
|
|
|
|
if (is_spacecraft_valid(fox_id)) {
|
|
|
|
metadata::add_pdu(status.data, d_8b_words,
|
|
|
|
amsat_fox_duv_frame_size);
|
|
|
|
metadata::add_symbol_erasures(status.data, d_erasure_cnt);
|
|
|
|
metadata::add_corrected_bits(status.data, ret);
|
|
|
|
metadata::add_time_iso8601(status.data);
|
|
|
|
status.decode_success = true;
|
|
|
|
d_bitstream.erase(d_bitstream.begin(),
|
|
|
|
d_bitstream.begin() + (i + 1) * 10 + 1);
|
|
|
|
return status;
|
2019-09-02 19:20:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
/* Frame could not be decoded. Retry to sync */
|
|
|
|
cont = true;
|
|
|
|
break;
|
2019-09-02 19:20:21 +02:00
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
}
|
2019-09-02 19:20:21 +02:00
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
if (cont) {
|
|
|
|
continue;
|
2019-09-02 19:20:21 +02:00
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
return status;
|
|
|
|
default:
|
|
|
|
throw std::invalid_argument(
|
|
|
|
"amsat_duv_decoder: Invalid decoding state");
|
|
|
|
}
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
amsat_duv_decoder::reset()
|
2019-09-01 22:32:59 +02:00
|
|
|
{
|
|
|
|
d_erasure_cnt = 0;
|
|
|
|
d_word_cnt = 0;
|
2019-09-02 19:20:21 +02:00
|
|
|
d_state = SEARCH_SYNC;
|
2019-09-12 15:25:10 +02:00
|
|
|
d_bitstream.clear();
|
2019-09-02 19:20:21 +02:00
|
|
|
d_bitstream_idx = 0;
|
2019-09-01 22:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace satnogs */
|
|
|
|
} /* namespace gr */
|
|
|
|
|