/* * Copyright 2013-2020 Software Radio Systems Limited * * This file is part of srsLTE. * * srsLTE is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * srsLTE is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * A copy of the GNU Affero General Public License can be found in * the LICENSE file in the top-level directory of this distribution * and at http://www.gnu.org/licenses/. * */ #ifndef SRSLTE_FSM_H #define SRSLTE_FSM_H #include "choice_type.h" #include "srslte/common/logmap.h" #include #include #include #include #include #include namespace srslte { //! Helper to print the name of a type for logging #if defined(__GNUC__) && !defined(__clang__) template std::string get_type_name() { static const char* funcname = __PRETTY_FUNCTION__; static const std::string s = []() { static const char* pos1 = strchr(funcname, '=') + 2; static const char* pos2 = strchr(pos1, ';'); std::string s2{pos1, pos2}; size_t colon_pos = s2.rfind(':'); return colon_pos == std::string::npos ? s2 : s2.substr(colon_pos + 1, s2.size()); }(); return s; } #else template std::string get_type_name() { return "anonymous"; } #endif //! This version leverages type deduction. (e.g. get_type_name(var)) template std::string get_type_name(const T& t) { return get_type_name(); } //! When there is no state transition struct same_state { }; namespace fsm_details { //! Visitor to get a state's name string struct state_name_visitor { template void operator()(State&& s) { name = get_type_name(s); } std::string name = "invalid state"; }; struct fsm_helper { //! Metafunction to determine if FSM can hold given State type template using get_fsm_state_list = decltype(std::declval().states); template using enable_if_fsm_state = typename get_fsm_state_list::template enable_if_can_hold; template using disable_if_fsm_state = typename get_fsm_state_list::template disable_if_can_hold; template static auto call_init(FSM* f) -> decltype(f->derived()->do_init()) { f->derived()->do_init(); } static void call_init(...) {} template static auto call_enter(FSM* f, State* s) -> decltype(f->enter(*s)) { f->enter(*s); call_init(s); } static void call_enter(...) {} template static auto call_exit(FSM* f, State* s) -> decltype(f->exit(*s)) { f->exit(*s); } static void call_exit(...) {} template struct enter_visitor { enter_visitor(FSM* f_) : f(f_) {} template void operator()(State&& s) { call_enter(f, &s); } FSM* f; }; //! Stayed in same state template static void handle_state_change(FSM* f, same_state* s, PrevState* p) { // do nothing } //! TargetState is type-erased (a choice). Apply its stored type to the fsm current state template static void handle_state_change(FSM* f, choice_t* s, PrevState* p) { fsm_details::fsm_helper::variant_convert visitor{f, p}; s->visit(visitor); } //! Simple state transition in FSM template static enable_if_fsm_state handle_state_change(FSM* f, State* s, PrevState* p) { static_assert(not std::is_same::value, "State cannot transition to itself.\n"); call_exit(f, &srslte::get(f->states)); f->states.transit(std::move(*s)); call_enter(f, &srslte::get(f->states)); } //! State not present in current FSM. Attempt state transition in parent FSM in the case of NestedFSM template static disable_if_fsm_state handle_state_change(FSM* f, State* s, PrevState* p) { static_assert(FSM::is_nested, "State is not present in the FSM list of valid states"); if (p != nullptr) { // srslte::get(f->states).do_exit(); call_exit(f, &srslte::get(f->states)); } handle_state_change(f->parent_fsm()->derived(), s, static_cast(f)); } //! Trigger Event, that will result in a state transition template struct trigger_visitor { trigger_visitor(FSM* f_, Event&& ev_) : f(f_), ev(std::forward(ev_)) {} //! Trigger visitor callback for the current state template void operator()(CurrentState& s) { call_trigger(&s); } //! Compute next state type template using NextState = decltype(std::declval().react(std::declval(), std::declval())); //! In case a "react(State&, Event) -> NextState" method is found template auto call_trigger(State* current_state) -> NextState { static_assert(not std::is_same, State>::value, "State cannot transition to itself.\n"); auto target_state = f->react(*current_state, std::move(ev)); f->log_fsm_activity("Detected fsm transition \"%s\" -> \"%s\"", get_type_name(*current_state).c_str(), get_type_name(target_state).c_str()); fsm_helper::handle_state_change(f, &target_state, current_state); return target_state; } //! No react method found. Try forward trigger to HSM template void call_trigger(State* current_state, Args&&... args) { call_trigger_stage2(current_state); } //! In case a react(CurrentState&, Event) method is not found, but we are in a NestedFSM with a trigger method template auto call_trigger_stage2(State* s) -> decltype(std::declval().trigger(std::declval())) { s->trigger(std::move(ev)); } //! No trigger or react method found. Do nothing void call_trigger_stage2(...) { f->unhandled_event(std::move(ev)); } FSM* f; Event ev; }; template struct variant_convert { variant_convert(FSM* f_, PrevState* p_) : f(f_), p(p_) {} template void operator()(State& s) { handle_state_change(f, &s, p); } FSM* f; PrevState* p; }; }; } // namespace fsm_details //! CRTP Class for all non-nested FSMs template class fsm_t { protected: using base_t = fsm_t; // get access to derived protected members from the base class derived_view : public Derived { public: using derived_t = Derived; using Derived::do_init; using Derived::enter; using Derived::exit; using Derived::react; using Derived::states; using Derived::unhandled_event; using Derived::base_t::unhandled_event; }; public: static const bool is_nested = false; template struct state_list : public choice_t { using base_t = choice_t; template state_list(fsm_t* f, Args&&... args) : base_t(std::forward(args)...) { if (not Derived::is_nested) { fsm_details::fsm_helper::enter_visitor visitor{f->derived()}; this->visit(visitor); } } template void transit(State&& s) { this->template emplace(std::forward(s)); } }; explicit fsm_t(srslte::log_ref log_) : log_h(log_) {} // Push Events to FSM template void trigger(Ev&& e) { fsm_details::fsm_helper::trigger_visitor visitor{derived(), std::forward(e)}; derived()->states.visit(visitor); } template bool is_in_state() const { return derived()->states.template is(); } template const State* get_state() const { return srslte::get_if(derived()->states); } std::string get_state_name() const { fsm_details::state_name_visitor visitor{}; derived()->states.visit(visitor); return visitor.name; } //! Static method to check if State belongs to the list of possible states template constexpr static bool can_hold_state() { return fsm_details::fsm_helper::get_fsm_state_list >::template can_hold_type(); } 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(this); } const derived_view* derived() const { return static_cast(this); } void do_init() { fsm_details::fsm_helper::enter_visitor visitor{derived()}; derived()->states.visit(visitor); } void enter() {} void exit() {} template void unhandled_event(Event&& e) { switch (fsm_event_log_level) { case LOG_LEVEL_DEBUG: log_h->debug("Unhandled event caught: \"%s\"\n", get_type_name().c_str()); break; case LOG_LEVEL_INFO: log_h->info("Unhandled event caught: \"%s\"\n", get_type_name().c_str()); break; case LOG_LEVEL_WARNING: log_h->warning("Unhandled event caught: \"%s\"\n", get_type_name().c_str()); break; case LOG_LEVEL_ERROR: log_h->error("Unhandled event caught: \"%s\"\n", get_type_name().c_str()); break; default: break; } } template void log_fsm_activity(const char* format, Args&&... args) { switch (fsm_event_log_level) { case LOG_LEVEL_DEBUG: log_h->debug(format, std::forward(args)...); break; case LOG_LEVEL_INFO: log_h->info(format, std::forward(args)...); break; case LOG_LEVEL_WARNING: log_h->warning(format, std::forward(args)...); break; case LOG_LEVEL_ERROR: log_h->error(format, std::forward(args)...); break; default: break; } } srslte::log_ref log_h; srslte::LOG_LEVEL_ENUM fsm_event_log_level = LOG_LEVEL_DEBUG; }; template class nested_fsm_t : public fsm_t { public: using base_t = nested_fsm_t; using parent_t = ParentFSM; static const bool is_nested = true; explicit nested_fsm_t(ParentFSM* parent_fsm_) : fsm_t(parent_fsm_->get_log()), fsm_ptr(parent_fsm_) {} // 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; } protected: using parent_fsm_t = ParentFSM; using fsm_t::enter; using fsm_t::do_init; ParentFSM* fsm_ptr = nullptr; }; template struct proc_complete_ev { proc_complete_ev(bool success_) : success(success_) {} bool success; }; // event template struct proc_launch_ev { std::tuple args; explicit proc_launch_ev(Args&&... args_) : args(std::forward(args_)...) {} }; template class proc_fsm_t : public fsm_t { using fsm_type = Derived; using fsm_t::derived; friend struct fsm_details::fsm_helper; protected: using fsm_t::log_h; using fsm_t::unhandled_event; void unhandled_event(srslte::proc_launch_ev e) { log_h->warning("Unhandled event \"launch\" caught when procedure is already running\n"); } public: using base_t = proc_fsm_t; using fsm_t::trigger; // states struct idle_st { }; struct complete_st { complete_st(bool success_) : success(success_) {} bool success; Result result; }; explicit proc_fsm_t(srslte::log_ref log_) : fsm_t(log_) {} bool is_running() const { return base_t::template is_in_state(); } template void launch(Args&&... args) { trigger(proc_launch_ev(std::forward(args)...)); } protected: void exit(idle_st& s) { launch_counter++; log_h->info("Starting run no. %d\n", launch_counter); } void enter(complete_st& s) { trigger(srslte::proc_complete_ev{s.success}); } private: int launch_counter = 0; }; } // namespace srslte #endif // SRSLTE_FSM_H