]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - drivers/can_lincan/can_lincan.c
4e81849060f4680dbcd1abc5c0283b665105db75
[CanFestival-3.git] / drivers / can_lincan / can_lincan.c
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****************/
34 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
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{
42     res = read(fd0,&canmsg,sizeof(canmsg_t));
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 = 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 /***************************************************************************/
66 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
67 {
68   int res;
69   struct canmsg_t canmsg;
70
71
72   canmsg.flags = 0;
73   canmsg.id = m->cob_id;
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
85   res = write(fd0,&canmsg,sizeof(canmsg_t));
86   if(res!=sizeof(canmsg_t))
87     return 1;
88
89   return 0;
90 }
91
92 /***************************************************************************/
93 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
94 {
95         printf("canChangeBaudRate not yet supported by this driver\n");
96         return 0;
97 }
98
99 /***************************************************************************/
100 static const char lnx_can_dev_prefix[] = "/dev/can";
101
102 CAN_HANDLE canOpen_driver(s_BOARD *board)
103 {
104   int name_len = strlen(board->busname);
105   int prefix_len = strlen(lnx_can_dev_prefix);
106   char dev_name[prefix_len+name_len+1];
107   int o_flags = 0;
108   CAN_HANDLE fd0;
109
110   fd0=malloc(sizeof(*fd0));
111   if(fd0==NULL)
112     return NULL;
113
114   /*o_flags = O_NONBLOCK;*/
115
116   memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
117   memcpy(dev_name+prefix_len,board->busname,name_len);
118   dev_name[prefix_len+name_len] = 0;
119
120   fd0 = open(dev_name, O_RDWR|o_flags);
121   if(fd0 < 0){
122     fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
123     goto error_ret;
124   }
125
126   return fd0;
127
128  error_ret:
129   free(fd0);
130   return NULL;
131 }
132
133 /***************************************************************************/
134 int canClose_driver(CAN_HANDLE fd0)
135 {
136   if(!fd0)
137     return 0;
138   close(fd0);
139   return 0;
140 }