1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012 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 "repack_bits_bb_impl.h"
28 #include <gnuradio/io_signature.h>
29 
30 namespace gr {
31 namespace blocks {
32 
make(int k,int l,const std::string & len_tag_key,bool align_output,endianness_t endianness)33 repack_bits_bb::sptr repack_bits_bb::make(int k,
34                                           int l,
35                                           const std::string& len_tag_key,
36                                           bool align_output,
37                                           endianness_t endianness)
38 {
39     return gnuradio::get_initial_sptr(
40         new repack_bits_bb_impl(k, l, len_tag_key, align_output, endianness));
41 }
42 
repack_bits_bb_impl(int k,int l,const std::string & len_tag_key,bool align_output,endianness_t endianness)43 repack_bits_bb_impl::repack_bits_bb_impl(int k,
44                                          int l,
45                                          const std::string& len_tag_key,
46                                          bool align_output,
47                                          endianness_t endianness)
48     : tagged_stream_block("repack_bits_bb",
49                           io_signature::make(1, 1, sizeof(char)),
50                           io_signature::make(1, 1, sizeof(char)),
51                           len_tag_key),
52       d_k(k),
53       d_l(l),
54       d_packet_mode(!len_tag_key.empty()),
55       d_in_index(0),
56       d_out_index(0),
57       d_align_output(align_output),
58       d_endianness(endianness)
59 {
60     if (d_k > 8 || d_k < 1 || d_l > 8 || d_l < 1) {
61         throw std::invalid_argument("k and l must be in [1, 8]");
62     }
63 
64     set_relative_rate((uint64_t)d_k, (uint64_t)d_l);
65 }
66 
set_k_and_l(int k,int l)67 void repack_bits_bb_impl::set_k_and_l(int k, int l)
68 {
69     gr::thread::scoped_lock guard(d_setlock);
70     d_k = k;
71     d_l = l;
72     set_relative_rate((uint64_t)d_k, (uint64_t)d_l);
73 }
74 
~repack_bits_bb_impl()75 repack_bits_bb_impl::~repack_bits_bb_impl() {}
76 
calculate_output_stream_length(const gr_vector_int & ninput_items)77 int repack_bits_bb_impl::calculate_output_stream_length(const gr_vector_int& ninput_items)
78 {
79     int n_out_bytes_required = (ninput_items[0] * d_k) / d_l;
80     if ((ninput_items[0] * d_k) % d_l &&
81         (!d_packet_mode || (d_packet_mode && !d_align_output))) {
82         n_out_bytes_required++;
83     }
84 
85     return n_out_bytes_required;
86 }
87 
work(int noutput_items,gr_vector_int & ninput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)88 int repack_bits_bb_impl::work(int noutput_items,
89                               gr_vector_int& ninput_items,
90                               gr_vector_const_void_star& input_items,
91                               gr_vector_void_star& output_items)
92 {
93     gr::thread::scoped_lock guard(d_setlock);
94     const unsigned char* in = (const unsigned char*)input_items[0];
95     unsigned char* out = (unsigned char*)output_items[0];
96     int bytes_to_write = noutput_items;
97 
98     if (d_packet_mode) { // noutput_items could be larger than necessary
99         int bytes_to_read = ninput_items[0];
100         bytes_to_write = bytes_to_read * d_k / d_l;
101         if (!d_align_output && (((bytes_to_read * d_k) % d_l) != 0)) {
102             bytes_to_write++;
103         }
104     }
105 
106     int n_read = 0;
107     int n_written = 0;
108     switch (d_endianness) {
109     case GR_LSB_FIRST:
110         while (n_written < bytes_to_write && n_read < ninput_items[0]) {
111             if (d_out_index == 0) { // Starting a fresh byte
112                 out[n_written] = 0;
113             }
114             out[n_written] |= ((in[n_read] >> d_in_index) & 0x01) << d_out_index;
115 
116             d_in_index = (d_in_index + 1) % d_k;
117             d_out_index = (d_out_index + 1) % d_l;
118             if (d_in_index == 0) {
119                 n_read++;
120                 d_in_index = 0;
121             }
122             if (d_out_index == 0) {
123                 n_written++;
124                 d_out_index = 0;
125             }
126         }
127 
128         if (d_packet_mode) {
129             if (d_out_index) {
130                 n_written++;
131                 d_out_index = 0;
132             }
133         } else {
134             consume_each(n_read);
135         }
136         break;
137 
138 
139     case GR_MSB_FIRST:
140         while (n_written < bytes_to_write && n_read < ninput_items[0]) {
141             if (d_out_index == 0) { // Starting a fresh byte
142                 out[n_written] = 0;
143             }
144             out[n_written] |= ((in[n_read] >> (d_k - 1 - d_in_index)) & 0x01)
145                               << (d_l - 1 - d_out_index);
146 
147             d_in_index = (d_in_index + 1) % d_k;
148             d_out_index = (d_out_index + 1) % d_l;
149             if (d_in_index == 0) {
150                 n_read++;
151                 d_in_index = 0;
152             }
153             if (d_out_index == 0) {
154                 n_written++;
155                 d_out_index = 0;
156             }
157         }
158 
159         if (d_packet_mode) {
160             if (d_out_index) {
161                 n_written++;
162                 d_out_index = 0;
163             }
164         } else {
165             consume_each(n_read);
166         }
167         break;
168 
169     default:
170         throw std::runtime_error("repack_bits_bb: unrecognized endianness value.");
171     }
172 
173     return n_written;
174 }
175 
176 } /* namespace blocks */
177 } /* namespace gr */
178