2019-09-01 16:09:35 +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/ax25_decoder.h>
|
|
|
|
#include <satnogs/ax25.h>
|
|
|
|
#include <satnogs/metadata.h>
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
namespace gr {
|
|
|
|
namespace satnogs {
|
2019-09-01 16:09:35 +02:00
|
|
|
|
|
|
|
decoder::decoder_sptr
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::make(const std::string &addr, uint8_t ssid, bool promisc,
|
|
|
|
bool descramble, bool crc_check, size_t max_frame_len)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
2019-09-12 15:25:10 +02:00
|
|
|
return decoder::decoder_sptr(
|
|
|
|
new ax25_decoder(addr, ssid, promisc, descramble, crc_check,
|
|
|
|
max_frame_len));
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::ax25_decoder(const std::string &addr, uint8_t ssid, bool promisc,
|
|
|
|
bool descramble, bool crc_check, size_t max_frame_len) :
|
|
|
|
decoder(sizeof(uint8_t), 2 * max_frame_len * 8),
|
|
|
|
d_promisc(promisc),
|
|
|
|
d_descramble(descramble),
|
|
|
|
d_crc_check(crc_check),
|
|
|
|
d_max_frame_len(max_frame_len),
|
|
|
|
d_state(NO_SYNC),
|
|
|
|
d_shift_reg(0x0),
|
|
|
|
d_dec_b(0x0),
|
|
|
|
d_prev_bit_nrzi(0),
|
|
|
|
d_received_bytes(0),
|
|
|
|
d_decoded_bits(0),
|
|
|
|
d_lfsr(0x21, 0x0, 16),
|
|
|
|
d_frame_buffer(
|
|
|
|
new uint8_t[max_frame_len + AX25_MAX_ADDR_LEN + AX25_MAX_CTRL_LEN
|
|
|
|
+ sizeof(uint16_t)]),
|
|
|
|
d_start_idx(0),
|
2019-09-19 14:24:23 +02:00
|
|
|
d_frame_start(0),
|
|
|
|
d_sample_cnt(0)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_status_t
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::decode(const void *in, int len)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
const uint8_t *input = (const uint8_t *) in;
|
|
|
|
decoder_status_t status;
|
|
|
|
if (d_descramble) {
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
/* Perform NRZI decoding */
|
|
|
|
uint8_t b = (~((input[i] - d_prev_bit_nrzi) % 2)) & 0x1;
|
|
|
|
d_prev_bit_nrzi = input[i];
|
2019-09-12 15:25:10 +02:00
|
|
|
b = d_lfsr.next_bit_descramble(b);
|
|
|
|
d_bitstream.push_back(b);
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
/* Perform NRZI decoding */
|
|
|
|
uint8_t b = (~((input[i] - d_prev_bit_nrzi) % 2)) & 0x1;
|
|
|
|
d_prev_bit_nrzi = input[i];
|
2019-09-12 15:25:10 +02:00
|
|
|
d_bitstream.push_back(b);
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The decoder always consumes all the available input bits and stores them
|
|
|
|
* internally
|
|
|
|
*/
|
|
|
|
_decode(status);
|
|
|
|
status.consumed = len;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::reset()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
reset_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::_decode(decoder_status_t &status)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
bool cont = false;
|
2019-09-12 15:25:10 +02:00
|
|
|
switch (d_state) {
|
|
|
|
case NO_SYNC:
|
|
|
|
for (size_t i = 0; i < d_bitstream.size(); i++) {
|
|
|
|
decode_1b(d_bitstream[i]);
|
2019-10-03 15:54:37 +02:00
|
|
|
/*
|
|
|
|
* In case of scrambling the self synchronizing scrambler ensures
|
|
|
|
* that enough repetitions of the AX.25 flag have been received.
|
|
|
|
* However, this does not hold for the case of non scrambled
|
|
|
|
* transmissions. In this case, we wait for at least two consecutive
|
|
|
|
* AX.25 flags to reduce the false alarms. Experiments have shown
|
|
|
|
* that due to the poor CRC there were many false positive frames.
|
|
|
|
*
|
|
|
|
* It is expected however, to miss some transmissions that use only one
|
|
|
|
* AX.25 flag. We believe that such transmissions are yet rare.
|
|
|
|
*/
|
|
|
|
bool have_sync = false;
|
2019-12-27 18:51:54 +01:00
|
|
|
have_sync = ((d_shift_reg & 0xFF) == AX25_SYNC_FLAG)
|
|
|
|
&& (d_shift_reg >> 8) == AX25_SYNC_FLAG;
|
2019-10-03 15:54:37 +02:00
|
|
|
if (have_sync) {
|
2019-09-12 15:25:10 +02:00
|
|
|
d_bitstream.erase(d_bitstream.begin(),
|
|
|
|
d_bitstream.begin() + i + 1);
|
2019-09-19 14:24:23 +02:00
|
|
|
/* Increment the number of items read so far */
|
2019-09-19 20:09:22 +02:00
|
|
|
incr_nitems_read(i + 1);
|
2019-09-12 15:25:10 +02:00
|
|
|
enter_sync_state();
|
2019-09-19 14:24:23 +02:00
|
|
|
/* Mark possible start of the frame */
|
|
|
|
d_frame_start = nitems_read();
|
2019-09-12 15:25:10 +02:00
|
|
|
d_start_idx = 0;
|
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cont) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-19 14:24:23 +02:00
|
|
|
incr_nitems_read(d_bitstream.size());
|
2019-09-12 15:25:10 +02:00
|
|
|
d_bitstream.clear();
|
|
|
|
return false;
|
|
|
|
case IN_SYNC:
|
|
|
|
/*
|
|
|
|
* Most of the transmitters repeat several times the AX.25 SYNC
|
|
|
|
* In case of G3RUH this is mandatory to allow the self synchronizing
|
|
|
|
* scrambler to settle
|
|
|
|
*/
|
|
|
|
for (size_t i = d_start_idx; i < d_bitstream.size(); i++) {
|
|
|
|
decode_1b(d_bitstream[i]);
|
|
|
|
d_decoded_bits++;
|
|
|
|
if (d_decoded_bits == 8) {
|
|
|
|
/* Perhaps we are in frame! */
|
2019-10-03 15:54:37 +02:00
|
|
|
if ((d_shift_reg >> 8) != AX25_SYNC_FLAG) {
|
2019-09-12 15:25:10 +02:00
|
|
|
d_start_idx = i + 1;
|
|
|
|
enter_decoding_state();
|
2019-09-01 16:09:35 +02:00
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
d_decoded_bits = 0;
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
}
|
|
|
|
if (cont) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
d_start_idx = d_bitstream.size();
|
|
|
|
return false;
|
|
|
|
case DECODING:
|
|
|
|
for (size_t i = d_start_idx; i < d_bitstream.size(); i++) {
|
|
|
|
decode_1b(d_bitstream[i]);
|
2019-10-03 15:54:37 +02:00
|
|
|
if ((d_shift_reg >> 8) == AX25_SYNC_FLAG) {
|
2019-09-19 14:24:23 +02:00
|
|
|
d_sample_cnt = nitems_read() + i - d_frame_start;
|
2019-09-12 15:25:10 +02:00
|
|
|
LOG_DEBUG("Found frame end");
|
|
|
|
if (enter_frame_end(status)) {
|
|
|
|
d_bitstream.erase(d_bitstream.begin(),
|
|
|
|
d_bitstream.begin() + i + 1);
|
2019-09-19 14:24:23 +02:00
|
|
|
/* Increment the number of items read so far */
|
2019-09-19 20:09:22 +02:00
|
|
|
incr_nitems_read(i + 1);
|
2019-09-12 15:25:10 +02:00
|
|
|
d_start_idx = d_bitstream.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
cont = true;
|
|
|
|
break;
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
2019-10-03 15:54:37 +02:00
|
|
|
else if (((d_shift_reg >> 8) & 0xfc) == 0x7c) {
|
2019-09-12 15:25:10 +02:00
|
|
|
/*This was a stuffed bit */
|
|
|
|
d_dec_b <<= 1;
|
|
|
|
}
|
2019-10-03 15:54:37 +02:00
|
|
|
else if (((d_shift_reg >> 8) & 0xfe) == 0xfe) {
|
2019-09-12 15:25:10 +02:00
|
|
|
LOG_DEBUG("Invalid shift register value %u", d_received_bytes);
|
|
|
|
reset_state();
|
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2019-09-01 16:09:35 +02:00
|
|
|
d_decoded_bits++;
|
|
|
|
if (d_decoded_bits == 8) {
|
2019-09-12 15:25:10 +02:00
|
|
|
d_frame_buffer[d_received_bytes++] = d_dec_b;
|
2019-09-01 16:09:35 +02:00
|
|
|
d_decoded_bits = 0;
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
/*Check if the frame limit was reached */
|
|
|
|
if (d_received_bytes >= d_max_frame_len) {
|
|
|
|
LOG_DEBUG("Wrong size");
|
|
|
|
reset_state();
|
|
|
|
cont = true;
|
|
|
|
break;
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
if (cont) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
d_start_idx = d_bitstream.size();
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
LOG_ERROR("Invalid decoding state");
|
|
|
|
reset_state();
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-01 16:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::~ax25_decoder()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
delete[] d_frame_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::reset_state()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
d_state = NO_SYNC;
|
|
|
|
d_dec_b = 0x0;
|
|
|
|
d_shift_reg = 0x0;
|
|
|
|
d_decoded_bits = 0;
|
|
|
|
d_received_bytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::enter_sync_state()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
d_state = IN_SYNC;
|
|
|
|
d_dec_b = 0x0;
|
|
|
|
d_shift_reg = 0x0;
|
|
|
|
d_decoded_bits = 0;
|
|
|
|
d_received_bytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::enter_decoding_state()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
uint8_t tmp;
|
|
|
|
d_state = DECODING;
|
|
|
|
d_decoded_bits = 0;
|
|
|
|
d_received_bytes = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Due to the possibility of bit stuffing on the first byte some special
|
|
|
|
* handling is necessary
|
|
|
|
*/
|
2019-10-03 15:54:37 +02:00
|
|
|
if (((d_shift_reg >> 8) & 0xfc) == 0x7c) {
|
2019-09-01 16:09:35 +02:00
|
|
|
/*This was a stuffed bit */
|
|
|
|
d_dec_b <<= 1;
|
|
|
|
d_decoded_bits = 7;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d_frame_buffer[0] = d_dec_b;
|
|
|
|
d_decoded_bits = 0;
|
|
|
|
d_received_bytes = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::enter_frame_end(decoder_status_t &status)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
uint16_t fcs;
|
|
|
|
uint16_t recv_fcs = 0x0;
|
|
|
|
|
|
|
|
/* First check if the size of the frame is valid */
|
|
|
|
if (d_received_bytes < AX25_MIN_ADDR_LEN + sizeof(uint16_t)) {
|
2019-09-12 15:25:10 +02:00
|
|
|
reset_state();
|
2019-09-01 16:09:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the frame is correct using the FCS field
|
|
|
|
* Using this field also try to correct up to 2 error bits
|
|
|
|
*/
|
2019-09-12 15:25:10 +02:00
|
|
|
if (frame_check()) {
|
2019-09-19 14:55:45 +02:00
|
|
|
metadata::add_pdu(status.data, d_frame_buffer,
|
|
|
|
d_received_bytes - sizeof(uint16_t));
|
2019-09-01 21:30:57 +02:00
|
|
|
metadata::add_time_iso8601(status.data);
|
|
|
|
metadata::add_crc_valid(status.data, true);
|
|
|
|
metadata::add_sample_start(status.data, d_frame_start);
|
2019-09-19 14:24:23 +02:00
|
|
|
metadata::add_sample_cnt(status.data, d_sample_cnt);
|
2019-09-01 16:09:35 +02:00
|
|
|
status.decode_success = true;
|
2019-09-12 15:25:10 +02:00
|
|
|
reset_state();
|
2019-09-01 16:09:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-12 15:25:10 +02:00
|
|
|
else if (!d_crc_check) {
|
2019-09-19 14:55:45 +02:00
|
|
|
metadata::add_pdu(status.data, d_frame_buffer,
|
|
|
|
d_received_bytes - sizeof(uint16_t));
|
2019-09-01 21:30:57 +02:00
|
|
|
metadata::add_time_iso8601(status.data);
|
|
|
|
metadata::add_crc_valid(status.data, false);
|
2019-09-01 16:09:35 +02:00
|
|
|
status.decode_success = true;
|
|
|
|
LOG_DEBUG("Wrong crc");
|
2019-09-12 15:25:10 +02:00
|
|
|
reset_state();
|
2019-09-01 16:09:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::decode_1b(uint8_t in)
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* In AX.25 the LS bit is sent first */
|
2019-10-03 15:54:37 +02:00
|
|
|
d_shift_reg = (d_shift_reg >> 1) | (in << 15);
|
2019-09-01 16:09:35 +02:00
|
|
|
d_dec_b = (d_dec_b >> 1) | (in << 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-12 15:25:10 +02:00
|
|
|
ax25_decoder::frame_check()
|
2019-09-01 16:09:35 +02:00
|
|
|
{
|
|
|
|
uint16_t fcs;
|
|
|
|
uint16_t recv_fcs = 0x0;
|
|
|
|
uint8_t orig_byte;
|
|
|
|
|
|
|
|
/* Check if the frame is correct using the FCS field */
|
2019-09-12 15:25:10 +02:00
|
|
|
fcs = ax25_fcs(d_frame_buffer, d_received_bytes - sizeof(uint16_t));
|
2019-09-01 16:09:35 +02:00
|
|
|
recv_fcs = (((uint16_t) d_frame_buffer[d_received_bytes - 1]) << 8)
|
2019-09-12 15:25:10 +02:00
|
|
|
| d_frame_buffer[d_received_bytes - 2];
|
2019-09-01 16:09:35 +02:00
|
|
|
if (fcs == recv_fcs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace satnogs */
|
|
|
|
} /* namespace gr */
|
|
|
|
|