can_lincan.c

Go to the documentation of this file.
00001 /*
00002 This file is part of CanFestival, a library implementing CanOpen Stack.
00003 
00004 Copyright (C): Edouard TISSERANT and Francis DUPIN
00005 
00006 See COPYING file for copyrights details.
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <errno.h>
00026 #include <fcntl.h>
00027 
00028 #include "canmsg.h"
00029 #include "lincan.h"
00030 
00031 #include "can_driver.h"
00032 
00033 /*********functions which permit to communicate with the board****************/
00034 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
00035 {
00036   int res;
00037   struct canmsg_t canmsg;
00038 
00039   canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */
00040 
00041   do{
00042     res = read(fd0,&canmsg,sizeof(canmsg_t));
00043     if((res<0)&&(errno == -EAGAIN)) res = 0;
00044   }while(res==0);
00045 
00046   if(res != sizeof(canmsg_t)) // No new message
00047     return 1;
00048 
00049   if(canmsg.flags&MSG_EXT){
00050     /* There is no mark for extended messages in CanFestival */;
00051   }
00052 
00053   m->cob_id.w = canmsg.id;
00054   m->len = canmsg.length;
00055   if(canmsg.flags&MSG_RTR){
00056     m->rtr = 1;
00057   }else{
00058     m->rtr = 0;
00059     memcpy(m->data,canmsg.data,8);
00060   }
00061 
00062   return 0;
00063 }
00064 
00065 /***************************************************************************/
00066 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
00067 {
00068   int res;
00069   struct canmsg_t canmsg;
00070 
00071 
00072   canmsg.flags = 0;
00073   canmsg.id = m->cob_id.w;
00074   canmsg.length = m->len;
00075   if(m->rtr){
00076     canmsg.flags |= MSG_RTR;
00077   }else{
00078     memcpy(canmsg.data,m->data,8);
00079   }
00080 
00081   if(canmsg.id >= 0x800){
00082     canmsg.flags |= MSG_EXT;
00083   }
00084 
00085   res = write(fd0,&canmsg,sizeof(canmsg_t));
00086   if(res!=sizeof(canmsg_t))
00087     return 1;
00088 
00089   return 0;
00090 }
00091 
00092 /***************************************************************************/
00093 static const char lnx_can_dev_prefix[] = "/dev/can";
00094 
00095 CAN_HANDLE canOpen_driver(s_BOARD *board)
00096 {
00097   int name_len = strlen(board->busname);
00098   int prefix_len = strlen(lnx_can_dev_prefix);
00099   char dev_name[prefix_len+name_len+1];
00100   int o_flags = 0;
00101   CAN_HANDLE fd0;
00102 
00103   fd0=malloc(sizeof(*fd0));
00104   if(fd0==NULL)
00105     return NULL;
00106 
00107   /*o_flags = O_NONBLOCK;*/
00108 
00109   memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
00110   memcpy(dev_name+prefix_len,board->busname,name_len);
00111   dev_name[prefix_len+name_len] = 0;
00112 
00113   fd0 = open(dev_name, O_RDWR|o_flags);
00114   if(fd0 < 0){
00115     fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
00116     goto error_ret;
00117   }
00118 
00119   return fd0;
00120 
00121  error_ret:
00122   free(fd0);
00123   return NULL;
00124 }
00125 
00126 /***************************************************************************/
00127 int canClose_driver(CAN_HANDLE fd0)
00128 {
00129   if(!fd0)
00130     return 0;
00131   close(fd0);
00132   return 0;
00133 }

Generated on Mon Jul 2 19:10:15 2007 for CanFestival by  doxygen 1.5.1