fsm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. int
  27. mISDN_FsmNew(struct Fsm *fsm,
  28. struct FsmNode *fnlist, int fncount)
  29. {
  30. int i;
  31. fsm->jumpmatrix =
  32. kzalloc(array3_size(sizeof(FSMFNPTR), fsm->state_count,
  33. fsm->event_count),
  34. GFP_KERNEL);
  35. if (fsm->jumpmatrix == NULL)
  36. return -ENOMEM;
  37. for (i = 0; i < fncount; i++)
  38. if ((fnlist[i].state >= fsm->state_count) ||
  39. (fnlist[i].event >= fsm->event_count)) {
  40. printk(KERN_ERR
  41. "mISDN_FsmNew Error: %d st(%ld/%ld) ev(%ld/%ld)\n",
  42. i, (long)fnlist[i].state, (long)fsm->state_count,
  43. (long)fnlist[i].event, (long)fsm->event_count);
  44. } else
  45. fsm->jumpmatrix[fsm->state_count * fnlist[i].event +
  46. fnlist[i].state] = (FSMFNPTR) fnlist[i].routine;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(mISDN_FsmNew);
  50. void
  51. mISDN_FsmFree(struct Fsm *fsm)
  52. {
  53. kfree((void *) fsm->jumpmatrix);
  54. }
  55. EXPORT_SYMBOL(mISDN_FsmFree);
  56. int
  57. mISDN_FsmEvent(struct FsmInst *fi, int event, void *arg)
  58. {
  59. FSMFNPTR r;
  60. if ((fi->state >= fi->fsm->state_count) ||
  61. (event >= fi->fsm->event_count)) {
  62. printk(KERN_ERR
  63. "mISDN_FsmEvent Error st(%ld/%ld) ev(%d/%ld)\n",
  64. (long)fi->state, (long)fi->fsm->state_count, event,
  65. (long)fi->fsm->event_count);
  66. return 1;
  67. }
  68. r = fi->fsm->jumpmatrix[fi->fsm->state_count * event + fi->state];
  69. if (r) {
  70. if (fi->debug)
  71. fi->printdebug(fi, "State %s Event %s",
  72. fi->fsm->strState[fi->state],
  73. fi->fsm->strEvent[event]);
  74. r(fi, event, arg);
  75. return 0;
  76. } else {
  77. if (fi->debug)
  78. fi->printdebug(fi, "State %s Event %s no action",
  79. fi->fsm->strState[fi->state],
  80. fi->fsm->strEvent[event]);
  81. return 1;
  82. }
  83. }
  84. EXPORT_SYMBOL(mISDN_FsmEvent);
  85. void
  86. mISDN_FsmChangeState(struct FsmInst *fi, int newstate)
  87. {
  88. fi->state = newstate;
  89. if (fi->debug)
  90. fi->printdebug(fi, "ChangeState %s",
  91. fi->fsm->strState[newstate]);
  92. }
  93. EXPORT_SYMBOL(mISDN_FsmChangeState);
  94. static void
  95. FsmExpireTimer(struct timer_list *t)
  96. {
  97. struct FsmTimer *ft = from_timer(ft, t, tl);
  98. #if FSM_TIMER_DEBUG
  99. if (ft->fi->debug)
  100. ft->fi->printdebug(ft->fi, "FsmExpireTimer %lx", (long) ft);
  101. #endif
  102. mISDN_FsmEvent(ft->fi, ft->event, ft->arg);
  103. }
  104. void
  105. mISDN_FsmInitTimer(struct FsmInst *fi, struct FsmTimer *ft)
  106. {
  107. ft->fi = fi;
  108. #if FSM_TIMER_DEBUG
  109. if (ft->fi->debug)
  110. ft->fi->printdebug(ft->fi, "mISDN_FsmInitTimer %lx", (long) ft);
  111. #endif
  112. timer_setup(&ft->tl, FsmExpireTimer, 0);
  113. }
  114. EXPORT_SYMBOL(mISDN_FsmInitTimer);
  115. void
  116. mISDN_FsmDelTimer(struct FsmTimer *ft, int where)
  117. {
  118. #if FSM_TIMER_DEBUG
  119. if (ft->fi->debug)
  120. ft->fi->printdebug(ft->fi, "mISDN_FsmDelTimer %lx %d",
  121. (long) ft, where);
  122. #endif
  123. del_timer(&ft->tl);
  124. }
  125. EXPORT_SYMBOL(mISDN_FsmDelTimer);
  126. int
  127. mISDN_FsmAddTimer(struct FsmTimer *ft,
  128. int millisec, int event, void *arg, int where)
  129. {
  130. #if FSM_TIMER_DEBUG
  131. if (ft->fi->debug)
  132. ft->fi->printdebug(ft->fi, "mISDN_FsmAddTimer %lx %d %d",
  133. (long) ft, millisec, where);
  134. #endif
  135. if (timer_pending(&ft->tl)) {
  136. if (ft->fi->debug) {
  137. printk(KERN_WARNING
  138. "mISDN_FsmAddTimer: timer already active!\n");
  139. ft->fi->printdebug(ft->fi,
  140. "mISDN_FsmAddTimer already active!");
  141. }
  142. return -1;
  143. }
  144. ft->event = event;
  145. ft->arg = arg;
  146. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  147. add_timer(&ft->tl);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(mISDN_FsmAddTimer);
  151. void
  152. mISDN_FsmRestartTimer(struct FsmTimer *ft,
  153. int millisec, int event, void *arg, int where)
  154. {
  155. #if FSM_TIMER_DEBUG
  156. if (ft->fi->debug)
  157. ft->fi->printdebug(ft->fi, "mISDN_FsmRestartTimer %lx %d %d",
  158. (long) ft, millisec, where);
  159. #endif
  160. if (timer_pending(&ft->tl))
  161. del_timer(&ft->tl);
  162. ft->event = event;
  163. ft->arg = arg;
  164. ft->tl.expires = jiffies + (millisec * HZ) / 1000;
  165. add_timer(&ft->tl);
  166. }
  167. EXPORT_SYMBOL(mISDN_FsmRestartTimer);