Skip to content

Commit 4ecc6b3

Browse files
committed
Cleanup: avoid unnecessary bus checks in rx hooks
1 parent c7d0d5f commit 4ecc6b3

6 files changed

+62
-70
lines changed

board/safety/safety_chrysler.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,19 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
7676
chrysler_get_checksum, chrysler_compute_checksum,
7777
chrysler_get_counter);
7878

79-
if (valid) {
80-
int bus = GET_BUS(to_push);
79+
if (valid && (GET_BUS(to_push) == 0)) {
8180
int addr = GET_ADDR(to_push);
8281

8382
// Measured eps torque
84-
if ((addr == 544) && (bus == 0)) {
83+
if (addr == 544) {
8584
int torque_meas_new = ((GET_BYTE(to_push, 4) & 0x7U) << 8) + GET_BYTE(to_push, 5) - 1024U;
8685

8786
// update array of samples
8887
update_sample(&chrysler_torque_meas, torque_meas_new);
8988
}
9089

9190
// enter controls on rising edge of ACC, exit controls on ACC off
92-
if ((addr == 500) && (bus == 0)) {
91+
if (addr == 500) {
9392
int cruise_engaged = ((GET_BYTE(to_push, 2) & 0x38) >> 3) == 7;
9493
if (cruise_engaged && !chrysler_cruise_engaged_last) {
9594
controls_allowed = 1;
@@ -101,14 +100,14 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
101100
}
102101

103102
// update speed
104-
if ((addr == 514) && (bus == 0)) {
103+
if (addr == 514) {
105104
int speed_l = (GET_BYTE(to_push, 0) << 4) + (GET_BYTE(to_push, 1) >> 4);
106105
int speed_r = (GET_BYTE(to_push, 2) << 4) + (GET_BYTE(to_push, 3) >> 4);
107106
chrysler_speed = (speed_l + speed_r) / 2;
108107
}
109108

110109
// exit controls on rising edge of gas press
111-
if ((addr == 308) && (bus == 0)) {
110+
if (addr == 308) {
112111
bool gas = (GET_BYTE(to_push, 5) & 0x7F) != 0;
113112
if (gas && !chrysler_gas_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
114113
controls_allowed = 0;
@@ -117,7 +116,7 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
117116
}
118117

119118
// exit controls on rising edge of brake press
120-
if ((addr == 320) && (bus == 0)) {
119+
if (addr == 320) {
121120
bool brake = (GET_BYTE(to_push, 0) & 0x7) == 5;
122121
if (brake && (!chrysler_brake_prev || (chrysler_speed > CHRYSLER_STANDSTILL_THRSLD))) {
123122
controls_allowed = 0;
@@ -126,7 +125,7 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
126125
}
127126

128127
// check if stock camera ECU is on bus 0
129-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == 0x292)) {
128+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == 0x292)) {
130129
relay_malfunction = true;
131130
}
132131
}

board/safety/safety_gm.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ static int gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
4646
bool valid = addr_safety_check(to_push, gm_rx_checks, GM_RX_CHECK_LEN,
4747
NULL, NULL, NULL);
4848

49-
if (valid) {
50-
int bus = GET_BUS(to_push);
49+
if (valid && (GET_BUS(to_push) == 0)) {
5150
int addr = GET_ADDR(to_push);
5251

5352
if (addr == 388) {
@@ -115,7 +114,7 @@ static int gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
115114
// on powertrain bus.
116115
// 384 = ASCMLKASteeringCmd
117116
// 715 = ASCMGasRegenCmd
118-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && ((addr == 384) || (addr == 715))) {
117+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && ((addr == 384) || (addr == 715))) {
119118
relay_malfunction = true;
120119
}
121120
}

board/safety/safety_hyundai.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2727
bool valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN,
2828
NULL, NULL, NULL);
2929

30-
if (valid) {
31-
int bus = GET_BUS(to_push);
30+
if (valid && GET_BUS(to_push) == 0) {
3231
int addr = GET_ADDR(to_push);
3332

3433
if (addr == 897) {
@@ -51,7 +50,7 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
5150
}
5251

5352
// exit controls on rising edge of gas press
54-
if ((addr == 608) && (bus == 0)) {
53+
if (addr == 608) {
5554
bool gas = (GET_BYTE(to_push, 7) >> 6) != 0;
5655
if (gas && ! hyundai_gas_last) {
5756
controls_allowed = 0;
@@ -60,7 +59,7 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
6059
}
6160

6261
// check if stock camera ECU is on bus 0
63-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == 832)) {
62+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == 832)) {
6463
relay_malfunction = true;
6564
}
6665
}

board/safety/safety_subaru.h

+35-38
Original file line numberDiff line numberDiff line change
@@ -64,51 +64,48 @@ static int subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
6464
NULL, NULL, NULL);
6565
}
6666

67-
if (valid) {
68-
int bus = GET_BUS(to_push);
67+
if (valid && (GET_BUS(to_push) == 0)) {
6968
int addr = GET_ADDR(to_push);
70-
if (bus == 0) {
71-
if (((addr == 0x119) && subaru_global) ||
72-
((addr == 0x371) && !subaru_global)) {
73-
int torque_driver_new;
74-
if (subaru_global) {
75-
torque_driver_new = ((GET_BYTES_04(to_push) >> 16) & 0x7FF);
76-
} else {
77-
torque_driver_new = (GET_BYTE(to_push, 3) >> 5) + (GET_BYTE(to_push, 4) << 3);
78-
}
79-
torque_driver_new = to_signed(torque_driver_new, 11);
80-
update_sample(&subaru_torque_driver, torque_driver_new);
69+
if (((addr == 0x119) && subaru_global) ||
70+
((addr == 0x371) && !subaru_global)) {
71+
int torque_driver_new;
72+
if (subaru_global) {
73+
torque_driver_new = ((GET_BYTES_04(to_push) >> 16) & 0x7FF);
74+
} else {
75+
torque_driver_new = (GET_BYTE(to_push, 3) >> 5) + (GET_BYTE(to_push, 4) << 3);
8176
}
77+
torque_driver_new = to_signed(torque_driver_new, 11);
78+
update_sample(&subaru_torque_driver, torque_driver_new);
79+
}
8280

83-
// enter controls on rising edge of ACC, exit controls on ACC off
84-
if (((addr == 0x240) && subaru_global) ||
85-
((addr == 0x144) && !subaru_global)) {
86-
int bit_shift = subaru_global ? 9 : 17;
87-
int cruise_engaged = ((GET_BYTES_48(to_push) >> bit_shift) & 1);
88-
if (cruise_engaged && !subaru_cruise_engaged_last) {
89-
controls_allowed = 1;
90-
}
91-
if (!cruise_engaged) {
92-
controls_allowed = 0;
93-
}
94-
subaru_cruise_engaged_last = cruise_engaged;
81+
// enter controls on rising edge of ACC, exit controls on ACC off
82+
if (((addr == 0x240) && subaru_global) ||
83+
((addr == 0x144) && !subaru_global)) {
84+
int bit_shift = subaru_global ? 9 : 17;
85+
int cruise_engaged = ((GET_BYTES_48(to_push) >> bit_shift) & 1);
86+
if (cruise_engaged && !subaru_cruise_engaged_last) {
87+
controls_allowed = 1;
9588
}
96-
97-
// exit controls on rising edge of gas press
98-
if (((addr == 0x40) && subaru_global) ||
99-
((addr == 0x140) && !subaru_global)) {
100-
int byte = subaru_global ? 4 : 0;
101-
bool gas = GET_BYTE(to_push, byte) != 0;
102-
if (gas && !subaru_gas_last) {
103-
controls_allowed = 0;
104-
}
105-
subaru_gas_last = gas;
89+
if (!cruise_engaged) {
90+
controls_allowed = 0;
10691
}
92+
subaru_cruise_engaged_last = cruise_engaged;
93+
}
10794

108-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) &&
109-
(((addr == 0x122) && subaru_global) || ((addr == 0x164) && !subaru_global))) {
110-
relay_malfunction = true;
95+
// exit controls on rising edge of gas press
96+
if (((addr == 0x40) && subaru_global) ||
97+
((addr == 0x140) && !subaru_global)) {
98+
int byte = subaru_global ? 4 : 0;
99+
bool gas = GET_BYTE(to_push, byte) != 0;
100+
if (gas && !subaru_gas_last) {
101+
controls_allowed = 0;
111102
}
103+
subaru_gas_last = gas;
104+
}
105+
106+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) &&
107+
(((addr == 0x122) && subaru_global) || ((addr == 0x164) && !subaru_global))) {
108+
relay_malfunction = true;
112109
}
113110
}
114111
return valid;

board/safety/safety_toyota.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,11 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
6565

6666
bool valid = addr_safety_check(to_push, toyota_rx_checks, TOYOTA_RX_CHECKS_LEN,
6767
toyota_get_checksum, toyota_compute_checksum, NULL);
68-
if (valid) {
69-
int bus = GET_BUS(to_push);
68+
if (valid && (GET_BUS(to_push) == 0)) {
7069
int addr = GET_ADDR(to_push);
7170

7271
// get eps motor torque (0.66 factor in dbc)
73-
if ((addr == 0x260) && (bus == 0)) {
72+
if (addr == 0x260) {
7473
int torque_meas_new = (GET_BYTE(to_push, 5) << 8) | GET_BYTE(to_push, 6);
7574
torque_meas_new = to_signed(torque_meas_new, 16);
7675

@@ -86,7 +85,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
8685
}
8786

8887
// enter controls on rising edge of ACC, exit controls on ACC off
89-
if ((addr == 0x1D2) && (bus == 0)) {
88+
if (addr == 0x1D2) {
9089
// 5th bit is CRUISE_ACTIVE
9190
int cruise_engaged = GET_BYTE(to_push, 0) & 0x20;
9291
if (!cruise_engaged) {
@@ -99,7 +98,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
9998
}
10099

101100
// sample speed
102-
if ((addr == 0xaa) && (bus == 0)) {
101+
if (addr == 0xaa) {
103102
int speed = 0;
104103
// sum 4 wheel speeds
105104
for (int i=0; i<8; i+=2) {
@@ -111,7 +110,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
111110

112111
// exit controls on rising edge of brake pedal
113112
// most cars have brake_pressed on 0x226, corolla and rav4 on 0x224
114-
if (((addr == 0x224) || (addr == 0x226)) && (bus == 0)) {
113+
if ((addr == 0x224) || (addr == 0x226)) {
115114
int byte = (addr == 0x224) ? 0 : 4;
116115
bool brake = ((GET_BYTE(to_push, byte) >> 5) & 1) != 0;
117116
if (brake && (!toyota_brake_prev || toyota_moving)) {
@@ -121,7 +120,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
121120
}
122121

123122
// exit controls on rising edge of interceptor gas press
124-
if ((addr == 0x201) && (bus == 0)) {
123+
if (addr == 0x201) {
125124
gas_interceptor_detected = 1;
126125
int gas_interceptor = GET_INTERCEPTOR(to_push);
127126
if ((gas_interceptor > TOYOTA_GAS_INTERCEPTOR_THRSLD) &&
@@ -132,7 +131,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
132131
}
133132

134133
// exit controls on rising edge of gas press
135-
if ((addr == 0x2C1) && (bus == 0)) {
134+
if (addr == 0x2C1) {
136135
bool gas = GET_BYTE(to_push, 6) != 0;
137136
if (gas && !toyota_gas_prev && !gas_interceptor_detected) {
138137
controls_allowed = 0;
@@ -141,7 +140,7 @@ static int toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
141140
}
142141

143142
// 0x2E4 is lkas cmd. If it is on bus 0, then relay is unexpectedly closed
144-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == 0x2E4) && (bus == 0)) {
143+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == 0x2E4)) {
145144
relay_malfunction = true;
146145
}
147146
}

board/safety/safety_volkswagen.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,13 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
101101
bool valid = addr_safety_check(to_push, volkswagen_mqb_rx_checks, VOLKSWAGEN_MQB_RX_CHECKS_LEN,
102102
volkswagen_get_checksum, volkswagen_mqb_compute_crc, volkswagen_get_counter);
103103

104-
if (valid) {
105-
int bus = GET_BUS(to_push);
104+
if (valid && (GET_BUS(to_push) == 0)) {
106105
int addr = GET_ADDR(to_push);
107106

108107
// Update in-motion state by sampling front wheel speeds
109108
// Signal: ESP_19.ESP_VL_Radgeschw_02 (front left) in scaled km/h
110109
// Signal: ESP_19.ESP_VR_Radgeschw_02 (front right) in scaled km/h
111-
if ((bus == 0) && (addr == MSG_ESP_19)) {
110+
if (addr == MSG_ESP_19) {
112111
int wheel_speed_fl = GET_BYTE(to_push, 4) | (GET_BYTE(to_push, 5) << 8);
113112
int wheel_speed_fr = GET_BYTE(to_push, 6) | (GET_BYTE(to_push, 7) << 8);
114113
// Check for average front speed in excess of 0.3m/s, 1.08km/h
@@ -119,7 +118,7 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
119118
// Update driver input torque samples
120119
// Signal: EPS_01.Driver_Strain (absolute torque)
121120
// Signal: EPS_01.Driver_Strain_VZ (direction)
122-
if ((bus == 0) && (addr == MSG_EPS_01)) {
121+
if (addr == MSG_EPS_01) {
123122
int torque_driver_new = GET_BYTE(to_push, 5) | ((GET_BYTE(to_push, 6) & 0x1F) << 8);
124123
int sign = (GET_BYTE(to_push, 6) & 0x80) >> 7;
125124
if (sign == 1) {
@@ -130,14 +129,14 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
130129

131130
// Update ACC status from drivetrain coordinator for controls-allowed state
132131
// Signal: TSK_06.TSK_Status
133-
if ((bus == 0) && (addr == MSG_TSK_06)) {
132+
if (addr == MSG_TSK_06) {
134133
int acc_status = (GET_BYTE(to_push, 3) & 0x7);
135134
controls_allowed = ((acc_status == 3) || (acc_status == 4) || (acc_status == 5)) ? 1 : 0;
136135
}
137136

138137
// Exit controls on rising edge of gas press
139138
// Signal: Motor_20.MO_Fahrpedalrohwert_01
140-
if ((bus == 0) && (addr == MSG_MOTOR_20)) {
139+
if (addr == MSG_MOTOR_20) {
141140
int gas = (GET_BYTES_04(to_push) >> 12) & 0xFF;
142141
if ((gas > 0) && (volkswagen_gas_prev == 0)) {
143142
controls_allowed = 0;
@@ -147,7 +146,7 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
147146

148147
// Exit controls on rising edge of brake press
149148
// Signal: ESP_05.ESP_Fahrer_bremst
150-
if ((bus == 0) && (addr == MSG_ESP_05)) {
149+
if (addr == MSG_ESP_05) {
151150
bool brake_pressed = (GET_BYTE(to_push, 3) & 0x4) >> 2;
152151
if (brake_pressed && (!(volkswagen_brake_pressed_prev) || volkswagen_moving)) {
153152
controls_allowed = 0;
@@ -156,7 +155,7 @@ static int volkswagen_mqb_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
156155
}
157156

158157
// If there are HCA messages on bus 0 not sent by OP, there's a relay problem
159-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == MSG_HCA_01)) {
158+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == MSG_HCA_01)) {
160159
relay_malfunction = true;
161160
}
162161
}

0 commit comments

Comments
 (0)