xref: /linux/include/rv/automata.h (revision 30984ccf31b7f0678fad05a22e06bb9f6014c1ce)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira  <bristot@kernel.org>
4  *
5  * Deterministic automata helper functions, to be used with the automata
6  * models in C generated by the dot2k tool.
7  */
8 
9 #ifndef MONITOR_NAME
10 #error "MONITOR_NAME macro is not defined. Did you include $(MODEL_NAME).h generated by rvgen?"
11 #endif
12 
13 #ifndef type
14 #define type unsigned char
15 #endif
16 
17 #define RV_AUTOMATON_NAME CONCATENATE(automaton_, MONITOR_NAME)
18 #define EVENT_MAX CONCATENATE(event_max_, MONITOR_NAME)
19 #define STATE_MAX CONCATENATE(state_max_, MONITOR_NAME)
20 #define events CONCATENATE(events_, MONITOR_NAME)
21 #define states CONCATENATE(states_, MONITOR_NAME)
22 
23 /*
24  * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
25  *
26  * Define a set of helper functions for automata. The 'name' argument is used
27  * as suffix for the functions and data. These functions will handle automaton
28  * with data type 'type'.
29  */
30 #define DECLARE_AUTOMATA_HELPERS(name, type)
31 
32 /*
33  * model_get_state_name - return the (string) name of the given state
34  */
35 static char *model_get_state_name(enum states state)
36 {
37 	if ((state < 0) || (state >= STATE_MAX))
38 		return "INVALID";
39 
40 	return RV_AUTOMATON_NAME.state_names[state];
41 }
42 
43 /*
44  * model_get_event_name - return the (string) name of the given event
45  */
46 static char *model_get_event_name(enum events event)
47 {
48 	if ((event < 0) || (event >= EVENT_MAX))
49 		return "INVALID";
50 
51 	return RV_AUTOMATON_NAME.event_names[event];
52 }
53 
54 /*
55  * model_get_initial_state - return the automaton's initial state
56  */
57 static inline type model_get_initial_state(void)
58 {
59 	return RV_AUTOMATON_NAME.initial_state;
60 }
61 
62 /*
63  * model_get_next_state - process an automaton event occurrence
64  *
65  * Given the current state (curr_state) and the event (event), returns
66  * the next state, or INVALID_STATE in case of error.
67  */
68 static inline type model_get_next_state(enum states curr_state,
69 					       enum events event)
70 {
71 	if ((curr_state < 0) || (curr_state >= STATE_MAX))
72 		return INVALID_STATE;
73 
74 	if ((event < 0) || (event >= EVENT_MAX))
75 		return INVALID_STATE;
76 
77 	return RV_AUTOMATON_NAME.function[curr_state][event];
78 }
79 
80 /*
81  * model_is_final_state - check if the given state is a final state
82  */
83 static inline bool model_is_final_state(enum states state)
84 {
85 	if ((state < 0) || (state >= STATE_MAX))
86 		return 0;
87 
88 	return RV_AUTOMATON_NAME.final_states[state];
89 }
90