diff options
author | Marcus Müller <marcus.mueller@ettus.com> | 2018-03-30 13:30:42 +0200 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2018-03-30 20:46:04 +0200 |
commit | 50e0156324bec28d4e07b9598798b6267fb39155 (patch) | |
tree | eb68a0da2b193254aba5b105a2eb1593e53c596d | |
parent | a115ef0596144d33d7f8aa6f0e44459109dbc620 (diff) |
uhd: avoid potential static initialization order fiasco
-rw-r--r-- | gr-uhd/include/gnuradio/uhd/usrp_block.h | 19 | ||||
-rw-r--r-- | gr-uhd/lib/usrp_block_impl.cc | 124 | ||||
-rw-r--r-- | gr-uhd/lib/usrp_block_impl.h | 17 | ||||
-rw-r--r-- | gr-uhd/lib/usrp_sink_impl.cc | 6 | ||||
-rw-r--r-- | gr-uhd/lib/usrp_source_impl.cc | 5 |
5 files changed, 127 insertions, 44 deletions
diff --git a/gr-uhd/include/gnuradio/uhd/usrp_block.h b/gr-uhd/include/gnuradio/uhd/usrp_block.h index 6e4245e862..998908b67b 100644 --- a/gr-uhd/include/gnuradio/uhd/usrp_block.h +++ b/gr-uhd/include/gnuradio/uhd/usrp_block.h @@ -30,6 +30,24 @@ namespace gr { namespace uhd { + GR_UHD_API const pmt::pmt_t cmd_chan_key(); + GR_UHD_API const pmt::pmt_t cmd_gain_key(); + GR_UHD_API const pmt::pmt_t cmd_freq_key(); + GR_UHD_API const pmt::pmt_t cmd_lo_offset_key(); + GR_UHD_API const pmt::pmt_t cmd_tune_key(); + GR_UHD_API const pmt::pmt_t cmd_lo_freq_key(); + GR_UHD_API const pmt::pmt_t cmd_dsp_freq_key(); + GR_UHD_API const pmt::pmt_t cmd_rate_key(); + GR_UHD_API const pmt::pmt_t cmd_bandwidth_key(); + GR_UHD_API const pmt::pmt_t cmd_time_key(); + GR_UHD_API const pmt::pmt_t cmd_mboard_key(); + GR_UHD_API const pmt::pmt_t cmd_antenna_key(); + GR_UHD_API const pmt::pmt_t cmd_direction_key(); + GR_UHD_API const pmt::pmt_t cmd_tag_key(); + + GR_UHD_API const pmt::pmt_t ant_direction_rx(); + GR_UHD_API const pmt::pmt_t ant_direction_tx(); + /*! Base class for USRP blocks. * \ingroup uhd_blk * @@ -45,6 +63,7 @@ namespace gr { usrp_block(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature); + public: /*! diff --git a/gr-uhd/lib/usrp_block_impl.cc b/gr-uhd/lib/usrp_block_impl.cc index 0b94e36a68..70256d3071 100644 --- a/gr-uhd/lib/usrp_block_impl.cc +++ b/gr-uhd/lib/usrp_block_impl.cc @@ -39,6 +39,88 @@ usrp_block::usrp_block( // nop } +const pmt::pmt_t gr::uhd::cmd_chan_key() +{ + static const pmt::pmt_t val = pmt::mp("chan"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_gain_key() +{ + static const pmt::pmt_t val = pmt::mp("gain"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_freq_key() +{ + static const pmt::pmt_t val = pmt::mp("freq"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_lo_offset_key() +{ + static const pmt::pmt_t val = pmt::mp("lo_offset"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_tune_key() +{ + static const pmt::pmt_t val = pmt::mp("tune"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_lo_freq_key() +{ + static const pmt::pmt_t val = pmt::mp("lo_freq"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_dsp_freq_key() +{ + static const pmt::pmt_t val = pmt::mp("dsp_freq"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_rate_key() +{ + static const pmt::pmt_t val = pmt::mp("rate"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_bandwidth_key() +{ + static const pmt::pmt_t val = pmt::mp("bandwidth"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_time_key() +{ + static const pmt::pmt_t val = pmt::mp("time"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_mboard_key() +{ + static const pmt::pmt_t val = pmt::mp("mboard"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_antenna_key() +{ + static const pmt::pmt_t val = pmt::mp("antenna"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_direction_key() +{ + static const pmt::pmt_t val = pmt::mp("direction"); + return val; +} +const pmt::pmt_t gr::uhd::cmd_tag_key() +{ + static const pmt::pmt_t val = pmt::mp("tag"); + return val; +} + +const pmt::pmt_t gr::uhd::ant_direction_rx() +{ + static const pmt::pmt_t val = pmt::mp("RX"); + return val; +} +const pmt::pmt_t gr::uhd::ant_direction_tx() +{ + static const pmt::pmt_t val = pmt::mp("TX"); + return val; +} + usrp_block_impl::usrp_block_impl( const ::uhd::device_addr_t &device_addr, const ::uhd::stream_args_t &stream_args, @@ -69,15 +151,15 @@ usrp_block_impl::usrp_block_impl( // cuz we lazy: #define REGISTER_CMD_HANDLER(key, _handler) register_msg_cmd_handler(key, boost::bind(&usrp_block_impl::_handler, this, _1, _2, _3)) // Register default command handlers: - REGISTER_CMD_HANDLER(CMD_FREQ_KEY, _cmd_handler_freq); - REGISTER_CMD_HANDLER(CMD_GAIN_KEY, _cmd_handler_gain); - REGISTER_CMD_HANDLER(CMD_LO_OFFSET_KEY, _cmd_handler_looffset); - REGISTER_CMD_HANDLER(CMD_TUNE_KEY, _cmd_handler_tune); - REGISTER_CMD_HANDLER(CMD_LO_FREQ_KEY, _cmd_handler_lofreq); - REGISTER_CMD_HANDLER(CMD_DSP_FREQ_KEY, _cmd_handler_dspfreq); - REGISTER_CMD_HANDLER(CMD_RATE_KEY, _cmd_handler_rate); - REGISTER_CMD_HANDLER(CMD_BANDWIDTH_KEY, _cmd_handler_bw); - REGISTER_CMD_HANDLER(CMD_ANTENNA_KEY, _cmd_handler_antenna); + REGISTER_CMD_HANDLER(cmd_freq_key(), _cmd_handler_freq); + REGISTER_CMD_HANDLER(cmd_gain_key(), _cmd_handler_gain); + REGISTER_CMD_HANDLER(cmd_lo_offset_key(), _cmd_handler_looffset); + REGISTER_CMD_HANDLER(cmd_tune_key(), _cmd_handler_tune); + REGISTER_CMD_HANDLER(cmd_lo_freq_key(), _cmd_handler_lofreq); + REGISTER_CMD_HANDLER(cmd_dsp_freq_key(), _cmd_handler_dspfreq); + REGISTER_CMD_HANDLER(cmd_rate_key(), _cmd_handler_rate); + REGISTER_CMD_HANDLER(cmd_bandwidth_key(), _cmd_handler_bw); + REGISTER_CMD_HANDLER(cmd_antenna_key(), _cmd_handler_antenna); } usrp_block_impl::~usrp_block_impl() @@ -456,7 +538,7 @@ void usrp_block_impl::msg_handler_command(pmt::pmt_t msg) pmt::pmt_t new_msg = pmt::make_dict(); new_msg = pmt::dict_add(new_msg, pmt::tuple_ref(msg, 0), pmt::tuple_ref(msg, 1)); if (pmt::length(msg) == 3) { - new_msg = pmt::dict_add(new_msg, CMD_CHAN_KEY, pmt::tuple_ref(msg, 2)); + new_msg = pmt::dict_add(new_msg, cmd_chan_key(), pmt::tuple_ref(msg, 2)); } GR_LOG_WARN(d_debug_logger, boost::format("Using legacy message format (tuples): %s") % msg); return msg_handler_command(new_msg); @@ -481,14 +563,14 @@ void usrp_block_impl::msg_handler_command(pmt::pmt_t msg) /*** Start the actual message processing *************************/ /// 1) Check if there's a time stamp - if (pmt::dict_has_key(msg, CMD_TIME_KEY)) { + if (pmt::dict_has_key(msg, cmd_time_key())) { size_t mboard_index = pmt::to_long( pmt::dict_ref( - msg, CMD_MBOARD_KEY, + msg, cmd_mboard_key(), pmt::from_long( ::uhd::usrp::multi_usrp::ALL_MBOARDS ) // Default to all mboards ) ); - pmt::pmt_t timespec_p = pmt::dict_ref(msg, CMD_TIME_KEY, pmt::PMT_NIL); + pmt::pmt_t timespec_p = pmt::dict_ref(msg, cmd_time_key(), pmt::PMT_NIL); if (timespec_p == pmt::PMT_NIL) { clear_command_time(mboard_index); } else { @@ -504,7 +586,7 @@ void usrp_block_impl::msg_handler_command(pmt::pmt_t msg) /// 2) Read chan value int chan = int(pmt::to_long( pmt::dict_ref( - msg, CMD_CHAN_KEY, + msg, cmd_chan_key(), pmt::from_long(-1) // Default to all chans ) )); @@ -527,7 +609,7 @@ void usrp_block_impl::msg_handler_command(pmt::pmt_t msg) /// 4) See if a direction was specified pmt::pmt_t direction = pmt::dict_ref( - msg, CMD_DIRECTION_KEY, + msg, cmd_direction_key(), pmt::PMT_NIL // Anything except "TX" or "RX will default to the messaged block direction" ); @@ -573,8 +655,8 @@ void usrp_block_impl::_cmd_handler_freq(const pmt::pmt_t &freq_, int chan, const { double freq = pmt::to_double(freq_); ::uhd::tune_request_t new_tune_reqest(freq); - if (pmt::dict_has_key(msg, CMD_LO_OFFSET_KEY)) { - double lo_offset = pmt::to_double(pmt::dict_ref(msg, CMD_LO_OFFSET_KEY, pmt::PMT_NIL)); + if (pmt::dict_has_key(msg, cmd_lo_offset_key())) { + double lo_offset = pmt::to_double(pmt::dict_ref(msg, cmd_lo_offset_key(), pmt::PMT_NIL)); new_tune_reqest = ::uhd::tune_request_t(freq, lo_offset); } @@ -583,7 +665,7 @@ void usrp_block_impl::_cmd_handler_freq(const pmt::pmt_t &freq_, int chan, const void usrp_block_impl::_cmd_handler_looffset(const pmt::pmt_t &lo_offset, int chan, const pmt::pmt_t &msg) { - if (pmt::dict_has_key(msg, CMD_FREQ_KEY)) { + if (pmt::dict_has_key(msg, cmd_freq_key())) { // Then it's already taken care of return; } @@ -661,8 +743,8 @@ void usrp_block_impl::_cmd_handler_lofreq(const pmt::pmt_t &lofreq, int chan, co ::uhd::tune_request_t new_tune_request = _curr_tune_req[chan]; new_tune_request.rf_freq = pmt::to_double(lofreq); - if (pmt::dict_has_key(msg, CMD_DSP_FREQ_KEY)) { - new_tune_request.dsp_freq = pmt::to_double(pmt::dict_ref(msg, CMD_DSP_FREQ_KEY, pmt::PMT_NIL)); + if (pmt::dict_has_key(msg, cmd_dsp_freq_key())) { + new_tune_request.dsp_freq = pmt::to_double(pmt::dict_ref(msg, cmd_dsp_freq_key(), pmt::PMT_NIL)); } new_tune_request.rf_freq_policy = ::uhd::tune_request_t::POLICY_MANUAL; new_tune_request.dsp_freq_policy = ::uhd::tune_request_t::POLICY_MANUAL; @@ -672,7 +754,7 @@ void usrp_block_impl::_cmd_handler_lofreq(const pmt::pmt_t &lofreq, int chan, co void usrp_block_impl::_cmd_handler_dspfreq(const pmt::pmt_t &dspfreq, int chan, const pmt::pmt_t &msg) { - if (pmt::dict_has_key(msg, CMD_LO_FREQ_KEY)) { + if (pmt::dict_has_key(msg, cmd_lo_freq_key())) { // Then it's already dealt with return; } diff --git a/gr-uhd/lib/usrp_block_impl.h b/gr-uhd/lib/usrp_block_impl.h index ab7194393b..f5d89fdc52 100644 --- a/gr-uhd/lib/usrp_block_impl.h +++ b/gr-uhd/lib/usrp_block_impl.h @@ -37,23 +37,6 @@ namespace gr { static const size_t ALL_CHANS = ::uhd::usrp::multi_usrp::ALL_CHANS; static const std::string ALL_GAINS = ::uhd::usrp::multi_usrp::ALL_GAINS; - static const pmt::pmt_t CMD_CHAN_KEY = pmt::mp("chan"); - static const pmt::pmt_t CMD_GAIN_KEY = pmt::mp("gain"); - static const pmt::pmt_t CMD_FREQ_KEY = pmt::mp("freq"); - static const pmt::pmt_t CMD_LO_OFFSET_KEY = pmt::mp("lo_offset"); - static const pmt::pmt_t CMD_TUNE_KEY = pmt::mp("tune"); - static const pmt::pmt_t CMD_LO_FREQ_KEY = pmt::mp("lo_freq"); - static const pmt::pmt_t CMD_DSP_FREQ_KEY = pmt::mp("dsp_freq"); - static const pmt::pmt_t CMD_RATE_KEY = pmt::mp("rate"); - static const pmt::pmt_t CMD_BANDWIDTH_KEY = pmt::mp("bandwidth"); - static const pmt::pmt_t CMD_TIME_KEY = pmt::mp("time"); - static const pmt::pmt_t CMD_MBOARD_KEY = pmt::mp("mboard"); - static const pmt::pmt_t CMD_ANTENNA_KEY = pmt::mp("antenna"); - static const pmt::pmt_t CMD_DIRECTION_KEY = pmt::mp("direction"); - - static const pmt::pmt_t ANT_DIRECTION_RX = pmt::mp("RX"); - static const pmt::pmt_t ANT_DIRECTION_TX = pmt::mp("TX"); - #ifdef UHD_USRP_MULTI_USRP_LO_CONFIG_API static const std::string ALL_LOS = ::uhd::usrp::multi_usrp::ALL_LOS; #else diff --git a/gr-uhd/lib/usrp_sink_impl.cc b/gr-uhd/lib/usrp_sink_impl.cc index a9eb264eb8..9914d46e57 100644 --- a/gr-uhd/lib/usrp_sink_impl.cc +++ b/gr-uhd/lib/usrp_sink_impl.cc @@ -139,7 +139,7 @@ namespace gr { usrp_sink_impl::_set_center_freq_from_internals(size_t chan, pmt::pmt_t direction) { _chans_to_tune.reset(chan); - if (pmt::eqv(direction, ANT_DIRECTION_RX)) { + if (pmt::eqv(direction, ant_direction_rx())) { // TODO: what happens if the RX device is not instantiated? Catch error? return _dev->set_rx_freq(_curr_tune_req[chan], _stream_args.channels[chan]); } else { @@ -518,14 +518,14 @@ namespace gr { // If it's on the first sample, immediately do the tune: GR_LOG_DEBUG(d_debug_logger, boost::format("Received tx_freq on start of burst.")); pmt::pmt_t freq_cmd = pmt::make_dict(); - freq_cmd = pmt::dict_add(freq_cmd, CMD_FREQ_KEY, value); + freq_cmd = pmt::dict_add(freq_cmd, cmd_freq_key(), value); msg_handler_command(freq_cmd); } else if(pmt::equal(key, FREQ_KEY)) { // If it's not on the first sample, queue this command and only tx until here: GR_LOG_DEBUG(d_debug_logger, boost::format("Received tx_freq mid-burst.")); pmt::pmt_t freq_cmd = pmt::make_dict(); - freq_cmd = pmt::dict_add(freq_cmd, CMD_FREQ_KEY, value); + freq_cmd = pmt::dict_add(freq_cmd, cmd_freq_key(), value); commands_in_burst.push_back(freq_cmd); max_count = my_tag_count + 1; in_burst_cmd_offset = my_tag_count; diff --git a/gr-uhd/lib/usrp_source_impl.cc b/gr-uhd/lib/usrp_source_impl.cc index 84e7934e85..acf67b82ee 100644 --- a/gr-uhd/lib/usrp_source_impl.cc +++ b/gr-uhd/lib/usrp_source_impl.cc @@ -31,7 +31,6 @@ namespace gr { namespace uhd { - const pmt::pmt_t CMD_TAG_KEY = pmt::mp("tag"); usrp_source::sptr usrp_source::make(const ::uhd::device_addr_t &device_addr, @@ -83,7 +82,7 @@ namespace gr { #ifdef GR_UHD_USE_STREAM_API _samps_per_packet = 1; #endif - register_msg_cmd_handler(CMD_TAG_KEY, boost::bind(&usrp_source_impl::_cmd_handler_tag, this, _1)); + register_msg_cmd_handler(cmd_tag_key(), boost::bind(&usrp_source_impl::_cmd_handler_tag, this, _1)); } usrp_source_impl::~usrp_source_impl() @@ -154,7 +153,7 @@ namespace gr { usrp_source_impl::_set_center_freq_from_internals(size_t chan, pmt::pmt_t direction) { _chans_to_tune.reset(chan); - if (pmt::eqv(direction, ANT_DIRECTION_TX)) { + if (pmt::eqv(direction, ant_direction_tx())) { // TODO: what happens if the TX device is not instantiated? Catch error? return _dev->set_tx_freq(_curr_tune_req[chan], _stream_args.channels[chan]); } else { |