Skip to content

Commit

Permalink
Changes to test_hsm
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Apr 26, 2021
1 parent 9ac6b52 commit a99c644
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
9 changes: 9 additions & 0 deletions doc/hfsm_design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 1. hfsm_stack : CPU stack based implementation

This implementation requires n + 2 stack frames, where n is the depth of the hierarchical state machine.

## 2. hfsm_hardcoded: Hardcoded hierarchy definition

Stack usage improvements:

1. Superstate event handling (`default:` switch case) can be postponed after the stack-frame exits, within the `hfsm_post_event()` function by using return codes. The deep CPU stack can be converted into a `for` loop.
6 changes: 3 additions & 3 deletions doc/hfsm_test.puml
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ state TestHFSM {
state S21
S21 : <b>entry/</b> ref21++
S21 : <b>exit/</b> ref21--
S21 : <b>TInternal2/</b> tinternal2++

state S22
S22 : <b>entry/</b> ref22++
S22 : <b>exit/</b> ref22--
S22 : <b>TInternal2/</b> tinternal2++

[*] -> S21
S21 -right-> S22 : TPeer2
S22 --> S11 : TSuper2
}
S11 : <b>entry/</b> ref11++
S11 : <b>exit/</b> ref11--
S11 : <b>TInternal1/</b> tinternal1++

state S12
S12 : <b>entry/</b> ref12++
S12 : <b>exit/</b> ref12--
S12 : <b>TInternal1/</b> tinternal1++

[*] --> S11
S11 -right-> S12 : TPeer1
S12 -->S01 : TSuper1
}
S01 : <b>entry/</b> ref01++
S01 : <b>exit/</b> ref01--
S01 : <b>TInternal0/</b> tinternal0++

state S02
S02 : <b>entry/</b> ref02++
S02 : <b>exit/</b> ref02--
S02 : <b>TInternal0/</b> tinternal0++

[*] --> S01
S01 -right-> S02 : TPeer0
Expand Down
3 changes: 3 additions & 0 deletions doc/test_strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Test matrix for all supported transitions:
| TSub0 | Init |
| TSub1 | S01 |
| TSub2 | S11 |
| TInternal0 | S01, S11, S21, S22 |
| TInternal1 | S11, S21, S22 |
| TInternal2 | S21 |
| TSuper1 | S12 |
| TSuper2 | S22 |
| TPeer0 | S01, S11, S12, S21, S22 |
Expand Down
Binary file modified doc/testhfsm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 49 additions & 12 deletions test/test_hfsm_stack.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdint.h>
#include <assert.h>

#include <test_assert.h>

#include <hfsm_stack.h>
Expand Down Expand Up @@ -27,6 +29,11 @@ typedef enum
T_INTERNAL_0 = HFSM_EVENT(0),
T_INTERNAL_1 = HFSM_EVENT(1),
T_INTERNAL_2 = HFSM_EVENT(2),
T_SUPER_1 = HFSM_EVENT(3),
T_SUPER_2 = HFSM_EVENT(4),
T_PEER_0 = HFSM_EVENT(5),
T_PEER_1 = HFSM_EVENT(6),
T_PEER_2 = HFSM_EVENT(7),
} test_hfsm_event_type;

static int ref01 = 0;
Expand All @@ -52,16 +59,24 @@ static int S01(hfsm* me, hfsm_event event, int(** super_state)())
{
case HFSM_ENTRY:
ref01++;
return hfsm_transition_substate(me, S01, S11);
ret = hfsm_transition_substate(me, S01, S11);
break;

case HFSM_EXIT:
ref01--;
break;

case T_PEER_0:
ret = hfsm_transition_peer(me, S01, S02);
break;

case T_INTERNAL_0:
tinternal0++;
break;

default:
// Unknown event.
ASSERT_TRUE(0);
printf("Unknown event %d", event.type);
assert(0);
}

return ret;
Expand All @@ -86,13 +101,9 @@ static int S02(hfsm* me, hfsm_event event, int(** super_state)())
ref02--;
break;

case T_INTERNAL_0:
tinternal0++;
break;

default:
// TOP level - ignore unknown events.
ASSERT_TRUE(0);
printf("Unknown event %d", event.type);
assert(0);
}

return ret;
Expand All @@ -111,12 +122,21 @@ static int S11(hfsm* me, hfsm_event event, int(** super_state)())
{
case HFSM_ENTRY:
ref11++;
ret = hfsm_transition_substate(me, S11, S21);
break;

case HFSM_EXIT:
ref11--;
break;

case T_PEER_1:
ret = hfsm_transition_peer(me, S11, S12);
break;

case T_INTERNAL_1:
tinternal1++;
break;

default:
ret = S01(me, event, NULL);
}
Expand All @@ -143,6 +163,10 @@ static int S12(hfsm* me, hfsm_event event, int(** super_state)())
ref12--;
break;

case T_SUPER_1:
ret = hfsm_transition_superstate(me, S12, S01);
break;

default:
ret = S01(me, event, NULL);
}
Expand All @@ -162,11 +186,19 @@ static int S21(hfsm* me, hfsm_event event, int(** super_state)())
switch ((int)event.type)
{
case HFSM_ENTRY:
ref01++;
ref21++;
break;

case HFSM_EXIT:
ref01--;
ref21--;
break;

case T_PEER_2:
ret = hfsm_transition_peer(me, S21, S22);
break;

case T_INTERNAL_2:
tinternal2++;
break;

default:
Expand Down Expand Up @@ -195,6 +227,10 @@ static int S22(hfsm* me, hfsm_event event, int(** super_state)())
ref01--;
break;

case T_SUPER_2:
ret = hfsm_transition_superstate(me, S22, S11);
break;

default:
ret = S11(me, event, NULL);
}
Expand All @@ -205,8 +241,9 @@ static int S22(hfsm* me, hfsm_event event, int(** super_state)())

int test_hfsm_stack_internal()
{
ASSERT_TRUE(!hfsm_init(&test_hfsm, S01));

return 0;
return ret;
}

int main()
Expand Down

0 comments on commit a99c644

Please sign in to comment.