fixed issue with subfsm revisits not starting in initial substate

master
Francisco Paisana 5 years ago committed by Francisco Paisana
parent e1dc9cae15
commit 6322e447e9

@ -41,8 +41,7 @@ template <typename... NextStates>
struct to_states { struct to_states {
template <typename ChosenState> template <typename ChosenState>
to_states(to_state<ChosenState>) : state_idx(get_type_index<ChosenState, NextStates...>()) to_states(to_state<ChosenState>) : state_idx(get_type_index<ChosenState, NextStates...>())
{ {}
}
template <typename State> template <typename State>
bool is() const bool is() const
@ -56,8 +55,7 @@ struct to_states {
}; };
//! Return for when there is no state transition //! Return for when there is no state transition
struct same_state { struct same_state {};
};
//! Forward declaration //! Forward declaration
template <typename Derived> template <typename Derived>
@ -124,7 +122,12 @@ struct fsm_helper {
template <typename FSM, typename State> template <typename FSM, typename State>
static typename std::enable_if<is_fsm<State>()>::type call_enter(FSM* f, State* s) static typename std::enable_if<is_fsm<State>()>::type call_enter(FSM* f, State* s)
{ {
using init_type = typename fsm_state_list_type<State>::init_state_t;
// set default FSM type
s->derived()->states.template transit<init_type>();
// call FSM enter function
f->enter(*s); f->enter(*s);
// call initial substate enter
fsm_details::enter_visitor<typename State::derived_view> visitor{s->derived()}; fsm_details::enter_visitor<typename State::derived_view> visitor{s->derived()};
srslte::visit(visitor, s->derived()->states); srslte::visit(visitor, s->derived()->states);
} }
@ -140,6 +143,11 @@ struct fsm_helper {
{ {
// do nothing // do nothing
} }
template <typename FSM, typename PrevState>
static void handle_state_change(FSM* f, to_state<same_state>* s, PrevState* p)
{
// do nothing
}
//! TargetState is type-erased (a choice). Apply its stored type to the fsm current state //! TargetState is type-erased (a choice). Apply its stored type to the fsm current state
template <typename FSM, typename... Args, typename PrevState> template <typename FSM, typename... Args, typename PrevState>
static void handle_state_change(FSM* f, to_states<Args...>* s, PrevState* p) static void handle_state_change(FSM* f, to_states<Args...>* s, PrevState* p)
@ -185,9 +193,7 @@ struct fsm_helper {
{ {
result = call_trigger(&s); result = call_trigger(&s);
if (not result) { if (not result) {
auto target_state = f->react(s, std::move(ev)); result = call_react(s);
fsm_helper::handle_state_change(f, &target_state, &s);
result = not std::is_same<decltype(target_state), srslte::same_state>::value;
} }
} }
@ -195,11 +201,29 @@ struct fsm_helper {
template <typename State> template <typename State>
typename std::enable_if<is_nested_fsm<State>(), bool>::type call_trigger(State* s) typename std::enable_if<is_nested_fsm<State>(), bool>::type call_trigger(State* s)
{ {
return s->trigger(std::move(ev)); return s->trigger(std::forward<Event>(ev));
} }
//! In case a "trigger(Event)" method is not found //! In case a "trigger(Event)" method is not found
bool call_trigger(...) { return false; } bool call_trigger(...) { return false; }
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;
}
bool call_react(...)
{
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;
}
FSM* f; FSM* f;
Event ev; Event ev;
bool result = false; bool result = false;
@ -232,22 +256,25 @@ std::string get_type_name(const srslte::to_states<Args...>& t)
return v.name; return v.name;
} }
template <typename Derived, typename ParentFSM>
class nested_fsm_t;
//! CRTP Class for all non-nested FSMs //! CRTP Class for all non-nested FSMs
template <typename Derived> template <typename Derived>
class fsm_t class fsm_t
{ {
protected: protected:
using base_t = fsm_t<Derived>; using base_t = fsm_t<Derived>;
template <typename SubFSM>
using subfsm_t = nested_fsm_t<SubFSM, Derived>;
//! get access to derived protected members from the base //! get access to derived protected members from the base
class derived_view : public Derived class derived_view : public Derived
{ {
public: public:
using derived_t = Derived; using derived_t = Derived;
// propagate fsm_t methods using derived_t::base_t::enter;
using Derived::base_t::enter; using derived_t::base_t::exit;
using Derived::base_t::exit;
using Derived::base_t::react;
// propagate user fsm methods // propagate user fsm methods
using Derived::enter; using Derived::enter;
using Derived::exit; using Derived::exit;
@ -266,13 +293,14 @@ public:
template <typename... States> template <typename... States>
struct state_list : public std::tuple<States...> { struct state_list : public std::tuple<States...> {
using tuple_base_t = std::tuple<States...>; using tuple_base_t = std::tuple<States...>;
using init_state_t = typename std::decay<decltype(std::get<0>(std::declval<tuple_base_t>()))>::type;
template <typename... Args> template <typename... Args>
state_list(fsm_t<Derived>* f, Args&&... args) : tuple_base_t(std::forward<Args>(args)...) state_list(fsm_t<Derived>* f, Args&&... args) : tuple_base_t(std::forward<Args>(args)...)
{ {
if (not Derived::is_nested) { if (not Derived::is_nested) {
// If Root FSM, call initial state enter method // If Root FSM, call initial state enter method
fsm_details::enter_visitor<derived_view> visitor{f->derived()}; fsm_details::fsm_helper::call_enter(f->derived(), &get_unchecked<init_state_t>());
srslte::visit(visitor, *this);
} }
} }
@ -335,13 +363,25 @@ public:
return derived()->states.template is<State>(); return derived()->states.template is<State>();
} }
template <typename State>
const State* get_if_current_state() const
{
return is_in_state<State>() ? get_state<State>() : nullptr;
}
template <typename State>
State* get_state()
{
return &derived()->states.template get_unchecked<State>();
}
template <typename State> template <typename State>
const State* get_state() const const State* get_state() const
{ {
return is_in_state<State>() ? &derived()->states.template get_unchecked<State>() : nullptr; return &derived()->states.template get_unchecked<State>();
} }
std::string get_state_name() const std::string current_state_name() const
{ {
fsm_details::state_name_visitor visitor{}; fsm_details::state_name_visitor visitor{};
srslte::visit(visitor, derived()->states); srslte::visit(visitor, derived()->states);
@ -376,14 +416,6 @@ protected:
// do nothing by default // do nothing by default
} }
template <typename State, typename Event>
srslte::same_state react(State& s, Event&& e)
{
log_fsm_activity(
"FSM \"%s\": Unhandled event caught: \"%s\"\n", get_type_name(*this).c_str(), get_type_name<Event>().c_str());
return {};
}
template <typename... Args> template <typename... Args>
void log_fsm_activity(const char* format, Args&&... args) void log_fsm_activity(const char* format, Args&&... args)
{ {
@ -455,7 +487,6 @@ protected:
using fsm_t<Derived>::log_h; using fsm_t<Derived>::log_h;
using fsm_t<Derived>::enter; using fsm_t<Derived>::enter;
using fsm_t<Derived>::exit; using fsm_t<Derived>::exit;
using fsm_t<Derived>::react;
template <typename State> template <typename State>
auto react(State&, srslte::proc_launch_ev<int*> e) -> srslte::same_state auto react(State&, srslte::proc_launch_ev<int*> e) -> srslte::same_state
@ -469,14 +500,11 @@ public:
using fsm_t<Derived>::trigger; using fsm_t<Derived>::trigger;
// events // events
struct reset_ev { struct reset_ev {};
};
// states // states
struct idle_st { struct idle_st {};
}; struct complete_st {};
struct complete_st {
};
explicit proc_fsm_t(srslte::log_ref log_) : fsm_t<Derived>(log_) {} explicit proc_fsm_t(srslte::log_ref log_) : fsm_t<Derived>(log_) {}

@ -41,11 +41,12 @@ public:
explicit fsm1(srslte::log_ref log_) : srslte::fsm_t<fsm1>(log_) {} explicit fsm1(srslte::log_ref log_) : srslte::fsm_t<fsm1>(log_) {}
// this state is another FSM // this state is another FSM
class fsm2 : public srslte::nested_fsm_t<fsm2, fsm1> class fsm2 : public subfsm_t<fsm2>
{ {
public: public:
// states // states
struct state_inner {}; struct state_inner {};
struct state_inner2 {};
explicit fsm2(fsm1* f_) : nested_fsm_t(f_) {} explicit fsm2(fsm1* f_) : nested_fsm_t(f_) {}
~fsm2() { log_h->info("%s being destroyed!", get_type_name(*this).c_str()); } ~fsm2() { log_h->info("%s being destroyed!", get_type_name(*this).c_str()); }
@ -56,14 +57,16 @@ public:
log_h->info("fsm1::%s::enter called\n", srslte::get_type_name(s).c_str()); log_h->info("fsm1::%s::enter called\n", srslte::get_type_name(s).c_str());
parent_fsm()->inner_enter_counter++; parent_fsm()->inner_enter_counter++;
} }
void exit(state_inner& s) { log_h->info("fsm1::%s::exit called\n", srslte::get_type_name(s).c_str()); } void enter(state_inner2& s) { log_h->info("fsm1::%s::enter called\n", srslte::get_type_name(s).c_str()); }
void exit(state_inner2& s) { log_h->info("fsm1::%s::exit called\n", srslte::get_type_name(s).c_str()); }
// FSM2 transitions // FSM2 transitions
auto react(state_inner& s, ev1 e) -> srslte::same_state; auto react(state_inner& s, ev1 e) -> srslte::same_state;
auto react(state_inner& s, ev2 e) -> srslte::to_state<state1>; auto react(state_inner& s, ev2 e) -> to_state<state_inner2>;
auto react(state_inner2& s, ev2 e) -> to_state<state1>;
// list of states // list of states
state_list<state_inner> states{this}; state_list<state_inner, state_inner2> states{this};
}; };
protected: protected:
@ -110,7 +113,13 @@ auto fsm1::fsm2::react(state_inner& s, ev1) -> srslte::same_state
return {}; return {};
} }
auto fsm1::fsm2::react(state_inner& s, ev2) -> srslte::to_state<state1> auto fsm1::fsm2::react(state_inner& s, ev2) -> to_state<state_inner2>
{
log_h->info("fsm2::state_inner::react called\n");
return {};
}
auto fsm1::fsm2::react(state_inner2& s, ev2) -> to_state<state1>
{ {
log_h->info("fsm2::state_inner::react called\n"); log_h->info("fsm2::state_inner::react called\n");
return {}; return {};
@ -161,45 +170,58 @@ int test_hsm()
fsm1 f{log_h}; fsm1 f{log_h};
TESTASSERT(f.idle_enter_counter == 1); TESTASSERT(f.idle_enter_counter == 1);
TESTASSERT(get_type_name(f) == "fsm1"); TESTASSERT(get_type_name(f) == "fsm1");
TESTASSERT(f.get_state_name() == "idle_st"); TESTASSERT(f.current_state_name() == "idle_st");
TESTASSERT(f.is_in_state<fsm1::idle_st>()); TESTASSERT(f.is_in_state<fsm1::idle_st>());
TESTASSERT(f.foo_counter == 0); TESTASSERT(f.foo_counter == 0);
// Moving Idle -> State1 // Moving Idle -> State1
ev1 e; ev1 e;
f.trigger(e); f.trigger(e);
TESTASSERT(f.get_state_name() == "state1"); TESTASSERT(f.current_state_name() == "state1");
TESTASSERT(f.is_in_state<fsm1::state1>()); TESTASSERT(f.is_in_state<fsm1::state1>());
// Moving State1 -> fsm2 // Moving State1 -> fsm2
f.trigger(e); f.trigger(e);
TESTASSERT(f.get_state_name() == "fsm2"); TESTASSERT(f.current_state_name() == "fsm2");
TESTASSERT(f.is_in_state<fsm1::fsm2>()); TESTASSERT(f.is_in_state<fsm1::fsm2>());
TESTASSERT(f.get_state<fsm1::fsm2>()->get_state_name() == "state_inner"); TESTASSERT(f.get_if_current_state<fsm1::fsm2>()->current_state_name() == "state_inner");
TESTASSERT(f.inner_enter_counter == 1); TESTASSERT(f.inner_enter_counter == 1);
// Fsm2 does not listen to ev1 // Fsm2 does not listen to ev1
f.trigger(e); f.trigger(e);
TESTASSERT(std::string{f.get_state_name()} == "fsm2"); TESTASSERT(f.current_state_name() == "fsm2");
TESTASSERT(f.is_in_state<fsm1::fsm2>()); TESTASSERT(f.is_in_state<fsm1::fsm2>());
TESTASSERT(f.get_state<fsm1::fsm2>()->get_state_name() == "state_inner"); TESTASSERT(f.get_if_current_state<fsm1::fsm2>()->current_state_name() == "state_inner");
// Fsm2 state_inner -> state_inner2
f.trigger(ev2{});
TESTASSERT(f.current_state_name() == "fsm2");
TESTASSERT(f.is_in_state<fsm1::fsm2>());
TESTASSERT(f.get_if_current_state<fsm1::fsm2>()->current_state_name() == "state_inner2");
// Moving fsm2 -> state1 // Moving fsm2 -> state1
f.trigger(ev2{}); f.trigger(ev2{});
TESTASSERT(f.get_state_name() == "state1"); TESTASSERT(f.current_state_name() == "state1");
TESTASSERT(f.is_in_state<fsm1::state1>()); TESTASSERT(f.is_in_state<fsm1::state1>());
TESTASSERT(f.state1_enter_counter == 2); TESTASSERT(f.state1_enter_counter == 2);
// Moving state1 -> idle // Moving state1 -> idle
f.trigger(ev2{}); f.trigger(ev2{});
TESTASSERT(std::string{f.get_state_name()} == "idle_st"); TESTASSERT(std::string{f.current_state_name()} == "idle_st");
TESTASSERT(f.is_in_state<fsm1::idle_st>()); TESTASSERT(f.is_in_state<fsm1::idle_st>());
TESTASSERT(f.foo_counter == 1); TESTASSERT(f.foo_counter == 1);
TESTASSERT(f.idle_enter_counter == 2); TESTASSERT(f.idle_enter_counter == 2);
// Call unhandled event // Call unhandled event
f.trigger(ev2{}); f.trigger(ev2{});
TESTASSERT(f.get_state_name() == "idle_st"); TESTASSERT(f.current_state_name() == "idle_st");
// Enter fsm2 again
f.trigger(ev1{});
f.trigger(ev1{});
TESTASSERT(f.current_state_name() == "fsm2");
TESTASSERT(f.is_in_state<fsm1::fsm2>());
TESTASSERT(f.get_if_current_state<fsm1::fsm2>()->current_state_name() == "state_inner");
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }
@ -305,7 +327,7 @@ public:
nas_fsm(srslte::log_ref log_) : fsm_t<nas_fsm>(log_) {} nas_fsm(srslte::log_ref log_) : fsm_t<nas_fsm>(log_) {}
protected: protected:
auto react(emm_null_st& s, enable_s1_ev ev) -> to_state<emm_deregistered>; 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, 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_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, emm_registr_fail_ev ev) -> to_state<emm_deregistered>;
@ -334,7 +356,7 @@ protected:
#define LOGEVENT() log_h->info("Received an \"%s\" event\n", srslte::get_type_name(ev).c_str()) #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, enable_s1_ev ev) -> to_state<emm_deregistered> auto nas_fsm::react(emm_null_st& s, const enable_s1_ev& ev) -> to_state<emm_deregistered>
{ {
LOGEVENT(); LOGEVENT();
return {}; return {};

Loading…
Cancel
Save