Abstract packet type/implementation details.

This commit is contained in:
Jared Boone 2015-12-07 12:35:05 -08:00
parent b9ea7fa786
commit bd33e652ea
16 changed files with 129 additions and 90 deletions

View File

@ -39,7 +39,7 @@ struct CRCBitRemap {
}
};
using CRCFieldReader = ::FieldReader<std::bitset<1024>, CRCBitRemap>;
using CRCFieldReader = ::FieldReader<::Packet, CRCBitRemap>;
struct PacketLengthRange {
constexpr PacketLengthRange(
@ -131,8 +131,11 @@ struct PacketTooLong {
};
struct CRCCheck {
bool operator()(const std::bitset<1024>& payload, const size_t data_length) {
CRCFieldReader field_crc { payload };
bool operator()(const ::Packet& packet) {
const size_t data_and_fcs_length = packet.size() - 7;
const size_t data_length = data_and_fcs_length - 16;
CRCFieldReader field_crc { packet };
CRC<uint16_t> ais_fcs { 0x1021 };
uint16_t crc_calculated = 0xffff;
@ -198,12 +201,12 @@ static char char_to_ascii(const uint8_t c) {
}
size_t Packet::length() const {
return payload_length_;
return packet_.size();
}
bool Packet::is_valid() const {
// Subtract end flag (8 bits) - one unstuffing bit (occurs during end flag).
const size_t data_and_fcs_length = payload_length_ - 7;
const size_t data_and_fcs_length = length() - 7;
if( data_and_fcs_length < 38 ) {
return false;
@ -221,7 +224,7 @@ bool Packet::is_valid() const {
}
CRCCheck crc_ok;
if( !crc_ok(payload_, data_length) ) {
if( !crc_ok(packet_) ) {
return false;
}
@ -334,7 +337,7 @@ void AISView::on_show() {
const auto message = static_cast<const AISPacketMessage*>(p);
rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime);
const baseband::ais::Packet packet { datetime, message->packet.payload, message->packet.bits_received };
const baseband::ais::Packet packet { datetime, message->packet.packet };
if( this->model.on_packet(packet) ) {
this->on_packet(packet);
}

View File

@ -26,6 +26,7 @@
#include "message.hpp"
#include "log_file.hpp"
#include "field_reader.hpp"
#include "packet.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
@ -48,7 +49,7 @@ struct BitRemap {
}
};
using FieldReader = ::FieldReader<std::bitset<1024>, BitRemap>;
using FieldReader = ::FieldReader<::Packet, BitRemap>;
struct DateTime {
uint16_t year;
@ -68,12 +69,10 @@ class Packet {
public:
constexpr Packet(
const rtc::RTC& received_at,
const std::bitset<1024>& payload,
const size_t payload_length
) : payload_ { payload },
payload_length_ { payload_length },
const ::Packet& packet
) : packet_ { packet },
received_at_ { received_at },
field_ { payload_ }
field_ { packet_ }
{
}
@ -97,8 +96,7 @@ public:
Longitude longitude(const size_t start_bit) const;
private:
const std::bitset<1024> payload_;
const size_t payload_length_;
const ::Packet packet_;
const rtc::RTC received_at_;
const FieldReader field_;
};

View File

@ -35,7 +35,7 @@ using namespace lpc43xx;
namespace ert {
size_t Packet::length() const {
return payload_length_;
return packet_.size();
}
bool Packet::is_valid() const {
@ -115,7 +115,7 @@ void ERTView::on_show() {
const auto message = static_cast<const ERTPacketMessage*>(p);
rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime);
const ert::Packet packet { datetime, message->packet.type, message->packet.payload, message->packet.bits_received };
const ert::Packet packet { datetime, message->packet.type, message->packet.packet };
if( this->model.on_packet(packet) ) {
this->on_packet(packet);
}

View File

@ -27,6 +27,7 @@
#include "log_file.hpp"
#include "manchester.hpp"
#include "field_reader.hpp"
#include "packet.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
@ -51,12 +52,10 @@ public:
Packet(
const rtc::RTC& received_at,
const ERTPacket::Type type,
const std::bitset<1024>& payload,
const size_t payload_length
) : payload_ { payload },
payload_length_ { payload_length },
const ::Packet& packet
) : packet_ { packet },
received_at_ { received_at },
decoder_ { payload_, payload_length_ },
decoder_ { packet_ },
reader_ { decoder_ },
type_ { type }
{
@ -77,8 +76,7 @@ public:
private:
using Reader = FieldReader<ManchesterDecoder, BitRemap>;
const std::bitset<1024> payload_;
const size_t payload_length_;
const ::Packet packet_;
const rtc::RTC received_at_;
const ManchesterDecoder decoder_;
const Reader reader_;

View File

@ -44,7 +44,7 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) {
rtc::RTC received_at;
rtcGetTime(&RTCD1, &received_at);
const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received, 1);
const ManchesterDecoder decoder(message.packet.packet, 1);
const auto hex_formatted = format_manchester(decoder);
if( log_file.is_ready() ) {

View File

@ -25,9 +25,9 @@
ManchesterDecoder::DecodedSymbol ManchesterDecoder::operator[](const size_t index) const {
const size_t encoded_index = index * 2;
if( (encoded_index + 1) < count ) {
const auto value = encoded[encoded_index + sense];
const auto error = encoded[encoded_index + 0] == encoded[encoded_index + 1];
if( (encoded_index + 1) < packet.size() ) {
const auto value = packet[encoded_index + sense];
const auto error = packet[encoded_index + 0] == packet[encoded_index + 1];
return { value, error };
} else {
return { 0, 1 };
@ -35,7 +35,7 @@ ManchesterDecoder::DecodedSymbol ManchesterDecoder::operator[](const size_t inde
}
size_t ManchesterDecoder::symbols_count() const {
return count / 2;
return packet.size() / 2;
}
ManchesterFormatted format_manchester(

View File

@ -27,6 +27,8 @@
#include <bitset>
#include <string>
#include "packet.hpp"
class ManchesterDecoder {
public:
struct DecodedSymbol {
@ -35,11 +37,9 @@ public:
};
constexpr ManchesterDecoder(
const std::bitset<1024>& encoded,
const size_t count,
const ::Packet& packet,
const size_t sense = 0
) : encoded { encoded },
count { count },
) : packet { packet },
sense { sense }
{
}
@ -49,8 +49,7 @@ public:
size_t symbols_count() const;
private:
const std::bitset<1024>& encoded;
const size_t count;
const ::Packet& packet;
const size_t sense;
};

View File

@ -28,6 +28,7 @@
#include <functional>
#include "bit_pattern.hpp"
#include "packet.hpp"
struct NeverMatch {
bool operator()(const BitHistory&, const size_t) const {
@ -46,8 +47,7 @@ struct FixedLength {
template<typename PreambleMatcher, typename UnstuffMatcher, typename EndMatcher>
class PacketBuilder {
public:
using PayloadType = std::bitset<1024>;
using PayloadHandlerFunc = std::function<void(const PayloadType& payload, const size_t bits_received)>;
using PayloadHandlerFunc = std::function<void(const ::Packet& packet)>;
PacketBuilder(
const PreambleMatcher preamble_matcher,
@ -78,18 +78,18 @@ public:
switch(state) {
case State::Preamble:
if( preamble(bit_history, bits_received) ) {
if( preamble(bit_history, packet.size()) ) {
state = State::Payload;
}
break;
case State::Payload:
if( !unstuff(bit_history, bits_received) ) {
payload[bits_received++] = symbol;
if( !unstuff(bit_history, packet.size()) ) {
packet.add(symbol);
}
if( end(bit_history, bits_received) ) {
payload_handler(payload, bits_received);
if( end(bit_history, packet.size()) ) {
payload_handler(packet);
reset_state();
} else {
if( packet_truncated() ) {
@ -111,7 +111,7 @@ private:
};
bool packet_truncated() const {
return bits_received >= payload.size();
return packet.size() >= packet.capacity();
}
const PayloadHandlerFunc payload_handler;
@ -121,12 +121,11 @@ private:
UnstuffMatcher unstuff;
EndMatcher end;
size_t bits_received { 0 };
State state { State::Preamble };
PayloadType payload;
::Packet packet;
void reset_state() {
bits_received = 0;
packet.clear();
state = State::Preamble;
}
};

View File

@ -60,11 +60,9 @@ void AISProcessor::consume_symbol(
}
void AISProcessor::payload_handler(
const std::bitset<1024>& payload,
const size_t bits_received
const ::Packet& packet
) {
AISPacketMessage message;
message.packet.payload = payload;
message.packet.bits_received = bits_received;
message.packet.packet = packet;
shared_memory.application_queue.push(message);
}

View File

@ -30,6 +30,7 @@
#include "clock_recovery.hpp"
#include "symbol_coding.hpp"
#include "packet_builder.hpp"
#include "packet.hpp"
#include "message.hpp"
@ -41,8 +42,6 @@
class AISProcessor : public BasebandProcessor {
public:
using payload_t = std::bitset<1024>;
void execute(buffer_c8_t buffer) override;
private:
@ -58,13 +57,13 @@ private:
{ 0b0101010101111110, 16, 1 },
{ 0b111110, 6 },
{ 0b01111110, 8 },
[this](const payload_t& payload, const size_t bits_received) {
this->payload_handler(payload, bits_received);
[this](const ::Packet& packet) {
this->payload_handler(packet);
}
};
void consume_symbol(const float symbol);
void payload_handler(const payload_t& payload, const size_t bits_received);
void payload_handler(const ::Packet& packet);
};
#endif/*__PROC_AIS_H__*/

View File

@ -95,23 +95,19 @@ void ERTProcessor::consume_symbol(
}
void ERTProcessor::scm_handler(
const std::bitset<1024>& payload,
const size_t bits_received
const ::Packet& packet
) {
ERTPacketMessage message;
message.packet.type = ERTPacket::Type::SCM;
message.packet.payload = payload;
message.packet.bits_received = bits_received;
message.packet.packet = packet;
shared_memory.application_queue.push(message);
}
void ERTProcessor::idm_handler(
const std::bitset<1024>& payload,
const size_t bits_received
const ::Packet& packet
) {
ERTPacketMessage message;
message.packet.type = ERTPacket::Type::IDM;
message.packet.payload = payload;
message.packet.bits_received = bits_received;
message.packet.packet = packet;
shared_memory.application_queue.push(message);
}

View File

@ -29,6 +29,7 @@
#include "clock_recovery.hpp"
#include "symbol_coding.hpp"
#include "packet_builder.hpp"
#include "packet.hpp"
#include "message.hpp"
@ -45,13 +46,10 @@ constexpr size_t scm_payload_length_max { 150 };
constexpr uint64_t idm_preamble_and_sync_manchester { 0b0110011001100110011001100110011001010110011010011001100101011010 };
constexpr size_t idm_preamble_and_sync_length { 64 - 16 };
// TODO: Should be 1408, but need to widen pathway to application processor...
constexpr size_t idm_payload_length_max { 960 };
constexpr size_t idm_payload_length_max { 1408 };
class ERTProcessor : public BasebandProcessor {
public:
using payload_t = std::bitset<1024>;
void execute(buffer_c8_t buffer) override;
private:
@ -74,8 +72,8 @@ private:
{ scm_preamble_and_sync_manchester, scm_preamble_and_sync_length, 1 },
{ },
{ scm_payload_length_max },
[this](const payload_t& payload, const size_t bits_received) {
this->scm_handler(payload, bits_received);
[this](const ::Packet& packet) {
this->scm_handler(packet);
}
};
@ -83,14 +81,14 @@ private:
{ idm_preamble_and_sync_manchester, idm_preamble_and_sync_length, 1 },
{ },
{ idm_payload_length_max },
[this](const payload_t& payload, const size_t bits_received) {
this->idm_handler(payload, bits_received);
[this](const ::Packet& packet) {
this->idm_handler(packet);
}
};
void consume_symbol(const float symbol);
void scm_handler(const payload_t& payload, const size_t bits_received);
void idm_handler(const payload_t& payload, const size_t bits_received);
void scm_handler(const ::Packet& packet);
void idm_handler(const ::Packet& packet);
float sum_half_period[2];
float sum_period[3];

View File

@ -58,11 +58,9 @@ void TPMSProcessor::consume_symbol(
}
void TPMSProcessor::payload_handler(
const std::bitset<1024>& payload,
const size_t bits_received
const ::Packet& packet
) {
TPMSPacketMessage message;
message.packet.payload = payload;
message.packet.bits_received = bits_received;
message.packet.packet = packet;
shared_memory.application_queue.push(message);
}

View File

@ -30,6 +30,7 @@
#include "clock_recovery.hpp"
#include "symbol_coding.hpp"
#include "packet_builder.hpp"
#include "packet.hpp"
#include "message.hpp"
@ -49,8 +50,6 @@ constexpr std::array<std::complex<float>, 8> rect_taps_153k6_1t_p { {
class TPMSProcessor : public BasebandProcessor {
public:
using payload_t = std::bitset<1024>;
void execute(buffer_c8_t buffer) override;
private:
@ -65,13 +64,13 @@ private:
{ 0b010101010101010101010101010110, 30, 1 },
{ },
{ 256 },
[this](const payload_t& payload, const size_t bits_received) {
this->payload_handler(payload, bits_received);
[this](const ::Packet& packet) {
this->payload_handler(packet);
}
};
void consume_symbol(const float symbol);
void payload_handler(const payload_t& payload, const size_t bits_received);
void payload_handler(const ::Packet& packet);
};
#endif/*__PROC_TPMS_H__*/

View File

@ -27,6 +27,8 @@
#include <array>
#include <functional>
#include "packet.hpp"
#include "utility.hpp"
#include "ch.h"
@ -201,11 +203,8 @@ public:
ChannelSpectrum spectrum;
};
#include <bitset>
struct AISPacket {
std::bitset<1024> payload;
size_t bits_received { 0 };
::Packet packet;
};
class AISPacketMessage : public Message {
@ -219,8 +218,7 @@ public:
};
struct TPMSPacket {
std::bitset<1024> payload;
size_t bits_received { 0 };
::Packet packet;
};
class TPMSPacketMessage : public Message {
@ -249,8 +247,7 @@ struct ERTPacket {
};
Type type { Type::Unknown };
std::bitset<1024> payload;
size_t bits_received { 0 };
::Packet packet;
};
class ERTPacketMessage : public Message {

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* 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 2, 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PACKET_H__
#define __PACKET_H__
#include <cstddef>
#include <bitset>
class Packet {
public:
void add(const bool symbol) {
if( count < capacity() ) {
data[count++] = symbol;
}
}
uint_fast8_t operator[](const size_t index) const {
return (index < size()) ? data[index] : 0;
}
size_t size() const {
return count;
}
size_t capacity() const {
return data.size();
}
void clear() {
count = 0;
}
private:
std::bitset<1408> data;
size_t count { 0 };
};
#endif/*__PACKET_H__*/