GCC Code Coverage Report


Directory: ./
File: safety/safety_honda.h
Date: 2025-02-27 09:09:49
Exec Total Coverage
Lines: 187 187 100.0%
Functions: 10 10 100.0%
Branches: 217 232 93.5%

Line Branch Exec Source
1 #pragma once
2
3 #include "safety_declarations.h"
4
5 // All common address checks except SCM_BUTTONS which isn't on one Nidec safety configuration
6 #define HONDA_COMMON_NO_SCM_FEEDBACK_RX_CHECKS(pt_bus) \
7 {.msg = {{0x1A6, (pt_bus), 8, .check_checksum = true, .max_counter = 3U, .frequency = 25U}, /* SCM_BUTTONS */ \
8 {0x296, (pt_bus), 4, .check_checksum = true, .max_counter = 3U, .frequency = 25U}, { 0 }}}, \
9 {.msg = {{0x158, (pt_bus), 8, .check_checksum = true, .max_counter = 3U, .frequency = 100U}, { 0 }, { 0 }}}, /* ENGINE_DATA */ \
10 {.msg = {{0x17C, (pt_bus), 8, .check_checksum = true, .max_counter = 3U, .frequency = 100U}, { 0 }, { 0 }}}, /* POWERTRAIN_DATA */ \
11
12 #define HONDA_COMMON_RX_CHECKS(pt_bus) \
13 HONDA_COMMON_NO_SCM_FEEDBACK_RX_CHECKS(pt_bus) \
14 {.msg = {{0x326, (pt_bus), 8, .check_checksum = true, .max_counter = 3U, .frequency = 10U}, { 0 }, { 0 }}}, /* SCM_FEEDBACK */ \
15
16 // Alternate brake message is used on some Honda Bosch, and Honda Bosch radarless (where PT bus is 0)
17 #define HONDA_ALT_BRAKE_ADDR_CHECK(pt_bus) \
18 {.msg = {{0x1BE, (pt_bus), 3, .check_checksum = true, .max_counter = 3U, .frequency = 50U}, { 0 }, { 0 }}}, /* BRAKE_MODULE */ \
19
20
21 // Nidec and bosch radarless has the powertrain bus on bus 0
22 static RxCheck honda_common_rx_checks[] = {
23 HONDA_COMMON_RX_CHECKS(0)
24 };
25
26 enum {
27 HONDA_BTN_NONE = 0,
28 HONDA_BTN_MAIN = 1,
29 HONDA_BTN_CANCEL = 2,
30 HONDA_BTN_SET = 3,
31 HONDA_BTN_RESUME = 4,
32 };
33
34 static int honda_brake = 0;
35 static bool honda_brake_switch_prev = false;
36 static bool honda_alt_brake_msg = false;
37 static bool honda_fwd_brake = false;
38 static bool honda_bosch_long = false;
39 static bool honda_bosch_radarless = false;
40 typedef enum {HONDA_NIDEC, HONDA_BOSCH} HondaHw;
41 static HondaHw honda_hw = HONDA_NIDEC;
42
43
44 179143 static int honda_get_pt_bus(void) {
45
4/4
✓ Branch 0 taken 56970 times.
✓ Branch 1 taken 122173 times.
✓ Branch 2 taken 29278 times.
✓ Branch 3 taken 27692 times.
179143 return ((honda_hw == HONDA_BOSCH) && !honda_bosch_radarless) ? 1 : 0;
46 }
47
48 3637 static uint32_t honda_get_checksum(const CANPacket_t *to_push) {
49 3637 int checksum_byte = GET_LEN(to_push) - 1U;
50 3637 return (uint8_t)(GET_BYTE(to_push, checksum_byte)) & 0xFU;
51 }
52
53 3637 static uint32_t honda_compute_checksum(const CANPacket_t *to_push) {
54 3637 int len = GET_LEN(to_push);
55 3637 uint8_t checksum = 0U;
56 3637 unsigned int addr = GET_ADDR(to_push);
57
2/2
✓ Branch 0 taken 10911 times.
✓ Branch 1 taken 3637 times.
14548 while (addr > 0U) {
58 10911 checksum += (uint8_t)(addr & 0xFU); addr >>= 4;
59 }
60
2/2
✓ Branch 0 taken 16158 times.
✓ Branch 1 taken 3637 times.
19795 for (int j = 0; j < len; j++) {
61 16158 uint8_t byte = GET_BYTE(to_push, j);
62 16158 checksum += (uint8_t)(byte & 0xFU) + (byte >> 4U);
63
2/2
✓ Branch 0 taken 3637 times.
✓ Branch 1 taken 12521 times.
16158 if (j == (len - 1)) {
64 3637 checksum -= (byte & 0xFU); // remove checksum in message
65 }
66 }
67 3637 return (uint8_t)((8U - checksum) & 0xFU);
68 }
69
70 3637 static uint8_t honda_get_counter(const CANPacket_t *to_push) {
71 3637 int counter_byte = GET_LEN(to_push) - 1U;
72 3637 return (GET_BYTE(to_push, counter_byte) >> 4U) & 0x3U;
73 }
74
75 72176 static void honda_rx_hook(const CANPacket_t *to_push) {
76
6/6
✓ Branch 0 taken 54133 times.
✓ Branch 1 taken 18043 times.
✓ Branch 2 taken 20091 times.
✓ Branch 3 taken 34042 times.
✓ Branch 4 taken 18043 times.
✓ Branch 5 taken 20091 times.
72176 const bool pcm_cruise = ((honda_hw == HONDA_BOSCH) && !honda_bosch_long) || (honda_hw == HONDA_NIDEC);
77 72176 int pt_bus = honda_get_pt_bus();
78
79 72176 int addr = GET_ADDR(to_push);
80 72176 int bus = GET_BUS(to_push);
81
82 // sample speed
83
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 72088 times.
72176 if (addr == 0x158) {
84 // first 2 bytes
85 88 vehicle_moving = GET_BYTE(to_push, 0) | GET_BYTE(to_push, 1);
86 }
87
88 // check ACC main state
89 // 0x326 for all Bosch and some Nidec, 0x1A6 for some Nidec
90
4/4
✓ Branch 0 taken 72100 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 72057 times.
72176 if ((addr == 0x326) || (addr == 0x1A6)) {
91
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 43 times.
119 acc_main_on = GET_BIT(to_push, ((addr == 0x326) ? 28U : 47U));
92
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 52 times.
119 if (!acc_main_on) {
93 67 controls_allowed = false;
94 }
95 }
96
97 // enter controls when PCM enters cruise state
98
4/4
✓ Branch 0 taken 52085 times.
✓ Branch 1 taken 20091 times.
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 51921 times.
72176 if (pcm_cruise && (addr == 0x17C)) {
99 164 const bool cruise_engaged = GET_BIT(to_push, 38U);
100 // engage on rising edge
101
4/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 28 times.
164 if (cruise_engaged && !cruise_engaged_prev) {
102 48 controls_allowed = true;
103 }
104
105 // Since some Nidec cars can brake down to 0 after the PCM disengages,
106 // we don't disengage when the PCM does.
107
4/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 32 times.
164 if (!cruise_engaged && (honda_hw != HONDA_NIDEC)) {
108 56 controls_allowed = false;
109 }
110 164 cruise_engaged_prev = cruise_engaged;
111 }
112
113 // state machine to enter and exit controls for button enabling
114 // 0x1A6 for the ILX, 0x296 for the Civic Touring
115
6/6
✓ Branch 0 taken 72133 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 3210 times.
✓ Branch 3 taken 68923 times.
✓ Branch 4 taken 3221 times.
✓ Branch 5 taken 32 times.
72176 if (((addr == 0x1A6) || (addr == 0x296)) && (bus == pt_bus)) {
116 3221 int button = (GET_BYTE(to_push, 0) & 0xE0U) >> 5;
117
118 // enter controls on the falling edge of set or resume
119
4/4
✓ Branch 0 taken 2835 times.
✓ Branch 1 taken 386 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 2755 times.
3221 bool set = (button != HONDA_BTN_SET) && (cruise_button_prev == HONDA_BTN_SET);
120
4/4
✓ Branch 0 taken 2849 times.
✓ Branch 1 taken 372 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 2769 times.
3221 bool res = (button != HONDA_BTN_RESUME) && (cruise_button_prev == HONDA_BTN_RESUME);
121
8/8
✓ Branch 0 taken 1614 times.
✓ Branch 1 taken 1607 times.
✓ Branch 2 taken 1536 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 1506 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 30 times.
✓ Branch 7 taken 1476 times.
3221 if (acc_main_on && !pcm_cruise && (set || res)) {
122 60 controls_allowed = true;
123 }
124
125 // exit controls once main or cancel are pressed
126
4/4
✓ Branch 0 taken 2867 times.
✓ Branch 1 taken 354 times.
✓ Branch 2 taken 374 times.
✓ Branch 3 taken 2493 times.
3221 if ((button == HONDA_BTN_MAIN) || (button == HONDA_BTN_CANCEL)) {
127 728 controls_allowed = false;
128 }
129 3221 cruise_button_prev = button;
130 }
131
132 // user brake signal on 0x17C reports applied brake from computer brake on accord
133 // and crv, which prevents the usual brake safety from working correctly. these
134 // cars have a signal on 0x1BE which only detects user's brake being applied so
135 // in these cases, this is used instead.
136 // most hondas: 0x17C
137 // accord, crv: 0x1BE
138
2/2
✓ Branch 0 taken 17024 times.
✓ Branch 1 taken 55152 times.
72176 if (honda_alt_brake_msg) {
139
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 16984 times.
17024 if (addr == 0x1BE) {
140 40 brake_pressed = GET_BIT(to_push, 4U);
141 }
142 } else {
143
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 54963 times.
55152 if (addr == 0x17C) {
144 // also if brake switch is 1 for two CAN frames, as brake pressed is delayed
145 189 const bool brake_switch = GET_BIT(to_push, 32U);
146
3/6
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 135 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
189 brake_pressed = (GET_BIT(to_push, 53U)) || (brake_switch && honda_brake_switch_prev);
147 189 honda_brake_switch_prev = brake_switch;
148 }
149 }
150
151
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 71949 times.
72176 if (addr == 0x17C) {
152 227 gas_pressed = GET_BYTE(to_push, 0) != 0U;
153 }
154
155 // disable stock Honda AEB in alternative experience
156
1/2
✓ Branch 0 taken 72176 times.
✗ Branch 1 not taken.
72176 if (!(alternative_experience & ALT_EXP_DISABLE_STOCK_AEB)) {
157
4/4
✓ Branch 0 taken 23556 times.
✓ Branch 1 taken 48620 times.
✓ Branch 2 taken 1036 times.
✓ Branch 3 taken 22520 times.
72176 if ((bus == 2) && (addr == 0x1FA)) {
158 1036 bool honda_stock_aeb = GET_BIT(to_push, 29U);
159 1036 int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) | (GET_BYTE(to_push, 1) >> 6);
160
161 // Forward AEB when stock braking is higher than openpilot braking
162 // only stop forwarding when AEB event is over
163
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1024 times.
1036 if (!honda_stock_aeb) {
164 12 honda_fwd_brake = false;
165
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 512 times.
1024 } else if (honda_stock_brake >= honda_brake) {
166 512 honda_fwd_brake = true;
167 } else {
168 // Leave Honda forward brake as is
169 }
170 }
171 }
172
173
2/2
✓ Branch 0 taken 54133 times.
✓ Branch 1 taken 18043 times.
72176 int bus_rdr_car = (honda_hw == HONDA_BOSCH) ? 0 : 2; // radar bus, car side
174 72176 bool stock_ecu_detected = false;
175
176 // If steering controls messages are received on the destination bus, it's an indication
177 // that the relay might be malfunctioning
178
4/4
✓ Branch 0 taken 72152 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 72128 times.
72176 if ((addr == 0xE4) || (addr == 0x194)) {
179
8/8
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 8 times.
48 if (((honda_hw != HONDA_NIDEC) && (bus == bus_rdr_car)) || ((honda_hw == HONDA_NIDEC) && (bus == 0))) {
180 16 stock_ecu_detected = true;
181 }
182 }
183 // If Honda Bosch longitudinal mode is selected we need to ensure the radar is turned off
184 // Verify this by ensuring ACC_CONTROL (0x1DF) is not received on the PT bus
185
8/8
✓ Branch 0 taken 20091 times.
✓ Branch 1 taken 52085 times.
✓ Branch 2 taken 10047 times.
✓ Branch 3 taken 10044 times.
✓ Branch 4 taken 4415 times.
✓ Branch 5 taken 5632 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 4414 times.
72176 if (honda_bosch_long && !honda_bosch_radarless && (bus == pt_bus) && (addr == 0x1DF)) {
186 1 stock_ecu_detected = true;
187 }
188
189 72176 generic_rx_checks(stock_ecu_detected);
190
191 72176 }
192
193 106967 static bool honda_tx_hook(const CANPacket_t *to_send) {
194
195 const LongitudinalLimits HONDA_BOSCH_LONG_LIMITS = {
196 .max_accel = 200, // accel is used for brakes
197 .min_accel = -350,
198
199 .max_gas = 2000,
200 .inactive_gas = -30000,
201 };
202
203 const LongitudinalLimits HONDA_NIDEC_LONG_LIMITS = {
204 .max_gas = 198, // 0xc6
205 .max_brake = 255,
206
207 .inactive_speed = 0,
208 };
209
210 106967 bool tx = true;
211 106967 int addr = GET_ADDR(to_send);
212 106967 int bus = GET_BUS(to_send);
213
214 106967 int bus_pt = honda_get_pt_bus();
215
2/2
✓ Branch 0 taken 627 times.
✓ Branch 1 taken 106340 times.
106967 int bus_buttons = (honda_bosch_radarless) ? 2 : bus_pt; // the camera controls ACC on radarless Bosch cars
216
217 // ACC_HUD: safety check (nidec w/o pedal)
218
3/4
✓ Branch 0 taken 102004 times.
✓ Branch 1 taken 4963 times.
✓ Branch 2 taken 102004 times.
✗ Branch 3 not taken.
106967 if ((addr == 0x30C) && (bus == bus_pt)) {
219 102004 int pcm_speed = (GET_BYTE(to_send, 0) << 8) | GET_BYTE(to_send, 1);
220 102004 int pcm_gas = GET_BYTE(to_send, 2);
221
222 102004 bool violation = false;
223 102004 violation |= longitudinal_speed_checks(pcm_speed, HONDA_NIDEC_LONG_LIMITS);
224 102004 violation |= longitudinal_gas_checks(pcm_gas, HONDA_NIDEC_LONG_LIMITS);
225
2/2
✓ Branch 0 taken 62198 times.
✓ Branch 1 taken 39806 times.
102004 if (violation) {
226 62198 tx = false;
227 }
228 }
229
230 // BRAKE: safety check (nidec)
231
3/4
✓ Branch 0 taken 2125 times.
✓ Branch 1 taken 104842 times.
✓ Branch 2 taken 2125 times.
✗ Branch 3 not taken.
106967 if ((addr == 0x1FA) && (bus == bus_pt)) {
232 2125 honda_brake = (GET_BYTE(to_send, 0) << 2) + ((GET_BYTE(to_send, 1) >> 6) & 0x3U);
233
2/2
✓ Branch 1 taken 1092 times.
✓ Branch 2 taken 1033 times.
2125 if (longitudinal_brake_checks(honda_brake, HONDA_NIDEC_LONG_LIMITS)) {
234 1092 tx = false;
235 }
236
2/2
✓ Branch 0 taken 1060 times.
✓ Branch 1 taken 1065 times.
2125 if (honda_fwd_brake) {
237 1060 tx = false;
238 }
239 }
240
241 // BRAKE/GAS: safety check (bosch)
242
3/4
✓ Branch 0 taken 2181 times.
✓ Branch 1 taken 104786 times.
✓ Branch 2 taken 2181 times.
✗ Branch 3 not taken.
106967 if ((addr == 0x1DF) && (bus == bus_pt)) {
243 2181 int accel = (GET_BYTE(to_send, 3) << 3) | ((GET_BYTE(to_send, 4) >> 5) & 0x7U);
244 2181 accel = to_signed(accel, 11);
245
246 2181 int gas = (GET_BYTE(to_send, 0) << 8) | GET_BYTE(to_send, 1);
247 2181 gas = to_signed(gas, 16);
248
249 2181 bool violation = false;
250 2181 violation |= longitudinal_accel_checks(accel, HONDA_BOSCH_LONG_LIMITS);
251 2181 violation |= longitudinal_gas_checks(gas, HONDA_BOSCH_LONG_LIMITS);
252
2/2
✓ Branch 0 taken 1606 times.
✓ Branch 1 taken 575 times.
2181 if (violation) {
253 1606 tx = false;
254 }
255 }
256
257 // ACCEL: safety check (radarless)
258
3/4
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 106358 times.
✓ Branch 2 taken 609 times.
✗ Branch 3 not taken.
106967 if ((addr == 0x1C8) && (bus == bus_pt)) {
259 609 int accel = (GET_BYTE(to_send, 0) << 4) | (GET_BYTE(to_send, 1) >> 4);
260 609 accel = to_signed(accel, 12);
261
262 609 bool violation = false;
263 609 violation |= longitudinal_accel_checks(accel, HONDA_BOSCH_LONG_LIMITS);
264
2/2
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 233 times.
609 if (violation) {
265 376 tx = false;
266 }
267 }
268
269 // STEER: safety check
270
3/4
✓ Branch 0 taken 106951 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 106951 times.
106967 if ((addr == 0xE4) || (addr == 0x194)) {
271
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!controls_allowed) {
272 16 bool steer_applied = GET_BYTE(to_send, 0) | GET_BYTE(to_send, 1);
273
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (steer_applied) {
274 8 tx = false;
275 }
276 }
277 }
278
279 // Bosch supplemental control check
280
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 106963 times.
106967 if (addr == 0xE5) {
281
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if ((GET_BYTES(to_send, 0, 4) != 0x10800004U) || ((GET_BYTES(to_send, 4, 4) & 0x00FFFFFFU) != 0x0U)) {
282 4 tx = false;
283 }
284 }
285
286 // FORCE CANCEL: safety check only relevant when spamming the cancel button in Bosch HW
287 // ensuring that only the cancel button press is sent (VAL 2) when controls are off.
288 // This avoids unintended engagements while still allowing resume spam
289
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 106951 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
106967 if ((addr == 0x296) && !controls_allowed && (bus == bus_buttons)) {
290
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (((GET_BYTE(to_send, 0) >> 5) & 0x7U) != 2U) {
291 8 tx = false;
292 }
293 }
294
295 // Only tester present ("\x02\x3E\x80\x00\x00\x00\x00\x00") allowed on diagnostics address
296
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 106963 times.
106967 if (addr == 0x18DAB0F1) {
297
3/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
4 if ((GET_BYTES(to_send, 0, 4) != 0x00803E02U) || (GET_BYTES(to_send, 4, 4) != 0x0U)) {
298 3 tx = false;
299 }
300 }
301
302 106967 return tx;
303 }
304
305 56 static safety_config honda_nidec_init(uint16_t param) {
306 static CanMsg HONDA_N_TX_MSGS[] = {{0xE4, 0, 5}, {0x194, 0, 4}, {0x1FA, 0, 8}, {0x30C, 0, 8}, {0x33D, 0, 5}};
307
308 56 const uint16_t HONDA_PARAM_NIDEC_ALT = 4;
309
310 56 honda_hw = HONDA_NIDEC;
311 56 honda_brake = 0;
312 56 honda_brake_switch_prev = false;
313 56 honda_fwd_brake = false;
314 56 honda_alt_brake_msg = false;
315 56 honda_bosch_long = false;
316 56 honda_bosch_radarless = false;
317
318 safety_config ret;
319
320 56 bool enable_nidec_alt = GET_FLAG(param, HONDA_PARAM_NIDEC_ALT);
321
322
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 if (enable_nidec_alt) {
323 // For Nidecs with main on signal on an alternate msg (missing 0x326)
324 static RxCheck honda_nidec_alt_rx_checks[] = {
325 HONDA_COMMON_NO_SCM_FEEDBACK_RX_CHECKS(0)
326 };
327
328 28 SET_RX_CHECKS(honda_nidec_alt_rx_checks, ret);
329 } else {
330 28 SET_RX_CHECKS(honda_common_rx_checks, ret);
331 }
332
333 56 SET_TX_MSGS(HONDA_N_TX_MSGS, ret);
334
335 56 return ret;
336 }
337
338 177 static safety_config honda_bosch_init(uint16_t param) {
339 static CanMsg HONDA_BOSCH_TX_MSGS[] = {{0xE4, 0, 5}, {0xE5, 0, 8}, {0x296, 1, 4}, {0x33D, 0, 5}, {0x33DA, 0, 5}, {0x33DB, 0, 8}}; // Bosch
340 static CanMsg HONDA_BOSCH_LONG_TX_MSGS[] = {{0xE4, 1, 5}, {0x1DF, 1, 8}, {0x1EF, 1, 8}, {0x1FA, 1, 8}, {0x30C, 1, 8}, {0x33D, 1, 5}, {0x33DA, 1, 5}, {0x33DB, 1, 8}, {0x39F, 1, 8}, {0x18DAB0F1, 1, 8}}; // Bosch w/ gas and brakes
341 static CanMsg HONDA_RADARLESS_TX_MSGS[] = {{0xE4, 0, 5}, {0x296, 2, 4}, {0x33D, 0, 8}}; // Bosch radarless
342 static CanMsg HONDA_RADARLESS_LONG_TX_MSGS[] = {{0xE4, 0, 5}, {0x33D, 0, 8}, {0x1C8, 0, 8}, {0x30C, 0, 8}}; // Bosch radarless w/ gas and brakes
343
344 177 const uint16_t HONDA_PARAM_ALT_BRAKE = 1;
345 177 const uint16_t HONDA_PARAM_RADARLESS = 8;
346
347 static RxCheck honda_common_alt_brake_rx_checks[] = {
348 HONDA_COMMON_RX_CHECKS(0)
349 HONDA_ALT_BRAKE_ADDR_CHECK(0)
350 };
351
352 static RxCheck honda_bosch_alt_brake_rx_checks[] = {
353 HONDA_COMMON_RX_CHECKS(1)
354 HONDA_ALT_BRAKE_ADDR_CHECK(1)
355 };
356
357 // Bosch has pt on bus 1, verified 0x1A6 does not exist
358 static RxCheck honda_bosch_rx_checks[] = {
359 HONDA_COMMON_RX_CHECKS(1)
360 };
361
362 177 honda_hw = HONDA_BOSCH;
363 177 honda_brake_switch_prev = false;
364 177 honda_bosch_radarless = GET_FLAG(param, HONDA_PARAM_RADARLESS);
365 // Checking for alternate brake override from safety parameter
366 177 honda_alt_brake_msg = GET_FLAG(param, HONDA_PARAM_ALT_BRAKE);
367
368 // radar disabled so allow gas/brakes
369 #ifdef ALLOW_DEBUG
370 177 const uint16_t HONDA_PARAM_BOSCH_LONG = 2;
371 177 honda_bosch_long = GET_FLAG(param, HONDA_PARAM_BOSCH_LONG);
372 #endif
373
374 safety_config ret;
375
4/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 60 times.
177 if (honda_bosch_radarless && honda_alt_brake_msg) {
376 28 SET_RX_CHECKS(honda_common_alt_brake_rx_checks, ret);
377
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 89 times.
149 } else if (honda_bosch_radarless) {
378 60 SET_RX_CHECKS(honda_common_rx_checks, ret);
379
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 61 times.
89 } else if (honda_alt_brake_msg) {
380 28 SET_RX_CHECKS(honda_bosch_alt_brake_rx_checks, ret);
381 } else {
382 61 SET_RX_CHECKS(honda_bosch_rx_checks, ret);
383 }
384
385
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 89 times.
177 if (honda_bosch_radarless) {
386
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 55 times.
88 if (honda_bosch_long) {
387 33 SET_TX_MSGS(HONDA_RADARLESS_LONG_TX_MSGS, ret);
388 } else {
389 55 SET_TX_MSGS(HONDA_RADARLESS_TX_MSGS, ret);
390 }
391 } else {
392
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 55 times.
89 if (honda_bosch_long) {
393 34 SET_TX_MSGS(HONDA_BOSCH_LONG_TX_MSGS, ret);
394 } else {
395 55 SET_TX_MSGS(HONDA_BOSCH_TX_MSGS, ret);
396 }
397 }
398 177 return ret;
399 }
400
401 33792 static int honda_nidec_fwd_hook(int bus_num, int addr) {
402 // fwd from car to camera. also fwd certain msgs from camera to car
403 // 0xE4 is steering on all cars except CRV and RDX, 0x194 for CRV and RDX,
404 // 0x1FA is brake control, 0x30C is acc hud, 0x33D is lkas hud
405 33792 int bus_fwd = -1;
406
407
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 22528 times.
33792 if (bus_num == 0) {
408 11264 bus_fwd = 2;
409 }
410
411
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 22528 times.
33792 if (bus_num == 2) {
412 // block stock lkas messages and stock acc messages (if OP is doing ACC)
413
6/6
✓ Branch 0 taken 11260 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11256 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 11252 times.
11264 bool is_lkas_msg = (addr == 0xE4) || (addr == 0x194) || (addr == 0x33D);
414 11264 bool is_acc_hud_msg = addr == 0x30C;
415 11264 bool is_brake_msg = addr == 0x1FA;
416
8/8
✓ Branch 0 taken 11252 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 11248 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 11244 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
11264 bool block_fwd = is_lkas_msg || is_acc_hud_msg || (is_brake_msg && !honda_fwd_brake);
417
2/2
✓ Branch 0 taken 11246 times.
✓ Branch 1 taken 18 times.
11264 if (!block_fwd) {
418 11246 bus_fwd = 0;
419 }
420 }
421
422 33792 return bus_fwd;
423 }
424
425 50688 static int honda_bosch_fwd_hook(int bus_num, int addr) {
426 50688 int bus_fwd = -1;
427
428
2/2
✓ Branch 0 taken 16896 times.
✓ Branch 1 taken 33792 times.
50688 if (bus_num == 0) {
429 16896 bus_fwd = 2;
430 }
431
2/2
✓ Branch 0 taken 16896 times.
✓ Branch 1 taken 33792 times.
50688 if (bus_num == 2) {
432
10/10
✓ Branch 0 taken 16890 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 16884 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 16878 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 16872 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 16866 times.
16896 bool is_lkas_msg = (addr == 0xE4) || (addr == 0xE5) || (addr == 0x33D) || (addr == 0x33DA) || (addr == 0x33DB);
433
8/8
✓ Branch 0 taken 16890 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 16884 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 4 times.
16896 bool is_acc_msg = ((addr == 0x1C8) || (addr == 0x30C)) && honda_bosch_radarless && honda_bosch_long;
434
4/4
✓ Branch 0 taken 16866 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 16864 times.
16896 bool block_msg = is_lkas_msg || is_acc_msg;
435
2/2
✓ Branch 0 taken 16864 times.
✓ Branch 1 taken 32 times.
16896 if (!block_msg) {
436 16864 bus_fwd = 0;
437 }
438 }
439
440 50688 return bus_fwd;
441 }
442
443 const safety_hooks honda_nidec_hooks = {
444 .init = honda_nidec_init,
445 .rx = honda_rx_hook,
446 .tx = honda_tx_hook,
447 .fwd = honda_nidec_fwd_hook,
448 .get_counter = honda_get_counter,
449 .get_checksum = honda_get_checksum,
450 .compute_checksum = honda_compute_checksum,
451 };
452
453 const safety_hooks honda_bosch_hooks = {
454 .init = honda_bosch_init,
455 .rx = honda_rx_hook,
456 .tx = honda_tx_hook,
457 .fwd = honda_bosch_fwd_hook,
458 .get_counter = honda_get_counter,
459 .get_checksum = honda_get_checksum,
460 .compute_checksum = honda_compute_checksum,
461 };
462