Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
CAN_socket_driver.cpp
Go to the documentation of this file.
1/***********************************************************************/
25#ifndef _WIN32
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
32#include <net/if.h>
33#include <sys/ioctl.h>
34#include <sys/socket.h>
35
36#include <linux/can.h>
37#include <linux/can/raw.h>
38#include <CAN_socket_driver.h>
39
41
43{
44 struct sockaddr_can addr;
45 struct ifreq ifr;
46
47 if ((CAN_socket = socket (PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
48 {
49 perror ("CAN Socket");
50 return 1;
51 }
52
53 strcpy (ifr.ifr_name, "can0");
55
56 memset (&addr, 0, sizeof(addr));
57 addr.can_family = AF_CAN;
58 addr.can_ifindex = ifr.ifr_ifindex;
59
60 int result = bind (CAN_socket, (struct sockaddr*) &addr, sizeof(addr));
61 if( result < 0)
62 {
63 perror ("CAN Bind");
64 return 1;
65 }
66 return 0;
67}
68
70{
71 return CAN_socket != 0;
72}
73
75{
76 if( CAN_socket == 0)
77 return false;
78 return (close (CAN_socket) < 0);
79}
80
82{
83 if( CAN_socket == 0)
84 return 0;
85
86 struct can_frame frame;
87 frame.can_id = p.id;
88 frame.can_dlc = p.dlc;
89 *(uint64_t *)(frame.data)=p.data_l;
90
91 int retv = write (CAN_socket, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame);
92 if( retv < 0)
93 {
95 CAN_socket = 0;
96 }
97 return retv;
98}
99
100#endif // _WIN32
int CAN_socket_initialize(void)
bool CAN_socket_close(void)
int CAN_socket_send(const CANpacket &p)
int CAN_socket
bool CAN_socket_is_open(void)
I/O over the generic Linux CANsocket interface.
basic CAN packet type
uint16_t dlc
data length code
uint16_t id
identifier
uint64_t data_l
data seen as 64-bit integer
mathematical vector of arbitrary type and size
Definition vector.h:40
vector(void)
Definition vector.h:45