-
Notifications
You must be signed in to change notification settings - Fork 2
/
cansocket.cpp
157 lines (114 loc) · 3.09 KB
/
cansocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "cansocket.h"
#include <net/if.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <cstring>
namespace
{
class Scope_guard
{
public:
Scope_guard(int fd) : fd_{fd} {}
~Scope_guard() { if (fd_ != -1) ::close(fd_); fd_ = -1; }
Scope_guard(const Scope_guard&) = delete;
Scope_guard& operator=(const Scope_guard&) = delete;
void release() { fd_ = -1; }
private:
int fd_;
};
} // namespace
void can::Socket::open(const std::string& device)
{
if (fd_ != -1)
throw Socket_error{"Already open"};
fd_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (fd_ == -1)
throw Socket_error{"Could not open"};
Scope_guard guard{fd_};
if (device.size() + 1 >= IFNAMSIZ)
throw Socket_error{"Device name too long"};
addr_.can_family = AF_CAN;
ifreq ifr;
std::memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
std::strcpy(ifr.ifr_name, device.c_str());
if (ioctl(fd_, SIOCGIFINDEX, &ifr) < 0)
throw Socket_error{"Error retrieving interface index"};
addr_.can_ifindex = ifr.ifr_ifindex;
guard.release();
}
void can::Socket::close()
{
if (fd_ != -1) {
::close(fd_);
}
reset();
}
void can::Socket::bind()
{
if (::bind(fd_, reinterpret_cast<sockaddr*>(&addr_), sizeof(addr_)) < 0)
throw Socket_error{"Error while binding socket"};
msg_.msg_name = &addr_;
msg_.msg_iov = &iov_;
msg_.msg_iovlen = 1;
msg_.msg_control = cmsg_buffer.data();
}
void can::Socket::set_receive_timeout(time_t timeout)
{
if (timeout <= 0)
throw Socket_error{"Timeout must be larger then 0"};
timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0)
throw Socket_error{"Error setting receive timeout"};
}
void can::Socket::set_socket_timestamp(bool enable)
{
const int param = enable ? 1 : 0;
if (setsockopt(fd_, SOL_SOCKET, SO_TIMESTAMP, ¶m, sizeof(param)) != 0)
throw Socket_error{"Error setting socket timestamp"};
}
int can::Socket::transmit(const can_frame* frame)
{
return write(fd_, frame, sizeof(can_frame));
}
int can::Socket::receive(can_frame* frame)
{
iov_.iov_base = frame;
iov_.iov_len = sizeof(can_frame);
msg_.msg_namelen = sizeof(addr_);
msg_.msg_controllen = 0;
msg_.msg_flags = 0;
return recvmsg(fd_, &msg_, 0);
}
int can::Socket::receive(can_frame* frame, std::uint64_t* time)
{
iov_.iov_base = frame;
iov_.iov_len = sizeof(can_frame);
msg_.msg_namelen = sizeof(addr_);
msg_.msg_controllen = cmsg_buffer.size();
msg_.msg_flags = 0;
auto len = recvmsg(fd_, &msg_, 0);
// Get receive time from ancillary data
for (auto* cmsg = CMSG_FIRSTHDR(&msg_);
cmsg && cmsg->cmsg_level == SOL_SOCKET;
cmsg = CMSG_NXTHDR(&msg_, cmsg)) {
if (cmsg->cmsg_type == SO_TIMESTAMP) {
timeval tv;
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
*time = (tv.tv_sec * 1'000'000ull + tv.tv_usec) / 1000ull; // Time in ms
}
}
return len;
}
void can::Socket::reset()
{
fd_ = -1;
addr_ = sockaddr_can{};
iov_ = iovec{};
msg_ = msghdr{};
}