src/lifegrd.c
author etisserant
Sun, 15 Apr 2007 00:46:40 +0200
changeset 160 636d875c85dd
parent 149 fe50ada8020b
child 177 d485a3fc5739
permissions -rw-r--r--
Removed genrated dead prototypes for depricated callbacks.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
#include <data.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
#include "lifegrd.h"
149
fe50ada8020b Changes in the API:
etisserant
parents: 71
diff changeset
    25
#include "canfestival.h"
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    27
/* Prototypes for internals functions */
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    28
void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id);
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    29
void ProducerHearbeatAlarm(CO_Data* d, UNS32 id);
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    30
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    31
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    32
/*****************************************************************************/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
e_nodeState getNodeState (CO_Data* d, UNS8 nodeId)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
	e_nodeState networkNodeState = d->NMTable[nodeId];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
	return networkNodeState;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    39
/*****************************************************************************/
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    40
/* The Consumer Timer Callback */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
{
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    43
        /*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
	
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    45
	/* call heartbeat error with NodeId */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
	(*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 ));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
    49
/*****************************************************************************/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
void proceedNODE_GUARD(CO_Data* d, Message* m )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
  UNS8 nodeId = (UNS8) GET_NODE_ID((*m));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
  if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    55
  { /* Receiving a NMT NodeGuarding (request of the state by the master) */
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    56
    /*  only answer to the NMT NodeGuarding request, the master is not checked (not implemented) */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
    if (nodeId == *d->bDeviceNodeId )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
      Message msg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
      msg.cob_id.w = *d->bDeviceNodeId + 0x700;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
      msg.len = (UNS8)0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
      msg.rtr = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
      msg.data[0] = d->nodeState; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
      if (d->toggle)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
      {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
        msg.data[0] |= 0x80 ;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
        d->toggle = 0 ;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
      else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
        d->toggle = 1 ; 
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    71
      /* send the nodeguard response. */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
      MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState);
149
fe50ada8020b Changes in the API:
etisserant
parents: 71
diff changeset
    73
      canSend(d->canHandle,&msg );
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
    }  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    76
  }else{ /* Not a request CAN */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
    MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
    /* the slave's state receievd is stored in the NMTable */
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    80
      /* The state is stored on 7 bit */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
    d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
    /* Boot-Up frame reception */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
    if ( d->NMTable[nodeId] == Initialisation)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
      {
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    86
        /* The device send the boot-up message (Initialisation) */
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    87
        /* to indicate the master that it is entered in pre_operational mode */
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    88
        /* Because the  device enter automaticaly in pre_operational mode, */
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    89
        /* the pre_operational mode is stored */
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
    90
/*        NMTable[bus_id][nodeId] = Pre_operational; */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
        MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
      
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
    if( d->NMTable[nodeId] != Unknown_state ) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
        UNS8 index, ConsummerHeartBeat_nodeId ;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
        for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
        {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
            ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
            if ( nodeId == ConsummerHeartBeat_nodeId )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
            {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
                TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   102
            	/* Renew alarm for next heartbeat. */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
            	DelAlarm(d->ConsumerHeartBeatTimers[index]);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
            	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
            }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
        }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
   111
/*****************************************************************************/
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   112
/* The Consumer Timer Callback */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
void ProducerHearbeatAlarm(CO_Data* d, UNS32 id)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
	if(*d->ProducerHeartBeatTime)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
		Message msg;
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   118
		/* Time expired, the heartbeat must be sent immediately
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   119
		 * generate the correct node-id: this is done by the offset 1792
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   120
		 * (decimal) and additionaly
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   121
		 * the node-id of this device.
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   122
		 */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
		msg.cob_id.w = *d->bDeviceNodeId + 0x700;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
		msg.len = (UNS8)0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
		msg.rtr = 0;
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   126
		msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   127
		/* send the heartbeat */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
      		MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState);
149
fe50ada8020b Changes in the API:
etisserant
parents: 71
diff changeset
   129
      		canSend(d->canHandle,&msg );
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
	}else{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
		d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
   135
/*****************************************************************************/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
void heartbeatInit(CO_Data* d)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
{
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   138
    UNS8 index; /* Index to scan the table of heartbeat consumers */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
    d->toggle = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
    for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
        TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
71
95cd3376cc9f compilator compatitibility
frdupin
parents: 53
diff changeset
   145
        /* MSG_WAR(0x3121, "should_time : ", should_time ) ; */
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
        if ( time )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
        {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
            	d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
        }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
    if ( *d->ProducerHeartBeatTime )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
    	TIMEVAL time = *d->ProducerHeartBeatTime;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
    	d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHearbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
53
73bc47a0db55 Add prototypes
frdupin
parents: 0
diff changeset
   159
/*****************************************************************************/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
void heartbeatStop(CO_Data* d)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
    UNS8 index;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
    for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   165
        d->ConsumerHeartBeatTimers[index + 1] = DelAlarm(d->ConsumerHeartBeatTimers[index + 1]);;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
    d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   169
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   170
149
fe50ada8020b Changes in the API:
etisserant
parents: 71
diff changeset
   171
void _heartbeatError(UNS8 heartbeatID){}