7 #include <sys/socket.h> 11 #include <linux/can.h> 12 #include <linux/can/bcm.h> 13 #include <linux/can/error.h> 23 : size(sizeof(bcm_msg_head) + sizeof(can_frame)*n), data(new uint8_t[size])
26 memset(data, 0, size);
30 return *(bcm_msg_head*)data;
33 long long usec = boost::chrono::duration_cast<boost::chrono::microseconds>(period).count();
34 head().ival2.tv_sec = usec / 1000000;
35 head().ival2.tv_usec = usec % 1000000;
41 return ::write(s, data, size) > 0;
52 bool init(
const std::string &device){
53 s_ = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
55 if(s_ < 0 )
return false;
57 strcpy(ifr.ifr_name, device.c_str());
58 int ret = ioctl(s_, SIOCGIFINDEX, &ifr);
65 struct sockaddr_can addr = {0};
66 addr.can_family = AF_CAN;
67 addr.can_ifindex = ifr.ifr_ifindex;
69 ret = connect(s_, (
struct sockaddr *)&addr,
sizeof(addr));
77 template<
typename DurationType>
bool startTX(DurationType period,
Header header,
size_t num,
Frame *frames) {
84 head.opcode = TX_SETUP;
85 head.flags |= SETTIMER | STARTTIMER;
87 for(
size_t i=0; i < num; ++i){
88 head.frames[i].can_dlc = frames[i].
dlc;
89 head.frames[i].can_id = head.can_id;
90 for(
size_t j = 0; j < head.frames[i].can_dlc; ++j){
91 head.frames[i].data[j] = frames[i].
data[j];
98 msg.
head().opcode = TX_DELETE;
100 return msg.
write(s_);
bool stopTX(Header header)
void setHeader(Header header)
boost::array< value_type, 8 > data
array for 8 data bytes with bounds checking
bool init(const std::string &device)
bool startTX(DurationType period, Header header, size_t num, Frame *frames)
unsigned char dlc
len of data