fsm.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2010-2012 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. typedef void (*cs_fsm_cb)(struct cs_fsm *fsm, int cb_event, int32_t curr_state,
  45. int32_t next_state, int32_t fsm_event, void *data);
  46. #define CS_FSM_NEXT_STATE_SIZE 32
  47. #define CS_FSM_STATE_NONE -1
  48. #define CS_FSM_CB_EVENT_PROCESS_NF 0
  49. #define CS_FSM_CB_EVENT_STATE_SET 1
  50. #define CS_FSM_CB_EVENT_STATE_SET_NF 2
  51. struct cs_fsm_entry {
  52. int32_t curr_state;
  53. int32_t event;
  54. cs_fsm_event_action_fn handler_fn;
  55. int32_t next_states[CS_FSM_NEXT_STATE_SIZE];
  56. };
  57. struct cs_fsm {
  58. const char *name;
  59. int32_t curr_state;
  60. int32_t curr_entry;
  61. size_t entries;
  62. struct cs_fsm_entry *table;
  63. cs_fsm_state_to_str_fn state_to_str;
  64. cs_fsm_event_to_str_fn event_to_str;
  65. };
  66. /*
  67. * the table entry is defined by the state + event (curr_entry).
  68. * so cs_fsm_process() sets the entry and cs_fsm_state_set()
  69. * sets the new state.
  70. */
  71. static inline void cs_fsm_process (struct cs_fsm *fsm, int32_t new_event, void * data, cs_fsm_cb cb)
  72. {
  73. int32_t i;
  74. for (i = 0; i < fsm->entries; i++) {
  75. if (fsm->table[i].event == new_event &&
  76. fsm->table[i].curr_state == fsm->curr_state) {
  77. assert (fsm->table[i].handler_fn != NULL);
  78. /* set current entry */
  79. fsm->curr_entry = i;
  80. fsm->table[i].handler_fn (fsm, new_event, data);
  81. return;
  82. }
  83. }
  84. if (cb != NULL) {
  85. cb(fsm, CS_FSM_CB_EVENT_PROCESS_NF, fsm->curr_state, CS_FSM_STATE_NONE, new_event, data);
  86. }
  87. }
  88. static inline void cs_fsm_state_set (struct cs_fsm* fsm, int32_t next_state, void* data, cs_fsm_cb cb)
  89. {
  90. int i;
  91. struct cs_fsm_entry *entry = &fsm->table[fsm->curr_entry];
  92. if (fsm->curr_state == next_state) {
  93. return;
  94. }
  95. /*
  96. * confirm that "next_state" is in the current entry's next list
  97. */
  98. for (i = 0; i < CS_FSM_NEXT_STATE_SIZE; i++) {
  99. if (entry->next_states[i] < 0) {
  100. break;
  101. }
  102. if (entry->next_states[i] == next_state) {
  103. if (cb != NULL) {
  104. cb(fsm, CS_FSM_CB_EVENT_STATE_SET, fsm->curr_state, next_state, entry->event, data);
  105. }
  106. fsm->curr_state = next_state;
  107. return;
  108. }
  109. }
  110. if (cb != NULL) {
  111. cb(fsm, CS_FSM_CB_EVENT_STATE_SET_NF, fsm->curr_state, next_state, entry->event, data);
  112. }
  113. }
  114. #endif /* FSM_H_DEFINED */