Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build message lookup table in DBC #942

Merged
merged 10 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions can/packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ CANPacker::CANPacker(const std::string& dbc_name) {
}

std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalPackValue> &signals) {
std::vector<uint8_t> ret(dbc->addr_to_msg.at(address)->size, 0);
auto msg_it = dbc->addr_to_msg.find(address);
if (msg_it == dbc->addr_to_msg.end()) {
LOGE("invalid address %d\n", address);
return {};
}

std::vector<uint8_t> ret(msg_it->second->size, 0);

// set all values for all given signal/value pairs
bool counter_set = false;
for (const auto& sigval : signals) {
auto sig_it = signal_lookup.find(std::make_pair(address, sigval.name));
if (sig_it == signal_lookup.end()) {
// TODO: do something more here. invalid flag like CANParser?
WARN("undefined signal %s - %d\n", sigval.name.c_str(), address);
LOGE("undefined signal %s - %d\n", sigval.name.c_str(), address);
continue;
}
const auto &sig = sig_it->second;
Expand Down
22 changes: 14 additions & 8 deletions can/packer_pyx.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# distutils: language = c++
# cython: c_string_encoding=ascii, language_level=3

from libc.stdint cimport uint8_t
from libc.stdint cimport uint8_t, uint32_t
from libcpp.vector cimport vector

from .common cimport CANPacker as cpp_CANPacker
Expand Down Expand Up @@ -37,11 +37,17 @@ cdef class CANPacker:
return self.packer.pack(addr, values_thing)

cpdef make_can_msg(self, name_or_addr, bus, values):
cdef uint32_t address = 0
cdef const Msg* m
if isinstance(name_or_addr, int):
m = self.dbc.addr_to_msg.at(name_or_addr)
else:
m = self.dbc.name_to_msg.at(name_or_addr.encode("utf8"))

cdef vector[uint8_t] val = self.pack(m.address, values)
return [m.address, 0, (<char *>&val[0])[:val.size()], bus]
try:
if isinstance(name_or_addr, int):
m = self.dbc.addr_to_msg.at(name_or_addr)
deanlee marked this conversation as resolved.
Show resolved Hide resolved
else:
m = self.dbc.name_to_msg.at(name_or_addr.encode("utf8"))
address = m.address
except IndexError:
# The C++ pack function will log an error message for invalid addresses
pass

cdef vector[uint8_t] val = self.pack(address, values)
return [address, 0, (<char *>&val[0])[:val.size()], bus]