python.diff

Josh Blum, 06/24/2012 09:01 pm

Download (34.8 kB)

 
b/gnuradio-core/CMakeLists.txt
1
# Copyright 2010-2011 Free Software Foundation, Inc.
1
# Copyright 2010-2012 Free Software Foundation, Inc.
2 2
#
3 3
# This file is part of GNU Radio
4 4
#
......
64 64

65 65
GR_SET_GLOBAL(GNURADIO_CORE_SWIG_INCLUDE_DIRS
66 66
    ${CMAKE_SOURCE_DIR}/gruel/src/swig
67
    ${CMAKE_BINARY_DIR}/gruel/src/swig
67 68
    ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/swig
68 69
    ${GNURADIO_CORE_INCLUDE_DIRS}
69 70
)
b/gnuradio-core/src/lib/general/CMakeLists.txt
186 186
    gr_agc2_ff
187 187
    gr_align_on_samplenumbers_ss
188 188
    gr_bin_statistics_f
189
    gr_block_gateway
189 190
    gr_bytes_to_syms
190 191
    gr_char_to_float
191 192
    gr_char_to_short
b/gnuradio-core/src/lib/general/general.i
1 1
/* -*- c++ -*- */
2 2
/*
3
 * Copyright 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
3
 * Copyright 2004-2012 Free Software Foundation, Inc.
4 4
 *
5 5
 * This file is part of GNU Radio
6 6
 *
......
254 254
%include "gr_cpm.i"
255 255
%include "gr_correlate_access_code_tag_bb.i"
256 256
%include "gr_add_ff.i"
257
%include "gr_block_gateway.i"
b/gnuradio-core/src/lib/general/gr_block_gateway.cc
1
/*
2
 * Copyright 2011-2012 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GNU Radio
5
 *
6
 * GNU Radio is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3, or (at your option)
9
 * any later version.
10
 *
11
 * GNU Radio is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GNU Radio; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 51 Franklin Street,
19
 * Boston, MA 02110-1301, USA.
20
 */
21

22
#include <gr_block_gateway.h>
23
#include <gr_io_signature.h>
24
#include <iostream>
25
#include <boost/bind.hpp>
26

27
/***********************************************************************
28
 * Helper routines
29
 **********************************************************************/
30
template <typename OutType, typename InType>
31
void copy_pointers(OutType &out, const InType &in){
32
    out.resize(in.size());
33
    for (size_t i = 0; i < in.size(); i++){
34
        out[i] = (void *)(in[i]);
35
    }
36
}
37

38
/***********************************************************************
39
 * The gr_block gateway implementation class
40
 **********************************************************************/
41
class gr_block_gateway_impl : public gr_block_gateway{
42
public:
43
    gr_block_gateway_impl(
44
        gr_feval_ll *handler,
45
        const std::string &name,
46
        gr_io_signature_sptr in_sig,
47
        gr_io_signature_sptr out_sig,
48
        const gr_block_gw_work_type work_type,
49
        const unsigned factor
50
    ):
51
        gr_block(name, in_sig, out_sig),
52
        _handler(handler),
53
        _work_type(work_type)
54
    {
55
        switch(_work_type){
56
        case GR_BLOCK_GW_WORK_GENERAL:
57
            _decim = 1; //not relevant, but set anyway
58
            _interp = 1; //not relevant, but set anyway
59
            break;
60

61
        case GR_BLOCK_GW_WORK_SYNC:
62
            _decim = 1;
63
            _interp = 1;
64
            this->set_fixed_rate(true);
65
            break;
66

67
        case GR_BLOCK_GW_WORK_DECIM:
68
            _decim = factor;
69
            _interp = 1;
70
            break;
71

72
        case GR_BLOCK_GW_WORK_INTERP:
73
            _decim = 1;
74
            _interp = factor;
75
            this->set_output_multiple(_interp);
76
            break;
77
        }
78
    }
79

80
    /*******************************************************************
81
     * Overloads for various scheduler-called functions
82
     ******************************************************************/
83
    void forecast(
84
        int noutput_items,
85
        gr_vector_int &ninput_items_required
86
    ){
87
        switch(_work_type){
88
        case GR_BLOCK_GW_WORK_GENERAL:
89
            _message.action = gr_block_gw_message_type::ACTION_FORECAST;
90
            _message.forecast_args_noutput_items = noutput_items;
91
            _message.forecast_args_ninput_items_required = ninput_items_required;
92
            _handler->calleval(0);
93
            ninput_items_required = _message.forecast_args_ninput_items_required;
94
            return;
95

96
        default:
97
            unsigned ninputs = ninput_items_required.size();
98
            for (unsigned i = 0; i < ninputs; i++)
99
                ninput_items_required[i] = fixed_rate_noutput_to_ninput(noutput_items);
100
            return;
101
        }
102
    }
103

104
    int general_work(
105
        int noutput_items,
106
        gr_vector_int &ninput_items,
107
        gr_vector_const_void_star &input_items,
108
        gr_vector_void_star &output_items
109
    ){
110
        switch(_work_type){
111
        case GR_BLOCK_GW_WORK_GENERAL:
112
            _message.action = gr_block_gw_message_type::ACTION_GENERAL_WORK;
113
            _message.general_work_args_noutput_items = noutput_items;
114
            _message.general_work_args_ninput_items = ninput_items;
115
            copy_pointers(_message.general_work_args_input_items, input_items);
116
            _message.general_work_args_output_items = output_items;
117
            _handler->calleval(0);
118
            return _message.general_work_args_return_value;
119

120
        default:
121
            int r = work (noutput_items, input_items, output_items);
122
            if (r > 0) consume_each(r*_decim/_interp);
123
            return r;
124
        }
125
    }
126

127
    int work(
128
        int noutput_items,
129
        gr_vector_const_void_star &input_items,
130
        gr_vector_void_star &output_items
131
    ){
132
        _message.action = gr_block_gw_message_type::ACTION_WORK;
133
        _message.work_args_ninput_items = fixed_rate_noutput_to_ninput(noutput_items);
134
        if (_message.work_args_ninput_items == 0) return -1;
135
        _message.work_args_noutput_items = noutput_items;
136
        copy_pointers(_message.work_args_input_items, input_items);
137
        _message.work_args_output_items = output_items;
138
        _handler->calleval(0);
139
        return _message.work_args_return_value;
140
    }
141

142
    int fixed_rate_noutput_to_ninput(int noutput_items){
143
        return (noutput_items*_decim/_interp) + history() - 1;
144
    }
145

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

150
    bool start(void){
151
        _message.action = gr_block_gw_message_type::ACTION_START;
152
        _handler->calleval(0);
153
        return _message.start_args_return_value;
154
    }
155

156
    bool stop(void){
157
        _message.action = gr_block_gw_message_type::ACTION_STOP;
158
        _handler->calleval(0);
159
        return _message.stop_args_return_value;
160
    }
161

162
    gr_block_gw_message_type &gr_block_message(void){
163
        return _message;
164
    }
165

166
private:
167
    gr_feval_ll *_handler;
168
    gr_block_gw_message_type _message;
169
    const gr_block_gw_work_type _work_type;
170
    unsigned _decim, _interp;
171
};
172

173
boost::shared_ptr<gr_block_gateway> gr_make_block_gateway(
174
    gr_feval_ll *handler,
175
    const std::string &name,
176
    gr_io_signature_sptr in_sig,
177
    gr_io_signature_sptr out_sig,
178
    const gr_block_gw_work_type work_type,
179
    const unsigned factor
180
){
181
    return boost::shared_ptr<gr_block_gateway>(
182
        new gr_block_gateway_impl(handler, name, in_sig, out_sig, work_type, factor)
183
    );
184
}
b/gnuradio-core/src/lib/general/gr_block_gateway.h
1
/*
2
 * Copyright 2011-2012 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GNU Radio
5
 *
6
 * GNU Radio is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3, or (at your option)
9
 * any later version.
10
 *
11
 * GNU Radio is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GNU Radio; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 51 Franklin Street,
19
 * Boston, MA 02110-1301, USA.
20
 */
21

22
#ifndef INCLUDED_GRBLOCK_GATEWAY_H
23
#define INCLUDED_GRBLOCK_GATEWAY_H
24

25
#include <gr_core_api.h>
26
#include <gr_block.h>
27
#include <gr_feval.h>
28

29
/*!
30
 * The work type enum tells the gateway what kind of block to implement.
31
 * The choices are familiar gnuradio block overloads (sync, decim, interp).
32
 */
33
enum gr_block_gw_work_type{
34
    GR_BLOCK_GW_WORK_GENERAL,
35
    GR_BLOCK_GW_WORK_SYNC,
36
    GR_BLOCK_GW_WORK_DECIM,
37
    GR_BLOCK_GW_WORK_INTERP,
38
};
39

40
/*!
41
 * Shared message structure between python and gateway.
42
 * Each action type represents a scheduler-called function.
43
 */
44
struct gr_block_gw_message_type{
45
    enum action_type{
46
        ACTION_GENERAL_WORK, //dispatch work
47
        ACTION_WORK, //dispatch work
48
        ACTION_FORECAST, //dispatch forecast
49
        ACTION_START, //dispatch start
50
        ACTION_STOP, //dispatch stop
51
    };
52

53
    action_type action;
54

55
    int general_work_args_noutput_items;
56
    std::vector<int> general_work_args_ninput_items;
57
    std::vector<void *> general_work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
58
    std::vector<void *> general_work_args_output_items;
59
    int general_work_args_return_value;
60

61
    int work_args_ninput_items;
62
    int work_args_noutput_items;
63
    std::vector<void *> work_args_input_items; //TODO this should be const void*, but swig cant int cast it right
64
    std::vector<void *> work_args_output_items;
65
    int work_args_return_value;
66

67
    int forecast_args_noutput_items;
68
    std::vector<int> forecast_args_ninput_items_required;
69

70
    bool start_args_return_value;
71

72
    bool stop_args_return_value;
73
};
74

75
/*!
76
 * The gateway block which performs all the magic.
77
 *
78
 * The gateway provides access to all the gr_block routines.
79
 * The methods prefixed with gr_block__ are renamed
80
 * to class methods without the prefix in python.
81
 */
82
class GR_CORE_API gr_block_gateway : virtual public gr_block{
83
public:
84
    //! Provide access to the shared message object
85
    virtual gr_block_gw_message_type &gr_block_message(void) = 0;
86

87
    long gr_block__unique_id(void) const{
88
        return gr_block::unique_id();
89
    }
90

91
    std::string gr_block__name(void) const{
92
        return gr_block::name();
93
    }
94

95
    unsigned gr_block__history(void) const{
96
        return gr_block::history();
97
    }
98

99
    void gr_block__set_history(unsigned history){
100
        return gr_block::set_history(history);
101
    }
102

103
    void gr_block__set_fixed_rate(bool fixed_rate){
104
        return gr_block::set_fixed_rate(fixed_rate);
105
    }
106

107
    bool gr_block__fixed_rate(void) const{
108
        return gr_block::fixed_rate();
109
    }
110

111
    void gr_block__set_output_multiple(int multiple){
112
        return gr_block::set_output_multiple(multiple);
113
    }
114

115
    int gr_block__output_multiple(void) const{
116
        return gr_block::output_multiple();
117
    }
118

119
    void gr_block__consume(int which_input, int how_many_items){
120
        return gr_block::consume(which_input, how_many_items);
121
    }
122

123
    void gr_block__consume_each(int how_many_items){
124
        return gr_block::consume_each(how_many_items);
125
    }
126

127
    void gr_block__produce(int which_output, int how_many_items){
128
        return gr_block::produce(which_output, how_many_items);
129
    }
130

131
    void gr_block__set_relative_rate(double relative_rate){
132
        return gr_block::set_relative_rate(relative_rate);
133
    }
134

135
    double gr_block__relative_rate(void) const{
136
        return gr_block::relative_rate();
137
    }
138

139
    uint64_t gr_block__nitems_read(unsigned int which_input){
140
        return gr_block::nitems_read(which_input);
141
    }
142

143
    uint64_t gr_block__nitems_written(unsigned int which_output){
144
        return gr_block::nitems_written(which_output);
145
    }
146

147
    gr_block::tag_propagation_policy_t gr_block__tag_propagation_policy(void){
148
        return gr_block::tag_propagation_policy();
149
    }
150

151
    void gr_block__set_tag_propagation_policy(gr_block::tag_propagation_policy_t p){
152
        return gr_block::set_tag_propagation_policy(p);
153
    }
154

155
    void gr_block__add_item_tag(
156
        unsigned int which_output, const gr_tag_t &tag
157
    ){
158
        return gr_block::add_item_tag(which_output, tag);
159
    }
160

161
    void gr_block__add_item_tag(
162
        unsigned int which_output,
163
        uint64_t abs_offset,
164
        const pmt::pmt_t &key,
165
        const pmt::pmt_t &value,
166
        const pmt::pmt_t &srcid=pmt::PMT_F
167
    ){
168
        return gr_block::add_item_tag(which_output, abs_offset, key, value, srcid);
169
    }
170

171
    std::vector<gr_tag_t> gr_block__get_tags_in_range(
172
        unsigned int which_input,
173
        uint64_t abs_start,
174
        uint64_t abs_end
175
    ){
176
        std::vector<gr_tag_t> tags;
177
        gr_block::get_tags_in_range(tags, which_input, abs_start, abs_end);
178
        return tags;
179
    }
180

181
    std::vector<gr_tag_t> gr_block__get_tags_in_range(
182
        unsigned int which_input,
183
        uint64_t abs_start,
184
        uint64_t abs_end,
185
        const pmt::pmt_t &key
186
    ){
187
        std::vector<gr_tag_t> tags;
188
        gr_block::get_tags_in_range(tags, which_input, abs_start, abs_end, key);
189
        return tags;
190
    }
191
};
192

193
/*!
194
 * Make a new gateway block.
195
 * \param handler the swig director object with callback
196
 * \param name the name of the block (Ex: "Shirley")
197
 * \param in_sig the input signature for this block
198
 * \param out_sig the output signature for this block
199
 * \param work_type the type of block overload to implement
200
 * \param factor the decimation or interpolation factor
201
 * \return a new gateway block
202
 */
203
GR_CORE_API boost::shared_ptr<gr_block_gateway> gr_make_block_gateway(
204
    gr_feval_ll *handler,
205
    const std::string &name,
206
    gr_io_signature_sptr in_sig,
207
    gr_io_signature_sptr out_sig,
208
    const gr_block_gw_work_type work_type,
209
    const unsigned factor
210
);
211

212
#endif /* INCLUDED_GRBLOCK_GATEWAY_H */
b/gnuradio-core/src/lib/general/gr_block_gateway.i
1
/*
2
 * Copyright 2011-2012 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GNU Radio
5
 *
6
 * GNU Radio is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3, or (at your option)
9
 * any later version.
10
 *
11
 * GNU Radio is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GNU Radio; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 51 Franklin Street,
19
 * Boston, MA 02110-1301, USA.
20
 */
21

22
////////////////////////////////////////////////////////////////////////
23
// standard includes
24
////////////////////////////////////////////////////////////////////////
25
%include <gnuradio.i>
26
%include <gr_tags.i>
27
%include <gr_feval.i>
28

29
////////////////////////////////////////////////////////////////////////
30
// block headers
31
////////////////////////////////////////////////////////////////////////
32
%{
33
#include <gr_block_gateway.h>
34
%}
35

36
////////////////////////////////////////////////////////////////////////
37
// data type support
38
////////////////////////////////////////////////////////////////////////
39
%template(int_vector_t) std::vector<int>;
40
%template(void_star_vector_t) std::vector<void *>;
41

42
////////////////////////////////////////////////////////////////////////
43
// block magic
44
////////////////////////////////////////////////////////////////////////
45
GR_SWIG_BLOCK_MAGIC(gr,block_gateway);
46
%include <gr_block_gateway.h>
b/gnuradio-core/src/python/gnuradio/gr/CMakeLists.txt
23 23
GR_PYTHON_INSTALL(FILES
24 24
    __init__.py
25 25
    exceptions.py
26
    gateway.py
26 27
    gr_threading.py
27 28
    gr_threading_23.py
28 29
    gr_threading_24.py
......
43 44
foreach(py_qa_test_file ${py_qa_test_files})
44 45
    get_filename_component(py_qa_test_name ${py_qa_test_file} NAME_WE)
45 46
    set(GR_TEST_PYTHON_DIRS
47
        ${CMAKE_SOURCE_DIR}/gruel/src/python
48
        ${CMAKE_BINARY_DIR}/gruel/src/swig
46 49
        ${CMAKE_BINARY_DIR}/gnuradio-core/src/python
47 50
        ${CMAKE_BINARY_DIR}/gnuradio-core/src/lib/swig
48 51
    )
b/gnuradio-core/src/python/gnuradio/gr/__init__.py
1 1
#
2
# Copyright 2003,2004,2006,2008,2009,2010 Free Software Foundation, Inc.
2
# Copyright 2003-2012 Free Software Foundation, Inc.
3 3
#
4 4
# This file is part of GNU Radio
5 5
#
......
44 44
from exceptions import *
45 45
from hier_block2 import *
46 46
from top_block import *
47
from gateway import basic_block, sync_block, decim_block, interp_block
47 48

48 49
if _RTLD_GLOBAL != 0:
49 50
    sys.setdlopenflags(_dlopenflags)             # Restore original flags
b/gnuradio-core/src/python/gnuradio/gr/gateway.py
1
#
2
# Copyright 2011-2012 Free Software Foundation, Inc.
3
#
4
# This file is part of GNU Radio
5
#
6
# GNU Radio is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3, or (at your option)
9
# any later version.
10
#
11
# GNU Radio is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with GNU Radio; see the file COPYING.  If not, write to
18
# the Free Software Foundation, Inc., 51 Franklin Street,
19
# Boston, MA 02110-1301, USA.
20
#
21

22
import gnuradio_core as gr_core
23
from gnuradio_core import io_signature, io_signaturev
24
from gnuradio_core import gr_block_gw_message_type
25
from gnuradio_core import block_gateway
26
import numpy
27

28
########################################################################
29
# Magic to turn pointers into numpy arrays
30
# http://docs.scipy.org/doc/numpy/reference/arrays.interface.html
31
########################################################################
32
def pointer_to_ndarray(addr, dtype, nitems):
33
    class array_like:
34
        __array_interface__ = {
35
            'data' : (int(addr), False),
36
            'typestr' : dtype.base.str,
37
            'descr' : dtype.base.descr,
38
            'shape' : (nitems,) + dtype.shape,
39
            'strides' : None,
40
            'version' : 3
41
        }
42
    return numpy.asarray(array_like()).view(dtype.base)
43

44
########################################################################
45
# Handler that does callbacks from C++
46
########################################################################
47
class gateway_handler(gr_core.feval_ll):
48

49
    #dont put a constructor, it wont work
50

51
    def init(self, callback):
52
        self._callback = callback
53

54
    def eval(self, arg):
55
        try: self._callback()
56
        except Exception as ex:
57
            print("handler caught exception: %s"%ex)
58
            import traceback; traceback.print_exc()
59
            raise ex
60
        return 0
61

62
########################################################################
63
# The guts that make this into a gr block
64
########################################################################
65
class gateway_block(object):
66

67
    def __init__(self, name, in_sig, out_sig, work_type, factor):
68

69
        #ensure that the sigs are iterable dtypes
70
        def sig_to_dtype_sig(sig):
71
            if sig is None: sig = ()
72
            return map(numpy.dtype, sig)
73
        self.__in_sig = sig_to_dtype_sig(in_sig)
74
        self.__out_sig = sig_to_dtype_sig(out_sig)
75

76
        #cache the ranges to iterate when dispatching work
77
        self.__in_indexes = range(len(self.__in_sig))
78
        self.__out_indexes = range(len(self.__out_sig))
79

80
        #convert the signatures into gr.io_signatures
81
        def sig_to_gr_io_sigv(sig):
82
            if not len(sig): return io_signature(0, 0, 0)
83
            return io_signaturev(len(sig), len(sig), [s.itemsize for s in sig])
84
        gr_in_sig = sig_to_gr_io_sigv(self.__in_sig)
85
        gr_out_sig = sig_to_gr_io_sigv(self.__out_sig)
86

87
        #create internal gateway block
88
        self.__handler = gateway_handler()
89
        self.__handler.init(self.__gr_block_handle)
90
        self.__gateway = block_gateway(
91
            self.__handler, name, gr_in_sig, gr_out_sig, work_type, factor)
92
        self.__message = self.__gateway.gr_block_message()
93

94
        #register gr_block functions
95
        prefix = 'gr_block__'
96
        for attr in [x for x in dir(self.__gateway) if x.startswith(prefix)]:
97
            setattr(self, attr.replace(prefix, ''), getattr(self.__gateway, attr))
98
        self.pop_msg_queue = lambda: gr_core.gr_block_gw_pop_msg_queue_safe(self.__gateway)
99

100
    def to_basic_block(self):
101
        """
102
        Makes this block connectable by hier/top block python
103
        """
104
        return self.__gateway.to_basic_block()
105

106
    def __gr_block_handle(self):
107
        """
108
        Dispatch tasks according to the action type specified in the message.
109
        """
110
        if self.__message.action == gr_block_gw_message_type.ACTION_GENERAL_WORK:
111
            self.__message.general_work_args_return_value = self.general_work(
112

113
                input_items=[pointer_to_ndarray(
114
                    self.__message.general_work_args_input_items[i],
115
                    self.__in_sig[i],
116
                    self.__message.general_work_args_ninput_items[i]
117
                ) for i in self.__in_indexes],
118

119
                output_items=[pointer_to_ndarray(
120
                    self.__message.general_work_args_output_items[i],
121
                    self.__out_sig[i],
122
                    self.__message.general_work_args_noutput_items
123
                ) for i in self.__out_indexes],
124
            )
125

126
        elif self.__message.action == gr_block_gw_message_type.ACTION_WORK:
127
            self.__message.work_args_return_value = self.work(
128

129
                input_items=[pointer_to_ndarray(
130
                    self.__message.work_args_input_items[i],
131
                    self.__in_sig[i],
132
                    self.__message.work_args_ninput_items
133
                ) for i in self.__in_indexes],
134

135
                output_items=[pointer_to_ndarray(
136
                    self.__message.work_args_output_items[i],
137
                    self.__out_sig[i],
138
                    self.__message.work_args_noutput_items
139
                ) for i in self.__out_indexes],
140
            )
141

142
        elif self.__message.action == gr_block_gw_message_type.ACTION_FORECAST:
143
            self.forecast(
144
                noutput_items=self.__message.forecast_args_noutput_items,
145
                ninput_items_required=self.__message.forecast_args_ninput_items_required,
146
            )
147

148
        elif self.__message.action == gr_block_gw_message_type.ACTION_START:
149
            self.__message.start_args_return_value = self.start()
150

151
        elif self.__message.action == gr_block_gw_message_type.ACTION_STOP:
152
            self.__message.stop_args_return_value = self.stop()
153

154
    def forecast(self, noutput_items, ninput_items_required):
155
        """
156
        forecast is only called from a general block
157
        this is the default implementation
158
        """
159
        for ninput_item in ninput_items_required:
160
            ninput_item = noutput_items + self.history() - 1;
161
        return
162

163
    def general_work(self, *args, **kwargs):
164
        """general work to be overloaded in a derived class"""
165
        raise NotImplementedError("general work not implemented")
166

167
    def work(self, *args, **kwargs):
168
        """work to be overloaded in a derived class"""
169
        raise NotImplementedError("work not implemented")
170

171
    def start(self): return True
172
    def stop(self): return True
173

174
########################################################################
175
# Wrappers for the user to inherit from
176
########################################################################
177
class basic_block(gateway_block):
178
    def __init__(self, name, in_sig, out_sig):
179
        gateway_block.__init__(self,
180
            name=name,
181
            in_sig=in_sig,
182
            out_sig=out_sig,
183
            work_type=gr_core.GR_BLOCK_GW_WORK_GENERAL,
184
            factor=1, #not relevant factor
185
        )
186

187
class sync_block(gateway_block):
188
    def __init__(self, name, in_sig, out_sig):
189
        gateway_block.__init__(self,
190
            name=name,
191
            in_sig=in_sig,
192
            out_sig=out_sig,
193
            work_type=gr_core.GR_BLOCK_GW_WORK_SYNC,
194
            factor=1,
195
        )
196

197
class decim_block(gateway_block):
198
    def __init__(self, name, in_sig, out_sig, decim):
199
        gateway_block.__init__(self,
200
            name=name,
201
            in_sig=in_sig,
202
            out_sig=out_sig,
203
            work_type=gr_core.GR_BLOCK_GW_WORK_DECIM,
204
            factor=decim,
205
        )
206

207
class interp_block(gateway_block):
208
    def __init__(self, name, in_sig, out_sig, interp):
209
        gateway_block.__init__(self,
210
            name=name,
211
            in_sig=in_sig,
212
            out_sig=out_sig,
213
            work_type=gr_core.GR_BLOCK_GW_WORK_INTERP,
214
            factor=interp,
215
        )
b/gnuradio-core/src/python/gnuradio/gr/qa_block_gateway.py
1
#
2
# Copyright 2011-2012 Free Software Foundation, Inc.
3
#
4
# This file is part of GNU Radio
5
#
6
# GNU Radio is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3, or (at your option)
9
# any later version.
10
#
11
# GNU Radio is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with GNU Radio; see the file COPYING.  If not, write to
18
# the Free Software Foundation, Inc., 51 Franklin Street,
19
# Boston, MA 02110-1301, USA.
20
#
21

22
from gnuradio import gr, gr_unittest
23
import pmt #from gruel import pmt
24
import numpy
25

26
class add_2_f32_1_f32(gr.sync_block):
27
    def __init__(self):
28
        gr.sync_block.__init__(
29
            self,
30
            name = "add 2 f32",
31
            in_sig = [numpy.float32, numpy.float32],
32
            out_sig = [numpy.float32],
33
        )
34

35
    def work(self, input_items, output_items):
36
        output_items[0][:] = input_items[0] + input_items[1]
37
        return len(output_items[0])
38

39
class add_2_fc32_1_fc32(gr.sync_block):
40
    def __init__(self):
41
        gr.sync_block.__init__(
42
            self,
43
            name = "add 2 fc32",
44
            in_sig = [numpy.complex64, numpy.complex64],
45
            out_sig = [numpy.complex64],
46
        )
47

48
    def work(self, input_items, output_items):
49
        output_items[0][:] = input_items[0] + input_items[1]
50
        return len(output_items[0])
51

52
class convolve(gr.sync_block):
53
    """
54
    A demonstration using block history to properly perform a convolution.
55
    """
56
    def __init__(self):
57
        gr.sync_block.__init__(
58
            self,
59
            name = "convolve",
60
            in_sig = [numpy.float32],
61
            out_sig = [numpy.float32]
62
        )
63
        self._taps = [1, 0, 0, 0]
64
        self.set_history(len(self._taps))
65

66
    def work(self, input_items, output_items):
67
        output_items[0][:] = numpy.convolve(input_items[0], self._taps, mode='valid')
68
        return len(output_items[0])
69

70
class decim2x(gr.decim_block):
71
    def __init__(self):
72
        gr.decim_block.__init__(
73
            self,
74
            name = "decim2x",
75
            in_sig = [numpy.float32],
76
            out_sig = [numpy.float32],
77
            decim = 2
78
        )
79

80
    def work(self, input_items, output_items):
81
        output_items[0][:] = input_items[0][::2]
82
        return len(output_items[0])
83

84
class interp2x(gr.interp_block):
85
    def __init__(self):
86
        gr.interp_block.__init__(
87
            self,
88
            name = "interp2x",
89
            in_sig = [numpy.float32],
90
            out_sig = [numpy.float32],
91
            interp = 2
92
        )
93

94
    def work(self, input_items, output_items):
95
        output_items[0][1::2] = input_items[0]
96
        output_items[0][::2] = input_items[0]
97
        return len(output_items[0])
98

99
class tag_source(gr.sync_block):
100
    def __init__(self):
101
        gr.sync_block.__init__(
102
            self,
103
            name = "tag source",
104
            in_sig = None,
105
            out_sig = [numpy.float32],
106
        )
107

108
    def work(self, input_items, output_items):
109
        num_output_items = len(output_items[0])
110

111
        #put code here to fill the output items...
112

113
        #make a new tag on the middle element every time work is called
114
        count = self.nitems_written(0) + num_output_items/2
115
        key = pmt.pmt_string_to_symbol("example_key")
116
        value = pmt.pmt_string_to_symbol("example_value")
117
        self.add_item_tag(0, count, key, value)
118

119
        return num_output_items
120

121
class tag_sink(gr.sync_block):
122
    def __init__(self):
123
        gr.sync_block.__init__(
124
            self,
125
            name = "tag sink",
126
            in_sig = [numpy.float32],
127
            out_sig = None,
128
        )
129
        self.key = None
130

131
    def work(self, input_items, output_items):
132
        num_input_items = len(input_items[0])
133

134
        #put code here to process the input items...
135

136
        #print all the tags received in this work call
137
        nread = self.nitems_read(0)
138
        tags = self.get_tags_in_range(0, nread, nread+num_input_items)
139
        for tag in tags:
140
            print tag.offset
141
            print pmt.pmt_symbol_to_string(tag.key)
142
            print pmt.pmt_symbol_to_string(tag.value)
143
            self.key = pmt.pmt_symbol_to_string(tag.key)
144

145
        return num_input_items
146

147
class fc32_to_f32_2(gr.sync_block):
148
    def __init__(self):
149
        gr.sync_block.__init__(
150
            self,
151
            name = "fc32_to_f32_2",
152
            in_sig = [numpy.complex64],
153
            out_sig = [(numpy.float32, 2)],
154
        )
155

156
    def work(self, input_items, output_items):
157
        output_items[0][::,0] = numpy.real(input_items[0])
158
        output_items[0][::,1] = numpy.imag(input_items[0])
159
        return len(output_items[0])
160

161
class test_block_gateway(gr_unittest.TestCase):
162

163
    def test_add_f32(self):
164
        tb = gr.top_block()
165
        src0 = gr.vector_source_f([1, 3, 5, 7, 9], False)
166
        src1 = gr.vector_source_f([0, 2, 4, 6, 8], False)
167
        adder = add_2_f32_1_f32()
168
        sink = gr.vector_sink_f()
169
        tb.connect((src0, 0), (adder, 0))
170
        tb.connect((src1, 0), (adder, 1))
171
        tb.connect(adder, sink)
172
        tb.run()
173
        self.assertEqual(sink.data(), (1, 5, 9, 13, 17))
174

175
    def test_add_fc32(self):
176
        tb = gr.top_block()
177
        src0 = gr.vector_source_c([1, 3j, 5, 7j, 9], False)
178
        src1 = gr.vector_source_c([0, 2j, 4, 6j, 8], False)
179
        adder = add_2_fc32_1_fc32()
180
        sink = gr.vector_sink_c()
181
        tb.connect((src0, 0), (adder, 0))
182
        tb.connect((src1, 0), (adder, 1))
183
        tb.connect(adder, sink)
184
        tb.run()
185
        self.assertEqual(sink.data(), (1, 5j, 9, 13j, 17))
186

187
    def test_convolve(self):
188
        tb = gr.top_block()
189
        src = gr.vector_source_f([1, 2, 3, 4, 5, 6, 7, 8], False)
190
        cv = convolve()
191
        sink = gr.vector_sink_f()
192
        tb.connect(src, cv, sink)
193
        tb.run()
194
        self.assertEqual(sink.data(), (1, 2, 3, 4, 5, 6, 7, 8))
195

196
    def test_decim2x(self):
197
        tb = gr.top_block()
198
        src = gr.vector_source_f([1, 2, 3, 4, 5, 6, 7, 8], False)
199
        d2x = decim2x()
200
        sink = gr.vector_sink_f()
201
        tb.connect(src, d2x, sink)
202
        tb.run()
203
        self.assertEqual(sink.data(), (1, 3, 5, 7))
204

205
    def test_interp2x(self):
206
        tb = gr.top_block()
207
        src = gr.vector_source_f([1, 3, 5, 7, 9], False)
208
        i2x = interp2x()
209
        sink = gr.vector_sink_f()
210
        tb.connect(src, i2x, sink)
211
        tb.run()
212
        self.assertEqual(sink.data(), (1, 1, 3, 3, 5, 5, 7, 7, 9, 9))
213

214
    def test_tags(self):
215
        src = tag_source()
216
        sink = tag_sink()
217
        head = gr.head(gr.sizeof_float, 50000) #should be enough items to get a tag through
218
        tb = gr.top_block()
219
        tb.connect(src, head, sink)
220
        tb.run()
221
        self.assertEqual(sink.key, "example_key")
222

223
    def test_fc32_to_f32_2(self):
224
        tb = gr.top_block()
225
        src = gr.vector_source_c([1+2j, 3+4j, 5+6j, 7+8j, 9+10j], False)
226
        convert = fc32_to_f32_2()
227
        v2s = gr.vector_to_stream(gr.sizeof_float, 2)
228
        sink = gr.vector_sink_f()
229
        tb.connect(src, convert, v2s, sink)
230
        tb.run()
231
        self.assertEqual(sink.data(), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
232

233
if __name__ == '__main__':
234
    gr_unittest.run(test_block_gateway, "test_block_gateway.xml")
235