2 * A generic FSM based on fsm used in isdn4linux
7 #include <linux/module.h>
8 #include <linux/slab.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 = kzalloc(sizeof(fsm_instance
), order
);
27 "fsm(%s): init_fsm: Couldn't alloc instance\n", name
);
30 strlcpy(this->name
, name
, sizeof(this->name
));
31 init_waitqueue_head(&this->wait_q
);
33 f
= kzalloc(sizeof(fsm
), order
);
36 "fsm(%s): init_fsm: Couldn't alloc fsm\n", name
);
40 f
->nr_events
= nr_events
;
41 f
->nr_states
= nr_states
;
42 f
->event_names
= event_names
;
43 f
->state_names
= state_names
;
46 m
= kcalloc(nr_states
*nr_events
, sizeof(fsm_function_t
), order
);
49 "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name
);
55 for (i
= 0; i
< tmpl_len
; i
++) {
56 if ((tmpl
[i
].cond_state
>= nr_states
) ||
57 (tmpl
[i
].cond_event
>= nr_events
) ) {
59 "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
60 name
, i
, (long)tmpl
[i
].cond_state
, (long)f
->nr_states
,
61 (long)tmpl
[i
].cond_event
, (long)f
->nr_events
);
65 m
[nr_states
* tmpl
[i
].cond_event
+ tmpl
[i
].cond_state
] =
72 kfree_fsm(fsm_instance
*this)
76 kfree(this->f
->jumpmatrix
);
82 "fsm: kfree_fsm called with NULL argument\n");
87 fsm_print_history(fsm_instance
*fi
)
92 if (fi
->history_size
>= FSM_HISTORY_SIZE
)
93 idx
= fi
->history_index
;
95 printk(KERN_DEBUG
"fsm(%s): History:\n", fi
->name
);
96 for (i
= 0; i
< fi
->history_size
; i
++) {
97 int e
= fi
->history
[idx
].event
;
98 int s
= fi
->history
[idx
++].state
;
99 idx
%= FSM_HISTORY_SIZE
;
101 printk(KERN_DEBUG
" S=%s\n",
102 fi
->f
->state_names
[s
]);
104 printk(KERN_DEBUG
" S=%s E=%s\n",
105 fi
->f
->state_names
[s
],
106 fi
->f
->event_names
[e
]);
108 fi
->history_size
= fi
->history_index
= 0;
112 fsm_record_history(fsm_instance
*fi
, int state
, int event
)
114 fi
->history
[fi
->history_index
].state
= state
;
115 fi
->history
[fi
->history_index
++].event
= event
;
116 fi
->history_index
%= FSM_HISTORY_SIZE
;
117 if (fi
->history_size
< FSM_HISTORY_SIZE
)
123 fsm_getstate_str(fsm_instance
*fi
)
125 int st
= atomic_read(&fi
->state
);
126 if (st
>= fi
->f
->nr_states
)
128 return fi
->f
->state_names
[st
];
132 fsm_expire_timer(fsm_timer
*this)
135 printk(KERN_DEBUG
"fsm(%s): Timer %p expired\n",
136 this->fi
->name
, this);
138 fsm_event(this->fi
, this->expire_event
, this->event_arg
);
142 fsm_settimer(fsm_instance
*fi
, fsm_timer
*this)
145 this->tl
.function
= (void *)fsm_expire_timer
;
146 this->tl
.data
= (long)this;
148 printk(KERN_DEBUG
"fsm(%s): Create timer %p\n", fi
->name
,
151 init_timer(&this->tl
);
155 fsm_deltimer(fsm_timer
*this)
158 printk(KERN_DEBUG
"fsm(%s): Delete timer %p\n", this->fi
->name
,
161 del_timer(&this->tl
);
165 fsm_addtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
169 printk(KERN_DEBUG
"fsm(%s): Add timer %p %dms\n",
170 this->fi
->name
, this, millisec
);
173 init_timer(&this->tl
);
174 this->tl
.function
= (void *)fsm_expire_timer
;
175 this->tl
.data
= (long)this;
176 this->expire_event
= event
;
177 this->event_arg
= arg
;
178 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
179 add_timer(&this->tl
);
183 /* FIXME: this function is never used, why */
185 fsm_modtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
189 printk(KERN_DEBUG
"fsm(%s): Restart timer %p %dms\n",
190 this->fi
->name
, this, millisec
);
193 del_timer(&this->tl
);
194 init_timer(&this->tl
);
195 this->tl
.function
= (void *)fsm_expire_timer
;
196 this->tl
.data
= (long)this;
197 this->expire_event
= event
;
198 this->event_arg
= arg
;
199 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
200 add_timer(&this->tl
);
203 EXPORT_SYMBOL(init_fsm
);
204 EXPORT_SYMBOL(kfree_fsm
);
205 EXPORT_SYMBOL(fsm_settimer
);
206 EXPORT_SYMBOL(fsm_deltimer
);
207 EXPORT_SYMBOL(fsm_addtimer
);
208 EXPORT_SYMBOL(fsm_modtimer
);
209 EXPORT_SYMBOL(fsm_getstate_str
);
211 #if FSM_DEBUG_HISTORY
212 EXPORT_SYMBOL(fsm_print_history
);
213 EXPORT_SYMBOL(fsm_record_history
);