author | etisserant |
Wed, 04 Apr 2007 13:11:49 +0200 | |
changeset 146 | 71d531bf5b68 |
parent 145 | e747d2e26af0 |
child 157 | 8b45ff4202c3 |
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 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
30 |
#include "can_driver.h" |
0 | 31 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
32 |
#define MAX_NB_CAN_PIPES 16 |
0 | 33 |
|
34 |
typedef struct { |
|
35 |
char used; |
|
36 |
int pipe[2]; |
|
37 |
} CANPipe; |
|
38 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
39 |
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},}; |
0 | 40 |
|
41 |
/*********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
|
42 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
0 | 43 |
{ |
44 |
if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message)) |
|
45 |
{ |
|
46 |
return 1; |
|
47 |
} |
|
48 |
return 0; |
|
49 |
} |
|
50 |
||
51 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
52 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
0 | 53 |
{ |
54 |
int i; |
|
55 |
// Send to all readers, except myself |
|
56 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
57 |
{ |
|
58 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) |
|
59 |
{ |
|
60 |
write(canpipes[i].pipe[1], m, sizeof(Message)); |
|
61 |
} |
|
62 |
} |
|
63 |
return 0; |
|
64 |
} |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
65 |
/* |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
66 |
int TranslateBaudeRate(char* optarg){ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
78 |
}*/ |
0 | 79 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
80 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
0 | 81 |
{ |
82 |
int i; |
|
83 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
84 |
{ |
|
85 |
if(!canpipes[i].used) |
|
86 |
break; |
|
87 |
} |
|
88 |
||
89 |
/* Create the pipe. */ |
|
90 |
if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe)) |
|
91 |
{ |
|
92 |
fprintf (stderr, "Open failed.\n"); |
|
93 |
return (CAN_HANDLE)NULL; |
|
94 |
} |
|
95 |
||
96 |
canpipes[i].used = 1; |
|
97 |
return (CAN_HANDLE) &canpipes[i]; |
|
98 |
} |
|
99 |
||
100 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
101 |
int canClose_driver(CAN_HANDLE fd0) |
0 | 102 |
{ |
103 |
close(((CANPipe*)fd0)->pipe[0]); |
|
104 |
close(((CANPipe*)fd0)->pipe[1]); |
|
105 |
((CANPipe*)fd0)->used = 0; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
106 |
return 0; |
0 | 107 |
} |
108 |
||
109 |