author | etisserant |
Sat, 12 May 2007 22:42:51 +0200 | |
changeset 191 | 1e6e3d261b8f |
parent 189 | 1c1d1893f1c9 |
child 192 | 9dd6f17ef7e5 |
permissions | -rw-r--r-- |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
1 |
/* |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
3 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
5 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
6 |
See COPYING file for copyrights details. |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
7 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
8 |
This library is free software; you can redistribute it and/or |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
9 |
modify it under the terms of the GNU Lesser General Public |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
10 |
License as published by the Free Software Foundation; either |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
11 |
version 2.1 of the License, or (at your option) any later version. |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
12 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
13 |
This library is distributed in the hope that it will be useful, |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
16 |
Lesser General Public License for more details. |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
17 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
18 |
You should have received a copy of the GNU Lesser General Public |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
19 |
License along with this library; if not, write to the Free Software |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
21 |
*/ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
22 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
23 |
#include <stdio.h> |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
24 |
#include <string.h> |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
25 |
#include <stdlib.h> |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
26 |
#include <stddef.h> /* for NULL */ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
27 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
28 |
#include "config.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
29 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
30 |
#ifdef RTCAN_SOCKET |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
31 |
#include "rtdm/rtcan.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
32 |
#define CAN_IFNAME "rtcan%s" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
33 |
#define CAN_SOCKET rt_dev_socket |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
34 |
#define CAN_CLOSE rt_dev_close |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
35 |
#define CAN_RECV rt_dev_recv |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
36 |
#define CAN_SEND rt_dev_send |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
37 |
#define CAN_BIND rt_dev_bind |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
38 |
#define CAN_IOCTL rt_dev_ioctl |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
39 |
#else |
187 | 40 |
#include <sys/socket.h> |
41 |
#include <sys/ioctl.h> |
|
42 |
#include "linux/can.h" |
|
43 |
#include "linux/can/raw.h" |
|
44 |
#include "net/if.h" |
|
45 |
#define PF_CAN 29 |
|
46 |
#define AF_CAN PF_CAN |
|
47 |
//#include "af_can.h" |
|
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
48 |
#define CAN_IFNAME "can%s" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
49 |
#define CAN_SOCKET socket |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
50 |
#define CAN_CLOSE close |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
51 |
#define CAN_RECV recv |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
52 |
#define CAN_SEND send |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
53 |
#define CAN_BIND bind |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
54 |
#define CAN_IOCTL ioctl |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
55 |
#endif |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
56 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
57 |
#include "can_driver.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
58 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
59 |
/*********functions which permit to communicate with the board****************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
47
diff
changeset
|
60 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
61 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
62 |
int res; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
63 |
struct can_frame frame; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
64 |
|
187 | 65 |
res = CAN_RECV(*(int*)fd0, &frame, sizeof(frame), 0); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
66 |
if (res < 0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
67 |
return 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
68 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
69 |
m->cob_id.w = frame.can_id & CAN_EFF_MASK; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
70 |
m->len = frame.can_dlc; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
71 |
if (frame.can_id & CAN_RTR_FLAG) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
72 |
m->rtr = 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
73 |
else |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
74 |
m->rtr = 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
75 |
memcpy(m->data, frame.data, 8); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
76 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
77 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
78 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
79 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
80 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
81 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
47
diff
changeset
|
82 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
83 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
84 |
int res; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
85 |
struct can_frame frame; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
86 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
87 |
frame.can_id = m->cob_id.w; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
88 |
if (frame.can_id >= 0x800) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
89 |
frame.can_id |= CAN_EFF_FLAG; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
90 |
frame.can_dlc = m->len; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
91 |
if (m->rtr) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
92 |
frame.can_id |= CAN_RTR_FLAG; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
93 |
else |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
94 |
memcpy(frame.data, m->data, 8); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
95 |
|
187 | 96 |
res = CAN_SEND(*(int*)fd0, &frame, sizeof(frame), 0); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
97 |
if (res < 0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
98 |
return 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
99 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
100 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
101 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
102 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
103 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
47
diff
changeset
|
104 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
105 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
106 |
struct ifreq ifr; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
107 |
struct sockaddr_can addr; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
108 |
int err; |
187 | 109 |
CAN_HANDLE fd0 = malloc(sizeof(int)); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
110 |
|
189 | 111 |
*(int*)fd0 = CAN_SOCKET(PF_CAN, SOCK_RAW, CAN_RAW); |
187 | 112 |
if(*(int*)fd0 < 0){ |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
113 |
fprintf(stderr,"Socket creation failed.\n"); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
114 |
goto error_ret; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
115 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
116 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
117 |
snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname); |
187 | 118 |
err = CAN_IOCTL(*(int*)fd0, SIOCGIFINDEX, &ifr); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
119 |
if (err) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
120 |
fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
121 |
goto error_close; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
122 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
123 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
124 |
addr.can_family = AF_CAN; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
125 |
addr.can_ifindex = ifr.ifr_ifindex; |
187 | 126 |
err = CAN_BIND(*(int*)fd0, (struct sockaddr *)&addr, |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
127 |
sizeof(addr)); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
128 |
if (err) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
129 |
fprintf(stderr, "Binding failed.\n"); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
130 |
goto error_close; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
131 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
132 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
133 |
return fd0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
134 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
135 |
error_close: |
189 | 136 |
CAN_CLOSE(*(int*)fd0); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
137 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
138 |
error_ret: |
187 | 139 |
free(fd0); |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
140 |
return NULL; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
141 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
142 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
143 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
47
diff
changeset
|
144 |
int canClose_driver(CAN_HANDLE fd0) |
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
145 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
146 |
if (fd0) { |
187 | 147 |
CAN_CLOSE(*(int*)fd0); |
148 |
free(fd0); |
|
47
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
149 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
150 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
151 |
} |