00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00034 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
00035 {
00036 int res;
00037 struct canmsg_t canmsg;
00038
00039 canmsg.flags = 0;
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))
00047 return 1;
00048
00049 if(canmsg.flags&MSG_EXT){
00050 ;
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
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 }