GCC Code Coverage Report


Directory: ./
File: safety/safety_chrysler.h
Date: 2025-02-27 09:09:49
Exec Total Coverage
Lines: 94 94 100.0%
Functions: 7 7 100.0%
Branches: 81 82 98.8%

Line Branch Exec Source
1 #pragma once
2
3 #include "safety_declarations.h"
4
5 typedef struct {
6 const int EPS_2;
7 const int ESP_1;
8 const int ESP_8;
9 const int ECM_5;
10 const int DAS_3;
11 const int DAS_6;
12 const int LKAS_COMMAND;
13 const int CRUISE_BUTTONS;
14 } ChryslerAddrs;
15
16 typedef enum {
17 CHRYSLER_RAM_DT,
18 CHRYSLER_RAM_HD,
19 CHRYSLER_PACIFICA, // plus Jeep
20 } ChryslerPlatform;
21 static ChryslerPlatform chrysler_platform;
22 static const ChryslerAddrs *chrysler_addrs;
23
24 157 static uint32_t chrysler_get_checksum(const CANPacket_t *to_push) {
25 157 int checksum_byte = GET_LEN(to_push) - 1U;
26 157 return (uint8_t)(GET_BYTE(to_push, checksum_byte));
27 }
28
29 157 static uint32_t chrysler_compute_checksum(const CANPacket_t *to_push) {
30 // TODO: clean this up
31 // http://illmatics.com/Remote%20Car%20Hacking.pdf
32 157 uint8_t checksum = 0xFFU;
33 157 int len = GET_LEN(to_push);
34
2/2
✓ Branch 0 taken 1099 times.
✓ Branch 1 taken 157 times.
1256 for (int j = 0; j < (len - 1); j++) {
35 1099 uint8_t shift = 0x80U;
36 1099 uint8_t curr = (uint8_t)GET_BYTE(to_push, j);
37
2/2
✓ Branch 0 taken 8792 times.
✓ Branch 1 taken 1099 times.
9891 for (int i=0; i<8; i++) {
38 8792 uint8_t bit_sum = curr & shift;
39 8792 uint8_t temp_chk = checksum & 0x80U;
40
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 8437 times.
8792 if (bit_sum != 0U) {
41 355 bit_sum = 0x1C;
42
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 229 times.
355 if (temp_chk != 0U) {
43 126 bit_sum = 1;
44 }
45 355 checksum = checksum << 1;
46 355 temp_chk = checksum | 1U;
47 355 bit_sum ^= temp_chk;
48 } else {
49
2/2
✓ Branch 0 taken 4261 times.
✓ Branch 1 taken 4176 times.
8437 if (temp_chk != 0U) {
50 4261 bit_sum = 0x1D;
51 }
52 8437 checksum = checksum << 1;
53 8437 bit_sum ^= checksum;
54 }
55 8792 checksum = bit_sum;
56 8792 shift = shift >> 1;
57 }
58 }
59 157 return (uint8_t)(~checksum);
60 }
61
62 157 static uint8_t chrysler_get_counter(const CANPacket_t *to_push) {
63 157 return (uint8_t)(GET_BYTE(to_push, 6) >> 4);
64 }
65
66 25480 static void chrysler_rx_hook(const CANPacket_t *to_push) {
67 25480 const int bus = GET_BUS(to_push);
68 25480 const int addr = GET_ADDR(to_push);
69
70 // Measured EPS torque
71
4/4
✓ Branch 0 taken 8572 times.
✓ Branch 1 taken 16908 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 8530 times.
25480 if ((bus == 0) && (addr == chrysler_addrs->EPS_2)) {
72 42 int torque_meas_new = ((GET_BYTE(to_push, 4) & 0x7U) << 8) + GET_BYTE(to_push, 5) - 1024U;
73 42 update_sample(&torque_meas, torque_meas_new);
74 }
75
76 // enter controls on rising edge of ACC, exit controls on ACC off
77
2/2
✓ Branch 0 taken 8494 times.
✓ Branch 1 taken 16986 times.
25480 const int das_3_bus = (chrysler_platform == CHRYSLER_PACIFICA) ? 0 : 2;
78
4/4
✓ Branch 0 taken 8506 times.
✓ Branch 1 taken 16974 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 8485 times.
25480 if ((bus == das_3_bus) && (addr == chrysler_addrs->DAS_3)) {
79 21 bool cruise_engaged = GET_BIT(to_push, 21U);
80 21 pcm_cruise_check(cruise_engaged);
81 }
82
83 // TODO: use the same message for both
84 // update vehicle moving
85
6/6
✓ Branch 0 taken 16986 times.
✓ Branch 1 taken 8494 times.
✓ Branch 2 taken 5710 times.
✓ Branch 3 taken 11276 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 5696 times.
25480 if ((chrysler_platform != CHRYSLER_PACIFICA) && (bus == 0) && (addr == chrysler_addrs->ESP_8)) {
86 14 vehicle_moving = ((GET_BYTE(to_push, 4) << 8) + GET_BYTE(to_push, 5)) != 0U;
87 }
88
6/6
✓ Branch 0 taken 8494 times.
✓ Branch 1 taken 16986 times.
✓ Branch 2 taken 2862 times.
✓ Branch 3 taken 5632 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2854 times.
25480 if ((chrysler_platform == CHRYSLER_PACIFICA) && (bus == 0) && (addr == 514)) {
89 8 int speed_l = (GET_BYTE(to_push, 0) << 4) + (GET_BYTE(to_push, 1) >> 4);
90 8 int speed_r = (GET_BYTE(to_push, 2) << 4) + (GET_BYTE(to_push, 3) >> 4);
91
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 vehicle_moving = (speed_l != 0) || (speed_r != 0);
92 }
93
94 // exit controls on rising edge of gas press
95
4/4
✓ Branch 0 taken 8572 times.
✓ Branch 1 taken 16908 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 8542 times.
25480 if ((bus == 0) && (addr == chrysler_addrs->ECM_5)) {
96 30 gas_pressed = GET_BYTE(to_push, 0U) != 0U;
97 }
98
99 // exit controls on rising edge of brake press
100
4/4
✓ Branch 0 taken 8572 times.
✓ Branch 1 taken 16908 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 8536 times.
25480 if ((bus == 0) && (addr == chrysler_addrs->ESP_1)) {
101 36 brake_pressed = ((GET_BYTE(to_push, 0U) & 0xFU) >> 2U) == 1U;
102 }
103
104
4/4
✓ Branch 0 taken 8572 times.
✓ Branch 1 taken 16908 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8569 times.
25480 generic_rx_checks((bus == 0) && (addr == chrysler_addrs->LKAS_COMMAND));
105 25480 }
106
107 10816 static bool chrysler_tx_hook(const CANPacket_t *to_send) {
108 const SteeringLimits CHRYSLER_STEERING_LIMITS = {
109 .max_steer = 261,
110 .max_rt_delta = 112,
111 .max_rt_interval = 250000,
112 .max_rate_up = 3,
113 .max_rate_down = 3,
114 .max_torque_error = 80,
115 .type = TorqueMotorLimited,
116 };
117
118 const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = {
119 .max_steer = 350,
120 .max_rt_delta = 112,
121 .max_rt_interval = 250000,
122 .max_rate_up = 6,
123 .max_rate_down = 6,
124 .max_torque_error = 80,
125 .type = TorqueMotorLimited,
126 };
127
128 const SteeringLimits CHRYSLER_RAM_HD_STEERING_LIMITS = {
129 .max_steer = 361,
130 .max_rt_delta = 182,
131 .max_rt_interval = 250000,
132 .max_rate_up = 14,
133 .max_rate_down = 14,
134 .max_torque_error = 80,
135 .type = TorqueMotorLimited,
136 };
137
138 10816 bool tx = true;
139 10816 int addr = GET_ADDR(to_send);
140
141 // STEERING
142
2/2
✓ Branch 0 taken 10789 times.
✓ Branch 1 taken 27 times.
10816 if (addr == chrysler_addrs->LKAS_COMMAND) {
143 10789 int start_byte = (chrysler_platform == CHRYSLER_PACIFICA) ? 0 : 1;
144 10789 int desired_torque = ((GET_BYTE(to_send, start_byte) & 0x7U) << 8) | GET_BYTE(to_send, start_byte + 1);
145 10789 desired_torque -= 1024;
146
147
2/2
✓ Branch 0 taken 6974 times.
✓ Branch 1 taken 3815 times.
17763 const SteeringLimits limits = (chrysler_platform == CHRYSLER_PACIFICA) ? CHRYSLER_STEERING_LIMITS :
148
2/2
✓ Branch 0 taken 3570 times.
✓ Branch 1 taken 3404 times.
6974 (chrysler_platform == CHRYSLER_RAM_DT) ? CHRYSLER_RAM_DT_STEERING_LIMITS : CHRYSLER_RAM_HD_STEERING_LIMITS;
149
150
2/2
✓ Branch 0 taken 3815 times.
✓ Branch 1 taken 6974 times.
10789 bool steer_req = (chrysler_platform == CHRYSLER_PACIFICA) ? GET_BIT(to_send, 4U) : (GET_BYTE(to_send, 3) & 0x7U) == 2U;
151
2/2
✓ Branch 1 taken 6562 times.
✓ Branch 2 taken 4227 times.
10789 if (steer_torque_cmd_checks(desired_torque, steer_req, limits)) {
152 6562 tx = false;
153 }
154 }
155
156 // FORCE CANCEL: only the cancel button press is allowed
157
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10792 times.
10816 if (addr == chrysler_addrs->CRUISE_BUTTONS) {
158 24 const bool is_cancel = GET_BYTE(to_send, 0) == 1U;
159 24 const bool is_resume = GET_BYTE(to_send, 0) == 0x10U;
160
6/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
24 const bool allowed = is_cancel || (is_resume && controls_allowed);
161
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 9 times.
24 if (!allowed) {
162 15 tx = false;
163 }
164 }
165
166 10816 return tx;
167 }
168
169 25344 static int chrysler_fwd_hook(int bus_num, int addr) {
170 25344 int bus_fwd = -1;
171
172 // forward to camera
173
2/2
✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 16896 times.
25344 if (bus_num == 0) {
174 8448 bus_fwd = 2;
175 }
176
177 // forward all messages from camera except LKAS messages
178
4/4
✓ Branch 0 taken 25335 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 25326 times.
25344 const bool is_lkas = ((addr == chrysler_addrs->LKAS_COMMAND) || (addr == chrysler_addrs->DAS_6));
179
4/4
✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 16896 times.
✓ Branch 2 taken 8442 times.
✓ Branch 3 taken 6 times.
25344 if ((bus_num == 2) && !is_lkas){
180 8442 bus_fwd = 0;
181 }
182
183 25344 return bus_fwd;
184 }
185
186 99 static safety_config chrysler_init(uint16_t param) {
187
188 99 const uint32_t CHRYSLER_PARAM_RAM_DT = 1U; // set for Ram DT platform
189
190 // CAN messages for Chrysler/Jeep platforms
191 static const ChryslerAddrs CHRYSLER_ADDRS = {
192 .EPS_2 = 0x220, // EPS driver input torque
193 .ESP_1 = 0x140, // Brake pedal and vehicle speed
194 .ESP_8 = 0x11C, // Brake pedal and vehicle speed
195 .ECM_5 = 0x22F, // Throttle position sensor
196 .DAS_3 = 0x1F4, // ACC engagement states from DASM
197 .DAS_6 = 0x2A6, // LKAS HUD and auto headlight control from DASM
198 .LKAS_COMMAND = 0x292, // LKAS controls from DASM
199 .CRUISE_BUTTONS = 0x23B, // Cruise control buttons
200 };
201
202 // CAN messages for the 5th gen RAM DT platform
203 static const ChryslerAddrs CHRYSLER_RAM_DT_ADDRS = {
204 .EPS_2 = 0x31, // EPS driver input torque
205 .ESP_1 = 0x83, // Brake pedal and vehicle speed
206 .ESP_8 = 0x79, // Brake pedal and vehicle speed
207 .ECM_5 = 0x9D, // Throttle position sensor
208 .DAS_3 = 0x99, // ACC engagement states from DASM
209 .DAS_6 = 0xFA, // LKAS HUD and auto headlight control from DASM
210 .LKAS_COMMAND = 0xA6, // LKAS controls from DASM
211 .CRUISE_BUTTONS = 0xB1, // Cruise control buttons
212 };
213
214 static RxCheck chrysler_ram_dt_rx_checks[] = {
215 {.msg = {{CHRYSLER_RAM_DT_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
216 {.msg = {{CHRYSLER_RAM_DT_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
217 {.msg = {{CHRYSLER_RAM_DT_ADDRS.ESP_8, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
218 {.msg = {{CHRYSLER_RAM_DT_ADDRS.ECM_5, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
219 {.msg = {{CHRYSLER_RAM_DT_ADDRS.DAS_3, 2, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
220 };
221
222 static RxCheck chrysler_rx_checks[] = {
223 {.msg = {{CHRYSLER_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
224 {.msg = {{CHRYSLER_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
225 //{.msg = {{ESP_8, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}}},
226 {.msg = {{514, 0, 8, .check_checksum = false, .max_counter = 0U, .frequency = 100U}, { 0 }, { 0 }}},
227 {.msg = {{CHRYSLER_ADDRS.ECM_5, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
228 {.msg = {{CHRYSLER_ADDRS.DAS_3, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
229 };
230
231 static const CanMsg CHRYSLER_TX_MSGS[] = {
232 {CHRYSLER_ADDRS.CRUISE_BUTTONS, 0, 3},
233 {CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6},
234 {CHRYSLER_ADDRS.DAS_6, 0, 8},
235 };
236
237 static const CanMsg CHRYSLER_RAM_DT_TX_MSGS[] = {
238 {CHRYSLER_RAM_DT_ADDRS.CRUISE_BUTTONS, 2, 3},
239 {CHRYSLER_RAM_DT_ADDRS.LKAS_COMMAND, 0, 8},
240 {CHRYSLER_RAM_DT_ADDRS.DAS_6, 0, 8},
241 };
242
243 #ifdef ALLOW_DEBUG
244 // CAN messages for the 5th gen RAM HD platform
245 static const ChryslerAddrs CHRYSLER_RAM_HD_ADDRS = {
246 .EPS_2 = 0x220, // EPS driver input torque
247 .ESP_1 = 0x140, // Brake pedal and vehicle speed
248 .ESP_8 = 0x11C, // Brake pedal and vehicle speed
249 .ECM_5 = 0x22F, // Throttle position sensor
250 .DAS_3 = 0x1F4, // ACC engagement states from DASM
251 .DAS_6 = 0x275, // LKAS HUD and auto headlight control from DASM
252 .LKAS_COMMAND = 0x276, // LKAS controls from DASM
253 .CRUISE_BUTTONS = 0x23A, // Cruise control buttons
254 };
255
256 static RxCheck chrysler_ram_hd_rx_checks[] = {
257 {.msg = {{CHRYSLER_RAM_HD_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
258 {.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
259 {.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_8, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
260 {.msg = {{CHRYSLER_RAM_HD_ADDRS.ECM_5, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
261 {.msg = {{CHRYSLER_RAM_HD_ADDRS.DAS_3, 2, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
262 };
263
264 static const CanMsg CHRYSLER_RAM_HD_TX_MSGS[] = {
265 {CHRYSLER_RAM_HD_ADDRS.CRUISE_BUTTONS, 2, 3},
266 {CHRYSLER_RAM_HD_ADDRS.LKAS_COMMAND, 0, 8},
267 {CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8},
268 };
269
270 99 const uint32_t CHRYSLER_PARAM_RAM_HD = 2U; // set for Ram HD platform
271 99 bool enable_ram_hd = GET_FLAG(param, CHRYSLER_PARAM_RAM_HD);
272 #endif
273
274 safety_config ret;
275
276 99 bool enable_ram_dt = GET_FLAG(param, CHRYSLER_PARAM_RAM_DT);
277
278
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 66 times.
99 if (enable_ram_dt) {
279 33 chrysler_platform = CHRYSLER_RAM_DT;
280 33 chrysler_addrs = &CHRYSLER_RAM_DT_ADDRS;
281 33 ret = BUILD_SAFETY_CFG(chrysler_ram_dt_rx_checks, CHRYSLER_RAM_DT_TX_MSGS);
282 #ifdef ALLOW_DEBUG
283
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 } else if (enable_ram_hd) {
284 33 chrysler_platform = CHRYSLER_RAM_HD;
285 33 chrysler_addrs = &CHRYSLER_RAM_HD_ADDRS;
286 33 ret = BUILD_SAFETY_CFG(chrysler_ram_hd_rx_checks, CHRYSLER_RAM_HD_TX_MSGS);
287 #endif
288 } else {
289 33 chrysler_platform = CHRYSLER_PACIFICA;
290 33 chrysler_addrs = &CHRYSLER_ADDRS;
291 33 ret = BUILD_SAFETY_CFG(chrysler_rx_checks, CHRYSLER_TX_MSGS);
292 }
293 99 return ret;
294 }
295
296 const safety_hooks chrysler_hooks = {
297 .init = chrysler_init,
298 .rx = chrysler_rx_hook,
299 .tx = chrysler_tx_hook,
300 .fwd = chrysler_fwd_hook,
301 .get_counter = chrysler_get_counter,
302 .get_checksum = chrysler_get_checksum,
303 .compute_checksum = chrysler_compute_checksum,
304 };
305