cb_hsm.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2023-06-03 tyx first implementation
  7. */
  8. #ifndef CB_HSM_H_
  9. #define CB_HSM_H_
  10. #include "cb_def.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #define CB_HSM_STATE_LEVEL_MAX (16)
  15. typedef enum cb_hsm_ret
  16. {
  17. CB_HSM_RET_NULL = 0,
  18. CB_HSM_RET_IGNORED,
  19. CB_HSM_RET_HANDLED,
  20. CB_HSM_RET_SUPER,
  21. CB_HSM_RET_TRAN,
  22. } cb_hsm_ret_t;
  23. typedef unsigned short cb_hsm_topic_t;
  24. #define CB_HSM_TOPIC_NULL (0x00u)
  25. #define CB_HSM_TOPIC_ENTER (0x01u)
  26. #define CB_HSM_TOPIC_EXIT (0x02u)
  27. #define CB_HSM_TOPIC_INIT (0x03u)
  28. #define CB_HSM_TOPIC_USER (0x10u)
  29. struct cb_hsm_event;
  30. typedef struct cb_hsm_event cb_hsm_event_t;
  31. struct cb_hsm_event
  32. {
  33. cb_hsm_topic_t topic;
  34. };
  35. struct cb_hsm_actor;
  36. typedef struct cb_hsm_actor cb_hsm_actor_t;
  37. typedef cb_hsm_ret_t (*cb_hsm_state_t)(cb_hsm_actor_t *me, const cb_hsm_event_t * const e);
  38. struct cb_hsm_actor
  39. {
  40. volatile cb_hsm_state_t state;
  41. };
  42. #define CB_HSM_START(_me, _init) cb_hsm_start((cb_hsm_actor_t*)(_me), (cb_hsm_state_t)_init)
  43. #define CB_HSM_TRAN(_me, _target) cb_hsm_tran((cb_hsm_actor_t*)(_me), (cb_hsm_state_t)_target)
  44. #define CB_HSM_SUPER(_me, _super) cb_hsm_super((cb_hsm_actor_t*)(_me), (cb_hsm_state_t)_super)
  45. #define CB_HSM_HANDLED(_me) cb_hsm_handle((cb_hsm_actor_t*)(_me))
  46. // Event handler. When a state machine receives an event, it processes the event using this function
  47. void cb_hsm_event_handle(cb_hsm_actor_t *me, const cb_hsm_event_t * const e);
  48. cb_hsm_ret_t cb_hsm_state_top(cb_hsm_actor_t* me, const cb_hsm_event_t* const e);
  49. // The hierarchical state machine object is initialized, setting the state to the top-level state.
  50. cb_inline void cb_hsm_init(cb_hsm_actor_t *me)
  51. {
  52. me->state = cb_hsm_state_top;
  53. }
  54. // Instead of using these functions directly, use the macro CB_HSM_xx
  55. void cb_hsm_start(cb_hsm_actor_t *me, cb_hsm_state_t state_init);
  56. cb_hsm_ret_t cb_hsm_tran(cb_hsm_actor_t *me, cb_hsm_state_t state);
  57. cb_hsm_ret_t cb_hsm_super(cb_hsm_actor_t *me, cb_hsm_state_t state);
  58. cb_hsm_ret_t cb_hsm_handle(cb_hsm_actor_t *me);
  59. #ifdef __cplusplus
  60. }
  61. #endif
  62. #endif