XCP_SIM
linux_socket_can.c
1 /*
2  * BlueParrot XCP
3  *
4  * (C) 2007-2021 by Christoph Schueler <github.com/Christoph2,
5  * cpu12.gems@googlemail.com>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * s. FLOSS-EXCEPTION.txt
24  */
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <linux/can.h>
29 #include <linux/can/raw.h>
30 #include <memory.h>
31 #include <net/if.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <unistd.h>
39 
41 #include "xcp.h"
42 #include "xcp_hw.h"
45 #define err_abort(code, text) \
46  do { \
47  fprintf(stderr, "%s at \"%s\":%d: %s\n\r", text, __FILE__, __LINE__, strerror(code)); \
48  abort(); \
49  } while (0)
50 
51 #define errno_abort(text) \
52  do { \
53  fprintf(stderr, "%s at \"%s\":%d: %s\n\r", text, __FILE__, __LINE__, strerror(errno)); \
54  abort(); \
55  } while (0)
56 
57 typedef struct tagXcpTl_ConnectionType {
58  int can_socket;
59  bool connected;
61 
62 unsigned char buf[XCP_COMM_BUFLEN];
63 
64 static XcpTl_ConnectionType XcpTl_Connection;
65 
66 int locate_interface(int socket, char const* name) {
67  struct ifreq ifr;
68 
69  strcpy(ifr.ifr_name, name);
70  if (ioctl(socket, SIOCGIFINDEX, &ifr) == -1) {
71  errno_abort("locate_interface::ioctl()");
72  }
73 
74  return ifr.ifr_ifindex;
75 }
76 
77 void XcpTl_Init(void) {
78  int enable_sockopt = 1;
79  int flags;
80  struct sockaddr_can addr;
81  struct can_filter rfilter[2];
82 
83  memset(&XcpTl_Connection, '\x00', sizeof(XcpTl_ConnectionType));
84 
85  XcpTl_Connection.can_socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
86  if (XcpTl_Connection.can_socket == -1) {
87  errno_abort("XcpTl_Init::socket()");
88  }
89  if (Xcp_Options.fd) {
90  if (setsockopt(XcpTl_Connection.can_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_sockopt, sizeof(enable_sockopt)) ==
91  -1) {
92  errno_abort("Your kernel doesn't supports CAN-FD.\n\r");
93  }
94  }
95 #if 0
96  flags = fcntl(XcpTl_Connection.can_socket, F_GETFL, 0);
97  if (flags == -1) {
98  errno_abort("fcntl(F_GETFL)");
99  }
100  flags |= O_NONBLOCK;
101  if (fcntl(XcpTl_Connection.can_socket, F_SETFL, flags) == -1) {
102  errno_abort("fcntl(F_SETFL)");
103  }
104 #endif
105  if (setsockopt(XcpTl_Connection.can_socket, SOL_SOCKET, SO_TIMESTAMP, &enable_sockopt, sizeof(enable_sockopt)) < 0) {
106  // Enable precision timestamps.
107  errno_abort("setsockopt(SO_TIMESTAMP)");
108  }
109 
110  /* Select that CAN interface, and bind the socket to it. */
111  addr.can_family = AF_CAN;
112  addr.can_ifindex = locate_interface(XcpTl_Connection.can_socket, Xcp_Options.interface);
113  bind(XcpTl_Connection.can_socket, (struct sockaddr*)&addr, sizeof(addr));
114  if (XcpTl_Connection.can_socket == -1) {
115  errno_abort("XcpTl_Init::bind()");
116  }
117 
118  rfilter[0].can_id = XCP_ON_CAN_INBOUND_IDENTIFIER;
119  rfilter[0].can_mask = XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_INBOUND_IDENTIFIER) ? CAN_EFF_FLAG : CAN_SFF_MASK;
120  rfilter[1].can_id = XCP_ON_CAN_BROADCAST_IDENTIFIER;
121  rfilter[1].can_mask = XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(XCP_ON_CAN_BROADCAST_IDENTIFIER) ? CAN_EFF_FLAG : CAN_SFF_MASK;
122 
123  setsockopt(XcpTl_Connection.can_socket, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
124 }
125 
126 void XcpTl_DeInit(void) { close(XcpTl_Connection.can_socket); }
127 
128 void* XcpTl_Thread(void* param) {
129  XCP_FOREVER { XcpTl_MainFunction(); }
130  return NULL;
131 }
132 
133 void XcpTl_MainFunction(void) {
134  if (XcpTl_FrameAvailable(0, 1000) > 0) {
135  XcpTl_RxHandler();
136  }
137 }
138 
139 void XcpTl_RxHandler(void) {
140  struct canfd_frame frame;
141  int nbytes = 0;
142 
143  nbytes = read(XcpTl_Connection.can_socket, &frame, CANFD_MTU);
144 
145  if (nbytes == CANFD_MTU) {
146  // printf("got CAN FD frame with length %d\n\r", frame.len);
147  /* FD frame.flags could be used here */
148  } else if (nbytes == CAN_MTU) {
149  // printf("got legacy CAN frame with length %d\n", frame.len);
150  } else {
151  errno_abort("can raw socket read");
152  }
153  //printf("#%d bytes received from '%04x' DLC: %d .\n\r", nbytes, frame.can_id, frame.len);
154  if (frame.len > 0) {
155  Xcp_CtoIn.len = frame.len;
156  //Xcp_CtoIn.data = (__u8*)&frame.data + XCP_TRANSPORT_LAYER_BUFFER_OFFSET;
157  //XcpUtl_MemCopy(Xcp_CtoIn.data, (__u8*)&frame.data + XCP_TRANSPORT_LAYER_BUFFER_OFFSET, nbytes - XCP_TRANSPORT_LAYER_BUFFER_OFFSET);
158  XcpUtl_MemCopy(Xcp_CtoIn.data, &frame.data, frame.len);
159  //XcpUtl_Hexdump(Xcp_CtoIn.data, frame.len);
160  Xcp_DispatchCommand(&Xcp_CtoIn);
161  }
162 }
163 
164 
165 void XcpTl_TxHandler(void) {}
166 
167 int16_t XcpTl_FrameAvailable(uint32_t sec, uint32_t usec) {
168 #if 0
169  struct timeval timeout;
170  fd_set fds;
171  int16_t res;
172 
173  timeout.tv_sec = sec;
174  timeout.tv_usec = usec;
175 
176  FD_ZERO(&fds);
177  FD_SET(XcpTl_Connection.can_socket, &fds);
178 
179  // Return value:
180  // -1: error occurred
181  // 0: timed out
182  // > 0: data ready to be read
183  res = select(0, &fds, 0, 0, &timeout);
184  if (res != 0) {
185  printf("sel: %d\r\n", res);
186  }
187  if (res == -1) {
188  XcpHw_ErrorMsg("XcpTl_FrameAvailable:select()", errno);
189  exit(2);
190  }
191  return res;
192 #endif
193  return 1;
194 }
195 
196 void XcpTl_Send(uint8_t const* buf, uint16_t len) {
197  struct can_frame frame = {0};
198  struct canfd_frame frame_fd = {0};
199 
200  XCP_TL_ENTER_CRITICAL();
201  if (Xcp_Options.fd) {
202  frame_fd.len = len;
203  frame_fd.can_id = XCP_ON_CAN_OUTBOUND_IDENTIFIER;
204  memcpy(frame_fd.data, buf, len);
205  (void)write(XcpTl_Connection.can_socket, &frame_fd, sizeof(struct canfd_frame));
206  } else {
207  frame.can_dlc = len;
208  frame.can_id = XCP_ON_CAN_OUTBOUND_IDENTIFIER;
209  memcpy(frame.data, buf, len);
210  (void)write(XcpTl_Connection.can_socket, &frame, sizeof(struct can_frame));
211  }
212  XCP_TL_LEAVE_CRITICAL();
213 }
214 
215 void XcpTl_SaveConnection(void) { XcpTl_Connection.connected = XCP_TRUE; }
216 
217 void XcpTl_ReleaseConnection(void) { XcpTl_Connection.connected = XCP_FALSE; }
218 
219 bool XcpTl_VerifyConnection(void) { return XCP_TRUE; }
220 
221 void XcpTl_PrintConnectionInformation(void) {
222  printf("\n\rXCPonCan\n\r");
223 #if 0
224  printf("\nXCPonCan -- Listening on port %s / %s [%s]\n\r",
225  DEFAULT_PORT,
226  Xcp_Options.tcp ? "TCP" : "UDP",
227  Xcp_Options.ipv6 ? "IPv6" : "IPv4"
228  );
229 #endif
230 }