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

View file

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

View file

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

View file

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

View file

@ -44,7 +44,7 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) {
rtc::RTC received_at; rtc::RTC received_at;
rtcGetTime(&RTCD1, &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); const auto hex_formatted = format_manchester(decoder);
if( log_file.is_ready() ) { if( log_file.is_ready() ) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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