Skip to content

Commit

Permalink
improve func
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Jun 11, 2024
1 parent 6edb1f8 commit fbd4e5c
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions selfdrive/pandad/can_list_to_can_capnp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ void can_list_to_can_capnp_cpp(const std::vector<can_frame> &can_list, std::stri
capnp::writeMessage(output_stream, msg);
}

void can_capnp_to_can_list_cpp(const std::vector<std::string> &strings, std::vector<CanData> &can_data, bool sendcan) {
kj::Array<capnp::word> aligned_buf;
can_data.reserve(strings.size());

for (const auto &s : strings) {
const size_t buf_size = (s.length() / sizeof(capnp::word)) + 1;
if (aligned_buf.size() < buf_size) {
aligned_buf = kj::heapArray<capnp::word>(buf_size);
}
memcpy(aligned_buf.begin(), s.data(), s.length());
// Converts a vector of Cap'n Proto serialized can strings into a vector of CanData structures.
void can_capnp_to_can_list_cpp(const std::vector<std::string> &strings, std::vector<CanData> &can_list, bool sendcan) {
AlignedBuffer aligned_buf;
can_list.reserve(strings.size());

for (const auto &str : strings) {
// extract the messages
capnp::FlatArrayMessageReader cmsg(aligned_buf.slice(0, buf_size));
cereal::Event::Reader event = cmsg.getRoot<cereal::Event>();

auto &can = can_data.emplace_back();
can.nanos = event.getLogMonoTime();

auto cans = sendcan ? event.getSendcan() : event.getCan();
can.frames.reserve(cans.size());
for (const auto &c : cans) {
auto &frame = can.frames.emplace_back();
frame.src = c.getSrc();
frame.address = c.getAddress();
auto dat = c.getDat();
frame.dat.assign(dat.begin(), dat.end());
capnp::FlatArrayMessageReader reader(aligned_buf.align(str.data(), str.size()));
cereal::Event::Reader event = reader.getRoot<cereal::Event>();

auto frames = sendcan ? event.getSendcan() : event.getCan();

// Add new CanData entry
CanData &can_data = can_list.emplace_back();
can_data.nanos = event.getLogMonoTime();
can_data.frames.reserve(frames.size());

// Populate CAN frames
for (const auto &frame : frames) {
CanFrame &can_frame = can_data.frames.emplace_back();
can_frame.src = frame.getSrc();
can_frame.address = frame.getAddress();

// Copy CAN data
auto dat = frame.getDat();
can_frame.dat.assign(dat.begin(), dat.end());
}
}
}

0 comments on commit fbd4e5c

Please sign in to comment.