Blame view

src/linux/can_socket.cpp 1.13 KB
ea9eaf95   Peter M. Groen   Fixed Headers
1
2
  #include "socket-cpp/can_socket.h"
  #include "socket-cpp/socket.h"
48b4c725   Peter M. Groen   Setting up Socket-pp
3
4
5
6
  #include <sys/ioctl.h>
  
  using namespace std;
  using namespace std::chrono;
ea9eaf95   Peter M. Groen   Fixed Headers
7
  using namespace osdev::components::socket-cpp;
48b4c725   Peter M. Groen   Setting up Socket-pp
8
9
10
  
  can_socket::can_socket(const can_address& addr)
  {
ea9eaf95   Peter M. Groen   Fixed Headers
11
  	socket_t socketHandle = create_handle( SOCK_RAW, CAN_RAW );
48b4c725   Peter M. Groen   Setting up Socket-pp
12
  
ea9eaf95   Peter M. Groen   Fixed Headers
13
14
15
16
  	if ( check_socket_bool( socketHandle ) ) 
      {
  		reset( socketHandle );
  		bind( addr );
48b4c725   Peter M. Groen   Setting up Socket-pp
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
  	}
  }
  
  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));
  }