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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
/* -*- c++ -*- */
/*
* Copyright 2006,2007,2009,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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "hier_block2_detail.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/prefs.h>
#include <stdexcept>
#include <sstream>
#include <boost/format.hpp>
namespace gr {
#define HIER_BLOCK2_DETAIL_DEBUG 0
hier_block2_detail::hier_block2_detail(hier_block2 *owner)
: d_owner(owner),
d_parent_detail(0),
d_fg(make_flowgraph())
{
int min_inputs = owner->input_signature()->min_streams();
int max_inputs = owner->input_signature()->max_streams();
int min_outputs = owner->output_signature()->min_streams();
int max_outputs = owner->output_signature()->max_streams();
if(max_inputs == io_signature::IO_INFINITE ||
max_outputs == io_signature::IO_INFINITE ||
(min_inputs != max_inputs) ||(min_outputs != max_outputs) ) {
std::stringstream msg;
msg << "Hierarchical blocks do not yet support arbitrary or"
<< " variable numbers of inputs or outputs (" << d_owner->name() << ")";
throw std::runtime_error(msg.str());
}
d_inputs = std::vector<endpoint_vector_t>(max_inputs);
d_outputs = endpoint_vector_t(max_outputs);
}
hier_block2_detail::~hier_block2_detail()
{
d_owner = 0; // Don't use delete, we didn't allocate
}
void
hier_block2_detail::connect(basic_block_sptr block)
{
std::stringstream msg;
// Check if duplicate
if(std::find(d_blocks.begin(), d_blocks.end(), block) != d_blocks.end()) {
msg << "Block " << block << " already connected.";
throw std::invalid_argument(msg.str());
}
// Check if has inputs or outputs
if(block->input_signature()->max_streams() != 0 ||
block->output_signature()->max_streams() != 0) {
msg << "Block " << block << " must not have any input or output ports";
throw std::invalid_argument(msg.str());
}
hier_block2_sptr hblock(cast_to_hier_block2_sptr(block));
if(hblock && hblock.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connect: block is hierarchical, setting parent to " << this << std::endl;
hblock->d_detail->d_parent_detail = this;
}
d_blocks.push_back(block);
}
void
hier_block2_detail::connect(basic_block_sptr src, int src_port,
basic_block_sptr dst, int dst_port)
{
std::stringstream msg;
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connecting: " << endpoint(src, src_port)
<< " -> " << endpoint(dst, dst_port) << std::endl;
if(src.get() == dst.get())
throw std::invalid_argument("connect: src and destination blocks cannot be the same");
hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));
if(src_block && src.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connect: src is hierarchical, setting parent to " << this << std::endl;
src_block->d_detail->d_parent_detail = this;
}
if(dst_block && dst.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connect: dst is hierarchical, setting parent to " << this << std::endl;
dst_block->d_detail->d_parent_detail = this;
}
// Connections to block inputs or outputs
int max_port;
if(src.get() == d_owner) {
max_port = src->input_signature()->max_streams();
if((max_port != -1 && (src_port >= max_port)) || src_port < 0) {
msg << "source port " << src_port << " out of range for " << src;
throw std::invalid_argument(msg.str());
}
return connect_input(src_port, dst_port, dst);
}
if(dst.get() == d_owner) {
max_port = dst->output_signature()->max_streams();
if((max_port != -1 && (dst_port >= max_port)) || dst_port < 0) {
msg << "destination port " << dst_port << " out of range for " << dst;
throw std::invalid_argument(msg.str());
}
return connect_output(dst_port, src_port, src);
}
// Internal connections
d_fg->connect(src, src_port, dst, dst_port);
// TODO: connects to NC
}
void
hier_block2_detail::msg_connect(basic_block_sptr src, pmt::pmt_t srcport,
basic_block_sptr dst, pmt::pmt_t dstport)
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connecting message port..." << std::endl;
// register the subscription
// this is done later...
// src->message_port_sub(srcport, pmt::cons(dst->alias_pmt(), dstport));
// add block uniquely to list to internal blocks
if(std::find(d_blocks.begin(), d_blocks.end(), dst) == d_blocks.end()){
d_blocks.push_back(src);
d_blocks.push_back(dst);
}
bool hier_out = (d_owner == src.get()) && src->message_port_is_hier_out(srcport);;
bool hier_in = (d_owner == dst.get()) && dst->message_port_is_hier_in(dstport);
hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));
if(src_block && src.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connect: src is hierarchical, setting parent to " << this << std::endl;
src_block->d_detail->d_parent_detail = this;
}
if(dst_block && dst.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "connect: dst is hierarchical, setting parent to " << this << std::endl;
dst_block->d_detail->d_parent_detail = this;
}
// add edge for this message connection
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << boost::format("connect( (%s, %s, %d), (%s, %s, %d) )\n") % \
src % srcport % hier_out %
dst % dstport % hier_in;
d_fg->connect(msg_endpoint(src, srcport, hier_out), msg_endpoint(dst, dstport, hier_in));
}
void
hier_block2_detail::msg_disconnect(basic_block_sptr src, pmt::pmt_t srcport,
basic_block_sptr dst, pmt::pmt_t dstport)
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnecting message port..." << std::endl;
// unregister the subscription - if already subscribed
src->message_port_unsub(srcport, pmt::cons(dst->alias_pmt(), dstport));
// remove edge for this message connection
bool hier_out = (d_owner == src.get()) && src->message_port_is_hier_out(srcport);;
bool hier_in = (d_owner == dst.get()) && dst->message_port_is_hier_in(dstport);
d_fg->disconnect(msg_endpoint(src, srcport, hier_out), msg_endpoint(dst, dstport, hier_in));
}
void
hier_block2_detail::disconnect(basic_block_sptr block)
{
// Check on singleton list
for(basic_block_viter_t p = d_blocks.begin(); p != d_blocks.end(); p++) {
if(*p == block) {
d_blocks.erase(p);
hier_block2_sptr hblock(cast_to_hier_block2_sptr(block));
if(block && block.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnect: block is hierarchical, clearing parent" << std::endl;
hblock->d_detail->d_parent_detail = 0;
}
return;
}
}
// Otherwise find all edges containing block
edge_vector_t edges, tmp = d_fg->edges();
edge_vector_t::iterator p;
for(p = tmp.begin(); p != tmp.end(); p++) {
if((*p).src().block() == block || (*p).dst().block() == block) {
edges.push_back(*p);
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnect: block found in edge " << (*p) << std::endl;
}
}
if(edges.size() == 0) {
std::stringstream msg;
msg << "cannot disconnect block " << block << ", not found";
throw std::invalid_argument(msg.str());
}
for(p = edges.begin(); p != edges.end(); p++) {
disconnect((*p).src().block(), (*p).src().port(),
(*p).dst().block(), (*p).dst().port());
}
}
void
hier_block2_detail::disconnect(basic_block_sptr src, int src_port,
basic_block_sptr dst, int dst_port)
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnecting: " << endpoint(src, src_port)
<< " -> " << endpoint(dst, dst_port) << std::endl;
if(src.get() == dst.get())
throw std::invalid_argument("disconnect: source and destination blocks cannot be the same");
hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));
if(src_block && src.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnect: src is hierarchical, clearing parent" << std::endl;
src_block->d_detail->d_parent_detail = 0;
}
if(dst_block && dst.get() != d_owner) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "disconnect: dst is hierarchical, clearing parent" << std::endl;
dst_block->d_detail->d_parent_detail = 0;
}
if(src.get() == d_owner)
return disconnect_input(src_port, dst_port, dst);
if(dst.get() == d_owner)
return disconnect_output(dst_port, src_port, src);
// Internal connections
d_fg->disconnect(src, src_port, dst, dst_port);
}
void
hier_block2_detail::connect_input(int my_port, int port,
basic_block_sptr block)
{
std::stringstream msg;
if(my_port < 0 || my_port >= (signed)d_inputs.size()) {
msg << "input port " << my_port << " out of range for " << block;
throw std::invalid_argument(msg.str());
}
endpoint_vector_t &endps = d_inputs[my_port];
endpoint endp(block, port);
endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp);
if(p != endps.end()) {
msg << "external input port " << my_port << " already wired to " << endp;
throw std::invalid_argument(msg.str());
}
endps.push_back(endp);
}
void
hier_block2_detail::connect_output(int my_port, int port,
basic_block_sptr block)
{
std::stringstream msg;
if(my_port < 0 || my_port >= (signed)d_outputs.size()) {
msg << "output port " << my_port << " out of range for " << block;
throw std::invalid_argument(msg.str());
}
if(d_outputs[my_port].block()) {
msg << "external output port " << my_port << " already connected from "
<< d_outputs[my_port];
throw std::invalid_argument(msg.str());
}
d_outputs[my_port] = endpoint(block, port);
}
void
hier_block2_detail::disconnect_input(int my_port, int port,
basic_block_sptr block)
{
std::stringstream msg;
if(my_port < 0 || my_port >= (signed)d_inputs.size()) {
msg << "input port number " << my_port << " out of range for " << block;
throw std::invalid_argument(msg.str());
}
endpoint_vector_t &endps = d_inputs[my_port];
endpoint endp(block, port);
endpoint_viter_t p = std::find(endps.begin(), endps.end(), endp);
if(p == endps.end()) {
msg << "external input port " << my_port << " not connected to " << endp;
throw std::invalid_argument(msg.str());
}
endps.erase(p);
}
void
hier_block2_detail::disconnect_output(int my_port, int port,
basic_block_sptr block)
{
std::stringstream msg;
if(my_port < 0 || my_port >= (signed)d_outputs.size()) {
msg << "output port number " << my_port << " out of range for " << block;
throw std::invalid_argument(msg.str());
}
if(d_outputs[my_port].block() != block) {
msg << "block " << block << " not assigned to output "
<< my_port << ", can't disconnect";
throw std::invalid_argument(msg.str());
}
d_outputs[my_port] = endpoint();
}
endpoint_vector_t
hier_block2_detail::resolve_port(int port, bool is_input)
{
std::stringstream msg;
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Resolving port " << port << " as an "
<< (is_input ? "input" : "output")
<< " of " << d_owner->name() << std::endl;
endpoint_vector_t result;
if(is_input) {
if(port < 0 || port >= (signed)d_inputs.size()) {
msg << "resolve_port: hierarchical block '" << d_owner->name()
<< "': input " << port << " is out of range";
throw std::runtime_error(msg.str());
}
if(d_inputs[port].empty()) {
msg << "resolve_port: hierarchical block '" << d_owner->name()
<< "': input " << port << " is not connected internally";
throw std::runtime_error(msg.str());
}
endpoint_vector_t &endps = d_inputs[port];
endpoint_viter_t p;
for(p = endps.begin(); p != endps.end(); p++) {
endpoint_vector_t tmp = resolve_endpoint(*p, true);
std::copy(tmp.begin(), tmp.end(), back_inserter(result));
}
}
else {
if(port < 0 || port >= (signed)d_outputs.size()) {
msg << "resolve_port: hierarchical block '" << d_owner->name()
<< "': output " << port << " is out of range";
throw std::runtime_error(msg.str());
}
if(d_outputs[port] == endpoint()) {
msg << "resolve_port: hierarchical block '" << d_owner->name()
<< "': output " << port << " is not connected internally";
throw std::runtime_error(msg.str());
}
result = resolve_endpoint(d_outputs[port], false);
}
if(result.empty()) {
msg << "resolve_port: hierarchical block '" << d_owner->name()
<< "': unable to resolve "
<< (is_input ? "input port " : "output port ")
<< port;
throw std::runtime_error(msg.str());
}
return result;
}
void
hier_block2_detail::disconnect_all()
{
d_fg->clear();
d_blocks.clear();
d_inputs.clear();
d_outputs.clear();
}
endpoint_vector_t
hier_block2_detail::resolve_endpoint(const endpoint &endp, bool is_input) const
{
std::stringstream msg;
endpoint_vector_t result;
// Check if endpoint is a leaf node
if(cast_to_block_sptr(endp.block())) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Block " << endp.block() << " is a leaf node, returning." << std::endl;
result.push_back(endp);
return result;
}
// Check if endpoint is a hierarchical block
hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(endp.block()));
if(hier_block2) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Resolving endpoint " << endp << " as an "
<< (is_input ? "input" : "output")
<< ", recursing" << std::endl;
return hier_block2->d_detail->resolve_port(endp.port(), is_input);
}
msg << "unable to resolve" << (is_input ? " input " : " output ")
<< "endpoint " << endp;
throw std::runtime_error(msg.str());
}
void
hier_block2_detail::flatten_aux(flat_flowgraph_sptr sfg) const
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << " ** Flattening " << d_owner->name() << std::endl;
// Add my edges to the flow graph, resolving references to actual endpoints
edge_vector_t edges = d_fg->edges();
msg_edge_vector_t msg_edges = d_fg->msg_edges();
edge_viter_t p;
msg_edge_viter_t q,u;
// Only run setup_rpc if ControlPort config param is enabled.
bool ctrlport_on = prefs::singleton()->get_bool("ControlPort", "on", false);
// For every block (gr::block and gr::hier_block2), set up the RPC
// interface.
for(p = edges.begin(); p != edges.end(); p++) {
basic_block_sptr b;
b = p->src().block();
if(ctrlport_on) {
if(!b->is_rpc_set()) {
b->setup_rpc();
b->rpc_set();
}
}
b = p->dst().block();
if(ctrlport_on) {
if(!b->is_rpc_set()) {
b->setup_rpc();
b->rpc_set();
}
}
}
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Flattening stream connections: " << std::endl;
for(p = edges.begin(); p != edges.end(); p++) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Flattening edge " << (*p) << std::endl;
endpoint_vector_t src_endps = resolve_endpoint(p->src(), false);
endpoint_vector_t dst_endps = resolve_endpoint(p->dst(), true);
endpoint_viter_t s, d;
for(s = src_endps.begin(); s != src_endps.end(); s++) {
for(d = dst_endps.begin(); d != dst_endps.end(); d++) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << (*s) << "->" << (*d) << std::endl;
sfg->connect(*s, *d);
}
}
}
// loop through flattening hierarchical connections
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "Flattening msg connections: " << std::endl;
std::vector<std::pair<msg_endpoint, bool> > resolved_endpoints;
for(q = msg_edges.begin(); q != msg_edges.end(); q++) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << boost::format(" flattening edge ( %s, %s, %d) -> ( %s, %s, %d)\n") % \
q->src().block() % q->src().port() % q->src().is_hier() % q->dst().block() % \
q->dst().port() % q->dst().is_hier();
bool normal_connection = true;
// resolve existing connections to hier ports
if(q->dst().is_hier()) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << boost::format(" resolve hier output (%s, %s)") % \
q->dst().block() % q->dst().port() << std::endl;
sfg->replace_endpoint( q->dst(), q->src(), true );
resolved_endpoints.push_back(std::pair<msg_endpoint, bool>(q->dst(),true));
normal_connection = false;
}
if(q->src().is_hier()) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << boost::format(" resolve hier input (%s, %s)") % \
q->src().block() % q->src().port() << std::endl;
sfg->replace_endpoint( q->src(), q->dst(), false );
resolved_endpoints.push_back(std::pair<msg_endpoint, bool>(q->src(),false));
normal_connection = false;
}
// propogate non hier connections through
if(normal_connection){
sfg->connect( q->src(), q->dst() );
}
}
for(std::vector<std::pair<msg_endpoint, bool> >::iterator it = resolved_endpoints.begin();
it != resolved_endpoints.end(); it++) {
sfg->clear_endpoint((*it).first, (*it).second);
}
/*
// connect primitive edges in the new fg
for(q = msg_edges.begin(); q != msg_edges.end(); q++) {
if((!q->src().is_hier()) && (!q->dst().is_hier())) {
sfg->connect(q->src(), q->dst());
}
else {
std::cout << "not connecting hier connection!" << std::endl;
}
}
*/
// Construct unique list of blocks used either in edges, inputs,
// outputs, or by themselves. I still hate STL.
basic_block_vector_t blocks; // unique list of used blocks
basic_block_vector_t tmp = d_fg->calc_used_blocks();
// First add the list of singleton blocks
std::vector<basic_block_sptr>::const_iterator b; // Because flatten_aux is const
for(b = d_blocks.begin(); b != d_blocks.end(); b++)
tmp.push_back(*b);
// Now add the list of connected input blocks
std::stringstream msg;
for(unsigned int i = 0; i < d_inputs.size(); i++) {
if(d_inputs[i].size() == 0) {
msg << "In hierarchical block " << d_owner->name() << ", input " << i
<< " is not connected internally";
throw std::runtime_error(msg.str());
}
for(unsigned int j = 0; j < d_inputs[i].size(); j++)
tmp.push_back(d_inputs[i][j].block());
}
for(unsigned int i = 0; i < d_outputs.size(); i++) {
basic_block_sptr blk = d_outputs[i].block();
if(!blk) {
msg << "In hierarchical block " << d_owner->name() << ", output " << i
<< " is not connected internally";
throw std::runtime_error(msg.str());
}
tmp.push_back(blk);
}
sort(tmp.begin(), tmp.end());
std::insert_iterator<basic_block_vector_t> inserter(blocks, blocks.begin());
unique_copy(tmp.begin(), tmp.end(), inserter);
// Recurse hierarchical children
for(basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(*p));
if(hier_block2 && (hier_block2.get() != d_owner)) {
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "flatten_aux: recursing into hierarchical block "
<< hier_block2 << std::endl;
hier_block2->d_detail->flatten_aux(sfg);
}
}
}
void
hier_block2_detail::lock()
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "lock: entered in " << this << std::endl;
if(d_parent_detail)
d_parent_detail->lock();
else
d_owner->lock();
}
void
hier_block2_detail::unlock()
{
if(HIER_BLOCK2_DETAIL_DEBUG)
std::cout << "unlock: entered in " << this << std::endl;
if(d_parent_detail)
d_parent_detail->unlock();
else
d_owner->unlock();
}
} /* namespace gr */
|