Guard.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "sc_types.h"
  4. #include "Guard.h"
  5. /*! \file Implementation of the state machine 'Guard'
  6. */
  7. /* prototypes of all internal functions */
  8. static sc_boolean guard_check_main_region_A_tr0_tr0(const Guard* handle);
  9. static sc_boolean guard_check_main_region_A_tr1_tr1(const Guard* handle);
  10. static sc_boolean guard_check_main_region_B_tr0_tr0(const Guard* handle);
  11. static void guard_effect_main_region_A_tr0(Guard* handle);
  12. static void guard_effect_main_region_A_tr1(Guard* handle);
  13. static void guard_effect_main_region_B_tr0(Guard* handle);
  14. static void guard_enact_main_region_B(Guard* handle);
  15. static void guard_enseq_main_region_A_default(Guard* handle);
  16. static void guard_enseq_main_region_B_default(Guard* handle);
  17. static void guard_enseq_main_region_default(Guard* handle);
  18. static void guard_exseq_main_region_A(Guard* handle);
  19. static void guard_exseq_main_region_B(Guard* handle);
  20. static void guard_exseq_main_region(Guard* handle);
  21. static void guard_react_main_region_A(Guard* handle);
  22. static void guard_react_main_region_B(Guard* handle);
  23. static void guard_react_main_region__entry_Default(Guard* handle);
  24. static void guard_clearInEvents(Guard* handle);
  25. static void guard_clearOutEvents(Guard* handle);
  26. void guard_init(Guard* handle)
  27. {
  28. sc_integer i;
  29. for (i = 0; i < GUARD_MAX_ORTHOGONAL_STATES; ++i)
  30. {
  31. handle->stateConfVector[i] = Guard_last_state;
  32. }
  33. handle->stateConfVectorPosition = 0;
  34. guard_clearInEvents(handle);
  35. guard_clearOutEvents(handle);
  36. /* Default init sequence for statechart Guard */
  37. handle->iface.MyVar = 0;
  38. }
  39. void guard_enter(Guard* handle)
  40. {
  41. /* Default enter sequence for statechart Guard */
  42. guard_enseq_main_region_default(handle);
  43. }
  44. void guard_exit(Guard* handle)
  45. {
  46. /* Default exit sequence for statechart Guard */
  47. guard_exseq_main_region(handle);
  48. }
  49. sc_boolean guard_isActive(const Guard* handle)
  50. {
  51. sc_boolean result = bool_false;
  52. int i;
  53. for(i = 0; i < GUARD_MAX_ORTHOGONAL_STATES; i++)
  54. {
  55. result = result || handle->stateConfVector[i] != Guard_last_state;
  56. }
  57. return result;
  58. }
  59. /*
  60. * Always returns 'false' since this state machine can never become final.
  61. */
  62. sc_boolean guard_isFinal(const Guard* handle)
  63. {
  64. return bool_false;
  65. }
  66. static void guard_clearInEvents(Guard* handle)
  67. {
  68. handle->iface.Event1_raised = bool_false;
  69. handle->iface.Event2_raised = bool_false;
  70. handle->iface.Return_raised = bool_false;
  71. }
  72. static void guard_clearOutEvents(Guard* handle)
  73. {
  74. }
  75. void guard_runCycle(Guard* handle)
  76. {
  77. guard_clearOutEvents(handle);
  78. for (handle->stateConfVectorPosition = 0;
  79. handle->stateConfVectorPosition < GUARD_MAX_ORTHOGONAL_STATES;
  80. handle->stateConfVectorPosition++)
  81. {
  82. switch (handle->stateConfVector[handle->stateConfVectorPosition])
  83. {
  84. case Guard_main_region_A :
  85. {
  86. guard_react_main_region_A(handle);
  87. break;
  88. }
  89. case Guard_main_region_B :
  90. {
  91. guard_react_main_region_B(handle);
  92. break;
  93. }
  94. default:
  95. break;
  96. }
  97. }
  98. guard_clearInEvents(handle);
  99. }
  100. sc_boolean guard_isStateActive(const Guard* handle, GuardStates state)
  101. {
  102. sc_boolean result = bool_false;
  103. switch (state)
  104. {
  105. case Guard_main_region_A :
  106. result = (sc_boolean) (handle->stateConfVector[SCVI_GUARD_MAIN_REGION_A] == Guard_main_region_A
  107. );
  108. break;
  109. case Guard_main_region_B :
  110. result = (sc_boolean) (handle->stateConfVector[SCVI_GUARD_MAIN_REGION_B] == Guard_main_region_B
  111. );
  112. break;
  113. default:
  114. result = bool_false;
  115. break;
  116. }
  117. return result;
  118. }
  119. void guardIface_raise_event1(Guard* handle)
  120. {
  121. handle->iface.Event1_raised = bool_true;
  122. }
  123. void guardIface_raise_event2(Guard* handle)
  124. {
  125. handle->iface.Event2_raised = bool_true;
  126. }
  127. void guardIface_raise_return(Guard* handle)
  128. {
  129. handle->iface.Return_raised = bool_true;
  130. }
  131. sc_integer guardIface_get_myVar(const Guard* handle)
  132. {
  133. return handle->iface.MyVar;
  134. }
  135. void guardIface_set_myVar(Guard* handle, sc_integer value)
  136. {
  137. handle->iface.MyVar = value;
  138. }
  139. /* implementations of all internal functions */
  140. static sc_boolean guard_check_main_region_A_tr0_tr0(const Guard* handle)
  141. {
  142. return ((handle->iface.Event1_raised) && (handle->iface.MyVar == 10)) ? bool_true : bool_false;
  143. }
  144. static sc_boolean guard_check_main_region_A_tr1_tr1(const Guard* handle)
  145. {
  146. return handle->iface.Event2_raised;
  147. }
  148. static sc_boolean guard_check_main_region_B_tr0_tr0(const Guard* handle)
  149. {
  150. return handle->iface.Return_raised;
  151. }
  152. static void guard_effect_main_region_A_tr0(Guard* handle)
  153. {
  154. guard_exseq_main_region_A(handle);
  155. guard_enseq_main_region_B_default(handle);
  156. }
  157. static void guard_effect_main_region_A_tr1(Guard* handle)
  158. {
  159. guard_exseq_main_region_A(handle);
  160. guard_enseq_main_region_B_default(handle);
  161. }
  162. static void guard_effect_main_region_B_tr0(Guard* handle)
  163. {
  164. guard_exseq_main_region_B(handle);
  165. guard_enseq_main_region_A_default(handle);
  166. }
  167. /* Entry action for state 'B'. */
  168. static void guard_enact_main_region_B(Guard* handle)
  169. {
  170. /* Entry action for state 'B'. */
  171. handle->iface.MyVar = 10;
  172. }
  173. /* 'default' enter sequence for state A */
  174. static void guard_enseq_main_region_A_default(Guard* handle)
  175. {
  176. /* 'default' enter sequence for state A */
  177. handle->stateConfVector[0] = Guard_main_region_A;
  178. handle->stateConfVectorPosition = 0;
  179. }
  180. /* 'default' enter sequence for state B */
  181. static void guard_enseq_main_region_B_default(Guard* handle)
  182. {
  183. /* 'default' enter sequence for state B */
  184. guard_enact_main_region_B(handle);
  185. handle->stateConfVector[0] = Guard_main_region_B;
  186. handle->stateConfVectorPosition = 0;
  187. }
  188. /* 'default' enter sequence for region main region */
  189. static void guard_enseq_main_region_default(Guard* handle)
  190. {
  191. /* 'default' enter sequence for region main region */
  192. guard_react_main_region__entry_Default(handle);
  193. }
  194. /* Default exit sequence for state A */
  195. static void guard_exseq_main_region_A(Guard* handle)
  196. {
  197. /* Default exit sequence for state A */
  198. handle->stateConfVector[0] = Guard_last_state;
  199. handle->stateConfVectorPosition = 0;
  200. }
  201. /* Default exit sequence for state B */
  202. static void guard_exseq_main_region_B(Guard* handle)
  203. {
  204. /* Default exit sequence for state B */
  205. handle->stateConfVector[0] = Guard_last_state;
  206. handle->stateConfVectorPosition = 0;
  207. }
  208. /* Default exit sequence for region main region */
  209. static void guard_exseq_main_region(Guard* handle)
  210. {
  211. /* Default exit sequence for region main region */
  212. /* Handle exit of all possible states (of Guard.main_region) at position 0... */
  213. switch(handle->stateConfVector[ 0 ])
  214. {
  215. case Guard_main_region_A :
  216. {
  217. guard_exseq_main_region_A(handle);
  218. break;
  219. }
  220. case Guard_main_region_B :
  221. {
  222. guard_exseq_main_region_B(handle);
  223. break;
  224. }
  225. default: break;
  226. }
  227. }
  228. /* The reactions of state A. */
  229. static void guard_react_main_region_A(Guard* handle)
  230. {
  231. /* The reactions of state A. */
  232. if (guard_check_main_region_A_tr0_tr0(handle) == bool_true)
  233. {
  234. guard_effect_main_region_A_tr0(handle);
  235. } else
  236. {
  237. if (guard_check_main_region_A_tr1_tr1(handle) == bool_true)
  238. {
  239. guard_effect_main_region_A_tr1(handle);
  240. }
  241. }
  242. }
  243. /* The reactions of state B. */
  244. static void guard_react_main_region_B(Guard* handle)
  245. {
  246. /* The reactions of state B. */
  247. if (guard_check_main_region_B_tr0_tr0(handle) == bool_true)
  248. {
  249. guard_effect_main_region_B_tr0(handle);
  250. }
  251. }
  252. /* Default react sequence for initial entry */
  253. static void guard_react_main_region__entry_Default(Guard* handle)
  254. {
  255. /* Default react sequence for initial entry */
  256. guard_enseq_main_region_A_default(handle);
  257. }