Use latest version of conf.d/templates submodule.
[apps/agl-service-unicens.git] / ucs2-lib / inc / ucs_fsm.h
1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491                                                                            */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
4 /*                                                                                                */
5 /* This program is free software: you can redistribute it and/or modify                           */
6 /* it under the terms of the GNU General Public License as published by                           */
7 /* the Free Software Foundation, either version 2 of the License, or                              */
8 /* (at your option) any later version.                                                            */
9 /*                                                                                                */
10 /* This program is distributed in the hope that it will be useful,                                */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
13 /* GNU General Public License for more details.                                                   */
14 /*                                                                                                */
15 /* You should have received a copy of the GNU General Public License                              */
16 /* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
17 /*                                                                                                */
18 /* You may also obtain this software under a propriety license from Microchip.                    */
19 /* Please contact Microchip for further information.                                              */
20 /*------------------------------------------------------------------------------------------------*/
21
22 /*!
23  * \file
24  * \brief Internal header file of the Finite State Machine.
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup G_FSM
28  * @{
29  */
30
31 #ifndef UCS_FSM_H
32 #define UCS_FSM_H
33
34 /*------------------------------------------------------------------------------------------------*/
35 /* Includes                                                                                       */
36 /*------------------------------------------------------------------------------------------------*/
37 #include "ucs_types_cfg.h"
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 /*------------------------------------------------------------------------------------------------*/
45 /* Definitions                                                                                    */
46 /*------------------------------------------------------------------------------------------------*/
47 /*! \brief Internal state machine states which are also used as return values for method 
48  *         Fsm_Service().
49  */
50 typedef enum Fsm_State_
51 {
52     FSM_STATE_IDLE,     /*!< \brief The state machine is in idle mode */
53     FSM_STATE_SERVICE,  /*!< \brief An event is pending and the state machine must be serviced */
54     FSM_STATE_WAIT,     /*!< \brief Waiting for asynchronous data/signal/event */
55     FSM_STATE_END,      /*!< \brief The state machine is finished */
56     FSM_STATE_ERROR     /*!< \brief An error occurred while processing the state machine */
57
58 } Fsm_State_t;
59
60 /*------------------------------------------------------------------------------------------------*/
61 /* Structures                                                                                     */
62 /*------------------------------------------------------------------------------------------------*/
63 /*! \brief Function signature used for state machine actions */
64 typedef void (*Fsm_Act_t)(void *self);
65
66 /*! \brief Structure is used to define state elements */
67 typedef struct Fsm_StateElem_
68 {
69     /*! \brief Function pointer to the action that shall be executed */
70     Fsm_Act_t action_fptr;
71     /*! \brief Next state */
72     int8_t next_state;
73
74 } Fsm_StateElem_t;
75
76 /*! \brief Class structure of the finite state machine */
77 typedef struct CFsm_
78 {
79     /*! \brief Reference to transition table */
80     const Fsm_StateElem_t *transition_table_ptr;
81     /*! \brief Instance pointer used for actions */
82     void *inst_ptr;
83     /*! \brief Current event */
84     int8_t event_occured;
85     /*! \brief Current state */
86     int8_t current_state;
87     /*! \brief Maximum number of events */
88     uint8_t num_events;
89     /*! \brief Internal state of the state machine */
90     Fsm_State_t internal_state;
91
92 } CFsm;
93
94 /*------------------------------------------------------------------------------------------------*/
95 /* Prototypes                                                                                     */
96 /*------------------------------------------------------------------------------------------------*/
97 extern void Fsm_Ctor(CFsm *self, void *inst_ptr, const Fsm_StateElem_t *trans_table_ptr,
98                      uint8_t num_events, int8_t init_state);
99 extern Fsm_State_t Fsm_Service(CFsm *self);
100 extern void Fsm_SetEvent(CFsm *self, int8_t e);
101 extern void Fsm_Wait(CFsm *self);
102 extern void Fsm_End(CFsm *self);
103
104 #ifdef __cplusplus
105 }   /* extern "C" */
106 #endif
107
108 #endif  /* #ifndef UCS_FSM_H */
109
110 /*!
111  * @}
112  * \endcond
113  */
114
115 /*------------------------------------------------------------------------------------------------*/
116 /* End of file                                                                                    */
117 /*------------------------------------------------------------------------------------------------*/
118