mirror of https://github.com/rusefi/opendbc.git
CANParser: clip `counter_fail` to the max (#976)
* test * fix * clean up * test * fix * clean up * debug * Revert "debug" This reverts commit 3fc8cf5a2073d65cf1efa81515b420a29e8298a4. * recover * also can update at bottom since we don't break early * changes * Revert "changes" This reverts commit a3b6fb5f6b38bb2f7ebd46c730487552bde8c6cb. * don't need \n * cmt * no need to change print behavior here * fix fix revert * info needs \n
This commit is contained in:
parent
ffa2ffdefe
commit
098fbe1b0e
|
@ -74,20 +74,16 @@ bool MessageState::parse(uint64_t nanos, const std::vector<uint8_t> &dat) {
|
|||
|
||||
|
||||
bool MessageState::update_counter_generic(int64_t v, int cnt_size) {
|
||||
uint8_t old_counter = counter;
|
||||
counter = v;
|
||||
if (((old_counter+1) & ((1 << cnt_size) -1)) != v) {
|
||||
counter_fail += 1;
|
||||
if (((counter + 1) & ((1 << cnt_size) -1)) != v) {
|
||||
counter_fail = std::min(counter_fail + 1, MAX_BAD_COUNTER);
|
||||
if (counter_fail > 1) {
|
||||
INFO("0x%X COUNTER FAIL #%d -- %d -> %d\n", address, counter_fail, old_counter, (int)v);
|
||||
}
|
||||
if (counter_fail >= MAX_BAD_COUNTER) {
|
||||
return false;
|
||||
INFO("0x%X COUNTER FAIL #%d -- %d -> %d\n", address, counter_fail, counter, (int)v);
|
||||
}
|
||||
} else if (counter_fail > 0) {
|
||||
counter_fail--;
|
||||
}
|
||||
return true;
|
||||
counter = v;
|
||||
return counter_fail < MAX_BAD_COUNTER;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ from opendbc.can.parser import CANParser
|
|||
from opendbc.can.packer import CANPacker
|
||||
from opendbc.can.tests import TEST_DBC
|
||||
|
||||
MAX_BAD_COUNTER = 5
|
||||
|
||||
|
||||
# Python implementation so we don't have to depend on boardd
|
||||
def can_list_to_can_capnp(can_msgs, msgtype='can', logMonoTime=None):
|
||||
|
@ -94,6 +96,31 @@ class TestCanParserPacker(unittest.TestCase):
|
|||
parser.update_strings([dat])
|
||||
self.assertTrue(parser.can_valid)
|
||||
|
||||
def test_parser_counter_can_valid(self):
|
||||
"""
|
||||
Tests number of allowed bad counters + ensures CAN stays invalid
|
||||
while receiving invalid messages + that we can recover
|
||||
"""
|
||||
msgs = [
|
||||
("STEERING_CONTROL", 0),
|
||||
]
|
||||
packer = CANPacker("honda_civic_touring_2016_can_generated")
|
||||
parser = CANParser("honda_civic_touring_2016_can_generated", msgs, 0)
|
||||
|
||||
msg = packer.make_can_msg("STEERING_CONTROL", 0, {"COUNTER": 0})
|
||||
bts = can_list_to_can_capnp([msg])
|
||||
|
||||
# bad static counter, invalid once it's seen MAX_BAD_COUNTER messages
|
||||
for idx in range(0x1000):
|
||||
parser.update_strings([bts])
|
||||
self.assertEqual((idx + 1) < MAX_BAD_COUNTER, parser.can_valid)
|
||||
|
||||
# one to recover
|
||||
msg = packer.make_can_msg("STEERING_CONTROL", 0, {"COUNTER": 1})
|
||||
bts = can_list_to_can_capnp([msg])
|
||||
parser.update_strings([bts])
|
||||
self.assertTrue(parser.can_valid)
|
||||
|
||||
def test_packer_parser(self):
|
||||
msgs = [
|
||||
("Brake_Status", 0),
|
||||
|
|
Loading…
Reference in New Issue