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 "random_pdu_impl.h"
28 #include <gnuradio/blocks/pdu.h>
29 #include <gnuradio/io_signature.h>
30
31 namespace gr {
32 namespace blocks {
33
34 random_pdu::sptr
make(int min_items,int max_items,unsigned char byte_mask,int length_modulo)35 random_pdu::make(int min_items, int max_items, unsigned char byte_mask, int length_modulo)
36 {
37 return gnuradio::get_initial_sptr(
38 new random_pdu_impl(min_items, max_items, byte_mask, length_modulo));
39 }
40
random_pdu_impl(int min_items,int max_items,unsigned char byte_mask,int length_modulo)41 random_pdu_impl::random_pdu_impl(int min_items,
42 int max_items,
43 unsigned char byte_mask,
44 int length_modulo)
45 : block("random_pdu", io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)),
46 d_urange(min_items, max_items),
47 d_brange(0, 255),
48 d_rvar(d_rng, d_urange),
49 d_bvar(d_rng, d_brange),
50 d_mask(byte_mask),
51 d_length_modulo(length_modulo)
52 {
53 message_port_register_out(pdu::pdu_port_id());
54 message_port_register_in(pmt::mp("generate"));
55 set_msg_handler(pmt::mp("generate"),
56 [this](pmt::pmt_t msg) { this->generate_pdu(msg); });
57 if (length_modulo < 1)
58 throw std::runtime_error("length_module must be >= 1");
59 if (max_items < length_modulo)
60 throw std::runtime_error("max_items must be >= to length_modulo");
61 }
62
start()63 bool random_pdu_impl::start()
64 {
65 output_random();
66 return true;
67 }
68
output_random()69 void random_pdu_impl::output_random()
70 {
71 // pick a random vector length
72 int len = d_rvar();
73 len = std::max(d_length_modulo, len - len % d_length_modulo);
74
75 // fill it with random bytes
76 std::vector<unsigned char> vec(len);
77 for (int i = 0; i < len; i++)
78 vec[i] = ((unsigned char)d_bvar()) & d_mask;
79
80 // send the vector
81 pmt::pmt_t vecpmt(pmt::make_blob(&vec[0], len));
82 pmt::pmt_t pdu(pmt::cons(pmt::PMT_NIL, vecpmt));
83
84 message_port_pub(pdu::pdu_port_id(), pdu);
85 }
86
87 } /* namespace blocks */
88 } /* namespace gr */
89