nico@207: nico@207: nico@207: CanFestival: /home/epimerde/documents/tc11/CanFestival-3/src/lifegrd.c Source File nico@207: nico@207: nico@207: nico@207: nico@207:
nico@207:
nico@207:
nico@207:
nico@207:

/home/epimerde/documents/tc11/CanFestival-3/src/lifegrd.c

Go to the documentation of this file.
00001 /*
nico@207: 00002 This file is part of CanFestival, a library implementing CanOpen Stack. 
nico@207: 00003 
nico@207: 00004 Copyright (C): Edouard TISSERANT and Francis DUPIN
nico@207: 00005 
nico@207: 00006 See COPYING file for copyrights details.
nico@207: 00007 
nico@207: 00008 This library is free software; you can redistribute it and/or
nico@207: 00009 modify it under the terms of the GNU Lesser General Public
nico@207: 00010 License as published by the Free Software Foundation; either
nico@207: 00011 version 2.1 of the License, or (at your option) any later version.
nico@207: 00012 
nico@207: 00013 This library is distributed in the hope that it will be useful,
nico@207: 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
nico@207: 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nico@207: 00016 Lesser General Public License for more details.
nico@207: 00017 
nico@207: 00018 You should have received a copy of the GNU Lesser General Public
nico@207: 00019 License along with this library; if not, write to the Free Software
nico@207: 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
nico@207: 00021 */
nico@207: 00022 
nico@207: 00023 #include <data.h>
nico@207: 00024 #include "lifegrd.h"
nico@207: 00025 #include "canfestival.h"
nico@207: 00026 
nico@207: 00027 /* Prototypes for internals functions */
nico@207: 00028 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id);
nico@207: 00029 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id);
nico@207: 00030 UNS32 OnHearbeatProducerUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex);
nico@207: 00031 
nico@207: 00032 /*****************************************************************************/
nico@207: 00033 e_nodeState getNodeState (CO_Data* d, UNS8 nodeId)
nico@207: 00034 {
nico@207: 00035         e_nodeState networkNodeState = d->NMTable[nodeId];
nico@207: 00036         return networkNodeState;
nico@207: 00037 }
nico@207: 00038 
nico@207: 00039 /*****************************************************************************/
nico@207: 00040 /* The Consumer Timer Callback */
nico@207: 00041 void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id)
nico@207: 00042 {
nico@207: 00043         /*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/
nico@207: 00044         
nico@207: 00045         /* call heartbeat error with NodeId */
nico@207: 00046         (*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 ));
nico@207: 00047 }
nico@207: 00048 
nico@207: 00049 /*****************************************************************************/
nico@207: 00050 void proceedNODE_GUARD(CO_Data* d, Message* m )
nico@207: 00051 {
nico@207: 00052   UNS8 nodeId = (UNS8) GET_NODE_ID((*m));
nico@207: 00053   
nico@207: 00054   if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */
nico@207: 00055   { /* Receiving a NMT NodeGuarding (request of the state by the master) */
nico@207: 00056     /*  only answer to the NMT NodeGuarding request, the master is not checked (not implemented) */
nico@207: 00057     if (nodeId == *d->bDeviceNodeId )
nico@207: 00058     {
nico@207: 00059       Message msg;
nico@207: 00060       msg.cob_id.w = *d->bDeviceNodeId + 0x700;
nico@207: 00061       msg.len = (UNS8)0x01;
nico@207: 00062       msg.rtr = 0;
nico@207: 00063       msg.data[0] = d->nodeState; 
nico@207: 00064       if (d->toggle)
nico@207: 00065       {
nico@207: 00066         msg.data[0] |= 0x80 ;
nico@207: 00067         d->toggle = 0 ;
nico@207: 00068       }
nico@207: 00069       else
nico@207: 00070         d->toggle = 1 ; 
nico@207: 00071       /* send the nodeguard response. */
nico@207: 00072       MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState);
nico@207: 00073       canSend(d->canHandle,&msg );
nico@207: 00074     }  
nico@207: 00075 
nico@207: 00076   }else{ /* Not a request CAN */
nico@207: 00077 
nico@207: 00078     MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId);
nico@207: 00079     /* the slave's state receievd is stored in the NMTable */
nico@207: 00080       /* The state is stored on 7 bit */
nico@207: 00081     d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ;
nico@207: 00082     
nico@207: 00083     /* Boot-Up frame reception */
nico@207: 00084     if ( d->NMTable[nodeId] == Initialisation)
nico@207: 00085       {
nico@207: 00086         /* The device send the boot-up message (Initialisation) */
nico@207: 00087         /* to indicate the master that it is entered in pre_operational mode */
nico@207: 00088         /* Because the  device enter automaticaly in pre_operational mode, */
nico@207: 00089         /* the pre_operational mode is stored */
nico@207: 00090 /*        NMTable[bus_id][nodeId] = Pre_operational; */
nico@207: 00091         MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId);
nico@207: 00092       }
nico@207: 00093       
nico@207: 00094     if( d->NMTable[nodeId] != Unknown_state ) {
nico@207: 00095         UNS8 index, ConsummerHeartBeat_nodeId ;
nico@207: 00096         for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
nico@207: 00097         {
nico@207: 00098             ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 );
nico@207: 00099             if ( nodeId == ConsummerHeartBeat_nodeId )
nico@207: 00100             {
nico@207: 00101                 TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
nico@207: 00102                 /* Renew alarm for next heartbeat. */
nico@207: 00103                 DelAlarm(d->ConsumerHeartBeatTimers[index]);
nico@207: 00104                 d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
nico@207: 00105             }
nico@207: 00106         }
nico@207: 00107     }
nico@207: 00108   }
nico@207: 00109 }
nico@207: 00110 
nico@207: 00111 /*****************************************************************************/
nico@207: 00112 /* The Consumer Timer Callback */
nico@207: 00113 void ProducerHearbeatAlarm(CO_Data* d, UNS32 id)
nico@207: 00114 {
nico@207: 00115         if(*d->ProducerHeartBeatTime)
nico@207: 00116         {
nico@207: 00117                 Message msg;
nico@207: 00118                 /* Time expired, the heartbeat must be sent immediately
nico@207: 00119                  * generate the correct node-id: this is done by the offset 1792
nico@207: 00120                  * (decimal) and additionaly
nico@207: 00121                  * the node-id of this device.
nico@207: 00122                  */
nico@207: 00123                 
nico@207: 00124                 msg.cob_id.w = *d->bDeviceNodeId + 0x700;
nico@207: 00125                 msg.len = (UNS8)0x01;
nico@207: 00126                 msg.rtr = 0;
nico@207: 00127                 msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/
nico@207: 00128                 /* send the heartbeat */
nico@207: 00129                 MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState);
nico@207: 00130                 canSend(d->canHandle,&msg );
nico@207: 00131         
nico@207: 00132         }else{
nico@207: 00133                 d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);
nico@207: 00134         }
nico@207: 00135 }
nico@207: 00136 
nico@207: 00137 /*****************************************************************************/
nico@207: 00138 /* This is called when Index 0x1017 is updated.*/
nico@207: 00139 UNS32 OnHeartbeatProducerUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
nico@207: 00140 {
nico@207: 00141         heartbeatStop(d);
nico@207: 00142         heartbeatInit(d);
nico@207: 00143         return 0;
nico@207: 00144 }
nico@207: 00145 /*****************************************************************************/
nico@207: 00146 
nico@207: 00147 void heartbeatInit(CO_Data* d)
nico@207: 00148 {
nico@207: 00149                 
nico@207: 00150     UNS8 index; /* Index to scan the table of heartbeat consumers */
nico@207: 00151         RegisterSetODentryCallBack(d, 0x1017, 0x00, &OnHeartbeatProducerUpdate);
nico@207: 00152     
nico@207: 00153     d->toggle = 0;
nico@207: 00154         
nico@207: 00155     for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
nico@207: 00156     {
nico@207: 00157         TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
nico@207: 00158         /* MSG_WAR(0x3121, "should_time : ", should_time ) ; */
nico@207: 00159         if ( time )
nico@207: 00160         {
nico@207: 00161                 d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
nico@207: 00162         }
nico@207: 00163     }
nico@207: 00164 
nico@207: 00165     if ( *d->ProducerHeartBeatTime )
nico@207: 00166     {
nico@207: 00167         TIMEVAL time = *d->ProducerHeartBeatTime;
nico@207: 00168         d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHearbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time));
nico@207: 00169     }
nico@207: 00170 }
nico@207: 00171 
nico@207: 00172 /*****************************************************************************/
nico@207: 00173 void heartbeatStop(CO_Data* d)
nico@207: 00174 {
nico@207: 00175     UNS8 index;
nico@207: 00176     for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
nico@207: 00177     {
nico@207: 00178         d->ConsumerHeartBeatTimers[index + 1] = DelAlarm(d->ConsumerHeartBeatTimers[index + 1]);;
nico@207: 00179     }
nico@207: 00180 
nico@207: 00181     d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);;
nico@207: 00182 }
nico@207: 00183 
nico@207: 00184 void _heartbeatError(UNS8 heartbeatID){}
nico@207: 

Generated on Mon Jun 4 17:09:26 2007 for CanFestival by  nico@207: nico@207: doxygen 1.5.1
nico@207: nico@207: