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])
30 std::memset(data, 0, 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;
45 return ::write(s, data, size) > 0;
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) {
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;
104 return msg.
write(s_);
std::array< value_type, 8 > data
array for 8 data bytes with bounds checking
bool stopTX(Header header)
void setHeader(Header header)
bool init(const std::string &device)
bool startTX(DurationType period, Header header, size_t num, Frame *frames)
unsigned char dlc
len of data