TestMasterSlave.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 #if defined(WIN32) && !defined(__CYGWIN__)
00024 #include <windows.h>
00025 #include "getopt.h"
00026 void pause(void)
00027 {
00028         system("PAUSE");
00029 }
00030 #else
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <stdlib.h>
00035 #include <signal.h>
00036 #endif
00037 
00038 #include "canfestival.h"
00039 //#include <can_driver.h>
00040 //#include <timers_driver.h>
00041 
00042 #include "Master.h"
00043 #include "Slave.h"
00044 #include "TestMasterSlave.h"
00045 
00046 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
00047 {
00048         eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
00049         return 0;
00050 }
00051 
00052 s_BOARD SlaveBoard = {"0", "125K"};
00053 s_BOARD MasterBoard = {"1", "125K"};
00054 
00055 #if !defined(WIN32) || defined(__CYGWIN__)
00056 void catch_signal(int sig)
00057 {
00058   signal(SIGTERM, catch_signal);
00059   signal(SIGINT, catch_signal);
00060   eprintf("Got Signal %d\n",sig);
00061 }
00062 #endif
00063 
00064 void help()
00065 {
00066   printf("**************************************************************\n");
00067   printf("*  TestMasterSlave                                           *\n");
00068   printf("*                                                            *\n");
00069   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
00070   printf("*  nodes in the same process. A master and a slave. Both     *\n");
00071   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
00072   printf("*  SDO and PDO. Master configure heartbeat producer time     *\n");
00073   printf("*  at 1000 ms for slave node-id 0x02 by concise DCF.         *\n");                                  
00074   printf("*                                                            *\n");
00075   printf("*   Usage:                                                   *\n");
00076   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
00077   printf("*                                                            *\n");
00078   printf("*   OPTIONS:                                                 *\n");
00079   printf("*     -l : Can library [\"libcanfestival_can_virtual.so\"]     *\n");
00080   printf("*                                                            *\n");
00081   printf("*    Slave:                                                  *\n");
00082   printf("*     -s : bus name [\"0\"]                                    *\n");
00083   printf("*     -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
00084   printf("*                                                            *\n");
00085   printf("*    Master:                                                 *\n");
00086   printf("*     -m : bus name [\"1\"]                                    *\n");
00087   printf("*     -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
00088   printf("*                                                            *\n");
00089   printf("**************************************************************\n");
00090 }
00091 
00092 /***************************  INIT  *****************************************/
00093 void InitNodes(CO_Data* d, UNS32 id)
00094 {
00095         /****************************** INITIALISATION SLAVE *******************************/
00096         if(strcmp(SlaveBoard.baudrate, "none")) {
00097                 /* Defining the node Id */
00098                 setNodeId(&TestSlave_Data, 0x02);
00099                 /* init */
00100                 setState(&TestSlave_Data, Initialisation);
00101         }
00102 
00103         /****************************** INITIALISATION MASTER *******************************/
00104         if(strcmp(MasterBoard.baudrate, "none")){
00105                 RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
00106                 
00107                 /* Defining the node Id */
00108                 setNodeId(&TestMaster_Data, 0x01);
00109 
00110                 /* init */
00111                 setState(&TestMaster_Data, Initialisation);
00112                         
00113         }
00114 }
00115 
00116 /****************************************************************************/
00117 /***************************  MAIN  *****************************************/
00118 /****************************************************************************/
00119 int main(int argc,char **argv)
00120 {
00121 
00122   int c;
00123   extern char *optarg;
00124   char* LibraryPath="libcanfestival_can_virtual.so";
00125 
00126   while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
00127   {
00128     switch(c)
00129     {
00130       case 's' :
00131         if (optarg[0] == 0)
00132         {
00133           help();
00134           exit(1);
00135         }
00136         SlaveBoard.busname = optarg;
00137         break;
00138       case 'm' :
00139         if (optarg[0] == 0)
00140         {
00141           help();
00142           exit(1);
00143         }
00144         MasterBoard.busname = optarg;
00145         break;
00146       case 'S' :
00147         if (optarg[0] == 0)
00148         {
00149           help();
00150           exit(1);
00151         }
00152         SlaveBoard.baudrate = optarg;
00153         break;
00154       case 'M' :
00155         if (optarg[0] == 0)
00156         {
00157           help();
00158           exit(1);
00159         }
00160         MasterBoard.baudrate = optarg;
00161         break;
00162       case 'l' :
00163         if (optarg[0] == 0)
00164         {
00165           help();
00166           exit(1);
00167         }
00168         LibraryPath = optarg;
00169         break;
00170       default:
00171         help();
00172         exit(1);
00173     }
00174   }
00175 
00176 #if !defined(WIN32) || defined(__CYGWIN__)
00177   /* install signal handler for manual break */
00178         signal(SIGTERM, catch_signal);
00179         signal(SIGINT, catch_signal);
00180 #endif
00181 
00182 #ifndef NOT_USE_DYNAMIC_LOADING
00183         LoadCanDriver(LibraryPath);
00184 #endif          
00185         // Open CAN devices
00186 
00187         if(strcmp(SlaveBoard.baudrate, "none")){
00188                 
00189                 TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
00190                 TestSlave_Data.initialisation = TestSlave_initialisation;
00191                 TestSlave_Data.preOperational = TestSlave_preOperational;
00192                 TestSlave_Data.operational = TestSlave_operational;
00193                 TestSlave_Data.stopped = TestSlave_stopped;
00194                 TestSlave_Data.post_sync = TestSlave_post_sync;
00195                 TestSlave_Data.post_TPDO = TestSlave_post_TPDO;
00196                 TestSlave_Data.storeODSubIndex = TestSlave_storeODSubIndex;             
00197 
00198                 if(!canOpen(&SlaveBoard,&TestSlave_Data)){
00199                         eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
00200                         goto fail_slave;
00201                 }
00202         }
00203         if(strcmp(MasterBoard.baudrate, "none")){
00204                 
00205                 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
00206                 TestMaster_Data.initialisation = TestMaster_initialisation;
00207                 TestMaster_Data.preOperational = TestMaster_preOperational;
00208                 TestMaster_Data.operational = TestMaster_operational;
00209                 TestMaster_Data.stopped = TestMaster_stopped;
00210                 TestMaster_Data.post_sync = TestMaster_post_sync;
00211                 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
00212                 
00213                 if(!canOpen(&MasterBoard,&TestMaster_Data)){
00214                         eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
00215                         goto fail_master;
00216                 }
00217         }
00218 
00219         // Start timer thread
00220         StartTimerLoop(&InitNodes);
00221 
00222         // wait Ctrl-C
00223         
00224         pause();
00225         eprintf("Finishing.\n");
00226         
00227         masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Reset_Node);
00228         eprintf("reset\n");
00229         // Stop master
00230         setState(&TestMaster_Data, Stopped);
00231         
00232         // Stop timer thread
00233         StopTimerLoop();
00234         
00235         // Close CAN devices (and can threads)
00236         if(strcmp(SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
00237 fail_master:
00238         if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);    
00239 fail_slave:
00240         return 0;
00241 }

Generated on Mon Jul 2 19:10:16 2007 for CanFestival by  doxygen 1.5.1