state_machine.h 5.5 KB

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