Support arbitrary DBC paths

This commit is contained in:
Adeeb Shihadeh 2022-06-22 14:17:22 -07:00
parent e7cd3ebc89
commit f4b182dae4
4 changed files with 21 additions and 10 deletions

View File

@ -73,6 +73,6 @@ struct DBC {
std::vector<Val> 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<std::string> get_dbc_names();

View File

@ -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<ChecksumState> 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<std::string, DBC*> 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<std::string> 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<std::string> dbcs;
for (std::filesystem::directory_iterator i(dbc_file_path), end; i != end; i++) {
if (!is_directory(i->path())) {

View File

@ -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