GCC Code Coverage Report


Directory: ./
File: safety/safety_rivian.h
Date: 2025-02-27 09:09:49
Exec Total Coverage
Lines: 55 55 100.0%
Functions: 4 4 100.0%
Branches: 44 44 100.0%

Line Branch Exec Source
1 #pragma once
2
3 #include "safety_declarations.h"
4
5 static bool rivian_longitudinal = false;
6
7 21936 static void rivian_rx_hook(const CANPacket_t *to_push) {
8 21936 int bus = GET_BUS(to_push);
9 21936 int addr = GET_ADDR(to_push);
10
11
2/2
✓ Branch 0 taken 10658 times.
✓ Branch 1 taken 11278 times.
21936 if (bus == 0) {
12 // Vehicle speed
13
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 10642 times.
10658 if (addr == 0x208) {
14 16 vehicle_moving = GET_BYTE(to_push, 6) | GET_BYTE(to_push, 7);
15 }
16
17 // Driver torque
18
2/2
✓ Branch 0 taken 4970 times.
✓ Branch 1 taken 5688 times.
10658 if (addr == 0x380) {
19 4970 int torque_driver_new = (((GET_BYTE(to_push, 2) << 4) | (GET_BYTE(to_push, 3) >> 4))) - 2050U;
20 4970 update_sample(&torque_driver, torque_driver_new);
21 }
22
23 // Gas pressed
24
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10636 times.
10658 if (addr == 0x150) {
25 22 gas_pressed = GET_BYTE(to_push, 3) | (GET_BYTE(to_push, 4) & 0xC0U);
26 }
27
28 // Brake pressed
29
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 10632 times.
10658 if (addr == 0x38f) {
30 26 brake_pressed = GET_BIT(to_push, 23U);
31 }
32
33 10658 generic_rx_checks(addr == 0x120); // ACM_lkaHbaCmd
34
2/2
✓ Branch 0 taken 5329 times.
✓ Branch 1 taken 5329 times.
10658 if (rivian_longitudinal) {
35 5329 generic_rx_checks(addr == 0x160); // ACM_longitudinalRequest
36 }
37 }
38
39
2/2
✓ Branch 0 taken 5646 times.
✓ Branch 1 taken 16290 times.
21936 if (bus == 2) {
40 // Cruise state
41
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5630 times.
5646 if (addr == 0x100) {
42 16 pcm_cruise_check(GET_BIT(to_push, 21U));
43 }
44 }
45 21936 }
46
47 5498 static bool rivian_tx_hook(const CANPacket_t *to_send) {
48 const SteeringLimits RIVIAN_STEERING_LIMITS = {
49 .max_steer = 250,
50 .max_rate_up = 3,
51 .max_rate_down = 5,
52 .max_rt_delta = 125,
53 .max_rt_interval = 250000,
54 .driver_torque_multiplier = 2,
55 .driver_torque_allowance = 100,
56 .type = TorqueDriverLimited,
57 };
58
59 const LongitudinalLimits RIVIAN_LONG_LIMITS = {
60 .max_accel = 200,
61 .min_accel = -350,
62 .inactive_accel = 0,
63 };
64
65 5498 bool tx = true;
66 5498 int bus = GET_BUS(to_send);
67
68
2/2
✓ Branch 0 taken 5494 times.
✓ Branch 1 taken 4 times.
5498 if (bus == 0) {
69 5494 int addr = GET_ADDR(to_send);
70
71 // Steering control
72
2/2
✓ Branch 0 taken 4886 times.
✓ Branch 1 taken 608 times.
5494 if (addr == 0x120) {
73 4886 int desired_torque = ((GET_BYTE(to_send, 2) << 3U) | (GET_BYTE(to_send, 3) >> 5U)) - 1024U;
74 4886 bool steer_req = GET_BIT(to_send, 28U);
75
76
2/2
✓ Branch 1 taken 2434 times.
✓ Branch 2 taken 2452 times.
4886 if (steer_torque_cmd_checks(desired_torque, steer_req, RIVIAN_STEERING_LIMITS)) {
77 2434 tx = false;
78 }
79 }
80
81 // Longitudinal control
82
2/2
✓ Branch 0 taken 608 times.
✓ Branch 1 taken 4886 times.
5494 if (addr == 0x160) {
83 608 int raw_accel = ((GET_BYTE(to_send, 2) << 3) | (GET_BYTE(to_send, 3) >> 5)) - 1024U;
84
2/2
✓ Branch 1 taken 376 times.
✓ Branch 2 taken 232 times.
608 if (longitudinal_accel_checks(raw_accel, RIVIAN_LONG_LIMITS)) {
85 376 tx = false;
86 }
87 }
88 }
89
90 5498 return tx;
91 }
92
93 16896 static int rivian_fwd_hook(int bus, int addr) {
94 16896 int bus_fwd = -1;
95 16896 bool block_msg = false;
96
97
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 11264 times.
16896 if (bus == 0) {
98 // SCCM_WheelTouch
99
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5630 times.
5632 if (addr == 0x321) {
100 2 block_msg = true;
101 }
102
103
2/2
✓ Branch 0 taken 5630 times.
✓ Branch 1 taken 2 times.
5632 if (!block_msg) {
104 5630 bus_fwd = 2;
105 }
106 }
107
108
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 11264 times.
16896 if (bus == 2) {
109 // ACM_lkaHbaCmd
110
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5630 times.
5632 if (addr == 0x120) {
111 2 block_msg = true;
112 }
113
114 // ACM_longitudinalRequest
115
4/4
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 2816 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2815 times.
5632 if (rivian_longitudinal && (addr == 0x160)) {
116 1 block_msg = true;
117 }
118
119
2/2
✓ Branch 0 taken 5629 times.
✓ Branch 1 taken 3 times.
5632 if (!block_msg) {
120 5629 bus_fwd = 0;
121 }
122 }
123
124 16896 return bus_fwd;
125 }
126
127 62 static safety_config rivian_init(uint16_t param) {
128 // 0x120 = ACM_lkaHbaCmd, 0x160 = ACM_longitudinalRequest, 0x321 = SCCM_WheelTouch
129 static const CanMsg RIVIAN_TX_MSGS[] = {{0x120, 0, 8}, {0x321, 2, 7}};
130 static const CanMsg RIVIAN_LONG_TX_MSGS[] = {{0x120, 0, 8}, {0x321, 2, 7}, {0x160, 0, 5}};
131
132 static RxCheck rivian_rx_checks[] = {
133 {.msg = {{0x208, 0, 8, .frequency = 50U}, { 0 }, { 0 }}}, // ESP_Status (speed)
134 {.msg = {{0x150, 0, 7, .frequency = 50U}, { 0 }, { 0 }}}, // VDM_PropStatus (gas pedal)
135 {.msg = {{0x38f, 0, 6, .frequency = 50U}, { 0 }, { 0 }}}, // iBESP2 (brakes)
136 {.msg = {{0x100, 2, 8, .frequency = 100U}, { 0 }, { 0 }}}, // ACM_Status (cruise state)
137 };
138
139 UNUSED(param);
140 #ifdef ALLOW_DEBUG
141 62 const int FLAG_RIVIAN_LONG_CONTROL = 1;
142 62 rivian_longitudinal = GET_FLAG(param, FLAG_RIVIAN_LONG_CONTROL);
143 #endif
144
145
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 30 times.
62 return rivian_longitudinal ? BUILD_SAFETY_CFG(rivian_rx_checks, RIVIAN_LONG_TX_MSGS) : \
146 BUILD_SAFETY_CFG(rivian_rx_checks, RIVIAN_TX_MSGS);
147 }
148
149 const safety_hooks rivian_hooks = {
150 .init = rivian_init,
151 .rx = rivian_rx_hook,
152 .tx = rivian_tx_hook,
153 .fwd = rivian_fwd_hook,
154 };
155