2020-01-18 02:50:44 +08:00
|
|
|
# distutils: language = c++
|
|
|
|
# cython: language_level=3
|
2024-08-01 06:37:39 +08:00
|
|
|
from cython.operator cimport dereference as deref, preincrement as preinc
|
2020-01-18 02:50:44 +08:00
|
|
|
from libcpp.vector cimport vector
|
|
|
|
from libcpp.string cimport string
|
|
|
|
from libcpp cimport bool
|
2024-08-01 06:37:39 +08:00
|
|
|
from libc.stdint cimport uint8_t, uint32_t, uint64_t
|
2020-01-18 02:50:44 +08:00
|
|
|
|
2023-07-19 14:58:51 +08:00
|
|
|
cdef extern from "panda.h":
|
|
|
|
cdef struct can_frame:
|
|
|
|
long address
|
|
|
|
string dat
|
|
|
|
long src
|
2020-01-18 02:50:44 +08:00
|
|
|
|
2024-08-01 06:37:39 +08:00
|
|
|
cdef extern from "opendbc/can/common.h":
|
|
|
|
cdef struct CanFrame:
|
|
|
|
long src
|
|
|
|
uint32_t address
|
|
|
|
vector[uint8_t] dat
|
|
|
|
|
|
|
|
cdef struct CanData:
|
|
|
|
uint64_t nanos
|
|
|
|
vector[CanFrame] frames
|
|
|
|
|
2023-07-19 14:58:51 +08:00
|
|
|
cdef extern from "can_list_to_can_capnp.cc":
|
2024-08-01 06:37:39 +08:00
|
|
|
void can_list_to_can_capnp_cpp(const vector[can_frame] &can_list, string &out, bool sendcan, bool valid)
|
|
|
|
void can_capnp_to_can_list_cpp(const vector[string] &strings, vector[CanData] &can_data, bool sendcan)
|
2020-01-18 02:50:44 +08:00
|
|
|
|
|
|
|
def can_list_to_can_capnp(can_msgs, msgtype='can', valid=True):
|
2024-05-13 13:01:08 +08:00
|
|
|
cdef can_frame *f
|
2020-01-18 02:50:44 +08:00
|
|
|
cdef vector[can_frame] can_list
|
2021-04-29 04:50:21 +08:00
|
|
|
|
2024-05-13 13:01:08 +08:00
|
|
|
can_list.reserve(len(can_msgs))
|
2020-01-18 02:50:44 +08:00
|
|
|
for can_msg in can_msgs:
|
2024-05-13 13:01:08 +08:00
|
|
|
f = &(can_list.emplace_back())
|
2020-01-18 02:50:44 +08:00
|
|
|
f.address = can_msg[0]
|
2024-07-31 12:26:58 +08:00
|
|
|
f.dat = can_msg[1]
|
|
|
|
f.src = can_msg[2]
|
2024-05-13 13:01:08 +08:00
|
|
|
|
2020-01-18 02:50:44 +08:00
|
|
|
cdef string out
|
|
|
|
can_list_to_can_capnp_cpp(can_list, out, msgtype == 'sendcan', valid)
|
|
|
|
return out
|
2024-08-01 06:37:39 +08:00
|
|
|
|
|
|
|
def can_capnp_to_list(strings, msgtype='can'):
|
|
|
|
cdef vector[CanData] data
|
|
|
|
can_capnp_to_can_list_cpp(strings, data, msgtype == 'sendcan')
|
|
|
|
|
|
|
|
result = []
|
|
|
|
cdef CanData *d
|
|
|
|
cdef vector[CanData].iterator it = data.begin()
|
|
|
|
while it != data.end():
|
|
|
|
d = &deref(it)
|
2024-08-08 07:12:00 +08:00
|
|
|
frames = [(f.address, (<char *>&f.dat[0])[:f.dat.size()], f.src) for f in d.frames]
|
|
|
|
result.append((d.nanos, frames))
|
2024-08-01 06:37:39 +08:00
|
|
|
preinc(it)
|
|
|
|
return result
|