forked from evgenyi1/FSM-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_tool_framework.c
574 lines (523 loc) · 19.5 KB
/
fsm_tool_framework.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//***************************************************************
// File: FsmFramework.c
// Class/Module: Fsm C Framework
//
// Purpose:
// Description:
//
// Modification history:
// 6/2/2010 [LEONIDC] Created
//
//***************************************************************
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#ifndef WIN32
#include <netinet/in.h>
#include <unistd.h>
#include <termios.h>
#include "logging.h"
#endif
#include "fsm_tool_framework.h"
void fsm_state_base_entry(fsm_base *fsm, fsm_state_base *target_state);
void fsm_state_base_exit(fsm_base *fsm, fsm_state_base *state);
void fsm_state_simple_entry(fsm_base *fsm, fsm_state_base *target_state);
void fsm_state_simple_exit(fsm_base *fsm, fsm_state_base *target_state);
void fsm_state_composed_entry(fsm_base *fsm, fsm_state_base *target_state);
void fsm_state_composed_exit(fsm_base *fsm,fsm_state_base *target_state);
int fsm_schedule_timeout(fsm_base *fsm, fsm_timer_index timer_index, int timeout, unsigned short id);
int fsm_unschedule_timeout(fsm_base *fsm, fsm_timer_index timer_index);
void print_fsm(fsm_base *fsm);
int fsm_tm_sched(fsm_base *fsm, int timeout, fsm_timer_index timer_index ,unsigned short id);
int fsm_tm_unsched(fsm_base *fsm, unsigned short id);
int fsm_tm_init(timer_mngr *timer_manager);
int fsm_tm_deinit(timer_mngr *timer_manager);
//------------------------------------------------------------------------
// fsm_init
//
// Purpose: init of fsm_base
// initialized attributes , current state assigned to Default state of FSM ,
// passed pointer to real Timer Manager class
// Output :
// Input :
// Precondition:
//
// Returns: none
//------------------------------------------------------------------------
int fsm_init(fsm_base *fsm, fsm_state_base *curr_state_entry, uint8 num_states,
int *state_timer_values, fsm_user_trace user_trace,
void *timer_sched, void *timer_unsched)
{
int rc = 1;
//itsTraceMode = FALSE;
fsm->busy_flag = 0;
fsm->current_event = NULL;
fsm->curr_state = curr_state_entry;
fsm->prev_state = fsm->last_simple_state = fsm->curr_state;
fsm->state_timers = state_timer_values;
fsm->num_states = num_states;
fsm->user_trace = user_trace;
fsm->tmr_mngr.sched_func = timer_sched;
fsm->tmr_mngr.unsched_func = timer_unsched;
fsm_tm_init(&fsm->tmr_mngr);
fsm_trace(fsm, "\nFsm Framework called %s for type %d\n", __FUNCTION__, fsm->fsm_type);
fsm_trace(fsm, "FSM passed to %s state\n\n", fsm->curr_state->name);
return rc;
}
int fsm_deinit(fsm_base *fsm)
{
fsm->state_timers = NULL;
fsm->num_states = 0;
fsm->user_trace = NULL;
fsm_tm_deinit(&fsm->tmr_mngr);
return 0;
}
void fsm_set_timer_value(fsm_base *fsm, int state, int value)
{
// to add validation that condition state cannot have timeouts !! and state without timers transition also cannot accept timer value !=0 TODO (generate bit additional in state)
if(state < fsm->num_states) {
fsm->state_timers[state] = value;
}
}
int fsm_get_timer_value(fsm_base *fsm, int state)
{
if(state < fsm->num_states) {
return fsm->state_timers[state];
}
return 0;
}
//----------------------------------------------------------------------
// timer_trigger_operation
// Purpose: called when timer message arrived to FSM
// Input : <id> Id of the timer
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
void fsm_timer_trigger_operation(fsm_base *fsm, unsigned short id)
{
fsm_timer_event ev;
if ( id == AUX_TIMEOUT_ID ) {
ev.opcode = AUX_TIMER_EVENT;
ev.name = "AUX_TIMER_EVENT";
}
else {
ev.opcode = TIMER_EVENT;
ev.name = "TIMER_EVENT";
}
ev.id = id; /* id of timer = the name of the state where timer was scheduled */
fsm_handle_event(fsm, (fsm_event_base*) &ev);
}
//----------------------------------------------------------------------
// print_fsm
// Purpose: debug print of FSM
// prints current event,current state of FSM, and previous event and State
// called user_trace function that specifies generic trace with other FSM attributes
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
void print_fsm(fsm_base* fsm)
{
int len = 0;
char tracer_buffer[300];
len += sprintf(tracer_buffer,
"Curr. state = %s, Prev. state = %s",
fsm->curr_state->name, fsm->prev_state->name);
if(fsm->user_trace) {
fsm->user_trace(tracer_buffer, len);
}
}
//----------------------------------------------------------------------
// fsm_trace
// Purpose: if defined trace mode for FSm called
// UserTrace function redefined in FSM
//
// Input : formatted string
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
void fsm_trace(fsm_base *fsm, const char *fmt,...)
{
int len;
va_list ap;
char tracer_buffer[300];
va_start(ap, fmt);
len = vsprintf(tracer_buffer, fmt, ap);
if(fsm->user_trace) {
fsm->user_trace(tracer_buffer, len);
}
va_end(ap);
}
//----------------------------------------------------------------------
// fsm_handle_event
//
// Purpose: Handling of event by FSM:
//
// - call HandleEvent method of the Current State. Find target state
// while target state = ConditionState continue to call fsm_handle_event of target state
// - process AUX timer event
// - Trace FSM print events (timer or normal), current state, target state
//
// Input : ev - pointer to incoming event
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
fsm_consume_results fsm_handle_event(fsm_base *fsm, fsm_event_base *ev)
{
if(!ev) {
return FSM_CONSUME_ERROR;
}
fsm_trace(fsm, "Event %s arrived to state %s (%d)\n", ev->name, fsm->curr_state->name, fsm->curr_state->id);
if(ev->opcode == TIMER_EVENT) {
fsm_trace(fsm, " timer_id = %d ", (( fsm_timer_event*)ev)->id);
}
if(fsm->busy_flag) {
fsm_trace(fsm, "FSM Framework Error: try to recursive entry in fsm %d\n", fsm->fsm_type);
return FSM_CONSUME_ERROR;
}
fsm->current_event = ev;
fsm->busy_flag = 1;
fsm->consume_event_result = FSM_CONSUMED;
if(ev->opcode == AUX_TIMER_EVENT) {
fsm_unschedule_aux_timer(fsm);
}
do {
fsm->reaction_in_state = 0;
if( (fsm->target_state = fsm_state_handle_event(fsm, fsm->curr_state, ev)) != NULL ) {
if(fsm->consume_event_result == FSM_CONSUME_ERROR) {
break; /* returns with consume error */
}
if (fsm->target_state->type == SIMPLE) {
fsm->last_simple_state = fsm->target_state;
}
fsm->prev_state = fsm->curr_state;
fsm->curr_state = fsm->target_state;
}
else {
if(fsm->curr_state->type == CONDITION) {
fsm_trace(fsm, "FSM_framework: Error :event %s is not consumed in condition state %s\n",
fsm->current_event->name, fsm->curr_state->name);
fsm->consume_event_result = FSM_CONSUME_ERROR;
}
else {
fsm->consume_event_result = FSM_NOT_CONSUMED;
}
fsm_trace(fsm,"Event %s is not consumed in state %s (%d)\n\n", ev->name, fsm->curr_state->name, fsm->curr_state->id);
break;
}
if(fsm->target_state->type == CONDITION) { //m_pCurrentEvent = &Null_ev ; // need to preserve this event for pass its parameters to reactions
fsm_trace(fsm,"Check Condition %s state\n",fsm->curr_state->name);
}
} while ((fsm->target_state->type == CONDITION) && (fsm->consume_event_result == FSM_CONSUMED));
if(fsm->target_state) {
fsm_trace(fsm,"FSM passed to %s state\n\n", fsm->curr_state->name);
}
fsm->busy_flag = 0;
fsm->current_event = NULL;
return fsm->consume_event_result;
}
//----------------------------------------------------------------------
// fsm_schedule_aux_timer
// Purpose: Scheduler AUX Timeout of FSM - called by user
// Input : timeout - timeout in ms
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
int fsm_schedule_aux_timer(fsm_base *fsm, int timeout)
{
int rc = 1;
rc = fsm_tm_sched(fsm, timeout, AUX_TIMEOUT_ID, 0);
if(rc == 0) {
fsm_trace(fsm, "Auxiliary timer %d was not scheduled in state %s ", timeout, fsm->curr_state->name);
}
return rc;
}
//----------------------------------------------------------------------
// fsm_unschedule_aux_timer
// Purpose: Scheduler AUX Timeout of FSM - called by user
// Input :
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
int fsm_unschedule_aux_timer(fsm_base* fsm)
{
int rc = 1;
rc = fsm_tm_unsched(fsm, AUX_TIMEOUT_ID);
if(rc == 0) {
fsm_trace(fsm, "Auxiliary timer was not unscheduled in state %s ", fsm->curr_state->name);
}
return rc;
}
/****************************************************************************/
// fsm_state_base functions
/****************************************************************************/
//----------------------------------------------------------------------
// fsm_state_handle_event
// Called for Current state of the FSM to check whether Event consumed, perform State reactions
// call Exit actions of the Current state (if need )
// define Target state , call Entry actions of Target State(if need)
// Precondition:
//
// Returns: target state
//----------------------------------------------------------------------
fsm_state_base *fsm_state_handle_event(fsm_base *fsm, fsm_state_base *state, fsm_event_base *ev)
{
fsm_state_base *target_state = NULL;
target_state = ((fsm_state_dispatch)(state->state_dispatcher))(fsm, state->id);
if(target_state == NULL) {
if(state->composed) {
target_state = ((fsm_state_dispatch)(state->composed->state_dispatcher))(fsm, state->composed->id);
}
}
if(target_state && target_state->type != CONDITION)
{
fsm->consumed_in_composed = (target_state->default_substate) ? 1 : 0;
/* if arc on composed state(CurrentState == targetState ) and also "reaction in state" - nothing to do .
* By this we will support arcs on composed states */
if ((fsm->curr_state) && (state->composed) &&
(state->composed == target_state) && (fsm->reaction_in_state)) {
target_state = fsm->curr_state;
}
else {
if(target_state->default_substate) {
target_state = target_state->default_substate;
}
fsm_state_simple_exit(fsm, target_state);
fsm_state_simple_entry(fsm, target_state);
}
}
return target_state;
}
//----------------------------------------------------------------------
// fsm_schedule_timeout
// Purpose: Scheduler of Timeout Automatically called by framework
// Input : fsm - pointer to user's FSM
// Output :
// Precondition:
//
// Returns: status
//----------------------------------------------------------------------
int fsm_schedule_timeout(fsm_base *fsm, fsm_timer_index timer_index, int timeout, unsigned short id)
{
int rc = 1;
if(fsm->reaction_in_state || timeout == 0) {
return 0;
}
fsm_trace(fsm, "fsm_tm_sched in state for %d msec\n", timeout);
rc = fsm_tm_sched(fsm, timeout, timer_index, id);
if(rc == 0) {
fsm_trace(fsm," Timer %d was not scheduled \n", timer_index);
}
return rc;
}
//--------------------------------------------------------------
// fsm_unschedule_timeout
// Purpose: Unscheduler Timeout . automatic framework call
// Input : fsm - pointer to user's FSM
// Output :
// Precondition:
//
// Returns: status
//----------------------------------------------------------------------
int fsm_unschedule_timeout (fsm_base * fsm, fsm_timer_index timer_index)
{
int rc = 1;
unsigned short id;
const char *name;
id = (timer_index ==SIMPLE_STATE_TMR) ? (fsm->curr_state->id) : fsm->curr_state->composed->id;
name = (timer_index ==SIMPLE_STATE_TMR) ? (fsm->curr_state->name) : fsm->curr_state->composed->name;
if(fsm->reaction_in_state || fsm->state_timers[id] == 0) {
return 0;
}
fsm_trace(fsm, "UnsnchedlTm in state %s for %d msec \n", name, fsm->state_timers[id]);
rc = fsm_tm_unsched(fsm, id);
if(rc == 0) {
fsm_trace(fsm,"Timer was not unscheduled in state %s \n", name);
}
return rc;
}
//--------------------------------------------------------------
// fsm_state_base_entry
// Purpose:
// Entry function called upon entry each state
// possible actions - scheduler timer , call entryState function
// Input : fsm - pointer to user's FSM
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
void fsm_state_base_entry(fsm_base *fsm, fsm_state_base *state)
{
if(state->type == COMPOSED) {
fsm_schedule_timeout(fsm, COMPOSED_STATE_TMR, fsm->state_timers[state->id], state->id);
}
else {
fsm_schedule_timeout(fsm, SIMPLE_STATE_TMR, fsm->state_timers[state->id], state->id);
}
if(state->entry_func) {
((fsm_state_entry)(state->entry_func))(fsm);
}
}
//--------------------------------------------------------------
// fsm_state_base_exit
//
// Purpose: Exit function called upon exit from each state
// possible actions - unscheduler timer , call exitState function
//
// Input : fsm - pointer to user's FSM
// Output :
// Precondition:
//
// Returns: none
//----------------------------------------------------------------------
void fsm_state_base_exit(fsm_base *fsm, fsm_state_base *state)
{
if (state->type == COMPOSED) {
fsm_unschedule_timeout(fsm, COMPOSED_STATE_TMR);
}
else {
fsm_unschedule_timeout(fsm, SIMPLE_STATE_TMR);
}
if (state->exit_func) {
((fsm_state_exit)(state->exit_func))(fsm);
}
}
//##################################################
// State Simple functions
//##################################################
//--------------------------------------------------------------------
// fsm_state_simple_entry
// redefinition of base behavior: first call Base Entry function and
// then - call Entry function of Parent Composed state(if parent defined for this state)
//--------------------------------------------------------------------
void fsm_state_simple_entry(fsm_base *fsm, fsm_state_base *target_state)
{
fsm_state_base_entry(fsm, target_state);
if(target_state->composed) {
fsm_state_composed_entry(fsm, target_state->composed);
}
}
//--------------------------------------------------------------------
// fsm_state_simple_exit
// redefinition of base behavior: first call Base Exit function and
// then - call Exit function of Parent Composed state(if parent defined for this state)itsReactionInState
//--------------------------------------------------------------------
void fsm_state_simple_exit(fsm_base *fsm, fsm_state_base *target_state)
{
fsm_state_base_exit(fsm, fsm->curr_state);
if(fsm->curr_state->composed) {
fsm_state_composed_exit(fsm, target_state);
}
}
//##################################################
// State Composed functions
//##################################################
//--------------------------------------------------------------------
// fsm_state_composed_entry
// if Composed state was changed as a result of event consumption
// then call Entry from the Base class
//--------------------------------------------------------------------
void fsm_state_composed_entry(fsm_base *fsm, fsm_state_base *target_state)
{
if (!(fsm->last_simple_state->composed) ||
(fsm->last_simple_state->composed != target_state)) {
fsm_state_base_entry(fsm, target_state);
}
}
//--------------------------------------------------------------------
// fsm_state_composed_exit
// if Composed state was changed as a result of event consumption then called Exit method
// from the Base class
//
//--------------------------------------------------------------------
void fsm_state_composed_exit(fsm_base *fsm, fsm_state_base *target_state)
{
if( target_state->type != CONDITION) {
if(!(target_state->composed) ||
(target_state->composed != fsm->curr_state->composed) /* use LastSimpleState */
/* ((target_state->itsComposed == fsm->itsCurrStateP)&&(fsm->itsLastConsumed == fsm->itsCurrStateP->itsComposed )) New condition ! to validate arc on composed */
) {
fsm_state_base_exit(fsm, fsm->curr_state->composed);
}
}
}
//*****************************************Timer functions**************************************************************
int fsm_tm_sched(fsm_base *fsm, int timeout, fsm_timer_index timer_index, unsigned short id)
{
timer_mngr *tmr = (timer_mngr*) &(fsm->tmr_mngr);
int rc = 0;
if(timer_index < LAST_INDEX_TMR) {
tmr->client_data[timer_index].fsm_timer_id = id;
tmr->client_data[timer_index].scheduled_status = 1;
tmr->client_data[timer_index].fsm = fsm;
/*return lew_event_reg_timer(tmr->tmr_context, &tmr->itsClientData[theTimerIndex].itsTimerRef, tmr->tmrMngr.itsCallback , (void *)&tmr->itsClientData[theTimerIndex], theTimeout); */
if (tmr->sched_func) {
rc = ((real_tm_sched)tmr->sched_func)(timeout, (void *)&tmr->client_data[timer_index], &tmr->client_data[timer_index].timer_ref);
}
}
return rc;
}
//-----------------------------------------------------------------------------------
int fsm_tm_unsched(fsm_base *fsm, unsigned short id)
{
int i;
int rc = 0;
timer_mngr *tmr = (timer_mngr*) &(fsm->tmr_mngr);
for (i = SIMPLE_STATE_TMR; i < LAST_INDEX_TMR; i++) {
if (tmr->client_data[i].fsm_timer_id == id) {
if(tmr->unsched_func) {
rc = ((real_tm_unsched)tmr->unsched_func)(&tmr->client_data[i].timer_ref);
}
tmr->client_data[i].scheduled_status = 0;
break;
}
}
if (i == LAST_INDEX_TMR) {
// error!
}
return rc;
}
//-----------------------------------------------------------------------------------
int fsm_tm_init(timer_mngr *timer_manager)
{
int i, rc = 0;
for(i = 0; i < LAST_INDEX_TMR; i++) {
timer_manager->client_data[i].fsm = NULL;
timer_manager->client_data[i].scheduled_status = 0;
timer_manager->client_data[i].timer_ref = 0;
}
return rc;
}
//-----------------------------------------------------------------------------------
int fsm_tm_deinit(timer_mngr *timer_manager)
{
int i;
for(i = SIMPLE_STATE_TMR; i < LAST_INDEX_TMR; i++) {
if(timer_manager->client_data[i].scheduled_status) {
/*rc = IPC_removeTimerByRef(itsClientData[i].itsTimerRef);//lew_event_cancel(tmr->tmr_context ,&tmr->itsClientData[theTimerIndex].itsTimerRef);*/
if(timer_manager->unsched_func) {
((real_tm_unsched)timer_manager->unsched_func)(&timer_manager->client_data[i].timer_ref);
}
timer_manager->client_data[i].scheduled_status = 0;
timer_manager->client_data[i].timer_ref = 0;
break;
}
}
timer_manager->unsched_func = NULL;
timer_manager->sched_func = NULL;
return 0;
}