author | groke6 |
Tue, 08 Jan 2008 10:47:10 +0100 | |
changeset 349 | 1e6dd4ef46b9 |
parent 301 | f4b64aa848e1 |
child 384 | 83793fc7ce48 |
permissions | -rw-r--r-- |
0 | 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 |
/* |
|
24 |
Virtual CAN driver. |
|
25 |
*/ |
|
26 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
27 |
#include <stdio.h> |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
28 |
#include <unistd.h> |
0 | 29 |
|
301 | 30 |
#define NEED_PRINT_MESSAGE |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
31 |
#include "can_driver.h" |
157 | 32 |
#include "def.h" |
0 | 33 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
34 |
#define MAX_NB_CAN_PIPES 16 |
0 | 35 |
|
36 |
typedef struct { |
|
37 |
char used; |
|
38 |
int pipe[2]; |
|
39 |
} CANPipe; |
|
40 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
41 |
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},}; |
0 | 42 |
|
43 |
/*********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:
0
diff
changeset
|
44 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
0 | 45 |
{ |
193
0487270d441c
Fixed problems in can_virtual, loop when closing pipes.
etisserant
parents:
157
diff
changeset
|
46 |
if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) != (ssize_t)sizeof(Message)) |
0 | 47 |
{ |
48 |
return 1; |
|
49 |
} |
|
50 |
return 0; |
|
51 |
} |
|
52 |
||
53 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
54 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
0 | 55 |
{ |
56 |
int i; |
|
157 | 57 |
|
58 |
printf("%x->[ ", (CANPipe*)fd0 - &canpipes[0]); |
|
59 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
60 |
{ |
|
61 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) |
|
62 |
{ |
|
63 |
printf("%x ",i); |
|
64 |
} |
|
65 |
} |
|
66 |
printf(" ]"); |
|
67 |
print_message(m); |
|
68 |
||
0 | 69 |
// Send to all readers, except myself |
70 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
71 |
{ |
|
72 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) |
|
73 |
{ |
|
74 |
write(canpipes[i].pipe[1], m, sizeof(Message)); |
|
75 |
} |
|
76 |
} |
|
77 |
return 0; |
|
78 |
} |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
79 |
/* |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
80 |
int TranslateBaudeRate(char* optarg){ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
81 |
if(!strcmp( optarg, "1M")) return 1000; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
82 |
if(!strcmp( optarg, "500K")) return 500; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
83 |
if(!strcmp( optarg, "250K")) return 250; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
84 |
if(!strcmp( optarg, "125K")) return 125; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
85 |
if(!strcmp( optarg, "100K")) return 100; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
86 |
if(!strcmp( optarg, "50K")) return 50; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
87 |
if(!strcmp( optarg, "20K")) return 20; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
88 |
if(!strcmp( optarg, "10K")) return 10; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
89 |
if(!strcmp( optarg, "5K")) return 5; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
90 |
if(!strcmp( optarg, "none")) return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
91 |
return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
92 |
}*/ |
0 | 93 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
94 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
0 | 95 |
{ |
96 |
int i; |
|
97 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
98 |
{ |
|
99 |
if(!canpipes[i].used) |
|
100 |
break; |
|
101 |
} |
|
102 |
||
103 |
/* Create the pipe. */ |
|
104 |
if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe)) |
|
105 |
{ |
|
106 |
fprintf (stderr, "Open failed.\n"); |
|
107 |
return (CAN_HANDLE)NULL; |
|
108 |
} |
|
109 |
||
110 |
canpipes[i].used = 1; |
|
111 |
return (CAN_HANDLE) &canpipes[i]; |
|
112 |
} |
|
113 |
||
114 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
115 |
int canClose_driver(CAN_HANDLE fd0) |
0 | 116 |
{ |
117 |
close(((CANPipe*)fd0)->pipe[0]); |
|
118 |
close(((CANPipe*)fd0)->pipe[1]); |
|
119 |
((CANPipe*)fd0)->used = 0; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
120 |
return 0; |
0 | 121 |
} |
122 |
||
123 |