author | lbessard |
Wed, 03 Jan 2007 10:33:55 +0100 | |
changeset 63 | 2be18e405e40 |
parent 47 | 8a1047ab51f4 |
child 145 | e747d2e26af0 |
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 |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
40 |
#include "af_can.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
41 |
#define CAN_IFNAME "can%s" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
42 |
#define CAN_SOCKET socket |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
43 |
#define CAN_CLOSE close |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
44 |
#define CAN_RECV recv |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
45 |
#define CAN_SEND send |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
46 |
#define CAN_BIND bind |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
47 |
#define CAN_IOCTL ioctl |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
48 |
#endif |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
49 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
50 |
struct CANPort; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
51 |
#define CAN_HANDLE struct CANPort * |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
52 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
53 |
#include <applicfg.h> |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
54 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
55 |
#include "timer.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
56 |
#include "can_driver.h" |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
57 |
#include "timers_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 |
typedef struct CANPort { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
60 |
int fd; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
61 |
TASK_HANDLE receiveTask; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
62 |
CO_Data* d; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
63 |
} CANPort; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
64 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
65 |
/*********functions which permit to communicate with the board****************/ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
66 |
UNS8 canReceive(CAN_HANDLE fd0, Message *m) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
67 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
68 |
int res; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
69 |
struct can_frame frame; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
70 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
71 |
res = CAN_RECV(fd0->fd, &frame, sizeof(frame), 0); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
72 |
if (res < 0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
73 |
return 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
74 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
75 |
m->cob_id.w = frame.can_id & CAN_EFF_MASK; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
76 |
m->len = frame.can_dlc; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
77 |
if (frame.can_id & CAN_RTR_FLAG) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
78 |
m->rtr = 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
79 |
else |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
80 |
m->rtr = 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
81 |
memcpy(m->data, frame.data, 8); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
82 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
83 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
84 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
85 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
86 |
void canReceiveLoop(CAN_HANDLE fd0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
87 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
88 |
CO_Data* d = fd0->d; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
89 |
Message m; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
90 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
91 |
while (1) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
92 |
if (canReceive(fd0, &m) != 0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
93 |
break; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
94 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
95 |
EnterMutex(); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
96 |
canDispatch(d, &m); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
97 |
LeaveMutex(); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
98 |
} |
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 |
|
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 |
UNS8 canSend(CAN_HANDLE fd0, Message *m) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
103 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
104 |
int res; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
105 |
struct can_frame frame; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
106 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
107 |
frame.can_id = m->cob_id.w; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
108 |
if (frame.can_id >= 0x800) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
109 |
frame.can_id |= CAN_EFF_FLAG; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
110 |
frame.can_dlc = m->len; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
111 |
if (m->rtr) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
112 |
frame.can_id |= CAN_RTR_FLAG; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
113 |
else |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
114 |
memcpy(frame.data, m->data, 8); |
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 |
res = CAN_SEND(fd0->fd, &frame, sizeof(frame), 0); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
117 |
if (res < 0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
118 |
return 1; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
119 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
120 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
121 |
} |
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 |
CAN_HANDLE canOpen(s_BOARD *board) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
125 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
126 |
CAN_HANDLE fd0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
127 |
struct ifreq ifr; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
128 |
struct sockaddr_can addr; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
129 |
int err; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
130 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
131 |
fd0 = malloc(sizeof(*fd0)); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
132 |
if (!fd0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
133 |
return NULL; |
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 |
fd0->fd = CAN_SOCKET(PF_CAN, SOCK_RAW, 0); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
136 |
if(fd0->fd < 0){ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
137 |
fprintf(stderr,"Socket creation failed.\n"); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
138 |
goto error_ret; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
139 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
140 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
141 |
snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
142 |
err = CAN_IOCTL(fd0->fd, SIOCGIFINDEX, &ifr); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
143 |
if (err) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
144 |
fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
145 |
goto error_close; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
146 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
147 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
148 |
addr.can_family = AF_CAN; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
149 |
addr.can_ifindex = ifr.ifr_ifindex; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
150 |
err = CAN_BIND(fd0->fd, (struct sockaddr *)&addr, |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
151 |
sizeof(addr)); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
152 |
if (err) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
153 |
fprintf(stderr, "Binding failed.\n"); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
154 |
goto error_close; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
155 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
156 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
157 |
fd0->d = board->d; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
158 |
CreateReceiveTask(fd0, &fd0->receiveTask); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
159 |
return fd0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
160 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
161 |
error_close: |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
162 |
CAN_CLOSE(fd0->fd); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
163 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
164 |
error_ret: |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
165 |
free(fd0); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
166 |
return NULL; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
167 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
168 |
|
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
169 |
/***************************************************************************/ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
170 |
int canClose(CAN_HANDLE fd0) |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
171 |
{ |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
172 |
if (fd0) { |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
173 |
WaitReceiveTaskEnd(&fd0->receiveTask); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
174 |
CAN_CLOSE(fd0->fd); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
175 |
free(fd0); |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
176 |
} |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
177 |
return 0; |
8a1047ab51f4
SOCKET-CAN support added. Many thanks to Jan Kiszka !
etisserant
parents:
diff
changeset
|
178 |
} |