New blocks (msg to tag and controlled resampler) related stuff
diff --git a/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc b/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc
new file mode 100644
index 0000000..62087d6
--- /dev/null
+++ b/lib/misc_utils/controlled_fractional_resampler_cc_impl.cc
@@ -0,0 +1,198 @@
+/* -*- c++ -*- */
+/* @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ * 
+ * Gr-gsm is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * Gr-gsm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with gr-gsm; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ * 
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gnuradio/io_signature.h>
+#include "controlled_fractional_resampler_cc_impl.h"
+#include <stdexcept>
+
+namespace gr {
+  namespace gsm {
+
+    controlled_fractional_resampler_cc::sptr
+    controlled_fractional_resampler_cc::make(float phase_shift, float resamp_ratio)
+    {
+      return gnuradio::get_initial_sptr
+        (new controlled_fractional_resampler_cc_impl(phase_shift, resamp_ratio));
+    }
+
+    controlled_fractional_resampler_cc_impl::controlled_fractional_resampler_cc_impl
+                                     (float phase_shift, float resamp_ratio)
+      : block("controlled_fractional_resampler_cc",
+              io_signature::make(1, 1, sizeof(gr_complex)),
+              io_signature::make(1, 1, sizeof(gr_complex))),
+        d_mu(phase_shift), d_mu_inc(resamp_ratio),
+        d_resamp(new mmse_fir_interpolator_cc())
+    {
+      this->set_tag_propagation_policy(TPP_DONT);
+      if(resamp_ratio <=  0)
+        throw std::out_of_range("resampling ratio must be > 0");
+      if(phase_shift <  0  || phase_shift > 1)
+        throw std::out_of_range("phase shift ratio must be > 0 and < 1");
+
+      set_relative_rate(1.0 / resamp_ratio);
+    }
+
+    controlled_fractional_resampler_cc_impl::~controlled_fractional_resampler_cc_impl()
+    {
+      delete d_resamp;
+    }
+
+    void
+    controlled_fractional_resampler_cc_impl::forecast(int noutput_items,
+                                           gr_vector_int &ninput_items_required)
+    {
+      unsigned ninputs = ninput_items_required.size();
+      for(unsigned i=0; i < ninputs; i++) {
+        ninput_items_required[i] =
+          (int)ceil((noutput_items * d_mu_inc) + d_resamp->ntaps());
+      }
+    }
+
+    int
+    controlled_fractional_resampler_cc_impl::general_work(int noutput_items,
+                                               gr_vector_int &ninput_items,
+                                               gr_vector_const_void_star &input_items,
+                                               gr_vector_void_star &output_items)
+    {
+      const gr_complex *in = (const gr_complex*)input_items[0];
+      gr_complex *out = (gr_complex*)output_items[0];
+      
+      uint64_t processed_in = 0; //input samples processed in the last call to resample function
+      uint64_t processed_in_sum = 0; //input samples processed during a whole call to general_work function
+      uint64_t produced_out_sum = 0; //output samples produced during a whole call to general_work function
+
+      std::vector<tag_t> set_resamp_ratio_tags;
+
+      pmt::pmt_t key = pmt::string_to_symbol("set_resamp_ratio");
+      get_tags_in_window(set_resamp_ratio_tags, 0, 0, ninput_items[0]);
+
+//      std::cout << "-----------------------------" << std::endl;
+//      std::cout << "ninput_items[0] " << ninput_items[0] << std::endl;
+//      std::cout << "noutput_items " << noutput_items << std::endl;
+      
+      bool all_output_samples_produced = false;
+      for(std::vector<tag_t>::iterator i_tag = set_resamp_ratio_tags.begin(); i_tag < set_resamp_ratio_tags.end(); i_tag++)
+      {
+        uint64_t tag_offset_rel = i_tag->offset - nitems_read(0);
+        
+        if(pmt::symbol_to_string(i_tag->key) == "set_resamp_ratio")
+        {
+          uint64_t samples_to_produce = static_cast<uint64_t>(round(static_cast<double>(tag_offset_rel-processed_in_sum)/d_mu_inc)); //tu może być problem - bo to jest głupota przy d_mu_inc różnym od 1.0
+          
+          if( (samples_to_produce + produced_out_sum) > noutput_items)
+          {
+            samples_to_produce = noutput_items - produced_out_sum;
+            all_output_samples_produced = true;
+          }
+          
+  //        std::cout << "samples_to_produce = (tag_offset_rel-processed_in_sum)/d_mu_inc: " << samples_to_produce << " = " << tag_offset_rel << " - " << processed_in_sum << std::endl;
+          processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce);
+          processed_in_sum = processed_in_sum + processed_in;
+          produced_out_sum = produced_out_sum + samples_to_produce;
+
+  //        std::cout << "d_ii " << d_ii << std::endl;        
+  //        std::cout << "produced_out_sum + nitems_written(0) " << produced_out_sum + nitems_written(0) << std::endl;
+          if(all_output_samples_produced)
+          {
+            break;
+          } else {
+              add_item_tag(0, produced_out_sum + nitems_written(0), i_tag->key, i_tag->value);                       
+              set_resamp_ratio(pmt::to_double(i_tag->value));
+          }
+        } else {
+          uint64_t out_samples_to_tag = round(static_cast<double>(tag_offset_rel-processed_in_sum)/d_mu_inc);
+          if( (out_samples_to_tag + produced_out_sum) <= noutput_items)
+          {
+            add_item_tag(0, produced_out_sum + out_samples_to_tag + nitems_written(0), i_tag->key, i_tag->value);
+          }
+        }
+//        std::cout << "processed_in: " << processed_in <<   " tag_offset_rel: " << tag_offset_rel << " produced_out_sum: " << produced_out_sum << std::endl;        
+//        std::cout << "Setting resamp ratio: " << d_mu_inc << std::endl;
+      }
+
+//      std::cout << "noutput_items: " << noutput_items << " produced_out_sum: " << produced_out_sum << std::endl;
+//      std::cout << "last_resample_outputs: " << (noutput_items-produced_out_sum) << std::endl;
+//      processed_in = resample(in, processed_in_sum, out, produced_out_sum, samples_to_produce);
+//      processed_in_sum = processed_in_sum + processed_in;
+//      produced_out_sum = produced_out_sum + samples_to_produce;
+//      processed_in = resample(in, 0, out, 0, noutput_items);
+
+      if(!all_output_samples_produced)
+      {
+        processed_in = resample(in, processed_in_sum, out, produced_out_sum, (noutput_items-produced_out_sum));
+        processed_in_sum = processed_in_sum + processed_in;
+      }
+      
+      consume_each(processed_in_sum);
+      return noutput_items;
+    }
+    
+    inline uint64_t 
+    controlled_fractional_resampler_cc_impl::resample(const gr_complex *in, uint64_t first_in_sample, gr_complex *out, uint64_t first_out_sample, uint64_t samples_to_produce)
+    {
+      int ii = first_in_sample;
+      int oo = first_out_sample;
+      while(oo < (first_out_sample+samples_to_produce)) //produce samples_to_produce number of samples
+      {
+        out[oo++] = d_resamp->interpolate(&in[ii], d_mu);
+      
+        double s = d_mu + d_mu_inc;
+        double f = floor(s);
+        int incr = (int)f;
+        d_mu = s - f;
+        ii += incr;
+      }
+      return ii-first_in_sample; //number of input samples processed
+    }
+
+    float
+    controlled_fractional_resampler_cc_impl::mu() const
+    {
+      return d_mu;
+    }
+
+    float
+    controlled_fractional_resampler_cc_impl::resamp_ratio() const
+    {
+      return d_mu_inc;
+    }
+
+    void
+    controlled_fractional_resampler_cc_impl::set_mu(float mu)
+    {
+      d_mu = mu;
+    }
+
+    void
+    controlled_fractional_resampler_cc_impl::set_resamp_ratio(float resamp_ratio)
+    {
+      d_mu_inc = resamp_ratio;
+      set_relative_rate(1.0 / resamp_ratio);
+    }
+
+  } /* namespace grgsm */
+} /* namespace gr */
+
diff --git a/lib/misc_utils/controlled_fractional_resampler_cc_impl.h b/lib/misc_utils/controlled_fractional_resampler_cc_impl.h
new file mode 100644
index 0000000..dab8a0b
--- /dev/null
+++ b/lib/misc_utils/controlled_fractional_resampler_cc_impl.h
@@ -0,0 +1,72 @@
+/* -*- c++ -*- */
+/* @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ * 
+ * Gr-gsm is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * Gr-gsm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with gr-gsm; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ * 
+ */
+
+#ifndef INCLUDED_GRGSM_CONTROLLED_FRACTIONAL_RESAMPLER_CC_IMPL_H
+#define INCLUDED_GRGSM_CONTROLLED_FRACTIONAL_RESAMPLER_CC_IMPL_H
+
+#include <grgsm/controlled_fractional_resampler_cc.h>
+//#include <gnuradio/filter/fractional_resampler_cc.h>
+#include <gnuradio/filter/mmse_fir_interpolator_cc.h>
+
+using namespace gr::filter;
+
+namespace gr {
+  namespace gsm {
+
+    class controlled_fractional_resampler_cc_impl : public controlled_fractional_resampler_cc
+    {
+    private:
+      float d_mu;
+      float d_mu_inc;
+      mmse_fir_interpolator_cc *d_resamp;
+      
+      inline uint64_t resample(const gr_complex *in, 
+           uint64_t first_in_sample, 
+           gr_complex *out, 
+           uint64_t first_out_sample, 
+           uint64_t samples_to_produce);
+                                               
+    public:
+      controlled_fractional_resampler_cc_impl(float phase_shift,
+                                   float resamp_ratio);
+      ~controlled_fractional_resampler_cc_impl();
+
+      void forecast(int noutput_items,
+		       gr_vector_int &ninput_items_required);
+
+      int general_work(int noutput_items,
+		       gr_vector_int &ninput_items,
+		       gr_vector_const_void_star &input_items,
+		       gr_vector_void_star &output_items);
+
+
+      float mu() const;
+      float resamp_ratio() const;
+      void set_mu(float mu);
+      void set_resamp_ratio(float resamp_ratio);
+    };
+
+  } // namespace grgsm
+} // namespace gr
+
+#endif /* INCLUDED_GRGSM_CONTROLLED_FRACTIONAL_RESAMPLER_CC_IMPL_H */
+
diff --git a/lib/misc_utils/controlled_rotator_cc_impl.cc b/lib/misc_utils/controlled_rotator_cc_impl.cc
index 6211d49..68fa207 100644
--- a/lib/misc_utils/controlled_rotator_cc_impl.cc
+++ b/lib/misc_utils/controlled_rotator_cc_impl.cc
@@ -74,10 +74,8 @@
     controlled_rotator_cc_impl::work(int noutput_items,
 			  gr_vector_const_void_star &input_items,
 			  gr_vector_void_star &output_items)
-    {
-      const gr_complex *in = (const gr_complex *)input_items[0];
-      gr_complex *out = (gr_complex *)output_items[0];
-
+		{
+		  //process phase_inc input
       if(input_items.size() == 2) {
         int ii=0;
         const float *pp = (const float *)input_items[1];
@@ -101,10 +99,38 @@
           ii++;
         }
       }
-      d_r.rotateN(out, const_cast<gr_complex *>(in), noutput_items); //const_cast<gr_complex *> is workaround old implementation of rotateN that is still present in ubuntu 14.04 packages
-      return noutput_items;
-    }
+      		
+      //get complex input and output
+      const gr_complex *in = (const gr_complex *)input_items[0];
+      gr_complex *out = (gr_complex *)output_items[0];
+		  //get tags
 
+      uint64_t processed_in = 0;
+      uint64_t produced_out = 0;
+
+      std::vector<tag_t> set_phase_inc_tags;
+
+      pmt::pmt_t key = pmt::string_to_symbol("set_phase_inc");
+      get_tags_in_window(set_phase_inc_tags, 0, 0, noutput_items, key);
+      
+      for(std::vector<tag_t>::iterator i_tag = set_phase_inc_tags.begin(); i_tag < set_phase_inc_tags.end(); i_tag++){
+        uint64_t tag_offset_rel = i_tag->offset-nitems_read(0);
+        set_phase_inc(pmt::to_double(i_tag->value));
+        uint64_t samples_to_process = tag_offset_rel-processed_in;
+        d_r.rotateN((out+produced_out), const_cast<gr_complex *>(in+processed_in), samples_to_process);
+        processed_in = processed_in + samples_to_process;
+        produced_out = produced_out + samples_to_process;
+//        std::cout << "Rotator, phase inc: " << pmt::to_double(i_tag->value) << std::endl;
+        
+        float freq_offset_setting = (pmt::to_double(i_tag->value) / (2*M_PI)) * d_samp_rate; //send stream tag with a new value of the frequency offset
+        pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
+        pmt::pmt_t value =  pmt::from_double(freq_offset_setting);
+        add_item_tag(0,i_tag->offset, key, value);
+      }
+      
+      d_r.rotateN((out+produced_out), const_cast<gr_complex *>(in+processed_in), (noutput_items-produced_out)); //const_cast<gr_complex *> is workaround old implementation of rotateN that is still present in ubuntu 14.04 packages
+      return noutput_items;
+		}
   } /* namespace gsm */
 } /* namespace gr */
 
diff --git a/lib/misc_utils/controlled_rotator_cc_impl.h b/lib/misc_utils/controlled_rotator_cc_impl.h
index 14064cb..45c5af9 100644
--- a/lib/misc_utils/controlled_rotator_cc_impl.h
+++ b/lib/misc_utils/controlled_rotator_cc_impl.h
@@ -42,6 +42,7 @@
 
       virtual void set_phase_inc(double phase_inc);
       virtual void set_samp_rate(double samp_rate);
+
       // Where all the action really happens
       int work(int noutput_items,
 	       gr_vector_const_void_star &input_items,
diff --git a/lib/misc_utils/msg_to_tag_impl.cc b/lib/misc_utils/msg_to_tag_impl.cc
new file mode 100644
index 0000000..e572864
--- /dev/null
+++ b/lib/misc_utils/msg_to_tag_impl.cc
@@ -0,0 +1,99 @@
+/* -*- c++ -*- */
+/* @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ * 
+ * Gr-gsm is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * Gr-gsm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with gr-gsm; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ * 
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gnuradio/io_signature.h>
+#include "msg_to_tag_impl.h"
+
+namespace gr {
+  namespace gsm {
+
+    msg_to_tag::sptr
+    msg_to_tag::make()
+    {
+      return gnuradio::get_initial_sptr
+        (new msg_to_tag_impl());
+    }
+
+    void msg_to_tag_impl::queue_msg(pmt::pmt_t msg){
+      if(pmt::is_dict(msg)){
+        try {
+          pmt::pmt_t keys = pmt::dict_keys(msg);
+        } catch (const pmt::wrong_type &e) {
+          msg = pmt::dict_add(pmt::make_dict(), pmt::car(msg), pmt::cdr(msg));
+        }
+      }
+      d_msg_queue.push_back(msg);
+    }
+
+    /*
+     * The private constructor
+     */
+    msg_to_tag_impl::msg_to_tag_impl()
+      : gr::sync_block("msg_to_tag",
+              gr::io_signature::make(1, 1, sizeof(gr_complex)),
+              gr::io_signature::make(1, 1, sizeof(gr_complex)))              
+    {
+      message_port_register_in(pmt::mp("msg"));
+      set_msg_handler(pmt::mp("msg"), boost::bind(&msg_to_tag_impl::queue_msg, this, _1));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    msg_to_tag_impl::~msg_to_tag_impl()
+    {
+    }
+
+    int
+    msg_to_tag_impl::work(int noutput_items,
+        gr_vector_const_void_star &input_items,
+        gr_vector_void_star &output_items)
+    {
+      while(!d_msg_queue.empty()){
+        pmt::pmt_t msg(d_msg_queue.front());
+        d_msg_queue.pop_front();
+        if(pmt::is_dict(msg)){
+          pmt::pmt_t klist(pmt::dict_keys(msg));
+          for (size_t i = 0; i < pmt::length(klist); i++) {
+            pmt::pmt_t k(pmt::nth(i, klist));
+            pmt::pmt_t v(pmt::dict_ref(msg, k, pmt::PMT_NIL));
+            add_item_tag(0, nitems_written(0), k, v, alias_pmt());
+          }
+        } else if(pmt::is_number(msg)) {
+          add_item_tag(0, nitems_written(0), pmt::intern(""), msg, alias_pmt());
+        } else if(pmt::is_symbol(msg)) {
+          add_item_tag(0, nitems_written(0), msg, pmt::intern(""), alias_pmt());
+        }        
+      }
+
+      memcpy(output_items[0], input_items[0], sizeof(gr_complex)*noutput_items);
+      // Tell runtime system how many output items we produced.
+      return noutput_items;
+    }
+
+  } /* namespace grgsm */
+} /* namespace gr */
+
diff --git a/lib/misc_utils/msg_to_tag_impl.h b/lib/misc_utils/msg_to_tag_impl.h
new file mode 100644
index 0000000..77d03d0
--- /dev/null
+++ b/lib/misc_utils/msg_to_tag_impl.h
@@ -0,0 +1,51 @@
+/* -*- c++ -*- */
+/* @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ * 
+ * Gr-gsm is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * Gr-gsm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with gr-gsm; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ * 
+ */
+
+#ifndef INCLUDED_GRGSM_MSG_TO_TAG_IMPL_H
+#define INCLUDED_GRGSM_MSG_TO_TAG_IMPL_H
+
+#include <grgsm/msg_to_tag.h>
+
+namespace gr {
+  namespace gsm {
+
+    class msg_to_tag_impl : public msg_to_tag
+    {
+     private:
+        std::deque<pmt::pmt_t> d_msg_queue;
+
+     public:
+      msg_to_tag_impl();
+      ~msg_to_tag_impl();
+      void queue_msg(pmt::pmt_t msg);
+
+      // Where all the action really happens
+      int work(int noutput_items,
+         gr_vector_const_void_star &input_items,
+         gr_vector_void_star &output_items);
+    };
+
+  } // namespace grgsm
+} // namespace gr
+
+#endif /* INCLUDED_GRGSM_MSG_TO_TAG_IMPL_H */
+