fsm.c 5.0 KB

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