Roman Khassraf | c323454 | 2015-08-03 16:17:46 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 3 | * @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com> |
Roman Khassraf | c323454 | 2015-08-03 16:17:46 +0200 | [diff] [blame] | 4 | * @section LICENSE |
| 5 | * |
| 6 | * Gr-gsm is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * Gr-gsm is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with gr-gsm; see the file COPYING. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/io_signature.h> |
| 28 | #include "message_file_source_impl.h" |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #define PMT_SIZE 49 |
| 32 | |
| 33 | namespace gr { |
| 34 | namespace gsm { |
| 35 | |
| 36 | message_file_source::sptr |
| 37 | message_file_source::make(const std::string &filename) |
| 38 | { |
| 39 | return gnuradio::get_initial_sptr |
| 40 | (new message_file_source_impl(filename)); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * The private constructor |
| 45 | */ |
| 46 | message_file_source_impl::message_file_source_impl(const std::string &filename) |
| 47 | : gr::block("message_file_source", |
| 48 | gr::io_signature::make(0, 0, 0), |
| 49 | gr::io_signature::make(0, 0, 0)), |
| 50 | d_input_file(filename.c_str(), std::ifstream::binary), |
| 51 | d_finished(false) |
| 52 | { |
| 53 | message_port_register_out(pmt::mp("out")); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Our virtual destructor. |
| 58 | */ |
| 59 | message_file_source_impl::~message_file_source_impl() |
| 60 | { |
| 61 | if (d_finished == false){ |
| 62 | d_finished = true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | bool message_file_source_impl::start() |
| 67 | { |
| 68 | d_finished = false; |
| 69 | d_thread = boost::shared_ptr<gr::thread::thread> |
| 70 | (new gr::thread::thread(boost::bind(&message_file_source_impl::run, this))); |
| 71 | return block::start(); |
| 72 | } |
| 73 | |
| 74 | bool message_file_source_impl::stop() |
| 75 | { |
| 76 | d_finished = true; |
| 77 | d_thread->interrupt(); |
| 78 | d_thread->join(); |
| 79 | return block::stop(); |
| 80 | } |
| 81 | |
| 82 | bool message_file_source_impl::finished() |
| 83 | { |
| 84 | return d_finished; |
| 85 | } |
| 86 | |
| 87 | void message_file_source_impl::run() |
| 88 | { |
| 89 | char *unserialized = (char*)malloc(sizeof(char) * PMT_SIZE); |
| 90 | while (d_input_file.read(unserialized, PMT_SIZE) && !d_finished) |
| 91 | { |
| 92 | if (d_input_file.bad()) |
| 93 | { |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | std::string s(unserialized, PMT_SIZE); |
| 98 | pmt::pmt_t burst = pmt::deserialize_str(s); |
| 99 | message_port_pub(pmt::mp("out"), burst); |
| 100 | } |
| 101 | d_input_file.close(); |
| 102 | post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); |
| 103 | } |
| 104 | } /* namespace gsm */ |
| 105 | } /* namespace gr */ |
| 106 | |