state_machine.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * wpa_supplicant/hostapd - State machine definitions
  3. * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file includes a set of pre-processor macros that can be used to
  9. * implement a state machine. In addition to including this header file, each
  10. * file implementing a state machine must define STATE_MACHINE_DATA to be the
  11. * data structure including state variables (enum machine_state,
  12. * Boolean changed), and STATE_MACHINE_DEBUG_PREFIX to be a string that is used
  13. * as a prefix for all debug messages. If SM_ENTRY_MA macro is used to define
  14. * a group of state machines with shared data structure, STATE_MACHINE_ADDR
  15. * needs to be defined to point to the MAC address used in debug output.
  16. * SM_ENTRY_M macro can be used to define similar group of state machines
  17. * without this additional debug info.
  18. */
  19. #ifndef STATE_MACHINE_H
  20. #define STATE_MACHINE_H
  21. /**
  22. * SM_STATE - Declaration of a state machine function
  23. * @machine: State machine name
  24. * @state: State machine state
  25. *
  26. * This macro is used to declare a state machine function. It is used in place
  27. * of a C function definition to declare functions to be run when the state is
  28. * entered by calling SM_ENTER or SM_ENTER_GLOBAL.
  29. */
  30. #define SM_STATE(machine, state) \
  31. static void sm_ ## machine ## _ ## state ## _Enter(STATE_MACHINE_DATA *sm, \
  32. int global)
  33. /**
  34. * SM_ENTRY - State machine function entry point
  35. * @machine: State machine name
  36. * @state: State machine state
  37. *
  38. * This macro is used inside each state machine function declared with
  39. * SM_STATE. SM_ENTRY should be in the beginning of the function body, but
  40. * after declaration of possible local variables. This macro prints debug
  41. * information about state transition and update the state machine state.
  42. */
  43. #define SM_ENTRY(machine, state) \
  44. if (!global || sm->machine ## _state != machine ## _ ## state) { \
  45. sm->changed = TRUE; \
  46. wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " #machine \
  47. " entering state " #state); \
  48. } \
  49. sm->machine ## _state = machine ## _ ## state;
  50. /**
  51. * SM_ENTRY_M - State machine function entry point for state machine group
  52. * @machine: State machine name
  53. * @_state: State machine state
  54. * @data: State variable prefix (full variable: prefix_state)
  55. *
  56. * This macro is like SM_ENTRY, but for state machine groups that use a shared
  57. * data structure for more than one state machine. Both machine and prefix
  58. * parameters are set to "sub-state machine" name. prefix is used to allow more
  59. * than one state variable to be stored in the same data structure.
  60. */
  61. #define SM_ENTRY_M(machine, _state, data) \
  62. if (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
  63. sm->changed = TRUE; \
  64. wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " \
  65. #machine " entering state " #_state); \
  66. } \
  67. sm->data ## _ ## state = machine ## _ ## _state;
  68. /**
  69. * SM_ENTRY_MA - State machine function entry point for state machine group
  70. * @machine: State machine name
  71. * @_state: State machine state
  72. * @data: State variable prefix (full variable: prefix_state)
  73. *
  74. * This macro is like SM_ENTRY_M, but a MAC address is included in debug
  75. * output. STATE_MACHINE_ADDR has to be defined to point to the MAC address to
  76. * be included in debug.
  77. */
  78. #define SM_ENTRY_MA(machine, _state, data) \
  79. if (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
  80. sm->changed = TRUE; \
  81. wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " MACSTR " " \
  82. #machine " entering state " #_state, \
  83. MAC2STR(STATE_MACHINE_ADDR)); \
  84. } \
  85. sm->data ## _ ## state = machine ## _ ## _state;
  86. /**
  87. * SM_ENTER - Enter a new state machine state
  88. * @machine: State machine name
  89. * @state: State machine state
  90. *
  91. * This macro expands to a function call to a state machine function defined
  92. * with SM_STATE macro. SM_ENTER is used in a state machine step function to
  93. * move the state machine to a new state.
  94. */
  95. #define SM_ENTER(machine, state) \
  96. sm_ ## machine ## _ ## state ## _Enter(sm, 0)
  97. /**
  98. * SM_ENTER_GLOBAL - Enter a new state machine state based on global rule
  99. * @machine: State machine name
  100. * @state: State machine state
  101. *
  102. * This macro is like SM_ENTER, but this is used when entering a new state
  103. * based on a global (not specific to any particular state) rule. A separate
  104. * macro is used to avoid unwanted debug message floods when the same global
  105. * rule is forcing a state machine to remain in on state.
  106. */
  107. #define SM_ENTER_GLOBAL(machine, state) \
  108. sm_ ## machine ## _ ## state ## _Enter(sm, 1)
  109. /**
  110. * SM_STEP - Declaration of a state machine step function
  111. * @machine: State machine name
  112. *
  113. * This macro is used to declare a state machine step function. It is used in
  114. * place of a C function definition to declare a function that is used to move
  115. * state machine to a new state based on state variables. This function uses
  116. * SM_ENTER and SM_ENTER_GLOBAL macros to enter new state.
  117. */
  118. #define SM_STEP(machine) \
  119. static void sm_ ## machine ## _Step(STATE_MACHINE_DATA *sm)
  120. /**
  121. * SM_STEP_RUN - Call the state machine step function
  122. * @machine: State machine name
  123. *
  124. * This macro expands to a function call to a state machine step function
  125. * defined with SM_STEP macro.
  126. */
  127. #define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
  128. #endif /* STATE_MACHINE_H */