fsm.c 4.8 KB

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