fsm.c 5.0 KB

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