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 */
31 #include "rtdm/rtcan.h"
32 #define CAN_IFNAME "rtcan%s"
33 #define CAN_SOCKET rt_dev_socket
34 #define CAN_CLOSE rt_dev_close
35 #define CAN_RECV rt_dev_recv
36 #define CAN_SEND rt_dev_send
37 #define CAN_BIND rt_dev_bind
38 #define CAN_IOCTL rt_dev_ioctl
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include "linux/can.h"
43 #include "linux/can/raw.h"
48 #define CAN_IFNAME "can%s"
49 #define CAN_SOCKET socket
50 #define CAN_CLOSE close
54 #define CAN_IOCTL ioctl
57 #include "can_driver.h"
59 /*********functions which permit to communicate with the board****************/
60 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
63 struct can_frame frame;
65 res = CAN_RECV(*(int*)fd0, &frame, sizeof(frame), 0);
69 m->cob_id.w = frame.can_id & CAN_EFF_MASK;
70 m->len = frame.can_dlc;
71 if (frame.can_id & CAN_RTR_FLAG)
75 memcpy(m->data, frame.data, 8);
81 /***************************************************************************/
82 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
85 struct can_frame frame;
87 frame.can_id = m->cob_id.w;
88 if (frame.can_id >= 0x800)
89 frame.can_id |= CAN_EFF_FLAG;
90 frame.can_dlc = m->len;
92 frame.can_id |= CAN_RTR_FLAG;
94 memcpy(frame.data, m->data, 8);
96 res = CAN_SEND(*(int*)fd0, &frame, sizeof(frame), 0);
103 /***************************************************************************/
104 CAN_HANDLE canOpen_driver(s_BOARD *board)
107 struct sockaddr_can addr;
109 CAN_HANDLE fd0 = malloc(sizeof(int));
111 *(int*)fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, CAN_RAW);
113 fprintf(stderr,"Socket creation failed.\n");
117 snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname);
118 err = CAN_IOCTL(*(int*)fd0, SIOCGIFINDEX, &ifr);
120 fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name);
124 addr.can_family = AF_CAN;
125 addr.can_ifindex = ifr.ifr_ifindex;
126 err = CAN_BIND(*(int*)fd0, (struct sockaddr *)&addr,
129 fprintf(stderr, "Binding failed.\n");
136 CAN_CLOSE(*(int*)fd0);
143 /***************************************************************************/
144 int canClose_driver(CAN_HANDLE fd0)
147 CAN_CLOSE(*(int*)fd0);