Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyundai CAN: utilize macros for common address checks #1655

Merged
merged 6 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,40 @@ const CanMsg HYUNDAI_CAMERA_SCC_TX_MSGS[] = {
{0x485, 0, 4}, // LFAHDA_MFC Bus 0
};

#define HYUNDAI_COMMON_ADDR_CHECKS(legacy) \
{.msg = {{0x260, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U}, \
{0x371, 0, 8, .expected_timestep = 10000U}, { 0 }}}, \
{.msg = {{0x386, 0, 8, .check_checksum = !(legacy), .max_counter = (legacy) ? 0U : 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \
{.msg = {{0x394, 0, 8, .check_checksum = !(legacy), .max_counter = (legacy) ? 0U : 7U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \

#define HYUNDAI_SCC12_ADDR_CHECK(scc_bus) \
{.msg = {{0x421, (scc_bus), 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \

AddrCheckStruct hyundai_addr_checks[] = {
{.msg = {{0x260, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U},
{0x371, 0, 8, .expected_timestep = 10000U}, { 0 }}},
{.msg = {{0x386, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x394, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x421, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}},
HYUNDAI_COMMON_ADDR_CHECKS(false)
HYUNDAI_SCC12_ADDR_CHECK(0)
};
#define HYUNDAI_ADDR_CHECK_LEN (sizeof(hyundai_addr_checks) / sizeof(hyundai_addr_checks[0]))

AddrCheckStruct hyundai_cam_scc_addr_checks[] = {
{.msg = {{0x260, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U},
{0x371, 0, 8, .expected_timestep = 10000U}, { 0 }}},
{.msg = {{0x386, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x394, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x421, 2, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}},
HYUNDAI_COMMON_ADDR_CHECKS(false)
HYUNDAI_SCC12_ADDR_CHECK(2)
};
#define HYUNDAI_CAM_SCC_ADDR_CHECK_LEN (sizeof(hyundai_cam_scc_addr_checks) / sizeof(hyundai_cam_scc_addr_checks[0]))

AddrCheckStruct hyundai_long_addr_checks[] = {
{.msg = {{0x260, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U},
{0x371, 0, 8, .expected_timestep = 10000U}, { 0 }}},
{.msg = {{0x386, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x394, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}, { 0 }, { 0 }}},
HYUNDAI_COMMON_ADDR_CHECKS(false)
// Use CLU11 (buttons) to manage controls allowed instead of SCC cruise state
{.msg = {{0x4F1, 0, 4, .check_checksum = false, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}},
};
#define HYUNDAI_LONG_ADDR_CHECK_LEN (sizeof(hyundai_long_addr_checks) / sizeof(hyundai_long_addr_checks[0]))

// older hyundai models have less checks due to missing counters and checksums
AddrCheckStruct hyundai_legacy_addr_checks[] = {
{.msg = {{0x260, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U},
{0x371, 0, 8, .expected_timestep = 10000U}, { 0 }}},
{.msg = {{0x386, 0, 8, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x394, 0, 8, .expected_timestep = 10000U}, { 0 }, { 0 }}},
{.msg = {{0x421, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}, { 0 }, { 0 }}},
HYUNDAI_COMMON_ADDR_CHECKS(true)
HYUNDAI_SCC12_ADDR_CHECK(0)
};
#define HYUNDAI_LEGACY_ADDR_CHECK_LEN (sizeof(hyundai_legacy_addr_checks) / sizeof(hyundai_legacy_addr_checks[0]))

bool hyundai_legacy = false;

addr_checks hyundai_rx_checks = {hyundai_addr_checks, HYUNDAI_ADDR_CHECK_LEN};
addr_checks hyundai_rx_checks = SET_ADDR_CHECKS(hyundai_addr_checks);

static uint8_t hyundai_get_counter(CANPacket_t *to_push) {
int addr = GET_ADDR(to_push);
Expand Down Expand Up @@ -329,11 +323,11 @@ static const addr_checks* hyundai_init(uint16_t param) {
}

if (hyundai_longitudinal) {
hyundai_rx_checks = (addr_checks){hyundai_long_addr_checks, HYUNDAI_LONG_ADDR_CHECK_LEN};
hyundai_rx_checks = SET_ADDR_CHECKS(hyundai_long_addr_checks);
} else if (hyundai_camera_scc) {
hyundai_rx_checks = (addr_checks){hyundai_cam_scc_addr_checks, HYUNDAI_CAM_SCC_ADDR_CHECK_LEN};
hyundai_rx_checks = SET_ADDR_CHECKS(hyundai_cam_scc_addr_checks);
} else {
hyundai_rx_checks = (addr_checks){hyundai_addr_checks, HYUNDAI_ADDR_CHECK_LEN};
hyundai_rx_checks = SET_ADDR_CHECKS(hyundai_addr_checks);
}
return &hyundai_rx_checks;
}
Expand All @@ -344,7 +338,7 @@ static const addr_checks* hyundai_legacy_init(uint16_t param) {
hyundai_longitudinal = false;
hyundai_camera_scc = false;

hyundai_rx_checks = (addr_checks){hyundai_legacy_addr_checks, HYUNDAI_LEGACY_ADDR_CHECK_LEN};
hyundai_rx_checks = SET_ADDR_CHECKS(hyundai_legacy_addr_checks);
return &hyundai_rx_checks;
}

Expand Down
Loading