Skip to content

Commit

Permalink
Hyundai: counter check (#530)
Browse files Browse the repository at this point in the history
* hyundai counter check

* fix misra

* add counter to tests
  • Loading branch information
adeebshihadeh committed May 9, 2020
1 parent a439071 commit 869f123
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
34 changes: 27 additions & 7 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,40 @@ const int HYUNDAI_DRIVER_TORQUE_FACTOR = 2;
const int HYUNDAI_STANDSTILL_THRSLD = 30; // ~1kph
const AddrBus HYUNDAI_TX_MSGS[] = {{832, 0}, {1265, 0}, {1157, 0}};

// TODO: do checksum and counter checks
// TODO: do checksum checks
AddrCheckStruct hyundai_rx_checks[] = {
{.addr = {608}, .bus = 0, .expected_timestep = 10000U},
{.addr = {897}, .bus = 0, .expected_timestep = 10000U},
{.addr = {902}, .bus = 0, .expected_timestep = 10000U},
{.addr = {916}, .bus = 0, .expected_timestep = 10000U},
{.addr = {1057}, .bus = 0, .expected_timestep = 20000U},
{.addr = {608}, .bus = 0, .max_counter = 3U, .expected_timestep = 10000U},
{.addr = {897}, .bus = 0, .max_counter = 255U, .expected_timestep = 10000U},
{.addr = {902}, .bus = 0, .max_counter = 3U, .expected_timestep = 10000U},
{.addr = {916}, .bus = 0, .max_counter = 7U, .expected_timestep = 10000U},
{.addr = {1057}, .bus = 0, .max_counter = 15U, .expected_timestep = 20000U},
};
const int HYUNDAI_RX_CHECK_LEN = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_checks[0]);

static uint8_t hyundai_get_counter(CAN_FIFOMailBox_TypeDef *to_push) {
int addr = GET_ADDR(to_push);

uint8_t cnt;
if (addr == 608) {
cnt = (GET_BYTE(to_push, 7) >> 4) & 0x3;
} else if (addr == 897) {
cnt = GET_BYTE(to_push, 5);
} else if (addr == 902) {
cnt = (GET_BYTE(to_push, 1) >> 6) & 0x3;
} else if (addr == 916) {
cnt = (GET_BYTE(to_push, 1) >> 5) & 0x7;
} else if (addr == 1057) {
cnt = GET_BYTE(to_push, 7) & 0xF;
} else {
cnt = 0;
}
return cnt;
}

static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {

bool valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN,
NULL, NULL, NULL);
NULL, NULL, hyundai_get_counter);

bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;

Expand Down
16 changes: 13 additions & 3 deletions tests/safety/test_hyundai.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class TestHyundaiSafety(common.PandaSafetyTest):
FWD_BLACKLISTED_ADDRS = {2: [832, 1157]}
FWD_BUS_LOOKUP = {0: 2, 2: 0}

cnt_gas = 0
cnt_speed = 0
cnt_brake = 0
cnt_cruise = 0

def setUp(self):
self.packer = CANPackerPanda("hyundai_kia_generic")
self.safety = libpandasafety_py.libpandasafety
Expand All @@ -36,20 +41,25 @@ def _button_msg(self, buttons):
return self.packer.make_can_msg_panda("CLU11", 0, values)

def _gas_msg(self, val):
values = {"CF_Ems_AclAct": val}
values = {"CF_Ems_AclAct": val, "AliveCounter": self.cnt_gas % 4}
self.__class__.cnt_gas += 1
return self.packer.make_can_msg_panda("EMS16", 0, values)

def _brake_msg(self, brake):
values = {"DriverBraking": brake}
values = {"DriverBraking": brake, "AliveCounterTCS": self.cnt_brake % 8}
self.__class__.cnt_brake += 1
return self.packer.make_can_msg_panda("TCS13", 0, values)

def _speed_msg(self, speed):
# panda safety doesn't scale, so undo the scaling
values = {"WHL_SPD_%s"%s: speed*0.03125 for s in ["FL", "FR", "RL", "RR"]}
values["WHL_SPD_AliveCounter_LSB"] = self.cnt_speed % 4
self.__class__.cnt_speed += 1
return self.packer.make_can_msg_panda("WHL_SPD11", 0, values)

def _pcm_status_msg(self, enabled):
values = {"ACCMode": enabled}
values = {"ACCMode": enabled, "CR_VSM_Alive": self.cnt_cruise % 16}
self.__class__.cnt_cruise += 1
return self.packer.make_can_msg_panda("SCC12", 0, values)

def _set_prev_torque(self, t):
Expand Down

0 comments on commit 869f123

Please sign in to comment.