fsm.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: fsm.c,v 1.1.4.1 2001/11/20 14:19:35 kai Exp $
  2.  *
  3.  * Finite state machine
  4.  *
  5.  * Author       Karsten Keil
  6.  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
  7.  *              by Kai Germaschewski <kai.germaschewski@gmx.de>
  8.  * 
  9.  * This software may be used and distributed according to the terms
  10.  * of the GNU General Public License, incorporated herein by reference.
  11.  *
  12.  * Thanks to    Jan den Ouden
  13.  *              Fritz Elfert
  14.  *
  15.  */
  16. #define __NO_VERSION__
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include "hisax.h"
  20. #define FSM_TIMER_DEBUG 0
  21. int __init
  22. FsmNew(struct Fsm *fsm, struct FsmNode *fnlist, int fncount)
  23. {
  24. int i;
  25. fsm->jumpmatrix = (FSMFNPTR *)
  26. kmalloc(sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count, GFP_KERNEL);
  27. if (!fsm->jumpmatrix)
  28. return -ENOMEM;
  29. memset(fsm->jumpmatrix, 0, sizeof (FSMFNPTR) * fsm->state_count * fsm->event_count);
  30. for (i = 0; i < fncount; i++) 
  31. if ((fnlist[i].state>=fsm->state_count) || (fnlist[i].event>=fsm->event_count)) {
  32. printk(KERN_ERR "FsmNew Error line %d st(%ld/%ld) ev(%ld/%ld)n",
  33. i,(long)fnlist[i].state,(long)fsm->state_count,
  34. (long)fnlist[i].event,(long)fsm->event_count);
  35. } else
  36. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  37. fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
  38. return 0;
  39. }
  40. void
  41. FsmFree(struct Fsm *fsm)
  42. {
  43. kfree((void *) fsm->jumpmatrix);
  44. }
  45. int
  46. FsmEvent(struct FsmInst *fi, int event, void *arg)
  47. {
  48. FSMFNPTR r;
  49. if ((fi->state>=fi->fsm->state_count) || (event >= fi->fsm->event_count)) {
  50. printk(KERN_ERR "FsmEvent Error st(%ld/%ld) ev(%d/%ld)n",
  51. (long)fi->state,(long)fi->fsm->state_count,event,(long)fi->fsm->event_count);
  52. return(1);
  53. }
  54. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  55. if (r) {
  56. if (fi->debug)
  57. fi->printdebug(fi, "State %s Event %s",
  58. fi->fsm->strState[fi->state],
  59. fi->fsm->strEvent[event]);
  60. r(fi, event, arg);
  61. return (0);
  62. } else {
  63. if (fi->debug)
  64. fi->printdebug(fi, "State %s Event %s no routine",
  65. fi->fsm->strState[fi->state],
  66. fi->fsm->strEvent[event]);
  67. return (!0);
  68. }
  69. }
  70. void
  71. FsmChangeState(struct FsmInst *fi, int newstate)
  72. {
  73. fi->state = newstate;
  74. if (fi->debug)
  75. fi->printdebug(fi, "ChangeState %s",
  76. fi->fsm->strState[newstate]);
  77. }
  78. static void
  79. FsmExpireTimer(struct FsmTimer *ft)
  80. {
  81. #if FSM_TIMER_DEBUG
  82. if (ft->fi->debug)
  83. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  84. #endif
  85. FsmEvent(ft->fi, ft->event, ft->arg);
  86. }
  87. void
  88. FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  89. {
  90. ft->fi = fi;
  91. ft->tl.function = (void *) FsmExpireTimer;
  92. ft->tl.data = (long) ft;
  93. #if FSM_TIMER_DEBUG
  94. if (ft->fi->debug)
  95. ft->fi->printdebug(ft->fi, "FsmInitTimer %lx", (long) ft);
  96. #endif
  97. init_timer(&ft->tl);
  98. }
  99. void
  100. FsmDelTimer(struct FsmTimer *ft, int where)
  101. {
  102. #if FSM_TIMER_DEBUG
  103. if (ft->fi->debug)
  104. ft->fi->printdebug(ft->fi, "FsmDelTimer %lx %d", (long) ft, where);
  105. #endif
  106. del_timer(&ft->tl);
  107. }
  108. int
  109. FsmAddTimer(struct FsmTimer *ft,
  110.     int millisec, int event, void *arg, int where)
  111. {
  112. #if FSM_TIMER_DEBUG
  113. if (ft->fi->debug)
  114. ft->fi->printdebug(ft->fi, "FsmAddTimer %lx %d %d",
  115. (long) ft, millisec, where);
  116. #endif
  117. if (timer_pending(&ft->tl)) {
  118. printk(KERN_WARNING "FsmAddTimer: timer already active!n");
  119. ft->fi->printdebug(ft->fi, "FsmAddTimer already active!");
  120. return -1;
  121. }
  122. init_timer(&ft->tl);
  123. ft->event = event;
  124. ft->arg = arg;
  125. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  126. add_timer(&ft->tl);
  127. return 0;
  128. }
  129. void
  130. FsmRestartTimer(struct FsmTimer *ft,
  131.     int millisec, int event, void *arg, int where)
  132. {
  133. #if FSM_TIMER_DEBUG
  134. if (ft->fi->debug)
  135. ft->fi->printdebug(ft->fi, "FsmRestartTimer %lx %d %d",
  136. (long) ft, millisec, where);
  137. #endif
  138. if (timer_pending(&ft->tl))
  139. del_timer(&ft->tl);
  140. init_timer(&ft->tl);
  141. ft->event = event;
  142. ft->arg = arg;
  143. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  144. add_timer(&ft->tl);
  145. }