blob: 85d5ad9df7d903dc609d7ee2b90b46d33b41dc4c [file] [log] [blame]
Roman Khassrafcd5a48b2015-05-28 19:23:55 +02001/* -*- c++ -*- */
2/* @file
Piotr Krysika6268a52017-08-23 16:02:19 +02003 * @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com>
Roman Khassrafcd5a48b2015-05-28 19:23:55 +02004 * @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 Khassraf1c3419e2015-08-03 10:58:57 +020028#include "burst_file_source_impl.h"
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020029#include "stdio.h"
30
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020031namespace gr {
32 namespace gsm {
33
Roman Khassraf1c3419e2015-08-03 10:58:57 +020034 burst_file_source::sptr
35 burst_file_source::make(const std::string &filename)
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020036 {
37 return gnuradio::get_initial_sptr
Roman Khassraf1c3419e2015-08-03 10:58:57 +020038 (new burst_file_source_impl(filename));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020039 }
40
41 /*
42 * The private constructor
43 */
Roman Khassraf1c3419e2015-08-03 10:58:57 +020044 burst_file_source_impl::burst_file_source_impl(const std::string &filename)
45 : gr::block("burst_file_source",
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020046 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 Khassraf1c3419e2015-08-03 10:58:57 +020057 burst_file_source_impl::~burst_file_source_impl()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020058 {
ptrkrysikb85c0622015-06-01 14:21:20 +020059 if (d_finished == false){
60 d_finished = true;
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020061 }
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020062 }
63
Roman Khassraf1c3419e2015-08-03 10:58:57 +020064 bool burst_file_source_impl::start()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020065 {
66 d_finished = false;
67 d_thread = boost::shared_ptr<gr::thread::thread>
Roman Khassraf1c3419e2015-08-03 10:58:57 +020068 (new gr::thread::thread(boost::bind(&burst_file_source_impl::run, this)));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020069 return block::start();
70 }
71
Roman Khassraf1c3419e2015-08-03 10:58:57 +020072 bool burst_file_source_impl::stop()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020073 {
74 d_finished = true;
Roman Khassraff2879042015-07-19 22:04:46 +020075 d_thread->interrupt();
76 d_thread->join();
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020077 return block::stop();
78 }
79
Roman Khassraf1c3419e2015-08-03 10:58:57 +020080 bool burst_file_source_impl::finished()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020081 {
82 return d_finished;
83 }
84
Roman Khassraf1c3419e2015-08-03 10:58:57 +020085 void burst_file_source_impl::run()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020086 {
Vasil Velichkov89585b32018-09-11 14:15:53 +030087 std::filebuf* pbuf = d_input_file.rdbuf();
88 while (!d_finished)
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020089 {
Vasil Velichkov89585b32018-09-11 14:15:53 +030090 pmt::pmt_t burst = pmt::deserialize(*pbuf);
91 if (pmt::is_eof_object(burst)) {
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020092 break;
93 }
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020094 message_port_pub(pmt::mp("out"), burst);
95 }
Piotr Krysik963f8b22015-05-31 11:51:09 +020096 d_input_file.close();
Roman Khassraff2879042015-07-19 22:04:46 +020097 post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1)));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020098 }
99 } /* namespace gsm */
100} /* namespace gr */
101