xref: /netbsd-src/external/bsd/wpa/dist/src/utils/state_machine.h (revision bb6183629cf165db498d8e1f4e2de129f7efb21c)
18dbcf02cSchristos /*
28dbcf02cSchristos  * wpa_supplicant/hostapd - State machine definitions
38dbcf02cSchristos  * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
78dbcf02cSchristos  *
88dbcf02cSchristos  * This file includes a set of pre-processor macros that can be used to
98dbcf02cSchristos  * implement a state machine. In addition to including this header file, each
108dbcf02cSchristos  * file implementing a state machine must define STATE_MACHINE_DATA to be the
118dbcf02cSchristos  * data structure including state variables (enum machine_state,
12*bb618362Schristos  * bool changed), and STATE_MACHINE_DEBUG_PREFIX to be a string that is used
138dbcf02cSchristos  * as a prefix for all debug messages. If SM_ENTRY_MA macro is used to define
148dbcf02cSchristos  * a group of state machines with shared data structure, STATE_MACHINE_ADDR
158dbcf02cSchristos  * needs to be defined to point to the MAC address used in debug output.
168dbcf02cSchristos  * SM_ENTRY_M macro can be used to define similar group of state machines
178dbcf02cSchristos  * without this additional debug info.
188dbcf02cSchristos  */
198dbcf02cSchristos 
208dbcf02cSchristos #ifndef STATE_MACHINE_H
218dbcf02cSchristos #define STATE_MACHINE_H
228dbcf02cSchristos 
238dbcf02cSchristos /**
248dbcf02cSchristos  * SM_STATE - Declaration of a state machine function
258dbcf02cSchristos  * @machine: State machine name
268dbcf02cSchristos  * @state: State machine state
278dbcf02cSchristos  *
288dbcf02cSchristos  * This macro is used to declare a state machine function. It is used in place
298dbcf02cSchristos  * of a C function definition to declare functions to be run when the state is
308dbcf02cSchristos  * entered by calling SM_ENTER or SM_ENTER_GLOBAL.
318dbcf02cSchristos  */
328dbcf02cSchristos #define SM_STATE(machine, state) \
338dbcf02cSchristos static void sm_ ## machine ## _ ## state ## _Enter(STATE_MACHINE_DATA *sm, \
348dbcf02cSchristos 	int global)
358dbcf02cSchristos 
368dbcf02cSchristos /**
378dbcf02cSchristos  * SM_ENTRY - State machine function entry point
388dbcf02cSchristos  * @machine: State machine name
398dbcf02cSchristos  * @state: State machine state
408dbcf02cSchristos  *
418dbcf02cSchristos  * This macro is used inside each state machine function declared with
428dbcf02cSchristos  * SM_STATE. SM_ENTRY should be in the beginning of the function body, but
438dbcf02cSchristos  * after declaration of possible local variables. This macro prints debug
448dbcf02cSchristos  * information about state transition and update the state machine state.
458dbcf02cSchristos  */
468dbcf02cSchristos #define SM_ENTRY(machine, state) \
478dbcf02cSchristos if (!global || sm->machine ## _state != machine ## _ ## state) { \
48*bb618362Schristos 	sm->changed = true; \
498dbcf02cSchristos 	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " #machine \
508dbcf02cSchristos 		   " entering state " #state); \
518dbcf02cSchristos } \
528dbcf02cSchristos sm->machine ## _state = machine ## _ ## state;
538dbcf02cSchristos 
548dbcf02cSchristos /**
558dbcf02cSchristos  * SM_ENTRY_M - State machine function entry point for state machine group
568dbcf02cSchristos  * @machine: State machine name
578dbcf02cSchristos  * @_state: State machine state
588dbcf02cSchristos  * @data: State variable prefix (full variable: prefix_state)
598dbcf02cSchristos  *
608dbcf02cSchristos  * This macro is like SM_ENTRY, but for state machine groups that use a shared
618dbcf02cSchristos  * data structure for more than one state machine. Both machine and prefix
628dbcf02cSchristos  * parameters are set to "sub-state machine" name. prefix is used to allow more
638dbcf02cSchristos  * than one state variable to be stored in the same data structure.
648dbcf02cSchristos  */
658dbcf02cSchristos #define SM_ENTRY_M(machine, _state, data) \
668dbcf02cSchristos if (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
67*bb618362Schristos 	sm->changed = true; \
688dbcf02cSchristos 	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " \
698dbcf02cSchristos 		   #machine " entering state " #_state); \
708dbcf02cSchristos } \
718dbcf02cSchristos sm->data ## _ ## state = machine ## _ ## _state;
728dbcf02cSchristos 
738dbcf02cSchristos /**
748dbcf02cSchristos  * SM_ENTRY_MA - State machine function entry point for state machine group
758dbcf02cSchristos  * @machine: State machine name
768dbcf02cSchristos  * @_state: State machine state
778dbcf02cSchristos  * @data: State variable prefix (full variable: prefix_state)
788dbcf02cSchristos  *
798dbcf02cSchristos  * This macro is like SM_ENTRY_M, but a MAC address is included in debug
808dbcf02cSchristos  * output. STATE_MACHINE_ADDR has to be defined to point to the MAC address to
818dbcf02cSchristos  * be included in debug.
828dbcf02cSchristos  */
838dbcf02cSchristos #define SM_ENTRY_MA(machine, _state, data) \
848dbcf02cSchristos if (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
85*bb618362Schristos 	sm->changed = true; \
868dbcf02cSchristos 	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " MACSTR " " \
878dbcf02cSchristos 		   #machine " entering state " #_state, \
888dbcf02cSchristos 		   MAC2STR(STATE_MACHINE_ADDR)); \
898dbcf02cSchristos } \
908dbcf02cSchristos sm->data ## _ ## state = machine ## _ ## _state;
918dbcf02cSchristos 
928dbcf02cSchristos /**
938dbcf02cSchristos  * SM_ENTER - Enter a new state machine state
948dbcf02cSchristos  * @machine: State machine name
958dbcf02cSchristos  * @state: State machine state
968dbcf02cSchristos  *
978dbcf02cSchristos  * This macro expands to a function call to a state machine function defined
988dbcf02cSchristos  * with SM_STATE macro. SM_ENTER is used in a state machine step function to
998dbcf02cSchristos  * move the state machine to a new state.
1008dbcf02cSchristos  */
1018dbcf02cSchristos #define SM_ENTER(machine, state) \
1028dbcf02cSchristos sm_ ## machine ## _ ## state ## _Enter(sm, 0)
1038dbcf02cSchristos 
1048dbcf02cSchristos /**
1058dbcf02cSchristos  * SM_ENTER_GLOBAL - Enter a new state machine state based on global rule
1068dbcf02cSchristos  * @machine: State machine name
1078dbcf02cSchristos  * @state: State machine state
1088dbcf02cSchristos  *
1098dbcf02cSchristos  * This macro is like SM_ENTER, but this is used when entering a new state
1108dbcf02cSchristos  * based on a global (not specific to any particular state) rule. A separate
1118dbcf02cSchristos  * macro is used to avoid unwanted debug message floods when the same global
1128dbcf02cSchristos  * rule is forcing a state machine to remain in on state.
1138dbcf02cSchristos  */
1148dbcf02cSchristos #define SM_ENTER_GLOBAL(machine, state) \
1158dbcf02cSchristos sm_ ## machine ## _ ## state ## _Enter(sm, 1)
1168dbcf02cSchristos 
1178dbcf02cSchristos /**
1188dbcf02cSchristos  * SM_STEP - Declaration of a state machine step function
1198dbcf02cSchristos  * @machine: State machine name
1208dbcf02cSchristos  *
1218dbcf02cSchristos  * This macro is used to declare a state machine step function. It is used in
1228dbcf02cSchristos  * place of a C function definition to declare a function that is used to move
1238dbcf02cSchristos  * state machine to a new state based on state variables. This function uses
1248dbcf02cSchristos  * SM_ENTER and SM_ENTER_GLOBAL macros to enter new state.
1258dbcf02cSchristos  */
1268dbcf02cSchristos #define SM_STEP(machine) \
1278dbcf02cSchristos static void sm_ ## machine ## _Step(STATE_MACHINE_DATA *sm)
1288dbcf02cSchristos 
1298dbcf02cSchristos /**
1308dbcf02cSchristos  * SM_STEP_RUN - Call the state machine step function
1318dbcf02cSchristos  * @machine: State machine name
1328dbcf02cSchristos  *
1338dbcf02cSchristos  * This macro expands to a function call to a state machine step function
1348dbcf02cSchristos  * defined with SM_STEP macro.
1358dbcf02cSchristos  */
1368dbcf02cSchristos #define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
1378dbcf02cSchristos 
1388dbcf02cSchristos #endif /* STATE_MACHINE_H */
139