1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "message_strobe_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <cstdio>
35 #include <iostream>
36 #include <stdexcept>
37 
38 namespace gr {
39 namespace blocks {
40 
make(pmt::pmt_t msg,long period_ms)41 message_strobe::sptr message_strobe::make(pmt::pmt_t msg, long period_ms)
42 {
43     return gnuradio::get_initial_sptr(new message_strobe_impl(msg, period_ms));
44 }
45 
message_strobe_impl(pmt::pmt_t msg,long period_ms)46 message_strobe_impl::message_strobe_impl(pmt::pmt_t msg, long period_ms)
47     : block("message_strobe", io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)),
48       d_finished(false),
49       d_period_ms(period_ms),
50       d_msg(msg),
51       d_port(pmt::mp("strobe"))
52 {
53     message_port_register_out(d_port);
54 
55     message_port_register_in(pmt::mp("set_msg"));
56     set_msg_handler(pmt::mp("set_msg"), [this](pmt::pmt_t msg) { this->set_msg(msg); });
57 }
58 
~message_strobe_impl()59 message_strobe_impl::~message_strobe_impl() {}
60 
start()61 bool message_strobe_impl::start()
62 {
63     // NOTE: d_finished should be something explicitly thread safe. But since
64     // nothing breaks on concurrent access, I'll just leave it as bool.
65     d_finished = false;
66     d_thread = boost::shared_ptr<gr::thread::thread>(
67         new gr::thread::thread(boost::bind(&message_strobe_impl::run, this)));
68 
69     return block::start();
70 }
71 
stop()72 bool message_strobe_impl::stop()
73 {
74     // Shut down the thread
75     d_finished = true;
76     d_thread->interrupt();
77     d_thread->join();
78 
79     return block::stop();
80 }
81 
run()82 void message_strobe_impl::run()
83 {
84     while (!d_finished) {
85         boost::this_thread::sleep(
86             boost::posix_time::milliseconds(static_cast<long>(d_period_ms)));
87         if (d_finished) {
88             return;
89         }
90 
91         message_port_pub(d_port, d_msg);
92     }
93 }
94 
95 } /* namespace blocks */
96 } /* namespace gr */
97