Skip to content

Commit

Permalink
Moving code within libs.
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Apr 27, 2021
1 parent c676a4a commit 99f92a1
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 230 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cmake.configureOnOpen": true,
"files.associations": {
"hfsm.h": "c",
"queue.h": "c"
"queue.h": "c",
"hfsm_hardcoded.h": "c"
}
}
4 changes: 4 additions & 0 deletions inc/hfsm.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef HFSM_H
#define HFSM_H

/**
* @brief hfsm event type.
*
Expand All @@ -23,3 +26,4 @@ typedef struct hfsm_event hfsm_event;

extern const hfsm_event hfsm_entry_event;
extern const hfsm_event hfsm_exit_event;
#endif //HFSM_H
128 changes: 10 additions & 118 deletions inc/hfsm_hardcoded.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,20 @@ struct hfsm

#define RET_HANDLE_BY_SUPERSTATE -1

int hfsm_init(hfsm* h, state_handler initial_state, get_parent get_parent_func)
{
assert(h != NULL);
h->current_state = initial_state;
h->get_parent_func = get_parent_func;
return h->current_state(h, hfsm_entry_event);
}

int _hfsm_recursive_exit(
int hfsm_init(
hfsm* h,
state_handler source_state)
{
assert(h != NULL);
assert(source_state != NULL);

int ret = 0;
// Super-state handler making a transition must exit all substates:
while (source_state != h->current_state)
{
// The current state cannot be null while walking the hierarchy chain from an sub-state to the
// super-state:
assert(h->current_state != NULL);

ret = h->current_state(h, hfsm_exit_event);
state_handler super_state = h->get_parent_func(h->current_state);
assert(super_state != NULL);

if (ret)
{
break;
}

h->current_state = super_state;
}

return ret;
}
state_handler initial_state,
get_parent get_parent_func);

/*
Supported transitions limited to the following:
- peer states (within the same top-level state).
- super state transitioning to another peer state (all sub-states will exit).
*/
int hfsm_transition_peer(hfsm* h, state_handler source_state, state_handler destination_state)
{
assert(h != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret = 0;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Exit the source state.
ret = h->current_state(h, hfsm_exit_event);
if (!ret)
{
// Enter the destination state:
h->current_state = destination_state;
ret = h->current_state(h, hfsm_entry_event);
}
}

return ret;
}
int hfsm_transition_peer(
hfsm* h,
state_handler source_state,
state_handler destination_state);

/*
Supported transitions limited to the following:
Expand All @@ -95,26 +42,7 @@ int hfsm_transition_peer(hfsm* h, state_handler source_state, state_handler dest
int hfsm_transition_substate(
hfsm* h,
state_handler source_state,
state_handler destination_state)
{
assert(h != NULL);
assert(h->current_state != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Transitions to sub-states will not exit the super-state:
h->current_state = destination_state;
ret = h->current_state(h, hfsm_entry_event);
}

return ret;
}
state_handler destination_state);

/*
Supported transitions limited to the following:
Expand All @@ -124,44 +52,8 @@ int hfsm_transition_substate(
int hfsm_transition_superstate(
hfsm* h,
state_handler source_state,
state_handler destination_state)
{
assert(h != NULL);
assert(h->current_state != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Transitions to super states will exit the substate but not enter the superstate again:
ret = h->current_state(h, hfsm_exit_event);
h->current_state = destination_state;
}

return ret;
}

int hfsm_post_event(hfsm* h, hfsm_event event)
{
assert(h != NULL);
int ret;

ret = h->current_state(h, event);

state_handler current = h->current_state;
while (ret == RET_HANDLE_BY_SUPERSTATE)
{
state_handler super = h->get_parent_func(current);
assert(super != NULL);
current = super;
ret = current(h, event);
}
state_handler destination_state);

return ret;
}
int hfsm_post_event(hfsm* h, hfsm_event event);

#endif //!HFSM_HARDCODED_H
114 changes: 8 additions & 106 deletions inc/hfsm_stack.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef hfsm_STACK_H
#define hfsm_STACK_H
#ifndef HFSM_STACK_H
#define HFSM_STACK_H

#include <stdint.h>
#include <stdbool.h>
Expand All @@ -15,70 +15,14 @@ struct hfsm
state_handler current_state;
};

int hfsm_init(hfsm* h, state_handler initial_state)
{
assert(h != NULL);
h->current_state = initial_state;
return h->current_state(h, hfsm_entry_event, NULL);
}

int _hfsm_recursive_exit(
hfsm* h,
state_handler source_state)
{
assert(h != NULL);
assert(source_state != NULL);

int ret = 0;
// Super-state handler making a transition must exit all substates:
while (source_state != h->current_state)
{
// The current state cannot be null while walking the hierarchy chain from an sub-state to the
// super-state:
assert(h->current_state != NULL);

state_handler super_state;
ret = h->current_state(h, hfsm_exit_event, &super_state);
if (ret)
{
break;
}

h->current_state = super_state;
}

return ret;
}
int hfsm_init(hfsm* h, state_handler initial_state);

/*
Supported transitions limited to the following:
- peer states (within the same top-level state).
- super state transitioning to another peer state (all sub-states will exit).
*/
int hfsm_transition_peer(hfsm* h, state_handler source_state, state_handler destination_state)
{
assert(h != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret = 0;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Exit the source state.
ret = h->current_state(h, hfsm_exit_event, NULL);
if (!ret)
{
// Enter the destination state:
h->current_state = destination_state;
ret = h->current_state(h, hfsm_entry_event, NULL);
}
}

return ret;
}
int hfsm_transition_peer(hfsm* h, state_handler source_state, state_handler destination_state);

/*
Supported transitions limited to the following:
Expand All @@ -88,26 +32,7 @@ int hfsm_transition_peer(hfsm* h, state_handler source_state, state_handler dest
int hfsm_transition_substate(
hfsm* h,
state_handler source_state,
state_handler destination_state)
{
assert(h != NULL);
assert(h->current_state != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Transitions to sub-states will not exit the super-state:
h->current_state = destination_state;
ret = h->current_state(h, hfsm_entry_event, NULL);
}

return ret;
}
state_handler destination_state);

/*
Supported transitions limited to the following:
Expand All @@ -117,31 +42,8 @@ int hfsm_transition_substate(
int hfsm_transition_superstate(
hfsm* h,
state_handler source_state,
state_handler destination_state)
{
assert(h != NULL);
assert(h->current_state != NULL);
assert(source_state != NULL);
assert(destination_state != NULL);

int ret;
// Super-state handler making a transition must exit all inner states:
ret = _hfsm_recursive_exit(h, source_state);

if (source_state == h->current_state)
{
// Transitions to super states will exit the substate but not enter the superstate again:
ret = h->current_state(h, hfsm_exit_event, NULL);
h->current_state = destination_state;
}
state_handler destination_state);

return ret;
}

int hfsm_post_event(hfsm* h, hfsm_event event)
{
assert(h != NULL);
return h->current_state(h, event, NULL);
}
int hfsm_post_event(hfsm* h, hfsm_event event);

#endif //!hfsm_STACK_H
#endif //!HFSM_STACK_H
5 changes: 2 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
file(GLOB Sources "*.c")

include_directories("../inc")

add_library(hfsm STATIC ${Sources})
add_library(hfsm_hardcoded STATIC hfsm_common_events.c hfsm_hardcoded.c)
add_library(hfsm_stack STATIC hfsm_common_events.c hfsm_stack.c)
Loading

0 comments on commit 99f92a1

Please sign in to comment.