494
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN
|
|
5 |
|
|
6 |
See COPYING file for copyrights details.
|
|
7 |
|
|
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.
|
|
12 |
|
|
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.
|
|
17 |
|
|
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
|
|
21 |
*/
|
|
22 |
|
|
23 |
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
24 |
#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000))
|
|
25 |
#define _CRT_SECURE_NO_DEPRECATE
|
|
26 |
#else
|
|
27 |
#include <stdio.h>
|
|
28 |
#include <string.h>
|
|
29 |
#include <errno.h>
|
|
30 |
#include <fcntl.h>
|
|
31 |
#endif
|
|
32 |
|
|
33 |
#include "can_driver.h"
|
|
34 |
#include "AnaGateDllCan.h"
|
|
35 |
|
|
36 |
|
|
37 |
typedef struct SAnaGatePort
|
|
38 |
{
|
|
39 |
int hHandle;
|
|
40 |
HANDLE hAnaRecEvent;
|
|
41 |
HANDLE hFesticalRecAcknowledge;
|
|
42 |
Message sMsgBuffer;
|
|
43 |
struct SAnaGatePort *pNext;
|
|
44 |
struct SAnaGatePort *pPrev;
|
|
45 |
} SAnaGatePort;
|
|
46 |
|
|
47 |
|
|
48 |
SAnaGatePort *pFirstAnaGatePort=NULL;
|
|
49 |
|
|
50 |
|
|
51 |
/********* AnaGate API CAN receive callback Funciton ****************/
|
|
52 |
void WINAPI AnaGateReceiveCallBack (int nIdentifier, const char* pcBuffer, int nBufferLen, int nFlags, int hHandle)
|
|
53 |
{
|
|
54 |
SAnaGatePort *pAnaGatePort = pFirstAnaGatePort;
|
|
55 |
int i;
|
|
56 |
|
|
57 |
while (pAnaGatePort->hHandle != hHandle )
|
|
58 |
{
|
|
59 |
pAnaGatePort = pAnaGatePort->pNext;
|
|
60 |
if (pAnaGatePort == pFirstAnaGatePort )
|
|
61 |
{
|
|
62 |
pAnaGatePort = NULL;
|
|
63 |
printf("AnaGateReceiveCallBack (AnaGate_Win32): ERROR: Can't find AnaGatePort-Objekt to the Received Handle %d\n",hHandle);
|
|
64 |
return;
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
pAnaGatePort->sMsgBuffer.cob_id = nIdentifier;
|
|
69 |
pAnaGatePort->sMsgBuffer.len= nBufferLen;
|
|
70 |
if (nFlags == 2)
|
|
71 |
pAnaGatePort->sMsgBuffer.rtr = 1;
|
|
72 |
else
|
|
73 |
pAnaGatePort->sMsgBuffer.rtr = 0;
|
|
74 |
|
|
75 |
for (i = 0 ; i < nBufferLen; i++)
|
|
76 |
{
|
|
77 |
pAnaGatePort->sMsgBuffer.data[i] = pcBuffer[i];
|
|
78 |
}
|
|
79 |
|
|
80 |
SetEvent(pAnaGatePort->hAnaRecEvent);
|
|
81 |
|
|
82 |
WaitForSingleObject(pAnaGatePort->hFesticalRecAcknowledge,INFINITE);
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
/*********functions which permit to communicate with the board****************/
|
703
|
87 |
UNS8 __stdcall canReceive_driver(CAN_HANDLE fd0, Message *m)
|
494
|
88 |
{
|
|
89 |
SAnaGatePort* pAnaGatePort = (SAnaGatePort*)fd0;
|
|
90 |
int i;
|
|
91 |
|
|
92 |
WaitForSingleObject( // Wait for receive event
|
|
93 |
pAnaGatePort->hAnaRecEvent, // event handle
|
|
94 |
INFINITE); // indefinite wait
|
|
95 |
|
|
96 |
|
|
97 |
m->cob_id = pAnaGatePort->sMsgBuffer.cob_id;
|
|
98 |
m->len = pAnaGatePort->sMsgBuffer.len;
|
|
99 |
m->rtr = pAnaGatePort->sMsgBuffer.rtr;
|
|
100 |
for (i = 0 ; i < pAnaGatePort->sMsgBuffer.len; i++)
|
|
101 |
{
|
|
102 |
m->data[i] = pAnaGatePort->sMsgBuffer.data[i];
|
|
103 |
}
|
|
104 |
|
|
105 |
SetEvent(pAnaGatePort->hFesticalRecAcknowledge); //Set Acknollede event
|
|
106 |
|
|
107 |
return 0;
|
|
108 |
}
|
|
109 |
|
|
110 |
/***************************************************************************/
|
703
|
111 |
UNS8 __stdcall canSend_driver(CAN_HANDLE fd0, Message const *m)
|
494
|
112 |
{
|
|
113 |
SAnaGatePort* pAnaCanPort = (SAnaGatePort*)fd0;
|
|
114 |
char cErrorMsg[100];
|
|
115 |
int nRetCode;
|
|
116 |
int nMsgTyp;
|
|
117 |
|
|
118 |
if (m->rtr == 0)
|
|
119 |
{
|
|
120 |
nMsgTyp = 0; //Normal;
|
|
121 |
}
|
|
122 |
else
|
|
123 |
{
|
|
124 |
nMsgTyp = 2; //Remote frame;
|
|
125 |
}
|
|
126 |
|
|
127 |
if ( (nRetCode = CANWrite(pAnaCanPort->hHandle , m->cob_id,(const char*) m->data, m->len, nMsgTyp) ) )
|
|
128 |
{
|
|
129 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
130 |
fprintf(stderr,"canSend_driver (AnaGate_Win32) %s \n",nRetCode);
|
|
131 |
//printf("canSend_driver (AnaGate_Win32) %s \n",nRetCode);
|
|
132 |
return 1;
|
|
133 |
}
|
|
134 |
|
|
135 |
return 0;
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
/***************************************************************************/
|
|
140 |
int TranslateBaudeRate(char* optarg){
|
|
141 |
if(!strcmp( optarg, "1M")) return 1000000;
|
|
142 |
if(!strcmp( optarg, "800K")) return 800000;
|
|
143 |
if(!strcmp( optarg, "500K")) return 500000;
|
|
144 |
if(!strcmp( optarg, "250K")) return 250000;
|
|
145 |
if(!strcmp( optarg, "125K")) return 125000;
|
|
146 |
if(!strcmp( optarg, "100K")) return 100000;
|
|
147 |
if(!strcmp( optarg, "50K")) return 50000;
|
|
148 |
if(!strcmp( optarg, "20K")) return 20000;
|
|
149 |
if(!strcmp( optarg, "10K")) return 10000;
|
|
150 |
|
|
151 |
return 0x0000;
|
|
152 |
}
|
|
153 |
/****************************************************************************/
|
|
154 |
|
703
|
155 |
UNS8 __stdcall canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud)
|
494
|
156 |
{
|
|
157 |
int nRetCode;
|
|
158 |
char cErrorMsg[100];
|
|
159 |
struct SAnaGatePort* pAnaGatePort = (struct SAnaGatePort*)fd0;
|
|
160 |
|
703
|
161 |
if (nRetCode = CANSetGlobals (pAnaGatePort->hHandle, TranslateBaudeRate(baud), 0, 0, 1, 0) )
|
494
|
162 |
{
|
|
163 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
164 |
fprintf(stderr, "canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg);
|
|
165 |
printf("canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg);
|
|
166 |
return 1;
|
|
167 |
}
|
|
168 |
return 0;
|
|
169 |
}
|
|
170 |
|
|
171 |
/***************************************************************************/
|
|
172 |
/* To open a connection to AnaGate CAN the s_BOARD board->busname must be
|
|
173 |
the AnaGate IP-Adresse followed from the CAN-Port (A or B) you want to use
|
|
174 |
For example "192.168.1.254:A"
|
|
175 |
*/
|
|
176 |
|
703
|
177 |
CAN_HANDLE __stdcall canOpen_driver(s_BOARD *board)
|
494
|
178 |
{
|
|
179 |
int PortNr;
|
|
180 |
char cErrorMsg[100];
|
|
181 |
int nRetCode;
|
|
182 |
char sIPAddress[16];
|
|
183 |
struct SAnaGatePort *pNewAnaGatePort;
|
|
184 |
char bBusnameValid = TRUE;
|
|
185 |
unsigned int nBusnameLen;
|
|
186 |
|
|
187 |
///////////////////////////////////////////
|
|
188 |
// Do some checkings concerning the busname
|
|
189 |
// format should be IP-Adress:Port
|
|
190 |
// e.g. 192.168.1.254:A
|
|
191 |
///////////////////////////////////////////
|
|
192 |
nBusnameLen = strlen(board->busname);
|
|
193 |
|
|
194 |
if ( nBusnameLen < strlen( "1.2.3.4:A" ) ) bBusnameValid = FALSE; // check minimum length of busname
|
|
195 |
if ( nBusnameLen > strlen( "123.234.345.456:A" ) ) bBusnameValid = FALSE; // check maximum length of busname
|
|
196 |
if ( bBusnameValid )
|
|
197 |
{
|
|
198 |
switch (board->busname[nBusnameLen-1]) // check Portname of busname
|
|
199 |
{
|
|
200 |
case ('A'): PortNr = 0; break;
|
|
201 |
case ('B'): PortNr = 1; break;
|
|
202 |
case ('C'): PortNr = 2; break;
|
|
203 |
case ('D'): PortNr = 3; break;
|
|
204 |
default : bBusnameValid = FALSE; break;
|
|
205 |
}
|
|
206 |
if (board->busname[nBusnameLen-2] != ':' ) bBusnameValid = FALSE; // check Colon before Portname
|
|
207 |
}
|
|
208 |
|
|
209 |
if ( ! bBusnameValid )
|
|
210 |
{
|
|
211 |
fprintf(stderr, "canOpen_driver (AnaGate_Win32): busname (\"%s\") has a wrong format. Use IPAddr:CANPort for example \"192.168.1.254:A\"\n",board->busname);
|
|
212 |
return (CAN_HANDLE) NULL;
|
|
213 |
}
|
|
214 |
|
|
215 |
board->busname[nBusnameLen-2] = 0; // NULL Terminator for IP Address string
|
|
216 |
strcpy (sIPAddress, board->busname);
|
|
217 |
|
|
218 |
pNewAnaGatePort = (SAnaGatePort*) malloc(sizeof (struct SAnaGatePort));
|
|
219 |
if (pFirstAnaGatePort == NULL)
|
|
220 |
{
|
|
221 |
pFirstAnaGatePort = pNewAnaGatePort;
|
|
222 |
pNewAnaGatePort->pNext = pNewAnaGatePort;
|
|
223 |
pNewAnaGatePort->pPrev = pNewAnaGatePort;
|
|
224 |
}
|
|
225 |
else
|
|
226 |
{ pNewAnaGatePort->pNext = pFirstAnaGatePort;
|
|
227 |
pNewAnaGatePort->pPrev = pFirstAnaGatePort->pPrev;
|
|
228 |
pFirstAnaGatePort->pPrev->pNext = pNewAnaGatePort;
|
|
229 |
pFirstAnaGatePort->pPrev = pNewAnaGatePort;
|
|
230 |
|
|
231 |
}
|
|
232 |
// Connect to AnaGate
|
|
233 |
if ( nRetCode = CANOpenDevice (&pNewAnaGatePort->hHandle,
|
|
234 |
0, /*confirmation*/
|
|
235 |
1, /*Monitor*/
|
|
236 |
PortNr,
|
|
237 |
sIPAddress,
|
|
238 |
1000 /*TimeOut*/ ) )
|
|
239 |
{
|
|
240 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
241 |
fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
242 |
//printf( "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
243 |
return (CAN_HANDLE) NULL;
|
|
244 |
}
|
|
245 |
|
|
246 |
// Inizial Baudrate
|
|
247 |
if (nRetCode = CANSetGlobals (pNewAnaGatePort->hHandle,
|
|
248 |
TranslateBaudeRate(board->baudrate),
|
|
249 |
0,/*OperatingMode = normal*/
|
|
250 |
0,/*CAN-Termination = off*/
|
703
|
251 |
1, /*HighSpeedMode = on*/
|
|
252 |
0 /*Timestamps = off*/) )
|
494
|
253 |
{
|
|
254 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
255 |
fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
256 |
//printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
257 |
return (CAN_HANDLE) NULL;
|
|
258 |
}
|
|
259 |
|
|
260 |
// Creat receive and receive-acknoledge event
|
|
261 |
pNewAnaGatePort->hAnaRecEvent = CreateEvent(
|
|
262 |
NULL, // default security attributes
|
|
263 |
FALSE, // manual-reset event
|
|
264 |
FALSE, // initial state is nonsignaled
|
|
265 |
NULL // object name
|
|
266 |
);
|
|
267 |
|
|
268 |
pNewAnaGatePort->hFesticalRecAcknowledge = CreateEvent(
|
|
269 |
NULL, // default security attributes
|
|
270 |
FALSE, // manual-reset event
|
|
271 |
FALSE, // initial state is nonsignaled
|
|
272 |
NULL // object name
|
|
273 |
);
|
|
274 |
|
|
275 |
// Install receive callback funktion
|
|
276 |
if (nRetCode = CANSetCallback(pNewAnaGatePort->hHandle, AnaGateReceiveCallBack) )
|
|
277 |
{
|
|
278 |
canClose_driver (pNewAnaGatePort);
|
|
279 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
280 |
fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
281 |
//printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress);
|
|
282 |
return (CAN_HANDLE) NULL;
|
|
283 |
}
|
|
284 |
|
|
285 |
return (CAN_HANDLE)pNewAnaGatePort;
|
|
286 |
}
|
|
287 |
|
|
288 |
/***************************************************************************/
|
703
|
289 |
int __stdcall canClose_driver(CAN_HANDLE fd0)
|
494
|
290 |
{
|
|
291 |
SAnaGatePort* pAnaCanPort = (SAnaGatePort*)fd0;
|
|
292 |
char cErrorMsg[100];
|
|
293 |
int nRetCode;
|
|
294 |
|
|
295 |
|
|
296 |
SetEvent (pAnaCanPort->hAnaRecEvent);
|
|
297 |
CloseHandle (pAnaCanPort->hAnaRecEvent);
|
|
298 |
CloseHandle (pAnaCanPort->hFesticalRecAcknowledge);
|
|
299 |
|
|
300 |
if ( nRetCode = CANCloseDevice(pAnaCanPort->hHandle) )
|
|
301 |
{
|
|
302 |
CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
|
|
303 |
fprintf(stderr, "canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
|
|
304 |
//printf("canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
|
|
305 |
}
|
|
306 |
|
|
307 |
if (pAnaCanPort->pNext == pAnaCanPort)
|
|
308 |
{
|
|
309 |
free (pAnaCanPort);
|
|
310 |
pFirstAnaGatePort=NULL;
|
|
311 |
}
|
|
312 |
else
|
|
313 |
{ pAnaCanPort->pNext->pPrev = pAnaCanPort->pPrev;
|
|
314 |
pAnaCanPort->pPrev->pNext = pAnaCanPort->pNext;
|
|
315 |
free (pAnaCanPort);
|
|
316 |
}
|
|
317 |
|
|
318 |
return 0;
|
|
319 |
}
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|