117680Spst/*
239300Sfenner * wpa_supplicant/hostapd - State machine definitions
317680Spst * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
417680Spst *
517680Spst * This software may be distributed under the terms of the BSD license.
617680Spst * See README for more details.
717680Spst *
817680Spst * This file includes a set of pre-processor macros that can be used to
917680Spst * implement a state machine. In addition to including this header file, each
1017680Spst * file implementing a state machine must define STATE_MACHINE_DATA to be the
1117680Spst * data structure including state variables (enum machine_state,
1217680Spst * Boolean changed), and STATE_MACHINE_DEBUG_PREFIX to be a string that is used
1317680Spst * as a prefix for all debug messages. If SM_ENTRY_MA macro is used to define
1417680Spst * a group of state machines with shared data structure, STATE_MACHINE_ADDR
1517680Spst * needs to be defined to point to the MAC address used in debug output.
1617680Spst * SM_ENTRY_M macro can be used to define similar group of state machines
1717680Spst * without this additional debug info.
1817680Spst */
1917680Spst
2017680Spst#ifndef STATE_MACHINE_H
2117680Spst#define STATE_MACHINE_H
2256896Sfenner
2356896Sfenner/**
2417680Spst * SM_STATE - Declaration of a state machine function
2517680Spst * @machine: State machine name
26127675Sbms * @state: State machine state
27127675Sbms *
2817680Spst * This macro is used to declare a state machine function. It is used in place
2917680Spst * of a C function definition to declare functions to be run when the state is
3056896Sfenner * entered by calling SM_ENTER or SM_ENTER_GLOBAL.
3156896Sfenner */
3256896Sfenner#define SM_STATE(machine, state) \
3356896Sfennerstatic void sm_ ## machine ## _ ## state ## _Enter(STATE_MACHINE_DATA *sm, \
34127675Sbms	int global)
3517680Spst
3617680Spst/**
3717680Spst * SM_ENTRY - State machine function entry point
3817680Spst * @machine: State machine name
3917680Spst * @state: State machine state
4017680Spst *
4175118Sfenner * This macro is used inside each state machine function declared with
4275118Sfenner * SM_STATE. SM_ENTRY should be in the beginning of the function body, but
4317680Spst * after declaration of possible local variables. This macro prints debug
4417680Spst * information about state transition and update the state machine state.
4598527Sfenner */
4698527Sfenner#define SM_ENTRY(machine, state) \
4717680Spstif (!global || sm->machine ## _state != machine ## _ ## state) { \
4817680Spst	sm->changed = TRUE; \
4917680Spst	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " #machine \
50127675Sbms		   " entering state " #state); \
51127675Sbms} \
52127675Sbmssm->machine ## _state = machine ## _ ## state;
53127675Sbms
54127675Sbms/**
55127675Sbms * SM_ENTRY_M - State machine function entry point for state machine group
56127675Sbms * @machine: State machine name
57127675Sbms * @_state: State machine state
58127675Sbms * @data: State variable prefix (full variable: prefix_state)
59127675Sbms *
60127675Sbms * This macro is like SM_ENTRY, but for state machine groups that use a shared
6117680Spst * data structure for more than one state machine. Both machine and prefix
6217680Spst * parameters are set to "sub-state machine" name. prefix is used to allow more
6317680Spst * than one state variable to be stored in the same data structure.
6417680Spst */
65127675Sbms#define SM_ENTRY_M(machine, _state, data) \
6617680Spstif (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
6717680Spst	sm->changed = TRUE; \
6898527Sfenner	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " \
6998527Sfenner		   #machine " entering state " #_state); \
7017680Spst} \
7198527Sfennersm->data ## _ ## state = machine ## _ ## _state;
7217680Spst
7317680Spst/**
74127675Sbms * SM_ENTRY_MA - State machine function entry point for state machine group
75127675Sbms * @machine: State machine name
7617680Spst * @_state: State machine state
77127675Sbms * @data: State variable prefix (full variable: prefix_state)
78127675Sbms *
79127675Sbms * This macro is like SM_ENTRY_M, but a MAC address is included in debug
8017680Spst * output. STATE_MACHINE_ADDR has to be defined to point to the MAC address to
8117680Spst * be included in debug.
82127675Sbms */
83127675Sbms#define SM_ENTRY_MA(machine, _state, data) \
84127675Sbmsif (!global || sm->data ## _ ## state != machine ## _ ## _state) { \
85127675Sbms	sm->changed = TRUE; \
86127675Sbms	wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " MACSTR " " \
8717680Spst		   #machine " entering state " #_state, \
8817680Spst		   MAC2STR(STATE_MACHINE_ADDR)); \
8917680Spst} \
9017680Spstsm->data ## _ ## state = machine ## _ ## _state;
91127675Sbms
9217680Spst/**
9317680Spst * SM_ENTER - Enter a new state machine state
9417680Spst * @machine: State machine name
95127675Sbms * @state: State machine state
9617680Spst *
9717680Spst * This macro expands to a function call to a state machine function defined
9817680Spst * with SM_STATE macro. SM_ENTER is used in a state machine step function to
99127675Sbms * move the state machine to a new state.
10017680Spst */
101127675Sbms#define SM_ENTER(machine, state) \
10217680Spstsm_ ## machine ## _ ## state ## _Enter(sm, 0)
103127675Sbms
10417680Spst/**
105127675Sbms * SM_ENTER_GLOBAL - Enter a new state machine state based on global rule
106127675Sbms * @machine: State machine name
107127675Sbms * @state: State machine state
108127675Sbms *
109127675Sbms * This macro is like SM_ENTER, but this is used when entering a new state
11017680Spst * based on a global (not specific to any particular state) rule. A separate
11117680Spst * macro is used to avoid unwanted debug message floods when the same global
11217680Spst * rule is forcing a state machine to remain in on state.
113127675Sbms */
11417680Spst#define SM_ENTER_GLOBAL(machine, state) \
11517680Spstsm_ ## machine ## _ ## state ## _Enter(sm, 1)
11617680Spst
11717680Spst/**
118127675Sbms * SM_STEP - Declaration of a state machine step function
11917680Spst * @machine: State machine name
12017680Spst *
12117680Spst * This macro is used to declare a state machine step function. It is used in
12217680Spst * place of a C function definition to declare a function that is used to move
123127675Sbms * state machine to a new state based on state variables. This function uses
12417680Spst * SM_ENTER and SM_ENTER_GLOBAL macros to enter new state.
12517680Spst */
12617680Spst#define SM_STEP(machine) \
12717680Spststatic void sm_ ## machine ## _Step(STATE_MACHINE_DATA *sm)
128127675Sbms
12917680Spst/**
13017680Spst * SM_STEP_RUN - Call the state machine step function
13117680Spst * @machine: State machine name
13217680Spst *
133127675Sbms * This macro expands to a function call to a state machine step function
13417680Spst * defined with SM_STEP macro.
13517680Spst */
13617680Spst#define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
13717680Spst
138127675Sbms#endif /* STATE_MACHINE_H */
13917680Spst