2 * $Id: fsm.c,v 1.6 2003/10/15 11:37:29 mschwide Exp $
4 * A generic FSM based on fsm used in isdn4linux
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/timer.h>
13 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
14 MODULE_DESCRIPTION("Finite state machine helper functions");
15 MODULE_LICENSE("GPL");
18 init_fsm(char *name
, const char **state_names
, const char **event_names
, int nr_states
,
19 int nr_events
, const fsm_node
*tmpl
, int tmpl_len
, int order
)
26 this = (fsm_instance
*)kmalloc(sizeof(fsm_instance
), order
);
29 "fsm(%s): init_fsm: Couldn't alloc instance\n", name
);
32 memset(this, 0, sizeof(fsm_instance
));
33 strlcpy(this->name
, name
, sizeof(this->name
));
35 f
= (fsm
*)kmalloc(sizeof(fsm
), order
);
38 "fsm(%s): init_fsm: Couldn't alloc fsm\n", name
);
42 memset(f
, 0, sizeof(fsm
));
43 f
->nr_events
= nr_events
;
44 f
->nr_states
= nr_states
;
45 f
->event_names
= event_names
;
46 f
->state_names
= state_names
;
49 m
= (fsm_function_t
*)kmalloc(
50 sizeof(fsm_function_t
) * nr_states
* nr_events
, order
);
53 "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name
);
57 memset(m
, 0, sizeof(fsm_function_t
) * f
->nr_states
* f
->nr_events
);
60 for (i
= 0; i
< tmpl_len
; i
++) {
61 if ((tmpl
[i
].cond_state
>= nr_states
) ||
62 (tmpl
[i
].cond_event
>= nr_events
) ) {
64 "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
65 name
, i
, (long)tmpl
[i
].cond_state
, (long)f
->nr_states
,
66 (long)tmpl
[i
].cond_event
, (long)f
->nr_events
);
70 m
[nr_states
* tmpl
[i
].cond_event
+ tmpl
[i
].cond_state
] =
77 kfree_fsm(fsm_instance
*this)
81 if (this->f
->jumpmatrix
)
82 kfree(this->f
->jumpmatrix
);
88 "fsm: kfree_fsm called with NULL argument\n");
93 fsm_print_history(fsm_instance
*fi
)
98 if (fi
->history_size
>= FSM_HISTORY_SIZE
)
99 idx
= fi
->history_index
;
101 printk(KERN_DEBUG
"fsm(%s): History:\n", fi
->name
);
102 for (i
= 0; i
< fi
->history_size
; i
++) {
103 int e
= fi
->history
[idx
].event
;
104 int s
= fi
->history
[idx
++].state
;
105 idx
%= FSM_HISTORY_SIZE
;
107 printk(KERN_DEBUG
" S=%s\n",
108 fi
->f
->state_names
[s
]);
110 printk(KERN_DEBUG
" S=%s E=%s\n",
111 fi
->f
->state_names
[s
],
112 fi
->f
->event_names
[e
]);
114 fi
->history_size
= fi
->history_index
= 0;
118 fsm_record_history(fsm_instance
*fi
, int state
, int event
)
120 fi
->history
[fi
->history_index
].state
= state
;
121 fi
->history
[fi
->history_index
++].event
= event
;
122 fi
->history_index
%= FSM_HISTORY_SIZE
;
123 if (fi
->history_size
< FSM_HISTORY_SIZE
)
129 fsm_getstate_str(fsm_instance
*fi
)
131 int st
= atomic_read(&fi
->state
);
132 if (st
>= fi
->f
->nr_states
)
134 return fi
->f
->state_names
[st
];
138 fsm_expire_timer(fsm_timer
*this)
141 printk(KERN_DEBUG
"fsm(%s): Timer %p expired\n",
142 this->fi
->name
, this);
144 fsm_event(this->fi
, this->expire_event
, this->event_arg
);
148 fsm_settimer(fsm_instance
*fi
, fsm_timer
*this)
151 this->tl
.function
= (void *)fsm_expire_timer
;
152 this->tl
.data
= (long)this;
154 printk(KERN_DEBUG
"fsm(%s): Create timer %p\n", fi
->name
,
157 init_timer(&this->tl
);
161 fsm_deltimer(fsm_timer
*this)
164 printk(KERN_DEBUG
"fsm(%s): Delete timer %p\n", this->fi
->name
,
167 del_timer(&this->tl
);
171 fsm_addtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
175 printk(KERN_DEBUG
"fsm(%s): Add timer %p %dms\n",
176 this->fi
->name
, this, millisec
);
179 init_timer(&this->tl
);
180 this->tl
.function
= (void *)fsm_expire_timer
;
181 this->tl
.data
= (long)this;
182 this->expire_event
= event
;
183 this->event_arg
= arg
;
184 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
185 add_timer(&this->tl
);
189 /* FIXME: this function is never used, why */
191 fsm_modtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
195 printk(KERN_DEBUG
"fsm(%s): Restart timer %p %dms\n",
196 this->fi
->name
, this, millisec
);
199 del_timer(&this->tl
);
200 init_timer(&this->tl
);
201 this->tl
.function
= (void *)fsm_expire_timer
;
202 this->tl
.data
= (long)this;
203 this->expire_event
= event
;
204 this->event_arg
= arg
;
205 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
206 add_timer(&this->tl
);
209 EXPORT_SYMBOL(init_fsm
);
210 EXPORT_SYMBOL(kfree_fsm
);
211 EXPORT_SYMBOL(fsm_settimer
);
212 EXPORT_SYMBOL(fsm_deltimer
);
213 EXPORT_SYMBOL(fsm_addtimer
);
214 EXPORT_SYMBOL(fsm_modtimer
);
215 EXPORT_SYMBOL(fsm_getstate_str
);
217 #if FSM_DEBUG_HISTORY
218 EXPORT_SYMBOL(fsm_print_history
);
219 EXPORT_SYMBOL(fsm_record_history
);