diff options
author | Clayton Smith <argilo@gmail.com> | 2020-10-12 09:04:02 -0400 |
---|---|---|
committer | Andrej Rode <mail@andrejro.de> | 2020-10-16 16:19:25 +0200 |
commit | 70b3680ad3cc2339f6a5d87ac54b6257b70083c7 (patch) | |
tree | 4c8cb0ee8e6d5cfae4fb988bf6d50423478f1948 /gr-vocoder/lib | |
parent | e935de54575436ee4b9345fb9668c27cbb0ef082 (diff) |
vocoder: refactor get_next_tx_char callback
The get_next_tx_char callback in the FreeDV modulator block is one of
the last places where snprintf is used. I've eliminated it by using C++
string methods instead. This also removes the 79-character limit.
This change also fixes a bug where the last character of the message was
not transmitted.
Lastly, I refactored the callback to be a static member function,
eliminating the need for a separate structure to hold the callback
state.
Diffstat (limited to 'gr-vocoder/lib')
-rw-r--r-- | gr-vocoder/lib/freedv_tx_ss_impl.cc | 34 | ||||
-rw-r--r-- | gr-vocoder/lib/freedv_tx_ss_impl.h | 16 |
2 files changed, 16 insertions, 34 deletions
diff --git a/gr-vocoder/lib/freedv_tx_ss_impl.cc b/gr-vocoder/lib/freedv_tx_ss_impl.cc index 310a2137e6..1f3dadeb52 100644 --- a/gr-vocoder/lib/freedv_tx_ss_impl.cc +++ b/gr-vocoder/lib/freedv_tx_ss_impl.cc @@ -19,24 +19,6 @@ #include <iostream> #include <stdexcept> -extern "C" { -char get_next_tx_char(void* callback_state) -{ - char c; - struct freedv_tx_callback_state* pstate; - - pstate = (struct freedv_tx_callback_state*)callback_state; - c = *pstate->ptx_str++; - - if (*pstate->ptx_str == 0) { - pstate->ptx_str = pstate->tx_str; - c = 0x0d; // FreeDV uses Carriage Return termination - } - - return c; -} -} - namespace gr { namespace vocoder { @@ -69,9 +51,8 @@ freedv_tx_ss_impl::freedv_tx_ss_impl(int mode, if ((d_freedv = freedv_open(mode)) == NULL) throw std::runtime_error("freedv_tx_ss_impl: freedv_open failed"); #endif - snprintf(d_cb_state.tx_str, 79, "%s", d_msg_text.c_str()); - d_cb_state.ptx_str = d_cb_state.tx_str; - freedv_set_callback_txt(d_freedv, NULL, get_next_tx_char, (void*)&d_cb_state); + d_tx_str = msg_txt + "\r"; // FreeDV uses Carriage Return termination + freedv_set_callback_txt(d_freedv, NULL, get_next_tx_char, this); d_nom_modem_samples = freedv_get_n_nom_modem_samples(d_freedv); set_output_multiple(d_nom_modem_samples); } @@ -124,5 +105,16 @@ int freedv_tx_ss_impl::work(int noutput_items, return noutput_items; } +char freedv_tx_ss_impl::get_next_tx_char(void* callback_state) +{ + freedv_tx_ss_impl* instance = static_cast<freedv_tx_ss_impl*>(callback_state); + char c = instance->d_tx_str[instance->d_tx_str_offset++]; + + if (instance->d_tx_str_offset == instance->d_tx_str.length()) + instance->d_tx_str_offset = 0; + + return c; +} + } /* namespace vocoder */ } /* namespace gr */ diff --git a/gr-vocoder/lib/freedv_tx_ss_impl.h b/gr-vocoder/lib/freedv_tx_ss_impl.h index 31ed023eb7..f4f341b97e 100644 --- a/gr-vocoder/lib/freedv_tx_ss_impl.h +++ b/gr-vocoder/lib/freedv_tx_ss_impl.h @@ -13,18 +13,6 @@ #include <gnuradio/vocoder/freedv_tx_ss.h> -extern "C" { -struct freedv_tx_callback_state { - char tx_str[80]; - char* ptx_str; - int calls; -}; -char get_next_tx_char(void* callback_state); -void get_next_proto(void* callback_state, char* proto_bits); -void datarx(void* callback_state, unsigned char* packet, size_t size); -void datatx(void* callback_state, unsigned char* packet, size_t* size); -} - namespace gr { namespace vocoder { @@ -33,7 +21,9 @@ class freedv_tx_ss_impl : public freedv_tx_ss private: short* d_speech_in; short* d_mod_out; - struct freedv_tx_callback_state d_cb_state; + std::string d_tx_str; + std::string::size_type d_tx_str_offset = 0; + static char get_next_tx_char(void* callback_state); struct freedv* d_freedv; int d_mode; std::string d_msg_text; |