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 | // NodeDrv
|
---|
28 | //
|
---|
29 | // Base class for a class describing the interface for the CAN nodes.
|
---|
30 | //
|
---|
31 | // to be overloaded:
|
---|
32 | // virtual void Init()
|
---|
33 | // virtual void StopDevice()
|
---|
34 | // virtual void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv)
|
---|
35 | // virtual void HandleSDOOK(WORD_t idx, BYTE_t subidx)
|
---|
36 | // virtual void HandleSDOError(LWORD_t data)
|
---|
37 | // virtual void HandlePDO1(BYTE_t *data, timeval_t *tv)
|
---|
38 | // virtual void HandlePDO2(BYTE_t *data, timeval_t *tv)
|
---|
39 | // virtual void HandlePDO3(BYTE_t *data, timeval_t *tv)
|
---|
40 | // virtual void HandlePDO4(BYTE_t *data, timeval_t *tv)
|
---|
41 | // virtual bool Reboot();
|
---|
42 | // virtual void CheckConnection();
|
---|
43 | //
|
---|
44 | ///////////////////////////////////////////////////////////////////////
|
---|
45 | #include "nodedrv.h"
|
---|
46 |
|
---|
47 | #include <iomanip.h>
|
---|
48 | #include <iostream.h>
|
---|
49 |
|
---|
50 | #include "network.h"
|
---|
51 | #include "MLogManip.h"
|
---|
52 |
|
---|
53 | ClassImp(NodeDrv);
|
---|
54 |
|
---|
55 | // --------------------------------------------------------------------------
|
---|
56 | //
|
---|
57 | // Constructor for one node. Sets the Node Id (<32) the logging stream
|
---|
58 | // and the node name. The name is a name for debug output.
|
---|
59 | //
|
---|
60 | NodeDrv::NodeDrv(BYTE_t nodeid, const char *name, MLog &out) : Log(out), fNetwork(NULL), fId(32), fError(0), fIsZombie(kTRUE)
|
---|
61 | {
|
---|
62 | if (nodeid>0x1f)
|
---|
63 | {
|
---|
64 | cout << "SetNode - Error: Only node Numbers < 32 are allowed"<< endl;
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | fId = nodeid;
|
---|
69 |
|
---|
70 | if (name)
|
---|
71 | fName = name;
|
---|
72 | else
|
---|
73 | {
|
---|
74 | fName = "Node#";
|
---|
75 | fName += (int)nodeid;
|
---|
76 | }
|
---|
77 |
|
---|
78 | lout << "- Node #" << (int)nodeid << " (" << name << ") initialized." << endl;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // --------------------------------------------------------------------------
|
---|
82 | //
|
---|
83 | // Empty destructor
|
---|
84 | //
|
---|
85 | NodeDrv::~NodeDrv()
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // This should be called from a master or main thread to get a node out
|
---|
92 | // of the Zombie-Status. Overload it by your needs.
|
---|
93 | //
|
---|
94 | bool NodeDrv::Reboot()
|
---|
95 | {
|
---|
96 | fIsZombie = false;
|
---|
97 |
|
---|
98 | Init();
|
---|
99 |
|
---|
100 | return !fIsZombie;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // --------------------------------------------------------------------------
|
---|
104 | //
|
---|
105 | // Init device, sets the pointer to the whole network and enables
|
---|
106 | // the Can messages to be passed through the interface:
|
---|
107 | // PDO1 tx
|
---|
108 | // PDO2 tx
|
---|
109 | // PDO3 tx
|
---|
110 | // PDO4 tx
|
---|
111 | // SDO rx
|
---|
112 | // SDO tx
|
---|
113 | //
|
---|
114 | bool NodeDrv::InitDevice(Network *net)
|
---|
115 | {
|
---|
116 | fNetwork = net;
|
---|
117 |
|
---|
118 | EnableCanMsg(kPDO1_TX);
|
---|
119 | EnableCanMsg(kPDO2_TX);
|
---|
120 | EnableCanMsg(kPDO3_TX);
|
---|
121 | EnableCanMsg(kPDO4_TX);
|
---|
122 | EnableCanMsg(kSDO_RX);
|
---|
123 | EnableCanMsg(kSDO_TX);
|
---|
124 |
|
---|
125 | fIsZombie = kFALSE;
|
---|
126 |
|
---|
127 | Init();
|
---|
128 |
|
---|
129 | return !fIsZombie;
|
---|
130 | }
|
---|
131 |
|
---|
132 | // --------------------------------------------------------------------------
|
---|
133 | //
|
---|
134 | // Print an "SDO idx/subidx set." from this device message.
|
---|
135 | // This output is never redirected to the GUI
|
---|
136 | //
|
---|
137 | void NodeDrv::HandleSDOOK(WORD_t idx, BYTE_t subidx)
|
---|
138 | {
|
---|
139 | const Bool_t gui = lout.IsOutputDeviceEnabled(MLog::eGui);
|
---|
140 |
|
---|
141 | if (gui)
|
---|
142 | lout << ddev(MLog::eGui);
|
---|
143 |
|
---|
144 | lout << hex << setfill('0');
|
---|
145 | lout << "Sdo=" << idx << "/" << (int)subidx << " set.";
|
---|
146 | lout << endl;
|
---|
147 |
|
---|
148 | if (gui)
|
---|
149 | lout << edev(MLog::eGui);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // --------------------------------------------------------------------------
|
---|
153 | //
|
---|
154 | // Print an error message with the corresponding data from this device.
|
---|
155 | //
|
---|
156 | void NodeDrv::HandleSDOError(LWORD_t data)
|
---|
157 | {
|
---|
158 | lout << "Nodedrv: SDO Error: Entry not found in dictionary (data=0x";
|
---|
159 | lout << hex << setfill('0') << setw(4) << data << ")";
|
---|
160 | lout << endl;
|
---|
161 | }
|
---|
162 |
|
---|
163 | // --------------------------------------------------------------------------
|
---|
164 | //
|
---|
165 | // Prints the received SDo from this device
|
---|
166 | //
|
---|
167 | void NodeDrv::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv)
|
---|
168 | {
|
---|
169 | cout << "SdoRx: Idx=0x"<< hex << idx << "/" << (int)subidx;
|
---|
170 | cout << ", val=0x" << val << endl;
|
---|
171 | }
|
---|
172 |
|
---|
173 | // --------------------------------------------------------------------------
|
---|
174 | //
|
---|
175 | // Sends the given PDO1 through the network to this device
|
---|
176 | // A PDO is carrying up to eight bytes of information.
|
---|
177 | //
|
---|
178 | // The message is not send if the node has the status Zombie.
|
---|
179 | // In this case false is returned, otherwise true
|
---|
180 | //
|
---|
181 | bool NodeDrv::SendPDO1(BYTE_t data[8])
|
---|
182 | {
|
---|
183 | if (!fIsZombie)
|
---|
184 | fNetwork->SendPDO1(fId, data);
|
---|
185 | return !fIsZombie;
|
---|
186 | }
|
---|
187 |
|
---|
188 | // --------------------------------------------------------------------------
|
---|
189 | //
|
---|
190 | // Sends the given PDO2 through the network to this device
|
---|
191 | // A PDO is carrying up to eight bytes of information.
|
---|
192 | //
|
---|
193 | // The message is not send if the node has the status Zombie.
|
---|
194 | // In this case false is returned, otherwise true
|
---|
195 | //
|
---|
196 | bool NodeDrv::SendPDO2(BYTE_t data[8])
|
---|
197 | {
|
---|
198 | if (!fIsZombie)
|
---|
199 | fNetwork->SendPDO2(fId, data);
|
---|
200 | return !fIsZombie;
|
---|
201 | }
|
---|
202 |
|
---|
203 | // --------------------------------------------------------------------------
|
---|
204 | //
|
---|
205 | // Sends the given PDO1 through the network to this device
|
---|
206 | // A PDO is carrying up to eight bytes of information.
|
---|
207 | //
|
---|
208 | // The message is not send if the node has the status Zombie.
|
---|
209 | // In this case false is returned, otherwise true
|
---|
210 | //
|
---|
211 | bool NodeDrv::SendPDO1(BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
---|
212 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
---|
213 | {
|
---|
214 | if (!fIsZombie)
|
---|
215 | fNetwork->SendPDO1(fId, m0, m1, m2, m3, m4, m5, m6, m7);
|
---|
216 | return !fIsZombie;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // Sends the given PDO2 through the network to this device
|
---|
222 | // A PDO is carrying up to eight bytes of information.
|
---|
223 | //
|
---|
224 | // The message is not send if the node has the status Zombie.
|
---|
225 | // In this case false is returned, otherwise true
|
---|
226 | //
|
---|
227 | bool NodeDrv::SendPDO2(BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
---|
228 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
---|
229 | {
|
---|
230 | if (!fIsZombie)
|
---|
231 | fNetwork->SendPDO2(fId, m0, m1, m2, m3, m4, m5, m6, m7);
|
---|
232 | return !fIsZombie;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // --------------------------------------------------------------------------
|
---|
236 | //
|
---|
237 | // Sends the given SDO through the network to this device
|
---|
238 | // An SDO message contains
|
---|
239 | // an address (this device)
|
---|
240 | // an index of the dictionary entry to address
|
---|
241 | // a subindex of this dictionary entry to access
|
---|
242 | // and a value to set for this dictionary entry
|
---|
243 | //
|
---|
244 | // The message is not send if the node has the status Zombie.
|
---|
245 | // In this case false is returned, otherwise true
|
---|
246 | //
|
---|
247 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
|
---|
248 | {
|
---|
249 | if (!fIsZombie)
|
---|
250 | fNetwork->SendSDO(fId, idx, subidx, val, store);
|
---|
251 | return !fIsZombie;
|
---|
252 | }
|
---|
253 |
|
---|
254 | // --------------------------------------------------------------------------
|
---|
255 | //
|
---|
256 | // Sends the given SDO through the network to this device
|
---|
257 | // An SDO message contains
|
---|
258 | // an address (this device)
|
---|
259 | // an index of the dictionary entry to address
|
---|
260 | // a subindex of this dictionary entry to access
|
---|
261 | // and a value to set for this dictionary entry
|
---|
262 | //
|
---|
263 | // The message is not send if the node has the status Zombie.
|
---|
264 | // In this case false is returned, otherwise true
|
---|
265 | //
|
---|
266 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
|
---|
267 | {
|
---|
268 | if (!fIsZombie)
|
---|
269 | fNetwork->SendSDO(fId, idx, subidx, val, store);
|
---|
270 | return !fIsZombie;
|
---|
271 | }
|
---|
272 |
|
---|
273 | // --------------------------------------------------------------------------
|
---|
274 | //
|
---|
275 | // Sends the given SDO through the network to this device
|
---|
276 | // An SDO message contains
|
---|
277 | // an address (this device)
|
---|
278 | // an index of the dictionary entry to address
|
---|
279 | // a subindex of this dictionary entry to access
|
---|
280 | // and a value to set for this dictionary entry
|
---|
281 | //
|
---|
282 | // The message is not send if the node has the status Zombie.
|
---|
283 | // In this case false is returned, otherwise true
|
---|
284 | //
|
---|
285 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
|
---|
286 | {
|
---|
287 | if (!fIsZombie)
|
---|
288 | fNetwork->SendSDO(fId, idx, subidx, val, store);
|
---|
289 | return !fIsZombie;
|
---|
290 | }
|
---|
291 |
|
---|
292 | // --------------------------------------------------------------------------
|
---|
293 | //
|
---|
294 | // Sends the given SDO through the network to this device
|
---|
295 | // An SDO message contains
|
---|
296 | // an address (this device)
|
---|
297 | // an index of the dictionary entry to address
|
---|
298 | // a subindex of this dictionary entry to access
|
---|
299 | // and a value to set for this dictionary entry
|
---|
300 | //
|
---|
301 | // The message is not send if the node has the status Zombie.
|
---|
302 | // In this case false is returned, otherwise true
|
---|
303 | //
|
---|
304 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t val)
|
---|
305 | {
|
---|
306 | if (!fIsZombie)
|
---|
307 | fNetwork->SendSDO(fId, idx, val, true);
|
---|
308 | return !fIsZombie;
|
---|
309 | }
|
---|
310 |
|
---|
311 | // --------------------------------------------------------------------------
|
---|
312 | //
|
---|
313 | // Sends the given SDO through the network to this device
|
---|
314 | // An SDO message contains
|
---|
315 | // an address (this device)
|
---|
316 | // an index of the dictionary entry to address
|
---|
317 | // a subindex of this dictionary entry to access
|
---|
318 | // and a value to set for this dictionary entry
|
---|
319 | //
|
---|
320 | // The message is not send if the node has the status Zombie.
|
---|
321 | // In this case false is returned, otherwise true
|
---|
322 | //
|
---|
323 | bool NodeDrv::SendSDO(WORD_t idx, WORD_t val)
|
---|
324 | {
|
---|
325 | if (!fIsZombie)
|
---|
326 | fNetwork->SendSDO(fId, idx, val, true);
|
---|
327 | return !fIsZombie;
|
---|
328 | }
|
---|
329 |
|
---|
330 | // --------------------------------------------------------------------------
|
---|
331 | //
|
---|
332 | // Sends the given SDO through the network to this device
|
---|
333 | // An SDO message contains
|
---|
334 | // an address (this device)
|
---|
335 | // an index of the dictionary entry to address
|
---|
336 | // a subindex of this dictionary entry to access
|
---|
337 | // and a value to set for this dictionary entry
|
---|
338 | //
|
---|
339 | // The message is not send if the node has the status Zombie.
|
---|
340 | // In this case false is returned, otherwise true
|
---|
341 | //
|
---|
342 | bool NodeDrv::SendSDO(WORD_t idx, LWORD_t val)
|
---|
343 | {
|
---|
344 | if (!fIsZombie)
|
---|
345 | fNetwork->SendSDO(fId, idx, val, true);
|
---|
346 | return !fIsZombie;
|
---|
347 | }
|
---|
348 |
|
---|
349 | // --------------------------------------------------------------------------
|
---|
350 | //
|
---|
351 | // Request a SDO for a given idx/subidx
|
---|
352 | // An SDO message contains
|
---|
353 | // an address (this device)
|
---|
354 | // an index of the dictionary entry to read
|
---|
355 | // a subindex of this dictionary entry to access
|
---|
356 | //
|
---|
357 | // The message is not send if the node has the status Zombie.
|
---|
358 | // In this case false is returned, otherwise true
|
---|
359 | //
|
---|
360 | bool NodeDrv::RequestSDO(WORD_t idx, BYTE_t subidx)
|
---|
361 | {
|
---|
362 | if (!fIsZombie)
|
---|
363 | fNetwork->RequestSDO(fId, idx, subidx);
|
---|
364 | return !fIsZombie;
|
---|
365 | }
|
---|
366 |
|
---|
367 | // --------------------------------------------------------------------------
|
---|
368 | //
|
---|
369 | // Send an NMT message (command) to this device
|
---|
370 | //
|
---|
371 | // The message is not send if the node has the status Zombie.
|
---|
372 | // In this case false is returned, otherwise true
|
---|
373 | //
|
---|
374 | bool NodeDrv::SendNMT(BYTE_t cmd)
|
---|
375 | {
|
---|
376 | if (!fIsZombie)
|
---|
377 | fNetwork->SendNMT(fId, cmd);
|
---|
378 | return !fIsZombie;
|
---|
379 | }
|
---|
380 |
|
---|
381 | // --------------------------------------------------------------------------
|
---|
382 | //
|
---|
383 | // Enable passthrough for the given functioncode of this device
|
---|
384 | //
|
---|
385 | void NodeDrv::EnableCanMsg(BYTE_t fcode)
|
---|
386 | {
|
---|
387 | fNetwork->EnableCanMsg(fId, fcode, TRUE);
|
---|
388 | }
|
---|
389 |
|
---|
390 | // --------------------------------------------------------------------------
|
---|
391 | //
|
---|
392 | // Wait a given timeout until the SDO with the given idx/subidx from
|
---|
393 | // this device has been received.
|
---|
394 | // You can stop waiting by StopWaitingForSDO.
|
---|
395 | // Return false if waiting timed out.
|
---|
396 | // If waiting timed out the node is set to status Zombie.
|
---|
397 | //
|
---|
398 | // If the node is already a zombie node, the message is deleted from the
|
---|
399 | // queue and no waiting is done, false is returned..
|
---|
400 | //
|
---|
401 | bool NodeDrv::WaitForSdo(WORD_t idx, BYTE_t subidx, WORDS_t timeout)
|
---|
402 | {
|
---|
403 | bool rc = fNetwork->WaitForSdo(fId, idx, subidx, fIsZombie?-1:timeout);
|
---|
404 |
|
---|
405 | if (!rc)
|
---|
406 | fIsZombie = kTRUE;
|
---|
407 |
|
---|
408 | return fIsZombie ? false : rc;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /*
|
---|
412 | void NodeDrv::WaitForSdos()
|
---|
413 | {
|
---|
414 | while (fNetwork->WaitingForSdo(fId))
|
---|
415 | usleep(1);
|
---|
416 | }
|
---|
417 | */
|
---|
418 |
|
---|
419 | // --------------------------------------------------------------------------
|
---|
420 | //
|
---|
421 | // Waits until the next Pdo1 from this device has been received
|
---|
422 | //
|
---|
423 | void NodeDrv::WaitForNextPdo1()
|
---|
424 | {
|
---|
425 | fNetwork->WaitForNextPdo1(fId);
|
---|
426 | }
|
---|
427 |
|
---|
428 | // --------------------------------------------------------------------------
|
---|
429 | //
|
---|
430 | // Waits until the next Pdo2 from this device has been received
|
---|
431 | //
|
---|
432 | void NodeDrv::WaitForNextPdo2()
|
---|
433 | {
|
---|
434 | fNetwork->WaitForNextPdo2(fId);
|
---|
435 | }
|
---|
436 |
|
---|
437 | // --------------------------------------------------------------------------
|
---|
438 | //
|
---|
439 | // Waits until the next Pdo3 from this device has been received
|
---|
440 | //
|
---|
441 | void NodeDrv::WaitForNextPdo3()
|
---|
442 | {
|
---|
443 | fNetwork->WaitForNextPdo3(fId);
|
---|
444 | }
|
---|
445 |
|
---|
446 | // --------------------------------------------------------------------------
|
---|
447 | //
|
---|
448 | // Waits until the next Pdo4 from this device has been received
|
---|
449 | //
|
---|
450 | void NodeDrv::WaitForNextPdo4()
|
---|
451 | {
|
---|
452 | fNetwork->WaitForNextPdo4(fId);
|
---|
453 | }
|
---|
454 |
|
---|