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;
};