From 51026609ce0fbfe131329ddddc479eabf66a9a46 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sun, 6 Dec 2015 15:31:17 -0800 Subject: [PATCH] ert::Packet parser. --- firmware/application/app_ert.cpp | 42 +++++++++++++++++++ firmware/application/app_ert.hpp | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/firmware/application/app_ert.cpp b/firmware/application/app_ert.cpp index 755bbcfe..fe017437 100644 --- a/firmware/application/app_ert.cpp +++ b/firmware/application/app_ert.cpp @@ -29,6 +29,48 @@ using namespace portapack; #include "lpc43xx_cpp.hpp" using namespace lpc43xx; +namespace ert { + +size_t Packet::length() const { + return payload_length_; +} + +bool Packet::is_valid() const { + return true; +} + +rtc::RTC Packet::received_at() const { + return received_at_; +} + +Packet::Type Packet::type() const { + return type_; +} + +ID Packet::id() const { + if( type() == Packet::Type::SCM ) { + const auto msb = reader_.read(0, 2); + const auto lsb = reader_.read(35, 24); + return (msb << 24) | lsb; + } + if( type() == Packet::Type::IDM ) { + return reader_.read(5 * 8, 32); + } + return invalid_id; +} + +Consumption Packet::consumption() const { + if( type() == Packet::Type::SCM ) { + return reader_.read(11, 24); + } + if( type() == Packet::Type::IDM ) { + return reader_.read(25 * 8, 32); + } + return invalid_consumption; +} + +} /* namespace ert */ + ERTModel::ERTModel() { receiver_model.set_baseband_configuration({ .mode = 6, diff --git a/firmware/application/app_ert.hpp b/firmware/application/app_ert.hpp index 8bb46138..ebc26864 100644 --- a/firmware/application/app_ert.hpp +++ b/firmware/application/app_ert.hpp @@ -25,8 +25,79 @@ #include "ui_console.hpp" #include "message.hpp" #include "log_file.hpp" +#include "manchester.hpp" +#include "field_reader.hpp" +#include "lpc43xx_cpp.hpp" +using namespace lpc43xx; + +#include #include +#include + +namespace ert { + +struct BitRemap { + size_t operator()(const size_t bit_index) const { + return bit_index; + } +}; + +using ID = uint32_t; +using Consumption = uint32_t; + +class Packet { +public: + enum Type { + Unknown = 0, + IDM = 1, + SCM = 2, + }; + + Packet( + const rtc::RTC& received_at, + const uint64_t preamble, + const std::bitset<1024>& payload, + const size_t payload_length + ) : payload_ { payload }, + payload_length_ { payload_length }, + received_at_ { received_at }, + decoder_ { payload_, payload_length_ }, + reader_ { decoder_ }, + type_ { Type::Unknown } + { + if( preamble == 0x1f2a60 ) { + type_ = Type::SCM; + } else if( preamble == 0x555516a3 ) { + type_ = Type::IDM; + } + } + + size_t length() const; + + bool is_valid() const; + + rtc::RTC received_at() const; + + Type type() const; + ID id() const; + Consumption consumption() const; + +private: + using Reader = FieldReader; + + const std::bitset<1024> payload_; + const size_t payload_length_; + const rtc::RTC received_at_; + const ManchesterDecoder decoder_; + const Reader reader_; + Type type_; + + const ID invalid_id = 0; + const Consumption invalid_consumption = 0; +}; + +} /* namespace ert */ class ERTModel { public: