etisserant@0: /* etisserant@0: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: etisserant@0: Copyright (C): Edouard TISSERANT and Francis DUPIN etisserant@0: etisserant@0: See COPYING file for copyrights details. etisserant@0: etisserant@0: This library is free software; you can redistribute it and/or etisserant@0: modify it under the terms of the GNU Lesser General Public etisserant@0: License as published by the Free Software Foundation; either etisserant@0: version 2.1 of the License, or (at your option) any later version. etisserant@0: etisserant@0: This library is distributed in the hope that it will be useful, etisserant@0: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: Lesser General Public License for more details. etisserant@0: etisserant@0: You should have received a copy of the GNU Lesser General Public etisserant@0: License along with this library; if not, write to the Free Software etisserant@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: */ etisserant@0: etisserant@0: #include etisserant@0: #include "lifegrd.h" etisserant@0: frdupin@71: /* Prototypes for internals functions */ frdupin@53: void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id); frdupin@53: void ProducerHearbeatAlarm(CO_Data* d, UNS32 id); frdupin@53: frdupin@53: frdupin@53: /*****************************************************************************/ etisserant@0: e_nodeState getNodeState (CO_Data* d, UNS8 nodeId) etisserant@0: { etisserant@0: e_nodeState networkNodeState = d->NMTable[nodeId]; etisserant@0: return networkNodeState; etisserant@0: } etisserant@0: frdupin@53: /*****************************************************************************/ frdupin@71: /* The Consumer Timer Callback */ etisserant@0: void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id) etisserant@0: { frdupin@71: /*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/ etisserant@0: frdupin@71: /* call heartbeat error with NodeId */ etisserant@0: (*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 )); etisserant@0: } etisserant@0: frdupin@53: /*****************************************************************************/ etisserant@0: void proceedNODE_GUARD(CO_Data* d, Message* m ) etisserant@0: { etisserant@0: UNS8 nodeId = (UNS8) GET_NODE_ID((*m)); etisserant@0: etisserant@0: if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */ frdupin@71: { /* Receiving a NMT NodeGuarding (request of the state by the master) */ frdupin@71: /* only answer to the NMT NodeGuarding request, the master is not checked (not implemented) */ etisserant@0: if (nodeId == *d->bDeviceNodeId ) etisserant@0: { etisserant@0: Message msg; etisserant@0: msg.cob_id.w = *d->bDeviceNodeId + 0x700; etisserant@0: msg.len = (UNS8)0x01; etisserant@0: msg.rtr = 0; etisserant@0: msg.data[0] = d->nodeState; etisserant@0: if (d->toggle) etisserant@0: { etisserant@0: msg.data[0] |= 0x80 ; etisserant@0: d->toggle = 0 ; etisserant@0: } etisserant@0: else etisserant@0: d->toggle = 1 ; frdupin@71: /* send the nodeguard response. */ etisserant@0: MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState); etisserant@0: (*d->canSend)(&msg ); etisserant@0: } etisserant@0: frdupin@71: }else{ /* Not a request CAN */ etisserant@0: etisserant@0: MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId); etisserant@0: /* the slave's state receievd is stored in the NMTable */ frdupin@71: /* The state is stored on 7 bit */ etisserant@0: d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ; etisserant@0: etisserant@0: /* Boot-Up frame reception */ etisserant@0: if ( d->NMTable[nodeId] == Initialisation) etisserant@0: { frdupin@71: /* The device send the boot-up message (Initialisation) */ frdupin@71: /* to indicate the master that it is entered in pre_operational mode */ frdupin@71: /* Because the device enter automaticaly in pre_operational mode, */ frdupin@71: /* the pre_operational mode is stored */ frdupin@71: /* NMTable[bus_id][nodeId] = Pre_operational; */ etisserant@0: MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId); etisserant@0: } etisserant@0: etisserant@0: if( d->NMTable[nodeId] != Unknown_state ) { etisserant@0: UNS8 index, ConsummerHeartBeat_nodeId ; etisserant@0: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) etisserant@0: { etisserant@0: ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 ); etisserant@0: if ( nodeId == ConsummerHeartBeat_nodeId ) etisserant@0: { etisserant@0: TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ; frdupin@71: /* Renew alarm for next heartbeat. */ etisserant@0: DelAlarm(d->ConsumerHeartBeatTimers[index]); etisserant@0: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0); etisserant@0: } etisserant@0: } etisserant@0: } etisserant@0: } etisserant@0: } etisserant@0: frdupin@53: /*****************************************************************************/ frdupin@71: /* The Consumer Timer Callback */ etisserant@0: void ProducerHearbeatAlarm(CO_Data* d, UNS32 id) etisserant@0: { etisserant@0: if(*d->ProducerHeartBeatTime) etisserant@0: { etisserant@0: Message msg; frdupin@71: /* Time expired, the heartbeat must be sent immediately frdupin@71: * generate the correct node-id: this is done by the offset 1792 frdupin@71: * (decimal) and additionaly frdupin@71: * the node-id of this device. frdupin@71: */ etisserant@0: msg.cob_id.w = *d->bDeviceNodeId + 0x700; etisserant@0: msg.len = (UNS8)0x01; etisserant@0: msg.rtr = 0; frdupin@71: msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/ frdupin@71: /* send the heartbeat */ etisserant@0: MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState); etisserant@0: (*d->canSend)(&msg ); etisserant@0: }else{ etisserant@0: d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer); etisserant@0: } etisserant@0: } etisserant@0: frdupin@53: /*****************************************************************************/ etisserant@0: void heartbeatInit(CO_Data* d) etisserant@0: { frdupin@71: UNS8 index; /* Index to scan the table of heartbeat consumers */ etisserant@0: etisserant@0: d->toggle = 0; etisserant@0: etisserant@0: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) etisserant@0: { etisserant@0: TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ; frdupin@71: /* MSG_WAR(0x3121, "should_time : ", should_time ) ; */ etisserant@0: if ( time ) etisserant@0: { etisserant@0: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: if ( *d->ProducerHeartBeatTime ) etisserant@0: { etisserant@0: TIMEVAL time = *d->ProducerHeartBeatTime; etisserant@0: d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHearbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time)); etisserant@0: } etisserant@0: } etisserant@0: frdupin@53: /*****************************************************************************/ etisserant@0: void heartbeatStop(CO_Data* d) etisserant@0: { etisserant@0: UNS8 index; etisserant@0: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) etisserant@0: { etisserant@0: d->ConsumerHeartBeatTimers[index + 1] = DelAlarm(d->ConsumerHeartBeatTimers[index + 1]);; etisserant@0: } etisserant@0: etisserant@0: d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);; etisserant@0: } etisserant@0: