1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2009,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 "copy_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <string.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(size_t itemsize)34 copy::sptr copy::make(size_t itemsize)
35 {
36     return gnuradio::get_initial_sptr(new copy_impl(itemsize));
37 }
38 
copy_impl(size_t itemsize)39 copy_impl::copy_impl(size_t itemsize)
40     : block("copy",
41             io_signature::make(1, -1, itemsize),
42             io_signature::make(1, -1, itemsize)),
43       d_itemsize(itemsize),
44       d_enabled(true)
45 {
46     message_port_register_in(pmt::mp("en"));
47     set_msg_handler(pmt::mp("en"), [this](pmt::pmt_t msg) { this->handle_enable(msg); });
48 }
49 
~copy_impl()50 copy_impl::~copy_impl() {}
51 
handle_enable(pmt::pmt_t msg)52 void copy_impl::handle_enable(pmt::pmt_t msg)
53 {
54     if (pmt::is_bool(msg)) {
55         bool en = pmt::to_bool(msg);
56         d_enabled = en;
57     }
58 }
59 
forecast(int noutput_items,gr_vector_int & ninput_items_required)60 void copy_impl::forecast(int noutput_items, gr_vector_int& ninput_items_required)
61 {
62     unsigned ninputs = ninput_items_required.size();
63     for (unsigned i = 0; i < ninputs; i++)
64         ninput_items_required[i] = noutput_items;
65 }
66 
check_topology(int ninputs,int noutputs)67 bool copy_impl::check_topology(int ninputs, int noutputs) { return ninputs == noutputs; }
68 
general_work(int noutput_items,gr_vector_int & ninput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)69 int copy_impl::general_work(int noutput_items,
70                             gr_vector_int& ninput_items,
71                             gr_vector_const_void_star& input_items,
72                             gr_vector_void_star& output_items)
73 {
74     const uint8_t** in = (const uint8_t**)&input_items[0];
75     uint8_t** out = (uint8_t**)&output_items[0];
76 
77     int n = 0;
78     if (d_enabled) {
79         int ninputs = input_items.size();
80         for (int i = 0; i < ninputs; i++) {
81             memcpy(out[i], in[i], noutput_items * d_itemsize);
82         }
83         n = noutput_items;
84     }
85 
86     consume_each(noutput_items);
87     return n;
88 }
89 
90 
setup_rpc()91 void copy_impl::setup_rpc()
92 {
93 #ifdef GR_CTRLPORT
94     add_rpc_variable(rpcbasic_sptr(new rpcbasic_register_handler<copy>(
95         alias(), "en", "", "Enable", RPC_PRIVLVL_MIN, DISPNULL)));
96 #endif /* GR_CTRLPORT */
97 }
98 
99 } /* namespace blocks */
100 } /* namespace gr */
101