162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira  <bristot@kernel.org>
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Deterministic automata helper functions, to be used with the automata
662306a36Sopenharmony_ci * models in C generated by the dot2k tool.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Define a set of helper functions for automata. The 'name' argument is used
1362306a36Sopenharmony_ci * as suffix for the functions and data. These functions will handle automaton
1462306a36Sopenharmony_ci * with data type 'type'.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#define DECLARE_AUTOMATA_HELPERS(name, type)					\
1762306a36Sopenharmony_ci										\
1862306a36Sopenharmony_ci/*										\
1962306a36Sopenharmony_ci * model_get_state_name_##name - return the (string) name of the given state	\
2062306a36Sopenharmony_ci */ 										\
2162306a36Sopenharmony_cistatic char *model_get_state_name_##name(enum states_##name state)		\
2262306a36Sopenharmony_ci{										\
2362306a36Sopenharmony_ci	if ((state < 0) || (state >= state_max_##name))				\
2462306a36Sopenharmony_ci		return "INVALID";						\
2562306a36Sopenharmony_ci										\
2662306a36Sopenharmony_ci	return automaton_##name.state_names[state];				\
2762306a36Sopenharmony_ci}										\
2862306a36Sopenharmony_ci										\
2962306a36Sopenharmony_ci/*										\
3062306a36Sopenharmony_ci * model_get_event_name_##name - return the (string) name of the given event	\
3162306a36Sopenharmony_ci */										\
3262306a36Sopenharmony_cistatic char *model_get_event_name_##name(enum events_##name event)		\
3362306a36Sopenharmony_ci{										\
3462306a36Sopenharmony_ci	if ((event < 0) || (event >= event_max_##name))				\
3562306a36Sopenharmony_ci		return "INVALID";						\
3662306a36Sopenharmony_ci										\
3762306a36Sopenharmony_ci	return automaton_##name.event_names[event];				\
3862306a36Sopenharmony_ci}										\
3962306a36Sopenharmony_ci										\
4062306a36Sopenharmony_ci/*										\
4162306a36Sopenharmony_ci * model_get_initial_state_##name - return the automaton's initial state		\
4262306a36Sopenharmony_ci */										\
4362306a36Sopenharmony_cistatic inline type model_get_initial_state_##name(void)				\
4462306a36Sopenharmony_ci{										\
4562306a36Sopenharmony_ci	return automaton_##name.initial_state;					\
4662306a36Sopenharmony_ci}										\
4762306a36Sopenharmony_ci										\
4862306a36Sopenharmony_ci/*										\
4962306a36Sopenharmony_ci * model_get_next_state_##name - process an automaton event occurrence		\
5062306a36Sopenharmony_ci *										\
5162306a36Sopenharmony_ci * Given the current state (curr_state) and the event (event), returns		\
5262306a36Sopenharmony_ci * the next state, or INVALID_STATE in case of error.				\
5362306a36Sopenharmony_ci */										\
5462306a36Sopenharmony_cistatic inline type model_get_next_state_##name(enum states_##name curr_state,	\
5562306a36Sopenharmony_ci					       enum events_##name event)	\
5662306a36Sopenharmony_ci{										\
5762306a36Sopenharmony_ci	if ((curr_state < 0) || (curr_state >= state_max_##name))		\
5862306a36Sopenharmony_ci		return INVALID_STATE;						\
5962306a36Sopenharmony_ci										\
6062306a36Sopenharmony_ci	if ((event < 0) || (event >= event_max_##name))				\
6162306a36Sopenharmony_ci		return INVALID_STATE;						\
6262306a36Sopenharmony_ci										\
6362306a36Sopenharmony_ci	return automaton_##name.function[curr_state][event];			\
6462306a36Sopenharmony_ci}										\
6562306a36Sopenharmony_ci										\
6662306a36Sopenharmony_ci/*										\
6762306a36Sopenharmony_ci * model_is_final_state_##name - check if the given state is a final state	\
6862306a36Sopenharmony_ci */										\
6962306a36Sopenharmony_cistatic inline bool model_is_final_state_##name(enum states_##name state)	\
7062306a36Sopenharmony_ci{										\
7162306a36Sopenharmony_ci	if ((state < 0) || (state >= state_max_##name))				\
7262306a36Sopenharmony_ci		return 0;							\
7362306a36Sopenharmony_ci										\
7462306a36Sopenharmony_ci	return automaton_##name.final_states[state];				\
7562306a36Sopenharmony_ci}
76