fsm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * A generic FSM based on fsm used in isdn4linux
  3. *
  4. */
  5. #include "fsm.h"
  6. #include <linux/config.h>
  7. #include <linux/module.h>
  8. #include <linux/timer.h>
  9. MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
  10. MODULE_DESCRIPTION("Finite state machine helper functions");
  11. MODULE_LICENSE("GPL");
  12. fsm_instance *
  13. init_fsm(char *name, const char **state_names, const char **event_names, int nr_states,
  14. int nr_events, const fsm_node *tmpl, int tmpl_len, gfp_t order)
  15. {
  16. int i;
  17. fsm_instance *this;
  18. fsm_function_t *m;
  19. fsm *f;
  20. this = kzalloc(sizeof(fsm_instance), order);
  21. if (this == NULL) {
  22. printk(KERN_WARNING
  23. "fsm(%s): init_fsm: Couldn't alloc instance\n", name);
  24. return NULL;
  25. }
  26. strlcpy(this->name, name, sizeof(this->name));
  27. f = kzalloc(sizeof(fsm), order);
  28. if (f == NULL) {
  29. printk(KERN_WARNING
  30. "fsm(%s): init_fsm: Couldn't alloc fsm\n", name);
  31. kfree_fsm(this);
  32. return NULL;
  33. }
  34. f->nr_events = nr_events;
  35. f->nr_states = nr_states;
  36. f->event_names = event_names;
  37. f->state_names = state_names;
  38. this->f = f;
  39. m = kcalloc(nr_states*nr_events, sizeof(fsm_function_t), order);
  40. if (m == NULL) {
  41. printk(KERN_WARNING
  42. "fsm(%s): init_fsm: Couldn't alloc jumptable\n", name);
  43. kfree_fsm(this);
  44. return NULL;
  45. }
  46. f->jumpmatrix = m;
  47. for (i = 0; i < tmpl_len; i++) {
  48. if ((tmpl[i].cond_state >= nr_states) ||
  49. (tmpl[i].cond_event >= nr_events) ) {
  50. printk(KERN_ERR
  51. "fsm(%s): init_fsm: Bad template l=%d st(%ld/%ld) ev(%ld/%ld)\n",
  52. name, i, (long)tmpl[i].cond_state, (long)f->nr_states,
  53. (long)tmpl[i].cond_event, (long)f->nr_events);
  54. kfree_fsm(this);
  55. return NULL;
  56. } else
  57. m[nr_states * tmpl[i].cond_event + tmpl[i].cond_state] =
  58. tmpl[i].function;
  59. }
  60. return this;
  61. }
  62. void
  63. kfree_fsm(fsm_instance *this)
  64. {
  65. if (this) {
  66. if (this->f) {
  67. kfree(this->f->jumpmatrix);
  68. kfree(this->f);
  69. }
  70. kfree(this);
  71. } else
  72. printk(KERN_WARNING
  73. "fsm: kfree_fsm called with NULL argument\n");
  74. }
  75. #if FSM_DEBUG_HISTORY
  76. void
  77. fsm_print_history(fsm_instance *fi)
  78. {
  79. int idx = 0;
  80. int i;
  81. if (fi->history_size >= FSM_HISTORY_SIZE)
  82. idx = fi->history_index;
  83. printk(KERN_DEBUG "fsm(%s): History:\n", fi->name);
  84. for (i = 0; i < fi->history_size; i++) {
  85. int e = fi->history[idx].event;
  86. int s = fi->history[idx++].state;
  87. idx %= FSM_HISTORY_SIZE;
  88. if (e == -1)
  89. printk(KERN_DEBUG " S=%s\n",
  90. fi->f->state_names[s]);
  91. else
  92. printk(KERN_DEBUG " S=%s E=%s\n",
  93. fi->f->state_names[s],
  94. fi->f->event_names[e]);
  95. }
  96. fi->history_size = fi->history_index = 0;
  97. }
  98. void
  99. fsm_record_history(fsm_instance *fi, int state, int event)
  100. {
  101. fi->history[fi->history_index].state = state;
  102. fi->history[fi->history_index++].event = event;
  103. fi->history_index %= FSM_HISTORY_SIZE;
  104. if (fi->history_size < FSM_HISTORY_SIZE)
  105. fi->history_size++;
  106. }
  107. #endif
  108. const char *
  109. fsm_getstate_str(fsm_instance *fi)
  110. {
  111. int st = atomic_read(&fi->state);
  112. if (st >= fi->f->nr_states)
  113. return "Invalid";
  114. return fi->f->state_names[st];
  115. }
  116. static void
  117. fsm_expire_timer(fsm_timer *this)
  118. {
  119. #if FSM_TIMER_DEBUG
  120. printk(KERN_DEBUG "fsm(%s): Timer %p expired\n",
  121. this->fi->name, this);
  122. #endif
  123. fsm_event(this->fi, this->expire_event, this->event_arg);
  124. }
  125. void
  126. fsm_settimer(fsm_instance *fi, fsm_timer *this)
  127. {
  128. this->fi = fi;
  129. this->tl.function = (void *)fsm_expire_timer;
  130. this->tl.data = (long)this;
  131. #if FSM_TIMER_DEBUG
  132. printk(KERN_DEBUG "fsm(%s): Create timer %p\n", fi->name,
  133. this);
  134. #endif
  135. init_timer(&this->tl);
  136. }
  137. void
  138. fsm_deltimer(fsm_timer *this)
  139. {
  140. #if FSM_TIMER_DEBUG
  141. printk(KERN_DEBUG "fsm(%s): Delete timer %p\n", this->fi->name,
  142. this);
  143. #endif
  144. del_timer(&this->tl);
  145. }
  146. int
  147. fsm_addtimer(fsm_timer *this, int millisec, int event, void *arg)
  148. {
  149. #if FSM_TIMER_DEBUG
  150. printk(KERN_DEBUG "fsm(%s): Add timer %p %dms\n",
  151. this->fi->name, this, millisec);
  152. #endif
  153. init_timer(&this->tl);
  154. this->tl.function = (void *)fsm_expire_timer;
  155. this->tl.data = (long)this;
  156. this->expire_event = event;
  157. this->event_arg = arg;
  158. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  159. add_timer(&this->tl);
  160. return 0;
  161. }
  162. /* FIXME: this function is never used, why */
  163. void
  164. fsm_modtimer(fsm_timer *this, int millisec, int event, void *arg)
  165. {
  166. #if FSM_TIMER_DEBUG
  167. printk(KERN_DEBUG "fsm(%s): Restart timer %p %dms\n",
  168. this->fi->name, this, millisec);
  169. #endif
  170. del_timer(&this->tl);
  171. init_timer(&this->tl);
  172. this->tl.function = (void *)fsm_expire_timer;
  173. this->tl.data = (long)this;
  174. this->expire_event = event;
  175. this->event_arg = arg;
  176. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  177. add_timer(&this->tl);
  178. }
  179. EXPORT_SYMBOL(init_fsm);
  180. EXPORT_SYMBOL(kfree_fsm);
  181. EXPORT_SYMBOL(fsm_settimer);
  182. EXPORT_SYMBOL(fsm_deltimer);
  183. EXPORT_SYMBOL(fsm_addtimer);
  184. EXPORT_SYMBOL(fsm_modtimer);
  185. EXPORT_SYMBOL(fsm_getstate_str);
  186. #if FSM_DEBUG_HISTORY
  187. EXPORT_SYMBOL(fsm_print_history);
  188. EXPORT_SYMBOL(fsm_record_history);
  189. #endif