From f4b182dae4ccc616bc37170490e875012e8d1b5a Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 22 Jun 2022 14:17:22 -0700 Subject: [PATCH] Support arbitrary DBC paths --- can/common_dbc.h | 2 +- can/dbc.cc | 18 ++++++++++++------ test.dbc => can/tests/test.dbc | 0 can/tests/test_packer_parser.py | 11 ++++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) rename test.dbc => can/tests/test.dbc (100%) diff --git a/can/common_dbc.h b/can/common_dbc.h index 9bd47c2..59556e6 100644 --- a/can/common_dbc.h +++ b/can/common_dbc.h @@ -73,6 +73,6 @@ struct DBC { std::vector vals; }; -DBC* dbc_parse(const std::string& dbc_name, const std::string& dbc_file_path); +DBC* dbc_parse(const std::string& dbc_path); const DBC* dbc_lookup(const std::string& dbc_name); std::vector get_dbc_names(); diff --git a/can/dbc.cc b/can/dbc.cc index e22b2ec..e0ad64b 100644 --- a/can/dbc.cc +++ b/can/dbc.cc @@ -99,10 +99,12 @@ void set_signal_type(Signal& s, ChecksumState* chk, const std::string& dbc_name, } } -DBC* dbc_parse(const std::string& dbc_name, const std::string& dbc_file_path) { - std::ifstream infile(dbc_file_path + "/" + dbc_name + ".dbc"); +DBC* dbc_parse(const std::string& dbc_path) { + std::ifstream infile(dbc_path); if (!infile) return nullptr; + const std::string dbc_name = std::filesystem::path(dbc_path).filename(); + std::unique_ptr checksum(get_checksum(dbc_name)); uint32_t address = 0; @@ -203,7 +205,7 @@ DBC* dbc_parse(const std::string& dbc_name, const std::string& dbc_file_path) { return dbc; } -const std::string get_dbc_file_path() { +const std::string get_dbc_root_path() { char *basedir = std::getenv("BASEDIR"); if (basedir != NULL) { return std::string(basedir) + "/opendbc"; @@ -215,18 +217,22 @@ const std::string get_dbc_file_path() { const DBC* dbc_lookup(const std::string& dbc_name) { static std::mutex lock; static std::map dbcs; - static const std::string& dbc_file_path = get_dbc_file_path(); + + std::string dbc_file_path = dbc_name; + if (!std::filesystem::exists(dbc_file_path)) { + dbc_file_path = get_dbc_root_path() + "/" + dbc_name + ".dbc"; + } std::unique_lock lk(lock); auto it = dbcs.find(dbc_name); if (it == dbcs.end()) { - it = dbcs.insert(it, {dbc_name, dbc_parse(dbc_name, dbc_file_path)}); + it = dbcs.insert(it, {dbc_name, dbc_parse(dbc_file_path)}); } return it->second; } std::vector get_dbc_names() { - static const std::string& dbc_file_path = get_dbc_file_path(); + static const std::string& dbc_file_path = get_dbc_root_path(); std::vector dbcs; for (std::filesystem::directory_iterator i(dbc_file_path), end; i != end; i++) { if (!is_directory(i->path())) { diff --git a/test.dbc b/can/tests/test.dbc similarity index 100% rename from test.dbc rename to can/tests/test.dbc diff --git a/can/tests/test_packer_parser.py b/can/tests/test_packer_parser.py index 92b398c..496f043 100755 --- a/can/tests/test_packer_parser.py +++ b/can/tests/test_packer_parser.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import os import unittest import random @@ -6,6 +7,10 @@ import cereal.messaging as messaging from opendbc.can.parser import CANParser from opendbc.can.packer import CANPacker + +TEST_DBC = os.path.abspath(os.path.join(os.path.dirname(__file__), "test.dbc")) + + # Python implementation so we don't have to depend on boardd def can_list_to_can_capnp(can_msgs, msgtype='can', logMonoTime=None): dat = messaging.new_message() @@ -30,7 +35,7 @@ def can_list_to_can_capnp(can_msgs, msgtype='can', logMonoTime=None): class TestCanParserPacker(unittest.TestCase): def test_packer(self): - packer = CANPacker("test") + packer = CANPacker(TEST_DBC) for b in range(6): for i in range(256): @@ -55,8 +60,8 @@ class TestCanParserPacker(unittest.TestCase): ] checks = [("STEERING_CONTROL", 0), ("CAN_FD_MESSAGE", 0)] - packer = CANPacker("test") - parser = CANParser("test", signals, checks, 0) + packer = CANPacker(TEST_DBC) + parser = CANParser(TEST_DBC, signals, checks, 0) idx = 0