fsm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * finite state machine implementation
  3. *
  4. * Author Karsten Keil <kkeil@novell.com>
  5. *
  6. * Thanks to Jan den Ouden
  7. * Fritz Elfert
  8. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/string.h>
  24. #include "fsm.h"
  25. #define FSM_TIMER_DEBUG 0
  26. void
  27. mISDN_FsmNew(struct Fsm *fsm,
  28. struct FsmNode *fnlist, int fncount)
  29. {
  30. int i;
  31. fsm->jumpmatrix = kzalloc(sizeof(FSMFNPTR) * fsm->state_count *
  32. fsm->event_count, GFP_KERNEL);
  33. for (i = 0; i < fncount; i++)
  34. if ((fnlist[i].state >= fsm->state_count) ||
  35. (fnlist[i].event >= fsm->event_count)) {
  36. printk(KERN_ERR
  37. "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
  38. i, (long)fnlist[i].state, (long)fsm->state_count,
  39. (long)fnlist[i].event, (long)fsm->event_count);
  40. } else
  41. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  42. fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
  43. }
  44. EXPORT_SYMBOL(mISDN_FsmNew);
  45. void
  46. mISDN_FsmFree(struct Fsm *fsm)
  47. {
  48. kfree((void *) fsm->jumpmatrix);
  49. }
  50. EXPORT_SYMBOL(mISDN_FsmFree);
  51. int
  52. mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
  53. {
  54. FSMFNPTR r;
  55. if ((fi->state >= fi->fsm->state_count) ||
  56. (event >= fi->fsm->event_count)) {
  57. printk(KERN_ERR
  58. "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
  59. (long)fi->state, (long)fi->fsm->state_count, event,
  60. (long)fi->fsm->event_count);
  61. return 1;
  62. }
  63. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  64. if (r) {
  65. if (fi->debug)
  66. fi->printdebug(fi, "State %s Event %s",
  67. fi->fsm->strState[fi->state],
  68. fi->fsm->strEvent[event]);
  69. r(fi, event, arg);
  70. return 0;
  71. } else {
  72. if (fi->debug)
  73. fi->printdebug(fi, "State %s Event %s no action",
  74. fi->fsm->strState[fi->state],
  75. fi->fsm->strEvent[event]);
  76. return 1;
  77. }
  78. }
  79. EXPORT_SYMBOL(mISDN_FsmEvent);
  80. void
  81. mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
  82. {
  83. fi->state = newstate;
  84. if (fi->debug)
  85. fi->printdebug(fi, "ChangeState %s",
  86. fi->fsm->strState[newstate]);
  87. }
  88. EXPORT_SYMBOL(mISDN_FsmChangeState);
  89. static void
  90. FsmExpireTimer(struct FsmTimer *ft)
  91. {
  92. #if FSM_TIMER_DEBUG
  93. if (ft->fi->debug)
  94. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  95. #endif
  96. mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
  97. }
  98. void
  99. mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  100. {
  101. ft->fi = fi;
  102. #if FSM_TIMER_DEBUG
  103. if (ft->fi->debug)
  104. ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
  105. #endif
  106. setup_timer(&ft->tl, (void *)FsmExpireTimer, (long)ft);
  107. }
  108. EXPORT_SYMBOL(mISDN_FsmInitTimer);
  109. void
  110. mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
  111. {
  112. #if FSM_TIMER_DEBUG
  113. if (ft->fi->debug)
  114. ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
  115. (long) ft, where);
  116. #endif
  117. del_timer(&ft->tl);
  118. }
  119. EXPORT_SYMBOL(mISDN_FsmDelTimer);
  120. int
  121. mISDN_FsmAddTimer(struct FsmTimer *ft,
  122. int millisec, int event, void *arg, int where)
  123. {
  124. #if FSM_TIMER_DEBUG
  125. if (ft->fi->debug)
  126. ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
  127. (long) ft, millisec, where);
  128. #endif
  129. if (timer_pending(&ft->tl)) {
  130. if (ft->fi->debug) {
  131. printk(KERN_WARNING
  132. "mISDN_FsmAddTimer: timer already active!\n");
  133. ft->fi->printdebug(ft->fi,
  134. "mISDN_FsmAddTimer already active!");
  135. }
  136. return -1;
  137. }
  138. init_timer(&ft->tl);
  139. ft->event = event;
  140. ft->arg = arg;
  141. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  142. add_timer(&ft->tl);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(mISDN_FsmAddTimer);
  146. void
  147. mISDN_FsmRestartTimer(struct FsmTimer *ft,
  148. int millisec, int event, void *arg, int where)
  149. {
  150. #if FSM_TIMER_DEBUG
  151. if (ft->fi->debug)
  152. ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
  153. (long) ft, millisec, where);
  154. #endif
  155. if (timer_pending(&ft->tl))
  156. del_timer(&ft->tl);
  157. init_timer(&ft->tl);
  158. ft->event = event;
  159. ft->arg = arg;
  160. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  161. add_timer(&ft->tl);
  162. }
  163. EXPORT_SYMBOL(mISDN_FsmRestartTimer);