| 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, timeval_t *tv) | 
|---|
| 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 <TTimer.h> | 
|---|
| 51 |  | 
|---|
| 52 | #include "MTime.h" | 
|---|
| 53 | #include "network.h" | 
|---|
| 54 | #include "MLogManip.h" | 
|---|
| 55 |  | 
|---|
| 56 | ClassImp(NodeDrv); | 
|---|
| 57 |  | 
|---|
| 58 | // -------------------------------------------------------------------------- | 
|---|
| 59 | // | 
|---|
| 60 | // Constructor for one node. Sets the Node Id (<32) the logging stream | 
|---|
| 61 | // and the node name. The name is a name for debug output. | 
|---|
| 62 | // | 
|---|
| 63 | NodeDrv::NodeDrv(BYTE_t nodeid, const char *name, MLog &out) : Log(out), fNetwork(NULL), fId(32), fError(0), fIsZombie(kTRUE), fGuard(NULL) | 
|---|
| 64 | { | 
|---|
| 65 | if (nodeid>0x1f) | 
|---|
| 66 | { | 
|---|
| 67 | cout << "SetNode - Error: Only node Numbers < 32 are allowed"<< endl; | 
|---|
| 68 | return; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | fId = nodeid; | 
|---|
| 72 |  | 
|---|
| 73 | if (name) | 
|---|
| 74 | fName = name; | 
|---|
| 75 | else | 
|---|
| 76 | { | 
|---|
| 77 | fName = "Node#"; | 
|---|
| 78 | fName += (int)nodeid; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | lout << "- Node #" << (int)nodeid << " (" << name << ") initialized." << endl; | 
|---|
| 82 |  | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | // -------------------------------------------------------------------------- | 
|---|
| 86 | // | 
|---|
| 87 | // destructor | 
|---|
| 88 | // | 
|---|
| 89 | NodeDrv::~NodeDrv() | 
|---|
| 90 | { | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | // -------------------------------------------------------------------------- | 
|---|
| 94 | // | 
|---|
| 95 | // This should be called from a master or main thread to get a node out | 
|---|
| 96 | // of the Zombie-Status. Overload it by your needs. | 
|---|
| 97 | // | 
|---|
| 98 | bool NodeDrv::Reboot() | 
|---|
| 99 | { | 
|---|
| 100 | fIsZombie = false; | 
|---|
| 101 |  | 
|---|
| 102 | Init(); | 
|---|
| 103 |  | 
|---|
| 104 | return !fIsZombie; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | // -------------------------------------------------------------------------- | 
|---|
| 108 | // | 
|---|
| 109 | // Init device, sets the pointer to the whole network and enables | 
|---|
| 110 | // the Can messages to be passed through the interface: | 
|---|
| 111 | //   PDO1 tx | 
|---|
| 112 | //   PDO2 tx | 
|---|
| 113 | //   PDO3 tx | 
|---|
| 114 | //   PDO4 tx | 
|---|
| 115 | //   SDO rx | 
|---|
| 116 | //   SDO tx | 
|---|
| 117 | // | 
|---|
| 118 | bool NodeDrv::InitDevice(Network *net) | 
|---|
| 119 | { | 
|---|
| 120 | fNetwork = net; | 
|---|
| 121 |  | 
|---|
| 122 | EnableCanMsg(kPDO1_TX); | 
|---|
| 123 | EnableCanMsg(kPDO2_TX); | 
|---|
| 124 | EnableCanMsg(kPDO3_TX); | 
|---|
| 125 | EnableCanMsg(kPDO4_TX); | 
|---|
| 126 | EnableCanMsg(kSDO_RX); | 
|---|
| 127 | EnableCanMsg(kSDO_TX); | 
|---|
| 128 | EnableCanMsg(kNodeguard); | 
|---|
| 129 | EnableCanMsg(kEMERGENCY); | 
|---|
| 130 |  | 
|---|
| 131 | fIsZombie = kFALSE; | 
|---|
| 132 |  | 
|---|
| 133 | Init(); | 
|---|
| 134 |  | 
|---|
| 135 | return !fIsZombie; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | // -------------------------------------------------------------------------- | 
|---|
| 139 | // | 
|---|
| 140 | // Print an "SDO idx/subidx set." from this device message. | 
|---|
| 141 | // This output is never redirected to the GUI. | 
|---|
| 142 | // In standard CANOpen operation data is meaningless (we are using | 
|---|
| 143 | // it in the 'non-standard' CANOpen communication with the MACS) | 
|---|
| 144 | // | 
|---|
| 145 | void NodeDrv::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv) | 
|---|
| 146 | { | 
|---|
| 147 | const Bool_t gui = lout.IsOutputDeviceEnabled(MLog::eGui); | 
|---|
| 148 |  | 
|---|
| 149 | if (gui) | 
|---|
| 150 | lout << ddev(MLog::eGui); | 
|---|
| 151 |  | 
|---|
| 152 | lout << hex << setfill('0'); | 
|---|
| 153 | lout << "Node #" << dec << (int)fId << ": Sdo=" << hex << idx  << "/" << (int)subidx << " set."; | 
|---|
| 154 | lout << endl; | 
|---|
| 155 |  | 
|---|
| 156 | if (gui) | 
|---|
| 157 | lout << edev(MLog::eGui); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | // -------------------------------------------------------------------------- | 
|---|
| 161 | // | 
|---|
| 162 | // Print an error message with the corresponding data from this device. | 
|---|
| 163 | // | 
|---|
| 164 | void NodeDrv::HandleSDOError(LWORD_t data) | 
|---|
| 165 | { | 
|---|
| 166 | lout << "Nodedrv: SDO Error: Entry not found in dictionary (data=0x"; | 
|---|
| 167 | lout << hex << setfill('0') << setw(4) << data << ")"; | 
|---|
| 168 | lout << endl; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | // -------------------------------------------------------------------------- | 
|---|
| 172 | // | 
|---|
| 173 | // Prints the received SDo from this device | 
|---|
| 174 | // | 
|---|
| 175 | void NodeDrv::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv) | 
|---|
| 176 | { | 
|---|
| 177 | cout << "SdoRx: Idx=0x"<< hex << idx << "/" << (int)subidx; | 
|---|
| 178 | cout << ", val=0x" << val << endl; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | // -------------------------------------------------------------------------- | 
|---|
| 182 | // | 
|---|
| 183 | // Sends the given PDO1 through the network to this device | 
|---|
| 184 | // A PDO is carrying up to eight bytes of information. | 
|---|
| 185 | // | 
|---|
| 186 | // The message is not send if the node has the status Zombie. | 
|---|
| 187 | // In this case false is returned, otherwise true | 
|---|
| 188 | // | 
|---|
| 189 | bool NodeDrv::SendPDO1(BYTE_t data[8]) | 
|---|
| 190 | { | 
|---|
| 191 | if (!fIsZombie) | 
|---|
| 192 | fNetwork->SendPDO1(fId, data); | 
|---|
| 193 | return !fIsZombie; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | // -------------------------------------------------------------------------- | 
|---|
| 197 | // | 
|---|
| 198 | // Sends the given PDO2 through the network to this device | 
|---|
| 199 | // A PDO is carrying up to eight bytes of information. | 
|---|
| 200 | // | 
|---|
| 201 | // The message is not send if the node has the status Zombie. | 
|---|
| 202 | // In this case false is returned, otherwise true | 
|---|
| 203 | // | 
|---|
| 204 | bool NodeDrv::SendPDO2(BYTE_t data[8]) | 
|---|
| 205 | { | 
|---|
| 206 | if (!fIsZombie) | 
|---|
| 207 | fNetwork->SendPDO2(fId, data); | 
|---|
| 208 | return !fIsZombie; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | // -------------------------------------------------------------------------- | 
|---|
| 212 | // | 
|---|
| 213 | // Sends the given PDO1 through the network to this device | 
|---|
| 214 | // A PDO is carrying up to eight bytes of information. | 
|---|
| 215 | // | 
|---|
| 216 | // The message is not send if the node has the status Zombie. | 
|---|
| 217 | // In this case false is returned, otherwise true | 
|---|
| 218 | // | 
|---|
| 219 | bool NodeDrv::SendPDO1(BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0, | 
|---|
| 220 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0) | 
|---|
| 221 | { | 
|---|
| 222 | if (!fIsZombie) | 
|---|
| 223 | fNetwork->SendPDO1(fId, m0, m1, m2, m3, m4, m5, m6, m7); | 
|---|
| 224 | return !fIsZombie; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | // -------------------------------------------------------------------------- | 
|---|
| 228 | // | 
|---|
| 229 | // Sends the given PDO2 through the network to this device | 
|---|
| 230 | // A PDO is carrying up to eight bytes of information. | 
|---|
| 231 | // | 
|---|
| 232 | // The message is not send if the node has the status Zombie. | 
|---|
| 233 | // In this case false is returned, otherwise true | 
|---|
| 234 | // | 
|---|
| 235 | bool NodeDrv::SendPDO2(BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0, | 
|---|
| 236 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0) | 
|---|
| 237 | { | 
|---|
| 238 | if (!fIsZombie) | 
|---|
| 239 | fNetwork->SendPDO2(fId, m0, m1, m2, m3, m4, m5, m6, m7); | 
|---|
| 240 | return !fIsZombie; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | // -------------------------------------------------------------------------- | 
|---|
| 244 | // | 
|---|
| 245 | // Sends the given SDO through the network to this device | 
|---|
| 246 | // An SDO message contains | 
|---|
| 247 | //  an address (this device) | 
|---|
| 248 | //  an index of the dictionary entry to address | 
|---|
| 249 | //  a subindex of this dictionary entry to access | 
|---|
| 250 | //  and a value to set for this dictionary entry | 
|---|
| 251 | // | 
|---|
| 252 | // The message is not send if the node has the status Zombie. | 
|---|
| 253 | // In this case false is returned, otherwise true | 
|---|
| 254 | // | 
|---|
| 255 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, BYTE_t val, bool store) | 
|---|
| 256 | { | 
|---|
| 257 | if (!fIsZombie) | 
|---|
| 258 | fNetwork->SendSDO(fId, idx, subidx, val, store); | 
|---|
| 259 | return !fIsZombie; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | // -------------------------------------------------------------------------- | 
|---|
| 263 | // | 
|---|
| 264 | // Sends the given SDO through the network to this device | 
|---|
| 265 | // An SDO message contains | 
|---|
| 266 | //  an address (this device) | 
|---|
| 267 | //  an index of the dictionary entry to address | 
|---|
| 268 | //  a subindex of this dictionary entry to access | 
|---|
| 269 | //  and a value to set for this dictionary entry | 
|---|
| 270 | // | 
|---|
| 271 | // The message is not send if the node has the status Zombie. | 
|---|
| 272 | // In this case false is returned, otherwise true | 
|---|
| 273 | // | 
|---|
| 274 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, WORD_t val, bool store) | 
|---|
| 275 | { | 
|---|
| 276 | if (!fIsZombie) | 
|---|
| 277 | fNetwork->SendSDO(fId, idx, subidx, val, store); | 
|---|
| 278 | return !fIsZombie; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | // -------------------------------------------------------------------------- | 
|---|
| 282 | // | 
|---|
| 283 | // Sends the given SDO through the network to this device | 
|---|
| 284 | // An SDO message contains | 
|---|
| 285 | //  an address (this device) | 
|---|
| 286 | //  an index of the dictionary entry to address | 
|---|
| 287 | //  a subindex of this dictionary entry to access | 
|---|
| 288 | //  and a value to set for this dictionary entry | 
|---|
| 289 | // | 
|---|
| 290 | // The message is not send if the node has the status Zombie. | 
|---|
| 291 | // In this case false is returned, otherwise true | 
|---|
| 292 | // | 
|---|
| 293 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, bool store) | 
|---|
| 294 | { | 
|---|
| 295 | if (!fIsZombie) | 
|---|
| 296 | fNetwork->SendSDO(fId, idx, subidx, val, store); | 
|---|
| 297 | return !fIsZombie; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | // -------------------------------------------------------------------------- | 
|---|
| 301 | // | 
|---|
| 302 | // Sends the given SDO through the network to this device | 
|---|
| 303 | // An SDO message contains | 
|---|
| 304 | //  an address (this device) | 
|---|
| 305 | //  an index of the dictionary entry to address | 
|---|
| 306 | //  a subindex of this dictionary entry to access | 
|---|
| 307 | //  and a value to set for this dictionary entry | 
|---|
| 308 | // | 
|---|
| 309 | // The message is not send if the node has the status Zombie. | 
|---|
| 310 | // In this case false is returned, otherwise true | 
|---|
| 311 | // | 
|---|
| 312 | bool NodeDrv::SendSDO(WORD_t idx, BYTE_t val) | 
|---|
| 313 | { | 
|---|
| 314 | if (!fIsZombie) | 
|---|
| 315 | fNetwork->SendSDO(fId, idx, val, true); | 
|---|
| 316 | return !fIsZombie; | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | // -------------------------------------------------------------------------- | 
|---|
| 320 | // | 
|---|
| 321 | // Sends the given SDO through the network to this device | 
|---|
| 322 | // An SDO message contains | 
|---|
| 323 | //  an address (this device) | 
|---|
| 324 | //  an index of the dictionary entry to address | 
|---|
| 325 | //  a subindex of this dictionary entry to access | 
|---|
| 326 | //  and a value to set for this dictionary entry | 
|---|
| 327 | // | 
|---|
| 328 | // The message is not send if the node has the status Zombie. | 
|---|
| 329 | // In this case false is returned, otherwise true | 
|---|
| 330 | // | 
|---|
| 331 | bool NodeDrv::SendSDO(WORD_t idx, WORD_t val) | 
|---|
| 332 | { | 
|---|
| 333 | if (!fIsZombie) | 
|---|
| 334 | fNetwork->SendSDO(fId, idx, val, true); | 
|---|
| 335 | return !fIsZombie; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | // -------------------------------------------------------------------------- | 
|---|
| 339 | // | 
|---|
| 340 | // Sends the given SDO through the network to this device | 
|---|
| 341 | // An SDO message contains | 
|---|
| 342 | //  an address (this device) | 
|---|
| 343 | //  an index of the dictionary entry to address | 
|---|
| 344 | //  a subindex of this dictionary entry to access | 
|---|
| 345 | //  and a value to set for this dictionary entry | 
|---|
| 346 | // | 
|---|
| 347 | // The message is not send if the node has the status Zombie. | 
|---|
| 348 | // In this case false is returned, otherwise true | 
|---|
| 349 | // | 
|---|
| 350 | bool NodeDrv::SendSDO(WORD_t idx, LWORD_t val) | 
|---|
| 351 | { | 
|---|
| 352 | if (!fIsZombie) | 
|---|
| 353 | fNetwork->SendSDO(fId, idx, val, true); | 
|---|
| 354 | return !fIsZombie; | 
|---|
| 355 | } | 
|---|
| 356 |  | 
|---|
| 357 | // -------------------------------------------------------------------------- | 
|---|
| 358 | // | 
|---|
| 359 | // Request a SDO for a given idx/subidx | 
|---|
| 360 | // An SDO message contains | 
|---|
| 361 | //  an address (this device) | 
|---|
| 362 | //  an index of the dictionary entry to read | 
|---|
| 363 | //  a subindex of this dictionary entry to access | 
|---|
| 364 | // | 
|---|
| 365 | // The message is not send if the node has the status Zombie. | 
|---|
| 366 | // In this case false is returned, otherwise true | 
|---|
| 367 | // | 
|---|
| 368 | bool NodeDrv::RequestSDO(WORD_t idx, BYTE_t subidx) | 
|---|
| 369 | { | 
|---|
| 370 | if (!fIsZombie) | 
|---|
| 371 | fNetwork->RequestSDO(fId, idx, subidx); | 
|---|
| 372 | return !fIsZombie; | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | // -------------------------------------------------------------------------- | 
|---|
| 376 | // | 
|---|
| 377 | // Send a NMT message (command) to this device | 
|---|
| 378 | // | 
|---|
| 379 | // The message is not send if the node has the status Zombie. | 
|---|
| 380 | // In this case false is returned, otherwise true | 
|---|
| 381 | // | 
|---|
| 382 | bool NodeDrv::SendNMT(BYTE_t cmd) | 
|---|
| 383 | { | 
|---|
| 384 | if (!fIsZombie) | 
|---|
| 385 | fNetwork->SendNMT(fId, cmd); | 
|---|
| 386 | return !fIsZombie; | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | // -------------------------------------------------------------------------- | 
|---|
| 390 | // | 
|---|
| 391 | // Send a Nodeguard message (command) to this device | 
|---|
| 392 | // | 
|---|
| 393 | void NodeDrv::SendNodeguard() | 
|---|
| 394 | { | 
|---|
| 395 | fNetwork->SendNodeguard(fId); | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | // -------------------------------------------------------------------------- | 
|---|
| 399 | // | 
|---|
| 400 | // Enable passthrough for the given functioncode of this device | 
|---|
| 401 | // | 
|---|
| 402 | void NodeDrv::EnableCanMsg(BYTE_t fcode) | 
|---|
| 403 | { | 
|---|
| 404 | fNetwork->EnableCanMsg(fId, fcode, TRUE); | 
|---|
| 405 | } | 
|---|
| 406 |  | 
|---|
| 407 | // -------------------------------------------------------------------------- | 
|---|
| 408 | // | 
|---|
| 409 | // Wait a given timeout until the SDO with the given idx/subidx from | 
|---|
| 410 | // this device has been received. | 
|---|
| 411 | // You can stop waiting by StopWaitingForSDO. | 
|---|
| 412 | // Return false if waiting timed out. | 
|---|
| 413 | // If waiting timed out the node is set to status Zombie. | 
|---|
| 414 | // | 
|---|
| 415 | // If the node is already a zombie node, the message is deleted from the | 
|---|
| 416 | // queue and no waiting is done, false is returned.. | 
|---|
| 417 | // | 
|---|
| 418 | bool NodeDrv::WaitForSdo(WORD_t idx, BYTE_t subidx, WORDS_t timeout, bool zombie) | 
|---|
| 419 | { | 
|---|
| 420 | bool rc = fNetwork->WaitForSdo(fId, idx, subidx, fIsZombie?-1:timeout); | 
|---|
| 421 | if (rc) | 
|---|
| 422 | return true; | 
|---|
| 423 |  | 
|---|
| 424 | lout << " + " << GetNodeName() << ": NodeDrv::WaitForSdo: 0x" << hex << idx << "/" << dec << (int)subidx << " --> ZOMBIE! " << MTime(-1) << endl; | 
|---|
| 425 | if (zombie) | 
|---|
| 426 | SetZombie(); | 
|---|
| 427 | return false; | 
|---|
| 428 | } | 
|---|
| 429 |  | 
|---|
| 430 | /* | 
|---|
| 431 | void NodeDrv::WaitForSdos() | 
|---|
| 432 | { | 
|---|
| 433 | while (fNetwork->WaitingForSdo(fId)) | 
|---|
| 434 | usleep(1); | 
|---|
| 435 | } | 
|---|
| 436 | */ | 
|---|
| 437 |  | 
|---|
| 438 | // -------------------------------------------------------------------------- | 
|---|
| 439 | // | 
|---|
| 440 | // Waits until the next Pdo1 from this device has been received | 
|---|
| 441 | // | 
|---|
| 442 | void NodeDrv::WaitForNextPdo1() | 
|---|
| 443 | { | 
|---|
| 444 | fNetwork->WaitForNextPdo1(fId); | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | // -------------------------------------------------------------------------- | 
|---|
| 448 | // | 
|---|
| 449 | // Waits until the next Pdo2 from this device has been received | 
|---|
| 450 | // | 
|---|
| 451 | void NodeDrv::WaitForNextPdo2() | 
|---|
| 452 | { | 
|---|
| 453 | fNetwork->WaitForNextPdo2(fId); | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 | // -------------------------------------------------------------------------- | 
|---|
| 457 | // | 
|---|
| 458 | // Waits until the next Pdo3 from this device has been received | 
|---|
| 459 | // | 
|---|
| 460 | void NodeDrv::WaitForNextPdo3() | 
|---|
| 461 | { | 
|---|
| 462 | fNetwork->WaitForNextPdo3(fId); | 
|---|
| 463 | } | 
|---|
| 464 |  | 
|---|
| 465 | // -------------------------------------------------------------------------- | 
|---|
| 466 | // | 
|---|
| 467 | // Waits until the next Pdo4 from this device has been received | 
|---|
| 468 | // | 
|---|
| 469 | void NodeDrv::WaitForNextPdo4() | 
|---|
| 470 | { | 
|---|
| 471 | fNetwork->WaitForNextPdo4(fId); | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | // -------------------------------------------------------------------------- | 
|---|
| 475 | // | 
|---|
| 476 | // Start the standard CANopen guarding of the device. | 
|---|
| 477 | // While ms is the guard time in millisec. This is the time between | 
|---|
| 478 | // two requests for a Nodeguard message. | 
|---|
| 479 | // ltf is the LifeTimeFactor. This means how often it is checked, that at | 
|---|
| 480 | // least one Nodeguard message was answered. | 
|---|
| 481 | // | 
|---|
| 482 | class NodeGuard : public MThread | 
|---|
| 483 | { | 
|---|
| 484 | Double_t fTimeoutTime;     //[s] | 
|---|
| 485 | Double_t fGuardTime;       //[s] | 
|---|
| 486 | Int_t    fLifeTimeFactor; | 
|---|
| 487 |  | 
|---|
| 488 | Bool_t   fIsCanOpen; | 
|---|
| 489 |  | 
|---|
| 490 | NodeDrv *fDrv; | 
|---|
| 491 |  | 
|---|
| 492 | public: | 
|---|
| 493 | NodeGuard(NodeDrv *drv, Int_t guard, Int_t ltf, Bool_t canopen) | 
|---|
| 494 | : MThread(false), fGuardTime(guard/1000.), fLifeTimeFactor(ltf), fIsCanOpen(canopen), fDrv(drv) { } | 
|---|
| 495 |  | 
|---|
| 496 | void Reset(timeval_t *tv=NULL) | 
|---|
| 497 | { | 
|---|
| 498 | MTime t; | 
|---|
| 499 | if (tv) | 
|---|
| 500 | t.Set(*tv); | 
|---|
| 501 | else | 
|---|
| 502 | t.Now(); | 
|---|
| 503 |  | 
|---|
| 504 | fTimeoutTime = t + (fGuardTime*fLifeTimeFactor); | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | void *Thread() | 
|---|
| 508 | { | 
|---|
| 509 | Reset(); | 
|---|
| 510 |  | 
|---|
| 511 | while (!HasStopFlag()) | 
|---|
| 512 | { | 
|---|
| 513 | // Sending nodeguards seems to result in | 
|---|
| 514 | // loosing CANbus messages or CANbus answers... | 
|---|
| 515 | // strange. Also protecting VmodIcan::SendCanFrame | 
|---|
| 516 | // by a Mutex doesn't help. | 
|---|
| 517 | if (fIsCanOpen) | 
|---|
| 518 | fDrv->SendNodeguard(); | 
|---|
| 519 |  | 
|---|
| 520 | MTime t; | 
|---|
| 521 | t.Now(); | 
|---|
| 522 |  | 
|---|
| 523 | const Double_t t0 = t+fGuardTime; | 
|---|
| 524 |  | 
|---|
| 525 | while (!HasStopFlag() && (double)t<t0 && (double)t<fTimeoutTime) | 
|---|
| 526 | { | 
|---|
| 527 | usleep(5); | 
|---|
| 528 | t.Now(); | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | //cout << "-d-> " << (Long_t)((fTimeoutTime-t)*1000) << endl; | 
|---|
| 532 | //cout << "-o-> " << (ULong_t)((fTimeoutTime)*1000)<< " " << (ULong_t)((t)*1000) << endl; | 
|---|
| 533 | //cout << "-g-> " << (Long_t)((t-t0)*1000)<< endl; | 
|---|
| 534 |  | 
|---|
| 535 | if ((double)t<fTimeoutTime) | 
|---|
| 536 | continue; | 
|---|
| 537 |  | 
|---|
| 538 | fDrv->SetZombie(false); | 
|---|
| 539 | return 0; | 
|---|
| 540 | } | 
|---|
| 541 | return 0; | 
|---|
| 542 | } | 
|---|
| 543 | }; | 
|---|
| 544 |  | 
|---|
| 545 | void NodeDrv::StartGuarding(Bool_t real=kTRUE) | 
|---|
| 546 | { | 
|---|
| 547 | if (fGuard) | 
|---|
| 548 | return; | 
|---|
| 549 |  | 
|---|
| 550 | fGuard = new NodeGuard(this, fGuardTime, fLifeTimeFactor, real); | 
|---|
| 551 | fGuard->Start(); | 
|---|
| 552 |  | 
|---|
| 553 | lout << "- " << GetNodeName() << ": Guarding (" << dec; | 
|---|
| 554 | lout << fLifeTimeFactor << "*" << fGuardTime << "ms) started." << endl; | 
|---|
| 555 | } | 
|---|
| 556 |  | 
|---|
| 557 | void NodeDrv::StartGuarding(Int_t ms, Int_t ltf, Bool_t real) | 
|---|
| 558 | { | 
|---|
| 559 | if (fGuard) | 
|---|
| 560 | { | 
|---|
| 561 | lout << "- " << GetNodeName() << ": ERROR - Guarding already started." << endl; | 
|---|
| 562 | return; | 
|---|
| 563 | } | 
|---|
| 564 |  | 
|---|
| 565 | fGuardTime      = ms; | 
|---|
| 566 | fLifeTimeFactor = ltf; | 
|---|
| 567 |  | 
|---|
| 568 | StartGuarding(real); | 
|---|
| 569 | } | 
|---|
| 570 |  | 
|---|
| 571 | void NodeDrv::StopGuarding() | 
|---|
| 572 | { | 
|---|
| 573 | if (!fGuard) | 
|---|
| 574 | return; | 
|---|
| 575 |  | 
|---|
| 576 | delete fGuard; | 
|---|
| 577 | fGuard=NULL; | 
|---|
| 578 |  | 
|---|
| 579 | lout << "- " << GetNodeName() << ": Guarding stopped." << endl; | 
|---|
| 580 | } | 
|---|
| 581 |  | 
|---|
| 582 | // -------------------------------------------------------------------------- | 
|---|
| 583 | // | 
|---|
| 584 | // Set the timeout timer to the time the event was received plus the | 
|---|
| 585 | // guard time times lifetimefactor. | 
|---|
| 586 | // | 
|---|
| 587 | void NodeDrv::HandleNodeguard(timeval_t *tv) | 
|---|
| 588 | { | 
|---|
| 589 | if (fGuard) | 
|---|
| 590 | fGuard->Reset(tv); | 
|---|
| 591 | } | 
|---|
| 592 |  | 
|---|
| 593 | void NodeDrv::SetZombie(bool stopguard) | 
|---|
| 594 | { | 
|---|
| 595 | fIsZombie = true; | 
|---|
| 596 | if (stopguard) | 
|---|
| 597 | StopGuarding(); | 
|---|
| 598 | else | 
|---|
| 599 | lout << " - " << GetNodeName() << ": Zombie set due to timeout." << endl; | 
|---|
| 600 | } | 
|---|