This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsm.h
131 lines (118 loc) · 3.73 KB
/
fsm.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
130
131
#ifndef FSM_H
#define FSM_H
/**
* @file fsm.h
* @brief an implementation for a FSM in C, this file contains
* all definitions required for FSM.
* @author Ankur Shrivastava
*/
// forward declaration
struct fsm_object;
/**
* @struct fsm_state fsm.h "fsm.h"
* @brief Stores information regarding state
*/
struct fsm_state{
/**
* Stores the name of the state
*/
char *name;
/**
* stores the function pointer for the state
*/
void (*function)(struct fsm_object* ,int, void**);
/**
* pointer to the next state
*/
struct fsm_state *next;
};
/**
* @struct fsm_object fsm.h "fsm.h"
* @brief stores info regarding state machine
*/
struct fsm_object{
/**
* pointer to the linked list of fsm_state structures
*/
struct fsm_state * fsm_base;
/**
* name of current FSM state
*/
struct fsm_state * fsm_cur_state;
/**
* number of argument passed to the nest state
*/
char * fsm_cur_state_name;
/**
* pointer to current FSM state
*/
int fsm_arg_num;
/**
* values of arguments passed to the next state
*/
void ** fsm_arg_value;
/**
* specifies if this FSM is running in an fsm loop or not
* setting it to zero will terminate the fsm
*/
int fsm_running;
};
/**
* Function to initialize the FSM
* @param obj pointer to structure of type fsm_object, which defines the FSM
*/
int fsm_init(struct fsm_object *obj);
/**
* The FSM entry point, this is where execution of code begins in FSM.
* @param obj pointer to structure of type fsm_object, which defines the FSM
*/
int fsm_main(struct fsm_object *obj);
/**
* Execution of next state takes place here
* @details function fsm_next can be used without fsm_main when we want to handle
* state execution and not rely on fsm_main 's loop
* @param obj pointer to structure of type fsm_object, which defines the FSM
*/
int fsm_next_state(struct fsm_object *obj);
/**
* Function to add a new state to the FSM.
* @param obj pointer to structure of type fsm_object, which defines the FSM
* @param state name of the state to be added.
* @param fun name of the function to be executed for this state
*/
int fsm_add(struct fsm_object *obj, char *state, void (*fun)(struct fsm_object *, int, void **) );
/**
* Function to add a default state to FSM.
* @details Adds a default state to FSM, this is the function called at the start of the FSM
* or in case of error, with the appropriate error code
* @param obj pointer to structure of type fsm_object, which defines the FSM
* @param fun name of the function to be executed for this state
*/
int fsm_default(struct fsm_object *obj, void (*fun)(struct fsm_object *, int, void **) );
/**
* Function to remove a state from the FSM.
* @param obj pointer to structure of type fsm_object, which defines the FSM
* @param state name of state to be removed
*/
int fsm_remove(struct fsm_object *obj, char *state);
/**
* Function to change state.
* @details changes state to the new specified state, if the state does not exist returns error,
* state change is not triggered till function calling fsm_to_state returns
* @param obj pointer to structure of type fsm_object, which defines the FSM
* @param state name of state to change to
* @param num number of arguments
* @param arg arguments
*/
int fsm_to_state(struct fsm_object *obj, char *state, int num, void** arg);
/**
* Function for FSM termination
* @param obj pointer to structure of type fsm_object, which defines the FSM
*/
void fsm_terminate(struct fsm_object *obj);
/**
* Function clean the fsm_object cleaning (free) the states inside the objects
* @param obj pointer to structure of type fsm_object, which defines the FSM
*/
void fsm_delete(struct fsm_object *obj);
#endif