From c323454a3fbd8d617022ee309613f21c8b18d797 Mon Sep 17 00:00:00 2001 From: Roman Khassraf Date: Mon, 3 Aug 2015 16:17:46 +0200 Subject: Implemented message file source and sink. Issue #101 --- grc/gsm_block_tree.xml | 2 + grc/misc_utils/CMakeLists.txt | 2 + grc/misc_utils/gsm_message_file_sink.xml | 23 ++++++ grc/misc_utils/gsm_message_file_source.xml | 23 ++++++ include/grgsm/misc_utils/CMakeLists.txt | 2 + include/grgsm/misc_utils/message_file_sink.h | 56 +++++++++++++ include/grgsm/misc_utils/message_file_source.h | 57 +++++++++++++ lib/CMakeLists.txt | 2 + lib/misc_utils/message_file_sink_impl.cc | 73 +++++++++++++++++ lib/misc_utils/message_file_sink_impl.h | 46 +++++++++++ lib/misc_utils/message_file_source_impl.cc | 106 +++++++++++++++++++++++++ lib/misc_utils/message_file_source_impl.h | 50 ++++++++++++ python/qa_message_source_sink.py | 80 +++++++++++++++++++ swig/grgsm_swig.i | 6 ++ 14 files changed, 528 insertions(+) create mode 100644 grc/misc_utils/gsm_message_file_sink.xml create mode 100644 grc/misc_utils/gsm_message_file_source.xml create mode 100644 include/grgsm/misc_utils/message_file_sink.h create mode 100644 include/grgsm/misc_utils/message_file_source.h create mode 100644 lib/misc_utils/message_file_sink_impl.cc create mode 100644 lib/misc_utils/message_file_sink_impl.h create mode 100644 lib/misc_utils/message_file_source_impl.cc create mode 100644 lib/misc_utils/message_file_source_impl.h create mode 100755 python/qa_message_source_sink.py diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml index 2503710..6496fd3 100644 --- a/grc/gsm_block_tree.xml +++ b/grc/gsm_block_tree.xml @@ -42,6 +42,8 @@ gsm_bursts_printer gsm_burst_sink gsm_burst_source + gsm_message_file_sink + gsm_message_file_source gsm_burst_sink_qa gsm_burst_source_qa gsm_extract_system_info diff --git a/grc/misc_utils/CMakeLists.txt b/grc/misc_utils/CMakeLists.txt index 5a3f874..0ace0d8 100644 --- a/grc/misc_utils/CMakeLists.txt +++ b/grc/misc_utils/CMakeLists.txt @@ -26,6 +26,8 @@ install(FILES gsm_bursts_printer.xml gsm_clock_offset_corrector.xml gsm_tmsi_dumper.xml + gsm_message_file_sink.xml + gsm_message_file_source.xml gsm_burst_sink.xml gsm_burst_source.xml gsm_burst_source_qa.xml diff --git a/grc/misc_utils/gsm_message_file_sink.xml b/grc/misc_utils/gsm_message_file_sink.xml new file mode 100644 index 0000000..5d4975d --- /dev/null +++ b/grc/misc_utils/gsm_message_file_sink.xml @@ -0,0 +1,23 @@ + + + Message file sink + gsm_message_file_sink + import grgsm + grgsm.message_file_sink($filename) + + + Destination file + filename + /tmp/output.msg + file_open + + + + in + message + + + +This block stores incoming gsm messages to a file. + + diff --git a/grc/misc_utils/gsm_message_file_source.xml b/grc/misc_utils/gsm_message_file_source.xml new file mode 100644 index 0000000..7095749 --- /dev/null +++ b/grc/misc_utils/gsm_message_file_source.xml @@ -0,0 +1,23 @@ + + + Message file source + gsm_message_file_source + import grgsm + grgsm.message_file_source($filename) + + + Source file + filename + /tmp/output.msg + file_open + + + + out + message + + + +This block outputs gsm messages stored in a file. + + diff --git a/include/grgsm/misc_utils/CMakeLists.txt b/include/grgsm/misc_utils/CMakeLists.txt index 8b74fc7..a7b9d67 100644 --- a/include/grgsm/misc_utils/CMakeLists.txt +++ b/include/grgsm/misc_utils/CMakeLists.txt @@ -24,6 +24,8 @@ install(FILES bursts_printer.h burst_sink.h burst_source.h + message_file_sink.h + message_file_source.h burst_sink_qa.h burst_source_qa.h extract_system_info.h diff --git a/include/grgsm/misc_utils/message_file_sink.h b/include/grgsm/misc_utils/message_file_sink.h new file mode 100644 index 0000000..61a9c53 --- /dev/null +++ b/include/grgsm/misc_utils/message_file_sink.h @@ -0,0 +1,56 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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_MESSAGE_FILE_SINK_H +#define INCLUDED_GSM_MESSAGE_FILE_SINK_H + +#include +#include + +namespace gr { + namespace gsm { + + /*! + * \brief <+description of block+> + * \ingroup gsm + * + */ + class GSM_API message_file_sink : virtual public gr::block + { + public: + typedef boost::shared_ptr sptr; + + /*! + * \brief Return a shared_ptr to a new instance of grgsm::message_file_sink. + * + * To avoid accidental use of raw pointers, grgsm::message_file_sink's + * constructor is in a private implementation + * class. grgsm::message_file_sink::make is the public interface for + * creating new instances. + */ + static sptr make(const std::string &filename); + }; + } // namespace gsm +} // namespace gr + +#endif /* INCLUDED_GSM_MESSAGE_FILE_SINK_H */ + diff --git a/include/grgsm/misc_utils/message_file_source.h b/include/grgsm/misc_utils/message_file_source.h new file mode 100644 index 0000000..51d2525 --- /dev/null +++ b/include/grgsm/misc_utils/message_file_source.h @@ -0,0 +1,57 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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_MESSAGE_FILE_SOURCE_H +#define INCLUDED_GSM_MESSAGE_FILE_SOURCE_H + +#include +#include + +namespace gr { + namespace gsm { + + /*! + * \brief <+description of block+> + * \ingroup gsm + * + */ + class GSM_API message_file_source : virtual public gr::block + { + public: + typedef boost::shared_ptr sptr; + + /*! + * \brief Return a shared_ptr to a new instance of grgsm::message_file_source. + * + * To avoid accidental use of raw pointers, grgsm::message_file_source's + * constructor is in a private implementation + * class. grgsm::message_file_source::make is the public interface for + * creating new instances. + */ + static sptr make(const std::string &filename); + }; + + } // namespace gsm +} // namespace gr + +#endif /* INCLUDED_GSM_MESSAGE_FILE_SOURCE_H */ + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 61f0858..1ddab6d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -54,6 +54,8 @@ list(APPEND grgsm_sources misc_utils/burst_source_impl.cc misc_utils/burst_sink_qa_impl.cc misc_utils/burst_source_qa_impl.cc + misc_utils/message_file_sink_impl.cc + misc_utils/message_file_source_impl.cc decryption/decryption_impl.cc ) add_library(gnuradio-grgsm SHARED ${grgsm_sources}) diff --git a/lib/misc_utils/message_file_sink_impl.cc b/lib/misc_utils/message_file_sink_impl.cc new file mode 100644 index 0000000..7e52993 --- /dev/null +++ b/lib/misc_utils/message_file_sink_impl.cc @@ -0,0 +1,73 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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 +#include "message_file_sink_impl.h" +#include + +namespace gr { + namespace gsm { + + message_file_sink::sptr + message_file_sink::make(const std::string &filename) + { + return gnuradio::get_initial_sptr + (new message_file_sink_impl(filename)); + } + + /* + * The private constructor + */ + message_file_sink_impl::message_file_sink_impl(const std::string &filename) + : gr::block("message_file_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(&message_file_sink_impl::process_message, this, _1)); + } + + /* + * Our virtual destructor. + */ + message_file_sink_impl::~message_file_sink_impl() + { + if (d_output_file.is_open()) + { + d_output_file.close(); + } + } + + void message_file_sink_impl::process_message(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/message_file_sink_impl.h b/lib/misc_utils/message_file_sink_impl.h new file mode 100644 index 0000000..6757e69 --- /dev/null +++ b/lib/misc_utils/message_file_sink_impl.h @@ -0,0 +1,46 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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_MESSAGE_FILE_SINK_IMPL_H +#define INCLUDED_GSM_MESSAGE_FILE_SINK_IMPL_H + +#include +#include + +namespace gr { + namespace gsm { + + class message_file_sink_impl : public message_file_sink + { + private: + std::ofstream d_output_file; + public: + message_file_sink_impl(const std::string &filename); + ~message_file_sink_impl(); + void process_message(pmt::pmt_t msg); + }; + + } // namespace gsm +} // namespace gr + +#endif /* INCLUDED_GSM_MESSAGE_FILE_SINK_IMPL_H */ + diff --git a/lib/misc_utils/message_file_source_impl.cc b/lib/misc_utils/message_file_source_impl.cc new file mode 100644 index 0000000..540fe9e --- /dev/null +++ b/lib/misc_utils/message_file_source_impl.cc @@ -0,0 +1,106 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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 +#include "message_file_source_impl.h" +#include + +#define PMT_SIZE 49 + +namespace gr { + namespace gsm { + + message_file_source::sptr + message_file_source::make(const std::string &filename) + { + return gnuradio::get_initial_sptr + (new message_file_source_impl(filename)); + } + + /* + * The private constructor + */ + message_file_source_impl::message_file_source_impl(const std::string &filename) + : gr::block("message_file_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. + */ + message_file_source_impl::~message_file_source_impl() + { + if (d_finished == false){ + d_finished = true; + } + } + + bool message_file_source_impl::start() + { + d_finished = false; + d_thread = boost::shared_ptr + (new gr::thread::thread(boost::bind(&message_file_source_impl::run, this))); + return block::start(); + } + + bool message_file_source_impl::stop() + { + d_finished = true; + d_thread->interrupt(); + d_thread->join(); + return block::stop(); + } + + bool message_file_source_impl::finished() + { + return d_finished; + } + + void message_file_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_input_file.close(); + post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); + } + } /* namespace gsm */ +} /* namespace gr */ + diff --git a/lib/misc_utils/message_file_source_impl.h b/lib/misc_utils/message_file_source_impl.h new file mode 100644 index 0000000..42f65cb --- /dev/null +++ b/lib/misc_utils/message_file_source_impl.h @@ -0,0 +1,50 @@ +/* -*- c++ -*- */ +/* @file + * @author Roman Khassraf + * @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_MESSAGE_FILE_SOURCE_IMPL_H +#define INCLUDED_GSM_MESSAGE_FILE_SOURCE_IMPL_H + +#include +#include + +namespace gr { + namespace gsm { + + class message_file_source_impl : public message_file_source + { + private: + boost::shared_ptr d_thread; + std::ifstream d_input_file; + bool d_finished; + void run(); + public: + message_file_source_impl(const std::string &filename); + ~message_file_source_impl(); + bool start(); + bool stop(); + bool finished(); + }; + } // namespace gsm +} // namespace gr + +#endif /* INCLUDED_GSM_MESSAGE_FILE_SOURCE_IMPL_H */ + diff --git a/python/qa_message_source_sink.py b/python/qa_message_source_sink.py new file mode 100755 index 0000000..7ed3608 --- /dev/null +++ b/python/qa_message_source_sink.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @file +# @author Roman Khassraf +# @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. +# +# + +from gnuradio import gr, gr_unittest, blocks +import grgsm +import os +import pmt +import sys +import tempfile + +class qa_message_source_sink (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block() + self.tmpfile = tempfile.NamedTemporaryFile() + + def tearDown (self): + self.tmpfile.close() + + #def getOutput(self): + #self.tmpfile.seek(0) + #return self.tmpfile.read() + + #def getOutputExpected(self, expected_lines): + #out = "" + #for l in expected_lines: + #out = out + l + "\n" + #return out + + def test_001_no_prefix_no_header (self): + """ + Four messages, without any prefix, no gsmtap header + """ + msgs_input = [ + "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", + #"02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", + #"02 04 01 00 00 00 cb 00 00 1d 3d 0e 01 00 00 00 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", + #"02 04 01 00 00 00 cb 00 00 1d 3d 12 02 00 00 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" + ] + + # there is a whitespace at the beginning of message_printer output + msgs_expected = [ + "02 04 01 00 00 00 c9 00 00 1d 3c e5 02 00 01 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", + #"02 04 01 00 00 00 ca 00 00 1d 3c e9 02 00 02 00 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b", + #" 59 06 1a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e5 04 00", + #" 15 06 21 00 01 f0 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b" + ] + + src = grgsm.message_source(msgs_input) + file_sink = grgsm.message_file_sink(self.tmpfile.name) + + #printer = grgsm.message_printer(pmt.intern(""), False) + #self.tb.msg_connect(src, "msgs", printer, "msgs") + self.tb.run() + + #self.assertEqual(self.getOutput(), self.getOutputExpected(msgs_expected)) + + +if __name__ == '__main__': + gr_unittest.run(qa_message_source_sink, "qa_message_source_sink.xml") diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i index 5dc1bcd..bf63f08 100644 --- a/swig/grgsm_swig.i +++ b/swig/grgsm_swig.i @@ -27,6 +27,8 @@ #include "grgsm/misc_utils/burst_sink_qa.h" #include "grgsm/misc_utils/burst_source.h" #include "grgsm/misc_utils/burst_source_qa.h" +#include "grgsm/misc_utils/message_file_sink.h" +#include "grgsm/misc_utils/message_file_source.h" %} @@ -72,3 +74,7 @@ GR_SWIG_BLOCK_MAGIC2(gsm, controlled_const_source_f); GR_SWIG_BLOCK_MAGIC2(gsm, message_printer); %include "grgsm/misc_utils/tmsi_dumper.h" GR_SWIG_BLOCK_MAGIC2(gsm, tmsi_dumper); +%include "grgsm/misc_utils/message_file_sink.h" +GR_SWIG_BLOCK_MAGIC2(gsm, message_file_sink); +%include "grgsm/misc_utils/message_file_source.h" +GR_SWIG_BLOCK_MAGIC2(gsm, message_file_source); -- cgit v1.2.3