File: block_gateway_impl.cc

package info (click to toggle)
gnuradio 3.7.13.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,992 kB
  • sloc: cpp: 155,723; python: 88,934; xml: 42,165; ansic: 41,036; fortran: 927; asm: 803; sh: 224; lisp: 31; makefile: 17
file content (186 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download | duplicates (5)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2011-2013 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.
 */

#include "block_gateway_impl.h"
#include <gnuradio/io_signature.h>
#include <iostream>
#include <boost/bind.hpp>

namespace gr {

  /***********************************************************************
   * Helper routines
   **********************************************************************/
  template <typename OutType, typename InType>
  void
  copy_pointers(OutType &out, const InType &in)
  {
    out.resize(in.size());
    for(size_t i = 0; i < in.size(); i++) {
      out[i] = (void *)(in[i]);
    }
  }


  block_gateway::sptr
  block_gateway::make(feval_ll *handler,
                      const std::string &name,
                      gr::io_signature::sptr in_sig,
                      gr::io_signature::sptr out_sig,
                      const block_gw_work_type work_type,
                      const unsigned factor)
  {
    return block_gateway::sptr
      (new block_gateway_impl(handler, name, in_sig, out_sig,
                              work_type, factor));
  }

  block_gateway_impl::block_gateway_impl(feval_ll *handler,
                                         const std::string &name,
                                         gr::io_signature::sptr in_sig,
                                         gr::io_signature::sptr out_sig,
                                         const block_gw_work_type work_type,
                                         const unsigned factor)
    : block(name, in_sig, out_sig),
      _handler(handler),
      _work_type(work_type)
  {
    switch(_work_type) {
    case GR_BLOCK_GW_WORK_GENERAL:
      _decim = 1; //not relevant, but set anyway
      _interp = 1; //not relevant, but set anyway
      break;

    case GR_BLOCK_GW_WORK_SYNC:
      _decim = 1;
      _interp = 1;
      this->set_fixed_rate(true);
      break;

    case GR_BLOCK_GW_WORK_DECIM:
      _decim = factor;
      _interp = 1;
      break;

    case GR_BLOCK_GW_WORK_INTERP:
      _decim = 1;
      _interp = factor;
      this->set_output_multiple(_interp);
      break;
    }
  }

  void
  block_gateway_impl::forecast(int noutput_items,
                               gr_vector_int &ninput_items_required)
  {
    switch(_work_type) {
    case GR_BLOCK_GW_WORK_GENERAL:
      _message.action = block_gw_message_type::ACTION_FORECAST;
      _message.forecast_args_noutput_items = noutput_items;
      _message.forecast_args_ninput_items_required = ninput_items_required;
      _handler->calleval(0);
      ninput_items_required = _message.forecast_args_ninput_items_required;
      return;

    default:
      unsigned ninputs = ninput_items_required.size();
      for(unsigned i = 0; i < ninputs; i++)
        ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items);
      return;
    }
  }

  int
  block_gateway_impl::general_work(int noutput_items,
                                   gr_vector_int &ninput_items,
                                   gr_vector_const_void_star &input_items,
                                   gr_vector_void_star &output_items)
  {
    switch(_work_type) {
    case GR_BLOCK_GW_WORK_GENERAL:
      _message.action = block_gw_message_type::ACTION_GENERAL_WORK;
      _message.general_work_args_noutput_items = noutput_items;
      _message.general_work_args_ninput_items = ninput_items;
      copy_pointers(_message.general_work_args_input_items, input_items);
      _message.general_work_args_output_items = output_items;
      _handler->calleval(0);
      return _message.general_work_args_return_value;

    default:
      int r = work (noutput_items, input_items, output_items);
      if(r > 0)
        consume_each(r*_decim/_interp);
      return r;
    }
  }

  int
  block_gateway_impl::work(int noutput_items,
                           gr_vector_const_void_star &input_items,
                           gr_vector_void_star &output_items)
  {
    _message.action = block_gw_message_type::ACTION_WORK;
    _message.work_args_ninput_items = fixed_rate_noutput_to_ninput(noutput_items);
    if(_message.work_args_ninput_items == 0)
      return -1;
    _message.work_args_noutput_items = noutput_items;
    copy_pointers(_message.work_args_input_items, input_items);
    _message.work_args_output_items = output_items;
    _handler->calleval(0);
    return _message.work_args_return_value;
  }

  int
  block_gateway_impl::fixed_rate_noutput_to_ninput(int noutput_items)
  {
    return (noutput_items*_decim/_interp) + history() - 1;
  }

  int
  block_gateway_impl::fixed_rate_ninput_to_noutput(int ninput_items)
  {
    return std::max(0, ninput_items - (int)history() + 1)*_interp/_decim;
  }

  bool
  block_gateway_impl::start(void)
  {
    _message.action = block_gw_message_type::ACTION_START;
    _handler->calleval(0);
    return _message.start_args_return_value;
  }

  bool
  block_gateway_impl::stop(void)
  {
    _message.action = block_gw_message_type::ACTION_STOP;
    _handler->calleval(0);
    return _message.stop_args_return_value;
  }

  block_gw_message_type&
  block_gateway_impl::block_message(void)
  {
    return _message;
  }

} /* namespace gr */