fsm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, gfp_t 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. kfree(this->f->jumpmatrix);
  74. kfree(this->f);
  75. }
  76. kfree(this);
  77. } else
  78. printk(KERN_WARNING
  79. "fsm: kfree_fsm called with NULL argument\n");
  80. }
  81. #if FSM_DEBUG_HISTORY
  82. void
  83. fsm_print_history(fsm_instance *fi)
  84. {
  85. int idx = 0;
  86. int i;
  87. if (fi->history_size >= FSM_HISTORY_SIZE)
  88. idx = fi->history_index;
  89. printk(KERN_DEBUG "fsm(%s): History:\n", fi->name);
  90. for (i = 0; i < fi->history_size; i++) {
  91. int e = fi->history[idx].event;
  92. int s = fi->history[idx++].state;
  93. idx %= FSM_HISTORY_SIZE;
  94. if (e == -1)
  95. printk(KERN_DEBUG " S=%s\n",
  96. fi->f->state_names[s]);
  97. else
  98. printk(KERN_DEBUG " S=%s E=%s\n",
  99. fi->f->state_names[s],
  100. fi->f->event_names[e]);
  101. }
  102. fi->history_size = fi->history_index = 0;
  103. }
  104. void
  105. fsm_record_history(fsm_instance *fi, int state, int event)
  106. {
  107. fi->history[fi->history_index].state = state;
  108. fi->history[fi->history_index++].event = event;
  109. fi->history_index %= FSM_HISTORY_SIZE;
  110. if (fi->history_size < FSM_HISTORY_SIZE)
  111. fi->history_size++;
  112. }
  113. #endif
  114. const char *
  115. fsm_getstate_str(fsm_instance *fi)
  116. {
  117. int st = atomic_read(&fi->state);
  118. if (st >= fi->f->nr_states)
  119. return "Invalid";
  120. return fi->f->state_names[st];
  121. }
  122. static void
  123. fsm_expire_timer(fsm_timer *this)
  124. {
  125. #if FSM_TIMER_DEBUG
  126. printk(KERN_DEBUG "fsm(%s): Timer %p expired\n",
  127. this->fi->name, this);
  128. #endif
  129. fsm_event(this->fi, this->expire_event, this->event_arg);
  130. }
  131. void
  132. fsm_settimer(fsm_instance *fi, fsm_timer *this)
  133. {
  134. this->fi = fi;
  135. this->tl.function = (void *)fsm_expire_timer;
  136. this->tl.data = (long)this;
  137. #if FSM_TIMER_DEBUG
  138. printk(KERN_DEBUG "fsm(%s): Create timer %p\n", fi->name,
  139. this);
  140. #endif
  141. init_timer(&this->tl);
  142. }
  143. void
  144. fsm_deltimer(fsm_timer *this)
  145. {
  146. #if FSM_TIMER_DEBUG
  147. printk(KERN_DEBUG "fsm(%s): Delete timer %p\n", this->fi->name,
  148. this);
  149. #endif
  150. del_timer(&this->tl);
  151. }
  152. int
  153. fsm_addtimer(fsm_timer *this, int millisec, int event, void *arg)
  154. {
  155. #if FSM_TIMER_DEBUG
  156. printk(KERN_DEBUG "fsm(%s): Add timer %p %dms\n",
  157. this->fi->name, this, millisec);
  158. #endif
  159. init_timer(&this->tl);
  160. this->tl.function = (void *)fsm_expire_timer;
  161. this->tl.data = (long)this;
  162. this->expire_event = event;
  163. this->event_arg = arg;
  164. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  165. add_timer(&this->tl);
  166. return 0;
  167. }
  168. /* FIXME: this function is never used, why */
  169. void
  170. fsm_modtimer(fsm_timer *this, int millisec, int event, void *arg)
  171. {
  172. #if FSM_TIMER_DEBUG
  173. printk(KERN_DEBUG "fsm(%s): Restart timer %p %dms\n",
  174. this->fi->name, this, millisec);
  175. #endif
  176. del_timer(&this->tl);
  177. init_timer(&this->tl);
  178. this->tl.function = (void *)fsm_expire_timer;
  179. this->tl.data = (long)this;
  180. this->expire_event = event;
  181. this->event_arg = arg;
  182. this->tl.expires = jiffies + (millisec * HZ) / 1000;
  183. add_timer(&this->tl);
  184. }
  185. EXPORT_SYMBOL(init_fsm);
  186. EXPORT_SYMBOL(kfree_fsm);
  187. EXPORT_SYMBOL(fsm_settimer);
  188. EXPORT_SYMBOL(fsm_deltimer);
  189. EXPORT_SYMBOL(fsm_addtimer);
  190. EXPORT_SYMBOL(fsm_modtimer);
  191. EXPORT_SYMBOL(fsm_getstate_str);
  192. #if FSM_DEBUG_HISTORY
  193. EXPORT_SYMBOL(fsm_print_history);
  194. EXPORT_SYMBOL(fsm_record_history);
  195. #endif