From 776287c0115301658fe74feb53f430461088df1d Mon Sep 17 00:00:00 2001 From: Henry N Date: Wed, 27 May 2020 13:12:52 +0200 Subject: [PATCH] amb8465: Find next start 0x44, after out of sync Should fix problems in #116 --- src/wmbus_amb8465.cc | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/wmbus_amb8465.cc b/src/wmbus_amb8465.cc index 8fbb1e9..9ae104e 100644 --- a/src/wmbus_amb8465.cc +++ b/src/wmbus_amb8465.cc @@ -348,23 +348,37 @@ FrameStatus WMBusAmber::checkAMB8465Frame(vector &data, // If it is not a 0xff we assume it is a message beginning with a length. // There might be a different mode where the data is wrapped in 0xff. But for the moment // this is what I see. - payload_len = data[0]; - if (payload_len < 10 || data[1] != 0x44) + size_t offset = 0; + + // The data[0] must be at least 10 bytes. C MM AAAA V T Ci + // And C must be 0x44. + while ((payload_len = data[offset]) < 10 || data[offset+1] != 0x44) { - // The data[0] must be at least 10 bytes. C MM AAAA V T Ci - // And C must be 0x44. - return ErrorInFrame; + offset++; + if (offset + 2 >= data.size()) { + // No sensible telegram in the buffer. Flush it! + // But not the last char, because the next char could be a 0x44 + verbose("(amb8465) no sensible telegram found, clearing buffer.\n"); + uchar last = data[data.size()-1]; + data.clear(); + data.insert(data.end(), &last, &last+1); // Re-insert the last byte. + return PartialFrame; + } } *msgid_out = 0; // 0 is used to signal *payload_len_out = payload_len; - *payload_offset = 1; - *frame_length = payload_len+1; + *payload_offset = offset+1; + *frame_length = payload_len+offset+1; if (data.size() < *frame_length) { debug("(amb8465) not enough bytes yet, partial frame %d %d.\n", data.size(), *frame_length); return PartialFrame; } + if (offset > 0) + { + verbose("(amb8465) out of sync, skipping %d bytes.\n", offset); + } debug("(amb8465) received full frame\n"); if (rssi_expected_)