Implementation of TCH/F demapper block. See issue #38
diff --git a/grc/demapping/CMakeLists.txt b/grc/demapping/CMakeLists.txt
index 6d5e673..609815b 100644
--- a/grc/demapping/CMakeLists.txt
+++ b/grc/demapping/CMakeLists.txt
@@ -18,5 +18,6 @@
 # Boston, MA 02110-1301, USA.
 
 install(FILES
-    gsm_universal_ctrl_chans_demapper.xml DESTINATION share/gnuradio/grc/blocks
+    gsm_universal_ctrl_chans_demapper.xml
+    gsm_tch_f_chans_demapper.xml DESTINATION share/gnuradio/grc/blocks
 )
diff --git a/grc/demapping/gsm_tch_f_chans_demapper.xml b/grc/demapping/gsm_tch_f_chans_demapper.xml
new file mode 100644
index 0000000..1aee0c6
--- /dev/null
+++ b/grc/demapping/gsm_tch_f_chans_demapper.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<block>
+  <name>TCH/F chans demapper</name>
+  <key>gsm_tch_f_chans_demapper</key>
+  <import>import grgsm</import>
+  <make>grgsm.tch_f_chans_demapper($timemeslot_nr)</make>
+
+  <param>
+    <name>timemeslot_nr</name>
+    <key>timemeslot_nr</key>
+    <value>2</value>
+    <type>int</type>
+    <hide>part</hide>
+  </param>
+
+  <sink>
+    <name>bursts</name>
+    <type>message</type>
+  </sink>
+  <source>
+    <name>tch_bursts</name>
+    <type>message</type>
+  </source>
+  <source>
+    <name>acch_bursts</name>
+    <type>message</type>
+  </source>
+</block>
diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml
index d5b822b..46f96ff 100644
--- a/grc/gsm_block_tree.xml
+++ b/grc/gsm_block_tree.xml
@@ -22,6 +22,7 @@
     <cat>
       <name>Logical channels demapping</name>
       <block>gsm_universal_ctrl_chans_demapper</block>
+      <block>gsm_tch_f_chans_demapper</block>
     </cat>
     <cat>
       <name>Decryption</name>
diff --git a/include/grgsm/demapping/CMakeLists.txt b/include/grgsm/demapping/CMakeLists.txt
index c451c3e..257f66e 100644
--- a/include/grgsm/demapping/CMakeLists.txt
+++ b/include/grgsm/demapping/CMakeLists.txt
@@ -21,5 +21,6 @@
 # Install public header files
 ########################################################################
 install(FILES
-    universal_ctrl_chans_demapper.h DESTINATION include/grgsm/demapping
+    universal_ctrl_chans_demapper.h 
+    tch_f_chans_demapper.h DESTINATION include/grgsm/demapping
 )
diff --git a/include/grgsm/demapping/tch_f_chans_demapper.h b/include/grgsm/demapping/tch_f_chans_demapper.h
new file mode 100644
index 0000000..1e9a7f0
--- /dev/null
+++ b/include/grgsm/demapping/tch_f_chans_demapper.h
@@ -0,0 +1,58 @@
+/* -*- 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_TCH_F_CHANS_DEMAPPER_H
+#define INCLUDED_GSM_TCH_F_CHANS_DEMAPPER_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API tch_f_chans_demapper : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<tch_f_chans_demapper> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of gsm::tch_f_chans_demapper.
+       *
+       * To avoid accidental use of raw pointers, gsm::tch_f_chans_demapper's
+       * constructor is in a private implementation
+       * class. gsm::tch_f_chans_demapper::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(unsigned int timeslot_nr);
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_TCH_F_CHANS_DEMAPPER_H */
+
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 2c29631..8bb97df 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -33,6 +33,7 @@
     misc_utils/bursts_printer_impl.cc
     misc_utils/extract_system_info_impl.cc
     demapping/universal_ctrl_chans_demapper_impl.cc
+    demapping/tch_f_chans_demapper_impl.cc
     decoding/control_channels_decoder_impl.cc
     decoding/cch.c
     decoding/fire_crc.c
diff --git a/lib/demapping/tch_f_chans_demapper_impl.cc b/lib/demapping/tch_f_chans_demapper_impl.cc
new file mode 100644
index 0000000..a2458f1
--- /dev/null
+++ b/lib/demapping/tch_f_chans_demapper_impl.cc
@@ -0,0 +1,188 @@
+/* -*- 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 "tch_f_chans_demapper_impl.h"
+#include <grgsm/endian.h>
+#include <grgsm/gsmtap.h>
+
+namespace gr {
+  namespace gsm {
+
+    tch_f_chans_demapper::sptr
+    tch_f_chans_demapper::make(unsigned int timeslot_nr)
+    {
+      return gnuradio::get_initial_sptr
+        (new tch_f_chans_demapper_impl(timeslot_nr));
+    }
+
+    /*
+     * The private constructor
+     * 
+     */
+    tch_f_chans_demapper_impl::tch_f_chans_demapper_impl(unsigned int timeslot_nr)
+      : gr::block("tch_f_chans_demapper",
+              gr::io_signature::make(0, 0, 0),
+              gr::io_signature::make(0, 0, 0)),
+       d_timeslot(timeslot_nr)
+              
+    {
+        for(int ii=0; ii<13; ii++)
+        {
+            int startpos = ((int)(ii / 4) * 4);
+            d_starts_fn_mod26[ii] = startpos;
+            d_starts_fn_mod26[ii + 13] = startpos + 13;
+        }
+        
+        d_bursts_stolen = false;
+
+        message_port_register_in(pmt::mp("bursts"));
+        set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_chans_demapper_impl::filter_tch_chans, this, _1));
+        message_port_register_out(pmt::mp("tch_bursts"));
+        message_port_register_out(pmt::mp("acch_bursts"));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    tch_f_chans_demapper_impl::~tch_f_chans_demapper_impl()
+    {
+    }
+    
+    void tch_f_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
+    {
+        pmt::pmt_t header_plus_burst = pmt::cdr(msg);
+        gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
+
+        uint32_t frame_nr = be32toh(header->frame_number);
+        uint32_t fn_mod26 = frame_nr % 26;
+        uint32_t fn26_start = d_starts_fn_mod26[fn_mod26];
+        uint32_t fn26_stop = fn26_start + 3;
+        int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
+
+        if(header->timeslot == d_timeslot){
+            header->sub_type = GSMTAP_CHANNEL_TCH_F;
+            
+            if (fn_mod26 == 12 || fn_mod26 == 25)
+            {
+                header->sub_type = GSMTAP_CHANNEL_ACCH|GSMTAP_CHANNEL_TCH_F;
+                
+                // position of SACCH burst based on timeslot
+                // see specification gsm 05.02
+                uint32_t index;
+                bool is_sacch = false;
+                
+                if (d_timeslot % 2 == 0 && fn_mod26 == 12)
+                {
+                    index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
+                    is_sacch = true;
+                }
+                else if (d_timeslot % 2 == 1 && fn_mod26 == 25) 
+                {
+                    index = (((frame_nr - 25) / 26) - ((d_timeslot - 1) / 2)) % 4;
+                    is_sacch = true;
+                }
+                
+                if (is_sacch)
+                {
+                    d_bursts_sacch[index] = msg;
+                    d_frame_numbers_sacch[index] = frame_nr;
+                    
+                    if (index == 3) 
+                    {
+                        //check for a situation where some bursts were lost
+                        //in this situation frame numbers won't be consecutive
+                        bool frames_are_consecutive = true;
+                        for(int jj=1; jj<4; jj++)
+                        {
+                            if((d_frame_numbers_sacch[jj]-d_frame_numbers_sacch[jj-1]) != 26)
+                            {
+                                frames_are_consecutive = false;
+                            }
+                        }
+                        if(frames_are_consecutive)
+                        {
+                            //send bursts to the output
+                            for(int jj=0; jj<4; jj++)
+                            {
+                                message_port_pub(pmt::mp("acch_bursts"), d_bursts_sacch[jj]);
+                            }
+                        }
+                    }
+                }
+            }
+            else
+            {
+                if(fn_mod26>=fn26_start && fn_mod26<=fn26_stop)
+                {
+                    uint32_t ii = fn_mod26 - fn26_start;
+                    d_frame_numbers[ii] = frame_nr;
+                    d_bursts[ii] = msg;
+                    
+                    // check if stealing bits are set
+                    // see gsm 05.03 
+                    if (static_cast<int>(burst_bits[60]) == 1 && static_cast<int>(burst_bits[87]) == 1)
+                    {
+                        d_bursts_stolen = true;
+                    }
+                }
+                
+                if(fn_mod26==fn26_stop)
+                {
+                    //check for a situation where some bursts were lost
+                    //in this situation frame numbers won't be consecutive
+                    bool frames_are_consecutive = true;
+                    
+                    for(int jj=1; jj<4; jj++)
+                    {
+                        if((d_frame_numbers[jj]-d_frame_numbers[jj-1])!=1)
+                        {
+                            frames_are_consecutive = false;
+                        }
+                    }
+                    if(frames_are_consecutive)
+                    {
+                        //send bursts to the output
+                        for(int jj=0; jj<4; jj++)
+                        {
+                            if (d_bursts_stolen)
+                            {
+                                message_port_pub(pmt::mp("acch_bursts"), d_bursts[jj]);
+                            }
+                            else
+                            {
+                                message_port_pub(pmt::mp("tch_bursts"), d_bursts[jj]);
+                            }
+                        }
+                        d_bursts_stolen = false;
+                    } 
+                }
+            }
+        }
+    }
+  } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/demapping/tch_f_chans_demapper_impl.h b/lib/demapping/tch_f_chans_demapper_impl.h
new file mode 100644
index 0000000..8abbc91
--- /dev/null
+++ b/lib/demapping/tch_f_chans_demapper_impl.h
@@ -0,0 +1,52 @@
+/* -*- 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_UNIVERSAL_TCH_CHANS_DEMAPPER_IMPL_H
+#define INCLUDED_GSM_UNIVERSAL_TCH_CHANS_DEMAPPER_IMPL_H
+
+#include <grgsm/demapping/tch_f_chans_demapper.h>
+
+namespace gr {
+  namespace gsm {
+
+    class tch_f_chans_demapper_impl : public tch_f_chans_demapper
+    {
+     private:
+      unsigned int d_starts_fn_mod26[26];
+      unsigned int d_timeslot;
+      uint32_t d_frame_numbers[4];
+      uint32_t d_frame_numbers_sacch[4];
+      pmt::pmt_t d_bursts[4];
+      bool d_bursts_stolen;
+      pmt::pmt_t d_bursts_sacch[4];
+     public:
+      tch_f_chans_demapper_impl(unsigned int timeslot_nr);
+      ~tch_f_chans_demapper_impl();
+
+      void filter_tch_chans(pmt::pmt_t msg);
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_UNIVERSAL_TCH_CHANS_DEMAPPER_IMPL_H */
+
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index a9f5091..4944c68 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -13,6 +13,7 @@
 #include "grgsm/decoding/control_channels_decoder.h"
 #include "grgsm/decryption/decryption.h"
 #include "grgsm/demapping/universal_ctrl_chans_demapper.h"
+#include "grgsm/demapping/tch_f_chans_demapper.h"
 #include "grgsm/misc_utils/bursts_printer.h"
 #include "grgsm/misc_utils/controlled_const_source_f.h"
 #include "grgsm/misc_utils/controlled_rotator_cc.h"
@@ -36,6 +37,8 @@
 
 %include "grgsm/demapping/universal_ctrl_chans_demapper.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, universal_ctrl_chans_demapper);
+%include "grgsm/demapping/tch_f_chans_demapper.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, tch_f_chans_demapper);
 
 %include "grgsm/misc_utils/bursts_printer.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, bursts_printer);