-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsm_tool_framework.c
814 lines (699 loc) · 26.3 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* Copyright (C) January 2014, Leonid Chernin & Evgeny Yoshpe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/****************************************************************
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>
#endif
#include "fsm_tool_framework.h"
int fsm_state_base_entry (struct fsm_base *fsm, struct fsm_state_base *target_state, struct fsm_event_base * ev);
int fsm_state_base_exit (struct fsm_base *fsm, struct fsm_state_base *state, struct fsm_event_base * ev);
int fsm_state_simple_entry (struct fsm_base *fsm, struct fsm_state_base *current_state, struct fsm_state_base *target_state, struct fsm_event_base * ev);
int fsm_state_simple_exit (struct fsm_base *fsm, struct fsm_state_base *current_state, struct fsm_state_base *target_state, struct fsm_event_base * ev);
int fsm_state_composed_entry (struct fsm_base *fsm, struct fsm_state_base *current_state, struct fsm_state_base *target_state, struct fsm_event_base * ev);
int fsm_state_composed_exit (struct fsm_base *fsm, struct fsm_state_base *current_state, struct fsm_state_base *target_state, struct fsm_event_base * ev);
int fsm_schedule_timeout(struct fsm_base *fsm, enum fsm_timer_index timer_index,
int timeout, unsigned char id);
int fsm_unschedule_timeout(struct fsm_base *fsm, enum fsm_timer_index timer_index);
int fsm_tm_sched(struct fsm_base *fsm, int timeout, enum fsm_timer_index timer_index,
unsigned char id);
int fsm_tm_unsched(struct fsm_base *fsm, unsigned char id);
int fsm_tm_init(struct timer_mngr *timer_manager);
int fsm_tm_deinit(struct timer_mngr *timer_manager, void* unsched);
int fsm_state_handle_event(struct fsm_base *fsm, struct fsm_state_base *state,
struct fsm_event_base *ev, struct fsm_state_base** target_state);
struct fsm_state_base * get_state_by_id(struct fsm_base *fsm, unsigned char id);
/*------------------------------------------------------------------------
fsm_init
Purpose: init of struct 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( struct fsm_base *fsm,
struct fsm_state_base *curr_state_entry,
uint8 num_states,
int *state_timer_values, fsm_user_trace user_trace,
void *timer_sched, void *timer_unsched,
struct fsm_static_data * static_data)
{
int rc = 0;
fsm->busy_flag = 0;
fsm->curr_state = curr_state_entry->id;
fsm->prev_state = fsm->curr_state ;
static_data->num_states = num_states;
static_data->tm_sched_func = timer_sched;
static_data->tm_unsched_func = timer_unsched;
static_data->user_trace = user_trace;
//static_data->timer_used = (static_data->tm_sched_func !=NULL);
if(static_data->timer_used) {
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
tm->state_timers = state_timer_values;
fsm_tm_init(&tm->tmr_mngr);
}
fsm->gen_data = static_data;
fsm_trace(fsm, "Fsm Framework called %s\n", __FUNCTION__);
fsm_trace(fsm, "FSM passed to %s state\n", curr_state_entry->name);
return rc;
}
int
fsm_deinit(struct fsm_base *fsm)
{
fsm_trace(fsm, "\nFsm Framework: called %s \n", __FUNCTION__);
if(fsm->curr_state)
{
fsm->gen_data->num_states = 0;
fsm->gen_data->user_trace = NULL;
fsm->gen_data->tm_sched_func = NULL;
fsm->gen_data->tm_unsched_func = NULL;
}
if(fsm->gen_data->timer_used)
{
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
tm->state_timers = NULL;
fsm_tm_deinit(&tm->tmr_mngr, fsm->gen_data->tm_unsched_func);
fsm->gen_data->tm_unsched_func = NULL;
fsm->gen_data->tm_sched_func = NULL;
}
return 0;
}
void
fsm_set_timer_value(struct 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->gen_data->num_states) {
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
tm->state_timers[state] = value;
}
}
int
fsm_get_timer_value(struct fsm_base *fsm, int state)
{
if (state < fsm->gen_data->num_states) {
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
return tm->state_timers[state];
}
return 0;
}
tbool
fsm_is_in_state (struct fsm_base *fsm, unsigned short state)
{
tbool res = 0;
if(fsm && (state <fsm->gen_data->num_states))
{
if (fsm->gen_data->states[state]->default_substate != NULL){ /* state - composed !*/
res = (fsm->gen_data->states[fsm->curr_state]->composed->id == state);
}
else{
res = (state == fsm->curr_state);
}
}
return res;
}
const char *
fsm_get_state(struct fsm_base *fsm, unsigned short *state)
{
if (fsm) {
*state = fsm->curr_state;
return (fsm->gen_data->states[fsm->curr_state]->name);
}
else {
return NULL;
}
}
/*----------------------------------------------------------------------
timer_trigger_operation
Purpose: called when timer message arrived to FSM
Input : <id> Id of the timer
Output :
Precondition:
Returns: none
----------------------------------------------------------------------*/
int
fsm_timer_trigger_operation(struct timer_params *timer_data)
{
struct fsm_timer_event ev;
if (timer_data->fsm_timer_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 = timer_data->fsm_timer_id; /* id of timer = the name of the state where timer was scheduled */
return fsm_handle_event(timer_data->fsm, (struct fsm_event_base*) &ev);
}
/*----------------------------------------------------------------------
fsm_print
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
fsm_print(struct fsm_base* fsm)
{
int len = 0;
char tracer_buffer[300];
len += sprintf(tracer_buffer,
"Curr. state = %s, Prev. state = %s\n",
fsm->gen_data->states[fsm->curr_state]->name,
fsm->gen_data->states[fsm->prev_state]->name);
if (fsm->gen_data->user_trace) {
fsm->gen_data->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(struct 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->gen_data->user_trace) {
fsm->gen_data->user_trace(tracer_buffer, len);
}
va_end(ap);
}
/*----------------------------------------------------------------------
get_state_by_state_id
Purpose: get state structure by state ID
Input :
Output :
Precondition:
Returns: none
----------------------------------------------------------------------*/
struct fsm_state_base *
get_state_by_id(struct fsm_base *fsm, unsigned char id)
{
if(!fsm) {
return NULL;
}
return fsm->gen_data->states[id];
}
/*----------------------------------------------------------------------
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
----------------------------------------------------------------------*/
int
fsm_handle_event(struct fsm_base *fsm, struct fsm_event_base *ev)
{
int err = FSM_CONSUMED;
struct fsm_state_base *target_state = NULL;
struct fsm_state_base *curr_state_p = get_state_by_id(fsm, fsm->curr_state);
if (!ev) {
return FSM_CONSUME_ERROR;
}
fsm_trace(fsm, "Event %s arrived to state %s (%d)\n", ev->name,
curr_state_p->name, curr_state_p->id);
if (ev->opcode == TIMER_EVENT) {
fsm_trace(fsm, " timer_id = %d\n", (( struct fsm_timer_event*)ev)->id);
}
if (fsm->busy_flag) {
fsm_trace(fsm, "FSM Framework Error: try to recursive entry in \n");
return FSM_CONSUME_ERROR;
}
/* fsm->current_event = ev; */
fsm->busy_flag = 1;
if (ev->opcode == AUX_TIMER_EVENT) {
fsm_unschedule_aux_timer(fsm);
}
do {
fsm->reaction_in_state = 0;
err = fsm_state_handle_event(fsm, curr_state_p, ev, &target_state);
if (err > FSM_LAST_FRAMEWORK_ERROR) {
// user returned error from fsm reaction
goto bail;
}
if (target_state != NULL) {
fsm->prev_state = curr_state_p->id;
curr_state_p = target_state;
}
else {
if (curr_state_p->type == CONDITION) {
fsm_trace(fsm, "FSM Framework Error: event %s is not consumed in condition state %s\n",
ev->name, curr_state_p->name);
err = FSM_CONSUME_ERROR;
}
fsm_trace(fsm, "Event %s is not consumed in state %s (%d)\n\n",
ev->name, curr_state_p->name, curr_state_p->id);
err = 0; /* no error on event is not consumed, only log print */
break;
}
if (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", curr_state_p->name);
fsm->curr_state = target_state->id;
}
} while ((target_state->type == CONDITION) && (err == FSM_CONSUMED));
if (target_state) {
fsm_trace(fsm, "FSM passed to %s state\n\n", target_state->name);
fsm->curr_state = target_state->id;
}
bail:
fsm->busy_flag = 0;
return err;
}
/*----------------------------------------------------------------------
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(struct fsm_base *fsm, int timeout)
{
int err = 0;
if (!fsm->gen_data->timer_used) {
fsm_trace(fsm, "Auxiliary timer was not scheduled in state %d because timer is not used in this FSM",
fsm->curr_state);
err = FSM_TIMER_USING_ERROR;
return err;
}
err = fsm_tm_sched(fsm, timeout, AUX_TIMEOUT_ID, 0);
if (err != 0) {
fsm_trace(fsm, "Auxiliary timer %d was not scheduled in state %d",
timeout, fsm->curr_state);
}
return err;
}
/*----------------------------------------------------------------------
fsm_unschedule_aux_timer
Purpose: Scheduler AUX Timeout of FSM - called by user
Input :
Output :
Precondition:
Returns: none
----------------------------------------------------------------------*/
int
fsm_unschedule_aux_timer(struct fsm_base* fsm)
{
int err = 0;
if (!fsm->gen_data->timer_used) {
fsm_trace(fsm, "Auxiliary timer was not unscheduled in state %d because timer is not used in this FSM",
fsm->curr_state );
err = FSM_TIMER_USING_ERROR;
return err;
}
err = fsm_tm_unsched(fsm, (unsigned char)AUX_TIMEOUT_ID);
if (err != 0) {
fsm_trace(fsm, "Auxiliary timer was not unscheduled in state %d",
fsm->curr_state );
}
return err;
}
/****************************************************************************
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
----------------------------------------------------------------------*/
int
fsm_state_handle_event(struct fsm_base *fsm, struct fsm_state_base *state,
struct fsm_event_base *ev, struct fsm_state_base **target_state)
{
int err = 0;
struct fsm_state_base *tmp_target_state = NULL;
err = ((fsm_state_dispatch)(state->state_dispatcher))(fsm, state->id, ev, &tmp_target_state);
if (err > FSM_LAST_FRAMEWORK_ERROR) {
// user returned error from fsm reaction
goto bail;
}
if (tmp_target_state == NULL) {
if (state->composed) {
err = ((fsm_state_dispatch)(state->composed->state_dispatcher))(fsm, state->composed->id, ev, &tmp_target_state);
if (err > FSM_LAST_FRAMEWORK_ERROR) {
// user returned error from fsm reaction
goto bail;
}
}
}
if (tmp_target_state) {
/* 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 ( (state->composed) &&
(state->composed == tmp_target_state) && (fsm->reaction_in_state)) {
tmp_target_state = fsm->gen_data->states[fsm->curr_state];
}
else {
if (tmp_target_state->default_substate) {
tmp_target_state = tmp_target_state->default_substate;
}
if (!fsm->reaction_in_state) {
err = fsm_state_simple_exit(fsm, state, tmp_target_state, ev);
if (err > FSM_LAST_FRAMEWORK_ERROR) {
goto bail;
}
err = fsm_state_simple_entry(fsm, state, tmp_target_state, ev);
if (err > FSM_LAST_FRAMEWORK_ERROR) {
goto bail;
}
}
}
}
bail:
*target_state = tmp_target_state;
return err;
}
/*----------------------------------------------------------------------
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(struct fsm_base *fsm, enum fsm_timer_index timer_index,
int timeout, unsigned char id)
{
int err = 0;
if (fsm->reaction_in_state ||
(timeout == 0)) {
return err;
}
if(!fsm->gen_data->timer_used) {
fsm_trace(fsm, "fsm_tm_sched: timer not used\n");
err = FSM_TIMER_USING_ERROR;
return err;
}
fsm_trace(fsm, "fsm_schedule_timeout in state for %d msec\n", timeout);
err = fsm_tm_sched(fsm, timeout, timer_index, id);
if (err != 0) {
fsm_trace(fsm, " Timer %d was not scheduled\n", timer_index);
}
return err;
}
/*--------------------------------------------------------------
fsm_unschedule_timeout
Purpose: Unscheduler Timeout . automatic framework call
Input : fsm - pointer to user's FSM
Output :
Precondition:
Returns: status
----------------------------------------------------------------------*/
int
fsm_unschedule_timeout(struct fsm_base *fsm, enum fsm_timer_index timer_index)
{
int err = 0;
unsigned char id;
const char *name;
struct fsm_state_base *curr_state_p = get_state_by_id(fsm, fsm->curr_state);
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
if (!fsm->gen_data->timer_used) {
return err;
}
id = (timer_index == SIMPLE_STATE_TMR) ? (curr_state_p->id) : curr_state_p->composed->id;
name = (timer_index == SIMPLE_STATE_TMR) ? (curr_state_p->name) : curr_state_p->composed->name;
if (fsm->reaction_in_state ||
(tm->state_timers[id] == 0)) {
return err;
}
fsm_trace(fsm, "fsm_unschedule_timeout in state %s for %d msec\n", name, tm->state_timers[id]);
err = fsm_tm_unsched(fsm, id);
if (err != 0) {
fsm_trace(fsm, "Timer was not unscheduled in state %s\n", name);
}
return err;
}
/*--------------------------------------------------------------
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 : error
Precondition:
Returns: none
----------------------------------------------------------------------*/
int
fsm_state_base_entry(struct fsm_base *fsm, struct fsm_state_base *state, struct fsm_event_base *ev)
{
int err = 0;
if (fsm->gen_data->timer_used) {
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
if (state->type == COMPOSED) {
err = fsm_schedule_timeout(fsm, COMPOSED_STATE_TMR,
tm->state_timers[state->id], state->id);
if (err != 0) {
goto bail;
}
}
else {
err = fsm_schedule_timeout(fsm, SIMPLE_STATE_TMR,
tm->state_timers[state->id], state->id);
if (err != 0) {
goto bail;
}
}
}
if (state->entry_func) {
err = ((fsm_state_entry)(state->entry_func))(fsm, ev);
}
bail:
return err;
}
/*--------------------------------------------------------------
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 : error
Precondition:
Returns: none
----------------------------------------------------------------------*/
int
fsm_state_base_exit(struct fsm_base *fsm, struct fsm_state_base *state, struct fsm_event_base *ev)
{
int err = 0;
if (fsm->gen_data->timer_used) {
if (state->type == COMPOSED) {
err = fsm_unschedule_timeout(fsm, COMPOSED_STATE_TMR);
if (err != 0) {
goto bail;
}
}else {
err = fsm_unschedule_timeout(fsm, SIMPLE_STATE_TMR);
if (err != 0) {
goto bail;
}
}
}
if (state->exit_func) {
err = ((fsm_state_exit)(state->exit_func))(fsm, ev);
}
bail:
return err;
}
/*---------------------------------------------------
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)
--------------------------------------------------------------------*/
int
fsm_state_simple_entry(struct fsm_base *fsm, struct fsm_state_base *current_state,
struct fsm_state_base *target_state, struct fsm_event_base *ev)
{
int err = 0;
err = fsm_state_base_entry(fsm, target_state, ev);
if (err != 0) {
goto bail;
}
if (target_state->composed) {
err = fsm_state_composed_entry(fsm, current_state , target_state->composed, ev);
}
bail:
return err;
}
/*--------------------------------------------------------------------
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
--------------------------------------------------------------------*/
int
fsm_state_simple_exit(struct fsm_base *fsm, struct fsm_state_base *current_state,
struct fsm_state_base *target_state, struct fsm_event_base *ev)
{
int err = 0;
err = fsm_state_base_exit(fsm, current_state, ev);
if (err != 0) {
goto bail;
}
if (current_state->composed) {
err = fsm_state_composed_exit(fsm, current_state, target_state, ev);
}
bail:
return err;
}
/*-------------------------------------------------
* 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
--------------------------------------------------------------------*/
int
fsm_state_composed_entry(struct fsm_base *fsm, struct fsm_state_base *current_state,
struct fsm_state_base *target_state, struct fsm_event_base *ev)
{
int err = 0;
if (!(current_state->composed) ||
(current_state->composed != target_state)) {
err = fsm_state_base_entry(fsm, target_state, ev);
}
return err;
}
/*--------------------------------------------------------------------
fsm_state_composed_exit
if Composed state was changed as a result of event consumption then called Exit method
from the Base class
--------------------------------------------------------------------*/
int
fsm_state_composed_exit(struct fsm_base *fsm, struct fsm_state_base *current_state,
struct fsm_state_base *target_state, struct fsm_event_base *ev)
{
int err = 0;
if (!(target_state->composed) ||
(target_state->composed != current_state->composed) /* use LastSimpleState */
/* ((target_state->itsComposed == fsm->itsCurrStateP)&&(fsm->itsLastConsumed == fsm->itsCurrStateP->itsComposed )) New condition ! to validate arc on composed */
) {
err = fsm_state_base_exit(fsm, current_state->composed, ev);
}
return err;
}
/******************************************Timer functions**************************************************************/
int
fsm_tm_sched(struct fsm_base *fsm, int timeout, enum fsm_timer_index timer_index,
unsigned char id)
{
int err = 0;
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
struct timer_mngr *tmr = (struct timer_mngr*) &(tm->tmr_mngr);
void *sched = fsm->gen_data->tm_sched_func;
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 (sched) {
err = ((real_tm_sched)sched)(timeout,
(void *)&tmr->client_data[timer_index],
&tmr->client_data[timer_index].timer_ref);
}
}
return err;
}
/* ----------------------------------------------------------------------------------- */
int
fsm_tm_unsched(struct fsm_base *fsm, unsigned char id)
{
int err = 0;
int i;
struct fsm_tm_base * tm = (struct fsm_tm_base *)&fsm[1];
struct timer_mngr *tmr = (struct timer_mngr*) &(tm->tmr_mngr);
void *unsched = fsm->gen_data->tm_unsched_func;
for (i = SIMPLE_STATE_TMR; i < LAST_INDEX_TMR; i++) {
if (tmr->client_data[i].fsm_timer_id == id) {
if (unsched) {
err = ((real_tm_unsched)unsched )(&tmr->client_data[i].timer_ref);
}
tmr->client_data[i].scheduled_status = 0;
break;
}
}
if (i == LAST_INDEX_TMR) {
/* error */
}
return err;
}
/* ----------------------------------------------------------------------------------- */
int
fsm_tm_init(struct 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(struct timer_mngr *timer_manager , void *unsched)
{
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 (unsched) {
((real_tm_unsched)unsched)(&timer_manager->
client_data[i].
timer_ref);
}
timer_manager->client_data[i].scheduled_status = 0;
timer_manager->client_data[i].timer_ref = 0;
break;
}
}
return 0;
}