nico@207: nico@207: nico@207: CanFestival: /home/epimerde/documents/tc11/CanFestival-3/drivers/can_virtual/can_virtual.c Source File nico@207: nico@207: nico@207: nico@207: nico@207:
nico@207:
nico@207:
nico@207:
nico@207:

/home/epimerde/documents/tc11/CanFestival-3/drivers/can_virtual/can_virtual.c

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

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