summaryrefslogtreecommitdiff
path: root/lib/block_handlers.cpp
blob: de0a8bf6f3091f65bc2019f1b8b5eb2b2f26c8d8 (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
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
//
// Copyright 2012 Josh Blum
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with io_sig program.  If not, see <http://www.gnu.org/licenses/>.

#include "element_impl.hpp"

using namespace gnuradio;

void ElementImpl::handle_input_msg(const tsbe::TaskInterface &handle, const size_t index, const tsbe::Wax &msg)
{
    if (msg.type() == typeid(Tag))
    {
        this->input_tags[index].push_back(msg.cast<Tag>());
        this->input_tags_changed[index] = true;
    }
    if (msg.type() == typeid(Token))
    {
        this->token_pool.push_back(msg.cast<Token>());
    }
    if (msg.type() == typeid(CheckTokensMessage))
    {
        if (this->input_tokens[index].unique())
        {
            this->mark_done(handle);
        }
    }
}

void ElementImpl::handle_output_msg(const tsbe::TaskInterface &handle, const size_t index, const tsbe::Wax &msg)
{
    if (msg.type() == typeid(Token))
    {
        this->token_pool.push_back(msg.cast<Token>());
    }
    if (msg.type() == typeid(CheckTokensMessage))
    {
        if (this->output_tokens[index].unique())
        {
            this->mark_done(handle);
        }
    }
}

template <typename V, typename T>
void resize_fill(V &v, const size_t new_len, const T &fill)
{
    if (v.size() >= new_len) return; //dont ever shrink it
    v.resize(new_len, fill);
}

template <typename V>
void resize_fill_back(V &v, const size_t new_len)
{
    if (v.empty()) v.push_back(0);
    resize_fill(v, new_len, v.back());
}

template <typename V, typename Sig>
void fill_item_sizes_from_sig(V &v, const Sig &s, const size_t size)
{
    v.resize(size);
    for (size_t i = 0; i < v.size(); i++)
    {
        v[i] = s->sizeof_stream_item(i);
    }
}

void ElementImpl::topology_update(const tsbe::TaskInterface &task_iface, const tsbe::Wax &state)
{
    const size_t num_inputs = task_iface.get_num_inputs();
    const size_t num_outputs = task_iface.get_num_outputs();

    //fill the item sizes from the IO signatures
    fill_item_sizes_from_sig(this->input_items_sizes, this->input_signature, num_inputs);
    fill_item_sizes_from_sig(this->output_items_sizes, this->output_signature, num_outputs);

    //resize and fill port properties
    resize_fill_back(this->input_history_items, num_inputs);
    resize_fill_back(this->output_multiple_items, num_outputs);

    //resize the bytes consumed/produced
    resize_fill(this->items_consumed, num_inputs, 0);
    resize_fill(this->items_produced, num_outputs, 0);

    //resize all work buffers to match current connections
    this->work_input_items.resize(num_inputs);
    this->work_output_items.resize(num_outputs);
    this->work_ninput_items.resize(num_inputs);
    this->input_items.resize(num_inputs);
    this->output_items.resize(num_outputs);
    this->consume_items.resize(num_inputs, 0);
    this->produce_items.resize(num_outputs, 0);
    this->input_buff_offsets.resize(num_inputs, 0);

    //resize tags vector to match sizes
    this->input_tags_changed.resize(num_inputs);
    this->input_tags.resize(num_inputs);
    this->output_tags.resize(num_outputs);

    //resize and clear that initial history
    this->history_buffs.resize(num_inputs);
    for (size_t i = 0; i < num_inputs; i++)
    {
        tsbe::Buffer &buff = this->history_buffs[i];
        const size_t num_bytes = this->input_items_sizes[i]*this->input_history_items[i];
        if (not buff or buff.get_length() != num_bytes)
        {
            tsbe::BufferConfig config;
            config.memory = NULL;
            config.length = num_bytes;
            buff = tsbe::Buffer(config);
        }
    }

    //allocate output tokens and send them downstream
    if (state.cast<TopBlockMessage>().what == TopBlockMessage::ACTIVE)
    {
        this->input_tokens.resize(num_inputs);
        for (size_t i = 0; i < num_inputs; i++)
        {
            this->input_tokens[i] = make_token();
            task_iface.post_upstream(i, this->input_tokens[i]);
        }
        this->output_tokens.resize(num_outputs);
        for (size_t i = 0; i < num_outputs; i++)
        {
            this->output_tokens[i] = make_token();
            task_iface.post_downstream(i, this->output_tokens[i]);
        }
    }

    if (state.cast<TopBlockMessage>().what == TopBlockMessage::ACTIVE)
    {
        this->active = true;
        this->token = state.cast<TopBlockMessage>().token;

        //causes initial processing kick-off for source blocks
        this->handle_allocation(task_iface);
    }
    if (state.cast<TopBlockMessage>().what == TopBlockMessage::INERT)
    {
        this->active = false;
        this->token = state.cast<TopBlockMessage>().token;
    }
}