-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathhsm.h
129 lines (102 loc) · 3.78 KB
/
hsm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* \file
* \brief hierarchical state machine
* \author Nandkishor Biradar
* \date 01 December 2018
* Copyright (c) 2018-2019 Nandkishor Biradar
* https://github.com/kiishor
* Distributed under the MIT License, (See accompanying
* file LICENSE or copy at https://mit-license.org/)
*/
#ifndef HSM_H
#define HSM_H
#ifdef HSM_CONFIG
#include "hsm_config.h"
#endif // HSM_CONFIG
/*
* --------------------- DEFINITION ---------------------
*/
#ifndef HIERARCHICAL_STATES
//! Default configuration is hierarchical state machine
#define HIERARCHICAL_STATES 1
#endif // HIERARCHICAL_STATES
#ifndef STATE_MACHINE_LOGGER
#define STATE_MACHINE_LOGGER 0 //!< Disable the logging of state machine
#endif // STATE_MACHINE_LOGGER
#ifndef HSM_USE_VARIABLE_LENGTH_ARRAY
#define HSM_USE_VARIABLE_LENGTH_ARRAY 1
#endif
/*
* --------------------- ENUMERATION ---------------------
*/
//! List of state machine result code
typedef enum
{
EVENT_HANDLED, //!< Event handled successfully.
EVENT_UN_HANDLED, //!< Event could not be handled.
//!< Handler handled the Event successfully and posted new event to itself.
TRIGGERED_TO_SELF,
}state_machine_result_t;
/*
* --------------------- STRUCTURE ---------------------
*/
#if HIERARCHICAL_STATES
typedef struct hierarchical_state state_t;
#else
typedef struct finite_state state_t;
#endif // HIERARCHICAL_STATES
typedef struct state_machine_t state_machine_t;
typedef state_machine_result_t (*state_handler) (state_machine_t* const State);
typedef void (*state_machine_event_logger)(uint32_t state_machine, uint32_t state, uint32_t event);
typedef void (*state_machine_result_logger)(uint32_t state, state_machine_result_t result);
//! finite state structure
struct finite_state{
state_handler Handler; //!< State handler function
state_handler Entry; //!< Entry action for state
state_handler Exit; //!< Exit action for state.
#if STATE_MACHINE_LOGGER
uint32_t Id; //!< unique identifier of state within the single state machine
#endif
};
//! Hierarchical state structure
struct hierarchical_state
{
state_handler Handler; //!< State handler function
state_handler Entry; //!< Entry action for state
state_handler Exit; //!< Exit action for state.
#if STATE_MACHINE_LOGGER
uint32_t Id; //!< unique identifier of state within the single state machine
#endif
const state_t* const Parent; //!< Parent state of the current state.
const state_t* const Node; //!< Child states of the current state.
uint32_t Level; //!< Hierarchy level from the top state.
};
//! Abstract state machine structure
struct state_machine_t
{
uint32_t Event; //!< Pending Event for state machine
const state_t* State; //!< State of state machine.
};
/*
* --------------------- EXPORTED FUNCTION ---------------------
*/
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
extern state_machine_result_t dispatch_event(state_machine_t* const pState_Machine[],
uint32_t quantity
#if STATE_MACHINE_LOGGER
,state_machine_event_logger event_logger
,state_machine_result_logger result_logger
#endif // STATE_MACHINE_LOGGER
);
#if HIERARCHICAL_STATES
extern state_machine_result_t traverse_state(state_machine_t* const pState_Machine,
const state_t* pTarget_State);
#endif // HIERARCHICAL_STATES
extern state_machine_result_t switch_state(state_machine_t* const pState_Machine,
const state_t* const pTarget_State);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // HSM_H