Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* @file |
| 3 | * @author Roman Khassraf <rkhassraf@gmail.com> |
| 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 "burst_source_impl.h" |
| 29 | #include "stdio.h" |
| 30 | |
| 31 | #define PMT_SIZE 174 |
| 32 | |
| 33 | namespace gr { |
| 34 | namespace gsm { |
| 35 | |
| 36 | burst_source::sptr |
| 37 | burst_source::make(const std::string &filename) |
| 38 | { |
| 39 | return gnuradio::get_initial_sptr |
| 40 | (new burst_source_impl(filename)); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * The private constructor |
| 45 | */ |
| 46 | burst_source_impl::burst_source_impl(const std::string &filename) |
| 47 | : gr::block("burst_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 | burst_source_impl::~burst_source_impl() |
| 60 | { |
ptrkrysik | b85c062 | 2015-06-01 14:21:20 +0200 | [diff] [blame] | 61 | if (d_finished == false){ |
| 62 | d_finished = true; |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 63 | } |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool burst_source_impl::start() |
| 67 | { |
| 68 | d_finished = false; |
| 69 | d_thread = boost::shared_ptr<gr::thread::thread> |
| 70 | (new gr::thread::thread(boost::bind(&burst_source_impl::run, this))); |
| 71 | return block::start(); |
| 72 | } |
| 73 | |
| 74 | bool burst_source_impl::stop() |
| 75 | { |
| 76 | d_finished = true; |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 77 | return block::stop(); |
| 78 | } |
| 79 | |
| 80 | bool burst_source_impl::finished() |
| 81 | { |
| 82 | return d_finished; |
| 83 | } |
| 84 | |
| 85 | void burst_source_impl::run() |
| 86 | { |
| 87 | char *unserialized = (char*)malloc(sizeof(char) * PMT_SIZE); |
| 88 | while (d_input_file.read(unserialized, PMT_SIZE) && !d_finished) |
| 89 | { |
| 90 | if (d_input_file.bad()) |
| 91 | { |
| 92 | break; |
| 93 | } |
ptrkrysik | b85c062 | 2015-06-01 14:21:20 +0200 | [diff] [blame] | 94 | |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 95 | std::string s(unserialized, PMT_SIZE); |
| 96 | pmt::pmt_t burst = pmt::deserialize_str(s); |
| 97 | message_port_pub(pmt::mp("out"), burst); |
| 98 | } |
Piotr Krysik | 963f8b2 | 2015-05-31 11:51:09 +0200 | [diff] [blame] | 99 | d_input_file.close(); |
ptrkrysik | b85c062 | 2015-06-01 14:21:20 +0200 | [diff] [blame] | 100 | d_thread->interrupt(); |
| 101 | d_thread->join(); |
Roman Khassraf | cd5a48b | 2015-05-28 19:23:55 +0200 | [diff] [blame] | 102 | } |
| 103 | } /* namespace gsm */ |
| 104 | } /* namespace gr */ |
| 105 | |