redesign fsm to use transition table

master
Francisco Paisana 5 years ago committed by Francisco Paisana
parent 49a375ce1a
commit 04e192eb0f

@ -31,35 +31,26 @@
namespace srslte {
//! Transition Type
template <typename NextState>
struct to_state {
using next_state = NextState;
};
template <typename... NextStates>
struct to_states {
template <typename ChosenState>
to_states(to_state<ChosenState>) : state_idx(get_type_index<ChosenState, NextStates...>())
{}
template <typename State>
bool is() const
{
return get_type_index<State, NextStates...>() == state_idx;
}
size_t get_type_idx() const { return state_idx; }
size_t state_idx;
};
//! Forward declaration
//! Forward declarations
template <typename Derived>
class fsm_t;
namespace fsm_details {
//! Meta-function to filter transition list <Rows...> by <Event, SrcState> types
template <class Event, class SrcState, class...>
struct filter_transition_type;
template <class Event, class SrcState, class... Rows>
struct filter_transition_type<Event, SrcState, type_list<Rows...> > {
template <class Row>
using predicate = typename Row::template is_match<SrcState, Event>;
using type = typename type_utils::filter<predicate, Rows...>::type;
};
template <class Event, class SrcState>
struct filter_transition_type<Event, SrcState, type_list<> > {
using type = type_list<>;
};
//! Visitor to get a state's name string
struct state_name_visitor {
template <typename State>
@ -67,39 +58,34 @@ struct state_name_visitor {
{
name = get_type_name(s);
}
std::string name = "invalid";
};
//! Visitor to convert a to_state<States...> back to a single state
template <typename FSM, typename PrevState>
struct to_state_visitor {
to_state_visitor(FSM* f_, PrevState* p_) : f(f_), p(p_) {}
template <typename State>
void operator()();
FSM* f;
PrevState* p;
};
//! Enable/Disable meta-function if <State> is part of <FSM> state list
template <typename FSM, typename State, typename T = void>
using enable_if_fsm_state = typename std::enable_if<FSM::template can_hold_state<State>(), T>::type;
template <typename FSM, typename State, typename T = void>
using disable_if_fsm_state = typename std::enable_if<not FSM::template can_hold_state<State>(), T>::type;
//! Helper metafunctions
template <typename FSM, typename State>
using enable_if_fsm_state = typename std::enable_if<FSM::template can_hold_state<State>()>::type;
template <typename FSM, typename State>
using disable_if_fsm_state = typename std::enable_if<not FSM::template can_hold_state<State>()>::type;
template <typename FSM>
constexpr bool is_fsm()
{
return std::is_base_of<fsm_t<FSM>, FSM>::value;
}
template <typename FSM>
constexpr typename std::enable_if<is_fsm<FSM>(), bool>::type is_subfsm()
{
return FSM::is_nested;
}
template <typename FSM>
constexpr typename std::enable_if<not is_fsm<FSM>(), bool>::type is_subfsm()
{
return false;
}
template <typename FSM>
using enable_if_subfsm = typename std::enable_if<is_subfsm<FSM>()>::type;
template <typename FSM>
@ -109,8 +95,10 @@ struct fsm_helper {
//! Metafunction to determine if FSM can hold given State type
template <typename FSM>
using fsm_state_list_type = decltype(std::declval<typename FSM::derived_view>().states);
template <typename FSM>
using fsm_transitions = typename FSM::derived_view::transitions;
//! Call FSM/State enter method
//! Call FSM/State enter method. If subfsm, call enter of subfsm init state as well
template <typename FSM, typename State>
static enable_if_subfsm<State> call_enter(FSM* f, State* s)
{
@ -122,55 +110,135 @@ struct fsm_helper {
// call initial substate enter
call_enter(s->derived(), &s->derived()->states.template get_unchecked<init_type>());
}
template <typename FSM, typename State, typename... Args>
static disable_if_subfsm<State> call_enter(FSM* f, State* s)
{
f->enter(*s);
}
//! TargetState is type-erased (a choice). Apply its stored type to the fsm current state
template <typename FSM, typename... Args, typename PrevState>
static void handle_state_change(FSM* f, to_states<Args...>* s, PrevState* p)
//! Simple state transition in FSM
template <typename FSM, typename SrcState, typename DestState>
static auto handle_state_transition(FSM* f) -> enable_if_fsm_state<FSM, DestState>
{
to_state_visitor<FSM, PrevState> visitor{f, p};
srslte::static_visit(visitor, *s);
f->exit(f->states.template get_unchecked<SrcState>());
f->states.template transit<DestState>();
call_enter(f, &f->states.template get_unchecked<DestState>());
}
//! Simple state transition in FSM (no same_state of entry in nested FSM)
template <typename FSM, typename State, typename PrevState>
static auto handle_state_change(FSM* f, to_state<State>* s, PrevState* p) -> enable_if_fsm_state<FSM, State>
//! State not present in current FSM. Attempt state transition in parent FSM in the case of NestedFSM
template <typename FSM, typename SrcState, typename DestState>
static auto handle_state_transition(FSM* f) -> disable_if_fsm_state<FSM, DestState>
{
if (std::is_same<State, PrevState>::value) {
f->log_h->info("FSM \"%s\": No transition occurred while in state \"%s\"\n",
get_type_name<typename FSM::derived_t>().c_str(),
get_type_name<State>().c_str());
return;
}
f->exit(f->states.template get_unchecked<PrevState>());
f->states.template transit<State>();
f->log_h->info("FSM \"%s\": Detected transition \"%s\" -> \"%s\"",
get_type_name<typename FSM::derived_t>().c_str(),
get_type_name<PrevState>().c_str(),
get_type_name<State>().c_str());
call_enter(f, &f->states.template get_unchecked<State>());
static_assert(FSM::is_nested, "State is not present in the FSM list of valid states");
f->exit(f->states.template get_unchecked<SrcState>());
handle_state_transition<typename FSM::parent_t::derived_view, typename FSM::derived_t, DestState>(
f->parent_fsm()->derived());
}
//! State not present in current FSM. Attempt state transition in parent FSM in the case of NestedFSM
template <typename FSM, typename State, typename PrevState>
static auto handle_state_change(FSM* f, to_state<State>* s, PrevState* p) -> disable_if_fsm_state<FSM, State>
template <typename State, typename FSM>
static auto get_state_recursive(FSM* f) -> enable_if_fsm_state<FSM, State, State*>
{
return &f->states.template get_unchecked<State>();
}
template <typename State, typename FSM>
static auto get_state_recursive(FSM* f) -> disable_if_fsm_state<FSM, State, State*>
{
static_assert(FSM::is_nested, "State is not present in the FSM list of valid states");
f->exit(f->states.template get_unchecked<PrevState>());
handle_state_change(f->parent_fsm()->derived(), s, static_cast<typename FSM::derived_t*>(f));
return get_state_recursive<State>(f->parent_fsm()->derived());
}
//! Trigger in first Row for which the Guard passes
template <typename FSM, typename... Types>
struct apply_first_guard_pass;
template <typename FSM, typename First, typename... Rows>
struct apply_first_guard_pass<FSM, type_list<First, Rows...> > {
template <typename SrcState>
static bool trigger(FSM* f, SrcState& s, const typename First::event_t& ev)
{
using src_state = SrcState;
using dest_state = typename First::dest_state_t;
using event_type = typename First::event_t;
bool triggered = First::react(f, s, ev);
if (triggered) {
// Log Transition
if (std::is_same<src_state, dest_state>::value) {
f->log_h->info("FSM \"%s\": Event \"%s\" updated state \"%s\"\n",
get_type_name<typename FSM::derived_t>().c_str(),
get_type_name<event_type>().c_str(),
get_type_name<src_state>().c_str());
} else {
f->log_h->info("FSM \"%s\": Transition %s-->%s (cause: %s)",
get_type_name<typename FSM::derived_t>().c_str(),
get_type_name<src_state>().c_str(),
get_type_name<dest_state>().c_str(),
get_type_name<event_type>().c_str());
// Apply state change operations
handle_state_transition<FSM, src_state, dest_state>(f);
}
return true;
}
return apply_first_guard_pass<FSM, type_list<Rows...> >::trigger(f, s, ev);
}
// template<typename SrcState>
// bool test_react(FSM *f, SrcState &s, const typename First::event_t &ev) {
// using dest_state = typename First::dest_state_t;
// if (First::guard == nullptr or (f->*First::guard)(ev)) {
// // Guard Passed. Apply react method
// dest_state *d = get_state_recursive<dest_state, FSM>(f);
// if (First::react != nullptr) {
// (f->*First::react)(*d, ev);
// }
// return true;
// }
// return false;
// }
//
// template<>
// bool
// test_react<typename First::src_state_t>(FSM *f, typename First::src_state_t &s, const typename First::event_t
// &ev) {
// using dest_state = typename First::dest_state_t;
//
// if (First::guard == nullptr or (f->*First::guard)(s, ev)) {
// // Guard Passed. Apply react method
// dest_state *d = get_state_recursive<dest_state, FSM>(f);
// if (First::react != nullptr) {
// (f->*First::react)(s, *d, ev);
// }
// return true;
// }
// return false;
// }
};
template <typename FSM>
struct apply_first_guard_pass<FSM, type_list<> > {
template <typename SrcState, typename Event>
static bool trigger(FSM* f, SrcState& s, const Event& ev)
{
f->log_fsm_activity("FSM \"%s\": Unhandled event caught: \"%s\"\n",
get_type_name<typename FSM::derived_t>().c_str(),
get_type_name<Event>().c_str());
return false;
}
};
//! Trigger Event, that will result in a state transition
template <typename FSM, typename Event>
struct trigger_visitor {
using event_t = typename std::decay<Event>::type;
trigger_visitor(FSM* f_, Event&& ev_) : f(f_), ev(std::forward<Event>(ev_)) {}
/**
* @brief Trigger visitor callback for the current state.
* @description tries to find an fsm::trigger method in case the current state is a nested fsm. If it does not
* find it, searches for a react(current_state&, event) method at the current level
* @description tries to find an fsm::trigger method in case the current state is a subfsm. If it does not
* find it, searches for a react(current_state&, dest_state&, event) method at the current level
* Stores True in "result" if state changed. False otherwise
*/
template <typename CurrentState>
@ -178,6 +246,7 @@ struct fsm_helper {
{
result = call_react(s);
}
template <typename CurrentState>
enable_if_subfsm<CurrentState> operator()(CurrentState& s)
{
@ -188,23 +257,12 @@ struct fsm_helper {
}
}
template <typename State>
using enable_if_react = decltype(std::declval<FSM>().react(std::declval<State&>(), std::declval<Event&&>()),
bool());
//! In case there is a react method
template <typename State>
auto call_react(State& s) -> decltype(std::declval<FSM>().react(s, std::declval<Event&&>()), bool())
{
auto target_state = f->react(s, std::forward<Event>(ev));
fsm_helper::handle_state_change(f, &target_state, &s);
return true;
}
template <typename... Args>
bool call_react(Args...)
template <typename SrcState>
bool call_react(SrcState& s)
{
f->log_fsm_activity(
"FSM \"%s\": Unhandled event caught: \"%s\"\n", get_type_name<FSM>().c_str(), get_type_name<Event>().c_str());
return false;
using trigger_list =
typename filter_transition_type<event_t, SrcState, typename FSM::derived_view::transitions>::type;
return apply_first_guard_pass<FSM, trigger_list>::trigger(f, s, ev);
}
FSM* f;
@ -213,25 +271,8 @@ struct fsm_helper {
};
};
template <typename FSM, typename PrevState>
template <typename State>
void to_state_visitor<FSM, PrevState>::operator()()
{
to_state<State> t;
fsm_helper::handle_state_change(f, &t, p);
}
} // namespace fsm_details
//! Gets the typename currently stored in the choice_t
template <typename... Args>
std::string get_type_name(const srslte::to_states<Args...>& t)
{
fsm_details::state_name_visitor v{};
srslte::visit(v, t);
return v.name;
}
template <typename Derived, typename ParentFSM>
class nested_fsm_t;
@ -244,7 +285,6 @@ protected:
template <typename SubFSM>
using subfsm_t = nested_fsm_t<SubFSM, Derived>;
public:
//! get access to derived protected members from the base
class derived_view : public Derived
{
@ -255,15 +295,75 @@ public:
// propagate user fsm methods
using Derived::enter;
using Derived::exit;
using Derived::react;
using Derived::states;
using Derived::transitions;
};
//! Params of a state transition
template <typename SrcState,
typename DestState,
typename Event,
void (Derived::*ReactFn)(SrcState&, DestState&, const Event&) = nullptr,
bool (Derived::*GuardFn)(SrcState&, const Event&) const = nullptr>
struct row {
using src_state_t = SrcState;
using dest_state_t = DestState;
using event_t = Event;
constexpr static void (Derived::*react_fn)(SrcState&, DestState&, const Event&) = ReactFn;
constexpr static bool (Derived::*guard_fn)(SrcState&, const Event&) const = GuardFn;
static bool react(derived_view* f, src_state_t& s, const event_t& ev)
{
if (guard_fn == nullptr or (f->*guard_fn)(s, ev)) {
dest_state_t* d = fsm_details::fsm_helper::get_state_recursive<dest_state_t, derived_view>(f);
if (react_fn != nullptr) {
(f->*react_fn)(s, *d, ev);
}
return true;
}
return false;
}
template <typename SrcState2, typename Event2>
using is_match = std::is_same<type_list<SrcState2, Event2>, type_list<src_state_t, event_t> >;
};
template <typename DestState,
typename Event,
void (Derived::*ReactFn)(DestState&, const Event&) = nullptr,
bool (Derived::*GuardFn)(const Event&) const = nullptr>
struct from_any_state {
using dest_state_t = DestState;
using event_t = Event;
constexpr static void (Derived::*react_fn)(DestState&, const Event&) = ReactFn;
constexpr static bool (Derived::*guard_fn)(const Event&) const = GuardFn;
template <typename SrcState>
static bool react(derived_view* f, SrcState& s, const event_t& ev)
{
if (guard_fn == nullptr or (f->*guard_fn)(ev)) {
dest_state_t* d = fsm_details::fsm_helper::get_state_recursive<dest_state_t, derived_view>(f);
if (react_fn != nullptr) {
(f->*react_fn)(*d, ev);
}
return true;
}
return false;
}
template <typename SrcState2, typename Event2>
using is_match = std::is_same<Event2, event_t>;
};
template <typename... Rows>
using transition_table = type_list<Rows...>;
public:
static const bool is_nested = false;
template <typename NextState>
using to_state = srslte::to_state<NextState>;
template <typename... NextStates>
using to_states = srslte::to_states<NextStates...>;
//! Struct used to store FSM states
template <typename... States>
@ -372,14 +472,16 @@ public:
return fsm_details::fsm_helper::fsm_state_list_type<fsm_t<Derived> >::template can_hold_type<State>();
}
void set_fsm_event_log_level(srslte::LOG_LEVEL_ENUM e) { fsm_event_log_level = e; }
void set_fsm_event_log_level(srslte::LOG_LEVEL_ENUM e) { fsm_event_log_level = e; }
srslte::log_ref get_log() const { return log_h; }
protected:
friend struct fsm_details::fsm_helper;
// Access to CRTP derived class
derived_view* derived() { return static_cast<derived_view*>(this); }
derived_view* derived() { return static_cast<derived_view*>(this); }
const derived_view* derived() const { return static_cast<const derived_view*>(this); }
template <typename State>
@ -387,12 +489,14 @@ protected:
{
// do nothing by default
}
template <typename State>
void exit(State& s)
{
// do nothing by default
}
//! Log FSM activity method, e.g. state transitions
template <typename... Args>
void log_fsm_activity(const char* format, Args&&... args)
{
@ -430,7 +534,8 @@ public:
// Get pointer to outer FSM in case of HSM
const parent_t* parent_fsm() const { return fsm_ptr; }
parent_t* parent_fsm() { return fsm_ptr; }
parent_t* parent_fsm() { return fsm_ptr; }
protected:
using parent_fsm_t = ParentFSM;
@ -443,6 +548,7 @@ protected:
template <typename Proc>
struct proc_complete_ev {
proc_complete_ev(bool success_) : success(success_) {}
bool success;
};
@ -450,6 +556,7 @@ struct proc_complete_ev {
template <typename... Args>
struct proc_launch_ev {
std::tuple<Args...> args;
explicit proc_launch_ev(Args&&... args_) : args(std::forward<Args>(args_)...) {}
};
@ -465,13 +572,6 @@ protected:
using fsm_t<Derived>::enter;
using fsm_t<Derived>::exit;
template <typename State>
auto react(State&, srslte::proc_launch_ev<int*> e) -> to_state<State>
{
log_h->warning("Unhandled event \"launch\" caught when procedure is already running\n");
return {};
}
public:
using base_t = proc_fsm_t<Derived, Result>;
using fsm_t<Derived>::trigger;
@ -480,8 +580,10 @@ public:
struct reset_ev {};
// states
struct idle_st {};
struct complete_st {};
struct idle_st {
bool success = false;
Result result = {};
};
explicit proc_fsm_t(srslte::log_ref log_) : fsm_t<Derived>(log_) {}
@ -494,39 +596,31 @@ public:
}
protected:
void enter(idle_st& s)
{
if (launch_counter > 0) {
log_h->info("Finished run no. %d %s\n", launch_counter, s.success ? "successfully" : "with an error");
}
}
void exit(idle_st& s)
{
launch_counter++;
log_h->info("Starting run no. %d\n", launch_counter);
log_h->info("FSM \"%s\": Starting run no. %d\n", get_type_name<Derived>().c_str(), launch_counter);
}
void enter(complete_st& s) { trigger(reset_ev{}); }
auto react(complete_st& s, reset_ev ev) -> to_state<idle_st> { return {}; }
bool is_success() const { return base_t::template get_state<idle_st>()->success; }
srslte::to_state<complete_st> set_success(Result&& r = {})
{
result = std::forward<Result>(r);
success = true;
return {};
}
srslte::to_state<complete_st> set_failure()
{
success = false;
return {};
}
bool is_success() const { return success; }
const Result& get_result() const
{
if (is_success()) {
return result;
return base_t::template get_state<idle_st>->result;
}
THROW_BAD_ACCESS("in proc_fsm_t::get_result");
}
private:
int launch_counter = 0;
bool success = false;
Result result = {};
int launch_counter = 0;
};
} // namespace srslte

@ -45,6 +45,10 @@ public:
std::abort()
#endif
/************************************
* get_type_name methods
***********************************/
//! Helper to print the name of a type for logging
/**
* @brief Helper function that returns a type name string
@ -97,9 +101,26 @@ std::string get_type_name(const T& t)
return get_type_name<T>();
}
/************************************
* type_list<Args...>
***********************************/
constexpr size_t invalid_type_index = std::numeric_limits<size_t>::max();
namespace type_utils_details {
template <typename... Args>
struct type_list {};
template <typename... Args>
constexpr size_t type_list_size(type_list<Args...> t)
{
return sizeof...(Args);
}
/************************************
* get_index_type/get_type_index
***********************************/
namespace type_utils {
//! Get Index of a type in a list of types (in reversed order)
template <typename T, typename... Types>
@ -131,6 +152,70 @@ struct get_type_reverse<I> {
using type = void;
};
} // namespace type_utils
//! Get index of T in Types...
template <typename T, typename... Types>
constexpr size_t get_type_index()
{
using namespace type_utils;
return (type_list_reverse_index<T, Types...>::index == invalid_type_index)
? invalid_type_index
: sizeof...(Types) - type_list_reverse_index<T, Types...>::index - 1;
}
//! If T is in Types...
template <typename T, typename... Types>
constexpr bool type_list_contains()
{
return get_type_index<T, Types...>() != invalid_type_index;
}
//! Get type of index I in Types...
template <size_t I, typename... Types>
struct get_index_type {
using type = typename type_utils::get_type_reverse<sizeof...(Types) - I - 1, Types...>::type;
};
/**************************
* Filter utils
*************************/
namespace type_utils {
//! pushes type to front of type_list
template <class...>
struct push_front;
template <class T, class... Types>
struct push_front<T, type_list<Types...> > {
using type = type_list<T, Types...>;
};
template <>
struct push_front<> {
using type = type_list<>;
};
//! Creates a type_list with the "Types..." for which Predicate<Type>::value is true
template <template <typename> class Predicate, class...>
struct filter;
template <template <typename> class Predicate, class First, class... Types>
struct filter<Predicate, First, Types...> {
using type = typename std::conditional<Predicate<First>::value,
typename push_front<First, typename filter<Predicate, Types...>::type>::type,
typename filter<Predicate, Types...>::type>::type;
};
template <template <typename> class Predicate>
struct filter<Predicate> {
using type = type_list<>;
};
} // namespace type_utils
/************************************
* static_visit
***********************************/
namespace type_utils {
template <typename F, typename... Types>
struct static_visit_impl;
@ -185,26 +270,14 @@ struct visit_impl<F, TypeList, First> {
static void apply(const TypeList& c, F&& f) { f(c.template get_unchecked<First>()); }
};
} // namespace type_utils_details
/**************************
* Invocable utils
*************************/
//! Get index of T in Types...
template <typename T, typename... Types>
constexpr size_t get_type_index()
{
using namespace type_utils_details;
return (type_list_reverse_index<T, Types...>::index == invalid_type_index)
? invalid_type_index
: sizeof...(Types) - type_list_reverse_index<T, Types...>::index - 1;
}
template <typename T, typename... Types>
constexpr bool type_list_contains()
{
return get_type_index<T, Types...>() != invalid_type_index;
}
template <size_t I, typename... Types>
struct get_index_type {
using type = typename type_utils_details::get_type_reverse<sizeof...(Types) - I - 1, Types...>::type;
};
template <class F, class... Args>
using invoke_result_t = typename std::result_of<F && (Args && ...)>::type;
} // namespace type_utils
//! srslte::get<Type> access methods
template <typename T, typename TypeContainer>
@ -259,8 +332,7 @@ const T& get(const TypeContainer<Args...>& c)
template <typename Visitor, typename... Args>
void static_visit(Visitor&& f, size_t current_idx)
{
type_utils_details::static_visit_impl<Visitor, Args...>::apply(sizeof...(Args) - current_idx - 1,
std::forward<Visitor>(f));
type_utils::static_visit_impl<Visitor, Args...>::apply(sizeof...(Args) - current_idx - 1, std::forward<Visitor>(f));
}
template <typename Visitor, template <typename...> class TypeSet, typename... Args>
void static_visit(Visitor&& f, const TypeSet<Args...>& tset)
@ -272,13 +344,13 @@ void static_visit(Visitor&& f, const TypeSet<Args...>& tset)
template <typename Visitor, template <typename...> class TypeSet, typename... Args>
void visit(Visitor&& f, TypeSet<Args...>& tset)
{
using namespace type_utils_details;
using namespace type_utils;
visit_impl<Visitor, TypeSet<Args...>, Args...>::apply(tset, std::forward<Visitor>(f));
}
template <typename Visitor, template <typename...> class TypeSet, typename... Args>
void visit(Visitor&& f, const TypeSet<Args...>& tset)
{
using namespace type_utils_details;
using namespace type_utils;
visit_impl<Visitor, TypeSet<Args...>, Args...>::apply(tset, std::forward<Visitor>(f));
}

@ -61,12 +61,17 @@ public:
void exit(state_inner2& s) { log_h->info("fsm1::%s::exit called\n", srslte::get_type_name(s).c_str()); }
// FSM2 transitions
auto react(state_inner& s, ev1 e) -> to_state<state_inner>;
auto react(state_inner& s, ev2 e) -> to_state<state_inner2>;
auto react(state_inner2& s, ev2 e) -> to_state<state1>;
void inner_action1(state_inner& s, state_inner& d, const ev1& e);
void inner_action2(state_inner& s, state_inner2& d, const ev2& e);
void inner_action3(state_inner2& s, state1& d, const ev2& e);
// list of states
state_list<state_inner, state_inner2> states{this};
using transitions = transition_table<row<state_inner, state_inner, ev1, &fsm2::inner_action1>,
row<state_inner, state_inner2, ev2, &fsm2::inner_action2>,
row<state_inner2, state1, ev2, &fsm2::inner_action3> >;
};
protected:
@ -74,15 +79,20 @@ protected:
void enter(idle_st& s);
void enter(state1& s);
// transitions
auto react(idle_st& s, ev1 e) -> srslte::to_state<state1>;
auto react(state1& s, ev1 e) -> srslte::to_state<fsm2>;
auto react(state1& s, ev2 e) -> srslte::to_states<idle_st, fsm2>;
void action1(idle_st& s, state1& d, const ev1& e);
void action2(state1& s, fsm2& d, const ev1& e);
void action3(state1& s, idle_st& d, const ev2& e);
void foo(ev1 e) { foo_counter++; }
// list of states
// list of states + transitions
state_list<idle_st, state1, fsm2> states = {this, idle_st{}, state1{}, fsm2{this}};
using transitions = transition_table<row<idle_st, state1, ev1, &fsm1::action1>,
row<state1, fsm2, ev1, &fsm1::action2>,
row<state1, idle_st, ev2, &fsm1::action3> >;
};
void fsm1::enter(idle_st& s)
@ -97,39 +107,35 @@ void fsm1::enter(state1& s)
}
// FSM event handlers
auto fsm1::fsm2::react(state_inner& s, ev1) -> to_state<state_inner>
void fsm1::fsm2::inner_action1(state_inner& s, state_inner& d, const ev1& e)
{
log_h->info("fsm2::state_inner::react called\n");
return {};
}
auto fsm1::fsm2::react(state_inner& s, ev2) -> to_state<state_inner2>
void fsm1::fsm2::inner_action2(state_inner& s, state_inner2& d, const ev2& e)
{
log_h->info("fsm2::state_inner::react called\n");
return {};
}
auto fsm1::fsm2::react(state_inner2& s, ev2) -> to_state<state1>
void fsm1::fsm2::inner_action3(state_inner2& s, state1& d, const ev2& e)
{
log_h->info("fsm2::state_inner::react called\n");
return {};
log_h->info("fsm2::state_inner2::react called\n");
}
auto fsm1::react(idle_st& s, ev1 e) -> to_state<state1>
void fsm1::action1(idle_st& s, state1& d, const ev1& e)
{
log_h->info("%s::react called\n", srslte::get_type_name(s).c_str());
foo(e);
return {};
}
auto fsm1::react(state1& s, ev1) -> to_state<fsm2>
void fsm1::action2(state1& s, fsm2& d, const ev1& ev)
{
log_h->info("%s::react called\n", srslte::get_type_name(s).c_str());
return {};
}
auto fsm1::react(state1& s, ev2) -> srslte::to_states<idle_st, fsm2>
void fsm1::action3(state1& s, idle_st& d, const ev2& ev)
{
log_h->info("%s::react called\n", srslte::get_type_name(s).c_str());
return srslte::to_state<idle_st>{};
}
// Static Checks
@ -139,6 +145,9 @@ namespace fsm_details {
static_assert(is_fsm<fsm1>(), "invalid metafunction\n");
static_assert(is_subfsm<fsm1::fsm2>(), "invalid metafunction\n");
static_assert(
type_list_size(typename filter_transition_type<ev1, fsm1::idle_st, fsm_helper::fsm_transitions<fsm1> >::type{}) > 0,
"invalid filter metafunction\n");
static_assert(std::is_same<fsm_helper::fsm_state_list_type<fsm1>,
fsm1::state_list<fsm1::idle_st, fsm1::state1, fsm1::fsm2> >::value,
"get state list failed\n");
@ -218,8 +227,9 @@ int test_hsm()
/////////////////////////////
struct procevent1 {};
struct procevent2 {};
struct procevent1 {
bool is_success;
};
struct proc1 : public srslte::proc_fsm_t<proc1, int> {
public:
@ -229,44 +239,40 @@ public:
protected:
// Transitions
auto react(idle_st& s, srslte::proc_launch_ev<int*> ev) -> to_state<procstate1>;
auto react(procstate1& s, procevent1 ev) -> to_state<complete_st>;
auto react(procstate1& s, procevent2 ev) -> to_state<complete_st>;
auto react(complete_st& s, reset_ev ev) -> to_state<idle_st>;
// example of uncaught event handling
template <typename State>
to_state<State> react(State& s, int e)
{
log_h->info("I dont know how to handle an \"int\" event\n");
return {};
}
void init(idle_st& s, procstate1& d, const srslte::proc_launch_ev<int*>& ev);
void handle_success(procstate1& s, idle_st& d, const procevent1& ev);
void handle_failure(procstate1& s, idle_st& d, const procevent1& ev);
bool is_success(procstate1& s, const procevent1& ev) const { return ev.is_success; }
bool is_failure(procstate1& s, const procevent1& ev) const { return not ev.is_success; }
state_list<idle_st, procstate1, complete_st> states{this, idle_st{}, procstate1{}, complete_st{}};
state_list<idle_st, procstate1> states{this, idle_st{}, procstate1{}};
using transitions =
transition_table<row<idle_st, procstate1, srslte::proc_launch_ev<int*>, &proc1::init>,
row<procstate1, idle_st, procevent1, &proc1::handle_success, &proc1::is_success>,
row<procstate1, idle_st, procevent1, &proc1::handle_failure, &proc1::is_failure> >;
};
auto proc1::react(idle_st& s, srslte::proc_launch_ev<int*> ev) -> to_state<procstate1>
void proc1::init(idle_st& s, procstate1& d, const srslte::proc_launch_ev<int*>& ev)
{
log_h->info("started!\n");
return {};
}
auto proc1::react(procstate1& s, procevent1 ev) -> to_state<complete_st>
void proc1::handle_success(procstate1& s, idle_st& d, const procevent1& ev)
{
log_h->info("success!\n");
return set_success(5);
d.success = true;
d.result = 5;
}
auto proc1::react(procstate1& s, procevent2 ev) -> to_state<complete_st>
void proc1::handle_failure(procstate1& s, idle_st& d, const procevent1& ev)
{
log_h->info("failure!\n");
return set_failure();
}
auto proc1::react(complete_st& s, reset_ev ev) -> to_state<idle_st>
{
log_h->info("propagate results %s\n", is_success() ? "success" : "failure");
if (is_success()) {
log_h->info("result was %d\n", get_result());
}
return {};
d.success = false;
d.result = 3;
}
int test_fsm_proc()
@ -274,13 +280,23 @@ int test_fsm_proc()
proc1 proc{srslte::logmap::get("PROC")};
proc.get_log()->set_level(srslte::LOG_LEVEL_INFO);
proc.set_fsm_event_log_level(srslte::LOG_LEVEL_INFO);
int v = 2;
TESTASSERT(proc.current_state_name() == "idle_st");
proc.launch(&v);
TESTASSERT(proc.current_state_name() == "procstate1");
proc.launch(&v);
TESTASSERT(proc.current_state_name() == "procstate1");
proc.trigger(5);
proc.trigger(procevent1{});
TESTASSERT(proc.current_state_name() == "procstate1");
proc.trigger(procevent1{true});
TESTASSERT(proc.current_state_name() == "idle_st");
TESTASSERT(proc.get_state<proc1::idle_st>()->success);
proc.launch(&v);
proc.trigger(procevent2{});
TESTASSERT(proc.current_state_name() == "procstate1");
proc.trigger(procevent1{false});
TESTASSERT(proc.current_state_name() == "idle_st");
TESTASSERT(not proc.get_state<proc1::idle_st>()->success);
return SRSLTE_SUCCESS;
}
@ -317,22 +333,6 @@ public:
nas_fsm(srslte::log_ref log_) : fsm_t<nas_fsm>(log_) {}
protected:
auto react(emm_null_st& s, const enable_s1_ev& ev) -> to_state<emm_deregistered>;
auto react(emm_deregistered& s, disable_s1_ev ev) -> to_state<emm_null_st>;
auto react(emm_deregistered& s, attach_request_ev ev) -> to_state<emm_registered_initiated>;
auto react(emm_registered_initiated& s, emm_registr_fail_ev ev) -> to_state<emm_deregistered>;
auto react(emm_registered_initiated& s, attach_accept_ev ev) -> to_state<emm_registered>;
auto react(emm_registered& s, sr_initiated_ev ev) -> to_state<emm_service_req_initiated>;
auto react(emm_service_req_initiated& s, sr_outcome_ev) -> to_state<emm_registered>;
auto react(emm_registered& s, tau_request_ev ev) -> to_state<emm_ta_updating_initiated>;
auto react(emm_registered& s, detach_request_ev ev) -> to_state<emm_deregistered_initiated>;
auto react(emm_ta_updating_initiated& s, tau_outcome_ev ev) -> to_state<emm_registered>;
auto react(emm_ta_updating_initiated& s, tau_reject_other_cause_ev ev) -> to_state<emm_deregistered>;
auto react(emm_deregistered_initiated& s, detach_accept_ev ev) -> to_state<emm_deregistered>;
// on power-off go to deregistered state. Disable react if we are already in deregistered
template <typename AnyState>
auto react(AnyState& s, power_off_ev ev) -> to_state<emm_deregistered>;
state_list<emm_null_st,
emm_deregistered,
emm_registered_initiated,
@ -341,82 +341,21 @@ protected:
emm_ta_updating_initiated,
emm_deregistered_initiated>
states{nullptr};
using transitions = transition_table<row<emm_null_st, emm_deregistered, enable_s1_ev>,
row<emm_deregistered, emm_null_st, disable_s1_ev>,
row<emm_deregistered, emm_registered_initiated, attach_request_ev>,
row<emm_registered_initiated, emm_deregistered, emm_registr_fail_ev>,
row<emm_registered_initiated, emm_registered, attach_accept_ev>,
row<emm_registered, emm_service_req_initiated, sr_initiated_ev>,
row<emm_service_req_initiated, emm_registered, sr_outcome_ev>,
row<emm_registered, emm_ta_updating_initiated, tau_request_ev>,
row<emm_registered, emm_deregistered_initiated, detach_request_ev>,
row<emm_ta_updating_initiated, emm_registered, tau_outcome_ev>,
row<emm_ta_updating_initiated, emm_deregistered, tau_reject_other_cause_ev>,
row<emm_deregistered_initiated, emm_deregistered, detach_accept_ev>,
from_any_state<emm_deregistered, power_off_ev> >;
};
#define LOGEVENT() log_h->info("Received an \"%s\" event\n", srslte::get_type_name(ev).c_str())
auto nas_fsm::react(emm_null_st& s, const enable_s1_ev& ev) -> to_state<emm_deregistered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_deregistered& s, disable_s1_ev ev) -> to_state<emm_null_st>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_deregistered& s, attach_request_ev ev) -> to_state<emm_registered_initiated>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_registered_initiated& s, emm_registr_fail_ev ev) -> to_state<emm_deregistered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_registered_initiated& s, attach_accept_ev ev) -> to_state<emm_registered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_registered& s, sr_initiated_ev ev) -> to_state<emm_service_req_initiated>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_service_req_initiated& s, sr_outcome_ev ev) -> to_state<emm_registered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_registered& s, tau_request_ev ev) -> to_state<emm_ta_updating_initiated>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_registered& s, detach_request_ev ev) -> to_state<emm_deregistered_initiated>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_ta_updating_initiated& s, tau_outcome_ev ev) -> to_state<emm_registered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_ta_updating_initiated& s, tau_reject_other_cause_ev ev) -> to_state<emm_deregistered>
{
LOGEVENT();
return {};
}
auto nas_fsm::react(emm_deregistered_initiated& s, detach_accept_ev ev) -> to_state<emm_deregistered>
{
LOGEVENT();
return {};
}
template <typename AnyState>
auto nas_fsm::react(AnyState& s, power_off_ev ev) -> to_state<emm_deregistered>
{
LOGEVENT();
return {};
}
int test_nas_fsm()
{
srslte::log_ref log_h{"NAS"};

Loading…
Cancel
Save