From 9628815da7c9079b45fe4e88b212c2d5d5399585 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Thu, 2 Jun 2016 22:22:22 -0700 Subject: [PATCH] TPMS: Validate checksum for the OOK 8k4 Schrader variant. --- firmware/common/tpms_packet.cpp | 38 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/firmware/common/tpms_packet.cpp b/firmware/common/tpms_packet.cpp index 8f1acffa6..55e809324 100644 --- a/firmware/common/tpms_packet.cpp +++ b/firmware/common/tpms_packet.cpp @@ -80,11 +80,39 @@ Optional Packet::reading_ook_8k192_schrader() const { } Optional Packet::reading_ook_8k4_schrader() const { - return Reading { - Reading::Type::GMC_96, - reader_.read(20, 32), - Pressure { static_cast(reader_.read(52, 8)) } - }; + /* + * Preamble: 01*40 + * System ID: 01100101, ??*20 (not really sure what this data is) + * ID: 32 Manchester symbols + * Value: 8 Manchester symbols (temperature?) + * Value: 8 Manchester symbols (pressure?) + * Checksum: 8 Manchester symbols (uint8_t sum of bytes starting with system ID) + */ + /* NOTE: First four bits of packet are consumed in preamble detection. + * Those bits assumed to be 0b0100", which may not be entirely true... + */ + constexpr uint8_t first_nibble = 0x4; + const auto system_id = (first_nibble << 20) | reader_.read(0, 20); + const auto id = reader_.read(20, 32); + const auto value_0 = reader_.read(52, 8); + const auto value_1 = reader_.read(60, 8); + const auto checksum = reader_.read(68, 8); + + uint8_t checksum_calculated = (first_nibble << 4) | reader_.read(0, 4); + for(size_t i=4; i<68; i+=8) { + checksum_calculated += reader_.read(i, 8); + } + + if( checksum_calculated == checksum ) { + return Reading { + Reading::Type::GMC_96, + id, + Pressure { static_cast(value_1) * 4 / 3 }, + Temperature { static_cast(value_0) - 50 } + }; + } else { + return { }; + } } Optional Packet::reading() const {