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 "stream_to_tagged_stream_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <cstring>
30 
31 namespace gr {
32 namespace blocks {
33 
34 stream_to_tagged_stream::sptr
make(size_t itemsize,unsigned int vlen,unsigned packet_len,const std::string & len_tag_key)35 stream_to_tagged_stream::make(size_t itemsize,
36                               unsigned int vlen,
37                               unsigned packet_len,
38                               const std::string& len_tag_key)
39 {
40     return gnuradio::get_initial_sptr(
41         new stream_to_tagged_stream_impl(itemsize, vlen, packet_len, len_tag_key));
42 }
43 
stream_to_tagged_stream_impl(size_t itemsize,unsigned int vlen,unsigned packet_len,const std::string & len_tag_key)44 stream_to_tagged_stream_impl::stream_to_tagged_stream_impl(size_t itemsize,
45                                                            unsigned int vlen,
46                                                            unsigned packet_len,
47                                                            const std::string& len_tag_key)
48     : gr::sync_block("stream_to_tagged_stream",
49                      gr::io_signature::make(1, 1, itemsize * vlen),
50                      gr::io_signature::make(1, 1, itemsize * vlen)),
51       d_itemsize(itemsize * vlen),
52       d_packet_len(packet_len),
53       d_packet_len_pmt(pmt::from_long(packet_len)),
54       d_len_tag_key(pmt::string_to_symbol(len_tag_key)),
55       d_next_tag_pos(0)
56 {
57 }
58 
~stream_to_tagged_stream_impl()59 stream_to_tagged_stream_impl::~stream_to_tagged_stream_impl() {}
60 
set_packet_len(unsigned packet_len)61 void stream_to_tagged_stream_impl::set_packet_len(unsigned packet_len)
62 {
63     gr::thread::scoped_lock guard(d_setlock);
64     d_packet_len = packet_len;
65 }
set_packet_len_pmt(unsigned packet_len)66 void stream_to_tagged_stream_impl::set_packet_len_pmt(unsigned packet_len)
67 {
68     gr::thread::scoped_lock guard(d_setlock);
69     d_packet_len_pmt = pmt::from_long(packet_len);
70 }
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)71 int stream_to_tagged_stream_impl::work(int noutput_items,
72                                        gr_vector_const_void_star& input_items,
73                                        gr_vector_void_star& output_items)
74 {
75     gr::thread::scoped_lock guard(d_setlock);
76     const unsigned char* in = (const unsigned char*)input_items[0];
77     unsigned char* out = (unsigned char*)output_items[0];
78     // Copy data
79     memcpy(out, in, noutput_items * d_itemsize);
80     // Add tags every d_packet_len
81     while (d_next_tag_pos < nitems_written(0) + noutput_items) {
82         add_item_tag(0, d_next_tag_pos, d_len_tag_key, d_packet_len_pmt);
83         d_next_tag_pos += d_packet_len;
84     }
85 
86     return noutput_items;
87 }
88 
start()89 bool stream_to_tagged_stream_impl::start()
90 {
91     d_next_tag_pos = 0;
92     return true;
93 }
94 
95 } /* namespace blocks */
96 } /* namespace gr */
97