etisserant@0: /* nico@207: This file is part of CanFestival, a library implementing CanOpen nico@207: Stack. nico@207: nico@207: Copyright (C): Edouard TISSERANT and Francis DUPIN nico@207: nico@207: See COPYING file for copyrights details. nico@207: nico@207: This library is free software; you can redistribute it and/or nico@207: modify it under the terms of the GNU Lesser General Public nico@207: License as published by the Free Software Foundation; either nico@207: version 2.1 of the License, or (at your option) any later version. nico@207: nico@207: This library is distributed in the hope that it will be useful, nico@207: but WITHOUT ANY WARRANTY; without even the implied warranty of nico@207: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nico@207: Lesser General Public License for more details. nico@207: nico@207: You should have received a copy of the GNU Lesser General Public nico@207: License along with this library; if not, write to the Free Software nico@207: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 nico@207: USA nico@207: */ nico@207: nico@207: /*! nico@208: ** @file lifegrd.c nico@207: ** @author Edouard TISSERANT nico@207: ** @date Mon Jun 4 17:19:24 2007 nico@207: ** nico@207: ** @brief nico@207: ** nico@207: ** etisserant@0: */ etisserant@0: etisserant@0: #include etisserant@0: #include "lifegrd.h" etisserant@149: #include "canfestival.h" etisserant@357: #include "dcf.h" etisserant@370: #include "sysdep.h" etisserant@0: nico@208: mwildbolz@750: void ConsumerHeartbeatAlarm(CO_Data* d, UNS32 id); mwildbolz@750: void ProducerHeartbeatAlarm(CO_Data* d, UNS32 id); mwildbolz@750: UNS32 OnHearbeatProducerUpdate(CO_Data* d, const indextable * unused_indextable, UNS8 unused_bSubindex); mwildbolz@750: mwildbolz@750: void GuardTimeAlarm(CO_Data* d, UNS32 id); mwildbolz@750: UNS32 OnNodeGuardUpdate(CO_Data* d, const indextable * unused_indextable, UNS8 unused_bSubindex); mwildbolz@750: mwildbolz@750: etisserant@0: e_nodeState getNodeState (CO_Data* d, UNS8 nodeId) etisserant@0: { Mongo@668: e_nodeState networkNodeState = Unknown_state; Mongo@668: #if NMT_MAX_NODE_ID>0 Mongo@668: if(nodeId < NMT_MAX_NODE_ID) Mongo@668: networkNodeState = d->NMTable[nodeId]; Mongo@668: #endif nico@207: return networkNodeState; nico@207: } nico@207: nico@208: /*! nico@208: ** The Consumer Timer Callback nico@207: ** nico@207: ** @param d nico@207: ** @param id mwildbolz@750: * @ingroup heartbeato nico@207: **/ mwildbolz@750: void ConsumerHeartbeatAlarm(CO_Data* d, UNS32 id) etisserant@0: { Christian@635: UNS8 nodeId = (UNS8)(((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16); nico@207: /*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/ nico@207: etisserant@352: /* timer have been notified and is now free (non periodic)*/ etisserant@352: /* -> avoid deleting re-assigned timer if message is received too late*/ etisserant@352: d->ConsumerHeartBeatTimers[id]=TIMER_NONE; Christian@635: Christian@635: /* set node state */ Christian@635: d->NMTable[nodeId] = Disconnected; nico@207: /*! call heartbeat error with NodeId */ Christian@635: (*d->heartbeatError)(d, nodeId); nico@207: } nico@207: etisserant@0: void proceedNODE_GUARD(CO_Data* d, Message* m ) etisserant@0: { etisserant@0: UNS8 nodeId = (UNS8) GET_NODE_ID((*m)); nico@207: nico@207: if((m->rtr == 1) ) nico@207: /*! nico@207: ** Notice that only the master can have sent this nico@207: ** node guarding request nico@207: */ nico@207: { nico@207: /*! nico@207: ** Receiving a NMT NodeGuarding (request of the state by the nico@207: ** master) nico@207: ** Only answer to the NMT NodeGuarding request, the master is nico@207: ** not checked (not implemented) nico@207: */ nico@207: if (nodeId == *d->bDeviceNodeId ) nico@207: { nico@207: Message msg; etisserant@370: UNS16 tmp = *d->bDeviceNodeId + 0x700; etisserant@370: msg.cob_id = UNS16_LE(tmp); nico@207: msg.len = (UNS8)0x01; nico@207: msg.rtr = 0; nico@207: msg.data[0] = d->nodeState; nico@207: if (d->toggle) nico@207: { nico@207: msg.data[0] |= 0x80 ; nico@207: d->toggle = 0 ; nico@207: } nico@207: else nico@207: d->toggle = 1 ; nico@215: /* send the nodeguard response. */ nico@207: MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState); nico@207: canSend(d->canHandle,&msg ); nico@207: } nico@207: nico@215: }else{ /* Not a request CAN */ Christian@635: /* The state is stored on 7 bit */ Christian@635: e_nodeState newNodeState = (e_nodeState) ((*m).data[0] & 0x7F); nico@207: nico@207: MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId); Christian@635: mwildbolz@750: /*! mwildbolz@750: ** Record node response for node guarding service mwildbolz@750: */ mwildbolz@750: d->nodeGuardStatus[nodeId] = *d->LifeTimeFactor; mwildbolz@750: Christian@635: if (d->NMTable[nodeId] != newNodeState) Christian@635: { Christian@635: (*d->post_SlaveStateChange)(d, nodeId, newNodeState); Christian@635: /* the slave's state receievd is stored in the NMTable */ Christian@635: d->NMTable[nodeId] = newNodeState; Christian@635: } nico@207: nico@215: /* Boot-Up frame reception */ nico@207: if ( d->NMTable[nodeId] == Initialisation) Mongo@669: { nico@215: /* nico@207: ** The device send the boot-up message (Initialisation) nico@207: ** to indicate the master that it is entered in nico@207: ** pre_operational mode nico@207: */ nico@207: MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId); Mongo@669: /* call post SlaveBootup with NodeId */ Mongo@669: (*d->post_SlaveBootup)(d, nodeId); Mongo@669: } nico@207: nico@207: if( d->NMTable[nodeId] != Unknown_state ) { mwildbolz@750: UNS8 index, ConsumerHeartBeat_nodeId ; nico@207: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) nico@207: { mwildbolz@750: ConsumerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 ); mwildbolz@750: if ( nodeId == ConsumerHeartBeat_nodeId ) nico@207: { nico@207: TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ; nico@207: /* Renew alarm for next heartbeat. */ nico@207: DelAlarm(d->ConsumerHeartBeatTimers[index]); mwildbolz@750: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHeartbeatAlarm, MS_TO_TIMEVAL(time), 0); nico@207: } nico@207: } nico@207: } nico@207: } nico@207: } nico@207: mwildbolz@750: /*! The Producer Timer Callback nico@207: ** nico@207: ** nico@207: ** @param d nico@207: ** @param id mwildbolz@750: * @ingroup heartbeato nico@207: **/ mwildbolz@750: void ProducerHeartbeatAlarm(CO_Data* d, UNS32 id) nico@207: { nico@207: if(*d->ProducerHeartBeatTime) etisserant@0: { etisserant@0: Message msg; nico@215: /* Time expired, the heartbeat must be sent immediately nico@207: ** generate the correct node-id: this is done by the offset 1792 nico@207: ** (decimal) and additionaly nico@207: ** the node-id of this device. nico@207: */ etisserant@370: UNS16 tmp = *d->bDeviceNodeId + 0x700; etisserant@370: msg.cob_id = UNS16_LE(tmp); etisserant@0: msg.len = (UNS8)0x01; etisserant@0: msg.rtr = 0; nico@215: msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/ nico@215: /* send the heartbeat */ nico@207: MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState); etisserant@149: canSend(d->canHandle,&msg ); nico@207: nico@207: }else{ nico@207: d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer); nico@207: } nico@207: } nico@207: mwildbolz@750: /** mwildbolz@750: * @brief The guardTime - Timer Callback. mwildbolz@750: * mwildbolz@750: * This function is called every GuardTime (OD 0x100C) ms
mwildbolz@750: * On every call, a NodeGuard-Request is sent to all nodes which have a mwildbolz@750: * node-state not equal to "Unknown" (according to NMTable). If the node has mwildbolz@750: * not responded within the lifetime, the nodeguardError function is called and mwildbolz@750: * the status of this node is set to "Disconnected" mwildbolz@750: * mwildbolz@750: * @param d Pointer on a CAN object data structure mwildbolz@750: * @param id mwildbolz@750: * @ingroup nodeguardo mwildbolz@750: */ mwildbolz@750: void GuardTimeAlarm(CO_Data* d, UNS32 id) mwildbolz@750: { mwildbolz@750: if (*d->GuardTime) { mwildbolz@750: UNS8 i; mwildbolz@750: mwildbolz@750: MSG_WAR(0x00, "Producing nodeguard-requests: ", 0); mwildbolz@750: mwildbolz@750: for (i = 0; i < NMT_MAX_NODE_ID; i++) { mwildbolz@750: /** Send node guard request to all nodes except this node, if the mwildbolz@750: * node state is not "Unknown_state" mwildbolz@750: */ mwildbolz@750: if (d->NMTable[i] != Unknown_state && i != *d->bDeviceNodeId) { mwildbolz@750: mwildbolz@750: /** Check if the node has confirmed the guarding request within mwildbolz@750: * the LifeTime (GuardTime x LifeTimeFactor) mwildbolz@750: */ mwildbolz@750: if (d->nodeGuardStatus[i] <= 0) { mwildbolz@750: mwildbolz@750: MSG_WAR(0x00, "Node Guard alarm for nodeId : ", i); mwildbolz@750: mwildbolz@750: // Call error-callback function mwildbolz@750: if (*d->nodeguardError) { mwildbolz@750: (*d->nodeguardError)(d, i); mwildbolz@750: } mwildbolz@750: mwildbolz@750: // Mark node as disconnected mwildbolz@750: d->NMTable[i] = Disconnected; mwildbolz@750: mwildbolz@750: } mwildbolz@750: mwildbolz@750: d->nodeGuardStatus[i]--; mwildbolz@750: mwildbolz@750: masterSendNMTnodeguard(d, i); mwildbolz@750: mwildbolz@750: } mwildbolz@750: } mwildbolz@750: } else { mwildbolz@750: d->GuardTimeTimer = DelAlarm(d->GuardTimeTimer); mwildbolz@750: } mwildbolz@750: mwildbolz@750: mwildbolz@750: mwildbolz@750: } mwildbolz@750: mwildbolz@750: /** mwildbolz@750: * This function is called, if index 0x100C or 0x100D is updated to mwildbolz@750: * restart the node-guarding service with the new parameters mwildbolz@750: * mwildbolz@750: * @param d Pointer on a CAN object data structure mwildbolz@750: * @param unused_indextable mwildbolz@750: * @param unused_bSubindex mwildbolz@750: * @ingroup nodeguardo mwildbolz@750: */ mwildbolz@750: UNS32 OnNodeGuardUpdate(CO_Data* d, const indextable * unused_indextable, UNS8 unused_bSubindex) mwildbolz@750: { mwildbolz@750: nodeguardStop(d); mwildbolz@750: nodeguardInit(d); mwildbolz@750: return 0; mwildbolz@750: } mwildbolz@750: mwildbolz@750: nico@207: /*! This is called when Index 0x1017 is updated. nico@207: ** nico@207: ** nico@207: ** @param d mwildbolz@750: ** @param unused_indextable mwildbolz@750: ** @param unused_bSubindex nico@207: ** nico@207: ** @return mwildbolz@750: * @ingroup heartbeato nico@207: **/ mwildbolz@750: UNS32 OnHeartbeatProducerUpdate(CO_Data* d, const indextable * unused_indextable, UNS8 unused_bSubindex) nico@207: { nico@207: heartbeatStop(d); nico@207: heartbeatInit(d); nico@207: return 0; nico@207: } nico@207: nico@207: void heartbeatInit(CO_Data* d) nico@207: { nico@207: nico@215: UNS8 index; /* Index to scan the table of heartbeat consumers */ nico@207: RegisterSetODentryCallBack(d, 0x1017, 0x00, &OnHeartbeatProducerUpdate); nico@207: nico@207: d->toggle = 0; nico@207: nico@207: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) nico@207: { nico@207: TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ; nico@207: if ( time ) etisserant@0: { mwildbolz@750: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHeartbeatAlarm, MS_TO_TIMEVAL(time), 0); etisserant@0: } etisserant@0: } nico@207: nico@207: if ( *d->ProducerHeartBeatTime ) nico@207: { nico@207: TIMEVAL time = *d->ProducerHeartBeatTime; mwildbolz@750: d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHeartbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time)); mwildbolz@750: } mwildbolz@750: } mwildbolz@750: mwildbolz@750: mwildbolz@750: void nodeguardInit(CO_Data* d) mwildbolz@750: { mwildbolz@750: mwildbolz@750: RegisterSetODentryCallBack(d, 0x100C, 0x00, &OnNodeGuardUpdate); mwildbolz@750: RegisterSetODentryCallBack(d, 0x100D, 0x00, &OnNodeGuardUpdate); mwildbolz@750: mwildbolz@750: if (*d->GuardTime && *d->LifeTimeFactor) { mwildbolz@750: UNS8 i; mwildbolz@750: mwildbolz@750: TIMEVAL time = *d->GuardTime; mwildbolz@750: d->GuardTimeTimer = SetAlarm(d, 0, &GuardTimeAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time)); mwildbolz@750: MSG_WAR(0x0, "GuardTime: ", time); mwildbolz@750: mwildbolz@750: for (i = 0; i < NMT_MAX_NODE_ID; i++) { mwildbolz@750: /** Set initial value for the nodes */ mwildbolz@750: if (d->NMTable[i] != Unknown_state && i != *d->bDeviceNodeId) { mwildbolz@750: d->nodeGuardStatus[i] = *d->LifeTimeFactor; mwildbolz@750: } mwildbolz@750: } mwildbolz@750: mwildbolz@750: MSG_WAR(0x0, "Timer for node-guarding startet", 0); mwildbolz@750: } mwildbolz@750: mwildbolz@750: } mwildbolz@750: etisserant@0: void heartbeatStop(CO_Data* d) etisserant@0: { nico@207: UNS8 index; nico@207: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) nico@207: { etisserant@423: d->ConsumerHeartBeatTimers[index] = DelAlarm(d->ConsumerHeartBeatTimers[index]); etisserant@423: } etisserant@423: etisserant@423: d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer); nico@207: } nico@207: mwildbolz@750: void nodeguardStop(CO_Data* d) mwildbolz@750: { mwildbolz@750: d->GuardTimeTimer = DelAlarm(d->GuardTimeTimer); mwildbolz@750: } mwildbolz@750: mwildbolz@750: mwildbolz@750: void lifeGuardInit(CO_Data* d) mwildbolz@750: { mwildbolz@750: heartbeatInit(d); mwildbolz@750: nodeguardInit(d); mwildbolz@750: } mwildbolz@750: mwildbolz@750: mwildbolz@750: void lifeGuardStop(CO_Data* d) mwildbolz@750: { mwildbolz@750: heartbeatStop(d); mwildbolz@750: nodeguardStop(d); mwildbolz@750: } mwildbolz@750: mwildbolz@750: etisserant@378: void _heartbeatError(CO_Data* d, UNS8 heartbeatID){} etisserant@378: void _post_SlaveBootup(CO_Data* d, UNS8 SlaveID){} Christian@635: void _post_SlaveStateChange(CO_Data* d, UNS8 nodeId, e_nodeState newNodeState){} mwildbolz@750: void _nodeguardError(CO_Data* d, UNS8 id){} mwildbolz@750: