Merge remote-tracking branch 'origin/romankh-master'
with implementation of AMR decoding

Conflicts:
	lib/decoding/tch_f_decoder_impl.cc
	lib/decoding/tch_f_decoder_impl.h
diff --git a/apps/airprobe_rtlsdr.grc b/apps/airprobe_rtlsdr.grc
index bbec4c0..9cac347 100644
--- a/apps/airprobe_rtlsdr.grc
+++ b/apps/airprobe_rtlsdr.grc
@@ -2079,7 +2079,7 @@
       <value>True</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>0</value>
     </param>
     <param>
@@ -2337,7 +2337,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>1</value>
     </param>
     <param>
@@ -2629,7 +2629,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>7</value>
     </param>
     <param>
@@ -2676,7 +2676,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>6</value>
     </param>
     <param>
@@ -2723,7 +2723,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>5</value>
     </param>
     <param>
@@ -2770,7 +2770,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>4</value>
     </param>
     <param>
@@ -2817,7 +2817,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>3</value>
     </param>
     <param>
@@ -2864,7 +2864,7 @@
       <value>False</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>2</value>
     </param>
     <param>
diff --git a/examples/airprobe_tchf.grc b/examples/airprobe_tchf.grc
index ae3738a..3b0036d 100644
--- a/examples/airprobe_tchf.grc
+++ b/examples/airprobe_tchf.grc
@@ -821,7 +821,7 @@
       <value>0</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>5</value>
     </param>
   </block>
@@ -919,7 +919,7 @@
       <value>[2,6,12,16,22,26,32,36,42,46]</value>
     </param>
     <param>
-      <key>timemeslot_nr</key>
+      <key>timeslot_nr</key>
       <value>0</value>
     </param>
   </block>
diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml
index 87bae67..3952fff 100644
--- a/grc/gsm_block_tree.xml
+++ b/grc/gsm_block_tree.xml
@@ -13,6 +13,7 @@
     <cat>
       <name>Receiver</name>
       <block>gsm_receiver</block>
+      <block>gsm_cx_channel_hopper</block>
       <block>gsm_fcch_burst_tagger</block>
       <block>gsm_sch_detector</block>
       <block>gsm_fcch_detector</block>
diff --git a/grc/receiver/CMakeLists.txt b/grc/receiver/CMakeLists.txt
index 25f08cc..bcef03d 100644
--- a/grc/receiver/CMakeLists.txt
+++ b/grc/receiver/CMakeLists.txt
@@ -23,5 +23,6 @@
     gsm_fcch_burst_tagger.xml
     gsm_sch_detector.xml
     gsm_fcch_detector.xml
+    gsm_cx_channel_hopper.xml
     gsm_clock_offset_control.xml DESTINATION share/gnuradio/grc/blocks
 )
diff --git a/grc/receiver/gsm_cx_channel_hopper.xml b/grc/receiver/gsm_cx_channel_hopper.xml
new file mode 100644
index 0000000..903c1a0
--- /dev/null
+++ b/grc/receiver/gsm_cx_channel_hopper.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0"?>
+<block>
+  <name>CX channel hopper</name>
+  <key>gsm_cx_channel_hopper</key>
+  <import>import grgsm</import>
+  <make>grgsm.cx_channel_hopper($ma, $maio, $hsn)</make>
+  <param>
+    <name>MA</name>
+    <key>ma</key>
+    <value>[]</value>
+    <type>int_vector</type>
+  </param>
+
+  <param>
+    <name>MAIO</name>
+    <key>maio</key>
+    <type>int</type>
+  </param>
+
+  <param>
+    <name>HSN</name>
+    <key>hsn</key>
+    <type>int</type>
+  </param>
+
+  <sink>
+    <name>CX</name>
+    <type>message</type>
+  </sink>
+
+  <source>
+    <name>bursts</name>
+    <type>message</type>
+  </source>
+</block>
diff --git a/include/grgsm/receiver/CMakeLists.txt b/include/grgsm/receiver/CMakeLists.txt
index 99ebf4f..bb4515d 100644
--- a/include/grgsm/receiver/CMakeLists.txt
+++ b/include/grgsm/receiver/CMakeLists.txt
@@ -22,5 +22,6 @@
 ########################################################################
 install(FILES
     clock_offset_control.h
+    cx_channel_hopper.h
     receiver.h DESTINATION include/grgsm/receiver
 )
diff --git a/include/grgsm/receiver/cx_channel_hopper.h b/include/grgsm/receiver/cx_channel_hopper.h
new file mode 100644
index 0000000..c8c2787
--- /dev/null
+++ b/include/grgsm/receiver/cx_channel_hopper.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_CX_CHANNEL_HOPPER_H
+#define INCLUDED_GSM_CX_CHANNEL_HOPPER_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+#include <vector>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API cx_channel_hopper : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<cx_channel_hopper> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of gsm::cx_channel_hopper.
+       *
+       * To avoid accidental use of raw pointers, gsm::cx_channel_hopper's
+       * constructor is in a private implementation
+       * class. gsm::cx_channel_hopper::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(const std::vector<int> &ma, int maio, int hsn);
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_CX_CHANNEL_HOPPER_H */
+
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index da02515..4a88edb 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -30,6 +30,7 @@
     receiver/viterbi_detector.cc
     receiver/sch.c
     receiver/clock_offset_control_impl.cc
+    receiver/cx_channel_hopper_impl.cc
     misc_utils/bursts_printer_impl.cc
     misc_utils/extract_system_info_impl.cc
     demapping/universal_ctrl_chans_demapper_impl.cc
@@ -50,8 +51,7 @@
     misc_utils/tmsi_dumper_impl.cc
     misc_utils/burst_sink_impl.cc
     misc_utils/burst_source_impl.cc
-    decryption/decryption_impl.cc
-    )
+    decryption/decryption_impl.cc )
 
 add_library(gnuradio-gsm SHARED ${grgsm_sources})
 target_link_libraries(gnuradio-gsm ${Boost_LIBRARIES} ${GNURADIO_RUNTIME_LIBRARIES} ${VOLK_LIBRARIES}
diff --git a/lib/receiver/cx_channel_hopper_impl.cc b/lib/receiver/cx_channel_hopper_impl.cc
new file mode 100644
index 0000000..3e23910
--- /dev/null
+++ b/lib/receiver/cx_channel_hopper_impl.cc
@@ -0,0 +1,137 @@
+/* -*- 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 <grgsm/gsmtap.h>
+#include "cx_channel_hopper_impl.h"
+
+namespace gr {
+  namespace gsm {
+
+    cx_channel_hopper::sptr
+    cx_channel_hopper::make(const std::vector<int> &ma, int maio, int hsn)
+    {
+        return gnuradio::get_initial_sptr
+          (new cx_channel_hopper_impl(ma, maio, hsn));
+    }
+
+    /*
+     * The private constructor
+     */
+    cx_channel_hopper_impl::cx_channel_hopper_impl(const std::vector<int> &ma, int maio, int hsn)
+        : gr::block("cx_channel_hopper",
+                gr::io_signature::make(0, 0, 0),
+                gr::io_signature::make(0, 0, 0)),
+        d_ma(ma),
+        d_maio(maio),
+        d_hsn(hsn)
+    {
+        d_narfcn = ma.size();
+
+        message_port_register_in(pmt::mp("CX"));
+        set_msg_handler(pmt::mp("CX"), boost::bind(&cx_channel_hopper_impl::assemble_bursts, this, _1));
+        message_port_register_out(pmt::mp("bursts"));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    cx_channel_hopper_impl::~cx_channel_hopper_impl()
+    {
+    }
+
+    /**
+     * Random number table used for calculating the 
+     * hopping sequence. Defined in GSM 05.02.
+     */
+    unsigned char RNTABLE[114] = {
+        48, 98, 63, 1, 36, 95, 78, 102, 94, 73, \
+        0, 64, 25, 81, 76, 59, 124, 23, 104, 100, \
+        101, 47, 118, 85, 18, 56, 96, 86, 54, 2, \
+        80, 34, 127, 13, 6, 89, 57, 103, 12, 74, \
+        55, 111, 75, 38, 109, 71, 112, 29, 11, 88, \
+        87, 19, 3, 68, 110, 26, 33, 31, 8, 45, \
+        82, 58, 40, 107, 32, 5, 106, 92, 62, 67, \
+        77, 108, 122, 37, 60, 66, 121, 42, 51, 126, \
+        117, 114, 4, 90, 43, 52, 53, 113, 120, 72, \
+        16, 49, 7, 79, 119, 61, 22, 84, 9, 97, \
+        91, 15, 21, 24, 46, 39, 93, 105, 65, 70, \
+        125, 99, 17, 123 \
+    };
+
+    /*
+     * Slow Frequency Hopping (SFH) MAI calculation based
+     * on airprobe-hopping by Bogdan Diaconescu.
+     */
+    int cx_channel_hopper_impl::calculate_ma_sfh(int maio, int hsn, int n, int fn)
+    {
+        int mai = 0;
+        int s = 0;
+        int nbin = floor(log2(n) + 1);
+        int t1 = fn / 1326;
+        int t2 = fn % 26;
+        int t3 = fn % 51;
+
+        if (hsn == 0)
+            mai = (fn + maio) % n;
+        else {
+            int t1r = t1 % 64;
+            int m = t2 + RNTABLE[(hsn ^ t1r) + t3];
+            int mprim = m % (1 << nbin);
+            int tprim = t3 % (1 << nbin);
+
+            if (mprim < n)
+                s = mprim;
+            else
+                s = (mprim + tprim) % n;
+
+            mai = (s + maio) % n;
+        }
+
+        return (mai);
+    }
+
+    /**
+     * Given MA, MAIO, HSN, and FN, decide which frames
+     * to forward to the demapper.
+     */
+    void cx_channel_hopper_impl::assemble_bursts(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);
+        uint16_t frame_ca = be16toh(header->arfcn);
+        int mai = calculate_ma_sfh(d_maio, d_hsn, d_narfcn, frame_nr);
+
+        if(d_ma[mai] == frame_ca) {
+            message_port_pub(pmt::mp("bursts"), msg);
+        }
+    }
+
+  } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/receiver/cx_channel_hopper_impl.h b/lib/receiver/cx_channel_hopper_impl.h
new file mode 100644
index 0000000..7a52b05
--- /dev/null
+++ b/lib/receiver/cx_channel_hopper_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_GSM_CX_CHANNEL_HOPPER_IMPL_H
+#define INCLUDED_GSM_CX_CHANNEL_HOPPER_IMPL_H
+
+#include <grgsm/receiver/cx_channel_hopper.h>
+
+namespace gr {
+  namespace gsm {
+
+    class cx_channel_hopper_impl : public cx_channel_hopper
+    {
+     private:
+      std::vector<int> d_ma; // Mobile Allocation list. Contains all channels that are used while channel hopping
+      int d_maio; // Mobile Allocation Index Offset
+      int d_hsn; // Hopping Sequence Number
+      int d_narfcn; // Length of d_ma
+
+      int calculate_ma_sfh(int maio, int hsn, int n, int fn);
+      void assemble_bursts(pmt::pmt_t msg);
+
+     public:
+      cx_channel_hopper_impl(const std::vector<int> &ma, int maio, int hsn);
+      ~cx_channel_hopper_impl();
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_CX_CHANNEL_HOPPER_IMPL_H */
+
diff --git a/lib/receiver/receiver_impl.cc b/lib/receiver/receiver_impl.cc
index 1833293..97402b2 100644
--- a/lib/receiver/receiver_impl.cc
+++ b/lib/receiver/receiver_impl.cc
@@ -344,14 +344,10 @@
                 break;      //do nothing
             }
             
-            if(input_nr==0)
+            if(input_nr==input_items.size()-1)
             {
                 d_burst_nr++;   //go to next burst
                 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset();  //consume samples of the burst up to next guard period
-            }
-            
-            if(input_nr==input_items.size()-1)
-            {
                 consume_each(to_consume);
             }
             //and add offset which is introduced by
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index 8c54e18..f1f37bf 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -10,6 +10,7 @@
 %{
 #include "grgsm/receiver/receiver.h"
 #include "grgsm/receiver/clock_offset_control.h"
+#include "grgsm/receiver/cx_channel_hopper.h"
 #include "grgsm/decoding/control_channels_decoder.h"
 #include "grgsm/decoding/tch_f_decoder.h"
 #include "grgsm/decryption/decryption.h"
@@ -30,7 +31,8 @@
 GR_SWIG_BLOCK_MAGIC2(gsm, receiver);
 %include "grgsm/receiver/clock_offset_control.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, clock_offset_control);
-
+%include "grgsm/receiver/cx_channel_hopper.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, cx_channel_hopper);
 
 %include "grgsm/decoding/control_channels_decoder.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, control_channels_decoder);
@@ -61,4 +63,3 @@
 GR_SWIG_BLOCK_MAGIC2(gsm, message_printer);
 %include "grgsm/misc_utils/tmsi_dumper.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, tmsi_dumper);
-