2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <stddef.h> /* for NULL */
32 #include "rtdm/rtcan.h"
33 #define CAN_IFNAME "rtcan%s"
34 #define CAN_SOCKET rt_dev_socket
35 #define CAN_CLOSE rt_dev_close
36 #define CAN_RECV rt_dev_recv
37 #define CAN_SEND rt_dev_send
38 #define CAN_BIND rt_dev_bind
39 #define CAN_IOCTL rt_dev_ioctl
40 #define CAN_ERRNO(err) (-err)
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include "linux/can.h"
45 #include "linux/can/raw.h"
50 #define CAN_IFNAME "can%s"
51 #define CAN_SOCKET socket
52 #define CAN_CLOSE close
56 #define CAN_IOCTL ioctl
57 #define CAN_ERRNO(err) errno
60 #include "can_driver.h"
62 /*********functions which permit to communicate with the board****************/
64 canReceive_driver (CAN_HANDLE fd0, Message * m)
67 struct can_frame frame;
69 res = CAN_RECV (*(int *) fd0, &frame, sizeof (frame), 0);
72 fprintf (stderr, "Recv failed: %s\n", strerror (CAN_ERRNO (res)));
76 m->cob_id = frame.can_id & CAN_EFF_MASK;
77 m->len = frame.can_dlc;
78 if (frame.can_id & CAN_RTR_FLAG)
82 memcpy (m->data, frame.data, 8);
84 #if defined DEBUG_MSG_CONSOLE_ON
92 /***************************************************************************/
94 canSend_driver (CAN_HANDLE fd0, Message * m)
97 struct can_frame frame;
99 frame.can_id = m->cob_id;
100 if (frame.can_id >= 0x800)
101 frame.can_id |= CAN_EFF_FLAG;
102 frame.can_dlc = m->len;
104 frame.can_id |= CAN_RTR_FLAG;
106 memcpy (frame.data, m->data, 8);
108 #if defined DEBUG_MSG_CONSOLE_ON
112 res = CAN_SEND (*(int *) fd0, &frame, sizeof (frame), 0);
115 fprintf (stderr, "Send failed: %s\n", strerror (CAN_ERRNO (res)));
122 /***************************************************************************/
125 TranslateBaudRate (const char *optarg)
131 len = strlen (optarg);
135 switch ((int) optarg[len - 1])
147 if ((sscanf (optarg, "%i", &val)) == 1)
156 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
158 printf("canChangeBaudRate not yet supported by this driver\n");
162 /***************************************************************************/
164 canOpen_driver (s_BOARD * board)
167 struct sockaddr_can addr;
169 CAN_HANDLE fd0 = malloc (sizeof (int));
171 can_baudrate_t *baudrate;
175 *(int *) fd0 = CAN_SOCKET (PF_CAN, SOCK_RAW, CAN_RAW);
176 if (*(int *) fd0 < 0)
178 fprintf (stderr, "Socket creation failed: %s\n",
179 strerror (CAN_ERRNO (*(int *) fd0)));
183 if (*board->busname >= '0' && *board->busname <= '9')
184 snprintf (ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname);
186 strncpy (ifr.ifr_name, board->busname, IFNAMSIZ);
187 err = CAN_IOCTL (*(int *) fd0, SIOCGIFINDEX, &ifr);
190 fprintf (stderr, "Getting IF index for %s failed: %s\n",
191 ifr.ifr_name, strerror (CAN_ERRNO (err)));
197 setsockopt(*(int *)fd0, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
198 &loopback, sizeof(loopback));
202 int recv_own_msgs = 0; /* 0 = disabled (default), 1 = enabled */
204 setsockopt(*(int *)fd0, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
205 &recv_own_msgs, sizeof(recv_own_msgs));
208 addr.can_family = AF_CAN;
209 addr.can_ifindex = ifr.ifr_ifindex;
210 err = CAN_BIND (*(int *) fd0, (struct sockaddr *) &addr, sizeof (addr));
213 fprintf (stderr, "Binding failed: %s\n", strerror (CAN_ERRNO (err)));
218 baudrate = (can_baudrate_t *) & ifr.ifr_ifru;
219 *baudrate = TranslateBaudRate (board->baudrate);
223 err = CAN_IOCTL (*(int *) fd0, SIOCSCANBAUDRATE, &ifr);
227 "Setting baudrate %d failed: %s\n",
228 *baudrate, strerror (CAN_ERRNO (err)));
232 mode = (can_mode_t *) & ifr.ifr_ifru;
233 *mode = CAN_MODE_START;
234 err = CAN_IOCTL (*(int *) fd0, SIOCSCANMODE, &ifr);
237 fprintf (stderr, "Starting CAN device failed: %s\n",
238 strerror (CAN_ERRNO (err)));
246 CAN_CLOSE (*(int *) fd0);
253 /***************************************************************************/
255 canClose_driver (CAN_HANDLE fd0)
259 CAN_CLOSE (*(int *) fd0);