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

View File

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