Merge pull request #60 from romankh/demapper-wrap

Preconfigured demapper blocks 
diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml
index e99e33e..87bae67 100644
--- a/grc/gsm_block_tree.xml
+++ b/grc/gsm_block_tree.xml
@@ -39,6 +39,8 @@
     <cat>
       <name>Utilities</name>
       <block>gsm_bursts_printer</block>
+      <block>gsm_burst_sink</block>
+      <block>gsm_burst_source</block>
       <block>gsm_extract_system_info</block>
       <block>gsm_controlled_rotator_cc</block>
       <block>gsm_controlled_const_source_f</block>
diff --git a/grc/misc_utils/CMakeLists.txt b/grc/misc_utils/CMakeLists.txt
index 9f64828..aa7517c 100644
--- a/grc/misc_utils/CMakeLists.txt
+++ b/grc/misc_utils/CMakeLists.txt
@@ -24,5 +24,7 @@
     gsm_message_printer.xml
     gsm_bursts_printer.xml
     gsm_clock_offset_corrector.xml 
-    gsm_tmsi_dumper.xml DESTINATION share/gnuradio/grc/blocks
+    gsm_tmsi_dumper.xml 
+    gsm_burst_sink.xml
+    gsm_burst_source.xml DESTINATION share/gnuradio/grc/blocks
 )
diff --git a/grc/misc_utils/gsm_burst_sink.xml b/grc/misc_utils/gsm_burst_sink.xml
new file mode 100644
index 0000000..a0e7532
--- /dev/null
+++ b/grc/misc_utils/gsm_burst_sink.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+<block>
+  <name>Burst sink</name>
+  <key>gsm_burst_sink</key>
+  <import>import grgsm</import>
+  <make>grgsm.burst_sink($filename)</make>
+
+  <param>
+    <name>Destination file</name>
+    <key>filename</key>
+    <value>/tmp/bursts</value>
+    <type>file_open</type>
+  </param>
+
+  <sink>
+    <name>in</name>
+    <type>message</type>
+  </sink>
+</block>
diff --git a/grc/misc_utils/gsm_burst_source.xml b/grc/misc_utils/gsm_burst_source.xml
new file mode 100644
index 0000000..9e78069
--- /dev/null
+++ b/grc/misc_utils/gsm_burst_source.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+<block>
+  <name>Burst source</name>
+  <key>gsm_burst_source</key>
+  <import>import grgsm</import>
+  <make>grgsm.burst_source($filename)</make>
+
+  <param>
+    <name>Source file</name>
+    <key>filename</key>
+    <value>/tmp/bursts</value>
+    <type>file_open</type>
+  </param>
+
+  <source>
+    <name>out</name>
+    <type>message</type>
+  </source>
+</block>
diff --git a/include/grgsm/misc_utils/CMakeLists.txt b/include/grgsm/misc_utils/CMakeLists.txt
index eb75bb3..3d3383a 100644
--- a/include/grgsm/misc_utils/CMakeLists.txt
+++ b/include/grgsm/misc_utils/CMakeLists.txt
@@ -22,6 +22,8 @@
 ########################################################################
 install(FILES
     bursts_printer.h
+    burst_sink.h
+    burst_source.h
     extract_system_info.h
     controlled_rotator_cc.h
     controlled_const_source_f.h
diff --git a/include/grgsm/misc_utils/burst_sink.h b/include/grgsm/misc_utils/burst_sink.h
new file mode 100644
index 0000000..6903df9
--- /dev/null
+++ b/include/grgsm/misc_utils/burst_sink.h
@@ -0,0 +1,56 @@
+/* -*- 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_BURST_SINK_H
+#define INCLUDED_GSM_BURST_SINK_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API burst_sink : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<burst_sink> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of grgsm::burst_sink.
+       *
+       * To avoid accidental use of raw pointers, grgsm::burst_sink's
+       * constructor is in a private implementation
+       * class. grgsm::burst_sink::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(const std::string &filename);
+    };
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_BURST_SINK_H */
+
diff --git a/include/grgsm/misc_utils/burst_source.h b/include/grgsm/misc_utils/burst_source.h
new file mode 100644
index 0000000..bd8644f
--- /dev/null
+++ b/include/grgsm/misc_utils/burst_source.h
@@ -0,0 +1,57 @@
+/* -*- 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_BURST_SOURCE_H
+#define INCLUDED_GSM_BURST_SOURCE_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+
+namespace gr {
+  namespace gsm {
+
+    /*!
+     * \brief <+description of block+>
+     * \ingroup gsm
+     *
+     */
+    class GSM_API burst_source : virtual public gr::block
+    {
+     public:
+      typedef boost::shared_ptr<burst_source> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of grgsm::burst_source.
+       *
+       * To avoid accidental use of raw pointers, grgsm::burst_source's
+       * constructor is in a private implementation
+       * class. grgsm::burst_source::make is the public interface for
+       * creating new instances.
+       */
+      static sptr make(const std::string &filename);
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_BURST_SOURCE_H */
+
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 028660f..6dc30a3 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -46,6 +46,8 @@
     misc_utils/controlled_const_source_f_impl.cc
     misc_utils/message_printer_impl.cc
     misc_utils/tmsi_dumper_impl.cc
+    misc_utils/burst_sink_impl.cc
+    misc_utils/burst_source_impl.cc
     decryption/decryption_impl.cc
     )
 
diff --git a/lib/misc_utils/burst_sink_impl.cc b/lib/misc_utils/burst_sink_impl.cc
new file mode 100644
index 0000000..fdb7d98
--- /dev/null
+++ b/lib/misc_utils/burst_sink_impl.cc
@@ -0,0 +1,73 @@
+/* -*- 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 "burst_sink_impl.h"
+#include "stdio.h"
+
+namespace gr {
+  namespace gsm {
+
+    burst_sink::sptr
+    burst_sink::make(const std::string &filename)
+    {
+      return gnuradio::get_initial_sptr
+        (new burst_sink_impl(filename));
+    }
+
+    /*
+     * The private constructor
+     */
+    burst_sink_impl::burst_sink_impl(const std::string &filename)
+      : gr::block("burst_sink",
+              gr::io_signature::make(0, 0, 0),
+              gr::io_signature::make(0, 0, 0)),
+              d_output_file(filename.c_str(), std::ofstream::binary)
+    {
+        message_port_register_in(pmt::mp("in"));
+        set_msg_handler(pmt::mp("in"), boost::bind(&burst_sink_impl::process_burst, this, _1));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    burst_sink_impl::~burst_sink_impl()
+    {
+        if (d_output_file.is_open())
+        {
+            d_output_file.close();
+        }
+    }
+
+    void burst_sink_impl::process_burst(pmt::pmt_t msg)
+    {
+        std::string s = pmt::serialize_str(msg);
+        const char *serialized = s.data();
+        d_output_file.write(serialized, s.length());
+    }
+  } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/misc_utils/burst_sink_impl.h b/lib/misc_utils/burst_sink_impl.h
new file mode 100644
index 0000000..f981826
--- /dev/null
+++ b/lib/misc_utils/burst_sink_impl.h
@@ -0,0 +1,46 @@
+/* -*- 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_BURST_SINK_IMPL_H
+#define INCLUDED_GSM_BURST_SINK_IMPL_H
+
+#include <grgsm/misc_utils/burst_sink.h>
+#include <fstream>
+
+namespace gr {
+  namespace gsm {
+
+    class burst_sink_impl : public burst_sink
+    {
+     private:
+        std::ofstream d_output_file;
+     public:
+      burst_sink_impl(const std::string &filename);
+      ~burst_sink_impl();
+      void process_burst(pmt::pmt_t msg);
+    };
+
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_BURST_SINK_IMPL_H */
+
diff --git a/lib/misc_utils/burst_source_impl.cc b/lib/misc_utils/burst_source_impl.cc
new file mode 100644
index 0000000..bd22885
--- /dev/null
+++ b/lib/misc_utils/burst_source_impl.cc
@@ -0,0 +1,108 @@
+/* -*- 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 "burst_source_impl.h"
+#include "stdio.h"
+
+#define PMT_SIZE 174
+
+namespace gr {
+  namespace gsm {
+
+    burst_source::sptr
+    burst_source::make(const std::string &filename)
+    {
+      return gnuradio::get_initial_sptr
+        (new burst_source_impl(filename));
+    }
+
+    /*
+     * The private constructor
+     */
+    burst_source_impl::burst_source_impl(const std::string &filename)
+      : gr::block("burst_source",
+              gr::io_signature::make(0, 0, 0),
+              gr::io_signature::make(0, 0, 0)),
+              d_input_file(filename.c_str(), std::ifstream::binary),
+              d_finished(false)
+    {
+        message_port_register_out(pmt::mp("out"));
+    }
+
+    /*
+     * Our virtual destructor.
+     */
+    burst_source_impl::~burst_source_impl()
+    {
+        if (d_input_file.is_open())
+        {
+            d_input_file.close();
+        }
+        d_finished = true;
+        d_thread->interrupt();
+        d_thread->join();
+    }
+
+    bool burst_source_impl::start()
+    {
+        d_finished = false;
+        d_thread = boost::shared_ptr<gr::thread::thread>
+            (new gr::thread::thread(boost::bind(&burst_source_impl::run, this)));
+        return block::start();
+    }
+
+    bool burst_source_impl::stop()
+    {
+        d_finished = true;
+        d_thread->interrupt();
+        d_thread->join();
+        return block::stop();
+    }
+
+    bool burst_source_impl::finished()
+    {
+        return d_finished;
+    }
+
+    void burst_source_impl::run()
+    {
+        char *unserialized = (char*)malloc(sizeof(char) * PMT_SIZE);
+        while (d_input_file.read(unserialized, PMT_SIZE) && !d_finished)
+        {
+            if (d_input_file.bad())
+            {
+                break;
+            }
+            std::string s(unserialized, PMT_SIZE);
+            pmt::pmt_t burst = pmt::deserialize_str(s);
+            message_port_pub(pmt::mp("out"), burst);
+        }
+        d_finished = true;
+    }
+  } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/misc_utils/burst_source_impl.h b/lib/misc_utils/burst_source_impl.h
new file mode 100644
index 0000000..85217b0
--- /dev/null
+++ b/lib/misc_utils/burst_source_impl.h
@@ -0,0 +1,50 @@
+/* -*- 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_BURST_SOURCE_IMPL_H
+#define INCLUDED_GSM_BURST_SOURCE_IMPL_H
+
+#include <grgsm/misc_utils/burst_source.h>
+#include <fstream>
+
+namespace gr {
+  namespace gsm {
+
+    class burst_source_impl : public burst_source
+    {
+     private:
+        boost::shared_ptr<gr::thread::thread> d_thread;
+        std::ifstream d_input_file;
+        bool d_finished;
+        void run();
+     public:
+        burst_source_impl(const std::string &filename);
+        ~burst_source_impl();
+        bool start();
+        bool stop();
+        bool finished();
+    };
+  } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_BURST_SOURCE_IMPL_H */
+
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index 93a7481..8c54e18 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -21,6 +21,8 @@
 #include "grgsm/misc_utils/extract_system_info.h"
 #include "grgsm/misc_utils/message_printer.h"
 #include "grgsm/misc_utils/tmsi_dumper.h"
+#include "grgsm/misc_utils/burst_sink.h"
+#include "grgsm/misc_utils/burst_source.h"
 %}
 
 
@@ -45,6 +47,10 @@
 
 %include "grgsm/misc_utils/bursts_printer.h"
 GR_SWIG_BLOCK_MAGIC2(gsm, bursts_printer);
+%include "grgsm/misc_utils/burst_sink.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, burst_sink);
+%include "grgsm/misc_utils/burst_source.h"
+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/controlled_rotator_cc.h"