blob: 76d1eaa073faba2cff7cfed2744207a2c39133d9 [file] [log] [blame]
Roman Khassrafcd5a48b2015-05-28 19:23:55 +02001/* -*- 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
33namespace 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 {
ptrkrysikb85c0622015-06-01 14:21:20 +020061 if (d_finished == false){
62 d_finished = true;
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020063 }
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020064 }
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 Khassraff2879042015-07-19 22:04:46 +020077 d_thread->interrupt();
78 d_thread->join();
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020079 return block::stop();
80 }
81
82 bool burst_source_impl::finished()
83 {
84 return d_finished;
85 }
86
87 void burst_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 }
ptrkrysikb85c0622015-06-01 14:21:20 +020096
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020097 std::string s(unserialized, PMT_SIZE);
98 pmt::pmt_t burst = pmt::deserialize_str(s);
99 message_port_pub(pmt::mp("out"), burst);
100 }
Piotr Krysik963f8b22015-05-31 11:51:09 +0200101 d_input_file.close();
Roman Khassraff2879042015-07-19 22:04:46 +0200102 post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1)));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +0200103 }
104 } /* namespace gsm */
105} /* namespace gr */
106