Added timestamping of bursts in the gsm receiver
diff --git a/lib/receiver/CMakeLists.txt b/lib/receiver/CMakeLists.txt
index 343d2f8..1c5fd53 100644
--- a/lib/receiver/CMakeLists.txt
+++ b/lib/receiver/CMakeLists.txt
@@ -23,4 +23,6 @@
     receiver_config.cc
     receiver_impl.cc
     viterbi_detector.cc
+    time_spec.cc
+    time_sample_ref.cc
 )
diff --git a/lib/receiver/receiver_impl.cc b/lib/receiver/receiver_impl.cc
index b7c5be2..db7d9dd 100644
--- a/lib/receiver/receiver_impl.cc
+++ b/lib/receiver/receiver_impl.cc
@@ -74,10 +74,12 @@
     ) : gr::sync_block("receiver",
           gr::io_signature::make(1, -1, sizeof(gr_complex)),
           gr::io_signature::make(0, 0, 0)),
+        d_rx_time_received(false),
+        d_time_samp_ref(GSM_SYMBOL_RATE * osr),
         d_OSR(osr),
         d_process_uplink(process_uplink),
         d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
-        d_counter(0),
+        d_counter(0),       //TODO: use nitems_read instead of d_counter
         d_fcch_start_pos(0),
         d_freq_offset_setting(0),
         d_state(fcch_search),
@@ -147,6 +149,7 @@
       std::vector<const gr_complex *> iii =
         (std::vector<const gr_complex *>) input_items;
 #endif
+      
 
       /* Time synchronization loop */
       float current_time =
@@ -175,7 +178,25 @@
           d_freq_offset_tag_in_fcch = tag_offset < last_sample_nr;
         }
       }
+      
+      /* Obtaining current time with use of rx_time tag provided i.e. by UHD devices */
+      /* And storing it in time_sample_ref for sample number to time conversion */
+      std::vector<tag_t> rx_time_tags;
 
+      get_tags_in_window(rx_time_tags, 0, 0, noutput_items, pmt::string_to_symbol("rx_time"));
+      if(!rx_time_tags.empty()){
+        d_rx_time_received = true;
+        tag_t rx_time_tag = *(rx_time_tags.begin());
+
+        uint64_t rx_time_full_part = to_uint64(tuple_ref(rx_time_tag.value,0));
+        double rx_time_frac_part = to_double(tuple_ref(rx_time_tag.value,1));
+        
+        time_spec_t current_rx_time = time_spec_t(rx_time_full_part, rx_time_frac_part);
+        uint64_t current_start_offset = rx_time_tag.offset;
+        d_time_samp_ref.update(current_rx_time, current_start_offset);
+//        std::cout << "Mam rx_time: " << current_rx_time.get_real_secs() << std::endl;
+      }
+      
       /* Main state machine */
       switch (d_state) {
       case fcch_search:
@@ -335,7 +356,7 @@
 
           /* Compose a message with GSMTAP header and bits */
           send_burst(d_burst_nr, output_binary,
-            GSMTAP_BURST_SCH, input_nr);
+            GSMTAP_BURST_SCH, input_nr, d_c0_burst_start);
 
           /* Attempt to decode SCH burst */
           rc = decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
@@ -381,7 +402,7 @@
 
           /* Compose a message with GSMTAP header and bits */
           send_burst(d_burst_nr, output_binary,
-            GSMTAP_BURST_NORMAL, input_nr);
+            GSMTAP_BURST_NORMAL, input_nr, d_c0_burst_start);
 
           break;
         }
@@ -405,13 +426,13 @@
 
             /* Compose a message with GSMTAP header and bits */
             send_burst(d_burst_nr, output_binary,
-              GSMTAP_BURST_NORMAL, input_nr); 
+              GSMTAP_BURST_NORMAL, input_nr, normal_burst_start); 
           } else {
             d_c0_burst_start = dummy_burst_start;
 
             /* Compose a message with GSMTAP header and bits */
             send_burst(d_burst_nr, dummy_burst,
-              GSMTAP_BURST_DUMMY, input_nr);
+              GSMTAP_BURST_DUMMY, input_nr, dummy_burst_start);
           }
 
           break;
@@ -463,7 +484,7 @@
             burst_start, output_binary);
 
           /* Compose a message with GSMTAP header and bits */
-          send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
+          send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr, burst_start);
 
           break;
         }
@@ -980,7 +1001,7 @@
     void
     receiver_impl::send_burst(burst_counter burst_nr,
       const unsigned char * burst_binary, uint8_t burst_type,
-      unsigned int input_nr)
+      unsigned int input_nr, unsigned int burst_start)
     {
       /* Buffer for GSMTAP header and burst */
       uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE];
@@ -1019,6 +1040,17 @@
       tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
       tap_header->snr_db = 0; /* FIXME: Can we calculate this? */
 
+      pmt::pmt_t pdu_header = pmt::make_dict();
+
+      /* Add timestamp of the first sample - if available */
+      if(d_rx_time_received) {
+        time_spec_t time_spec_of_first_sample = d_time_samp_ref.offset_to_time(nitems_read(0)+burst_start);
+        uint64_t full = time_spec_of_first_sample.get_full_secs();
+        double   frac = time_spec_of_first_sample.get_frac_secs();
+        pdu_header    = 
+          pmt::dict_add(pdu_header, pmt::mp("fn_time"), pmt::cons(pmt::from_uint64(frame_number), pmt::cons(pmt::from_uint64(full), pmt::from_double(frac))));
+      }
+
       /* Copy burst to the buffer */
       memcpy(burst, burst_binary, BURST_SIZE);
 
@@ -1093,6 +1125,5 @@
     {
       d_state = fcch_search;
     }
-
   } /* namespace gsm */
 } /* namespace gr */
diff --git a/lib/receiver/receiver_impl.h b/lib/receiver/receiver_impl.h
index 6831bf1..4972b62 100644
--- a/lib/receiver/receiver_impl.h
+++ b/lib/receiver/receiver_impl.h
@@ -28,12 +28,15 @@
 #include <gsm_constants.h>
 #include <receiver_config.h>
 #include <vector>
+#include "time_sample_ref.h"
 
 namespace gr {
   namespace gsm {
     class receiver_impl : public receiver
     {
      private:
+        bool d_rx_time_received;
+        time_sample_ref d_time_samp_ref;
         unsigned int d_c0_burst_start;
         float d_c0_signal_dbm;
         
@@ -195,7 +198,7 @@
          * @param burst_binary - content of the burst
          * @b_type - type of the burst
          */
-        void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr);
+        void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr, unsigned int burst_start=-1);
 
         /**
          * Configures burst types in different channels
@@ -209,13 +212,13 @@
             gr_vector_const_void_star &input_items, int noutput_items);
 
      public:
-       receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums, bool process_uplink);
-      ~receiver_impl();
+         receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums, bool process_uplink);
+        ~receiver_impl();
       
-      int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items);
-      virtual void set_cell_allocation(const std::vector<int> &cell_allocation);
-      virtual void set_tseq_nums(const std::vector<int> & tseq_nums);
-      virtual void reset();
+        int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items);
+        virtual void set_cell_allocation(const std::vector<int> &cell_allocation);
+        virtual void set_tseq_nums(const std::vector<int> & tseq_nums);
+        virtual void reset();
     };
   } // namespace gsm
 } // namespace gr
diff --git a/lib/receiver/time_sample_ref.cc b/lib/receiver/time_sample_ref.cc
new file mode 100644
index 0000000..0cf85ed
--- /dev/null
+++ b/lib/receiver/time_sample_ref.cc
@@ -0,0 +1,60 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author (C) 2017 by 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.
+ */
+
+#include <math.h>
+#include "time_sample_ref.h"
+
+namespace gr {
+  namespace gsm {
+    time_sample_ref::time_sample_ref(double samp_rate): d_samp_rate(samp_rate)
+    {
+    }
+    
+    time_sample_ref::~time_sample_ref()
+    {
+    }
+    
+    void time_sample_ref::update(time_spec_t last_rx_time, uint64_t current_start_offset)
+    {
+        d_last_rx_time = last_rx_time;
+        d_current_start_offset = current_start_offset;
+    }
+    
+    time_spec_t time_sample_ref::offset_to_time(uint64_t offset)
+    {
+      uint64_t samples_from_last_rx_time = offset - d_current_start_offset;
+      time_spec_t time = time_spec_t(static_cast<double>(samples_from_last_rx_time)/d_samp_rate) + d_last_rx_time;
+      
+      return time;
+    }
+
+    uint64_t time_sample_ref::time_to_offset(time_spec_t time)
+    {
+      double samples_since_last_rx_time_tag = (time-d_last_rx_time).get_real_secs()*d_samp_rate;
+//      double fractional_part = round(samples_since_last_rx_time_tag) - samples_since_last_rx_time_tag;
+      uint64_t offset = static_cast<uint64_t>(round(samples_since_last_rx_time_tag)) + d_current_start_offset;
+      
+      return offset;
+    }
+  } // namespace gsm
+} // namespace gr
+
diff --git a/lib/receiver/time_sample_ref.h b/lib/receiver/time_sample_ref.h
new file mode 100644
index 0000000..1fecb81
--- /dev/null
+++ b/lib/receiver/time_sample_ref.h
@@ -0,0 +1,49 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author (C) 2017 by 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_TIME_SAMPLE_REF_IMPL_H
+#define INCLUDED_TIME_SAMPLE_REF_IMPL_H
+
+#include <stdint.h>
+#include "time_spec.h"
+
+namespace gr {
+  namespace gsm {
+    /*
+    Class for storing time reference and for conversions time<->sample number 
+    */
+    class time_sample_ref
+    {
+     private:
+        double d_samp_rate;
+        time_spec_t d_last_rx_time;
+        uint64_t d_current_start_offset;
+     public:
+        time_sample_ref(double samp_rate);
+        ~time_sample_ref();
+        void update(time_spec_t last_rx_time, uint64_t current_start_offset);
+        time_spec_t offset_to_time(uint64_t offset);
+        uint64_t time_to_offset(time_spec_t time);
+    };
+  } // namespace gsm
+} // namespace gr
+#endif// INCLUDED_TIME_SAMPLE_REF_IMPL_H
diff --git a/lib/receiver/time_spec.cc b/lib/receiver/time_spec.cc
new file mode 100644
index 0000000..205508d
--- /dev/null
+++ b/lib/receiver/time_spec.cc
@@ -0,0 +1,170 @@
+//
+// Copyright 2011-2013 Ettus Research LLC
+//
+// This program 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 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+#include "time_spec.h"
+
+namespace gr {
+  namespace gsm {
+
+    /***********************************************************************
+     * Time spec system time
+     **********************************************************************/
+
+    #ifdef HAVE_CLOCK_GETTIME
+    #include <time.h>
+    time_spec_t time_spec_t::get_system_time(void){
+        timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);
+        return time_spec_t(ts.tv_sec, ts.tv_nsec, 1e9);
+    }
+    #endif /* HAVE_CLOCK_GETTIME */
+
+
+    #ifdef HAVE_MACH_ABSOLUTE_TIME
+    #include <mach/mach_time.h>
+    time_spec_t time_spec_t::get_system_time(void){
+        mach_timebase_info_data_t info; mach_timebase_info(&info);
+        intmax_t nanosecs = mach_absolute_time()*info.numer/info.denom;
+        return time_spec_t::from_ticks(nanosecs, 1e9);
+    }
+    #endif /* HAVE_MACH_ABSOLUTE_TIME */
+
+
+    #ifdef HAVE_QUERY_PERFORMANCE_COUNTER
+    #include <Windows.h>
+    time_spec_t time_spec_t::get_system_time(void){
+        LARGE_INTEGER counts, freq;
+        QueryPerformanceCounter(&counts);
+        QueryPerformanceFrequency(&freq);
+        return time_spec_t::from_ticks(counts.QuadPart, double(freq.QuadPart));
+    }
+    #endif /* HAVE_QUERY_PERFORMANCE_COUNTER */
+
+
+    #ifdef HAVE_MICROSEC_CLOCK
+    #include <boost/date_time/posix_time/posix_time.hpp>
+    namespace pt = boost::posix_time;
+    time_spec_t time_spec_t::get_system_time(void){
+        pt::ptime time_now = pt::microsec_clock::universal_time();
+        pt::time_duration time_dur = time_now - pt::from_time_t(0);
+        return time_spec_t(
+            time_t(time_dur.total_seconds()),
+            long(time_dur.fractional_seconds()),
+            double(pt::time_duration::ticks_per_second())
+        );
+    }
+    #endif /* HAVE_MICROSEC_CLOCK */
+
+    /***********************************************************************
+     * Time spec constructors
+     **********************************************************************/
+    #define time_spec_init(full, frac) { \
+        const time_t _full = time_t(full); \
+        const double _frac = double(frac); \
+        const int _frac_int = int(_frac); \
+        _full_secs = _full + _frac_int; \
+        _frac_secs = _frac - _frac_int; \
+        if (_frac_secs < 0) {\
+            _full_secs -= 1; \
+            _frac_secs += 1; \
+        } \
+    }
+
+    inline long long fast_llround(const double x){
+        return (long long)(x + 0.5); // assumption of non-negativity
+    }
+
+    time_spec_t::time_spec_t(const time_spec_t & spec){
+        time_spec_init(spec.get_full_secs(), spec.get_frac_secs());
+    }
+
+    time_spec_t::time_spec_t(double secs){
+        time_spec_init(0, secs);
+    }
+
+    time_spec_t::time_spec_t(time_t full_secs, double frac_secs){
+        time_spec_init(full_secs, frac_secs);
+    }
+
+    time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){
+        const double frac_secs = tick_count/tick_rate;
+        time_spec_init(full_secs, frac_secs);
+    }
+
+    time_spec_t time_spec_t::from_ticks(long long ticks, double tick_rate){
+        const long long rate_i = (long long)(tick_rate);
+        const double rate_f = tick_rate - rate_i;
+        const time_t secs_full = time_t(ticks/rate_i);
+        const long long ticks_error = ticks - (secs_full*rate_i);
+        const double ticks_frac = ticks_error - secs_full*rate_f;
+        return time_spec_t(secs_full, ticks_frac/tick_rate);
+    }
+
+    /***********************************************************************
+     * Time spec accessors
+     **********************************************************************/
+    long time_spec_t::get_tick_count(double tick_rate) const{
+        return long(fast_llround(this->get_frac_secs()*tick_rate));
+    }
+
+    long long time_spec_t::to_ticks(double tick_rate) const{
+        const long long rate_i = (long long)(tick_rate);
+        const double rate_f = tick_rate - rate_i;
+        const long long ticks_full = this->get_full_secs()*rate_i;
+        const double ticks_error = this->get_full_secs()*rate_f;
+        const double ticks_frac = this->get_frac_secs()*tick_rate;
+        return ticks_full + fast_llround(ticks_error + ticks_frac);
+    }
+
+    double time_spec_t::get_real_secs(void) const{
+        return this->get_full_secs() + this->get_frac_secs();
+    }
+
+    /***********************************************************************
+     * Time spec math overloads
+     **********************************************************************/
+    time_spec_t &time_spec_t::operator+=(const time_spec_t &rhs){
+        time_spec_init(
+            this->get_full_secs() + rhs.get_full_secs(),
+            this->get_frac_secs() + rhs.get_frac_secs()
+        );
+        return *this;
+    }
+
+    time_spec_t &time_spec_t::operator-=(const time_spec_t &rhs){
+        time_spec_init(
+            this->get_full_secs() - rhs.get_full_secs(),
+            this->get_frac_secs() - rhs.get_frac_secs()
+        );
+        return *this;
+    }
+
+    bool operator==(const time_spec_t &lhs, const time_spec_t &rhs){
+        return
+            lhs.get_full_secs() == rhs.get_full_secs() and
+            lhs.get_frac_secs() == rhs.get_frac_secs()
+        ;
+    }
+
+    bool operator<(const time_spec_t &lhs, const time_spec_t &rhs){
+        return (
+            (lhs.get_full_secs() < rhs.get_full_secs()) or (
+            (lhs.get_full_secs() == rhs.get_full_secs()) and
+            (lhs.get_frac_secs() < rhs.get_frac_secs())
+        ));
+    }
+  }
+}
diff --git a/lib/receiver/time_spec.h b/lib/receiver/time_spec.h
new file mode 100644
index 0000000..cc7f104
--- /dev/null
+++ b/lib/receiver/time_spec.h
@@ -0,0 +1,147 @@
+//
+// Copyright 2010-2012 Ettus Research LLC
+//
+// This program 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 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+#ifndef INCLUDED_TYPES_TIME_SPEC_HPP
+#define INCLUDED_TYPES_TIME_SPEC_HPP
+
+#include <boost/operators.hpp>
+#include <ctime>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * A time_spec_t holds a seconds and a fractional seconds time value.
+     * Depending upon usage, the time_spec_t can represent absolute times,
+     * relative times, or time differences (between absolute times).
+     *
+     * The time_spec_t provides clock-domain independent time storage,
+     * but can convert fractional seconds to/from clock-domain specific units.
+     *
+     * The fractional seconds are stored as double precision floating point.
+     * This gives the fractional seconds enough precision to unambiguously
+     * specify a clock-tick/sample-count up to rates of several petahertz.
+     */
+    class time_spec_t : boost::additive<time_spec_t>, boost::totally_ordered<time_spec_t>{
+    public:
+
+        /*!
+         * Get the system time in time_spec_t format.
+         * Uses the highest precision clock available.
+         * \return the system time as a time_spec_t
+         */
+        static time_spec_t get_system_time(void);
+
+        /*!
+         * Copy constructor
+         */
+        time_spec_t(const time_spec_t & spec);
+
+        /*!
+         * Create a time_spec_t from a real-valued seconds count.
+         * \param secs the real-valued seconds count (default = 0)
+         */
+        time_spec_t(double secs = 0);
+
+        /*!
+         * Create a time_spec_t from whole and fractional seconds.
+         * \param full_secs the whole/integer seconds count
+         * \param frac_secs the fractional seconds count (default = 0)
+         */
+        time_spec_t(time_t full_secs, double frac_secs = 0);
+
+        /*!
+         * Create a time_spec_t from whole seconds and fractional ticks.
+         * Translation from clock-domain specific units.
+         * \param full_secs the whole/integer seconds count
+         * \param tick_count the fractional seconds tick count
+         * \param tick_rate the number of ticks per second
+         */
+        time_spec_t(time_t full_secs, long tick_count, double tick_rate);
+
+        /*!
+         * Create a time_spec_t from a 64-bit tick count.
+         * Translation from clock-domain specific units.
+         * \param ticks an integer count of ticks
+         * \param tick_rate the number of ticks per second
+         */
+        static time_spec_t from_ticks(long long ticks, double tick_rate);
+
+        /*!
+         * Convert the fractional seconds to clock ticks.
+         * Translation into clock-domain specific units.
+         * \param tick_rate the number of ticks per second
+         * \return the fractional seconds tick count
+         */
+        long get_tick_count(double tick_rate) const;
+
+        /*!
+         * Convert the time spec into a 64-bit tick count.
+         * Translation into clock-domain specific units.
+         * \param tick_rate the number of ticks per second
+         * \return an integer number of ticks
+         */
+        long long to_ticks(const double tick_rate) const;
+
+        /*!
+         * Get the time as a real-valued seconds count.
+         * Note: If this time_spec_t represents an absolute time,
+         * the precision of the fractional seconds may be lost.
+         * \return the real-valued seconds
+         */
+        double get_real_secs(void) const;
+
+        /*!
+         * Get the whole/integer part of the time in seconds.
+         * \return the whole/integer seconds
+         */
+        time_t get_full_secs(void) const;
+
+        /*!
+         * Get the fractional part of the time in seconds.
+         * \return the fractional seconds
+         */
+        double get_frac_secs(void) const;
+
+        //! Implement addable interface
+        time_spec_t &operator+=(const time_spec_t &);
+
+        //! Implement subtractable interface
+        time_spec_t &operator-=(const time_spec_t &);
+
+    //private time storage details
+    private: time_t _full_secs; double _frac_secs;
+    };
+
+    //! Implement equality_comparable interface
+    bool operator==(const time_spec_t &, const time_spec_t &);
+
+    //! Implement less_than_comparable interface
+    bool operator<(const time_spec_t &, const time_spec_t &);
+
+    inline time_t time_spec_t::get_full_secs(void) const{
+        return this->_full_secs;
+    }
+
+    inline double time_spec_t::get_frac_secs(void) const{
+        return this->_frac_secs;
+    }
+
+  } //namespace transceiver
+} //namespace gr
+
+#endif /* INCLUDED_TYPES_TIME_SPEC_HPP */