]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/TestMasterSlave/TestMasterSlave.c
021e39a328c8dae7d5a0955ae2aa964c434603ce
[CanFestival-3.git] / examples / TestMasterSlave / TestMasterSlave.c
1 /*
2 This file is part of CanFestival, a library implementing CanOpen Stack. 
3
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
5
6 See COPYING file for copyrights details.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23 #if defined(WIN32) && !defined(__CYGWIN__)
24 #include <windows.h>
25 #include "getopt.h"
26 void pause(void)
27 {
28         system("PAUSE");
29 }
30 #else
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <signal.h>
36 #endif
37
38 #include "canfestival.h"
39 //#include <can_driver.h>
40 //#include <timers_driver.h>
41
42 #include "Master.h"
43 #include "Slave.h"
44 #include "TestMasterSlave.h"
45
46 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
47 {
48         eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
49         return 0;
50 }
51
52 s_BOARD SlaveBoard = {"0", "125K"};
53 s_BOARD MasterBoard = {"1", "125K"};
54
55 #if !defined(WIN32) || defined(__CYGWIN__)
56 void catch_signal(int sig)
57 {
58   signal(SIGTERM, catch_signal);
59   signal(SIGINT, catch_signal);
60   eprintf("Got Signal %d\n",sig);
61 }
62 #endif
63
64 void help(void)
65 {
66   printf("**************************************************************\n");
67   printf("*  TestMasterSlave                                           *\n");
68   printf("*                                                            *\n");
69   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
70   printf("*  nodes in the same process. A master and a slave. Both     *\n");
71   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
72   printf("*  SDO and PDO. Master configure heartbeat producer time     *\n");
73   printf("*  at 1000 ms for slave node-id 0x02 by concise DCF.         *\n");                                  
74   printf("*                                                            *\n");
75   printf("*   Usage:                                                   *\n");
76   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
77   printf("*                                                            *\n");
78   printf("*   OPTIONS:                                                 *\n");
79   printf("*     -l : Can library [\"libcanfestival_can_virtual.so\"]     *\n");
80   printf("*                                                            *\n");
81   printf("*    Slave:                                                  *\n");
82   printf("*     -s : bus name [\"0\"]                                    *\n");
83   printf("*     -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
84   printf("*                                                            *\n");
85   printf("*    Master:                                                 *\n");
86   printf("*     -m : bus name [\"1\"]                                    *\n");
87   printf("*     -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
88   printf("*                                                            *\n");
89   printf("**************************************************************\n");
90 }
91
92 /***************************  INIT  *****************************************/
93 void InitNodes(CO_Data* d, UNS32 id)
94 {
95         /****************************** INITIALISATION SLAVE *******************************/
96         if(strcmp(SlaveBoard.baudrate, "none")) {
97                 setNodeId(&TestSlave_Data, 0x02);
98
99                 /* init */
100                 setState(&TestSlave_Data, Initialisation);
101         }
102
103         /****************************** INITIALISATION MASTER *******************************/
104         if(strcmp(MasterBoard.baudrate, "none")){
105                 RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
106                 
107                 /* Defining the node Id */
108                 setNodeId(&TestMaster_Data, 0x01);
109
110                 /* init */
111                 setState(&TestMaster_Data, Initialisation);
112                         
113         }
114 }
115
116 /***************************  EXIT  *****************************************/
117 void Exit(CO_Data* d, UNS32 id)
118 {
119         masterSendNMTstateChange(&TestMaster_Data, 0x02, NMT_Reset_Node);    
120     
121     //Stop master
122         setState(&TestMaster_Data, Stopped);
123 }
124
125 /****************************************************************************/
126 /***************************  MAIN  *****************************************/
127 /****************************************************************************/
128 int main(int argc,char **argv)
129 {
130
131   int c;
132   extern char *optarg;
133   char* LibraryPath="../../drivers/can_virtual/libcanfestival_can_virtual.so";
134
135   while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
136   {
137     switch(c)
138     {
139       case 's' :
140         if (optarg[0] == 0)
141         {
142           help();
143           exit(1);
144         }
145         SlaveBoard.busname = optarg;
146         break;
147       case 'm' :
148         if (optarg[0] == 0)
149         {
150           help();
151           exit(1);
152         }
153         MasterBoard.busname = optarg;
154         break;
155       case 'S' :
156         if (optarg[0] == 0)
157         {
158           help();
159           exit(1);
160         }
161         SlaveBoard.baudrate = optarg;
162         break;
163       case 'M' :
164         if (optarg[0] == 0)
165         {
166           help();
167           exit(1);
168         }
169         MasterBoard.baudrate = optarg;
170         break;
171       case 'l' :
172         if (optarg[0] == 0)
173         {
174           help();
175           exit(1);
176         }
177         LibraryPath = optarg;
178         break;
179       default:
180         help();
181         exit(1);
182     }
183   }
184
185 #if !defined(WIN32) || defined(__CYGWIN__)
186   /* install signal handler for manual break */
187         signal(SIGTERM, catch_signal);
188         signal(SIGINT, catch_signal);
189         TimerInit();
190 #endif
191
192 #ifndef NOT_USE_DYNAMIC_LOADING
193         if (LoadCanDriver(LibraryPath) == NULL)
194             printf("Unable to load library: %s\n",LibraryPath);
195 #endif          
196         // Open CAN devices
197
198         if(strcmp(SlaveBoard.baudrate, "none")){
199                 
200                 TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
201                 TestSlave_Data.initialisation = TestSlave_initialisation;
202                 TestSlave_Data.preOperational = TestSlave_preOperational;
203                 TestSlave_Data.operational = TestSlave_operational;
204                 TestSlave_Data.stopped = TestSlave_stopped;
205                 TestSlave_Data.post_sync = TestSlave_post_sync;
206                 TestSlave_Data.post_TPDO = TestSlave_post_TPDO;
207                 TestSlave_Data.storeODSubIndex = TestSlave_storeODSubIndex;
208                 TestSlave_Data.post_emcy = TestSlave_post_emcy;
209
210                 if(!canOpen(&SlaveBoard,&TestSlave_Data)){
211                         eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
212                         goto fail_slave;
213                 }
214         }
215         if(strcmp(MasterBoard.baudrate, "none")){
216                 
217                 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
218                 TestMaster_Data.initialisation = TestMaster_initialisation;
219                 TestMaster_Data.preOperational = TestMaster_preOperational;
220                 TestMaster_Data.operational = TestMaster_operational;
221                 TestMaster_Data.stopped = TestMaster_stopped;
222                 TestMaster_Data.post_sync = TestMaster_post_sync;
223                 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
224                 TestMaster_Data.post_emcy = TestMaster_post_emcy;
225                 TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
226                 
227                 if(!canOpen(&MasterBoard,&TestMaster_Data)){
228                         eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
229                         goto fail_master;
230                 }
231         }
232
233         // Start timer thread
234         StartTimerLoop(&InitNodes);
235
236         // wait Ctrl-C
237         pause();
238
239         // Stop timer thread
240         StopTimerLoop(&Exit);
241         
242         // Close CAN devices (and can threads)
243         if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);    
244 fail_master:
245         if(strcmp(SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
246 fail_slave:
247         TimerCleanup();
248         return 0;
249 }