2010-10-19 00:59:08 +08:00
|
|
|
/*
|
|
|
|
# Copyright 2010 Nick Foster
|
2013-04-02 06:51:00 +08:00
|
|
|
# Copyright 2013 Nicholas Corgan
|
2010-10-19 00:59:08 +08:00
|
|
|
#
|
|
|
|
# This file is part of gr-air-modes
|
|
|
|
#
|
|
|
|
# gr-air-modes 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, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# gr-air-modes 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 gr-air-modes; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
*/
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-04-02 06:51:00 +08:00
|
|
|
#include <ciso646>
|
2010-09-15 13:01:56 +08:00
|
|
|
#include <air_modes_slicer.h>
|
2013-07-18 09:03:45 +08:00
|
|
|
#include <gnuradio/io_signature.h>
|
2010-09-15 13:01:56 +08:00
|
|
|
#include <air_modes_types.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2012-06-13 22:49:22 +08:00
|
|
|
#include <modes_crc.h>
|
2011-06-12 12:32:28 +08:00
|
|
|
#include <iostream>
|
2013-07-18 09:03:45 +08:00
|
|
|
#include <gnuradio/tags.h>
|
2010-09-15 13:01:56 +08:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
air_modes_slicer_sptr air_make_modes_slicer(int channel_rate, gr::msg_queue::sptr queue)
|
2010-09-15 13:01:56 +08:00
|
|
|
{
|
|
|
|
return air_modes_slicer_sptr (new air_modes_slicer(channel_rate, queue));
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
air_modes_slicer::air_modes_slicer(int channel_rate, gr::msg_queue::sptr queue) :
|
|
|
|
gr::sync_block ("modes_slicer",
|
|
|
|
gr::io_signature::make (1, 1, sizeof(float)), //stream 0 is received data, stream 1 is binary preamble detector output
|
|
|
|
gr::io_signature::make (0, 0, 0) )
|
2013-06-06 03:56:09 +08:00
|
|
|
{
|
2013-07-18 09:03:45 +08:00
|
|
|
//initialize private data here
|
2010-09-15 13:01:56 +08:00
|
|
|
d_chip_rate = 2000000; //2Mchips per second
|
2011-06-12 12:32:28 +08:00
|
|
|
d_samples_per_chip = 2;//FIXME this is constant now channel_rate / d_chip_rate;
|
2010-09-15 13:01:56 +08:00
|
|
|
d_samples_per_symbol = d_samples_per_chip * 2;
|
2013-06-06 05:50:10 +08:00
|
|
|
d_check_width = 120 * d_samples_per_symbol; //how far you will have to look ahead
|
2013-07-18 09:03:45 +08:00
|
|
|
d_queue = queue;
|
|
|
|
|
2013-06-06 05:50:10 +08:00
|
|
|
set_output_multiple(d_check_width*2); //how do you specify buffer size for sinks?
|
2010-09-15 13:01:56 +08:00
|
|
|
}
|
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
//this slicer is courtesy of Lincoln Labs. supposedly it is more resistant to mode A/C FRUIT.
|
|
|
|
//see http://adsb.tc.faa.gov/WG3_Meetings/Meeting8/Squitter-Lon.pdf
|
2011-06-12 12:40:51 +08:00
|
|
|
static slice_result_t slicer(const float bit0, const float bit1, const float ref) {
|
|
|
|
slice_result_t result;
|
2011-06-12 12:32:28 +08:00
|
|
|
|
|
|
|
//3dB limits for bit slicing and confidence measurement
|
2012-10-14 11:52:38 +08:00
|
|
|
float highlimit=ref*1.414;
|
|
|
|
float lowlimit=ref*0.707;
|
2011-06-12 12:32:28 +08:00
|
|
|
|
|
|
|
bool firstchip_inref = ((bit0 > lowlimit) && (bit0 < highlimit));
|
|
|
|
bool secondchip_inref = ((bit1 > lowlimit) && (bit1 < highlimit));
|
|
|
|
|
|
|
|
if(firstchip_inref && !secondchip_inref) {
|
2011-06-12 12:40:51 +08:00
|
|
|
result.decision = 1;
|
|
|
|
result.confidence = 1;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
|
|
|
else if(secondchip_inref && !firstchip_inref) {
|
2011-06-12 12:40:51 +08:00
|
|
|
result.decision = 0;
|
|
|
|
result.confidence = 1;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
|
|
|
else if(firstchip_inref && secondchip_inref) {
|
2011-06-12 12:40:51 +08:00
|
|
|
result.decision = bit0 > bit1;
|
|
|
|
result.confidence = 0;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
2011-06-12 15:56:22 +08:00
|
|
|
else {//if(!firstchip_inref && !secondchip_inref) {
|
2011-06-12 12:40:51 +08:00
|
|
|
result.decision = bit0 > bit1;
|
|
|
|
if(result.decision) {
|
|
|
|
if(bit1 < lowlimit * 0.5) result.confidence = 1;
|
|
|
|
else result.confidence = 0;
|
2011-06-12 12:32:28 +08:00
|
|
|
} else {
|
2011-06-12 12:40:51 +08:00
|
|
|
if(bit0 < lowlimit * 0.5) result.confidence = 1;
|
|
|
|
else result.confidence = 0;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-12 12:40:51 +08:00
|
|
|
return result;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
int air_modes_slicer::work(int noutput_items,
|
|
|
|
gr_vector_const_void_star &input_items,
|
|
|
|
gr_vector_void_star &output_items)
|
|
|
|
{
|
2011-06-12 12:32:28 +08:00
|
|
|
const float *in = (const float *) input_items[0];
|
2010-09-15 13:01:56 +08:00
|
|
|
int size = noutput_items - d_check_width; //since it's a sync block, i assume that it runs with ninput_items = noutput_items
|
2012-08-24 00:03:56 +08:00
|
|
|
|
|
|
|
if(0) std::cout << "Slicer called with " << size << " samples" << std::endl;
|
2010-11-21 09:06:48 +08:00
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
std::vector<gr::tag_t> tags;
|
2010-11-21 09:06:48 +08:00
|
|
|
uint64_t abs_sample_cnt = nitems_read(0);
|
2013-07-18 09:03:45 +08:00
|
|
|
get_tags_in_range(tags, 0, abs_sample_cnt, abs_sample_cnt + size, pmt::string_to_symbol("preamble_found"));
|
|
|
|
std::vector<gr::tag_t>::iterator tag_iter;
|
2011-08-25 09:19:57 +08:00
|
|
|
|
2010-11-21 09:06:48 +08:00
|
|
|
for(tag_iter = tags.begin(); tag_iter != tags.end(); tag_iter++) {
|
2011-11-18 07:16:56 +08:00
|
|
|
uint64_t i = tag_iter->offset - abs_sample_cnt;
|
2010-11-21 09:06:48 +08:00
|
|
|
modes_packet rx_packet;
|
|
|
|
|
|
|
|
memset(&rx_packet.data, 0x00, 14 * sizeof(unsigned char));
|
|
|
|
memset(&rx_packet.lowconfbits, 0x00, 24 * sizeof(unsigned char));
|
|
|
|
rx_packet.numlowconf = 0;
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
//let's use the preamble to get a reference level for the packet
|
|
|
|
//fixme: a better thing to do is create a bi-level avg 1 and avg 0
|
|
|
|
//through simple statistics, then take the median for your slice level
|
|
|
|
//this won't improve decoding but will improve confidence
|
|
|
|
rx_packet.reference_level = (in[i]
|
|
|
|
+ in[i+2]
|
|
|
|
+ in[i+7]
|
|
|
|
+ in[i+9]) / 4.0;
|
|
|
|
|
|
|
|
i += 16; //move on up to the first bit of the packet data
|
|
|
|
//now let's slice the header so we can determine if it's a short pkt or a long pkt
|
|
|
|
unsigned char pkt_hdr = 0;
|
|
|
|
for(int j=0; j < 5; j++) {
|
2011-06-12 12:40:51 +08:00
|
|
|
slice_result_t slice_result = slicer(in[i+j*2], in[i+j*2+1], rx_packet.reference_level);
|
|
|
|
if(slice_result.decision) pkt_hdr += 1 << (4-j);
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
2012-10-18 09:28:39 +08:00
|
|
|
if(pkt_hdr == 16 or pkt_hdr == 17 or pkt_hdr == 20 or pkt_hdr == 21) rx_packet.type = Long_Packet;
|
2011-06-12 12:32:28 +08:00
|
|
|
else rx_packet.type = Short_Packet;
|
2011-06-12 13:07:20 +08:00
|
|
|
int packet_length = (rx_packet.type == framer_packet_type(Short_Packet)) ? 56 : 112;
|
2010-11-21 09:06:48 +08:00
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
//it's slice time!
|
|
|
|
//TODO: don't repeat your work here, you already have the first 5 bits
|
2010-11-21 09:06:48 +08:00
|
|
|
for(int j = 0; j < packet_length; j++) {
|
2011-06-12 12:40:51 +08:00
|
|
|
slice_result_t slice_result = slicer(in[i+j*2], in[i+j*2+1], rx_packet.reference_level);
|
2011-06-12 12:32:28 +08:00
|
|
|
|
|
|
|
//put the data into the packet
|
2011-06-12 12:40:51 +08:00
|
|
|
if(slice_result.decision) {
|
2010-11-21 09:06:48 +08:00
|
|
|
rx_packet.data[j/8] += 1 << (7-(j%8));
|
|
|
|
}
|
|
|
|
//put the confidence decision into the packet
|
2011-06-12 12:40:51 +08:00
|
|
|
if(slice_result.confidence) {
|
2010-11-21 09:06:48 +08:00
|
|
|
//rx_packet.confidence[j/8] += 1 << (7-(j%8));
|
2011-06-12 12:40:51 +08:00
|
|
|
} else {
|
|
|
|
if(rx_packet.numlowconf < 24) rx_packet.lowconfbits[rx_packet.numlowconf++] = j;
|
|
|
|
}
|
2010-11-21 09:06:48 +08:00
|
|
|
}
|
2010-11-19 06:25:07 +08:00
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
/******************** BEGIN TIMESTAMP BS ******************/
|
|
|
|
rx_packet.timestamp = pmt::to_double(tag_iter->value);
|
|
|
|
/******************* END TIMESTAMP BS *********************/
|
|
|
|
|
|
|
|
//increment for the next round
|
|
|
|
|
|
|
|
//here you might want to traverse the whole packet and if you find all 0's, just toss it. don't know why these packets turn up, but they pass ECC.
|
2010-11-21 09:06:48 +08:00
|
|
|
bool zeroes = 1;
|
|
|
|
for(int m = 0; m < 14; m++) {
|
2013-07-18 09:03:45 +08:00
|
|
|
if(rx_packet.data[m]) zeroes = 0;
|
2010-11-21 09:06:48 +08:00
|
|
|
}
|
2011-06-12 16:14:49 +08:00
|
|
|
if(zeroes) {continue;} //toss it
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-06-22 00:42:58 +08:00
|
|
|
rx_packet.message_type = (rx_packet.data[0] >> 3) & 0x1F; //get the message type to make decisions on ECC methods
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2011-06-12 16:14:49 +08:00
|
|
|
if(rx_packet.type == Short_Packet && rx_packet.message_type != 11 && rx_packet.numlowconf > 0) {continue;}
|
|
|
|
if(rx_packet.message_type == 11 && rx_packet.numlowconf >= 10) {continue;}
|
2010-10-18 10:45:19 +08:00
|
|
|
|
2012-06-13 22:49:22 +08:00
|
|
|
rx_packet.crc = modes_check_crc(rx_packet.data, packet_length);
|
2010-11-21 09:06:48 +08:00
|
|
|
|
2012-06-13 22:49:22 +08:00
|
|
|
//crc for packets that aren't type 11 or type 17 is encoded with the transponder ID, which we don't know
|
2011-06-12 15:56:22 +08:00
|
|
|
//therefore we toss 'em if there's syndrome
|
2012-06-13 22:49:22 +08:00
|
|
|
//crc for the other short packets is usually nonzero, so they can't really be trusted that far
|
|
|
|
if(rx_packet.crc && (rx_packet.message_type == 11 || rx_packet.message_type == 17)) {continue;}
|
2010-11-21 09:06:48 +08:00
|
|
|
|
|
|
|
d_payload.str("");
|
2012-06-24 08:26:47 +08:00
|
|
|
for(int m = 0; m < packet_length/8; m++) {
|
|
|
|
d_payload << std::hex << std::setw(2) << std::setfill('0') << unsigned(rx_packet.data[m]);
|
2010-11-21 09:06:48 +08:00
|
|
|
}
|
2012-06-22 00:42:58 +08:00
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
d_payload << " " << std::setw(6) << rx_packet.crc << " " << std::dec << rx_packet.reference_level
|
2011-05-17 09:06:54 +08:00
|
|
|
<< " " << std::setprecision(10) << std::setw(10) << rx_packet.timestamp;
|
2013-07-18 09:03:45 +08:00
|
|
|
gr::message::sptr msg = gr::message::make_from_string(std::string(d_payload.str()));
|
2010-11-21 09:06:48 +08:00
|
|
|
d_queue->handle(msg);
|
2010-09-15 13:01:56 +08:00
|
|
|
}
|
2012-08-24 00:03:56 +08:00
|
|
|
if(0) std::cout << "Slicer consumed " << size << ", returned " << size << std::endl;
|
2010-09-15 13:01:56 +08:00
|
|
|
return size;
|
|
|
|
}
|