author | Christian Taedcke <hacking@taedcke.com> |
Tue, 12 Jun 2012 01:06:38 +0200 | |
changeset 736 | a915095ab9e7 |
parent 723 | c33da109a4c8 |
child 773 | c10def4bfbde |
permissions | -rwxr-xr-x |
556 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
Copyright (C) Win32 Port Leonid Tochinski |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
6 |
Modified by: Jaroslav Fojtik |
556 | 7 |
|
8 |
See COPYING file for copyrights details. |
|
9 |
||
10 |
This library is free software; you can redistribute it and/or |
|
11 |
modify it under the terms of the GNU Lesser General Public |
|
12 |
License as published by the Free Software Foundation; either |
|
13 |
version 2.1 of the License, or (at your option) any later version. |
|
14 |
||
15 |
This library is distributed in the hope that it will be useful, |
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
Lesser General Public License for more details. |
|
19 |
||
20 |
You should have received a copy of the GNU Lesser General Public |
|
21 |
License along with this library; if not, write to the Free Software |
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
26 |
CAN driver interface. |
|
27 |
*/ |
|
28 |
||
29 |
#include <windows.h> |
|
591 | 30 |
|
31 |
#ifdef __cplusplus |
|
32 |
extern "C" { |
|
33 |
#endif |
|
34 |
||
556 | 35 |
#include "canfestival.h" |
36 |
#include "timer.h" |
|
37 |
#include "timers_driver.h" |
|
591 | 38 |
|
39 |
#ifdef __cplusplus |
|
577 | 40 |
}; |
591 | 41 |
#endif |
42 |
||
556 | 43 |
// GetProcAddress doesn't have an UNICODE version for NT |
44 |
#ifdef UNDER_CE |
|
45 |
#define myTEXT(str) TEXT(str) |
|
46 |
#else |
|
47 |
#define myTEXT(str) str |
|
48 |
#endif |
|
49 |
||
50 |
#define MAX_NB_CAN_PORTS 16 |
|
51 |
||
52 |
typedef UNS8 (CALLBACK* CANRECEIVE_DRIVER_PROC)(void* inst, Message *m); |
|
53 |
typedef UNS8 (CALLBACK* CANSEND_DRIVER_PROC)(void* inst, const Message *m); |
|
54 |
typedef void* (CALLBACK* CANOPEN_DRIVER_PROC)(s_BOARD *board); |
|
55 |
typedef int (CALLBACK* CANCLOSE_DRIVER_PROC)(void* inst); |
|
56 |
typedef UNS8 (CALLBACK* CANCHANGEBAUDRATE_DRIVER_PROC)(void* fd, char* baud); |
|
57 |
||
58 |
CANRECEIVE_DRIVER_PROC m_canReceive; |
|
59 |
CANSEND_DRIVER_PROC m_canSend; |
|
60 |
CANOPEN_DRIVER_PROC m_canOpen; |
|
61 |
CANCLOSE_DRIVER_PROC m_canClose; |
|
62 |
CANCHANGEBAUDRATE_DRIVER_PROC m_canChangeBaudRate; |
|
63 |
||
64 |
/* CAN port structure */ |
|
65 |
typedef struct |
|
66 |
{ |
|
67 |
char used; /**< flag indicating CAN port usage, will be used to abort Receiver task*/ |
|
68 |
CAN_HANDLE fd; /**< CAN port file descriptor*/ |
|
69 |
TASK_HANDLE receiveTask; /**< CAN Receiver task*/ |
|
70 |
CO_Data* d; /**< CAN object data*/ |
|
71 |
}CANPort; |
|
72 |
||
73 |
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}}; |
|
74 |
||
75 |
||
76 |
/***************************************************************************/ |
|
77 |
UNS8 UnLoadCanDriver(LIB_HANDLE handle) |
|
78 |
{ |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
79 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 80 |
if(handle != NULL) |
81 |
{ |
|
82 |
FreeLibrary(handle); |
|
83 |
handle=NULL; |
|
84 |
return 0; |
|
85 |
} |
|
86 |
return -1; |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
87 |
#else |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
88 |
handle = NULL; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
89 |
return 0; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
90 |
#endif //NOT_USE_DYNAMIC_LOADING |
556 | 91 |
} |
92 |
||
93 |
/***************************************************************************/ |
|
94 |
/** |
|
95 |
* Loads the dll and get funcs ptr |
|
96 |
* |
|
97 |
* @param driver_name String containing driver's dynamic library name |
|
98 |
* @return Library handle |
|
99 |
*/ |
|
723 | 100 |
LIB_HANDLE LoadCanDriver(LPCSTR driver_name) |
556 | 101 |
{ |
102 |
// driver module handle |
|
103 |
LIB_HANDLE handle = NULL; |
|
104 |
||
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
105 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 106 |
if(handle == NULL) |
107 |
{ |
|
723 | 108 |
handle = LoadLibraryA(driver_name); |
556 | 109 |
} |
577 | 110 |
|
111 |
if (!handle) |
|
556 | 112 |
{ |
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
113 |
fprintf (stderr, "%d\n", GetLastError()); |
556 | 114 |
return NULL; |
115 |
} |
|
116 |
||
117 |
m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver")); |
|
118 |
m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver")); |
|
119 |
m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver")); |
|
120 |
m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver")); |
|
121 |
m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver")); |
|
696 | 122 |
|
123 |
if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL) |
|
124 |
{ |
|
125 |
m_canReceive = NULL; |
|
126 |
m_canSend = NULL; |
|
127 |
m_canOpen = NULL; |
|
128 |
m_canClose = NULL; |
|
129 |
m_canChangeBaudRate = NULL; |
|
130 |
FreeLibrary(handle); |
|
131 |
handle = NULL; |
|
132 |
} |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
133 |
#else |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
134 |
//compiled in... |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
135 |
handle = 1; //TODO: remove this hack |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
136 |
|
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
137 |
m_canReceive = canReceive_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
138 |
m_canSend = canSend_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
139 |
m_canOpen = canOpen_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
140 |
m_canClose = canClose_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
141 |
m_canChangeBaudRate = canChangeBaudRate_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
142 |
#endif |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
143 |
|
577 | 144 |
|
556 | 145 |
return handle; |
146 |
} |
|
147 |
||
148 |
/***************************************************************************/ |
|
149 |
UNS8 canSend(CAN_PORT port, Message *m) |
|
150 |
{ |
|
151 |
if (port && (m_canSend != NULL)) |
|
152 |
{ |
|
691
17d9c0736935
[ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents:
687
diff
changeset
|
153 |
return m_canSend(((CANPort*)port)->fd, m); |
17d9c0736935
[ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents:
687
diff
changeset
|
154 |
} |
696 | 155 |
return 1; /* NOT OK */ |
556 | 156 |
} |
157 |
||
158 |
/***************************************************************************/ |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
159 |
DWORD canReceiveLoop(CAN_PORT port) |
556 | 160 |
{ |
161 |
Message m; |
|
162 |
while(((CANPort*)port)->used) |
|
163 |
{ |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
164 |
if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue; |
556 | 165 |
EnterMutex(); |
166 |
canDispatch(((CANPort*)port)->d, &m); |
|
167 |
LeaveMutex(); |
|
168 |
} |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
169 |
return 0; |
556 | 170 |
} |
171 |
||
172 |
/***************************************************************************/ |
|
173 |
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d) |
|
174 |
{ |
|
175 |
int i; |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
176 |
CAN_HANDLE fd0; |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
177 |
|
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
178 |
|
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
179 |
/* Fix of multiple opening one data set, added by J.Fojtik. */ |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
180 |
if(d->canHandle) |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
181 |
{ |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
182 |
canClose(d); |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
183 |
} |
591 | 184 |
|
556 | 185 |
for(i=0; i < MAX_NB_CAN_PORTS; i++) |
186 |
{ |
|
187 |
if(!canports[i].used) |
|
188 |
break; |
|
189 |
} |
|
190 |
||
577 | 191 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 192 |
if (m_canOpen == NULL) |
193 |
{ |
|
194 |
fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
|
195 |
return NULL; |
|
196 |
} |
|
197 |
#endif |
|
577 | 198 |
|
591 | 199 |
fd0 = m_canOpen(board); |
556 | 200 |
if(fd0) |
201 |
{ |
|
202 |
canports[i].used = 1; |
|
203 |
canports[i].fd = fd0; |
|
204 |
canports[i].d = d; |
|
205 |
d->canHandle = (CAN_PORT)&canports[i]; |
|
577 | 206 |
CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop); |
556 | 207 |
return (CAN_PORT)&canports[i]; |
208 |
} |
|
209 |
else |
|
210 |
{ |
|
687 | 211 |
MSG("CanOpen : Cannot open board {busname='%S',baudrate='%S'}\n",board->busname, board->baudrate); |
556 | 212 |
return NULL; |
213 |
} |
|
214 |
} |
|
215 |
||
216 |
/***************************************************************************/ |
|
217 |
int canClose(CO_Data * d) |
|
218 |
{ |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
219 |
UNS8 res; |
591 | 220 |
CANPort* tmp; |
221 |
||
222 |
tmp = (CANPort*)d->canHandle; |
|
577 | 223 |
|
591 | 224 |
if(tmp) |
225 |
{ |
|
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
226 |
d->canHandle = NULL; |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
227 |
|
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
228 |
// close CAN port |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
229 |
res = m_canClose(tmp->fd); |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
230 |
|
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
231 |
// kill receiver task |
629 | 232 |
WaitReceiveTaskEnd(&tmp->receiveTask); |
233 |
||
697
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
234 |
// release used flag as a last step. |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
235 |
tmp->used = 0; |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
236 |
} |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
237 |
else res = 255; |
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
238 |
|
24a2aec61731
ID: 3459307 - Async problem in drivers/win32/win32.c
JaFojtik
parents:
696
diff
changeset
|
239 |
return res; |
556 | 240 |
} |
241 |
||
242 |
UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
|
243 |
{ |
|
244 |
if(port){ |
|
245 |
UNS8 res; |
|
246 |
//LeaveMutex(); |
|
247 |
res = m_canChangeBaudRate(((CANPort*)port)->fd, baud); |
|
248 |
//EnterMutex(); |
|
249 |
return res; // OK |
|
577 | 250 |
} |
556 | 251 |
return 1; // NOT OK |
252 |
} |
|
253 |