Skip to content

Commit

Permalink
Hyundai CAN-FD: Move CRC compute function to common Hyundai safety (#…
Browse files Browse the repository at this point in the history
…1634)

* HKG CAN-FD: Move CRC lookup function to common Hyundai safety

* cleanup

* declare in common

* Subaru: gen2 long safety (#1594)

gen2 long safety

* VW MQB: Check steer req bit (#1631)

vq mqb

* cleanup

* more cleanup

---------

Co-authored-by: Justin Newberry <justin@comma.ai>
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
  • Loading branch information
3 people authored Oct 3, 2023
1 parent 6bf6ba7 commit 7af3362
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
34 changes: 1 addition & 33 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = {
addr_checks hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_addr_checks);


uint16_t hyundai_canfd_crc_lut[256];


const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32;
const int HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING = 128;
bool hyundai_canfd_alt_buttons = false;
Expand All @@ -156,39 +153,10 @@ static uint32_t hyundai_canfd_get_checksum(CANPacket_t *to_push) {
return chksum;
}

static uint32_t hyundai_canfd_compute_checksum(CANPacket_t *to_push) {
int len = GET_LEN(to_push);
uint32_t address = GET_ADDR(to_push);

uint16_t crc = 0;

for (int i = 2; i < len; i++) {
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)];
}

// Add address to crc
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)];
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)];

if (len == 8) {
crc ^= 0x5f29U;
} else if (len == 16) {
crc ^= 0x041dU;
} else if (len == 24) {
crc ^= 0x819dU;
} else if (len == 32) {
crc ^= 0x9f5bU;
} else {

}

return crc;
}

static int hyundai_canfd_rx_hook(CANPacket_t *to_push) {

bool valid = addr_safety_check(to_push, &hyundai_canfd_rx_checks,
hyundai_canfd_get_checksum, hyundai_canfd_compute_checksum, hyundai_canfd_get_counter, NULL);
hyundai_canfd_get_checksum, hyundai_common_canfd_compute_checksum, hyundai_canfd_get_counter, NULL);

int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);
Expand Down
31 changes: 31 additions & 0 deletions board/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ bool hyundai_canfd_hda2 = false;
bool hyundai_alt_limits = false;
uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button

uint16_t hyundai_canfd_crc_lut[256];

void hyundai_common_init(uint16_t param) {
hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS);
hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS);
Expand Down Expand Up @@ -85,4 +87,33 @@ void hyundai_common_cruise_buttons_check(const int cruise_button, const int main
}
}

uint32_t hyundai_common_canfd_compute_checksum(CANPacket_t *to_push) {
int len = GET_LEN(to_push);
uint32_t address = GET_ADDR(to_push);

uint16_t crc = 0;

for (int i = 2; i < len; i++) {
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)];
}

// Add address to crc
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)];
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)];

if (len == 8) {
crc ^= 0x5f29U;
} else if (len == 16) {
crc ^= 0x041dU;
} else if (len == 24) {
crc ^= 0x819dU;
} else if (len == 32) {
crc ^= 0x9f5bU;
} else {

}

return crc;
}

#endif

0 comments on commit 7af3362

Please sign in to comment.