Roman Khassraf | adea86f | 2015-07-27 15:52:02 +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 "message_source_impl.h" |
Roman Khassraf | 277ed30 | 2015-08-03 09:47:36 +0200 | [diff] [blame] | 29 | #include <stdio.h> |
Roman Khassraf | adea86f | 2015-07-27 15:52:02 +0200 | [diff] [blame] | 30 | #include <grgsm/gsmtap.h> |
| 31 | #include <grgsm/endian.h> |
| 32 | #include <algorithm> |
Roman Khassraf | 277ed30 | 2015-08-03 09:47:36 +0200 | [diff] [blame] | 33 | #include <boost/scoped_ptr.hpp> |
Roman Khassraf | adea86f | 2015-07-27 15:52:02 +0200 | [diff] [blame] | 34 | #include <iostream> |
| 35 | #include <string> |
| 36 | #include <sstream> |
| 37 | |
| 38 | #define MSG_BYTE_LEN 39 |
| 39 | |
| 40 | |
| 41 | namespace gr { |
| 42 | namespace gsm { |
| 43 | |
| 44 | message_source::sptr |
| 45 | message_source::make(const std::vector<std::string> &msg_data) |
| 46 | { |
| 47 | return gnuradio::get_initial_sptr |
| 48 | (new message_source_impl(msg_data)); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * The private constructor |
| 53 | */ |
| 54 | message_source_impl::message_source_impl(const std::vector<std::string> &msg_data) |
| 55 | : gr::block("message_source", |
| 56 | gr::io_signature::make(0, 0, 0), |
| 57 | gr::io_signature::make(0, 0, 0)), |
| 58 | d_finished(false) |
| 59 | { |
| 60 | message_port_register_out(pmt::mp("msgs")); |
| 61 | set_msg_data(msg_data); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Our virtual destructor. |
| 66 | */ |
| 67 | message_source_impl::~message_source_impl() |
| 68 | { |
| 69 | if (d_finished == false){ |
| 70 | d_finished = true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void message_source_impl::set_msg_data(const std::vector<std::string> &msg_data) |
| 75 | { |
| 76 | for (int i=0; i<msg_data.size(); i++) |
| 77 | { |
| 78 | std::istringstream iss(msg_data[i]); |
| 79 | std::vector<uint8_t> bytes; |
| 80 | unsigned int c; |
| 81 | |
| 82 | while (iss >> std::hex >> c) |
| 83 | { |
| 84 | if (c < 256) |
| 85 | { |
| 86 | bytes.push_back(c); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (bytes.size() == MSG_BYTE_LEN) |
| 91 | { |
| 92 | d_msgs.push_back(bytes); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool message_source_impl::start() |
| 98 | { |
| 99 | d_finished = false; |
| 100 | d_thread = boost::shared_ptr<gr::thread::thread> |
| 101 | (new gr::thread::thread(boost::bind(&message_source_impl::run, this))); |
| 102 | return block::start(); |
| 103 | } |
| 104 | |
| 105 | bool message_source_impl::stop() |
| 106 | { |
| 107 | d_finished = true; |
| 108 | d_thread->interrupt(); |
| 109 | d_thread->join(); |
| 110 | return block::stop(); |
| 111 | } |
| 112 | |
| 113 | bool message_source_impl::finished() |
| 114 | { |
| 115 | return d_finished; |
| 116 | } |
| 117 | |
| 118 | void message_source_impl::run() |
| 119 | { |
| 120 | for (int i=0; i<d_msgs.size(); i++) |
| 121 | { |
| 122 | std::vector<uint8_t> current = d_msgs[i]; |
| 123 | pmt::pmt_t blob_header_plus_burst = pmt::make_blob(¤t[0], current.size()); |
| 124 | pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst); |
| 125 | message_port_pub(pmt::mp("msgs"), msg); |
| 126 | } |
| 127 | post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); |
| 128 | } |
| 129 | } /* namespace gsm */ |
| 130 | } /* namespace gr */ |
| 131 | |