can_socket.cpp
1.13 KB
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
#include "socket-cpp/can_socket.h"
#include "socket-cpp/socket.h"
#include <sys/ioctl.h>
using namespace std;
using namespace std::chrono;
using namespace osdev::components::socket-cpp;
can_socket::can_socket(const can_address& addr)
{
socket_t socketHandle = create_handle( SOCK_RAW, CAN_RAW );
if ( check_socket_bool( socketHandle ) )
{
reset( socketHandle );
bind( addr );
}
}
system_clock::time_point can_socket::last_frame_time()
{
timeval tv {};
// TODO: Handle error
::ioctl(handle(), SIOCGSTAMP, &tv);
return to_timepoint(tv);
}
double can_socket::last_frame_timestamp()
{
timeval tv {};
// TODO: Handle error
::ioctl(handle(), SIOCGSTAMP, &tv);
return double(tv.tv_sec) + 1.0e-6 * tv.tv_usec;
}
// --------------------------------------------------------------------------
ssize_t can_socket::recv_from(can_frame *frame, int flags,
can_address* srcAddr /*=nullptr*/)
{
sockaddr* p = srcAddr ? srcAddr->sockaddr_ptr() : nullptr;
socklen_t len = srcAddr ? srcAddr->size() : 0;
// TODO: Check returned length
return check_ret(::recvfrom(handle(), frame, sizeof(can_frame),
flags, p, &len));
}