Clean up ERT CRC code, add IDM CRC checking.

This commit is contained in:
Jared Boone 2015-12-07 15:32:26 -08:00
parent ee1eadce5b
commit b61ed7dce2
2 changed files with 28 additions and 10 deletions

View File

@ -77,7 +77,14 @@ ManchesterFormatted Packet::symbols_formatted() const {
}
bool Packet::crc_ok() const {
if( type() == ERTPacket::Type::SCM ) {
switch(type()) {
case ERTPacket::Type::SCM: return crc_ok_scm();
case ERTPacket::Type::IDM: return crc_ok_idm();
default: return false;
}
}
bool Packet::crc_ok_scm() const {
CRC<uint16_t> ert_bch { 0x6f63 };
size_t start_bit = 5;
auto crc_calculated = ert_bch.calculate_byte(0x0000, reader_.read(0, start_bit));
@ -86,9 +93,16 @@ bool Packet::crc_ok() const {
crc_calculated = ert_bch.calculate_byte(crc_calculated, byte);
}
return crc_calculated == 0x0000;
}
}
return false;
bool Packet::crc_ok_idm() const {
CRC<uint16_t> ert_crc_ccitt { 0x1021 };
uint16_t crc_calculated = 0xffff;
for(size_t i=0; i<length(); i+=8) {
const uint8_t byte = reader_.read(i, 8);
crc_calculated = ert_crc_ccitt.calculate_byte(crc_calculated, byte);
}
return crc_calculated == 0x1d0f;
}
} /* namespace ert */
@ -155,6 +169,7 @@ void ERTView::on_packet(const ert::Packet& packet) {
msg += to_string_dec_uint(packet.id(), 10);
msg += " ";
msg += to_string_dec_uint(packet.consumption(), 10);
msg += packet.crc_ok() ? " *" : " x";
break;
default:

View File

@ -86,6 +86,9 @@ private:
const ID invalid_id = 0;
const Consumption invalid_consumption = 0;
bool crc_ok_idm() const;
bool crc_ok_scm() const;
};
} /* namespace ert */