1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2010,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_debug_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <cstdio>
30 #include <iostream>
31 
32 namespace gr {
33 namespace blocks {
34 
make()35 message_debug::sptr message_debug::make()
36 {
37     return gnuradio::get_initial_sptr(new message_debug_impl());
38 }
39 
print(pmt::pmt_t msg)40 void message_debug_impl::print(pmt::pmt_t msg)
41 {
42     std::cout << "******* MESSAGE DEBUG PRINT ********\n";
43     pmt::print(msg);
44     std::cout << "************************************\n";
45 }
46 
store(pmt::pmt_t msg)47 void message_debug_impl::store(pmt::pmt_t msg)
48 {
49     gr::thread::scoped_lock guard(d_mutex);
50     d_messages.push_back(msg);
51 }
52 
print_pdu(pmt::pmt_t pdu)53 void message_debug_impl::print_pdu(pmt::pmt_t pdu)
54 {
55     if (pmt::is_null(pdu) || !pmt::is_pair(pdu)) {
56         GR_LOG_WARN(d_logger, "Non PDU type message received. Dropping.");
57         return;
58     }
59 
60     pmt::pmt_t meta = pmt::car(pdu);
61     pmt::pmt_t vector = pmt::cdr(pdu);
62     std::cout << "* MESSAGE DEBUG PRINT PDU VERBOSE *\n";
63     pmt::print(meta);
64     size_t len = pmt::blob_length(vector);
65     std::cout << "pdu_length = " << len << std::endl;
66     std::cout << "contents = " << std::endl;
67     size_t offset(0);
68     const uint8_t* d = (const uint8_t*)pmt::uniform_vector_elements(vector, offset);
69     for (size_t i = 0; i < len; i += 16) {
70         printf("%04x: ", ((unsigned int)i));
71         for (size_t j = i; j < std::min(i + 16, len); j++) {
72             printf("%02x ", d[j]);
73         }
74 
75         std::cout << std::endl;
76     }
77 
78     std::cout << "***********************************\n";
79 }
80 
num_messages()81 int message_debug_impl::num_messages()
82 {
83     gr::thread::scoped_lock guard(d_mutex);
84     return (int)d_messages.size();
85 }
86 
get_message(int i)87 pmt::pmt_t message_debug_impl::get_message(int i)
88 {
89     gr::thread::scoped_lock guard(d_mutex);
90 
91     if ((size_t)i >= d_messages.size()) {
92         throw std::runtime_error("message_debug: index for message out of bounds.\n");
93     }
94 
95     return d_messages[i];
96 }
97 
message_debug_impl()98 message_debug_impl::message_debug_impl()
99     : block("message_debug", io_signature::make(0, 0, 0), io_signature::make(0, 0, 0))
100 {
101     message_port_register_in(pmt::mp("print"));
102     set_msg_handler(pmt::mp("print"), [this](pmt::pmt_t msg) { this->print(msg); });
103 
104     message_port_register_in(pmt::mp("store"));
105     set_msg_handler(pmt::mp("store"), [this](pmt::pmt_t msg) { this->store(msg); });
106 
107     message_port_register_in(pmt::mp("print_pdu"));
108     set_msg_handler(pmt::mp("print_pdu"),
109                     [this](pmt::pmt_t msg) { this->print_pdu(msg); });
110 }
111 
~message_debug_impl()112 message_debug_impl::~message_debug_impl() {}
113 
114 } /* namespace blocks */
115 } /* namespace gr */
116