Merge pull request #82 from rpp0/rpp0-hopping-checks

Added input validation to CX channel hopper block
diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml
index 3952fff..94d1d82 100644
--- a/grc/gsm_block_tree.xml
+++ b/grc/gsm_block_tree.xml
@@ -43,6 +43,7 @@
       <block>gsm_burst_sink</block>
       <block>gsm_burst_source</block>
       <block>gsm_extract_system_info</block>
+      <block>gsm_extract_immediate_assignment</block>
       <block>gsm_controlled_rotator_cc</block>
       <block>gsm_controlled_const_source_f</block>
       <block>gsm_message_printer</block>
diff --git a/grc/misc_utils/CMakeLists.txt b/grc/misc_utils/CMakeLists.txt
index aa7517c..d545692 100644
--- a/grc/misc_utils/CMakeLists.txt
+++ b/grc/misc_utils/CMakeLists.txt
@@ -19,6 +19,7 @@
 
 install(FILES
     gsm_extract_system_info.xml
+    gsm_extract_immediate_assignment.xml
     gsm_controlled_rotator_cc.xml
     gsm_controlled_const_source_f.xml
     gsm_message_printer.xml
diff --git a/grc/misc_utils/gsm_bursts_printer.xml b/grc/misc_utils/gsm_bursts_printer.xml
index 6407571..abdb532 100644
--- a/grc/misc_utils/gsm_bursts_printer.xml
+++ b/grc/misc_utils/gsm_bursts_printer.xml
@@ -4,7 +4,7 @@
   <key>gsm_bursts_printer</key>
   <import>import grgsm</import>
   <import>import pmt</import>
-  <make>grgsm.bursts_printer(pmt.intern($prepend_string), $prepend_fnr)</make>
+  <make>grgsm.bursts_printer(pmt.intern($prepend_string), $prepend_fnr, $prepend_frame_count, $print_payload_only)</make>
 
   <param>
     <name>Prepend String</name>
@@ -27,9 +27,49 @@
       <key>True</key>
     </option>
   </param>
+  <param>
+    <name>Prepend Frame Count</name>
+    <key>prepend_frame_count</key>
+    <value>False</value>
+    <type>bool</type>
+    <option>
+      <name>False</name>
+      <key>False</key>
+    </option>
+    <option>
+      <name>True</name>
+      <key>True</key>
+    </option>
+  </param>
+  <param>
+    <name>Print payload only</name>
+    <key>print_payload_only</key>
+    <value>False</value>
+    <type>bool</type>
+    <option>
+      <name>False</name>
+      <key>False</key>
+    </option>
+    <option>
+      <name>True</name>
+      <key>True</key>
+    </option>
+  </param>
 
   <sink>
     <name>bursts</name>
     <type>message</type>
   </sink>
+
+  <doc>
+This block prints bursts to output. By default the whole burst including tail bits, stealing bits and training sequence is printed.
+
+If "Prepend Frame Number" is enabled, then the framenumber is prepended to each burst.
+
+If "Prepend Frame Count" is enabled, then the frame count for A5 is prependend to each burst.
+
+If "Print payload only" is enabled, then only the two data blocks of a burst are printed, tail bits, stealing bits and training sequence are omitted.
+
+Enabling all three options results in an output that is similar to the output of airprobe, i.e. the format is "frame_nr frame_count: databits"
+  </doc>
 </block>
diff --git a/grc/misc_utils/gsm_extract_immediate_assignment.xml b/grc/misc_utils/gsm_extract_immediate_assignment.xml
new file mode 100644
index 0000000..efb62da
--- /dev/null
+++ b/grc/misc_utils/gsm_extract_immediate_assignment.xml
@@ -0,0 +1,71 @@
+<?xml version="1.0"?>
+<block>
+  <name>Extract immediate assignment</name>
+  <key>gsm_extract_immediate_assignment</key>
+  <import>import grgsm</import>
+  <make>grgsm.extract_immediate_assignment($print_immediate_assignments, $ignore_gprs, $unique_references)</make>
+
+  <param>
+    <name>Print</name>
+    <key>print_immediate_assignments</key>
+    <value>False</value>
+    <type>bool</type>
+    <option>
+      <name>False</name>
+      <key>False</key>
+    </option>
+    <option>
+      <name>True</name>
+      <key>True</key>
+    </option>
+  </param>
+  <param>
+    <name>Ignore GPRS</name>
+    <key>ignore_gprs</key>
+    <value>False</value>
+    <type>bool</type>
+    <option>
+      <name>False</name>
+      <key>False</key>
+    </option>
+    <option>
+      <name>True</name>
+      <key>True</key>
+    </option>
+  </param>
+  <param>
+    <name>Unique per request ref</name>
+    <key>unique_references</key>
+    <value>False</value>
+    <type>bool</type>
+    <option>
+      <name>False</name>
+      <key>False</key>
+    </option>
+    <option>
+      <name>True</name>
+      <key>True</key>
+    </option>
+  </param>
+
+  <sink>
+    <name>msgs</name>
+    <type>message</type>
+  </sink>
+
+  <doc>
+Extracts the assignment information from immediate assignments, i.e. timeslot, channel type, etc.
+
+Input: decoded control channel messages
+
+If "Print" is enabled, the information will be printed to output also,
+else the data can be retrieved using functions:
+
+get_frame_numbers(), get_channel_types(), get_timeslots(), get_subchannels(), get_hopping(), get_maios(), get_hsns(), get_arfcns(), get_timing_advances(), get_mobile_allocations()
+
+If "Ignore GPRS" is enabled, the extractor will ignore Immediate Assignments that assign GPRS channels.
+
+If "Unique per request ref" is enabled, the extractor will capture the first Immediate Assignment for every unique request reference, and ignore further duplicate assignments
+
+  </doc>
+</block>
diff --git a/include/grgsm/misc_utils/CMakeLists.txt b/include/grgsm/misc_utils/CMakeLists.txt
index 3d3383a..3d63d46 100644
--- a/include/grgsm/misc_utils/CMakeLists.txt
+++ b/include/grgsm/misc_utils/CMakeLists.txt
@@ -25,6 +25,7 @@
     burst_sink.h
     burst_source.h
     extract_system_info.h
+    extract_immediate_assignment.h
     controlled_rotator_cc.h
     controlled_const_source_f.h
     message_printer.h 
diff --git a/include/grgsm/misc_utils/bursts_printer.h b/include/grgsm/misc_utils/bursts_printer.h
index b2dd1d1..099bf08 100644
--- a/include/grgsm/misc_utils/bursts_printer.h
+++ b/include/grgsm/misc_utils/bursts_printer.h
@@ -50,7 +50,8 @@
        * class. gsm::bursts_printer::make is the public interface for
        * creating new instances.
        */
-      static sptr make(pmt::pmt_t prepend_string, bool prepend_fnr=false);
+      static sptr make(pmt::pmt_t prepend_string, bool prepend_fnr=false,
+        bool prepend_frame_count=false, bool print_payload_only=false);
     };
 
   } // namespace gsm
diff --git a/include/grgsm/misc_utils/extract_immediate_assignment.h b/include/grgsm/misc_utils/extract_immediate_assignment.h
new file mode 100644
index 0000000..ac8a630
--- /dev/null
+++ b/include/grgsm/misc_utils/extract_immediate_assignment.h
@@ -0,0 +1,66 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Roman Khassraf <rkhassraf@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_EXTRACT_IMMEDIATE_ASSIGNMENT_H
+#define INCLUDED_GSM_EXTRACT_IMMEDIATE_ASSIGNMENT_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+#include <vector>
+
+namespace gr {
+  namespace gsm {
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API extract_immediate_assignment : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<extract_immediate_assignment> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of gsm::extract_immediate_assignment.
+       *
+       * To avoid accidental use of raw pointers, gsm::extract_immediate_assignment's
+       * constructor is in a private implementation
+       * class. gsm::extract_immediate_assignment::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(bool print_immediate_assignments=false, bool ignore_gprs=false, bool unique_references=false);
+      virtual std::vector<int> get_frame_numbers() = 0;
+      virtual std::vector<std::string> get_channel_types() = 0;
+      virtual std::vector<int> get_timeslots() = 0;
+      virtual std::vector<int> get_subchannels() = 0;
+      virtual std::vector<int> get_hopping() = 0;
+      virtual std::vector<int> get_maios() = 0;
+      virtual std::vector<int> get_hsns() = 0;
+      virtual std::vector<int> get_arfcns() = 0;
+      virtual std::vector<int> get_timing_advances() = 0;
+      virtual std::vector<std::string> get_mobile_allocations() = 0;
+    };
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_EXTRACT_IMMEDIATE_ASSIGNMENT_H */
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index ee4ef47..f2d70e3 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -33,6 +33,7 @@
     receiver/cx_channel_hopper_impl.cc
     misc_utils/bursts_printer_impl.cc
     misc_utils/extract_system_info_impl.cc
+    misc_utils/extract_immediate_assignment_impl.cc
     demapping/universal_ctrl_chans_demapper_impl.cc
     demapping/tch_f_chans_demapper_impl.cc
     decoding/control_channels_decoder_impl.cc
diff --git a/lib/decoding/tch_f_decoder_impl.cc b/lib/decoding/tch_f_decoder_impl.cc
index 3ec7205..a3b09fb 100644
--- a/lib/decoding/tch_f_decoder_impl.cc
+++ b/lib/decoding/tch_f_decoder_impl.cc
@@ -68,7 +68,7 @@
         {
             throw std::runtime_error("TCH/F Decoder: can't open file\n");
         }
-        
+
         const unsigned char amr_nb_magic[6] = { 0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a };
 
         if (d_tch_mode != TCH_FS)
@@ -163,6 +163,56 @@
                     pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
 
                     message_port_pub(pmt::mp("msgs"), msg_out);
+
+                    // if we are in an AMR-mode and we receive a channel mode modify message,
+                    // we set the mode according to the multirate configuration from the message
+                    // see GSM 04.18, section 9.1.5 and 10.5.2.21aa
+                    if (d_tch_mode  != TCH_FS && d_tch_mode != TCH_EFR)
+                    {
+                        if (outmsg[3] == 0x06 && outmsg[4] == 0x10)
+                        {
+                            // Verify that multirate version 1 is set
+                            if ((outmsg[11] >> 5) == 1)
+                            {
+                                // the set of active codecs, max 4 modes
+                                // active_codec_set[0] corresponds to CODEC_MODE_1 with lowest bit rate
+                                // active_codec_set[3] corresponds to CODEC_MODE_4 with highest bit rate
+                                tch_mode active_codec_set[4];
+                                uint8_t mode_count = 0;
+                                for (i = 0; i<8; i++)
+                                {
+                                    if (((outmsg[12] >> i) & 0x1) == 1 && mode_count < 4)
+                                    {
+                                        active_codec_set[mode_count++] = static_cast<tch_mode>(7-i);
+                                    }
+                                }
+
+                                // check Initial Codec Mode Indicator ICMI
+                                // if ICMI == 1, then use the one defined in start mode field
+                                // else use implicit rule defined in GSM 05.09, section 3.4.3
+                                if (((outmsg[11] >> 3) & 0x1) == 1)
+                                {
+                                    // from start field
+                                    setCodingMode(active_codec_set[ (outmsg[11] & 0x3) ]);
+                                }
+                                else
+                                {
+                                    // implicit mode
+                                    // if the set contains only 1 codec, we use that one
+                                    // else if there are 2 or 3 codecs in the set, we use the one with lowest bitrate
+                                    if (mode_count >= 1 && mode_count <= 3)
+                                    {
+                                        setCodingMode(active_codec_set[0]);
+                                    }
+                                    // if there are 4 codecs in the set, we use the second lowest bitrate
+                                    else if (mode_count == 4)
+                                    {
+                                        setCodingMode(active_codec_set[1]);
+                                    }
+                                }
+                            }
+                        }
+                    }
                 }
             }
 
@@ -302,8 +352,9 @@
 
     void tch_f_decoder_impl::setCodingMode(tch_mode mode)
     {
-        if (d_tch_mode  != TCH_FS && d_tch_mode != TCH_EFR)
+        if (mode  != TCH_FS && d_tch_mode != TCH_EFR)
         {
+            d_tch_mode = mode;
             mKd = GSM::gAMRKd[d_tch_mode];
             mTCHD.resize(mKd);
             mTCHU.resize(mKd+6);
diff --git a/lib/misc_utils/bursts_printer_impl.cc b/lib/misc_utils/bursts_printer_impl.cc
index a22e85f..3b4f026 100644
--- a/lib/misc_utils/bursts_printer_impl.cc
+++ b/lib/misc_utils/bursts_printer_impl.cc
@@ -44,39 +44,77 @@
         gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst);
         int8_t * burst = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
         size_t burst_len=pmt::blob_length(header_plus_burst)-sizeof(gsmtap_hdr);
-        uint32_t frame_nr;
+        uint32_t frame_nr = be32toh(header->frame_number);
 
         std::cout << d_prepend_string;
         if (d_prepend_fnr)
         {
-            frame_nr = be32toh(header->frame_number);
-            std::cout << frame_nr << ":";
+            std::cout << frame_nr;
         }
 
-        for(int ii=0; ii<burst_len; ii++)
+        if (d_prepend_fnr && d_prepend_frame_count)
         {
-          std::cout << std::setprecision(1) << static_cast<int>(burst[ii]) << "";
+            std::cout << " ";
         }
+
+        if (d_prepend_frame_count)
+        {
+            // calculate frame count for A5
+            uint16_t t1 = frame_nr/1326;
+            uint8_t t2 = frame_nr % 26;
+            uint8_t t3 = frame_nr % 51;
+            uint32_t frame_count = (t1<<11)|(t3<<5)|t2;
+            std::cout << frame_count;
+        }
+
+        if (d_prepend_fnr || d_prepend_frame_count)
+        {
+            std::cout << ": ";
+        }
+
+        if (d_print_payload_only)
+        {
+            for (int ii=0; ii<57; ii++)
+            {
+                std::cout << std::setprecision(1) << static_cast<int>(burst[ii + 3]);
+            }
+            for (int ii=0; ii<57; ii++)
+            {
+                std::cout << std::setprecision(1) << static_cast<int>(burst[ii + 88]);
+            }
+        }
+        else
+        {
+            for(int ii=0; ii<burst_len; ii++)
+            {
+              std::cout << std::setprecision(1) << static_cast<int>(burst[ii]);
+            }
+        }
+
         std::cout << std::endl;
     }
 
     bursts_printer::sptr
-    bursts_printer::make(pmt::pmt_t prepend_string, bool prepend_fnr)
+    bursts_printer::make(pmt::pmt_t prepend_string, bool prepend_fnr,
+        bool prepend_frame_count, bool print_payload_only)
     {
       return gnuradio::get_initial_sptr
-        (new bursts_printer_impl(prepend_string, prepend_fnr));
+        (new bursts_printer_impl(prepend_string, prepend_fnr, prepend_frame_count, print_payload_only));
     }
 
     /*
      * The private constructor
      */
-    bursts_printer_impl::bursts_printer_impl(pmt::pmt_t prepend_string, bool prepend_fnr)
+    bursts_printer_impl::bursts_printer_impl(pmt::pmt_t prepend_string, bool prepend_fnr,
+        bool prepend_frame_count, bool print_payload_only)
       : gr::block("bursts_printer",
               gr::io_signature::make(0, 0, 0),
               gr::io_signature::make(0, 0, 0))
     {
         d_prepend_string = prepend_string;
         d_prepend_fnr = prepend_fnr;
+        d_prepend_frame_count = prepend_frame_count;
+        d_print_payload_only = print_payload_only;
         message_port_register_in(pmt::mp("bursts"));
         set_msg_handler(pmt::mp("bursts"), boost::bind(&bursts_printer_impl::bursts_print, this, _1));
     }
diff --git a/lib/misc_utils/bursts_printer_impl.h b/lib/misc_utils/bursts_printer_impl.h
index 5655920..5a8a7ed 100644
--- a/lib/misc_utils/bursts_printer_impl.h
+++ b/lib/misc_utils/bursts_printer_impl.h
@@ -35,8 +35,11 @@
       void bursts_print(pmt::pmt_t burst);
       pmt::pmt_t d_prepend_string;
       bool d_prepend_fnr;
+      bool d_prepend_frame_count;
+      bool d_print_payload_only;
      public:
-      bursts_printer_impl(pmt::pmt_t prepend_string, bool prepend_fnr=false);
+      bursts_printer_impl(pmt::pmt_t prepend_string, bool prepend_fnr=false,
+        bool prepend_frame_count=false, bool print_payload_only=false);
       ~bursts_printer_impl();
     };
 
diff --git a/lib/misc_utils/extract_immediate_assignment_impl.cc b/lib/misc_utils/extract_immediate_assignment_impl.cc
new file mode 100644
index 0000000..0ce5fd1
--- /dev/null
+++ b/lib/misc_utils/extract_immediate_assignment_impl.cc
@@ -0,0 +1,346 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Roman Khassraf <rkhassraf@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 <unistd.h>
+#include <map>
+#include <endian.h>
+#include <boost/foreach.hpp>
+
+#include "extract_immediate_assignment_impl.h"
+
+namespace gr {
+  namespace gsm {
+    boost::mutex extract_immediate_assignment_mutex;
+
+    void extract_immediate_assignment_impl::process_message(pmt::pmt_t msg){
+        pmt::pmt_t message_plus_header_blob = pmt::cdr(msg);
+        uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob);
+        gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header;
+        uint8_t * msg_elements = (uint8_t *)(message_plus_header+sizeof(gsmtap_hdr));
+        uint32_t frame_nr = be32toh(header->frame_number);
+
+        if(msg_elements[2]==0x3f)
+        {
+            immediate_assignment current;
+            current.frame_nr = frame_nr;
+
+            /*
+                channel description, see table 10.23 in GSM 04.08
+
+                msg_elements[4], octet 2 in specs
+
+                5 bits channel type
+                    ignored in TBF
+                    00001   TCH/F
+                    0001T   TCH/H, subchannel/TDMA offset T
+                    001TT   SDCCH/4, subchannel/TDMA offset TT
+                    01TTT   SDCCH/8, subchannel/TDMA offset TTT
+                3 bits timeslot number TN
+            */
+            current.timeslot = (msg_elements[4] & 7);
+
+            uint8_t channeltype = (msg_elements[4] >> 3);
+            uint8_t mode = msg_elements[3] & (1 << 4);
+            if (mode == 0)
+            {
+                if (channeltype >= 8)
+                {
+                    current.channel_type = "SDCCH/8";
+                    current.subchannel = (channeltype & 7);
+                }
+                else if (channeltype >= 4 && channeltype <= 7)
+                {
+                    current.channel_type = "SDCCH/4";
+                    current.subchannel = (channeltype & 3);
+                }
+                else if (channeltype >= 2 && channeltype <= 3)
+                {
+                    current.channel_type = "TCH/H";
+                    current.subchannel = (channeltype & 1);
+                }
+                else
+                {
+                    current.channel_type = "TCH/F";
+                }
+            }
+            else
+            {
+                // We return if ignore_gprs is set true
+                if (d_ignore_gprs)
+                {
+                    return;
+                }
+                current.channel_type = "GPRS - Temporary Block Flow TBF";
+            }
+
+            /*
+                msg_elements[5], msg_elements[6] are octets 3 and 4 in specs
+
+                    3 bits training sequence (we dont process this for the moment)
+                    1 bit hopping channel H
+
+                    if H = 0:
+                        2 bit spare
+                        2 bit high part of single channel arfcn
+
+                        8 bit low part of single channel arfcn
+
+                    if H = 1:
+                        4 bit high part of MAIO
+
+                        2 bit low part of MAIO
+                        6bit HSN
+            */
+            current.hopping = (msg_elements[5] >> 4) & 1;
+            if (current.hopping)
+            {
+                uint8_t maio = (msg_elements[5] & 0xf) << 2;
+                maio |= (msg_elements[6] >> 6);
+                current.maio = maio;
+                current.hsn = (msg_elements[6] & 0x3f);
+            }
+            else
+            {
+                uint16_t arfcn = (msg_elements[5] & 3) << 8;
+                arfcn |= msg_elements[6];
+                current.arfcn = arfcn;
+            }
+
+            /*
+                msg_elements[7 - 9], octets 5 - 7 in specs, see 10.5.2.30 request reference, maybe later
+            */
+            uint8_t random_access_info = msg_elements[7];
+            uint8_t rr_t1 = (msg_elements[8] >> 3);
+            uint8_t rr_t2 = (msg_elements[9] & 0x1F);
+            uint8_t rr_t3 = (msg_elements[8] & 0x7) << 3;
+            rr_t3 |= (msg_elements[9] >> 5);
+            uint32_t request_fnr = 51*((rr_t3-rr_t2) % 26) + rr_t3 + (51*26*rr_t1);
+
+            // we will use random_access_info and request_fnr together as request_reference in the map,
+            // if unique_references is set true
+            uint32_t request_ref = (random_access_info << 0x16);
+            request_ref |= request_fnr;
+
+            /*
+                msg_elements[10]:   timing advance
+            */
+            current.timing_advance = msg_elements[10];
+
+            /*
+                msg_elements[11 - 20]:   mobile allocation, flexible length, see 10.5.2.21
+            */
+            uint8_t mobile_allocation_len = msg_elements[11];
+            if (mobile_allocation_len > 0)
+            {
+                std::string ma;
+                for (int i=0; i<mobile_allocation_len; i++)
+                {
+                    for (int j=0; j<8; j++)
+                    {
+                        ma.push_back('0' + ((msg_elements[12 + i] >> (7-j)) & 0x1));
+                    }
+                }
+                current.mobile_allocation = ma;
+            }
+
+            bool is_duplicate = false;
+            if (d_unique_references)
+            {
+                if (d_assignment_map.find(request_ref) != d_assignment_map.end())
+                {
+                    is_duplicate = true;
+                }
+                else
+                {
+                    d_assignment_map[request_ref] = current;
+                }
+            }
+            else
+            {
+                d_assignment_map[current.frame_nr] = current;
+            }
+
+            if (d_print_immediate_assignments && !is_duplicate)
+            {
+                std::cout << "\n------------------------------------------------\n" << std::endl;
+                std::cout << "FrameNr: " << (unsigned)current.frame_nr << std::endl;
+                std::cout << "Channel type: " << current.channel_type << std::endl;
+                std::cout << "Timeslot: " << (unsigned)current.timeslot << std::endl;
+                // Dont print subchannel if mode == 1 or if the assigned channel is TCH/F
+                if (mode == 0 && channeltype >= 2)
+                {
+                    std::cout << "Subchannel: " << (unsigned)current.subchannel << std::endl;
+                }
+                std::cout << "Hopping: " << (unsigned)current.hopping << std::endl;
+                if (current.hopping)
+                {
+                    std::cout << "MAIO: " << (unsigned)current.maio << std::endl;
+                    std::cout << "HSN: " << (unsigned)current.hsn << std::endl;
+                    std::cout << "Mobile Allocation: " << current.mobile_allocation << std::endl;
+                }
+                else
+                {
+                    std::cout << "ARFCN: " << (unsigned)current.arfcn << std::endl;
+                }
+                std::cout << "Timing Advance: " << (unsigned)current.timing_advance << std::endl;
+            }
+        }
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_frame_numbers()
+    {
+        std::vector<int> fnrs;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            fnrs.push_back(i.second.frame_nr);
+        }
+        return fnrs;
+    }
+
+    std::vector<std::string> extract_immediate_assignment_impl::get_channel_types()
+    {
+        std::vector<std::string> types;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            types.push_back(i.second.channel_type);
+        }
+        return types;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_timeslots()
+    {
+        std::vector<int> timeslots;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            timeslots.push_back(i.second.timeslot);
+        }
+        return timeslots;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_subchannels()
+    {
+        std::vector<int> subchannels;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            subchannels.push_back(i.second.subchannel);
+        }
+        return subchannels;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_hopping()
+    {
+        std::vector<int> hopping;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            hopping.push_back(i.second.hopping);
+        }
+        return hopping;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_maios()
+    {
+        std::vector<int> maios;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            maios.push_back(i.second.maio);
+        }
+        return maios;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_hsns()
+    {
+        std::vector<int> hsns;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            hsns.push_back(i.second.hsn);
+        }
+        return hsns;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_arfcns()
+    {
+        std::vector<int> arfcns;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            arfcns.push_back(i.second.arfcn);
+        }
+        return arfcns;
+    }
+
+    std::vector<int> extract_immediate_assignment_impl::get_timing_advances()
+    {
+        std::vector<int> tas;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            tas.push_back(i.second.timing_advance);
+        }
+        return tas;
+    }
+
+    std::vector<std::string> extract_immediate_assignment_impl::get_mobile_allocations()
+    {
+        std::vector<std::string> mobile_allocations;
+        BOOST_FOREACH(immediate_assignment_map::value_type &i, d_assignment_map)
+        {
+            mobile_allocations.push_back(i.second.mobile_allocation);
+        }
+        return mobile_allocations;
+    }
+
+    extract_immediate_assignment::sptr
+    extract_immediate_assignment::make(bool print_immediate_assignments, bool ignore_gprs, bool unique_references)
+    {
+      return gnuradio::get_initial_sptr
+        (new extract_immediate_assignment_impl(print_immediate_assignments, ignore_gprs, unique_references));
+    }
+
+    /*
+     * The private constructor
+     */
+    extract_immediate_assignment_impl::extract_immediate_assignment_impl(bool print_immediate_assignments,
+        bool ignore_gprs, bool unique_references)
+      : gr::block("extract_immediate_assignment",
+              gr::io_signature::make(0, 0, 0),
+              gr::io_signature::make(0, 0, 0))
+    {
+        d_print_immediate_assignments = print_immediate_assignments;
+        d_ignore_gprs = ignore_gprs;
+        d_unique_references = unique_references;
+        message_port_register_in(pmt::mp("msgs"));
+        set_msg_handler(pmt::mp("msgs"), boost::bind(&extract_immediate_assignment_impl::process_message, this, _1));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    extract_immediate_assignment_impl::~extract_immediate_assignment_impl()
+    {
+    }
+  } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/misc_utils/extract_immediate_assignment_impl.h b/lib/misc_utils/extract_immediate_assignment_impl.h
new file mode 100644
index 0000000..cf007e2
--- /dev/null
+++ b/lib/misc_utils/extract_immediate_assignment_impl.h
@@ -0,0 +1,80 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Roman Khassraf <rkhassraf@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_EXTRACT_IMMEDIATE_ASSIGNMENT_IMPL_H
+#define INCLUDED_GSM_EXTRACT_IMMEDIATE_ASSIGNMENT_IMPL_H
+
+#include <grgsm/misc_utils/extract_immediate_assignment.h>
+#include <map>
+#include <vector>
+
+namespace gr {
+  namespace gsm {
+
+    class immediate_assignment
+    {
+        public:
+            uint32_t frame_nr;
+            std::string channel_type;
+            uint8_t timeslot;
+            uint8_t subchannel;
+            uint8_t hopping;
+            uint8_t maio;
+            uint8_t hsn;
+            uint16_t arfcn;
+            uint8_t timing_advance;
+            std::string mobile_allocation;
+
+            immediate_assignment() : frame_nr(0), channel_type("unknown"), timeslot(0), subchannel(0),
+                hopping(false), maio(0), hsn(0), arfcn(0), timing_advance(0), mobile_allocation("") {};
+            ~immediate_assignment() {};
+    };
+
+    typedef std::map<uint32_t, immediate_assignment> immediate_assignment_map;
+
+    class extract_immediate_assignment_impl : public extract_immediate_assignment
+    {
+        private:
+            void process_message(pmt::pmt_t msg);
+            immediate_assignment_map d_assignment_map;
+            bool d_print_immediate_assignments;
+            bool d_ignore_gprs;
+            bool d_unique_references;
+        public:
+            virtual std::vector<int> get_frame_numbers();
+            virtual std::vector<std::string> get_channel_types();
+            virtual std::vector<int> get_timeslots();
+            virtual std::vector<int> get_subchannels();
+            virtual std::vector<int> get_hopping();
+            virtual std::vector<int> get_maios();
+            virtual std::vector<int> get_hsns();
+            virtual std::vector<int> get_arfcns();
+            virtual std::vector<int> get_timing_advances();
+            virtual std::vector<std::string> get_mobile_allocations();
+            extract_immediate_assignment_impl(bool print_immediate_assignments=false,
+                bool ignore_gprs=false, bool unique_references=false);
+            ~extract_immediate_assignment_impl();
+    };
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_EXTRACT_IMMEDIATE_ASSIGNMENT_IMPL_H */
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index f1f37bf..79831f8 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -20,6 +20,7 @@
 #include "grgsm/misc_utils/controlled_const_source_f.h"
 #include "grgsm/misc_utils/controlled_rotator_cc.h"
 #include "grgsm/misc_utils/extract_system_info.h"
+#include "grgsm/misc_utils/extract_immediate_assignment.h"
 #include "grgsm/misc_utils/message_printer.h"
 #include "grgsm/misc_utils/tmsi_dumper.h"
 #include "grgsm/misc_utils/burst_sink.h"
@@ -55,6 +56,8 @@
 GR_SWIG_BLOCK_MAGIC2(gsm, burst_source);
 %include "grgsm/misc_utils/extract_system_info.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, extract_system_info);
+%include "grgsm/misc_utils/extract_immediate_assignment.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, extract_immediate_assignment);
 %include "grgsm/misc_utils/controlled_rotator_cc.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, controlled_rotator_cc);
 %include "grgsm/misc_utils/controlled_const_source_f.h"