author | etisserant |
Thu, 12 Apr 2007 16:36:31 +0200 | |
changeset 155 | 746b49869cbc |
parent 145 | e747d2e26af0 |
child 365 | 9b76e0881beb |
permissions | -rw-r--r-- |
26 | 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 |
#include <stdio.h> |
|
24 |
#include <string.h> |
|
25 |
#include <errno.h> |
|
26 |
#include <fcntl.h> |
|
27 |
||
28 |
#include "canmsg.h" |
|
29 |
#include "lincan.h" |
|
30 |
||
31 |
#include "can_driver.h" |
|
32 |
||
33 |
/*********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:
26
diff
changeset
|
34 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
26 | 35 |
{ |
36 |
int res; |
|
37 |
struct canmsg_t canmsg; |
|
38 |
||
39 |
canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */ |
|
40 |
||
41 |
do{ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
42 |
res = read(fd0,&canmsg,sizeof(canmsg_t)); |
26 | 43 |
if((res<0)&&(errno == -EAGAIN)) res = 0; |
44 |
}while(res==0); |
|
45 |
||
46 |
if(res != sizeof(canmsg_t)) // No new message |
|
47 |
return 1; |
|
48 |
||
49 |
if(canmsg.flags&MSG_EXT){ |
|
50 |
/* There is no mark for extended messages in CanFestival */; |
|
51 |
} |
|
52 |
||
53 |
m->cob_id.w = canmsg.id; |
|
54 |
m->len = canmsg.length; |
|
55 |
if(canmsg.flags&MSG_RTR){ |
|
56 |
m->rtr = 1; |
|
57 |
}else{ |
|
58 |
m->rtr = 0; |
|
59 |
memcpy(m->data,canmsg.data,8); |
|
60 |
} |
|
61 |
||
62 |
return 0; |
|
63 |
} |
|
64 |
||
65 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
66 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
26 | 67 |
{ |
68 |
int res; |
|
69 |
struct canmsg_t canmsg; |
|
70 |
||
71 |
||
72 |
canmsg.flags = 0; |
|
73 |
canmsg.id = m->cob_id.w; |
|
74 |
canmsg.length = m->len; |
|
75 |
if(m->rtr){ |
|
76 |
canmsg.flags |= MSG_RTR; |
|
77 |
}else{ |
|
78 |
memcpy(canmsg.data,m->data,8); |
|
79 |
} |
|
80 |
||
81 |
if(canmsg.id >= 0x800){ |
|
82 |
canmsg.flags |= MSG_EXT; |
|
83 |
} |
|
84 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
85 |
res = write(fd0,&canmsg,sizeof(canmsg_t)); |
26 | 86 |
if(res!=sizeof(canmsg_t)) |
87 |
return 1; |
|
88 |
||
89 |
return 0; |
|
90 |
} |
|
91 |
||
92 |
/***************************************************************************/ |
|
93 |
static const char lnx_can_dev_prefix[] = "/dev/can"; |
|
94 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
95 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
26 | 96 |
{ |
97 |
int name_len = strlen(board->busname); |
|
98 |
int prefix_len = strlen(lnx_can_dev_prefix); |
|
99 |
char dev_name[prefix_len+name_len+1]; |
|
100 |
int o_flags = 0; |
|
101 |
CAN_HANDLE fd0; |
|
102 |
||
103 |
fd0=malloc(sizeof(*fd0)); |
|
104 |
if(fd0==NULL) |
|
105 |
return NULL; |
|
106 |
||
107 |
/*o_flags = O_NONBLOCK;*/ |
|
108 |
||
109 |
memcpy(dev_name,lnx_can_dev_prefix,prefix_len); |
|
110 |
memcpy(dev_name+prefix_len,board->busname,name_len); |
|
111 |
dev_name[prefix_len+name_len] = 0; |
|
112 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
113 |
fd0 = open(dev_name, O_RDWR|o_flags); |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
114 |
if(fd0 < 0){ |
26 | 115 |
fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname); |
116 |
goto error_ret; |
|
117 |
} |
|
118 |
||
119 |
return fd0; |
|
120 |
||
121 |
error_ret: |
|
122 |
free(fd0); |
|
123 |
return NULL; |
|
124 |
} |
|
125 |
||
126 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
127 |
int canClose_driver(CAN_HANDLE fd0) |
26 | 128 |
{ |
129 |
if(!fd0) |
|
130 |
return 0; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
131 |
close(fd0); |
26 | 132 |
return 0; |
133 |
} |