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 <gnuradio/blocks/pdu.h>
28
29 namespace gr {
30 namespace blocks {
31 namespace pdu {
32
pdu_port_id()33 const pmt::pmt_t pdu_port_id()
34 {
35 static const pmt::pmt_t pdu_port_id = pmt::mp("pdus");
36 return pdu_port_id;
37 }
38
itemsize(vector_type type)39 size_t itemsize(vector_type type)
40 {
41 switch (type) {
42 case byte_t:
43 return sizeof(char);
44 case float_t:
45 return sizeof(float);
46 case complex_t:
47 return sizeof(gr_complex);
48 default:
49 throw std::runtime_error("bad PDU type");
50 }
51 }
52
type_matches(vector_type type,pmt::pmt_t v)53 bool type_matches(vector_type type, pmt::pmt_t v)
54 {
55 switch (type) {
56 case byte_t:
57 return pmt::is_u8vector(v);
58 case float_t:
59 return pmt::is_f32vector(v);
60 case complex_t:
61 return pmt::is_c32vector(v);
62 default:
63 throw std::runtime_error("bad PDU type");
64 }
65 }
66
make_pdu_vector(vector_type type,const uint8_t * buf,size_t items)67 pmt::pmt_t make_pdu_vector(vector_type type, const uint8_t* buf, size_t items)
68 {
69 switch (type) {
70 case byte_t:
71 return pmt::init_u8vector(items, buf);
72 case float_t:
73 return pmt::init_f32vector(items, (const float*)buf);
74 case complex_t:
75 return pmt::init_c32vector(items, (const gr_complex*)buf);
76 default:
77 throw std::runtime_error("bad PDU type");
78 }
79 }
80
type_from_pmt(pmt::pmt_t vector)81 vector_type type_from_pmt(pmt::pmt_t vector)
82 {
83 if (pmt::is_u8vector(vector))
84 return byte_t;
85 if (pmt::is_f32vector(vector))
86 return float_t;
87 if (pmt::is_c32vector(vector))
88 return complex_t;
89 throw std::runtime_error("bad PDU type");
90 }
91
92 } /* namespace pdu */
93 } /* namespace blocks */
94 } /* namespace gr */
95