Skip to content

Commit

Permalink
match safety enum in cereal (commaai#285)
Browse files Browse the repository at this point in the history
* match safety enum in cereal

* removing tons of hardcoding

* add pythonpath to safety test

* Fix safety test in Docker
  • Loading branch information
rbiasini authored Oct 3, 2019
1 parent a338d39 commit 70219d7
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- run:
name: Run safety test
command: |
docker run panda_safety /bin/bash -c "cd /panda/tests/safety; ./test.sh"
docker run panda_safety /bin/bash -c "cd /panda/tests/safety; PYTHONPATH=/ ./test.sh"
misra-c2012:
machine:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.5.0
v1.5.1
37 changes: 19 additions & 18 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,43 @@ typedef struct {
const safety_hooks *hooks;
} safety_hook_config;

// from cereal.car.CarParams.SafetyModel
#define SAFETY_NOOUTPUT 0U
#define SAFETY_HONDA 1U
#define SAFETY_TOYOTA 2U
#define SAFETY_GM 3U
#define SAFETY_HONDA_BOSCH 4U
#define SAFETY_FORD 5U
#define SAFETY_CADILLAC 6U
#define SAFETY_HYUNDAI 7U
#define SAFETY_TESLA 8U
#define SAFETY_ELM327 3U
#define SAFETY_GM 4U
#define SAFETY_HONDA_BOSCH 5U
#define SAFETY_FORD 6U
#define SAFETY_CADILLAC 7U
#define SAFETY_HYUNDAI 8U
#define SAFETY_CHRYSLER 9U
#define SAFETY_SUBARU 10U
#define SAFETY_GM_PASSIVE 11U
#define SAFETY_MAZDA 12U
#define SAFETY_GM_ASCM 0x1334U
#define SAFETY_TOYOTA_IPAS 0x1335U
#define SAFETY_ALLOUTPUT 0x1337U
#define SAFETY_ELM327 0xE327U
#define SAFETY_TESLA 10U
#define SAFETY_SUBARU 11U
#define SAFETY_GM_PASSIVE 12U
#define SAFETY_MAZDA 13U
#define SAFETY_TOYOTA_IPAS 16U
#define SAFETY_ALLOUTPUT 17U
#define SAFETY_GM_ASCM 18U

const safety_hook_config safety_hook_registry[] = {
{SAFETY_NOOUTPUT, &nooutput_hooks},
{SAFETY_HONDA, &honda_hooks},
{SAFETY_HONDA_BOSCH, &honda_bosch_hooks},
{SAFETY_TOYOTA, &toyota_hooks},
{SAFETY_ELM327, &elm327_hooks},
{SAFETY_GM, &gm_hooks},
{SAFETY_HONDA_BOSCH, &honda_bosch_hooks},
{SAFETY_FORD, &ford_hooks},
{SAFETY_CADILLAC, &cadillac_hooks},
{SAFETY_HYUNDAI, &hyundai_hooks},
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_TESLA, &tesla_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_GM_PASSIVE, &gm_passive_hooks},
{SAFETY_MAZDA, &mazda_hooks},
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
{SAFETY_GM_PASSIVE, &gm_passive_hooks},
{SAFETY_GM_ASCM, &gm_ascm_hooks},
{SAFETY_TESLA, &tesla_hooks},
{SAFETY_ALLOUTPUT, &alloutput_hooks},
{SAFETY_ELM327, &elm327_hooks},
{SAFETY_GM_ASCM, &gm_ascm_hooks},
};

int safety_set_mode(uint16_t mode, int16_t param) {
Expand Down
4 changes: 3 additions & 1 deletion boardesp/elm327.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ typedef struct __attribute__((packed)) {
#define PANDA_USB_CAN_WRITE_BUS_NUM 3
#define PANDA_USB_LIN_WRITE_BUS_NUM 2

#define SAFETY_ELM327 3U

typedef struct _elm_tcp_conn {
struct espconn *conn;
struct _elm_tcp_conn *next;
Expand Down Expand Up @@ -1420,7 +1422,7 @@ static void ICACHE_FLASH_ATTR elm_process_at_cmd(char *cmd, uint16_t len) {

elm_append_rsp_const("\r\r");
elm_append_rsp_const(IDENT_MSG);
panda_set_safety_mode(0xE327);
panda_set_safety_mode(SAFETY_ELM327);

elm_proto_reinit(elm_current_proto());
return;
Expand Down
5 changes: 4 additions & 1 deletion drivers/linux/panda.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

#define PANDA_DLC_MASK 0x0F

#define SAFETY_ALLOUTPUT 17
#define SAFETY_NOOUTPUT 0

struct panda_usb_ctx {
struct panda_inf_priv *priv;
u32 ndx;
Expand Down Expand Up @@ -156,7 +159,7 @@ static int panda_set_output_enable(struct panda_inf_priv* priv, bool enable){
return usb_control_msg(priv->priv_dev->udev,
usb_sndctrlpipe(priv->priv_dev->udev, 0),
0xDC, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
enable ? 0x1337 : 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
enable ? SAFETY_ALLOUTPUT : SAFETY_NOOUTPUT, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
}

static void panda_usb_write_bulk_callback(struct urb *urb)
Expand Down
2 changes: 1 addition & 1 deletion drivers/windows/panda_shared/panda.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace panda {
typedef enum _PANDA_SAFETY_MODE : uint16_t {
SAFETY_NOOUTPUT = 0,
SAFETY_HONDA = 1,
SAFETY_ALLOUTPUT = 0x1337,
SAFETY_ALLOUTPUT = 17,
} PANDA_SAFETY_MODE;

typedef enum _PANDA_SERIAL_PORT : uint8_t {
Expand Down
26 changes: 15 additions & 11 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
DEBUG = os.getenv("PANDADEBUG") is not None

# *** wifi mode ***

def build_st(target, mkfile="Makefile"):
from panda import BASEDIR
cmd = 'cd %s && make -f %s clean && make -f %s %s >/dev/null' % (os.path.join(BASEDIR, "board"), mkfile, mkfile, target)
Expand Down Expand Up @@ -109,20 +108,25 @@ def close(self):
# *** normal mode ***

class Panda(object):

# matches cereal.car.CarParams.SafetyModel
SAFETY_NOOUTPUT = 0
SAFETY_HONDA = 1
SAFETY_TOYOTA = 2
SAFETY_GM = 3
SAFETY_HONDA_BOSCH = 4
SAFETY_FORD = 5
SAFETY_CADILLAC = 6
SAFETY_HYUNDAI = 7
SAFETY_TESLA = 8
SAFETY_ELM327 = 3
SAFETY_GM = 4
SAFETY_HONDA_BOSCH = 5
SAFETY_FORD = 6
SAFETY_CADILLAC = 7
SAFETY_HYUNDAI = 8
SAFETY_CHRYSLER = 9
SAFETY_TOYOTA_IPAS = 0x1335
SAFETY_TOYOTA_NOLIMITS = 0x1336
SAFETY_ALLOUTPUT = 0x1337
SAFETY_ELM327 = 0xE327
SAFETY_TESLA = 10
SAFETY_SUBARU = 11
SAFETY_GM_PASSIVE = 12
SAFETY_MAZDA = 13
SAFETY_TOYOTA_IPAS = 16
SAFETY_ALLOUTPUT = 17
SAFETY_GM_ASCM = 18

SERIAL_DEBUG = 0
SERIAL_ESP = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/can_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def sec_since_boot():

def can_printer():
p = Panda()
p.set_safety_mode(0x1337)
p.set_safety_mode(Panda.SAFETY_ALLOUTPUT)

start = sec_since_boot()
lp = sec_since_boot()
Expand Down
4 changes: 2 additions & 2 deletions tests/elm_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_elm_panda_safety_mode_KWPFast():
p_car.kline_drain()

p_elm = Panda("WIFI")
p_elm.set_safety_mode(0xE327);
p_elm.set_safety_mode(Panda.SAFETY_ELM327);

def get_checksum(dat):
result = 0
Expand Down Expand Up @@ -625,7 +625,7 @@ def test_elm_panda_safety_mode_ISO15765():
p_car.set_safety_mode(Panda.SAFETY_ALLOUTPUT)

p_elm = Panda("WIFI")
p_elm.set_safety_mode(0xE327);
p_elm.set_safety_mode(Panda.SAFETY_ELM327);

#sim = elm_car_simulator.ELMCarSimulator(serial, lin=False)
#sim.start()
Expand Down
2 changes: 1 addition & 1 deletion tests/pedal/enter_canloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def bulkRead(self, endpoint, length, timeout=0):
args = parser.parse_args()

p = Panda()
p.set_safety_mode(0x1337)
p.set_safety_mode(Panda.SAFETY_ALLOUTPUT)

while 1:
if len(p.can_recv()) == 0:
Expand Down
6 changes: 3 additions & 3 deletions tests/safety/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y clang make python python-pip git curl locales zlib1g-dev libffi-dev bzip2 libssl-dev libbz2-dev
RUN apt-get update && apt-get install -y clang make python python-pip git curl locales zlib1g-dev libffi-dev bzip2 libssl-dev libbz2-dev libusb-1.0-0

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
Expand All @@ -14,6 +14,6 @@ RUN pyenv install 3.7.3
RUN pyenv global 3.7.3
RUN pyenv rehash

COPY tests/safety/requirements.txt /panda/tests/safety/requirements.txt
RUN pip install -r /panda/tests/safety/requirements.txt
COPY tests/safety/requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . /panda
2 changes: 2 additions & 0 deletions tests/safety/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
cffi==1.11.4
numpy==1.14.5
libusb1==1.6.6
requests
3 changes: 2 additions & 1 deletion tests/safety/test_cadillac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 2
MAX_RATE_DOWN = 5
Expand Down Expand Up @@ -31,7 +32,7 @@ class TestCadillacSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(6, 0)
cls.safety.safety_set_mode(Panda.SAFETY_CADILLAC, 0)
cls.safety.init_tests_cadillac()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_chrysler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 3
MAX_RATE_DOWN = 3
Expand Down Expand Up @@ -35,7 +36,7 @@ class TestChryslerSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(9, 0)
cls.safety.safety_set_mode(Panda.SAFETY_CHRYSLER, 0)
cls.safety.init_tests_chrysler()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_gm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 7
MAX_RATE_DOWN = 17
Expand Down Expand Up @@ -32,7 +33,7 @@ class TestGmSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(3, 0)
cls.safety.safety_set_mode(Panda.SAFETY_GM, 0)
cls.safety.init_tests_gm()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_honda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_BRAKE = 255

Expand All @@ -11,7 +12,7 @@ class TestHondaSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(1, 0)
cls.safety.safety_set_mode(Panda.SAFETY_HONDA, 0)
cls.safety.init_tests_honda()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_honda_bosch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_BRAKE = 255

class TestHondaSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(4, 0)
cls.safety.safety_set_mode(Panda.SAFETY_HONDA_BOSCH, 0)
cls.safety.init_tests_honda()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_hyundai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 3
MAX_RATE_DOWN = 7
Expand Down Expand Up @@ -29,7 +30,7 @@ class TestHyundaiSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(7, 0)
cls.safety.safety_set_mode(Panda.SAFETY_HYUNDAI, 0)
cls.safety.init_tests_hyundai()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_subaru.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 50
MAX_RATE_DOWN = 70
Expand Down Expand Up @@ -29,7 +30,7 @@ class TestSubaruSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(10, 0)
cls.safety.safety_set_mode(Panda.SAFETY_SUBARU, 0)
cls.safety.init_tests_subaru()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_toyota.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

MAX_RATE_UP = 10
MAX_RATE_DOWN = 25
Expand Down Expand Up @@ -33,7 +34,7 @@ class TestToyotaSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(2, 100)
cls.safety.safety_set_mode(Panda.SAFETY_TOYOTA, 100)
cls.safety.init_tests_toyota()

def _send_msg(self, bus, addr, length):
Expand Down
3 changes: 2 additions & 1 deletion tests/safety/test_toyota_ipas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import numpy as np
import libpandasafety_py
from panda import Panda

IPAS_OVERRIDE_THRESHOLD = 200

Expand All @@ -25,7 +26,7 @@ class TestToyotaSafety(unittest.TestCase):
@classmethod
def setUp(cls):
cls.safety = libpandasafety_py.libpandasafety
cls.safety.safety_set_mode(0x1335, 66)
cls.safety.safety_set_mode(Panda.SAFETY_TOYOTA_IPAS, 66)
cls.safety.init_tests_toyota()

def _torque_driver_msg(self, torque):
Expand Down
Loading

0 comments on commit 70219d7

Please sign in to comment.