Reimplemented clock_offset_control block in C++
diff --git a/include/grgsm/receiver/CMakeLists.txt b/include/grgsm/receiver/CMakeLists.txt
index 4b92f7f..99ebf4f 100644
--- a/include/grgsm/receiver/CMakeLists.txt
+++ b/include/grgsm/receiver/CMakeLists.txt
@@ -21,5 +21,6 @@
 # Install public header files
 ########################################################################
 install(FILES
+    clock_offset_control.h
     receiver.h DESTINATION include/grgsm/receiver
 )
diff --git a/include/grgsm/receiver/clock_offset_control.h b/include/grgsm/receiver/clock_offset_control.h
new file mode 100644
index 0000000..981c0dc
--- /dev/null
+++ b/include/grgsm/receiver/clock_offset_control.h
@@ -0,0 +1,60 @@
+/* -*- 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_GSM_CLOCK_OFFSET_CONTROL_H
+#define INCLUDED_GSM_CLOCK_OFFSET_CONTROL_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API clock_offset_control : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<clock_offset_control> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of gsm::clock_offset_control.
+       *
+       * To avoid accidental use of raw pointers, gsm::clock_offset_control's
+       * constructor is in a private implementation
+       * class. gsm::clock_offset_control::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(float fc);
+      virtual void set_fc(float fc) = 0;      
+    };
+   
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_CLOCK_OFFSET_CONTROL_H */
+ 
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index feaf807..2c29631 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -29,6 +29,7 @@
     receiver/receiver_config.cc 
     receiver/viterbi_detector.cc 
     receiver/sch.c
+    receiver/clock_offset_control_impl.cc
     misc_utils/bursts_printer_impl.cc
     misc_utils/extract_system_info_impl.cc
     demapping/universal_ctrl_chans_demapper_impl.cc
diff --git a/lib/receiver/clock_offset_control_impl.cc b/lib/receiver/clock_offset_control_impl.cc
new file mode 100644
index 0000000..e97d5424
--- /dev/null
+++ b/lib/receiver/clock_offset_control_impl.cc
@@ -0,0 +1,160 @@
+/* -*- 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 <sch.h>
+#include "clock_offset_control_impl.h"
+
+namespace gr
+{
+namespace gsm
+{
+clock_offset_control::sptr
+clock_offset_control::make(float fc)
+{
+    return gnuradio::get_initial_sptr
+           (new clock_offset_control_impl(fc));
+}
+
+
+/*
+ * The private constructor
+ */
+clock_offset_control_impl::clock_offset_control_impl(float fc)
+    : gr::block("clock_offset_control",
+                gr::io_signature::make(0, 0, 0),
+                gr::io_signature::make(0, 0, 0)),
+                d_timer(d_io_service)
+
+{
+    message_port_register_in(pmt::mp("measurements"));
+    set_msg_handler(pmt::mp("measurements"), boost::bind(&clock_offset_control_impl::process_measurement, this, _1));    
+    message_port_register_out(pmt::mp("ppm"));
+    
+    set_fc(fc);
+    d_alfa = 0.3;
+    d_ppm_estimate = -1e6;
+    d_last_ppm_estimate = -1e6;    
+    d_first_measurement = true;
+    d_counter = 0;
+    d_last_state = "";
+    
+    d_timer.async_wait(boost::bind(&clock_offset_control_impl::timed_reset, this));
+}
+
+/*
+ * Our virtual destructor.
+ */
+clock_offset_control_impl::~clock_offset_control_impl()
+{
+}
+
+void clock_offset_control_impl::set_fc(float fc)
+{
+    d_fc = fc;
+}
+
+void clock_offset_control_impl::process_measurement(pmt::pmt_t msg)
+{
+    if(pmt::is_tuple(msg))
+    {
+        std::string key = pmt::symbol_to_string(pmt::tuple_ref(msg,0));
+        if(key == "freq_offset")
+        {
+            float freq_offset = pmt::to_double(pmt::tuple_ref(msg,1));
+            float ppm = -freq_offset/d_fc*1.0e6;
+            std::string state = pmt::symbol_to_string(pmt::tuple_ref(msg,2));
+            d_last_state = state;
+            if(abs(ppm) > 100) //safeguard against flawed measurements
+            {
+                ppm = 0; //!!! do poprawki ten warunek
+                reset();
+            }
+
+            if(state == "fcch_search")
+            {
+                pmt::pmt_t msg_ppm = pmt::from_double(ppm);
+                message_port_pub(pmt::intern("ppm"), msg_ppm);
+                d_timer.cancel();
+                d_timer.async_wait(boost::bind(&clock_offset_control_impl::timed_reset, this));
+            } 
+            else 
+            if (state == "synchronized")
+            {
+                d_timer.cancel();
+                if(d_first_measurement)
+                {
+                    d_ppm_estimate = ppm;
+                    d_first_measurement = false; 
+                }
+                else
+                {
+                    d_ppm_estimate = (1-d_alfa)*d_ppm_estimate+d_alfa*ppm;
+                }
+                
+                if(d_counter == 5)
+                {
+                    d_counter = 0;
+                    if(abs(d_last_ppm_estimate-d_ppm_estimate) > 0.1)
+                    {
+                        pmt::pmt_t msg_ppm = pmt::from_double(ppm);
+                        message_port_pub(pmt::intern("ppm"), msg_ppm);
+                        d_last_ppm_estimate = d_ppm_estimate;
+                    }
+                }
+                else
+                {
+                    d_counter=d_counter+1;
+                }
+            } 
+            else
+            if(state == "sync_loss")
+            {
+                reset();
+                pmt::pmt_t msg_ppm = pmt::from_double(0.0);
+                message_port_pub(pmt::intern("ppm"), msg_ppm);
+            }            
+        }
+    }
+}
+
+void clock_offset_control_impl::timed_reset()
+{
+    reset();
+    pmt::pmt_t msg_ppm = pmt::from_double(0.0);
+    message_port_pub(pmt::intern("ppm"), msg_ppm);
+}
+
+
+void clock_offset_control_impl::reset()
+{
+    d_ppm_estimate = -1e6;
+    d_counter = 0;
+    d_first_measurement = true;
+}
+
+} /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/receiver/clock_offset_control_impl.h b/lib/receiver/clock_offset_control_impl.h
new file mode 100644
index 0000000..823bfdf
--- /dev/null
+++ b/lib/receiver/clock_offset_control_impl.h
@@ -0,0 +1,59 @@
+/* -*- 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_GSM_CLOCK_OFFSET_CONTROL_IMPL_H
+#define INCLUDED_GSM_CLOCK_OFFSET_CONTROL_IMPL_H
+
+#include <grgsm/receiver/clock_offset_control.h>
+#include <string>
+#include <boost/asio.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+namespace gr {
+  namespace gsm {
+    class clock_offset_control_impl : public clock_offset_control
+    {
+     private:
+        float d_fc;
+        float d_alfa;
+        float d_ppm_estimate;
+        float d_last_ppm_estimate;        
+        bool  d_first_measurement;
+        int   d_counter;
+        std::string d_last_state;
+        boost::asio::io_service d_io_service;
+        boost::asio::deadline_timer d_timer;
+                        
+        void process_measurement(pmt::pmt_t msg);
+        void timed_reset();
+        void reset();
+     public:
+       clock_offset_control_impl(float fc);
+      ~clock_offset_control_impl();
+
+      virtual void set_fc(float fc);
+    };
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_CLOCK_OFFSET_CONTROL_IMPL_H */
+
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index 0fa695b..e3a40cb 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -36,7 +36,6 @@
     receiver/sch_detector.py
     receiver/fcch_detector.py 
     receiver/chirpz.py
-    receiver/clock_offset_control.py 
     misc_utils/clock_offset_corrector.py DESTINATION ${GR_PYTHON_DIR}/grgsm
 )
 
diff --git a/python/__init__.py b/python/__init__.py
index 9dd531a..41adfe8 100644
--- a/python/__init__.py
+++ b/python/__init__.py
@@ -49,7 +49,6 @@
 from fcch_burst_tagger import fcch_burst_tagger
 from sch_detector import sch_detector
 from fcch_detector import fcch_detector
-from clock_offset_control import clock_offset_control
 from clock_offset_corrector import clock_offset_corrector
 from gsm_input import gsm_input
 
diff --git a/python/receiver/README b/python/receiver/README
index 4bdc467..449171d 100644
--- a/python/receiver/README
+++ b/python/receiver/README
@@ -1,5 +1,4 @@
 chirpz.py - implementation of chirpz transform
-clock_offset_control.py - block implementing simple algorithm for clock offset control
 gsm_input.py - adapts signal to the input of the gsm receiver, executes clock offset correction
 fcch_detector.py - (experimental componen for a new gsm receiver) detects fcch bursts
 fcch_burst_tagger.py - (experimental componen for a new gsm receiver) part of fcch_detector - add fcch tag when FCCH burst is found
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index 52b7d88..a9f5091 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -9,6 +9,7 @@
 
 %{
 #include "grgsm/receiver/receiver.h"
+#include "grgsm/receiver/clock_offset_control.h"
 #include "grgsm/decoding/control_channels_decoder.h"
 #include "grgsm/decryption/decryption.h"
 #include "grgsm/demapping/universal_ctrl_chans_demapper.h"
@@ -23,6 +24,9 @@
 
 %include "grgsm/receiver/receiver.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, receiver);
+%include "grgsm/receiver/clock_offset_control.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, clock_offset_control);
+
 
 %include "grgsm/decoding/control_channels_decoder.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, control_channels_decoder);