1 /* -*- c++ -*- */
2 /*
3  * Copyright 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 "pdu_set_impl.h"
28 #include <gnuradio/blocks/pdu.h>
29 #include <gnuradio/io_signature.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(pmt::pmt_t k,pmt::pmt_t v)34 pdu_set::sptr pdu_set::make(pmt::pmt_t k, pmt::pmt_t v)
35 {
36     return gnuradio::get_initial_sptr(new pdu_set_impl(k, v));
37 }
38 
pdu_set_impl(pmt::pmt_t k,pmt::pmt_t v)39 pdu_set_impl::pdu_set_impl(pmt::pmt_t k, pmt::pmt_t v)
40     : block("pdu_set", io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)),
41       d_k(k),
42       d_v(v)
43 {
44     message_port_register_out(pdu::pdu_port_id());
45     message_port_register_in(pdu::pdu_port_id());
46     set_msg_handler(pdu::pdu_port_id(),
47                     [this](pmt::pmt_t msg) { this->handle_msg(msg); });
48 }
49 
handle_msg(pmt::pmt_t pdu)50 void pdu_set_impl::handle_msg(pmt::pmt_t pdu)
51 {
52     // add the field and publish
53     pmt::pmt_t meta = pmt::car(pdu);
54     if (pmt::is_null(meta)) {
55         meta = pmt::make_dict();
56     } else if (!pmt::is_dict(meta)) {
57         throw std::runtime_error("pdu_set received non PDU input");
58     }
59     meta = pmt::dict_add(meta, d_k, d_v);
60     message_port_pub(pdu::pdu_port_id(), pmt::cons(meta, pmt::cdr(pdu)));
61 }
62 
63 } /* namespace blocks */
64 } /* namespace gr */
65