mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
769 lines
23 KiB
C
769 lines
23 KiB
C
4 years ago
|
/**
|
||
5 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
5 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
5 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
5 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_FSM_H
|
||
|
#define SRSRAN_FSM_H
|
||
5 years ago
|
|
||
4 years ago
|
#include "srsran/adt/detail/type_utils.h"
|
||
|
#include "srsran/adt/move_callback.h"
|
||
|
#include "srsran/srslog/srslog.h"
|
||
5 years ago
|
#include <cstdio>
|
||
4 years ago
|
#include <deque>
|
||
5 years ago
|
#include <memory>
|
||
|
#include <tuple>
|
||
|
|
||
4 years ago
|
#define otherfsmDebug(f, fmt, ...) f->get_logger().debug("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__)
|
||
|
#define otherfsmInfo(f, fmt, ...) f->get_logger().info("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__)
|
||
5 years ago
|
#define otherfsmWarning(f, fmt, ...) \
|
||
4 years ago
|
f->get_logger().warning("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__)
|
||
|
#define otherfsmError(f, fmt, ...) f->get_logger().error("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__)
|
||
5 years ago
|
|
||
|
#define fsmDebug(fmt, ...) otherfsmDebug(this, fmt, ##__VA_ARGS__)
|
||
|
#define fsmInfo(fmt, ...) otherfsmInfo(this, fmt, ##__VA_ARGS__)
|
||
|
#define fsmWarning(fmt, ...) otherfsmWarning(this, fmt, ##__VA_ARGS__)
|
||
|
#define fsmError(fmt, ...) otherfsmError(this, fmt, ##__VA_ARGS__)
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
4 years ago
|
/// Forward declarations
|
||
5 years ago
|
template <typename Derived>
|
||
4 years ago
|
class base_fsm_t;
|
||
|
template <typename Derived, typename ParentFSM>
|
||
|
class composite_fsm_t;
|
||
|
|
||
4 years ago
|
/// Check if type T is an FSM
|
||
4 years ago
|
template <typename T>
|
||
|
using is_fsm = std::is_base_of<base_fsm_t<T>, T>;
|
||
|
|
||
4 years ago
|
/// Check if type T is a composite FSM
|
||
4 years ago
|
template <typename T, typename TCheck = void>
|
||
|
struct is_composite_fsm : public std::false_type {};
|
||
|
template <typename T>
|
||
|
struct is_composite_fsm<T, typename std::enable_if<is_fsm<T>::value>::type> {
|
||
|
const static bool value = T::is_nested;
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
namespace fsm_details {
|
||
|
|
||
4 years ago
|
/// check whether to log unhandled event
|
||
|
template <typename Event>
|
||
|
auto should_log_unhandled_event(const Event* ev) -> decltype(Event::log_verbose)
|
||
|
{
|
||
|
return Event::log_verbose;
|
||
|
}
|
||
|
inline bool should_log_unhandled_event(...)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
4 years ago
|
//! Meta-function to filter transition_list<Rows...> by <Event, SrcState> types
|
||
5 years ago
|
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<>;
|
||
|
};
|
||
|
|
||
5 years ago
|
//! Visitor to get current state's name string
|
||
5 years ago
|
struct state_name_visitor {
|
||
|
template <typename State>
|
||
|
void operator()(State&& s)
|
||
|
{
|
||
5 years ago
|
name = get_type_name(s);
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
std::string name = "invalid";
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
//! Enable/Disable SFINAE meta-function to check if <State> is part of <FSM> state list
|
||
5 years ago
|
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;
|
||
5 years ago
|
|
||
5 years ago
|
template <typename FSM>
|
||
4 years ago
|
using enable_if_subfsm = typename std::enable_if<is_composite_fsm<FSM>::value>::type;
|
||
5 years ago
|
template <typename FSM>
|
||
4 years ago
|
using disable_if_subfsm = typename std::enable_if<not is_composite_fsm<FSM>::value>::type;
|
||
5 years ago
|
|
||
5 years ago
|
//! 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;
|
||
5 years ago
|
|
||
5 years ago
|
//! Detection of enter/exit methods of a state.
|
||
|
template <typename FSM, typename State>
|
||
5 years ago
|
auto call_enter2(FSM* f, State* s) -> decltype(s->enter(f))
|
||
5 years ago
|
{
|
||
|
s->enter(f);
|
||
|
}
|
||
5 years ago
|
inline void call_enter2(...)
|
||
|
{
|
||
|
// do nothing
|
||
|
}
|
||
|
template <typename FSM, typename State, typename Event>
|
||
|
auto call_enter(FSM* f, State* s, const Event& ev) -> decltype(s->enter(f, ev))
|
||
|
{
|
||
|
// pass event to enter method
|
||
|
s->enter(f, ev);
|
||
|
}
|
||
|
template <typename FSM, typename State, typename... Args>
|
||
|
inline void call_enter(FSM* f, State* s, Args&&...)
|
||
|
{
|
||
|
call_enter2(f, s);
|
||
|
}
|
||
5 years ago
|
template <typename FSM, typename State>
|
||
5 years ago
|
auto call_exit(FSM* f, State* s) -> decltype(s->exit(f))
|
||
5 years ago
|
{
|
||
|
s->exit(f);
|
||
|
}
|
||
5 years ago
|
inline void call_exit(...) {}
|
||
5 years ago
|
|
||
|
//! Find State in FSM recursively (e.g. find State in FSM,FSM::parentFSM,FSM::parentFSM::parentFSM,...)
|
||
|
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>();
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
template <typename FSM>
|
||
|
typename FSM::derived_view* get_derived(FSM* f)
|
||
|
{
|
||
|
return static_cast<typename FSM::derived_view*>(f);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
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");
|
||
5 years ago
|
return get_state_recursive<State>(get_derived(f->parent_fsm()));
|
||
5 years ago
|
}
|
||
|
|
||
|
//! Helper type for FSM state-related operations
|
||
|
template <typename FSM, typename State>
|
||
|
struct state_traits {
|
||
4 years ago
|
static_assert(FSM::template can_hold_state<State>(), "FSM type does not hold provided State");
|
||
5 years ago
|
using state_t = State;
|
||
4 years ago
|
using is_subfsm = std::integral_constant<bool, ::srsran::is_composite_fsm<State>::value>;
|
||
5 years ago
|
|
||
|
//! enter new state. enter is called recursively for subFSMs
|
||
5 years ago
|
template <typename Event>
|
||
|
static void enter_state(FSM* f, State* s, const Event& ev)
|
||
|
{
|
||
|
enter_(f, s, ev, is_subfsm{});
|
||
|
}
|
||
5 years ago
|
//! Change state. If DestState is not a state of FSM, call same function for parentFSM recursively
|
||
5 years ago
|
template <typename DestState, typename Event>
|
||
|
static enable_if_fsm_state<FSM, DestState> transit_state(FSM* f, const Event& ev)
|
||
5 years ago
|
{
|
||
5 years ago
|
call_exit(f, &f->states.template get_unchecked<State>());
|
||
5 years ago
|
f->states.template transit<DestState>();
|
||
5 years ago
|
state_traits<FSM, DestState>::enter_state(f, &f->states.template get_unchecked<DestState>(), ev);
|
||
5 years ago
|
}
|
||
5 years ago
|
template <typename DestState, typename Event>
|
||
|
static disable_if_fsm_state<FSM, DestState> transit_state(FSM* f, const Event& ev)
|
||
5 years ago
|
{
|
||
5 years ago
|
using parent_state_traits = state_traits<typename FSM::parent_t::derived_view, typename FSM::derived_t>;
|
||
5 years ago
|
call_exit(f, &f->states.template get_unchecked<State>());
|
||
5 years ago
|
parent_state_traits::template transit_state<DestState>(get_derived(f->parent_fsm()), ev);
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
private:
|
||
|
//! In case of State is a subFSM
|
||
5 years ago
|
template <typename Event>
|
||
|
static void enter_(FSM* f, State* s, const Event& ev, std::true_type)
|
||
5 years ago
|
{
|
||
5 years ago
|
using init_type = typename fsm_state_list_type<State>::init_state_t;
|
||
|
// set default FSM type
|
||
5 years ago
|
get_derived(s)->states.template transit<init_type>();
|
||
5 years ago
|
// call FSM enter function
|
||
5 years ago
|
call_enter(f, s, ev);
|
||
5 years ago
|
// call initial substate enter
|
||
|
state_traits<typename State::derived_view, init_type>::enter_state(
|
||
5 years ago
|
get_derived(s), &get_derived(s)->states.template get_unchecked<init_type>(), ev);
|
||
5 years ago
|
}
|
||
5 years ago
|
//! In case of State is basic state
|
||
5 years ago
|
template <typename Event>
|
||
|
static void enter_(FSM* f, State* s, const Event& ev, std::false_type)
|
||
|
{
|
||
|
call_enter(f, s, ev);
|
||
|
}
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
//! Trigger Event reaction for the first Row for which the Guard passes
|
||
|
template <typename FSM, typename... Types>
|
||
|
struct apply_first_guard_pass;
|
||
5 years ago
|
|
||
5 years ago
|
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) {
|
||
5 years ago
|
otherfsmInfo(static_cast<typename FSM::derived_t*>(f),
|
||
4 years ago
|
"Event \"%s\" triggered state \"%s\" update",
|
||
5 years ago
|
get_type_name<event_type>().c_str(),
|
||
|
get_type_name<src_state>().c_str());
|
||
5 years ago
|
} else {
|
||
5 years ago
|
otherfsmInfo(static_cast<typename FSM::derived_t*>(f),
|
||
|
"transition detected - %s -> %s (cause: %s)",
|
||
|
get_type_name<src_state>().c_str(),
|
||
|
get_type_name<dest_state>().c_str(),
|
||
|
get_type_name<event_type>().c_str());
|
||
5 years ago
|
// Apply state change operations
|
||
5 years ago
|
state_traits<FSM, src_state>::template transit_state<dest_state>(f, ev);
|
||
5 years ago
|
}
|
||
5 years ago
|
return true;
|
||
5 years ago
|
}
|
||
5 years ago
|
return apply_first_guard_pass<FSM, type_list<Rows...> >::trigger(f, s, ev);
|
||
|
}
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
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)
|
||
|
{
|
||
4 years ago
|
if (should_log_unhandled_event(&ev)) {
|
||
|
otherfsmDebug(static_cast<typename FSM::derived_t*>(f),
|
||
4 years ago
|
"unhandled event caught in state \"%s\": \"%s\"",
|
||
4 years ago
|
get_type_name<SrcState>().c_str(),
|
||
|
get_type_name<Event>().c_str());
|
||
|
}
|
||
5 years ago
|
return false;
|
||
|
}
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
/// Trigger Event that may result in a state transition
|
||
5 years ago
|
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 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>
|
||
|
disable_if_subfsm<CurrentState> operator()(CurrentState& s)
|
||
|
{
|
||
|
result = call_react(s);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
template <typename CurrentState>
|
||
|
enable_if_subfsm<CurrentState> operator()(CurrentState& s)
|
||
|
{
|
||
|
// Enter here for SubFSMs
|
||
4 years ago
|
result = s.process_event(std::forward<Event>(ev));
|
||
5 years ago
|
if (not result) {
|
||
5 years ago
|
result = call_react(s);
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
template <typename SrcState>
|
||
|
bool call_react(SrcState& s)
|
||
|
{
|
||
|
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);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
FSM* f;
|
||
|
Event ev;
|
||
|
bool result = false;
|
||
5 years ago
|
};
|
||
|
|
||
|
} // namespace fsm_details
|
||
|
|
||
|
template <typename Derived>
|
||
4 years ago
|
class base_fsm_t
|
||
5 years ago
|
{
|
||
5 years ago
|
public:
|
||
4 years ago
|
using derived_t = Derived;
|
||
|
|
||
5 years ago
|
//! get access to derived protected members from the base
|
||
5 years ago
|
class derived_view : public Derived
|
||
|
{
|
||
|
public:
|
||
5 years ago
|
// propagate user fsm methods
|
||
5 years ago
|
using Derived::states;
|
||
5 years ago
|
using typename Derived::transitions;
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
template <typename... Rows>
|
||
|
using transition_table = type_list<Rows...>;
|
||
|
|
||
5 years ago
|
//! Params of a state transition
|
||
|
template <typename SrcState,
|
||
|
typename DestState,
|
||
|
typename Event,
|
||
5 years ago
|
void (Derived::*ReactFn)(SrcState&, const Event&) = nullptr,
|
||
|
bool (Derived::*GuardFn)(SrcState&, const Event&) = nullptr>
|
||
5 years ago
|
struct row {
|
||
5 years ago
|
using src_state_t = SrcState;
|
||
|
using dest_state_t = DestState;
|
||
|
using event_t = Event;
|
||
|
constexpr static void (Derived::*react_fn)(SrcState&, const Event&) = ReactFn;
|
||
|
constexpr static bool (Derived::*guard_fn)(SrcState&, const Event&) = GuardFn;
|
||
5 years ago
|
|
||
|
static bool react(derived_view* f, src_state_t& s, const event_t& ev)
|
||
|
{
|
||
|
if (guard_fn == nullptr or (f->*guard_fn)(s, ev)) {
|
||
|
if (react_fn != nullptr) {
|
||
5 years ago
|
(f->*react_fn)(s, ev);
|
||
5 years ago
|
}
|
||
|
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> >;
|
||
|
};
|
||
|
|
||
5 years ago
|
template <typename SrcState,
|
||
5 years ago
|
typename Event,
|
||
5 years ago
|
void (Derived::*ReactFn)(SrcState&, const Event&) = nullptr,
|
||
|
bool (Derived::*GuardFn)(SrcState&, const Event&) = nullptr>
|
||
|
using upd = row<SrcState, SrcState, Event, ReactFn, GuardFn>;
|
||
5 years ago
|
|
||
4 years ago
|
template <typename DestState,
|
||
|
typename Event,
|
||
|
void (Derived::*ReactFn)(const Event&) = nullptr,
|
||
|
bool (Derived::*GuardFn)(const Event&) = nullptr>
|
||
4 years ago
|
struct to_state {
|
||
5 years ago
|
using dest_state_t = DestState;
|
||
|
using event_t = Event;
|
||
4 years ago
|
constexpr static void (Derived::*react_fn)(const Event&) = ReactFn;
|
||
5 years ago
|
constexpr static bool (Derived::*guard_fn)(const Event&) = GuardFn;
|
||
5 years ago
|
|
||
|
template <typename SrcState>
|
||
|
static bool react(derived_view* f, SrcState& s, const event_t& ev)
|
||
|
{
|
||
|
if (guard_fn == nullptr or (f->*guard_fn)(ev)) {
|
||
4 years ago
|
if (react_fn != nullptr) {
|
||
|
(f->*react_fn)(ev);
|
||
|
}
|
||
5 years ago
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
template <typename SrcState2, typename Event2>
|
||
|
using is_match = std::is_same<Event2, event_t>;
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
//! Struct used to store FSM states
|
||
5 years ago
|
template <typename... States>
|
||
5 years ago
|
struct state_list : public std::tuple<States...> {
|
||
|
using tuple_base_t = std::tuple<States...>;
|
||
5 years ago
|
using init_state_t = typename std::decay<decltype(std::get<0>(std::declval<tuple_base_t>()))>::type;
|
||
4 years ago
|
static_assert(not type_list_contains<Derived, States...>(), "An FSM cannot contain itself as state");
|
||
5 years ago
|
|
||
5 years ago
|
template <typename... Args>
|
||
4 years ago
|
state_list(base_fsm_t<Derived>* f, Args&&... args) : tuple_base_t(std::forward<Args>(args)...)
|
||
5 years ago
|
{
|
||
|
if (not Derived::is_nested) {
|
||
5 years ago
|
// If Root FSM, call initial state enter method
|
||
5 years ago
|
fsm_details::state_traits<derived_view, init_state_t>::enter_state(
|
||
|
f->derived(), &get_unchecked<init_state_t>(), std::false_type{});
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
template <typename State>
|
||
|
bool is() const
|
||
|
{
|
||
|
return type_idx<State>() == current_idx;
|
||
|
}
|
||
|
|
||
|
template <typename State>
|
||
|
State& get_unchecked()
|
||
|
{
|
||
|
return std::get<type_idx<State>()>(*this);
|
||
|
}
|
||
|
|
||
|
template <typename State>
|
||
|
const State& get_unchecked() const
|
||
|
{
|
||
|
return std::get<type_idx<State>()>(*this);
|
||
|
}
|
||
|
|
||
|
template <typename NextState>
|
||
|
void transit()
|
||
|
{
|
||
|
current_idx = type_idx<NextState>();
|
||
|
}
|
||
|
|
||
5 years ago
|
template <typename State>
|
||
5 years ago
|
constexpr static bool can_hold_type()
|
||
5 years ago
|
{
|
||
4 years ago
|
return srsran::type_list_contains<State, States...>();
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
template <typename State>
|
||
|
constexpr static size_t type_idx()
|
||
|
{
|
||
|
return get_type_index<State, States...>();
|
||
|
}
|
||
|
|
||
|
size_t get_type_idx() const { return current_idx; }
|
||
|
|
||
|
private:
|
||
|
size_t current_idx = 0;
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
template <typename State>
|
||
|
bool is_in_state() const
|
||
|
{
|
||
|
return derived()->states.template is<State>();
|
||
|
}
|
||
|
|
||
5 years ago
|
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>();
|
||
|
}
|
||
|
|
||
5 years ago
|
template <typename State>
|
||
|
const State* get_state() const
|
||
|
{
|
||
5 years ago
|
return &derived()->states.template get_unchecked<State>();
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
std::string current_state_name() const
|
||
5 years ago
|
{
|
||
|
fsm_details::state_name_visitor visitor{};
|
||
4 years ago
|
srsran::visit(visitor, derived()->states);
|
||
5 years ago
|
return visitor.name;
|
||
|
}
|
||
|
|
||
5 years ago
|
//! Static method to check if State belongs to the list of possible states
|
||
|
template <typename State>
|
||
|
constexpr static bool can_hold_state()
|
||
|
{
|
||
4 years ago
|
return fsm_details::fsm_state_list_type<base_fsm_t<Derived> >::template can_hold_type<State>();
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
// Access to CRTP derived class
|
||
|
derived_view* derived() { return static_cast<derived_view*>(this); }
|
||
|
|
||
|
const derived_view* derived() const { return static_cast<const derived_view*>(this); }
|
||
|
|
||
|
template <typename Ev>
|
||
|
bool process_event(Ev&& e)
|
||
|
{
|
||
|
fsm_details::trigger_visitor<derived_view, Ev> visitor{derived(), std::forward<Ev>(e)};
|
||
4 years ago
|
srsran::visit(visitor, derived()->states);
|
||
4 years ago
|
return visitor.result;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
template <typename Derived, typename ParentFSM>
|
||
|
class composite_fsm_t;
|
||
|
|
||
|
//! CRTP Class for all non-nested FSMs
|
||
|
template <typename Derived>
|
||
|
class fsm_t : public base_fsm_t<Derived>
|
||
|
{
|
||
|
protected:
|
||
|
using base_t = fsm_t<Derived>;
|
||
|
template <typename SubFSM>
|
||
|
using subfsm_t = composite_fsm_t<SubFSM, Derived>;
|
||
|
|
||
|
public:
|
||
|
static const bool is_nested = false;
|
||
|
|
||
4 years ago
|
explicit fsm_t(srslog::basic_logger& logger) : logger(logger) {}
|
||
4 years ago
|
|
||
|
// Push Events to FSM
|
||
|
template <typename Ev>
|
||
|
bool trigger(Ev&& e)
|
||
|
{
|
||
|
if (trigger_locked) {
|
||
|
scheduled_event(std::forward<Ev>(e), typename std::is_lvalue_reference<Ev>::type{});
|
||
|
return false;
|
||
|
}
|
||
|
trigger_locked = true;
|
||
|
bool ret = process_event(std::forward<Ev>(e));
|
||
|
while (not pending_events.empty()) {
|
||
|
pending_events.front()();
|
||
|
pending_events.pop_front();
|
||
|
}
|
||
|
trigger_locked = false;
|
||
|
return ret;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
void set_fsm_event_log_level(srslog::basic_levels lvl) { log_level = lvl; }
|
||
5 years ago
|
|
||
4 years ago
|
srslog::basic_logger& get_logger() const { return logger; }
|
||
5 years ago
|
|
||
5 years ago
|
bool is_trigger_locked() const { return trigger_locked; }
|
||
|
|
||
5 years ago
|
//! Log FSM activity method, e.g. state transitions
|
||
5 years ago
|
template <typename... Args>
|
||
|
void log_fsm_activity(const char* format, Args&&... args)
|
||
|
{
|
||
4 years ago
|
switch (log_level) {
|
||
|
case srslog::basic_levels::debug:
|
||
|
logger.debug(format, std::forward<Args>(args)...);
|
||
5 years ago
|
break;
|
||
4 years ago
|
case srslog::basic_levels::info:
|
||
|
logger.info(format, std::forward<Args>(args)...);
|
||
5 years ago
|
break;
|
||
4 years ago
|
case srslog::basic_levels::warning:
|
||
|
logger.warning(format, std::forward<Args>(args)...);
|
||
5 years ago
|
break;
|
||
4 years ago
|
case srslog::basic_levels::error:
|
||
|
logger.error(format, std::forward<Args>(args)...);
|
||
5 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
protected:
|
||
4 years ago
|
using base_fsm_t<Derived>::derived;
|
||
|
using base_fsm_t<Derived>::process_event;
|
||
5 years ago
|
|
||
5 years ago
|
template <typename Ev>
|
||
4 years ago
|
void scheduled_event(Ev&& e, std::true_type t)
|
||
5 years ago
|
{
|
||
|
pending_events.emplace_back([this, e]() { process_event(e); });
|
||
|
}
|
||
|
template <typename Ev>
|
||
4 years ago
|
void scheduled_event(Ev&& e, std::false_type t)
|
||
5 years ago
|
{
|
||
|
pending_events.emplace_back(std::bind([this](Ev& e) { process_event(std::move(e)); }, std::move(e)));
|
||
|
}
|
||
|
|
||
4 years ago
|
srslog::basic_logger& logger;
|
||
|
srslog::basic_levels log_level = srslog::basic_levels::info;
|
||
|
bool trigger_locked = false;
|
||
4 years ago
|
std::deque<srsran::move_callback<void()> > pending_events;
|
||
5 years ago
|
};
|
||
|
|
||
|
template <typename Derived, typename ParentFSM>
|
||
4 years ago
|
class composite_fsm_t : public base_fsm_t<Derived>
|
||
5 years ago
|
{
|
||
|
public:
|
||
4 years ago
|
using base_t = composite_fsm_t<Derived, ParentFSM>;
|
||
5 years ago
|
using parent_t = ParentFSM;
|
||
5 years ago
|
static const bool is_nested = true;
|
||
|
|
||
4 years ago
|
explicit composite_fsm_t(ParentFSM* parent_fsm_) : fsm_ptr(parent_fsm_) {}
|
||
|
composite_fsm_t(composite_fsm_t&&) noexcept = default;
|
||
|
composite_fsm_t& operator=(composite_fsm_t&&) noexcept = default;
|
||
5 years ago
|
|
||
4 years ago
|
// Get pointer to outer FSM in case of HFSM
|
||
5 years ago
|
const parent_t* parent_fsm() const { return fsm_ptr; }
|
||
5 years ago
|
|
||
|
parent_t* parent_fsm() { return fsm_ptr; }
|
||
5 years ago
|
|
||
4 years ago
|
srslog::basic_logger& get_logger() const { return parent_fsm()->get_logger(); }
|
||
4 years ago
|
|
||
|
// Push Events to root FSM
|
||
|
template <typename Ev>
|
||
|
bool trigger(Ev&& e)
|
||
|
{
|
||
|
return parent_fsm()->trigger(std::forward<Ev>(e));
|
||
|
}
|
||
|
|
||
|
// Push events to this subFSM
|
||
|
using base_fsm_t<Derived>::process_event;
|
||
|
|
||
5 years ago
|
protected:
|
||
|
using parent_fsm_t = ParentFSM;
|
||
|
|
||
|
ParentFSM* fsm_ptr = nullptr;
|
||
|
};
|
||
|
|
||
5 years ago
|
/**************************
|
||
|
* Procedure FSM
|
||
|
*************************/
|
||
|
|
||
|
template <typename T>
|
||
5 years ago
|
struct proc_launch_ev {
|
||
5 years ago
|
T args;
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
template <typename Result>
|
||
|
struct proc_complete_ev {
|
||
|
Result result;
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
struct failure_ev {};
|
||
|
|
||
|
template <typename Derived, typename Result = bool>
|
||
5 years ago
|
class proc_fsm_t : public fsm_t<Derived>
|
||
|
{
|
||
|
using fsm_type = Derived;
|
||
|
using fsm_t<Derived>::derived;
|
||
5 years ago
|
|
||
|
protected:
|
||
4 years ago
|
using fsm_t<Derived>::logger;
|
||
5 years ago
|
|
||
5 years ago
|
public:
|
||
|
using base_t = proc_fsm_t<Derived, Result>;
|
||
|
using fsm_t<Derived>::trigger;
|
||
|
|
||
5 years ago
|
// events
|
||
5 years ago
|
template <typename Arg>
|
||
4 years ago
|
using launch_ev = srsran::proc_launch_ev<Arg>;
|
||
|
using complete_ev = srsran::proc_complete_ev<Result>;
|
||
5 years ago
|
|
||
5 years ago
|
// states
|
||
5 years ago
|
struct idle_st {
|
||
5 years ago
|
void enter(Derived* f)
|
||
|
{
|
||
|
if (f->launch_counter > 0) {
|
||
4 years ago
|
f->logger.warning(
|
||
|
"FSM \"%s\": No result was set for run no. %d", get_type_name<Derived>().c_str(), f->launch_counter);
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void enter(Derived* f, const complete_ev& ev)
|
||
|
{
|
||
4 years ago
|
f->logger.info("FSM \"%s\": Finished run no. %d", get_type_name<Derived>().c_str(), f->launch_counter);
|
||
5 years ago
|
f->last_result = ev.result;
|
||
|
for (auto& func : f->listening_fsms) {
|
||
|
func(ev);
|
||
5 years ago
|
}
|
||
5 years ago
|
f->listening_fsms.clear();
|
||
5 years ago
|
}
|
||
|
|
||
|
void exit(Derived* f)
|
||
|
{
|
||
|
f->launch_counter++;
|
||
4 years ago
|
f->logger.info("FSM \"%s\": Starting run no. %d", get_type_name<Derived>().c_str(), f->launch_counter);
|
||
5 years ago
|
}
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
explicit proc_fsm_t(srslog::basic_logger& logger) : fsm_t<Derived>(logger) {}
|
||
5 years ago
|
|
||
5 years ago
|
bool is_running() const { return not base_t::template is_in_state<idle_st>(); }
|
||
5 years ago
|
|
||
|
const Result& get_result() const
|
||
|
{
|
||
4 years ago
|
srsran_assert(launch_counter > 0 and base_t::template is_in_state<idle_st>(), "in proc_fsm_t::get_result");
|
||
|
return last_result;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
template <typename OtherFSM>
|
||
|
void await(OtherFSM* f)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (is_running()) {
|
||
|
listening_fsms.push_back([f](const complete_ev& ev) { return f->trigger(ev); });
|
||
|
} else {
|
||
|
f->trigger(last_result);
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
private:
|
||
5 years ago
|
int launch_counter = 0;
|
||
|
Result last_result = {};
|
||
|
|
||
|
std::vector<std::function<void(const complete_ev& ev)> > listening_fsms;
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
template <typename ProcFSM>
|
||
|
class proc_wait_st
|
||
|
{
|
||
|
public:
|
||
|
explicit proc_wait_st(ProcFSM* proc_ptr_) : proc_ptr(proc_ptr_) {}
|
||
5 years ago
|
|
||
5 years ago
|
template <typename FSM, typename Ev>
|
||
|
void enter(FSM* f, const Ev& ev)
|
||
|
{
|
||
|
if (proc_ptr->is_running()) {
|
||
4 years ago
|
f->get_logger().error("Unable to launch proc1");
|
||
5 years ago
|
f->trigger(typename ProcFSM::complete_ev{false});
|
||
|
}
|
||
4 years ago
|
proc_ptr->trigger(srsran::proc_launch_ev<Ev>{ev});
|
||
5 years ago
|
proc_ptr->await(f);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
ProcFSM* proc_ptr = nullptr;
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
/**************************************
|
||
|
* Event Trigger Scheduling
|
||
|
*************************************/
|
||
|
|
||
5 years ago
|
template <typename Event>
|
||
5 years ago
|
struct event_callback {
|
||
|
event_callback() = default;
|
||
|
template <typename FSM>
|
||
|
explicit event_callback(FSM* f)
|
||
|
{
|
||
|
callback = [f](const Event& ev) { f->trigger(ev); };
|
||
|
}
|
||
|
|
||
|
void operator()(const Event& ev) { callback(ev); }
|
||
|
void operator()(const Event& ev) const { callback(ev); }
|
||
|
|
||
4 years ago
|
srsran::move_task_t to_move_task(const Event& ev)
|
||
5 years ago
|
{
|
||
|
auto& copied_callback = callback;
|
||
|
return [copied_callback, ev]() { copied_callback(ev); };
|
||
|
}
|
||
|
|
||
5 years ago
|
std::function<void(const Event&)> callback;
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
template <typename Event>
|
||
4 years ago
|
srsran::move_task_t make_move_task(const event_callback<Event>& callback, const Event& ev)
|
||
5 years ago
|
{
|
||
|
auto& copied_callback = callback;
|
||
|
return [copied_callback, ev]() { copied_callback(ev); };
|
||
|
}
|
||
|
|
||
|
template <typename Event>
|
||
4 years ago
|
srsran::move_task_t make_move_task(std::vector<event_callback<Event> >&& callbacks, const Event& ev)
|
||
5 years ago
|
{
|
||
|
return std::bind(
|
||
|
[ev](const std::vector<event_callback<Event> >& callbacks) {
|
||
|
for (const auto& callback : callbacks) {
|
||
|
callback(ev);
|
||
|
}
|
||
|
},
|
||
|
std::move(callbacks));
|
||
|
}
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_FSM_H
|