2 * A generic FSM based on fsm used in isdn4linux
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/timer.h>
11 MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
12 MODULE_DESCRIPTION("Finite state machine helper functions");
13 MODULE_LICENSE("GPL");
16 init_fsm(char *name, const char **state_names, const char **event_names, int nr_states,
17 int nr_events, const fsm_node *tmpl, int tmpl_len, gfp_t order)
24 this = (fsm_instance *)kmalloc(sizeof(fsm_instance), order);
27 "fsm(%s): init_fsm: Couldn't alloc instance\n", name);
30 memset(this, 0, sizeof(fsm_instance));
31 strlcpy(this->name, name, sizeof(this->name));
33 f = (fsm *)kmalloc(sizeof(fsm), order);
36 "fsm(%s): init_fsm: Couldn't alloc fsm\n", name);
40 memset(f, 0, sizeof(fsm));
41 f->nr_events = nr_events;
42 f->nr_states = nr_states;
43 f->event_names = event_names;
44 f->state_names = state_names;
47 m = (fsm_function_t *)kmalloc(
48 sizeof(fsm_function_t) * nr_states * nr_events, order);
51 "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name);
55 memset(m, 0, sizeof(fsm_function_t) * f->nr_states * f->nr_events);
58 for (i = 0; i < tmpl_len; i++) {
59 if ((tmpl[i].cond_state >= nr_states) ||
60 (tmpl[i].cond_event >= nr_events) ) {
62 "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
63 name, i, (long)tmpl[i].cond_state, (long)f->nr_states,
64 (long)tmpl[i].cond_event, (long)f->nr_events);
68 m[nr_states * tmpl[i].cond_event + tmpl[i].cond_state] =
75 kfree_fsm(fsm_instance *this)
79 kfree(this->f->jumpmatrix);
85 "fsm: kfree_fsm called with NULL argument\n");
90 fsm_print_history(fsm_instance *fi)
95 if (fi->history_size >= FSM_HISTORY_SIZE)
96 idx = fi->history_index;
98 printk(KERN_DEBUG "fsm(%s): History:\n", fi->name);
99 for (i = 0; i < fi->history_size; i++) {
100 int e = fi->history[idx].event;
101 int s = fi->history[idx++].state;
102 idx %= FSM_HISTORY_SIZE;
104 printk(KERN_DEBUG " S=%s\n",
105 fi->f->state_names[s]);
107 printk(KERN_DEBUG " S=%s E=%s\n",
108 fi->f->state_names[s],
109 fi->f->event_names[e]);
111 fi->history_size = fi->history_index = 0;
115 fsm_record_history(fsm_instance *fi, int state, int event)
117 fi->history[fi->history_index].state = state;
118 fi->history[fi->history_index++].event = event;
119 fi->history_index %= FSM_HISTORY_SIZE;
120 if (fi->history_size < FSM_HISTORY_SIZE)
126 fsm_getstate_str(fsm_instance *fi)
128 int st = atomic_read(&fi->state);
129 if (st >= fi->f->nr_states)
131 return fi->f->state_names[st];
135 fsm_expire_timer(fsm_timer *this)
138 printk(KERN_DEBUG "fsm(%s): Timer %p expired\n",
139 this->fi->name, this);
141 fsm_event(this->fi, this->expire_event, this->event_arg);
145 fsm_settimer(fsm_instance *fi, fsm_timer *this)
148 this->tl.function = (void *)fsm_expire_timer;
149 this->tl.data = (long)this;
151 printk(KERN_DEBUG "fsm(%s): Create timer %p\n", fi->name,
154 init_timer(&this->tl);
158 fsm_deltimer(fsm_timer *this)
161 printk(KERN_DEBUG "fsm(%s): Delete timer %p\n", this->fi->name,
164 del_timer(&this->tl);
168 fsm_addtimer(fsm_timer *this, int millisec, int event, void *arg)
172 printk(KERN_DEBUG "fsm(%s): Add timer %p %dms\n",
173 this->fi->name, this, millisec);
176 init_timer(&this->tl);
177 this->tl.function = (void *)fsm_expire_timer;
178 this->tl.data = (long)this;
179 this->expire_event = event;
180 this->event_arg = arg;
181 this->tl.expires = jiffies + (millisec * HZ) / 1000;
182 add_timer(&this->tl);
186 /* FIXME: this function is never used, why */
188 fsm_modtimer(fsm_timer *this, int millisec, int event, void *arg)
192 printk(KERN_DEBUG "fsm(%s): Restart timer %p %dms\n",
193 this->fi->name, this, millisec);
196 del_timer(&this->tl);
197 init_timer(&this->tl);
198 this->tl.function = (void *)fsm_expire_timer;
199 this->tl.data = (long)this;
200 this->expire_event = event;
201 this->event_arg = arg;
202 this->tl.expires = jiffies + (millisec * HZ) / 1000;
203 add_timer(&this->tl);
206 EXPORT_SYMBOL(init_fsm);
207 EXPORT_SYMBOL(kfree_fsm);
208 EXPORT_SYMBOL(fsm_settimer);
209 EXPORT_SYMBOL(fsm_deltimer);
210 EXPORT_SYMBOL(fsm_addtimer);
211 EXPORT_SYMBOL(fsm_modtimer);
212 EXPORT_SYMBOL(fsm_getstate_str);
214 #if FSM_DEBUG_HISTORY
215 EXPORT_SYMBOL(fsm_print_history);
216 EXPORT_SYMBOL(fsm_record_history);