source: trunk/MagicSoft/Cosy/candrv/network.cc@ 1152

Last change on this file since 1152 was 1140, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.2 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of Stesy, the MAGIC Steering System
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 2001
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25///////////////////////////////////////////////////////////////////////
26//
27// Network
28//
29// is a collection of nodes which coordinates the network
30//
31///////////////////////////////////////////////////////////////////////
32#include "network.h"
33
34#include <iostream.h> // cout
35#include <iomanip.h> // setw, setfill
36
37ClassImp(Network);
38
39// --------------------------------------------------------------------------
40//
41// Start the canopen module
42// Initialize all nodes (calling NodeDrv::Init()
43//
44void Network::Start()
45{
46 lout << "- Starting network." << endl;
47
48 VmodIcan::Start();
49 InitNodes();
50
51 lout << "- Network started." << endl;
52}
53
54// --------------------------------------------------------------------------
55//
56// Stop all nodes, stop the can module
57//
58void Network::Stop()
59{
60 lout << "- Stopping network." << endl;
61
62 StopNodes();
63 VmodIcan::Stop();
64
65 lout << "- Network stopped." << endl;
66}
67
68// --------------------------------------------------------------------------
69//
70// Initialize the network, set all nodes to NULL (n/a)
71//
72Network::Network(const char *dev, const int baud, MLog &out) : CanOpen(dev, baud, out)
73{
74 for (int i=0; i<32; i++)
75 fNodes[i] = NULL;
76}
77
78// --------------------------------------------------------------------------
79//
80// Distributes the received SDO messages to the addressed nodes.
81// Depending on the received command either HandleSDO, HandleSDOOK or
82// HandleSDOError called.
83// HandleSDO: Handles a received value
84// HandleSDOOK: Handles the acknoledgment of a trasmitted SDO
85// HandleSDOError: Handles error occursion (see CanOpen standard)
86//
87void Network::HandleSDO(BYTE_t node, BYTE_t cmd, WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv)
88{
89 switch (cmd)
90 {
91 case kSDO_TX4: // answer to 0x40 with 4 bytes of data
92 if (fNodes[node])
93 {
94 fNodes[node]->HandleSDO(idx, subidx, data, tv);
95 return;
96 }
97 break;
98
99 case kSDO_TX3: // answer to 0x40 with 2 bytes of data
100 if (fNodes[node])
101 {
102 fNodes[node]->HandleSDO(idx, subidx, data>>16, tv);
103 return;
104 }
105 break;
106
107 case kSDO_TX1: // answer to 0x40 with 1 byte of data
108 if (fNodes[node])
109 {
110 fNodes[node]->HandleSDO(idx, subidx, data>>24, tv);
111 return;
112 }
113 break;
114
115 case kSDO_TX_OK: // answer to a SDO_TX message
116 if (fNodes[node])
117 {
118 fNodes[node]->HandleSDOOK(idx, subidx);
119 return;
120 }
121 break;
122
123 case kSDO_TX_ERROR: // error message (instead of 0x60)
124 if (fNodes[node])
125 {
126 fNodes[node]->HandleSDOError(data);
127 return;
128 }
129 break;
130 }
131 cout << dec << setfill('0');
132 cout << "Node=" << (int)node << " Cmd=0x" << hex << (int)cmd << ": ";
133 cout << "Sdo=" << idx << "/" << (int)subidx << ": 0x" << setw(8) << data;
134 cout << endl;
135}
136
137// --------------------------------------------------------------------------
138//
139// Distributes PDO1 messages to the correspoding node calling HandlePDO1
140//
141void Network::HandlePDO1(BYTE_t node, BYTE_t *data, timeval_t *tv)
142{
143 if (!fNodes[node])
144 {
145 cout << "Node " << dec << (int)node << ", PDO1: " << hex;
146 for (int i=0; i<8; i++)
147 cout << " 0x" << (int)data[i];
148 cout << endl;
149 return;
150 }
151
152 fNodes[node]->HandlePDO1(data, tv);
153}
154
155// --------------------------------------------------------------------------
156//
157// Distributes PDO2 messages to the correspoding node calling HandlePDO2
158//
159void Network::HandlePDO2(BYTE_t node, BYTE_t *data, timeval_t *tv)
160{
161 if (!fNodes[node])
162 {
163 cout << "Node " << dec << (int)node << ", PDO2: " << hex;
164 for (int i=0; i<8; i++)
165 cout << " 0x" << (int)data[i];
166 cout << endl;
167 return;
168 }
169
170 fNodes[node]->HandlePDO2(data, tv);
171}
172
173// --------------------------------------------------------------------------
174//
175// Distributes PDO3 messages to the correspoding node calling HandlePDO3
176//
177void Network::HandlePDO3(BYTE_t node, BYTE_t *data, timeval_t *tv)
178{
179 if (!fNodes[node])
180 {
181 cout << "Node " << dec << (int)node << ", PDO3: " << hex;
182 for (int i=0; i<8; i++)
183 cout << " 0x" << (int)data[i];
184 cout << endl;
185 return;
186 }
187
188 fNodes[node]->HandlePDO3(data, tv);
189}
190
191// --------------------------------------------------------------------------
192//
193// Distributes PDO4 messages to the correspoding node calling HandlePDO4
194//
195void Network::HandlePDO4(BYTE_t node, BYTE_t *data, timeval_t *tv)
196{
197 if (!fNodes[node])
198 {
199 cout << "Node " << dec << (int)node << ", PDO4: " << hex;
200 for (int i=0; i<8; i++)
201 cout << " 0x" << (int)data[i];
202 cout << endl;
203 return;
204 }
205
206 fNodes[node]->HandlePDO4(data, tv);
207}
208
209// --------------------------------------------------------------------------
210//
211// Sets a node to a given nodedrv. The id is requested from the drv object.
212//
213void Network::SetNode(NodeDrv *drv)
214{
215 const BYTE_t nodeid = drv->GetId();
216
217 if (nodeid>31)
218 {
219 cout << "SetNode - Error: Only node Numbers < 32 are allowed"<< endl;
220 return;
221 }
222
223 fNodes[nodeid] = drv;
224}
225
226// --------------------------------------------------------------------------
227//
228// Initializes all nodes calling InitDevice
229//
230void Network::InitNodes()
231{
232 for (int i=0; i<32; i++)
233 if (fNodes[i])
234 {
235 lout << "- Initializing Node #" << dec << i << endl;
236 fNodes[i]->InitDevice(this);
237 fNodeInitialized[i] = TRUE;
238 }
239 lout << "- All Nodes initialized." << endl;
240}
241
242// --------------------------------------------------------------------------
243//
244// Stop all nodes calling StopDevice
245//
246void Network::StopNodes()
247{
248 for (int i=0; i<32; i++)
249 if (fNodes[i] && fNodeInitialized[i])
250 {
251 lout << "- Stopping Node #" << dec << i << endl;
252 fNodes[i]->StopDevice();
253 }
254 lout << "- All Nodes stopped." << endl;
255}
256
257// --------------------------------------------------------------------------
258//
259// returns true if one of the nodes has the error-flag set (HasError).
260//
261bool Network::HasError() const
262{
263 for (int i=0; i<32; i++)
264 if (fNodes[i] && fNodes[i]->HasError())
265 return true;
266
267 return false;
268}
Note: See TracBrowser for help on using the repository browser.