Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +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 | cd5a48b | 2015-05-28 19:23:55 +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> |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 28 | #include "burst_file_source_impl.h" |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 29 | #include "stdio.h" |
| 30 | |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 31 | namespace gr { |
| 32 | namespace gsm { |
| 33 | |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 34 | burst_file_source::sptr |
| 35 | burst_file_source::make(const std::string &filename) |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 36 | { |
| 37 | return gnuradio::get_initial_sptr |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 38 | (new burst_file_source_impl(filename)); |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * The private constructor |
| 43 | */ |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 44 | burst_file_source_impl::burst_file_source_impl(const std::string &filename) |
| 45 | : gr::block("burst_file_source", |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 46 | gr::io_signature::make(0, 0, 0), |
| 47 | gr::io_signature::make(0, 0, 0)), |
| 48 | d_input_file(filename.c_str(), std::ifstream::binary), |
| 49 | d_finished(false) |
| 50 | { |
| 51 | message_port_register_out(pmt::mp("out")); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Our virtual destructor. |
| 56 | */ |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 57 | burst_file_source_impl::~burst_file_source_impl() |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 58 | { |
ptrkrysik | b85c062 | 2015-06-01 14:21:20 +0200 | [diff] [blame] | 59 | if (d_finished == false){ |
| 60 | d_finished = true; |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 61 | } |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 64 | bool burst_file_source_impl::start() |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 65 | { |
| 66 | d_finished = false; |
| 67 | d_thread = boost::shared_ptr<gr::thread::thread> |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 68 | (new gr::thread::thread(boost::bind(&burst_file_source_impl::run, this))); |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 69 | return block::start(); |
| 70 | } |
| 71 | |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 72 | bool burst_file_source_impl::stop() |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 73 | { |
| 74 | d_finished = true; |
Roman Khassraf | f287904 | 2015-07-19 22:04:46 +0200 | [diff] [blame] | 75 | d_thread->interrupt(); |
| 76 | d_thread->join(); |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 77 | return block::stop(); |
| 78 | } |
| 79 | |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 80 | bool burst_file_source_impl::finished() |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 81 | { |
| 82 | return d_finished; |
| 83 | } |
| 84 | |
Roman Khassraf | 1c3419e | 2015-08-03 10:58:57 +0200 | [diff] [blame] | 85 | void burst_file_source_impl::run() |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 86 | { |
Vasil Velichkov | 492f48c | 2018-09-11 14:15:53 +0300 | [diff] [blame] | 87 | std::filebuf* pbuf = d_input_file.rdbuf(); |
| 88 | while (!d_finished) |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 89 | { |
Vasil Velichkov | 492f48c | 2018-09-11 14:15:53 +0300 | [diff] [blame] | 90 | pmt::pmt_t burst = pmt::deserialize(*pbuf); |
| 91 | if (pmt::is_eof_object(burst)) { |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 92 | break; |
| 93 | } |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 94 | message_port_pub(pmt::mp("out"), burst); |
| 95 | } |
Piotr Krysik | 963f8b2 | 2015-05-31 11:51:09 +0200 | [diff] [blame] | 96 | d_input_file.close(); |
Roman Khassraf | f287904 | 2015-07-19 22:04:46 +0200 | [diff] [blame] | 97 | post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 98 | } |
| 99 | } /* namespace gsm */ |
| 100 | } /* namespace gr */ |
| 101 | |