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 |
|
|
27 |
#include <stdlib.h>
|
|
28 |
|
|
29 |
#include <sys/types.h>
|
|
30 |
#include <unistd.h>
|
|
31 |
#include <stdio.h>
|
|
32 |
|
|
33 |
#include <applicfg.h>
|
|
34 |
#include "timer.h"
|
|
35 |
#include "can_driver.h"
|
|
36 |
#include "timers_driver.h"
|
|
37 |
|
|
38 |
#define MAX_NB_CAN_PIPES 10
|
|
39 |
|
|
40 |
typedef struct {
|
|
41 |
char used;
|
|
42 |
int pipe[2];
|
|
43 |
TASK_HANDLE receiveTask;
|
|
44 |
CO_Data* d;
|
|
45 |
} CANPipe;
|
|
46 |
|
|
47 |
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,{0,0},},};
|
|
48 |
|
|
49 |
/*********functions which permit to communicate with the board****************/
|
|
50 |
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
|
|
51 |
{
|
|
52 |
if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message))
|
|
53 |
{
|
|
54 |
return 1;
|
|
55 |
}
|
|
56 |
return 0;
|
|
57 |
}
|
|
58 |
|
|
59 |
void canReceiveLoop(CAN_HANDLE fd0)
|
|
60 |
{
|
|
61 |
CO_Data* d = ((CANPipe*)fd0)->d;
|
|
62 |
Message m;
|
|
63 |
while (1) {
|
|
64 |
if(!canReceive(fd0, &m))
|
|
65 |
{
|
|
66 |
EnterMutex();
|
|
67 |
canDispatch(d, &m);
|
|
68 |
LeaveMutex();
|
|
69 |
}else{
|
|
70 |
break;
|
|
71 |
}
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
/***************************************************************************/
|
|
76 |
UNS8 canSend(CAN_HANDLE fd0, Message *m)
|
|
77 |
{
|
|
78 |
int i;
|
|
79 |
// Send to all readers, except myself
|
|
80 |
for(i=0; i < MAX_NB_CAN_PIPES; i++)
|
|
81 |
{
|
|
82 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
|
|
83 |
{
|
|
84 |
write(canpipes[i].pipe[1], m, sizeof(Message));
|
|
85 |
}
|
|
86 |
}
|
|
87 |
return 0;
|
|
88 |
}
|
|
89 |
|
|
90 |
/***************************************************************************/
|
|
91 |
CAN_HANDLE canOpen(s_BOARD *board)
|
|
92 |
{
|
|
93 |
int i;
|
|
94 |
for(i=0; i < MAX_NB_CAN_PIPES; i++)
|
|
95 |
{
|
|
96 |
if(!canpipes[i].used)
|
|
97 |
break;
|
|
98 |
}
|
|
99 |
|
|
100 |
/* Create the pipe. */
|
|
101 |
if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe))
|
|
102 |
{
|
|
103 |
fprintf (stderr, "Open failed.\n");
|
|
104 |
return (CAN_HANDLE)NULL;
|
|
105 |
}
|
|
106 |
|
|
107 |
canpipes[i].used = 1;
|
|
108 |
|
|
109 |
canpipes[i].d = board->d;
|
|
110 |
CreateReceiveTask((CAN_HANDLE) &canpipes[i], &canpipes[i].receiveTask);
|
|
111 |
|
|
112 |
return (CAN_HANDLE) &canpipes[i];
|
|
113 |
}
|
|
114 |
|
|
115 |
/***************************************************************************/
|
|
116 |
int canClose(CAN_HANDLE fd0)
|
|
117 |
{
|
|
118 |
close(((CANPipe*)fd0)->pipe[0]);
|
|
119 |
close(((CANPipe*)fd0)->pipe[1]);
|
|
120 |
((CANPipe*)fd0)->used = 0;
|
|
121 |
WaitReceiveTaskEnd(&((CANPipe*)fd0)->receiveTask);
|
|
122 |
}
|
|
123 |
|
|
124 |
|