/home/epimerde/documents/tc11/CanFestival-3/drivers/can_virtual/can_virtual.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 /*
00024         Virtual CAN driver.
00025 */
00026 
00027 #include <stdio.h>
00028 #include <unistd.h>
00029 
00030 #include "can_driver.h"
00031 #include "def.h"
00032 
00033 #define MAX_NB_CAN_PIPES 16
00034 
00035 typedef struct {
00036   char used;
00037   int pipe[2];
00038 } CANPipe;
00039 
00040 CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},};
00041 
00042 /*********functions which permit to communicate with the board****************/
00043 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
00044 {
00045         if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) != (ssize_t)sizeof(Message))
00046         {
00047                 return 1;
00048         }
00049         return 0;
00050 }
00051 
00052 #define MyCase(fc) case fc: printf(#fc);break;
00053 void print_message(Message *m)
00054 {
00055         int i;
00056         switch(m->cob_id.w >> 7)
00057         {
00058                 MyCase(SYNC)
00059                 MyCase(TIME_STAMP)
00060                 MyCase(PDO1tx)
00061                 MyCase(PDO1rx)
00062                 MyCase(PDO2tx)
00063                 MyCase(PDO2rx)
00064                 MyCase(PDO3tx)
00065                 MyCase(PDO3rx)
00066                 MyCase(PDO4tx)
00067                 MyCase(PDO4rx)
00068                 MyCase(SDOtx)
00069                 MyCase(SDOrx)
00070                 MyCase(NODE_GUARD)
00071                 MyCase(NMT)
00072         }
00073         printf(" rtr:%d", m->rtr);
00074         printf(" len:%d", m->len);
00075         for (i = 0 ; i < m->len ; i++)
00076                 printf(" %02x", m->data[i]);
00077         printf("\n");
00078 }
00079 
00080 /***************************************************************************/
00081 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
00082 {
00083   int i;
00084   
00085   printf("%x->[ ", (CANPipe*)fd0 - &canpipes[0]); 
00086   for(i=0; i < MAX_NB_CAN_PIPES; i++)
00087   {
00088         if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
00089         {
00090                 printf("%x ",i);        
00091         }
00092   }
00093   printf(" ]"); 
00094   print_message(m);
00095   
00096   // Send to all readers, except myself
00097   for(i=0; i < MAX_NB_CAN_PIPES; i++)
00098   {
00099         if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
00100         {
00101                 write(canpipes[i].pipe[1], m, sizeof(Message));
00102         }
00103   }
00104   return 0;
00105 }
00106 /*
00107 int TranslateBaudeRate(char* optarg){
00108         if(!strcmp( optarg, "1M")) return 1000;
00109         if(!strcmp( optarg, "500K")) return 500;
00110         if(!strcmp( optarg, "250K")) return 250;
00111         if(!strcmp( optarg, "125K")) return 125;
00112         if(!strcmp( optarg, "100K")) return 100;
00113         if(!strcmp( optarg, "50K")) return 50;
00114         if(!strcmp( optarg, "20K")) return 20;
00115         if(!strcmp( optarg, "10K")) return 10;
00116         if(!strcmp( optarg, "5K")) return 5;
00117         if(!strcmp( optarg, "none")) return 0;
00118         return 0;
00119 }*/
00120 /***************************************************************************/
00121 CAN_HANDLE canOpen_driver(s_BOARD *board)
00122 {
00123   int i;  
00124   for(i=0; i < MAX_NB_CAN_PIPES; i++)
00125   {
00126         if(!canpipes[i].used)
00127                 break;
00128   }
00129 
00130   /* Create the pipe.  */
00131   if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe))
00132     {
00133       fprintf (stderr, "Open failed.\n");
00134       return (CAN_HANDLE)NULL;
00135     }
00136 
00137    canpipes[i].used = 1;
00138    return (CAN_HANDLE) &canpipes[i];
00139 }
00140 
00141 /***************************************************************************/
00142 int canClose_driver(CAN_HANDLE fd0)
00143 {
00144   close(((CANPipe*)fd0)->pipe[0]);
00145   close(((CANPipe*)fd0)->pipe[1]);
00146   ((CANPipe*)fd0)->used = 0;
00147   return 0;
00148 }
00149 
00150 

Generated on Mon Jun 4 16:29:06 2007 for CanFestival by  doxygen 1.5.1