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 "tag_debug_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <iomanip>
30 #include <iostream>
31 
32 namespace gr {
33 namespace blocks {
34 
make(size_t sizeof_stream_item,const std::string & name,const std::string & key_filter)35 tag_debug::sptr tag_debug::make(size_t sizeof_stream_item,
36                                 const std::string& name,
37                                 const std::string& key_filter)
38 {
39     return gnuradio::get_initial_sptr(
40         new tag_debug_impl(sizeof_stream_item, name, key_filter));
41 }
42 
tag_debug_impl(size_t sizeof_stream_item,const std::string & name,const std::string & key_filter)43 tag_debug_impl::tag_debug_impl(size_t sizeof_stream_item,
44                                const std::string& name,
45                                const std::string& key_filter)
46     : sync_block("tag_debug",
47                  io_signature::make(1, -1, sizeof_stream_item),
48                  io_signature::make(0, 0, 0)),
49       d_name(name),
50       d_display(true)
51 {
52     set_key_filter(key_filter);
53 }
54 
~tag_debug_impl()55 tag_debug_impl::~tag_debug_impl() {}
56 
current_tags()57 std::vector<tag_t> tag_debug_impl::current_tags()
58 {
59     gr::thread::scoped_lock l(d_mutex);
60     return d_tags;
61 }
62 
num_tags()63 int tag_debug_impl::num_tags()
64 {
65     std::vector<tag_t> t;
66     get_tags_in_range(t, 0, 0, nitems_read(0));
67     return static_cast<int>(t.size());
68 }
69 
set_display(bool d)70 void tag_debug_impl::set_display(bool d) { d_display = d; }
71 
set_key_filter(const std::string & key_filter)72 void tag_debug_impl::set_key_filter(const std::string& key_filter)
73 {
74     if (key_filter.empty())
75         d_filter = pmt::PMT_NIL;
76     else
77         d_filter = pmt::intern(key_filter);
78 }
79 
key_filter() const80 std::string tag_debug_impl::key_filter() const { return pmt::symbol_to_string(d_filter); }
81 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)82 int tag_debug_impl::work(int noutput_items,
83                          gr_vector_const_void_star& input_items,
84                          gr_vector_void_star& output_items)
85 {
86     gr::thread::scoped_lock l(d_mutex);
87     bool toprint = false;
88 
89     std::stringstream sout;
90     if (d_display) {
91         sout << std::endl
92              << "----------------------------------------------------------------------";
93         sout << std::endl << "Tag Debug: " << d_name << std::endl;
94     }
95 
96     uint64_t abs_N, end_N;
97     for (size_t i = 0; i < input_items.size(); i++) {
98         abs_N = nitems_read(i);
99         end_N = abs_N + (uint64_t)(noutput_items);
100 
101         d_tags.clear();
102         if (pmt::is_null(d_filter))
103             get_tags_in_range(d_tags, i, abs_N, end_N);
104         else
105             get_tags_in_range(d_tags, i, abs_N, end_N, d_filter);
106 
107         if (!d_tags.empty()) {
108             toprint = true;
109         }
110 
111         if (d_display) {
112             sout << "Input Stream: " << std::setw(2) << std::setfill('0') << i
113                  << std::setfill(' ') << std::endl;
114             for (d_tags_itr = d_tags.begin(); d_tags_itr != d_tags.end(); d_tags_itr++) {
115                 sout << std::setw(10) << "Offset: " << d_tags_itr->offset << std::setw(10)
116                      << "Source: "
117                      << (pmt::is_symbol(d_tags_itr->srcid)
118                              ? pmt::symbol_to_string(d_tags_itr->srcid)
119                              : "n/a")
120                      << std::setw(10) << "Key: " << pmt::symbol_to_string(d_tags_itr->key)
121                      << std::setw(10) << "Value: ";
122                 sout << d_tags_itr->value << std::endl;
123             }
124         }
125     }
126 
127     if (d_display) {
128         sout << "----------------------------------------------------------------------";
129         sout << std::endl;
130 
131         if (toprint) {
132             std::cout << sout.str();
133         }
134     }
135 
136     return noutput_items;
137 }
138 
setup_rpc()139 void tag_debug_impl::setup_rpc()
140 {
141 #ifdef GR_CTRLPORT
142     add_rpc_variable(rpcbasic_sptr(
143         new rpcbasic_register_get<tag_debug, int>(alias(),
144                                                   "num. tags",
145                                                   &tag_debug::num_tags,
146                                                   pmt::from_long(0),
147                                                   pmt::from_long(10000),
148                                                   pmt::from_long(0),
149                                                   "",
150                                                   "Number of Tags",
151                                                   RPC_PRIVLVL_MIN,
152                                                   DISPTIME | DISPOPTSTRIP)));
153 #endif /* GR_CTRLPORT */
154 }
155 
156 } /* namespace blocks */
157 } /* namespace gr */
158