add hyundai legacy safety mode (#554)

master
Adeeb 2020-06-12 18:03:31 -07:00 committed by GitHub
parent d6668fe4ef
commit d0442fd1e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 10 deletions

View File

@ -37,6 +37,7 @@
#define SAFETY_HONDA_BOSCH_HARNESS 20U
#define SAFETY_VOLKSWAGEN_PQ 21U
#define SAFETY_SUBARU_LEGACY 22U
#define SAFETY_HYUNDAI_LEGACY 23U
uint16_t current_safety_mode = SAFETY_SILENT;
const safety_hooks *current_hooks = &nooutput_hooks;
@ -227,6 +228,7 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
{SAFETY_NISSAN, &nissan_hooks},
{SAFETY_NOOUTPUT, &nooutput_hooks},
{SAFETY_HYUNDAI_LEGACY, &hyundai_legacy_hooks},
#ifdef ALLOW_DEBUG
{SAFETY_MAZDA, &mazda_hooks},
{SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks},

View File

@ -19,18 +19,25 @@ const CanMsg HYUNDAI_TX_MSGS[] = {
// TODO: missing checksum for wheel speeds message,worst failure case is
// wheel speeds stuck at 0 and we don't disengage on brake press
// TODO: refactor addr check to cleanly re-enable commented out checks for cars that have them
AddrCheckStruct hyundai_rx_checks[] = {
{.msg = {{608, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U}}},
// TODO: older hyundai models don't populate the counter bits in 902
//{.msg = {{902, 0, 8, .max_counter = 15U, .expected_timestep = 10000U}}},
{.msg = {{902, 0, 8, .max_counter = 0U, .expected_timestep = 10000U}}},
//{.msg = {{916, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}}},
{.msg = {{916, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}},
{.msg = {{902, 0, 8, .check_checksum = false, .max_counter = 15U, .expected_timestep = 10000U}}},
{.msg = {{916, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}}},
{.msg = {{1057, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}}},
};
const int HYUNDAI_RX_CHECK_LEN = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_checks[0]);
// older hyundai models have less checks due to missing counters and checksums
AddrCheckStruct hyundai_legacy_rx_checks[] = {
{.msg = {{608, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U}}},
{.msg = {{902, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}},
{.msg = {{916, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}},
{.msg = {{1057, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}}},
};
const int HYUNDAI_LEGACY_RX_CHECK_LEN = sizeof(hyundai_legacy_rx_checks) / sizeof(hyundai_legacy_rx_checks[0]);
bool hyundai_legacy = false;
static uint8_t hyundai_get_counter(CAN_FIFOMailBox_TypeDef *to_push) {
int addr = GET_ADDR(to_push);
@ -82,9 +89,17 @@ static uint8_t hyundai_compute_checksum(CAN_FIFOMailBox_TypeDef *to_push) {
static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
bool valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN,
hyundai_get_checksum, hyundai_compute_checksum,
hyundai_get_counter);
bool valid;
if (hyundai_legacy) {
valid = addr_safety_check(to_push, hyundai_legacy_rx_checks, HYUNDAI_LEGACY_RX_CHECK_LEN,
hyundai_get_checksum, hyundai_compute_checksum,
hyundai_get_counter);
} else {
valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN,
hyundai_get_checksum, hyundai_compute_checksum,
hyundai_get_counter);
}
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
@ -234,8 +249,24 @@ static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return bus_fwd;
}
static void hyundai_init(int16_t param) {
UNUSED(param);
controls_allowed = false;
relay_malfunction_reset();
hyundai_legacy = false;
}
static void hyundai_legacy_init(int16_t param) {
UNUSED(param);
controls_allowed = false;
relay_malfunction_reset();
hyundai_legacy = true;
}
const safety_hooks hyundai_hooks = {
.init = nooutput_init,
.init = hyundai_init,
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
@ -243,3 +274,13 @@ const safety_hooks hyundai_hooks = {
.addr_check = hyundai_rx_checks,
.addr_check_len = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_checks[0]),
};
const safety_hooks hyundai_legacy_hooks = {
.init = hyundai_legacy_init,
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.fwd = hyundai_fwd_hook,
.addr_check = hyundai_legacy_rx_checks,
.addr_check_len = sizeof(hyundai_legacy_rx_checks) / sizeof(hyundai_legacy_rx_checks[0]),
};

View File

@ -129,6 +129,7 @@ class Panda(object):
SAFETY_HONDA_BOSCH_HARNESS = 20
SAFETY_VOLKSWAGEN_PQ = 21
SAFETY_SUBARU_LEGACY = 22
SAFETY_HYUNDAI_LEGACY = 23
SERIAL_DEBUG = 0
SERIAL_ESP = 1

View File

@ -191,5 +191,13 @@ class TestHyundaiSafety(common.PandaSafetyTest):
self.assertTrue(self._tx(self._button_msg(RESUME_BTN)))
class TestHyundaiLegacySafety(TestHyundaiSafety):
def setUp(self):
self.packer = CANPackerPanda("hyundai_kia_generic")
self.safety = libpandasafety_py.libpandasafety
self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_LEGACY, 0)
self.safety.init_tests()
if __name__ == "__main__":
unittest.main()