From 99f92a1142469ac91977a607fd5d5e4f5519b630 Mon Sep 17 00:00:00 2001 From: Cristian Pop Date: Tue, 27 Apr 2021 11:44:11 -0700 Subject: [PATCH] Moving code within libs. --- .vscode/settings.json | 3 +- inc/hfsm.h | 4 ++ inc/hfsm_hardcoded.h | 128 +++-------------------------------- inc/hfsm_stack.h | 114 +++----------------------------- src/CMakeLists.txt | 5 +- src/hfsm_hardcoded.c | 150 ++++++++++++++++++++++++++++++++++++++++++ src/hfsm_stack.c | 134 +++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 4 +- 8 files changed, 312 insertions(+), 230 deletions(-) create mode 100644 src/hfsm_hardcoded.c create mode 100644 src/hfsm_stack.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 7d0b6ae..0196a73 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,7 @@ "cmake.configureOnOpen": true, "files.associations": { "hfsm.h": "c", - "queue.h": "c" + "queue.h": "c", + "hfsm_hardcoded.h": "c" } } \ No newline at end of file diff --git a/inc/hfsm.h b/inc/hfsm.h index f22e042..05f701b 100644 --- a/inc/hfsm.h +++ b/inc/hfsm.h @@ -1,3 +1,6 @@ +#ifndef HFSM_H +#define HFSM_H + /** * @brief hfsm event type. * @@ -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 diff --git a/inc/hfsm_hardcoded.h b/inc/hfsm_hardcoded.h index 4155103..2343b38 100644 --- a/inc/hfsm_hardcoded.h +++ b/inc/hfsm_hardcoded.h @@ -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: @@ -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: @@ -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 diff --git a/inc/hfsm_stack.h b/inc/hfsm_stack.h index f41bf9b..bd9afb4 100644 --- a/inc/hfsm_stack.h +++ b/inc/hfsm_stack.h @@ -1,5 +1,5 @@ -#ifndef hfsm_STACK_H -#define hfsm_STACK_H +#ifndef HFSM_STACK_H +#define HFSM_STACK_H #include #include @@ -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: @@ -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: @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d246a0e..2baf8ca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/hfsm_hardcoded.c b/src/hfsm_hardcoded.c new file mode 100644 index 0000000..a6901c5 --- /dev/null +++ b/src/hfsm_hardcoded.c @@ -0,0 +1,150 @@ +#include +#include +#include + +#include + +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( + 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; +} + +/* + 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; +} + +/* + Supported transitions limited to the following: + - peer state transitioning to first-level inner state. + - super state transitioning to another first-level inner state. +*/ +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; +} + +/* + Supported transitions limited to the following: + - peer state transitioning to first-level inner state. + - super state transitioning to another first-level inner state. +*/ +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); + } + + return ret; +} diff --git a/src/hfsm_stack.c b/src/hfsm_stack.c new file mode 100644 index 0000000..ebcb0aa --- /dev/null +++ b/src/hfsm_stack.c @@ -0,0 +1,134 @@ +#include +#include +#include + +#include + +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; +} + +/* + 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; +} + +/* + Supported transitions limited to the following: + - peer state transitioning to first-level inner state. + - super state transitioning to another first-level inner state. +*/ +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; +} + +/* + Supported transitions limited to the following: + - peer state transitioning to first-level inner state. + - super state transitioning to another first-level inner state. +*/ +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; + } + + return ret; +} + +int hfsm_post_event(hfsm* h, hfsm_event event) +{ + assert(h != NULL); + return h->current_state(h, event, NULL); +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 263ccda..710b2b5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,9 +4,9 @@ add_executable(test_queue test_queue.c) add_test(NAME test_queue COMMAND test_queue) add_executable(test_hfsm_stack test_hfsm_stack.c) -target_link_libraries(test_hfsm_stack hfsm) +target_link_libraries(test_hfsm_stack hfsm_stack) add_test(NAME test_hfsm_stack COMMAND test_hfsm_stack) add_executable(test_hfsm_hardcoded test_hfsm_hardcoded.c) -target_link_libraries(test_hfsm_hardcoded hfsm) +target_link_libraries(test_hfsm_hardcoded hfsm_hardcoded) add_test(NAME test_hfsm_hardcoded COMMAND test_hfsm_hardcoded)