automata.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <[email protected]>
  4. *
  5. * Deterministic automata helper functions, to be used with the automata
  6. * models in C generated by the dot2k tool.
  7. */
  8. /*
  9. * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
  10. *
  11. * Define a set of helper functions for automata. The 'name' argument is used
  12. * as suffix for the functions and data. These functions will handle automaton
  13. * with data type 'type'.
  14. */
  15. #define DECLARE_AUTOMATA_HELPERS(name, type) \
  16. \
  17. /* \
  18. * model_get_state_name_##name - return the (string) name of the given state \
  19. */ \
  20. static char *model_get_state_name_##name(enum states_##name state) \
  21. { \
  22. if ((state < 0) || (state >= state_max_##name)) \
  23. return "INVALID"; \
  24. \
  25. return automaton_##name.state_names[state]; \
  26. } \
  27. \
  28. /* \
  29. * model_get_event_name_##name - return the (string) name of the given event \
  30. */ \
  31. static char *model_get_event_name_##name(enum events_##name event) \
  32. { \
  33. if ((event < 0) || (event >= event_max_##name)) \
  34. return "INVALID"; \
  35. \
  36. return automaton_##name.event_names[event]; \
  37. } \
  38. \
  39. /* \
  40. * model_get_initial_state_##name - return the automaton's initial state \
  41. */ \
  42. static inline type model_get_initial_state_##name(void) \
  43. { \
  44. return automaton_##name.initial_state; \
  45. } \
  46. \
  47. /* \
  48. * model_get_next_state_##name - process an automaton event occurrence \
  49. * \
  50. * Given the current state (curr_state) and the event (event), returns \
  51. * the next state, or INVALID_STATE in case of error. \
  52. */ \
  53. static inline type model_get_next_state_##name(enum states_##name curr_state, \
  54. enum events_##name event) \
  55. { \
  56. if ((curr_state < 0) || (curr_state >= state_max_##name)) \
  57. return INVALID_STATE; \
  58. \
  59. if ((event < 0) || (event >= event_max_##name)) \
  60. return INVALID_STATE; \
  61. \
  62. return automaton_##name.function[curr_state][event]; \
  63. } \
  64. \
  65. /* \
  66. * model_is_final_state_##name - check if the given state is a final state \
  67. */ \
  68. static inline bool model_is_final_state_##name(enum states_##name state) \
  69. { \
  70. if ((state < 0) || (state >= state_max_##name)) \
  71. return 0; \
  72. \
  73. return automaton_##name.final_states[state]; \
  74. }