fsm.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2010 Red Hat
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef FSM_H_DEFINED
  35. #define FSM_H_DEFINED
  36. #include <sys/time.h>
  37. #include <corosync/corotypes.h>
  38. #include "util.h"
  39. struct cs_fsm;
  40. struct cs_fsm_entry;
  41. typedef void (*cs_fsm_event_action_fn)(struct cs_fsm* fsm, int32_t event, void * data);
  42. typedef const char * (*cs_fsm_state_to_str_fn)(struct cs_fsm* fsm, int32_t state);
  43. typedef const char * (*cs_fsm_event_to_str_fn)(struct cs_fsm* fsm, int32_t event);
  44. #define CS_FSM_NEXT_STATE_SIZE 32
  45. struct cs_fsm_entry {
  46. int32_t curr_state;
  47. int32_t event;
  48. cs_fsm_event_action_fn handler_fn;
  49. int32_t next_states[CS_FSM_NEXT_STATE_SIZE];
  50. };
  51. struct cs_fsm {
  52. const char *name;
  53. int32_t curr_state;
  54. int32_t curr_entry;
  55. size_t entries;
  56. struct cs_fsm_entry *table;
  57. cs_fsm_state_to_str_fn state_to_str;
  58. cs_fsm_event_to_str_fn event_to_str;
  59. };
  60. /*
  61. * the table entry is defined by the state + event (curr_entry).
  62. * so cs_fsm_process() sets the entry and cs_fsm_state_set()
  63. * sets the new state.
  64. */
  65. static inline void cs_fsm_process (struct cs_fsm *fsm, int32_t new_event, void * data)
  66. {
  67. int32_t i;
  68. for (i = 0; i < fsm->entries; i++) {
  69. if (fsm->table[i].event == new_event &&
  70. fsm->table[i].curr_state == fsm->curr_state) {
  71. assert (fsm->table[i].handler_fn != NULL);
  72. /* set current entry */
  73. fsm->curr_entry = i;
  74. fsm->table[i].handler_fn (fsm, new_event, data);
  75. return;
  76. }
  77. }
  78. log_printf (LOGSYS_LEVEL_ERROR, "Fsm:%s could not find event \"%s\" in state \"%s\"",
  79. fsm->name, fsm->event_to_str(fsm, new_event), fsm->state_to_str(fsm, fsm->curr_state));
  80. corosync_exit_error(AIS_DONE_FATAL_ERR);
  81. }
  82. static inline void cs_fsm_state_set (struct cs_fsm* fsm, int32_t next_state, void* data)
  83. {
  84. int i;
  85. struct cs_fsm_entry *entry = &fsm->table[fsm->curr_entry];
  86. if (fsm->curr_state == next_state) {
  87. return;
  88. }
  89. /*
  90. * confirm that "next_state" is in the current entry's next list
  91. */
  92. for (i = 0; i < CS_FSM_NEXT_STATE_SIZE; i++) {
  93. if (entry->next_states[i] < 0) {
  94. break;
  95. }
  96. if (entry->next_states[i] == next_state) {
  97. log_printf (LOGSYS_LEVEL_INFO, "Fsm:%s event \"%s\", state \"%s\" --> \"%s\"\n",
  98. fsm->name,
  99. fsm->event_to_str(fsm, entry->event),
  100. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  101. fsm->state_to_str(fsm, next_state));
  102. fsm->curr_state = next_state;
  103. return;
  104. }
  105. }
  106. log_printf (LOGSYS_LEVEL_CRIT, "Fsm:%s Can't change state from \"%s\" to \"%s\" (event was \"%s\")\n",
  107. fsm->name,
  108. fsm->state_to_str(fsm, fsm->table[fsm->curr_entry].curr_state),
  109. fsm->state_to_str(fsm, next_state),
  110. fsm->event_to_str(fsm, entry->event));
  111. corosync_exit_error(AIS_DONE_FATAL_ERR);
  112. }
  113. #endif /* FSM_H_DEFINED */