portapack-mayhem/firmware/common/ert_packet.cpp

120 lines
3.0 KiB
C++
Raw Normal View History

2015-12-08 19:04:48 -05:00
/*
* 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.
*/
#include "ert_packet.hpp"
#include "crc.hpp"
namespace ert {
size_t Packet::length() const {
return decoder_.symbols_count();
2015-12-08 19:04:48 -05:00
}
bool Packet::is_valid() const {
return true;
2015-12-08 19:04:48 -05:00
}
Timestamp Packet::received_at() const {
return packet_.timestamp();
2015-12-08 19:04:48 -05:00
}
Packet::Type Packet::type() const {
return type_;
2015-12-08 19:04:48 -05:00
}
ID Packet::id() const {
if (type() == Type::SCM) {
const auto msb = reader_.read(0, 2);
const auto lsb = reader_.read(35, 24);
return (msb << 24) | lsb;
}
if (type() == Type::SCMPLUS) {
return reader_.read(2 * 8, 32);
}
if (type() == Type::IDM) {
return reader_.read(5 * 8, 32);
}
return invalid_id;
2015-12-08 19:04:48 -05:00
}
Consumption Packet::consumption() const {
if (type() == Type::SCM) {
return reader_.read(11, 24);
}
if (type() == Type::SCMPLUS) {
return reader_.read(6 * 8, 32);
}
if (type() == Type::IDM) {
return reader_.read(25 * 8, 32);
}
return invalid_consumption;
2015-12-08 19:04:48 -05:00
}
2016-04-06 19:16:10 -04:00
CommodityType Packet::commodity_type() const {
if (type() == Type::SCM) {
return reader_.read(5, 4);
}
if (type() == Type::SCMPLUS) {
return reader_.read(1 * 8 + 4, 4);
}
if (type() == Type::IDM) {
return reader_.read(4 * 8 + 4, 4);
}
return invalid_commodity_type;
2016-04-06 19:16:10 -04:00
}
FormattedSymbols Packet::symbols_formatted() const {
return format_symbols(decoder_);
2015-12-08 19:04:48 -05:00
}
bool Packet::crc_ok() const {
switch (type()) {
case Type::SCM:
return crc_ok_scm();
case Type::SCMPLUS:
case Type::IDM:
return crc_ok_ccitt();
default:
return false;
}
2015-12-08 19:04:48 -05:00
}
bool Packet::crc_ok_scm() const {
CRC<16> ert_bch{0x6f63};
size_t start_bit = 5;
ert_bch.process_byte(reader_.read(0, start_bit));
for (size_t i = start_bit; i < length(); i += 8) {
ert_bch.process_byte(reader_.read(i, 8));
}
return ert_bch.checksum() == 0x0000;
2015-12-08 19:04:48 -05:00
}
2023-04-23 02:29:51 -04:00
bool Packet::crc_ok_ccitt() const {
CRC<16> ert_crc_ccitt{0x1021, 0xffff, 0x1d0f};
for (size_t i = 0; i < length(); i += 8) {
ert_crc_ccitt.process_byte(reader_.read(i, 8));
}
return ert_crc_ccitt.checksum() == 0x0000;
2015-12-08 19:04:48 -05:00
}
} /* namespace ert */