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