blob: 43671509c96f403f31968d189ab08994e5f8d4f4 [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
31#define PMT_SIZE 174
32
33namespace gr {
34 namespace gsm {
35
Roman Khassraf1c3419e2015-08-03 10:58:57 +020036 burst_file_source::sptr
37 burst_file_source::make(const std::string &filename)
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020038 {
39 return gnuradio::get_initial_sptr
Roman Khassraf1c3419e2015-08-03 10:58:57 +020040 (new burst_file_source_impl(filename));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020041 }
42
43 /*
44 * The private constructor
45 */
Roman Khassraf1c3419e2015-08-03 10:58:57 +020046 burst_file_source_impl::burst_file_source_impl(const std::string &filename)
47 : gr::block("burst_file_source",
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020048 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 */
Roman Khassraf1c3419e2015-08-03 10:58:57 +020059 burst_file_source_impl::~burst_file_source_impl()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020060 {
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
Roman Khassraf1c3419e2015-08-03 10:58:57 +020066 bool burst_file_source_impl::start()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020067 {
68 d_finished = false;
69 d_thread = boost::shared_ptr<gr::thread::thread>
Roman Khassraf1c3419e2015-08-03 10:58:57 +020070 (new gr::thread::thread(boost::bind(&burst_file_source_impl::run, this)));
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020071 return block::start();
72 }
73
Roman Khassraf1c3419e2015-08-03 10:58:57 +020074 bool burst_file_source_impl::stop()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020075 {
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
Roman Khassraf1c3419e2015-08-03 10:58:57 +020082 bool burst_file_source_impl::finished()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020083 {
84 return d_finished;
85 }
86
Roman Khassraf1c3419e2015-08-03 10:58:57 +020087 void burst_file_source_impl::run()
Roman Khassrafcd5a48b2015-05-28 19:23:55 +020088 {
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