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: frdupin@53: void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id); nico@207: nico@208: frdupin@53: void ProducerHearbeatAlarm(CO_Data* d, UNS32 id); nico@207: greg@185: UNS32 OnHearbeatProducerUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex); frdupin@53: nico@207: /*! nico@207: ** nico@207: ** nico@207: ** @param d nico@207: ** @param nodeId nico@207: ** nico@207: ** @return nico@207: **/ etisserant@0: e_nodeState getNodeState (CO_Data* d, UNS8 nodeId) etisserant@0: { nico@207: e_nodeState networkNodeState = d->NMTable[nodeId]; 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 nico@207: **/ etisserant@0: void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id) etisserant@0: { 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; nico@207: /*! call heartbeat error with NodeId */ nico@207: (*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 )); nico@207: } nico@207: nico@207: /*! nico@207: ** nico@207: ** nico@207: ** @param d nico@207: ** @param m 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 */ nico@207: nico@207: MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId); nico@215: /* the slave's state receievd is stored in the NMTable */ nico@215: /* The state is stored on 7 bit */ nico@207: d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ; nico@207: nico@215: /* Boot-Up frame reception */ nico@207: if ( d->NMTable[nodeId] == Initialisation) nico@207: { 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: ** Because the device enter automaticaly in pre_operational nico@207: ** mode, nico@207: ** the pre_operational mode is stored nico@207: ** NMTable[bus_id][nodeId] = Pre_operational nico@207: */ nico@207: MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId); etisserant@343: groke6@349: if(!send_consise_dcf(d,nodeId)){ etisserant@343: /* call post SlaveBootup with NodeId */ groke6@349: (*d->post_SlaveBootup)(nodeId); groke6@349: } nico@207: } nico@207: nico@207: if( d->NMTable[nodeId] != Unknown_state ) { nico@207: UNS8 index, ConsummerHeartBeat_nodeId ; nico@207: for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ ) nico@207: { nico@207: ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 ); nico@207: if ( nodeId == ConsummerHeartBeat_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]); nico@207: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0); nico@207: } nico@207: } nico@207: } nico@207: } nico@207: } nico@207: nico@207: /*! The Consumer Timer Callback nico@207: ** nico@207: ** nico@207: ** @param d nico@207: ** @param id nico@207: **/ nico@207: void ProducerHearbeatAlarm(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: nico@207: /*! This is called when Index 0x1017 is updated. nico@207: ** nico@207: ** nico@207: ** @param d nico@207: ** @param unsused_indextable nico@207: ** @param unsused_bSubindex nico@207: ** nico@207: ** @return nico@207: **/ nico@207: UNS32 OnHeartbeatProducerUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex) nico@207: { nico@207: heartbeatStop(d); nico@207: heartbeatInit(d); nico@207: return 0; nico@207: } nico@207: nico@207: /*! nico@207: ** nico@207: ** nico@207: ** @param d 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@215: /* MSG_WAR(0x3121, "should_time : ", should_time ) ; */ nico@207: if ( time ) etisserant@0: { nico@207: d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0); etisserant@0: } etisserant@0: } nico@207: nico@207: if ( *d->ProducerHeartBeatTime ) nico@207: { nico@207: TIMEVAL time = *d->ProducerHeartBeatTime; nico@207: d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHearbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time)); nico@207: } nico@207: } nico@207: nico@207: /*! nico@207: ** nico@207: ** nico@207: ** @param d nico@207: **/ 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: { nico@207: d->ConsumerHeartBeatTimers[index + 1] = DelAlarm(d->ConsumerHeartBeatTimers[index + 1]);; nico@207: } nico@207: nico@207: d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);; nico@207: } nico@207: nico@207: /*! nico@207: ** nico@207: ** nico@207: ** @param heartbeatID nico@207: **/ etisserant@149: void _heartbeatError(UNS8 heartbeatID){} etisserant@343: void _post_SlaveBootup(UNS8 SlaveID){}