From 36fc88d2e2d8262f953ae9f39b97a78954aff5e8 Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Wed, 8 Jul 2020 13:14:18 +0100 Subject: [PATCH] fsm log macros, and utility methods to convert event callbacks to move tasks --- lib/include/srslte/common/fsm.h | 63 ++++++++++++++++++++++----- srsue/src/stack/rrc/phy_controller.cc | 30 ++++--------- 2 files changed, 60 insertions(+), 33 deletions(-) diff --git a/lib/include/srslte/common/fsm.h b/lib/include/srslte/common/fsm.h index 0a044b580..4664e2893 100644 --- a/lib/include/srslte/common/fsm.h +++ b/lib/include/srslte/common/fsm.h @@ -31,6 +31,17 @@ #include #include +#define otherfsmDebug(f, fmt, ...) f->get_log()->debug("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__) +#define otherfsmInfo(f, fmt, ...) f->get_log()->info("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__) +#define otherfsmWarning(f, fmt, ...) \ + f->get_log()->warning("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__) +#define otherfsmError(f, fmt, ...) f->get_log()->error("FSM \"%s\" - " fmt, get_type_name(*f).c_str(), ##__VA_ARGS__) + +#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__) + namespace srslte { //! Forward declarations @@ -215,16 +226,16 @@ struct apply_first_guard_pass > { if (triggered) { // Log Transition if (std::is_same::value) { - f->log_fsm_activity("FSM \"%s\": Event \"%s\" updated state \"%s\"\n", - get_type_name().c_str(), - get_type_name().c_str(), - get_type_name().c_str()); + otherfsmInfo(static_cast(f), + "Event \"%s\" triggered state \"%s\" update\n", + get_type_name().c_str(), + get_type_name().c_str()); } else { - f->log_fsm_activity("FSM \"%s\": Transition detected - %s -> %s (cause: %s)", - get_type_name().c_str(), - get_type_name().c_str(), - get_type_name().c_str(), - get_type_name().c_str()); + otherfsmInfo(static_cast(f), + "transition detected - %s -> %s (cause: %s)", + get_type_name().c_str(), + get_type_name().c_str(), + get_type_name().c_str()); // Apply state change operations state_traits::template transit_state(f, ev); } @@ -239,9 +250,8 @@ struct apply_first_guard_pass > { template static bool trigger(FSM* f, SrcState& s, const Event& ev) { - f->get_log()->debug("FSM \"%s\": Unhandled event caught: \"%s\"\n", - get_type_name().c_str(), - get_type_name().c_str()); + otherfsmDebug( + static_cast(f), "unhandled event caught: \"%s\"\n", get_type_name().c_str()); return false; } }; @@ -681,6 +691,10 @@ private: ProcFSM* proc_ptr = nullptr; }; +/************************************** + * Event Trigger Scheduling + *************************************/ + template struct event_callback { event_callback() = default; @@ -693,9 +707,34 @@ struct event_callback { void operator()(const Event& ev) { callback(ev); } void operator()(const Event& ev) const { callback(ev); } + srslte::move_task_t to_move_task(const Event& ev) + { + auto& copied_callback = callback; + return [copied_callback, ev]() { copied_callback(ev); }; + } + std::function callback; }; +template +srslte::move_task_t make_move_task(const event_callback& callback, const Event& ev) +{ + auto& copied_callback = callback; + return [copied_callback, ev]() { copied_callback(ev); }; +} + +template +srslte::move_task_t make_move_task(std::vector >&& callbacks, const Event& ev) +{ + return std::bind( + [ev](const std::vector >& callbacks) { + for (const auto& callback : callbacks) { + callback(ev); + } + }, + std::move(callbacks)); +} + } // namespace srslte #endif // SRSLTE_FSM_H diff --git a/srsue/src/stack/rrc/phy_controller.cc b/srsue/src/stack/rrc/phy_controller.cc index d406a9e4a..08c9cfe81 100644 --- a/srsue/src/stack/rrc/phy_controller.cc +++ b/srsue/src/stack/rrc/phy_controller.cc @@ -71,10 +71,7 @@ void phy_controller::selecting_cell::enter(phy_controller* f, const cell_sel_cmd csel_callback = ev.callback; csel_res.result = false; - f->log_h->info("Starting \"%s\" for pci=%d, earfcn=%d\n", - srslte::get_type_name(*this).c_str(), - target_cell.pci, - target_cell.earfcn); + fsmInfo("Starting for pci=%d, earfcn=%d\n", target_cell.pci, target_cell.earfcn); f->stack->start_cell_select(&target_cell); } @@ -83,15 +80,13 @@ void phy_controller::selecting_cell::exit(phy_controller* f) wait_in_sync_timer.stop(); if (csel_res.result) { - log_h->info("Cell %s successfully selected\n", to_string(target_cell).c_str()); + fsmInfo("Cell %s successfully selected\n", to_string(target_cell).c_str()); } else { - log_h->warning("Failed to select cell %s\n", to_string(target_cell).c_str()); + fsmWarning("Failed to select cell %s\n", to_string(target_cell).c_str()); } // Signal result back to FSM that called cell selection - auto& copied_callback = csel_callback; - bool result = csel_res.result; - f->stack->defer_task([copied_callback, result]() { copied_callback(result); }); + f->stack->defer_task(srslte::make_move_task(csel_callback, csel_res.result)); } void phy_controller::selecting_cell::wait_in_sync::enter(selecting_cell* f, const cell_sel_res& ev) @@ -109,7 +104,7 @@ bool phy_controller::start_cell_search(const srslte::event_callback()) { - log_h->warning("Failed to launch cell search\n"); + fsmWarning("Failed to launch cell search\n"); return false; } return true; @@ -140,25 +135,18 @@ void phy_controller::handle_cell_search_res(searching_cell& s, const cell_srch_r { switch (result.cs_ret.found) { case cell_search_ret_t::CELL_FOUND: - log_h->info("PHY cell search completed. Found cell %s\n", to_string(result.found_cell).c_str()); + fsmInfo("PHY cell search completed. Found cell %s\n", to_string(result.found_cell).c_str()); break; case cell_search_ret_t::CELL_NOT_FOUND: - log_h->warning("PHY cell search completed. No cells found.\n"); + fsmWarning("PHY cell search completed. No cells found.\n"); break; default: - log_h->error("Invalid cell search result\n"); + fsmError("Invalid cell search result\n"); // TODO: check what errors can happen (currently not handled in our code) } // Signal result back to FSM that called cell search - auto& moved_callbacks = csearch_callbacks; - stack->defer_task(std::bind( - [result](std::vector >& callbacks) { - for (auto& f : callbacks) { - f(result); - } - }, - std::move(moved_callbacks))); + stack->defer_task(srslte::make_move_task(std::move(csearch_callbacks), result)); } void phy_controller::share_cell_search_res(searching_cell& s, const cell_search_cmd& cmd)