summaryrefslogtreecommitdiff
path: root/gr-fec/lib/async_encoder_impl.cc
blob: b12ece1f3a8f988f6361457e7b0f76196273feed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- c++ -*- */
/*
 * Copyright 2014 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "async_encoder_impl.h"
#include <gnuradio/io_signature.h>
#include <stdio.h>

namespace gr {
  namespace fec {

    async_encoder::sptr
    async_encoder::make(generic_encoder::sptr my_encoder)
    {
      return gnuradio::get_initial_sptr
        (new async_encoder_impl(my_encoder));
    }

    async_encoder_impl::async_encoder_impl(generic_encoder::sptr my_encoder)
      : block("async_encoder",
              io_signature::make(0,0,0),
              io_signature::make(0,0,0)),
        d_input_item_size(sizeof(char)), d_output_item_size(sizeof(char))
    {
      d_encoder = my_encoder;
      message_port_register_in(pmt::mp("pdus"));
      message_port_register_out(pmt::mp("pdus"));
      set_msg_handler(pmt::mp("pdus"), boost::bind(&async_encoder_impl::encode, this ,_1) );
    }

    async_encoder_impl::~async_encoder_impl()
    {
    }

    void
    async_encoder_impl::encode(pmt::pmt_t msg)
    {
      // extract input pdu
      pmt::pmt_t meta(pmt::car(msg));
      pmt::pmt_t bits(pmt::cdr(msg));

      int nbits = pmt::length(bits);
      int blksize_in = d_encoder->get_input_size();
      int blksize_out = (int)d_encoder->rate();
      int nblocks = nbits/blksize_in;

      printf("async_encoder: blksize_in = %d, blksize_out = %d nblocks=%d\n",
             blksize_in, blksize_out, nblocks);

      pmt::pmt_t outvec(pmt::make_u8vector(blksize_out * nblocks, 0xFF));

      printf("encoder: outvec len = %d\n", blksize_out * nblocks);

      // get pmt buffer pointers
      size_t o0(0);
      const uint8_t* u8in = pmt::u8vector_elements(bits,o0);
      uint8_t* u8tmp = pmt::u8vector_writable_elements(bits,o0);
      uint8_t* u8out = pmt::u8vector_writable_elements(outvec,o0);

      // alloc playspace
      std::vector<uint8_t> blks_out(blksize_out);

      // loop over n fec blocks
      for(int i = 0; i < nblocks; i++) {
        size_t offset_in = i * blksize_in;
        size_t offset_out = i * blksize_out;

        for(int jj = 0; jj < blksize_in; jj++) {
          u8tmp[offset_in + jj] = 0x01 & u8in[offset_in + jj];
          //printf("in[%zd]=%u ", offset_in+jj, u8tmp[offset_in + jj]);
          //fflush(stdout);
        }

        // ENCODE!
        d_encoder->generic_work((void*)&u8tmp[offset_in], (void*)&blks_out[0]);

        // convert float -> u8
        for(int j = 0; j < blksize_out; j++) {
          u8out[offset_out + j] = (blks_out[j] > 0);
        }
      }

      message_port_pub(pmt::mp("pdus"), pmt::cons(meta, outvec));
    }

    int
    async_encoder_impl::general_work(int noutput_items,
                                     gr_vector_int& ninput_items,
                                     gr_vector_const_void_star &input_items,
                                     gr_vector_void_star &output_items)
    {
      return noutput_items;
    }

  } /* namespace fec */
} /* namespace gr */