1 /* $Id: fsm.c,v 1.14.6.4 2001/09/23 22:24:47 kai Exp $
6 * Copyright by Karsten Keil <keil@isdn4linux.de>
7 * by Kai Germaschewski <kai.germaschewski@gmx.de>
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
12 * Thanks to Jan den Ouden
17 #include <linux/module.h>
18 #include <linux/init.h>
21 #define FSM_TIMER_DEBUG 0
24 FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
28 fsm->jumpmatrix = (FSMFNPTR *)
29 kzalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
33 for (i = 0; i < fncount; i++)
34 if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) {
35 printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
36 i,(long)fnlist[i].state,(long)fsm->state_count,
37 (long)fnlist[i].event,(long)fsm->event_count);
39 fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
40 fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
45 FsmFree(struct Fsm *fsm)
47 kfree((void *) fsm->jumpmatrix);
51 FsmEvent(struct FsmInst *fi, int event, void *arg)
55 if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
56 printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
57 (long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count);
60 r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
63 fi->printdebug(fi, "State %s Event %s",
64 fi->fsm->strState[fi->state],
65 fi->fsm->strEvent[event]);
70 fi->printdebug(fi, "State %s Event %s no routine",
71 fi->fsm->strState[fi->state],
72 fi->fsm->strEvent[event]);
78 FsmChangeState(struct FsmInst *fi, int newstate)
82 fi->printdebug(fi, "ChangeState %s",
83 fi->fsm->strState[newstate]);
87 FsmExpireTimer(struct FsmTimer *ft)
91 ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
93 FsmEvent(ft->fi, ft->event, ft->arg);
97 FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
100 ft->tl.function = (void *) FsmExpireTimer;
101 ft->tl.data = (long) ft;
104 ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
110 FsmDelTimer(struct FsmTimer *ft, int where)
114 ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
120 FsmAddTimer(struct FsmTimer *ft,
121 int millisec, int event, void *arg, int where)
126 ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
127 (long) ft, millisec, where);
130 if (timer_pending(&ft->tl)) {
131 printk(KERN_WARNING "FsmAddTimer: timer already active!\n");
132 ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
138 ft->tl.expires = jiffies + (millisec * HZ) / 1000;
144 FsmRestartTimer(struct FsmTimer *ft,
145 int millisec, int event, void *arg, int where)
150 ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
151 (long) ft, millisec, where);
154 if (timer_pending(&ft->tl))
159 ft->tl.expires = jiffies + (millisec * HZ) / 1000;