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/slab.h>
19 #include <linux/init.h>
22 #define FSM_TIMER_DEBUG 0
25 FsmNew(struct Fsm
*fsm
, struct FsmNode
*fnlist
, int fncount
)
30 kzalloc(sizeof(FSMFNPTR
) * fsm
->state_count
* fsm
->event_count
, GFP_KERNEL
);
34 for (i
= 0; i
< fncount
; i
++)
35 if ((fnlist
[i
].state
>= fsm
->state_count
) || (fnlist
[i
].event
>= fsm
->event_count
)) {
36 printk(KERN_ERR
"FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)\n",
37 i
, (long)fnlist
[i
].state
, (long)fsm
->state_count
,
38 (long)fnlist
[i
].event
, (long)fsm
->event_count
);
40 fsm
->jumpmatrix
[fsm
->state_count
* fnlist
[i
].event
+
41 fnlist
[i
].state
] = (FSMFNPTR
)fnlist
[i
].routine
;
46 FsmFree(struct Fsm
*fsm
)
48 kfree((void *) fsm
->jumpmatrix
);
52 FsmEvent(struct FsmInst
*fi
, int event
, void *arg
)
56 if ((fi
->state
>= fi
->fsm
->state_count
) || (event
>= fi
->fsm
->event_count
)) {
57 printk(KERN_ERR
"FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
58 (long)fi
->state
, (long)fi
->fsm
->state_count
, event
, (long)fi
->fsm
->event_count
);
61 r
= fi
->fsm
->jumpmatrix
[fi
->fsm
->state_count
* event
+ fi
->state
];
64 fi
->printdebug(fi
, "State %s Event %s",
65 fi
->fsm
->strState
[fi
->state
],
66 fi
->fsm
->strEvent
[event
]);
71 fi
->printdebug(fi
, "State %s Event %s no routine",
72 fi
->fsm
->strState
[fi
->state
],
73 fi
->fsm
->strEvent
[event
]);
79 FsmChangeState(struct FsmInst
*fi
, int newstate
)
83 fi
->printdebug(fi
, "ChangeState %s",
84 fi
->fsm
->strState
[newstate
]);
88 FsmExpireTimer(struct FsmTimer
*ft
)
92 ft
->fi
->printdebug(ft
->fi
, "FsmExpireTimer %lx", (long) ft
);
94 FsmEvent(ft
->fi
, ft
->event
, ft
->arg
);
98 FsmInitTimer(struct FsmInst
*fi
, struct FsmTimer
*ft
)
101 ft
->tl
.function
= (void *) FsmExpireTimer
;
102 ft
->tl
.data
= (long) ft
;
105 ft
->fi
->printdebug(ft
->fi
, "FsmInitTimer %lx", (long) ft
);
111 FsmDelTimer(struct FsmTimer
*ft
, int where
)
115 ft
->fi
->printdebug(ft
->fi
, "FsmDelTimer %lx %d", (long) ft
, where
);
121 FsmAddTimer(struct FsmTimer
*ft
,
122 int millisec
, int event
, void *arg
, int where
)
127 ft
->fi
->printdebug(ft
->fi
, "FsmAddTimer %lx %d %d",
128 (long) ft
, millisec
, where
);
131 if (timer_pending(&ft
->tl
)) {
132 printk(KERN_WARNING
"FsmAddTimer: timer already active!\n");
133 ft
->fi
->printdebug(ft
->fi
, "FsmAddTimer already active!");
139 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;
145 FsmRestartTimer(struct FsmTimer
*ft
,
146 int millisec
, int event
, void *arg
, int where
)
151 ft
->fi
->printdebug(ft
->fi
, "FsmRestartTimer %lx %d %d",
152 (long) ft
, millisec
, where
);
155 if (timer_pending(&ft
->tl
))
160 ft
->tl
.expires
= jiffies
+ (millisec
* HZ
) / 1000;