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
, gfp_t 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 kfree(this->f
->jumpmatrix
);
87 "fsm: kfree_fsm called with NULL argument\n");
92 fsm_print_history(fsm_instance
*fi
)
97 if (fi
->history_size
>= FSM_HISTORY_SIZE
)
98 idx
= fi
->history_index
;
100 printk(KERN_DEBUG
"fsm(%s): History:\n", fi
->name
);
101 for (i
= 0; i
< fi
->history_size
; i
++) {
102 int e
= fi
->history
[idx
].event
;
103 int s
= fi
->history
[idx
++].state
;
104 idx
%= FSM_HISTORY_SIZE
;
106 printk(KERN_DEBUG
" S=%s\n",
107 fi
->f
->state_names
[s
]);
109 printk(KERN_DEBUG
" S=%s E=%s\n",
110 fi
->f
->state_names
[s
],
111 fi
->f
->event_names
[e
]);
113 fi
->history_size
= fi
->history_index
= 0;
117 fsm_record_history(fsm_instance
*fi
, int state
, int event
)
119 fi
->history
[fi
->history_index
].state
= state
;
120 fi
->history
[fi
->history_index
++].event
= event
;
121 fi
->history_index
%= FSM_HISTORY_SIZE
;
122 if (fi
->history_size
< FSM_HISTORY_SIZE
)
128 fsm_getstate_str(fsm_instance
*fi
)
130 int st
= atomic_read(&fi
->state
);
131 if (st
>= fi
->f
->nr_states
)
133 return fi
->f
->state_names
[st
];
137 fsm_expire_timer(fsm_timer
*this)
140 printk(KERN_DEBUG
"fsm(%s): Timer %p expired\n",
141 this->fi
->name
, this);
143 fsm_event(this->fi
, this->expire_event
, this->event_arg
);
147 fsm_settimer(fsm_instance
*fi
, fsm_timer
*this)
150 this->tl
.function
= (void *)fsm_expire_timer
;
151 this->tl
.data
= (long)this;
153 printk(KERN_DEBUG
"fsm(%s): Create timer %p\n", fi
->name
,
156 init_timer(&this->tl
);
160 fsm_deltimer(fsm_timer
*this)
163 printk(KERN_DEBUG
"fsm(%s): Delete timer %p\n", this->fi
->name
,
166 del_timer(&this->tl
);
170 fsm_addtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
174 printk(KERN_DEBUG
"fsm(%s): Add timer %p %dms\n",
175 this->fi
->name
, this, millisec
);
178 init_timer(&this->tl
);
179 this->tl
.function
= (void *)fsm_expire_timer
;
180 this->tl
.data
= (long)this;
181 this->expire_event
= event
;
182 this->event_arg
= arg
;
183 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
184 add_timer(&this->tl
);
188 /* FIXME: this function is never used, why */
190 fsm_modtimer(fsm_timer
*this, int millisec
, int event
, void *arg
)
194 printk(KERN_DEBUG
"fsm(%s): Restart timer %p %dms\n",
195 this->fi
->name
, this, millisec
);
198 del_timer(&this->tl
);
199 init_timer(&this->tl
);
200 this->tl
.function
= (void *)fsm_expire_timer
;
201 this->tl
.data
= (long)this;
202 this->expire_event
= event
;
203 this->event_arg
= arg
;
204 this->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
205 add_timer(&this->tl
);
208 EXPORT_SYMBOL(init_fsm
);
209 EXPORT_SYMBOL(kfree_fsm
);
210 EXPORT_SYMBOL(fsm_settimer
);
211 EXPORT_SYMBOL(fsm_deltimer
);
212 EXPORT_SYMBOL(fsm_addtimer
);
213 EXPORT_SYMBOL(fsm_modtimer
);
214 EXPORT_SYMBOL(fsm_getstate_str
);
216 #if FSM_DEBUG_HISTORY
217 EXPORT_SYMBOL(fsm_print_history
);
218 EXPORT_SYMBOL(fsm_record_history
);
This page took 0.034008 seconds and 5 git commands to generate.