]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/TestMasterSlave/TestMasterSlave.c
Removed SDOTimeout application callback.Please use SDO callbacks instead.
[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", "500K"};
53 s_BOARD MasterBoard = {"1", "500K"};
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()
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.                                              *\n");
73   printf("*                                                            *\n");
74   printf("*   Usage:                                                   *\n");
75   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
76   printf("*                                                            *\n");
77   printf("*   OPTIONS:                                                 *\n");
78   printf("*     -l : Can library [\"libcanfestival_can_virtual.so\"]     *\n");
79   printf("*                                                            *\n");
80   printf("*    Slave:                                                  *\n");
81   printf("*     -s : bus name [\"0\"]                                    *\n");
82   printf("*     -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
83   printf("*                                                            *\n");
84   printf("*    Master:                                                 *\n");
85   printf("*     -m : bus name [\"1\"]                                    *\n");
86   printf("*     -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
87   printf("*                                                            *\n");
88   printf("**************************************************************\n");
89 }
90
91 /***************************  INIT  *****************************************/
92 void InitNodes(CO_Data* d, UNS32 id)
93 {
94         /****************************** INITIALISATION SLAVE *******************************/
95         if(SlaveBoard.baudrate) {
96                 /* Defining the node Id */
97                 setNodeId(&TestSlave_Data, 0x02);
98                 /* init */
99                 setState(&TestSlave_Data, Initialisation);
100         }
101
102         /****************************** INITIALISATION MASTER *******************************/
103         if(MasterBoard.baudrate){
104                 RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
105
106                 /* Defining the node Id */
107                 setNodeId(&TestMaster_Data, 0x01);
108
109                 /* init */
110                 setState(&TestMaster_Data, Initialisation);
111
112                 /****************************** START *******************************/
113                 /* Put the master in operational mode */
114                 setState(&TestMaster_Data, Operational);
115   
116                 /* Ask slave node to go in operational mode */
117                 masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
118         }
119 }
120
121 /****************************************************************************/
122 /***************************  MAIN  *****************************************/
123 /****************************************************************************/
124 int main(int argc,char **argv)
125 {
126
127   char c;
128   extern char *optarg;
129   char* LibraryPath="libcanfestival_can_virtual.so";
130
131   while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
132   {
133     switch(c)
134     {
135       case 's' :
136         if (optarg[0] == 0)
137         {
138           help();
139           exit(1);
140         }
141         SlaveBoard.busname = optarg;
142         break;
143       case 'm' :
144         if (optarg[0] == 0)
145         {
146           help();
147           exit(1);
148         }
149         MasterBoard.busname = optarg;
150         break;
151       case 'S' :
152         if (optarg[0] == 0)
153         {
154           help();
155           exit(1);
156         }
157         SlaveBoard.baudrate = optarg;
158         break;
159       case 'M' :
160         if (optarg[0] == 0)
161         {
162           help();
163           exit(1);
164         }
165         MasterBoard.baudrate = optarg;
166         break;
167       case 'l' :
168         if (optarg[0] == 0)
169         {
170           help();
171           exit(1);
172         }
173         LibraryPath = optarg;
174         break;
175       default:
176         help();
177         exit(1);
178     }
179   }
180
181 #if !defined(WIN32) || defined(__CYGWIN__)
182   /* install signal handler for manual break */
183         signal(SIGTERM, catch_signal);
184         signal(SIGINT, catch_signal);
185 #endif
186
187 #ifndef NOT_USE_DYNAMIC_LOADING
188         LoadCanDriver(LibraryPath);
189 #endif          
190         // Open CAN devices
191         if(strcmp( SlaveBoard.baudrate, "none")){
192                 
193                 TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
194                 TestSlave_Data.initialisation = TestSlave_initialisation;
195                 TestSlave_Data.preOperational = TestSlave_preOperational;
196                 TestSlave_Data.operational = TestSlave_operational;
197                 TestSlave_Data.stopped = TestSlave_stopped;
198                 TestSlave_Data.post_sync = TestSlave_post_sync;
199                 TestSlave_Data.post_TPDO = TestSlave_post_TPDO;
200                 TestSlave_Data.storeODSubIndex = TestSlave_storeODSubIndex;             
201
202                 if(!canOpen(&SlaveBoard,&TestSlave_Data)){
203                         eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
204                         goto fail_slave;
205                 }
206         }
207
208         if(strcmp( MasterBoard.baudrate, "none")){
209                 
210                 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
211                 TestMaster_Data.initialisation = TestMaster_initialisation;
212                 TestMaster_Data.preOperational = TestMaster_preOperational;
213                 TestMaster_Data.operational = TestMaster_operational;
214                 TestMaster_Data.stopped = TestMaster_stopped;
215                 TestMaster_Data.post_sync = TestMaster_post_sync;
216                 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
217                 
218                 if(!canOpen(&MasterBoard,&TestMaster_Data)){
219                         eprintf("Cannot open Master Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
220                         goto fail_master;
221                 }
222         }
223         
224         // Start timer thread
225         StartTimerLoop(&InitNodes);
226
227         // wait Ctrl-C
228         pause();
229         eprintf("Finishing.\n");
230         
231         // Stop timer thread
232         StopTimerLoop();
233         
234         // Close CAN devices (and can threads)
235         if(strcmp( SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
236 fail_master:
237         if(strcmp( MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);   
238 fail_slave:
239         
240
241   return 0;
242 }