1*10491SRishi.Srivatsavai@Sun.COM /************************************************************************
2*10491SRishi.Srivatsavai@Sun.COM * RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
3*10491SRishi.Srivatsavai@Sun.COM * Copyright (C) 2001-2003 Optical Access
4*10491SRishi.Srivatsavai@Sun.COM * Author: Alex Rozin
5*10491SRishi.Srivatsavai@Sun.COM *
6*10491SRishi.Srivatsavai@Sun.COM * This file is part of RSTP library.
7*10491SRishi.Srivatsavai@Sun.COM *
8*10491SRishi.Srivatsavai@Sun.COM * RSTP library is free software; you can redistribute it and/or modify it
9*10491SRishi.Srivatsavai@Sun.COM * under the terms of the GNU Lesser General Public License as published by the
10*10491SRishi.Srivatsavai@Sun.COM * Free Software Foundation; version 2.1
11*10491SRishi.Srivatsavai@Sun.COM *
12*10491SRishi.Srivatsavai@Sun.COM * RSTP library is distributed in the hope that it will be useful, but
13*10491SRishi.Srivatsavai@Sun.COM * WITHOUT ANY WARRANTY; without even the implied warranty of
14*10491SRishi.Srivatsavai@Sun.COM * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15*10491SRishi.Srivatsavai@Sun.COM * General Public License for more details.
16*10491SRishi.Srivatsavai@Sun.COM *
17*10491SRishi.Srivatsavai@Sun.COM * You should have received a copy of the GNU Lesser General Public License
18*10491SRishi.Srivatsavai@Sun.COM * along with RSTP library; see the file COPYING. If not, write to the Free
19*10491SRishi.Srivatsavai@Sun.COM * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20*10491SRishi.Srivatsavai@Sun.COM * 02111-1307, USA.
21*10491SRishi.Srivatsavai@Sun.COM **********************************************************************/
22*10491SRishi.Srivatsavai@Sun.COM
23*10491SRishi.Srivatsavai@Sun.COM /* Generic (abstract) state machine : 17.13, 17.14 */
24*10491SRishi.Srivatsavai@Sun.COM
25*10491SRishi.Srivatsavai@Sun.COM #include "base.h"
26*10491SRishi.Srivatsavai@Sun.COM #include "statmch.h"
27*10491SRishi.Srivatsavai@Sun.COM #include "stp_vectors.h"
28*10491SRishi.Srivatsavai@Sun.COM
29*10491SRishi.Srivatsavai@Sun.COM #if STP_DBG
30*10491SRishi.Srivatsavai@Sun.COM # include "stpm.h"
31*10491SRishi.Srivatsavai@Sun.COM #endif
32*10491SRishi.Srivatsavai@Sun.COM
33*10491SRishi.Srivatsavai@Sun.COM STATE_MACH_T *
STP_state_mach_create(void (* concreteEnterState)(STATE_MACH_T *),Bool (* concreteCheckCondition)(STATE_MACH_T *),char * (* concreteGetStatName)(int),void * owner,char * name)34*10491SRishi.Srivatsavai@Sun.COM STP_state_mach_create (void (*concreteEnterState) (STATE_MACH_T*),
35*10491SRishi.Srivatsavai@Sun.COM Bool (*concreteCheckCondition) (STATE_MACH_T*),
36*10491SRishi.Srivatsavai@Sun.COM char *(*concreteGetStatName) (int),
37*10491SRishi.Srivatsavai@Sun.COM void *owner, char *name)
38*10491SRishi.Srivatsavai@Sun.COM {
39*10491SRishi.Srivatsavai@Sun.COM STATE_MACH_T *this;
40*10491SRishi.Srivatsavai@Sun.COM
41*10491SRishi.Srivatsavai@Sun.COM STP_MALLOC(this, STATE_MACH_T, "state machine");
42*10491SRishi.Srivatsavai@Sun.COM
43*10491SRishi.Srivatsavai@Sun.COM this->State = BEGIN;
44*10491SRishi.Srivatsavai@Sun.COM this->name = (char*) strdup (name);
45*10491SRishi.Srivatsavai@Sun.COM this->changeState = False;
46*10491SRishi.Srivatsavai@Sun.COM #if STP_DBG
47*10491SRishi.Srivatsavai@Sun.COM this->debug = False;
48*10491SRishi.Srivatsavai@Sun.COM this->ignoreHop2State = BEGIN;
49*10491SRishi.Srivatsavai@Sun.COM #endif
50*10491SRishi.Srivatsavai@Sun.COM this->concreteEnterState = concreteEnterState;
51*10491SRishi.Srivatsavai@Sun.COM this->concreteCheckCondition = concreteCheckCondition;
52*10491SRishi.Srivatsavai@Sun.COM this->concreteGetStatName = concreteGetStatName;
53*10491SRishi.Srivatsavai@Sun.COM this->owner.owner = owner;
54*10491SRishi.Srivatsavai@Sun.COM
55*10491SRishi.Srivatsavai@Sun.COM return this;
56*10491SRishi.Srivatsavai@Sun.COM }
57*10491SRishi.Srivatsavai@Sun.COM
58*10491SRishi.Srivatsavai@Sun.COM void
STP_state_mach_delete(STATE_MACH_T * this)59*10491SRishi.Srivatsavai@Sun.COM STP_state_mach_delete (STATE_MACH_T *this)
60*10491SRishi.Srivatsavai@Sun.COM {
61*10491SRishi.Srivatsavai@Sun.COM free (this->name);
62*10491SRishi.Srivatsavai@Sun.COM STP_FREE(this, "state machine");
63*10491SRishi.Srivatsavai@Sun.COM }
64*10491SRishi.Srivatsavai@Sun.COM
65*10491SRishi.Srivatsavai@Sun.COM Bool
STP_check_condition(STATE_MACH_T * this)66*10491SRishi.Srivatsavai@Sun.COM STP_check_condition (STATE_MACH_T* this)
67*10491SRishi.Srivatsavai@Sun.COM {
68*10491SRishi.Srivatsavai@Sun.COM Bool bret;
69*10491SRishi.Srivatsavai@Sun.COM
70*10491SRishi.Srivatsavai@Sun.COM bret = (*(this->concreteCheckCondition)) (this);
71*10491SRishi.Srivatsavai@Sun.COM if (bret) {
72*10491SRishi.Srivatsavai@Sun.COM this->changeState = True;
73*10491SRishi.Srivatsavai@Sun.COM }
74*10491SRishi.Srivatsavai@Sun.COM
75*10491SRishi.Srivatsavai@Sun.COM return bret;
76*10491SRishi.Srivatsavai@Sun.COM }
77*10491SRishi.Srivatsavai@Sun.COM
78*10491SRishi.Srivatsavai@Sun.COM Bool
STP_change_state(STATE_MACH_T * this)79*10491SRishi.Srivatsavai@Sun.COM STP_change_state (STATE_MACH_T* this)
80*10491SRishi.Srivatsavai@Sun.COM {
81*10491SRishi.Srivatsavai@Sun.COM register int number_of_loops;
82*10491SRishi.Srivatsavai@Sun.COM
83*10491SRishi.Srivatsavai@Sun.COM for (number_of_loops = 0; ; number_of_loops++) {
84*10491SRishi.Srivatsavai@Sun.COM if (! this->changeState) break;
85*10491SRishi.Srivatsavai@Sun.COM (*(this->concreteEnterState)) (this);
86*10491SRishi.Srivatsavai@Sun.COM this->changeState = False;
87*10491SRishi.Srivatsavai@Sun.COM (void) STP_check_condition (this);
88*10491SRishi.Srivatsavai@Sun.COM }
89*10491SRishi.Srivatsavai@Sun.COM
90*10491SRishi.Srivatsavai@Sun.COM return number_of_loops;
91*10491SRishi.Srivatsavai@Sun.COM }
92*10491SRishi.Srivatsavai@Sun.COM
93*10491SRishi.Srivatsavai@Sun.COM Bool
STP_hop_2_state(STATE_MACH_T * this,unsigned int new_state)94*10491SRishi.Srivatsavai@Sun.COM STP_hop_2_state (STATE_MACH_T* this, unsigned int new_state)
95*10491SRishi.Srivatsavai@Sun.COM {
96*10491SRishi.Srivatsavai@Sun.COM #ifdef STP_DBG
97*10491SRishi.Srivatsavai@Sun.COM switch (this->debug) {
98*10491SRishi.Srivatsavai@Sun.COM case 0: break;
99*10491SRishi.Srivatsavai@Sun.COM case 1:
100*10491SRishi.Srivatsavai@Sun.COM if (new_state == this->State || new_state == this->ignoreHop2State) break;
101*10491SRishi.Srivatsavai@Sun.COM stp_trace ("%-8s(%s-%s): %s=>%s",
102*10491SRishi.Srivatsavai@Sun.COM this->name,
103*10491SRishi.Srivatsavai@Sun.COM *this->owner.port->owner->name ? this->owner.port->owner->name : "Glbl",
104*10491SRishi.Srivatsavai@Sun.COM this->owner.port->port_name,
105*10491SRishi.Srivatsavai@Sun.COM (*(this->concreteGetStatName)) (this->State),
106*10491SRishi.Srivatsavai@Sun.COM (*(this->concreteGetStatName)) (new_state));
107*10491SRishi.Srivatsavai@Sun.COM break;
108*10491SRishi.Srivatsavai@Sun.COM case 2:
109*10491SRishi.Srivatsavai@Sun.COM if (new_state == this->State) break;
110*10491SRishi.Srivatsavai@Sun.COM stp_trace ("%s(%s): %s=>%s",
111*10491SRishi.Srivatsavai@Sun.COM this->name,
112*10491SRishi.Srivatsavai@Sun.COM *this->owner.stpm->name ? this->owner.stpm->name : "Glbl",
113*10491SRishi.Srivatsavai@Sun.COM (*(this->concreteGetStatName)) (this->State),
114*10491SRishi.Srivatsavai@Sun.COM (*(this->concreteGetStatName)) (new_state));
115*10491SRishi.Srivatsavai@Sun.COM break;
116*10491SRishi.Srivatsavai@Sun.COM }
117*10491SRishi.Srivatsavai@Sun.COM #endif
118*10491SRishi.Srivatsavai@Sun.COM
119*10491SRishi.Srivatsavai@Sun.COM this->State = new_state;
120*10491SRishi.Srivatsavai@Sun.COM this->changeState = True;
121*10491SRishi.Srivatsavai@Sun.COM return True;
122*10491SRishi.Srivatsavai@Sun.COM }
123*10491SRishi.Srivatsavai@Sun.COM
124