forked from marcoevang/camilladsp-setrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setrate_fsm.c
188 lines (136 loc) · 6.37 KB
/
setrate_fsm.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
////////////////////////////////////////////////////
//
// camilladsp-setrate
// Automatic sample rate switcher for CamillaDSP
//
// setrate_fsm.c
// Finite-state Machine
//
////////////////////////////////////////////////////
#include <stddef.h>
#include <string.h>
#include <libwebsockets.h>
#include "setrate.h"
// A cell of the transition table
typedef struct
{
int (*action)(void); // Pointer to function to be called when a transition occurs
states next_state; // Next state to be reached
} cell;
// Transition table of the finite-state machine
static cell transition_table[LAST_STATE + 1][LAST_EVENT + 1];
states state; // Global state
////////////////////////////////////////////////////////////
// Initialise the finite-state machine by assigning
// action function and next state in the transition table
////////////////////////////////////////////////////////////
void fsm_init()
{
int i, j;
for (i = FIRST_STATE; i <= LAST_STATE; i++)
for (j = FIRST_EVENT; j <= LAST_EVENT; j++)
{
transition_table[i][j].action = NULL;
transition_table[i][j].next_state = INIT;
}
transition_table[START][CONNECT].action = NULL;
transition_table[START][CONNECT].next_state = WAIT_RATE_CHANGE;
transition_table[START][DISCONNECT].action = reconnection_request;
transition_table[START][DISCONNECT].next_state = START;
transition_table[WAIT_RATE_CHANGE][RATE_CHANGE].action = callback_on_writeable;
transition_table[WAIT_RATE_CHANGE][RATE_CHANGE].next_state = RATE_CHANGED;
transition_table[WAIT_RATE_CHANGE][DISCONNECT].action = reconnection_request;
transition_table[WAIT_RATE_CHANGE][DISCONNECT].next_state = START;
transition_table[RATE_CHANGED][WRITEABLE].action = send_get_config;
transition_table[RATE_CHANGED][WRITEABLE].next_state = WAIT_CONFIG;
transition_table[RATE_CHANGED][DISCONNECT].action = reconnection_request;
transition_table[RATE_CHANGED][DISCONNECT].next_state = START;
transition_table[WAIT_CONFIG][CONFIG_OK].action = callback_on_writeable;
transition_table[WAIT_CONFIG][CONFIG_OK].next_state = CONFIG_RECEIVED_OK;
transition_table[WAIT_CONFIG][CONFIG_KO].action = callback_on_writeable;
transition_table[WAIT_CONFIG][CONFIG_KO].next_state = CONFIG_RECEIVED_KO;
transition_table[WAIT_CONFIG][DISCONNECT].action = reconnection_request;
transition_table[WAIT_CONFIG][DISCONNECT].next_state = START;
transition_table[CONFIG_RECEIVED_OK][WRITEABLE].action = send_set_config;
transition_table[CONFIG_RECEIVED_OK][WRITEABLE].next_state = WAIT_RESPONSE;
transition_table[CONFIG_RECEIVED_OK][DISCONNECT].action = reconnection_request;
transition_table[CONFIG_RECEIVED_OK][DISCONNECT].next_state = START;
transition_table[CONFIG_RECEIVED_KO][WRITEABLE].action = send_get_previous_config;
transition_table[CONFIG_RECEIVED_KO][WRITEABLE].next_state = WAIT_PREVIOUS_CONFIG;
transition_table[CONFIG_RECEIVED_KO][DISCONNECT].action = reconnection_request;
transition_table[CONFIG_RECEIVED_KO][DISCONNECT].next_state = START;
transition_table[WAIT_PREVIOUS_CONFIG][CONFIG_OK].action = callback_on_writeable;
transition_table[WAIT_PREVIOUS_CONFIG][CONFIG_OK].next_state = CONFIG_RECEIVED_OK;
transition_table[WAIT_PREVIOUS_CONFIG][CONFIG_KO].action = notify_failure;
transition_table[WAIT_PREVIOUS_CONFIG][CONFIG_KO].next_state = WAIT_RATE_CHANGE;
transition_table[WAIT_PREVIOUS_CONFIG][DISCONNECT].action = reconnection_request;
transition_table[WAIT_PREVIOUS_CONFIG][DISCONNECT].next_state= START;
transition_table[WAIT_RESPONSE][RESPONSE_OK].action = notify_success;
transition_table[WAIT_RESPONSE][RESPONSE_OK].next_state = WAIT_RATE_CHANGE;
transition_table[WAIT_RESPONSE][RESPONSE_KO].action = notify_failure;
transition_table[WAIT_RESPONSE][RESPONSE_KO].next_state = WAIT_RATE_CHANGE;
transition_table[WAIT_RESPONSE][DISCONNECT].action = reconnection_request;
transition_table[WAIT_RESPONSE][DISCONNECT].next_state = START;
writelog(NOTICE, "Finite-state machine initialised\n");
// Set the initial state
state = START;
}
/////////////////////////////////////////////////////////
// Trigger a transition of the finite-state machine
// depending on the current state and the incoming event
// (Mealy machine)
/////////////////////////////////////////////////////////
int fsm_transit(events event)
{
int (*action)();
states next_state;
int ret;
// Sanity checks
if (state < FIRST_STATE || state > LAST_STATE)
{
writelog(ERR, "Fatal error: State %d out of range\n", state);
exit(FAIL);
}
if (event < FIRST_EVENT || event > LAST_EVENT)
{
writelog(ERR, "Fatal error: Event %d out of range\n", event);
exit(FAIL);
}
// Pick next state in the transition table
next_state = transition_table[state][event].next_state;
// If next state is INIT there is nothing to be done
if (next_state == INIT)
return(SUCCESS);
// Pick action function in the transition table
action = transition_table[state][event].action;
writelog(NOTICE, "%20s: Event : %s\n", decode_state(state), decode_event(event));
writelog(NOTICE, "%20s: Action : %s\n", decode_state(state), decode_action(action));
writelog(NOTICE, "%20s: State : %s\n", decode_state(state), decode_state(next_state));
// Carry out the action, if any
if (action)
ret = action();
// Update the current state
state = next_state;
return(ret);
}
//////////////////////////////////////////////
//////////////////////////////////////////////
//// Actions of the finite-state machine ////
//////////////////////////////////////////////
//////////////////////////////////////////////
///////////////////////////////////////////////
// Notify success of the configuration update
//////////////////////////////////////////////
int notify_success(void)
{
writelog(USER, "%20s: END : Configuration update success\n", decode_state(state));
return(SUCCESS);
}
//////////////////////////////////////////////
// Notify failure of the configuration update
//////////////////////////////////////////////
int notify_failure(void)
{
writelog(WARN, "%20s: END : Configuration update abort\n", decode_state(state));
return(SUCCESS);
}