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 "tagged_stream_to_pdu_impl.h"
28 #include <gnuradio/blocks/pdu.h>
29 #include <gnuradio/io_signature.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(pdu::vector_type type,const std::string & lengthtagname)34 tagged_stream_to_pdu::sptr tagged_stream_to_pdu::make(pdu::vector_type type,
35                                                       const std::string& lengthtagname)
36 {
37     return gnuradio::get_initial_sptr(new tagged_stream_to_pdu_impl(type, lengthtagname));
38 }
39 
tagged_stream_to_pdu_impl(pdu::vector_type type,const std::string & lengthtagname)40 tagged_stream_to_pdu_impl::tagged_stream_to_pdu_impl(pdu::vector_type type,
41                                                      const std::string& lengthtagname)
42     : tagged_stream_block("tagged_stream_to_pdu",
43                           io_signature::make(1, 1, pdu::itemsize(type)),
44                           io_signature::make(0, 0, 0),
45                           lengthtagname),
46       d_type(type),
47       d_pdu_meta(pmt::PMT_NIL),
48       d_pdu_vector(pmt::PMT_NIL)
49 {
50     message_port_register_out(pdu::pdu_port_id());
51 }
52 
work(int noutput_items,gr_vector_int & ninput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)53 int tagged_stream_to_pdu_impl::work(int noutput_items,
54                                     gr_vector_int& ninput_items,
55                                     gr_vector_const_void_star& input_items,
56                                     gr_vector_void_star& output_items)
57 {
58     const uint8_t* in = (const uint8_t*)input_items[0];
59 
60     // Grab tags, throw them into dict
61     get_tags_in_range(d_tags, 0, nitems_read(0), nitems_read(0) + ninput_items[0]);
62     d_pdu_meta = pmt::make_dict();
63     for (d_tags_itr = d_tags.begin(); d_tags_itr != d_tags.end(); d_tags_itr++) {
64         d_pdu_meta = dict_add(d_pdu_meta, (*d_tags_itr).key, (*d_tags_itr).value);
65     }
66 
67     // Grab data, throw into vector
68     d_pdu_vector = pdu::make_pdu_vector(d_type, in, ninput_items[0]);
69 
70     // Send msg
71     pmt::pmt_t msg = pmt::cons(d_pdu_meta, d_pdu_vector);
72     message_port_pub(pdu::pdu_port_id(), msg);
73 
74     return ninput_items[0];
75 }
76 
77 } /* namespace blocks */
78 } /* namespace gr */
79