nico@215: nico@215:
nico@215:00001 /* nico@215: 00002 This file is part of CanFestival, a library implementing CanOpen Stack. nico@215: 00003 nico@215: 00004 Copyright (C): Edouard TISSERANT and Francis DUPIN nico@215: 00005 nico@215: 00006 See COPYING file for copyrights details. nico@215: 00007 nico@215: 00008 This library is free software; you can redistribute it and/or nico@215: 00009 modify it under the terms of the GNU Lesser General Public nico@215: 00010 License as published by the Free Software Foundation; either nico@215: 00011 version 2.1 of the License, or (at your option) any later version. nico@215: 00012 nico@215: 00013 This library is distributed in the hope that it will be useful, nico@215: 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of nico@215: 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nico@215: 00016 Lesser General Public License for more details. nico@215: 00017 nico@215: 00018 You should have received a copy of the GNU Lesser General Public nico@215: 00019 License along with this library; if not, write to the Free Software nico@215: 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA nico@215: 00021 */ nico@215: 00022 nico@215: 00023 /* nico@215: 00024 Virtual CAN driver. nico@215: 00025 */ nico@215: 00026 nico@215: 00027 #include <stdio.h> nico@215: 00028 #include <unistd.h> nico@215: 00029 nico@215: 00030 #include "can_driver.h" nico@215: 00031 #include "def.h" nico@215: 00032 etisserant@240: 00033 #define MAX_NB_CAN_PIPES 16 nico@215: 00034 nico@215: 00035 typedef struct { etisserant@240: 00036 char used; etisserant@240: 00037 int pipe[2]; nico@215: 00038 } CANPipe; nico@215: 00039 etisserant@240: 00040 CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},}; nico@215: 00041 nico@215: 00042 /*********functions which permit to communicate with the board****************/ etisserant@240: 00043 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) nico@215: 00044 { nico@215: 00045 if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) != (ssize_t)sizeof(Message)) nico@215: 00046 { nico@215: 00047 return 1; nico@215: 00048 } nico@215: 00049 return 0; nico@215: 00050 } nico@215: 00051 etisserant@240: 00052 #define MyCase(fc) case fc: printf(#fc);break; etisserant@240: 00053 void print_message(Message *m) nico@215: 00054 { nico@215: 00055 int i; etisserant@240: 00056 switch(m->cob_id.w >> 7) nico@215: 00057 { etisserant@240: 00058 MyCase(SYNC) etisserant@240: 00059 MyCase(TIME_STAMP) etisserant@240: 00060 MyCase(PDO1tx) etisserant@240: 00061 MyCase(PDO1rx) etisserant@240: 00062 MyCase(PDO2tx) etisserant@240: 00063 MyCase(PDO2rx) etisserant@240: 00064 MyCase(PDO3tx) etisserant@240: 00065 MyCase(PDO3rx) etisserant@240: 00066 MyCase(PDO4tx) etisserant@240: 00067 MyCase(PDO4rx) etisserant@240: 00068 MyCase(SDOtx) etisserant@240: 00069 MyCase(SDOrx) etisserant@240: 00070 MyCase(NODE_GUARD) etisserant@240: 00071 MyCase(NMT) nico@215: 00072 } etisserant@240: 00073 printf(" rtr:%d", m->rtr); etisserant@240: 00074 printf(" len:%d", m->len); etisserant@240: 00075 for (i = 0 ; i < m->len ; i++) etisserant@240: 00076 printf(" %02x", m->data[i]); nico@215: 00077 printf("\n"); nico@215: 00078 } nico@215: 00079 nico@215: 00080 /***************************************************************************/ etisserant@240: 00081 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) nico@215: 00082 { nico@215: 00083 int i; nico@215: 00084 etisserant@240: 00085 printf("%x->[ ", (CANPipe*)fd0 - &canpipes[0]); etisserant@240: 00086 for(i=0; i < MAX_NB_CAN_PIPES; i++) nico@215: 00087 { etisserant@240: 00088 if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) nico@215: 00089 { nico@215: 00090 printf("%x ",i); nico@215: 00091 } nico@215: 00092 } nico@215: 00093 printf(" ]"); etisserant@240: 00094 print_message(m); nico@215: 00095 nico@215: 00096 // Send to all readers, except myself nico@215: 00097 for(i=0; i < MAX_NB_CAN_PIPES; i++) nico@215: 00098 { etisserant@240: 00099 if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) nico@215: 00100 { etisserant@240: 00101 write(canpipes[i].pipe[1], m, sizeof(Message)); nico@215: 00102 } nico@215: 00103 } nico@215: 00104 return 0; nico@215: 00105 } nico@215: 00106 /* nico@215: 00107 int TranslateBaudeRate(char* optarg){ nico@215: 00108 if(!strcmp( optarg, "1M")) return 1000; nico@215: 00109 if(!strcmp( optarg, "500K")) return 500; nico@215: 00110 if(!strcmp( optarg, "250K")) return 250; nico@215: 00111 if(!strcmp( optarg, "125K")) return 125; nico@215: 00112 if(!strcmp( optarg, "100K")) return 100; nico@215: 00113 if(!strcmp( optarg, "50K")) return 50; nico@215: 00114 if(!strcmp( optarg, "20K")) return 20; nico@215: 00115 if(!strcmp( optarg, "10K")) return 10; nico@215: 00116 if(!strcmp( optarg, "5K")) return 5; nico@215: 00117 if(!strcmp( optarg, "none")) return 0; nico@215: 00118 return 0; nico@215: 00119 }*/ nico@215: 00120 /***************************************************************************/ etisserant@240: 00121 CAN_HANDLE canOpen_driver(s_BOARD *board) nico@215: 00122 { nico@215: 00123 int i; etisserant@240: 00124 for(i=0; i < MAX_NB_CAN_PIPES; i++) nico@215: 00125 { etisserant@240: 00126 if(!canpipes[i].used) nico@215: 00127 break; nico@215: 00128 } nico@215: 00129 nico@215: 00130 /* Create the pipe. */ etisserant@240: 00131 if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe)) nico@215: 00132 { nico@215: 00133 fprintf (stderr, "Open failed.\n"); etisserant@240: 00134 return (CAN_HANDLE)NULL; nico@215: 00135 } nico@215: 00136 etisserant@240: 00137 canpipes[i].used = 1; etisserant@240: 00138 return (CAN_HANDLE) &canpipes[i]; nico@215: 00139 } nico@215: 00140 nico@215: 00141 /***************************************************************************/ etisserant@240: 00142 int canClose_driver(CAN_HANDLE fd0) nico@215: 00143 { nico@215: 00144 close(((CANPipe*)fd0)->pipe[0]); nico@215: 00145 close(((CANPipe*)fd0)->pipe[1]); nico@215: 00146 ((CANPipe*)fd0)->used = 0; nico@215: 00147 return 0; nico@215: 00148 } nico@215: 00149 nico@215: 00150 etisserant@240: