Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira  <bristot@kernel.org>
0004  *
0005  * Deterministic automata helper functions, to be used with the automata
0006  * models in C generated by the dot2k tool.
0007  */
0008 
0009 /*
0010  * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
0011  *
0012  * Define a set of helper functions for automata. The 'name' argument is used
0013  * as suffix for the functions and data. These functions will handle automaton
0014  * with data type 'type'.
0015  */
0016 #define DECLARE_AUTOMATA_HELPERS(name, type)                    \
0017                                         \
0018 /*                                      \
0019  * model_get_state_name_##name - return the (string) name of the given state    \
0020  */                                         \
0021 static char *model_get_state_name_##name(enum states_##name state)      \
0022 {                                       \
0023     if ((state < 0) || (state >= state_max_##name))             \
0024         return "INVALID";                       \
0025                                         \
0026     return automaton_##name.state_names[state];             \
0027 }                                       \
0028                                         \
0029 /*                                      \
0030  * model_get_event_name_##name - return the (string) name of the given event    \
0031  */                                     \
0032 static char *model_get_event_name_##name(enum events_##name event)      \
0033 {                                       \
0034     if ((event < 0) || (event >= event_max_##name))             \
0035         return "INVALID";                       \
0036                                         \
0037     return automaton_##name.event_names[event];             \
0038 }                                       \
0039                                         \
0040 /*                                      \
0041  * model_get_initial_state_##name - return the automaton's initial state        \
0042  */                                     \
0043 static inline type model_get_initial_state_##name(void)             \
0044 {                                       \
0045     return automaton_##name.initial_state;                  \
0046 }                                       \
0047                                         \
0048 /*                                      \
0049  * model_get_next_state_##name - process an automaton event occurrence      \
0050  *                                      \
0051  * Given the current state (curr_state) and the event (event), returns      \
0052  * the next state, or INVALID_STATE in case of error.               \
0053  */                                     \
0054 static inline type model_get_next_state_##name(enum states_##name curr_state,   \
0055                            enum events_##name event)    \
0056 {                                       \
0057     if ((curr_state < 0) || (curr_state >= state_max_##name))       \
0058         return INVALID_STATE;                       \
0059                                         \
0060     if ((event < 0) || (event >= event_max_##name))             \
0061         return INVALID_STATE;                       \
0062                                         \
0063     return automaton_##name.function[curr_state][event];            \
0064 }                                       \
0065                                         \
0066 /*                                      \
0067  * model_is_final_state_##name - check if the given state is a final state  \
0068  */                                     \
0069 static inline bool model_is_final_state_##name(enum states_##name state)    \
0070 {                                       \
0071     if ((state < 0) || (state >= state_max_##name))             \
0072         return 0;                           \
0073                                         \
0074     return automaton_##name.final_states[state];                \
0075 }