fsm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. ft->tl.function = (void *) FsmExpireTimer;
  103. ft->tl.data = (long) ft;
  104. #if FSM_TIMER_DEBUG
  105. if (ft->fi->debug)
  106. ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
  107. #endif
  108. init_timer(&ft->tl);
  109. }
  110. EXPORT_SYMBOL(mISDN_FsmInitTimer);
  111. void
  112. mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
  113. {
  114. #if FSM_TIMER_DEBUG
  115. if (ft->fi->debug)
  116. ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
  117. (long) ft, where);
  118. #endif
  119. del_timer(&ft->tl);
  120. }
  121. EXPORT_SYMBOL(mISDN_FsmDelTimer);
  122. int
  123. mISDN_FsmAddTimer(struct FsmTimer *ft,
  124. int millisec, int event, void *arg, int where)
  125. {
  126. #if FSM_TIMER_DEBUG
  127. if (ft->fi->debug)
  128. ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
  129. (long) ft, millisec, where);
  130. #endif
  131. if (timer_pending(&ft->tl)) {
  132. if (ft->fi->debug) {
  133. printk(KERN_WARNING
  134. "mISDN_FsmAddTimer: timer already active!\n");
  135. ft->fi->printdebug(ft->fi,
  136. "mISDN_FsmAddTimer already active!");
  137. }
  138. return -1;
  139. }
  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. return 0;
  146. }
  147. EXPORT_SYMBOL(mISDN_FsmAddTimer);
  148. void
  149. mISDN_FsmRestartTimer(struct FsmTimer *ft,
  150. int millisec, int event, void *arg, int where)
  151. {
  152. #if FSM_TIMER_DEBUG
  153. if (ft->fi->debug)
  154. ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
  155. (long) ft, millisec, where);
  156. #endif
  157. if (timer_pending(&ft->tl))
  158. del_timer(&ft->tl);
  159. init_timer(&ft->tl);
  160. ft->event = event;
  161. ft->arg = arg;
  162. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  163. add_timer(&ft->tl);
  164. }
  165. EXPORT_SYMBOL(mISDN_FsmRestartTimer);