7 #include <sys/socket.h>
11 #include <linux/can.h>
12 #include <linux/can/bcm.h>
13 #include <linux/can/error.h>
17 #include <boost/chrono.hpp>
27 :
size(sizeof(bcm_msg_head) + sizeof(can_frame)*n),
data(new uint8_t[
size])
34 return *(bcm_msg_head*)
data;
37 long long usec = boost::chrono::duration_cast<boost::chrono::microseconds>(period).count();
38 head().ival2.tv_sec = usec / 1000000;
39 head().ival2.tv_usec = usec % 1000000;
56 bool init(
const std::string &device){
57 s_ = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
59 if(
s_ < 0 )
return false;
61 std::strcpy(ifr.ifr_name, device.c_str());
62 int ret = ioctl(
s_, SIOCGIFINDEX, &ifr);
69 struct sockaddr_can addr = {0};
70 addr.can_family = AF_CAN;
71 addr.can_ifindex = ifr.ifr_ifindex;
73 ret = connect(
s_, (
struct sockaddr *)&addr,
sizeof(addr));
81 template<
typename DurationType>
bool startTX(DurationType period,
Header header,
size_t num,
Frame *frames) {
86 bcm_msg_head &head = msg.
head();
88 head.opcode = TX_SETUP;
89 head.flags |= SETTIMER | STARTTIMER;
91 for(
size_t i=0; i < num; ++i){
92 head.frames[i].can_dlc = frames[i].
dlc;
93 head.frames[i].can_id = head.can_id;
94 for(
size_t j = 0; j < head.frames[i].can_dlc; ++j){
95 head.frames[i].data[j] = frames[i].
data[j];
102 msg.
head().opcode = TX_DELETE;