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.
610 lines
21 KiB
C++
610 lines
21 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
|
#include "srsran/adt/fsm.h"
|
||
4 years ago
|
#include "srsran/common/test_common.h"
|
||
5 years ago
|
|
||
|
/////////////////////////////
|
||
|
|
||
|
// Events
|
||
5 years ago
|
struct ev1 {};
|
||
|
struct ev2 {};
|
||
5 years ago
|
|
||
5 years ago
|
std::vector<std::string> calls;
|
||
4 years ago
|
|
||
5 years ago
|
template <typename State>
|
||
4 years ago
|
void call_log_helper(State* state, srslog::basic_logger& logger, const char* type)
|
||
5 years ago
|
{
|
||
4 years ago
|
std::string callname = srsran::get_type_name<State>() + "::" + type;
|
||
4 years ago
|
logger.info("%s custom called", callname.c_str());
|
||
5 years ago
|
calls.push_back(callname);
|
||
|
}
|
||
|
|
||
4 years ago
|
class fsm1 : public srsran::fsm_t<fsm1>
|
||
5 years ago
|
{
|
||
|
public:
|
||
5 years ago
|
uint32_t idle_enter_counter = 0, state1_enter_counter = 0, inner_enter_counter = 0;
|
||
5 years ago
|
uint32_t foo_counter = 0;
|
||
|
|
||
5 years ago
|
// states
|
||
5 years ago
|
struct idle_st {
|
||
|
void enter(fsm1* f);
|
||
|
};
|
||
|
struct state1 {
|
||
5 years ago
|
void enter(fsm1* f) {}
|
||
|
void enter(fsm1* f, const ev1& ev);
|
||
|
void enter(fsm1* f, const ev2& ev);
|
||
5 years ago
|
void exit(fsm1* f);
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
explicit fsm1(srslog::basic_logger& logger) : srsran::fsm_t<fsm1>(logger) {}
|
||
5 years ago
|
|
||
5 years ago
|
// this state is another FSM
|
||
5 years ago
|
class fsm2 : public subfsm_t<fsm2>
|
||
5 years ago
|
{
|
||
|
public:
|
||
5 years ago
|
// states
|
||
5 years ago
|
struct state_inner {
|
||
|
void enter(fsm2* f)
|
||
|
{
|
||
4 years ago
|
call_log_helper(this, f->get_logger(), "enter");
|
||
5 years ago
|
f->parent_fsm()->inner_enter_counter++;
|
||
|
}
|
||
|
};
|
||
|
struct state_inner2 {
|
||
4 years ago
|
void enter(fsm2* f) { call_log_helper(this, f->get_logger(), "enter"); }
|
||
|
void exit(fsm2* f) { call_log_helper(this, f->get_logger(), "exit"); }
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
explicit fsm2(fsm1* f_) : composite_fsm_t(f_) {}
|
||
5 years ago
|
fsm2(fsm2&&) = default;
|
||
|
fsm2& operator=(fsm2&&) = default;
|
||
4 years ago
|
~fsm2() { get_logger().info("%s being destroyed!", get_type_name(*this).c_str()); }
|
||
5 years ago
|
|
||
4 years ago
|
void enter(fsm1* f) { call_log_helper(this, get_logger(), "enter"); }
|
||
|
void exit(fsm1* f) { call_log_helper(this, get_logger(), "exit"); }
|
||
5 years ago
|
|
||
5 years ago
|
private:
|
||
5 years ago
|
void inner_action1(state_inner& s, const ev1& e);
|
||
|
void inner_action2(state_inner& s, const ev2& e);
|
||
|
void inner_action3(state_inner2& s, const ev2& e);
|
||
5 years ago
|
|
||
5 years ago
|
protected:
|
||
|
// list of states
|
||
5 years ago
|
state_list<state_inner, state_inner2> states{this};
|
||
5 years ago
|
// clang-format off
|
||
|
using transitions = transition_table<
|
||
5 years ago
|
// Start Target Event Action
|
||
5 years ago
|
// +------------+-------------+----+----------------------+
|
||
5 years ago
|
upd<state_inner, ev1, &fsm2::inner_action1 >,
|
||
5 years ago
|
row<state_inner, state_inner2, ev2, &fsm2::inner_action2 >,
|
||
|
row<state_inner2, state1, ev2, &fsm2::inner_action3 >
|
||
|
// +------------+-------------+----+----------------------+
|
||
|
>;
|
||
|
// clang-format on
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
private:
|
||
5 years ago
|
void action1(idle_st& s, const ev1& e);
|
||
|
void action2(state1& s, const ev1& e);
|
||
|
void action3(state1& s, const ev2& e);
|
||
5 years ago
|
|
||
5 years ago
|
protected:
|
||
|
void foo(ev1 e) { foo_counter++; }
|
||
|
|
||
5 years ago
|
// list of states + transitions
|
||
5 years ago
|
state_list<idle_st, state1, fsm2> states = {this, idle_st{}, state1{}, fsm2{this}};
|
||
5 years ago
|
|
||
5 years ago
|
// clang-format off
|
||
|
using transitions = transition_table<
|
||
|
// Start Target Event Action
|
||
|
// +------------+-------------+----+------------------+
|
||
5 years ago
|
row< idle_st, state1, ev1, &fsm1::action1 >,
|
||
|
row< state1, fsm2, ev1, &fsm1::action2 >,
|
||
|
row< state1, idle_st, ev2, &fsm1::action3 >
|
||
|
// +------------+-------------+----+------------------+
|
||
5 years ago
|
>;
|
||
|
// clang-format on
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
void fsm1::idle_st::enter(fsm1* f)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, f->logger, "enter");
|
||
5 years ago
|
f->idle_enter_counter++;
|
||
5 years ago
|
}
|
||
5 years ago
|
void fsm1::state1::enter(fsm1* f, const ev1& ev)
|
||
|
{
|
||
4 years ago
|
call_log_helper(this, f->logger, "enter");
|
||
5 years ago
|
f->state1_enter_counter++;
|
||
|
}
|
||
|
void fsm1::state1::enter(fsm1* f, const ev2& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, f->logger, "enter2");
|
||
5 years ago
|
f->state1_enter_counter++;
|
||
5 years ago
|
}
|
||
5 years ago
|
void fsm1::state1::exit(fsm1* f)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, f->logger, "exit");
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// FSM event handlers
|
||
5 years ago
|
void fsm1::fsm2::inner_action1(state_inner& s, const ev1& e)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, get_logger(), "inner_action1");
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
void fsm1::fsm2::inner_action2(state_inner& s, const ev2& e)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, get_logger(), "inner_action2");
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
void fsm1::fsm2::inner_action3(state_inner2& s, const ev2& e)
|
||
5 years ago
|
{
|
||
4 years ago
|
get_logger().info("fsm2::state_inner2::react called");
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
void fsm1::action1(idle_st& s, const ev1& e)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, logger, "action1");
|
||
5 years ago
|
foo(e);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void fsm1::action2(state1& s, const ev1& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, logger, "action2");
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void fsm1::action3(state1& s, const ev2& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
call_log_helper(this, logger, "action3");
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// Static Checks
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
namespace fsm_details {
|
||
|
|
||
4 years ago
|
static_assert(is_fsm<fsm1>::value, "invalid metafunction");
|
||
|
static_assert(is_composite_fsm<fsm1::fsm2>::value, "invalid metafunction");
|
||
5 years ago
|
static_assert(type_list_size(typename filter_transition_type<ev1, fsm1::idle_st, fsm_transitions<fsm1> >::type{}) > 0,
|
||
4 years ago
|
"invalid filter metafunction");
|
||
5 years ago
|
static_assert(
|
||
5 years ago
|
std::is_same<fsm_state_list_type<fsm1>, fsm1::state_list<fsm1::idle_st, fsm1::state1, fsm1::fsm2> >::value,
|
||
4 years ago
|
"get state list failed");
|
||
|
static_assert(fsm1::can_hold_state<fsm1::state1>(), "failed can_hold_state check");
|
||
|
static_assert(std::is_same<enable_if_fsm_state<fsm1, fsm1::idle_st>, void>::value, "get state list failed");
|
||
|
static_assert(std::is_same<disable_if_fsm_state<fsm1, fsm1::fsm2::state_inner>, void>::value, "get state list failed");
|
||
5 years ago
|
|
||
|
} // namespace fsm_details
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
|
// Runtime checks
|
||
|
|
||
5 years ago
|
int test_hsm()
|
||
|
{
|
||
4 years ago
|
srslog::basic_logger& logger = srslog::fetch_basic_logger("HSM", false);
|
||
|
logger.set_level(srslog::basic_levels::info);
|
||
5 years ago
|
|
||
4 years ago
|
fsm1 f{logger};
|
||
5 years ago
|
TESTASSERT(f.idle_enter_counter == 1);
|
||
|
TESTASSERT(get_type_name(f) == "fsm1");
|
||
5 years ago
|
TESTASSERT(f.current_state_name() == "idle_st");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::idle_st>());
|
||
|
TESTASSERT(f.foo_counter == 0);
|
||
|
|
||
|
// Moving Idle -> State1
|
||
|
ev1 e;
|
||
|
f.trigger(e);
|
||
5 years ago
|
TESTASSERT(f.current_state_name() == "state1");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::state1>());
|
||
|
|
||
|
// Moving State1 -> fsm2
|
||
|
f.trigger(e);
|
||
5 years ago
|
TESTASSERT(f.current_state_name() == "fsm2");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::fsm2>());
|
||
5 years ago
|
TESTASSERT(f.get_if_current_state<fsm1::fsm2>()->current_state_name() == "state_inner");
|
||
5 years ago
|
TESTASSERT(f.inner_enter_counter == 1);
|
||
5 years ago
|
|
||
|
// Fsm2 does not listen to ev1
|
||
|
f.trigger(e);
|
||
5 years ago
|
TESTASSERT(f.current_state_name() == "fsm2");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::fsm2>());
|
||
5 years ago
|
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");
|
||
5 years ago
|
|
||
|
// Moving fsm2 -> state1
|
||
|
f.trigger(ev2{});
|
||
5 years ago
|
TESTASSERT(f.current_state_name() == "state1");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::state1>());
|
||
|
TESTASSERT(f.state1_enter_counter == 2);
|
||
|
|
||
|
// Moving state1 -> idle
|
||
|
f.trigger(ev2{});
|
||
5 years ago
|
TESTASSERT(std::string{f.current_state_name()} == "idle_st");
|
||
5 years ago
|
TESTASSERT(f.is_in_state<fsm1::idle_st>());
|
||
|
TESTASSERT(f.foo_counter == 1);
|
||
|
TESTASSERT(f.idle_enter_counter == 2);
|
||
|
|
||
5 years ago
|
// Call unhandled event
|
||
|
f.trigger(ev2{});
|
||
5 years ago
|
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");
|
||
5 years ago
|
|
||
5 years ago
|
// Ensure correct call order
|
||
4 years ago
|
TESTASSERT(calls[0] == srsran::get_type_name<fsm1::idle_st>() + "::enter"); // enter for init state called
|
||
|
TESTASSERT(calls[1] == srsran::get_type_name<fsm1>() + "::action1");
|
||
|
TESTASSERT(calls[2] == srsran::get_type_name<fsm1::state1>() + "::enter");
|
||
|
TESTASSERT(calls[3] == srsran::get_type_name<fsm1>() + "::action2");
|
||
|
TESTASSERT(calls[4] == srsran::get_type_name<fsm1::state1>() + "::exit");
|
||
|
TESTASSERT(calls[5] == srsran::get_type_name<fsm1::fsm2>() + "::enter"); // entry is recursive
|
||
|
TESTASSERT(calls[6] == srsran::get_type_name<fsm1::fsm2::state_inner>() + "::enter");
|
||
|
TESTASSERT(calls[7] == srsran::get_type_name<fsm1::fsm2>() + "::inner_action1");
|
||
|
TESTASSERT(calls[8] == srsran::get_type_name<fsm1::fsm2>() + "::inner_action2");
|
||
|
TESTASSERT(calls[9] == srsran::get_type_name<fsm1::fsm2::state_inner2>() + "::enter");
|
||
|
TESTASSERT(calls[10] == srsran::get_type_name<fsm1::fsm2::state_inner2>() + "::exit");
|
||
|
TESTASSERT(calls[11] == srsran::get_type_name<fsm1::fsm2>() + "::exit"); // exit is recursive
|
||
|
TESTASSERT(calls[12] == srsran::get_type_name<fsm1::state1>() + "::enter2"); // differentiates different entry funcs
|
||
|
|
||
|
return SRSRAN_SUCCESS;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/////////////////////////////
|
||
5 years ago
|
|
||
5 years ago
|
struct procevent1 {
|
||
|
bool is_success;
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
struct proc1 : public srsran::proc_fsm_t<proc1, int> {
|
||
5 years ago
|
public:
|
||
5 years ago
|
struct procstate1 {
|
||
4 years ago
|
void enter(proc1* f, const srsran::proc_launch_ev<int>& ev);
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
explicit proc1(srslog::basic_logger& logger) : base_t(logger) {}
|
||
5 years ago
|
|
||
|
protected:
|
||
|
// Transitions
|
||
5 years ago
|
void handle_success(procstate1& s, const procevent1& ev);
|
||
|
void handle_failure(procstate1& s, const procevent1& ev);
|
||
5 years ago
|
|
||
5 years ago
|
bool is_success(procstate1& s, const procevent1& ev) { return ev.is_success; }
|
||
5 years ago
|
|
||
5 years ago
|
bool is_failure(procstate1& s, const procevent1& ev) { return not ev.is_success; }
|
||
5 years ago
|
|
||
5 years ago
|
state_list<idle_st, procstate1> states{this, idle_st{}, procstate1{}};
|
||
5 years ago
|
// clang-format off
|
||
|
using transitions = transition_table<
|
||
5 years ago
|
// Start Target Event Action Guard (optional)
|
||
|
// +------------+-------------+----------------+------------------------+--------------------+
|
||
|
row< idle_st, procstate1, launch_ev<int> >,
|
||
|
upd< procstate1, procevent1, &proc1::handle_success, &proc1::is_success >,
|
||
|
upd< procstate1, procevent1, &proc1::handle_failure, &proc1::is_failure >,
|
||
4 years ago
|
to_state< idle_st, complete_ev >
|
||
5 years ago
|
// +------------+-------------+----------------+------------------------+--------------------+
|
||
5 years ago
|
>;
|
||
|
// clang-format on
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
void proc1::procstate1::enter(proc1* f, const launch_ev<int>& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
f->logger.info("started!");
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void proc1::handle_success(procstate1& s, const procevent1& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
logger.info("success!");
|
||
5 years ago
|
trigger(complete_ev{5});
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void proc1::handle_failure(procstate1& s, const procevent1& ev)
|
||
5 years ago
|
{
|
||
4 years ago
|
logger.info("failure!");
|
||
5 years ago
|
trigger(complete_ev{3});
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
struct proc_listener_fsm : public srsran::fsm_t<proc_listener_fsm> {
|
||
5 years ago
|
public:
|
||
|
struct st1 {};
|
||
|
struct st2 {};
|
||
4 years ago
|
using proc1_st = srsran::proc_wait_st<proc1>;
|
||
5 years ago
|
|
||
4 years ago
|
proc_listener_fsm(srslog::basic_logger& logger, proc1* proc_ptr_) :
|
||
|
base_t(logger), states(this, st1{}, st2{}, proc1_st{proc_ptr_})
|
||
5 years ago
|
{}
|
||
|
|
||
|
protected:
|
||
|
bool is_success(proc1_st& s, const proc1::complete_ev& ev) { return ev.result; }
|
||
|
|
||
|
// clang-format off
|
||
|
state_list<st1, st2, proc1_st > states;
|
||
|
using f = proc_listener_fsm;
|
||
|
using transitions = transition_table<
|
||
|
// Start Target Event Action Guard (optional)
|
||
|
// +--------------+--------------+-----------------------+------------------------+-------------------+
|
||
|
row< st1, proc1_st, int >,
|
||
|
row< proc1_st, st2, proc1::complete_ev, nullptr, &f::is_success >,
|
||
|
row< proc1_st, st1, proc1::complete_ev >
|
||
|
// +--------------+--------------+-----------------------+------------------------+-------------------+
|
||
|
>;
|
||
|
// clang-format on
|
||
|
};
|
||
|
|
||
5 years ago
|
int test_fsm_proc()
|
||
|
{
|
||
4 years ago
|
proc1 proc{srslog::fetch_basic_logger("PROC", false)};
|
||
|
proc.get_logger().set_level(srslog::basic_levels::info);
|
||
|
proc.set_fsm_event_log_level(srslog::basic_levels::info);
|
||
5 years ago
|
|
||
5 years ago
|
int v = 2;
|
||
5 years ago
|
TESTASSERT(proc.current_state_name() == "idle_st");
|
||
4 years ago
|
proc.trigger(srsran::proc_launch_ev<int>{v});
|
||
5 years ago
|
TESTASSERT(proc.current_state_name() == "procstate1");
|
||
4 years ago
|
proc.trigger(srsran::proc_launch_ev<int>{v});
|
||
5 years ago
|
TESTASSERT(proc.current_state_name() == "procstate1");
|
||
4 years ago
|
proc.trigger(srsran::proc_launch_ev<int>{5});
|
||
5 years ago
|
TESTASSERT(proc.current_state_name() == "procstate1");
|
||
|
proc.trigger(procevent1{true});
|
||
|
TESTASSERT(proc.current_state_name() == "idle_st");
|
||
5 years ago
|
TESTASSERT(proc.get_result() == 5);
|
||
4 years ago
|
proc.trigger(srsran::proc_launch_ev<int>{v});
|
||
5 years ago
|
TESTASSERT(proc.current_state_name() == "procstate1");
|
||
|
proc.trigger(procevent1{false});
|
||
|
TESTASSERT(proc.current_state_name() == "idle_st");
|
||
5 years ago
|
TESTASSERT(proc.get_result() == 3);
|
||
|
|
||
|
{
|
||
4 years ago
|
proc_listener_fsm outer_fsm{srslog::fetch_basic_logger("TEST"), &proc};
|
||
5 years ago
|
TESTASSERT(outer_fsm.is_in_state<proc_listener_fsm::st1>());
|
||
|
outer_fsm.trigger(6);
|
||
|
TESTASSERT(outer_fsm.is_in_state<proc_listener_fsm::proc1_st>());
|
||
|
TESTASSERT(proc.is_running());
|
||
|
proc.trigger(procevent1{true});
|
||
|
TESTASSERT(not proc.is_running());
|
||
|
TESTASSERT(outer_fsm.is_in_state<proc_listener_fsm::st2>());
|
||
|
}
|
||
|
|
||
|
{
|
||
4 years ago
|
proc_listener_fsm outer_fsm{srslog::fetch_basic_logger("TEST"), &proc};
|
||
5 years ago
|
TESTASSERT(outer_fsm.is_in_state<proc_listener_fsm::st1>());
|
||
4 years ago
|
proc.trigger(srsran::proc_launch_ev<int>{v});
|
||
5 years ago
|
TESTASSERT(proc.is_running());
|
||
|
outer_fsm.trigger(7);
|
||
|
TESTASSERT(outer_fsm.is_in_state<proc_listener_fsm::st1>());
|
||
|
TESTASSERT(proc.is_running());
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
return SRSRAN_SUCCESS;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
///////////////////////////
|
||
|
|
||
4 years ago
|
class nas_fsm : public srsran::fsm_t<nas_fsm>
|
||
5 years ago
|
{
|
||
|
public:
|
||
|
// states
|
||
|
struct emm_null_st {};
|
||
|
struct emm_deregistered {};
|
||
|
struct emm_deregistered_initiated {};
|
||
|
struct emm_ta_updating_initiated {};
|
||
|
struct emm_registered {};
|
||
|
struct emm_service_req_initiated {};
|
||
|
struct emm_registered_initiated {};
|
||
|
|
||
|
// events
|
||
|
struct enable_s1_ev {};
|
||
|
struct disable_s1_ev {};
|
||
|
struct attach_request_ev {};
|
||
|
struct emm_registr_fail_ev {}; ///< attach rejected, network init DETACH request, lower layer failure
|
||
|
struct attach_accept_ev {}; ///< attach accept and default EPS bearer context activated
|
||
|
struct sr_initiated_ev {};
|
||
|
struct sr_outcome_ev {};
|
||
|
struct tau_request_ev {};
|
||
|
struct tau_outcome_ev {};
|
||
|
struct tau_reject_other_cause_ev {};
|
||
|
struct power_off_ev {};
|
||
|
struct detach_request_ev {};
|
||
|
struct detach_accept_ev {};
|
||
|
|
||
4 years ago
|
explicit nas_fsm(srslog::basic_logger& logger) : fsm_t<nas_fsm>(logger) {}
|
||
5 years ago
|
|
||
|
protected:
|
||
|
state_list<emm_null_st,
|
||
|
emm_deregistered,
|
||
|
emm_registered_initiated,
|
||
|
emm_registered,
|
||
|
emm_service_req_initiated,
|
||
|
emm_ta_updating_initiated,
|
||
|
emm_deregistered_initiated>
|
||
|
states{nullptr};
|
||
5 years ago
|
// clang-format off
|
||
|
using transitions = transition_table<
|
||
|
// Start Target Event
|
||
|
// +-----------------------------+-------------------------+-----------------------------+
|
||
|
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 >,
|
||
4 years ago
|
to_state< emm_deregistered, power_off_ev >
|
||
5 years ago
|
// +-----------------------------+-------------------------+-----------------------------+
|
||
5 years ago
|
>;
|
||
|
// clang-format on
|
||
5 years ago
|
};
|
||
|
|
||
|
int test_nas_fsm()
|
||
|
{
|
||
4 years ago
|
auto& logger = srslog::fetch_basic_logger("NAS", false);
|
||
|
logger.set_level(srslog::basic_levels::info);
|
||
|
|
||
|
nas_fsm fsm{logger};
|
||
|
|
||
5 years ago
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_null_st>());
|
||
|
|
||
|
// NULL -> EMM-DEREGISTERED
|
||
|
fsm.trigger(nas_fsm::enable_s1_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
|
// EMM-DEREGISTERED -> EMM-NULL -> EMM-DEREGISTERED
|
||
|
fsm.trigger(nas_fsm::disable_s1_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_null_st>());
|
||
|
fsm.trigger(nas_fsm::enable_s1_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
|
// EMM-DEREGISTERED -> EMM-REGISTERED-INITIATED -> EMM-DEREGISTERED
|
||
|
fsm.trigger(nas_fsm::attach_request_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered_initiated>());
|
||
|
fsm.trigger(nas_fsm::emm_registr_fail_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
|
// EMM-DEREGISTERED -> EMM-REGISTERED-INITIATED -> EMM-REGISTERED
|
||
|
fsm.trigger(nas_fsm::attach_request_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered_initiated>());
|
||
|
fsm.trigger(nas_fsm::attach_accept_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered>());
|
||
|
|
||
|
// EMM-REGISTERED -> EMM-SERVICE-REQUEST-INITIATED -> EMM-REGISTERED
|
||
|
fsm.trigger(nas_fsm::sr_initiated_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_service_req_initiated>());
|
||
|
fsm.trigger(nas_fsm::sr_outcome_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered>());
|
||
|
|
||
|
// EMM-REGISTERED -> EMM-TRACKING-AREA-UPDATING-INITIATED -> EMM-REGISTERED
|
||
|
fsm.trigger(nas_fsm::tau_request_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_ta_updating_initiated>());
|
||
|
fsm.trigger(nas_fsm::tau_outcome_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered>());
|
||
|
|
||
|
// EMM-REGISTERED -> EMM-DEREGISTED-INITIATED -> EMM-DEREGISTERED
|
||
|
fsm.trigger(nas_fsm::detach_request_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered_initiated>());
|
||
|
fsm.trigger(nas_fsm::detach_accept_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
|
// EMM-DEREGISTERED -> EMM-REGISTERED-INITIATED -> EMM-REGISTERED -> EMM-TRACKING-AREA-UPDATING-INITIATED ->
|
||
|
// EMM-DEREGISTERED
|
||
|
fsm.trigger(nas_fsm::attach_request_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered_initiated>());
|
||
|
fsm.trigger(nas_fsm::attach_accept_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_registered>());
|
||
|
fsm.trigger(nas_fsm::tau_request_ev{});
|
||
|
fsm.trigger(nas_fsm::tau_reject_other_cause_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
|
// EMM-DEREGISTERED -> EMM-REGISTERED-INITIATED -> EMM-REGISTERED -> EMM-SERVICE-REQUEST-INITIATED -> EMM-DEREGISTERED
|
||
|
// (power-off)
|
||
|
fsm.trigger(nas_fsm::attach_request_ev{});
|
||
|
fsm.trigger(nas_fsm::attach_accept_ev{});
|
||
|
fsm.trigger(nas_fsm::sr_initiated_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_service_req_initiated>());
|
||
|
fsm.trigger(nas_fsm::power_off_ev{});
|
||
|
TESTASSERT(fsm.is_in_state<nas_fsm::emm_deregistered>());
|
||
|
|
||
4 years ago
|
return SRSRAN_SUCCESS;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
struct fsm3 : public srsran::fsm_t<fsm3> {
|
||
5 years ago
|
struct st1 {};
|
||
|
struct st2 {
|
||
|
int counter = 0;
|
||
4 years ago
|
void enter(fsm3* fsm)
|
||
|
{
|
||
|
counter++;
|
||
|
fsm->events.push_back(fsm->current_state_name());
|
||
|
}
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
fsm3() : base_t(srslog::fetch_basic_logger("TEST")) {}
|
||
5 years ago
|
|
||
4 years ago
|
std::vector<std::string> events;
|
||
|
|
||
5 years ago
|
protected:
|
||
4 years ago
|
void handle_ev1(st1& s, const ev1& ev)
|
||
|
{
|
||
|
trigger(ev2{});
|
||
|
events.push_back(current_state_name() + "::action"); // still in st1
|
||
|
}
|
||
5 years ago
|
void handle_ev2(st2& s, const ev2& ev)
|
||
5 years ago
|
{
|
||
|
if (s.counter < 2) {
|
||
|
trigger(ev1{});
|
||
|
}
|
||
4 years ago
|
events.push_back(current_state_name() + "::action"); // still in st2
|
||
5 years ago
|
}
|
||
|
|
||
|
state_list<st1, st2> states{this};
|
||
|
// clang-format off
|
||
|
using transitions = transition_table<
|
||
|
// Start Target Event Action
|
||
|
// +------------------------+-------------------------+-------------------+--------------------+
|
||
4 years ago
|
row< st1, st2, ev1, &fsm3::handle_ev1 >,
|
||
|
row< st2, st1, ev2, &fsm3::handle_ev2 >
|
||
5 years ago
|
// +------------------------+-------------------------+-------------------+--------------------+
|
||
5 years ago
|
>;
|
||
|
// clang-format on
|
||
|
};
|
||
|
|
||
|
int test_fsm_self_trigger()
|
||
|
{
|
||
|
fsm3 fsm;
|
||
4 years ago
|
TESTASSERT(fsm.events.empty());
|
||
5 years ago
|
|
||
|
TESTASSERT(fsm.is_in_state<fsm3::st1>());
|
||
|
fsm.trigger(ev1{});
|
||
|
TESTASSERT(fsm.is_in_state<fsm3::st1>());
|
||
|
|
||
4 years ago
|
TESTASSERT(fsm.events.size() == 6);
|
||
|
TESTASSERT(fsm.events[0] == "st1::action");
|
||
|
TESTASSERT(fsm.events[1] == "st2");
|
||
|
TESTASSERT(fsm.events[2] == "st2::action");
|
||
|
TESTASSERT(fsm.events[3] == "st1::action");
|
||
|
TESTASSERT(fsm.events[4] == "st2");
|
||
|
TESTASSERT(fsm.events[5] == "st2::action");
|
||
|
|
||
4 years ago
|
return SRSRAN_SUCCESS;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
int main()
|
||
|
{
|
||
4 years ago
|
srslog::basic_logger& logger = srslog::fetch_basic_logger("TEST", false);
|
||
|
logger.set_level(srslog::basic_levels::info);
|
||
|
srslog::init();
|
||
|
|
||
4 years ago
|
TESTASSERT(test_hsm() == SRSRAN_SUCCESS);
|
||
4 years ago
|
logger.info("TEST \"hsm\" finished successfully\n");
|
||
4 years ago
|
TESTASSERT(test_fsm_proc() == SRSRAN_SUCCESS);
|
||
4 years ago
|
logger.info("TEST \"proc\" finished successfully\n");
|
||
4 years ago
|
TESTASSERT(test_nas_fsm() == SRSRAN_SUCCESS);
|
||
4 years ago
|
logger.info("TEST \"nas fsm\" finished successfully\n");
|
||
4 years ago
|
TESTASSERT(test_fsm_self_trigger() == SRSRAN_SUCCESS);
|
||
4 years ago
|
logger.info("TEST \"fsm self trigger\" finished successfully\n");
|
||
5 years ago
|
|
||
4 years ago
|
return SRSRAN_SUCCESS;
|
||
5 years ago
|
}
|