Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include "safety_declarations.h" | ||
4 | |||
5 | extern uint16_t hyundai_canfd_crc_lut[256]; | ||
6 | uint16_t hyundai_canfd_crc_lut[256]; | ||
7 | |||
8 | static const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms | ||
9 | // | ||
10 | extern const uint32_t HYUNDAI_STANDSTILL_THRSLD; | ||
11 | const uint32_t HYUNDAI_STANDSTILL_THRSLD = 12; // 0.375 kph | ||
12 | |||
13 | enum { | ||
14 | HYUNDAI_BTN_NONE = 0, | ||
15 | HYUNDAI_BTN_RESUME = 1, | ||
16 | HYUNDAI_BTN_SET = 2, | ||
17 | HYUNDAI_BTN_CANCEL = 4, | ||
18 | }; | ||
19 | |||
20 | // common state | ||
21 | extern bool hyundai_ev_gas_signal; | ||
22 | bool hyundai_ev_gas_signal = false; | ||
23 | |||
24 | extern bool hyundai_hybrid_gas_signal; | ||
25 | bool hyundai_hybrid_gas_signal = false; | ||
26 | |||
27 | extern bool hyundai_longitudinal; | ||
28 | bool hyundai_longitudinal = false; | ||
29 | |||
30 | extern bool hyundai_camera_scc; | ||
31 | bool hyundai_camera_scc = false; | ||
32 | |||
33 | extern bool hyundai_canfd_lka_steering; | ||
34 | bool hyundai_canfd_lka_steering = false; | ||
35 | |||
36 | extern bool hyundai_alt_limits; | ||
37 | bool hyundai_alt_limits = false; | ||
38 | |||
39 | extern bool hyundai_fcev_gas_signal; | ||
40 | bool hyundai_fcev_gas_signal = false; | ||
41 | |||
42 | extern bool hyundai_alt_limits_2; | ||
43 | bool hyundai_alt_limits_2 = false; | ||
44 | |||
45 | static uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button | ||
46 | |||
47 | 1111 | void hyundai_common_init(uint16_t param) { | |
48 | 1111 | const int HYUNDAI_PARAM_EV_GAS = 1; | |
49 | 1111 | const int HYUNDAI_PARAM_HYBRID_GAS = 2; | |
50 | 1111 | const int HYUNDAI_PARAM_CAMERA_SCC = 8; | |
51 | 1111 | const int HYUNDAI_PARAM_CANFD_LKA_STEERING = 16; | |
52 | 1111 | const int HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags | |
53 | 1111 | const int HYUNDAI_PARAM_FCEV_GAS = 256; | |
54 | 1111 | const int HYUNDAI_PARAM_ALT_LIMITS_2 = 512; | |
55 | |||
56 | 1111 | hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS); | |
57 |
4/4✓ Branch 0 taken 753 times.
✓ Branch 1 taken 358 times.
✓ Branch 2 taken 250 times.
✓ Branch 3 taken 503 times.
|
1111 | hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS); |
58 | 1111 | hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC); | |
59 | 1111 | hyundai_canfd_lka_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING); | |
60 | 1111 | hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS); | |
61 | 1111 | hyundai_fcev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_FCEV_GAS); | |
62 | 1111 | hyundai_alt_limits_2 = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS_2); | |
63 | |||
64 | 1111 | hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; | |
65 | |||
66 | #ifdef ALLOW_DEBUG | ||
67 | 1111 | const int HYUNDAI_PARAM_LONGITUDINAL = 4; | |
68 | 1111 | hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL); | |
69 | #else | ||
70 | hyundai_longitudinal = false; | ||
71 | #endif | ||
72 | 1111 | } | |
73 | |||
74 | 1575 | void hyundai_common_cruise_state_check(const bool cruise_engaged) { | |
75 | // some newer HKG models can re-enable after spamming cancel button, | ||
76 | // so keep track of user button presses to deny engagement if no interaction | ||
77 | |||
78 | // enter controls on rising edge of ACC and recent user button press, exit controls when ACC off | ||
79 |
2/2✓ Branch 0 taken 1572 times.
✓ Branch 1 taken 3 times.
|
1575 | if (!hyundai_longitudinal) { |
80 |
5/6✓ Branch 0 taken 764 times.
✓ Branch 1 taken 808 times.
✓ Branch 2 taken 764 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 418 times.
✓ Branch 5 taken 346 times.
|
1572 | if (cruise_engaged && !cruise_engaged_prev && (hyundai_last_button_interaction < HYUNDAI_PREV_BUTTON_SAMPLES)) { |
81 | 418 | controls_allowed = true; | |
82 | } | ||
83 | |||
84 |
2/2✓ Branch 0 taken 808 times.
✓ Branch 1 taken 764 times.
|
1572 | if (!cruise_engaged) { |
85 | 808 | controls_allowed = false; | |
86 | } | ||
87 | 1572 | cruise_engaged_prev = cruise_engaged; | |
88 | } | ||
89 | 1575 | } | |
90 | |||
91 | 10494 | void hyundai_common_cruise_buttons_check(const int cruise_button, const bool main_button) { | |
92 |
8/8✓ Branch 0 taken 9658 times.
✓ Branch 1 taken 836 times.
✓ Branch 2 taken 8800 times.
✓ Branch 3 taken 858 times.
✓ Branch 4 taken 7955 times.
✓ Branch 5 taken 845 times.
✓ Branch 6 taken 110 times.
✓ Branch 7 taken 7845 times.
|
10494 | if ((cruise_button == HYUNDAI_BTN_RESUME) || (cruise_button == HYUNDAI_BTN_SET) || (cruise_button == HYUNDAI_BTN_CANCEL) || main_button) { |
93 | 2649 | hyundai_last_button_interaction = 0U; | |
94 | } else { | ||
95 | 7845 | hyundai_last_button_interaction = MIN(hyundai_last_button_interaction + 1U, HYUNDAI_PREV_BUTTON_SAMPLES); | |
96 | } | ||
97 | |||
98 |
2/2✓ Branch 0 taken 6930 times.
✓ Branch 1 taken 3564 times.
|
10494 | if (hyundai_longitudinal) { |
99 | // enter controls on falling edge of resume or set | ||
100 |
4/4✓ Branch 0 taken 6138 times.
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 6003 times.
|
6930 | bool set = (cruise_button != HYUNDAI_BTN_SET) && (cruise_button_prev == HYUNDAI_BTN_SET); |
101 |
4/4✓ Branch 0 taken 6138 times.
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 6003 times.
|
6930 | bool res = (cruise_button != HYUNDAI_BTN_RESUME) && (cruise_button_prev == HYUNDAI_BTN_RESUME); |
102 |
4/4✓ Branch 0 taken 6795 times.
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 6660 times.
|
6930 | if (set || res) { |
103 | 270 | controls_allowed = true; | |
104 | } | ||
105 | |||
106 | // exit controls on cancel press | ||
107 |
2/2✓ Branch 0 taken 801 times.
✓ Branch 1 taken 6129 times.
|
6930 | if (cruise_button == HYUNDAI_BTN_CANCEL) { |
108 | 801 | controls_allowed = false; | |
109 | } | ||
110 | |||
111 | 6930 | cruise_button_prev = cruise_button; | |
112 | } | ||
113 | 10494 | } | |
114 | |||
115 | 129355 | uint32_t hyundai_common_canfd_compute_checksum(const CANPacket_t *to_push) { | |
116 | 129355 | int len = GET_LEN(to_push); | |
117 | 129355 | uint32_t address = GET_ADDR(to_push); | |
118 | |||
119 | 129355 | uint16_t crc = 0; | |
120 | |||
121 |
2/2✓ Branch 0 taken 2855346 times.
✓ Branch 1 taken 129355 times.
|
2984701 | for (int i = 2; i < len; i++) { |
122 | 2855346 | crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)]; | |
123 | } | ||
124 | |||
125 | // Add address to crc | ||
126 | 129355 | crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)]; | |
127 | 129355 | crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)]; | |
128 | |||
129 |
2/2✓ Branch 0 taken 128163 times.
✓ Branch 1 taken 1192 times.
|
129355 | if (len == 24) { |
130 | 128163 | crc ^= 0x819dU; | |
131 |
1/2✓ Branch 0 taken 1192 times.
✗ Branch 1 not taken.
|
1192 | } else if (len == 32) { |
132 | 1192 | crc ^= 0x9f5bU; | |
133 | } else { | ||
134 | |||
135 | } | ||
136 | |||
137 | 129355 | return crc; | |
138 | } | ||
139 |