3 * Copyright 2007,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <mblock/mblock.h>
26 #include <mblock/protocol_class.h>
27 #include <mblock/message.h>
28 #include <mblock/class_registry.h>
34 static pmt_t s_in = pmt_intern("in");
35 static pmt_t s_out = pmt_intern("out");
36 static pmt_t s_data = pmt_intern("data");
37 static pmt_t s_start = pmt_intern("start");
38 static pmt_t s_send_batch = pmt_intern("send-batch");
39 static pmt_t s_long0 = pmt_from_long(0);
50 * \brief mblock used for QA.
52 * Messages arriving on "in" consist of a pair containing a (long)
53 * message number in the car, and a (long) bitmap in the cdr. For
54 * each message received on "in", a new message is sent on "out". The
55 * new message is the same format as the input, but the bitmap in
56 * the cdr has a "1" or'd into it that corresponds to the bit number
57 * specified in the constructor.
59 * The bitmap can be used by the ultimate receiver to confirm
60 * traversal of a set of blocks, if the blocks are assigned unique bit
63 class qa_bitset : public mb_mblock
70 qa_bitset(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
71 void handle_message(mb_message_sptr msg);
74 qa_bitset::qa_bitset(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
75 : mb_mblock(runtime, instance_name, user_arg)
77 d_bitno = pmt_to_long(user_arg); // The bit we are to set
79 d_in = define_port("in", "qa-bitset", false, mb_port::EXTERNAL);
80 d_out = define_port("out", "qa-bitset", true, mb_port::EXTERNAL);
84 qa_bitset::handle_message(mb_message_sptr msg)
86 if (pmt_eq(msg->port_id(), s_in) && pmt_eq(msg->signal(), s_data)){
88 pmt_cons(pmt_car(msg->data()),
89 pmt_from_long((1L << d_bitno) | pmt_to_long(pmt_cdr(msg->data())))));
93 REGISTER_MBLOCK_CLASS(qa_bitset);
95 // ------------------------------------------------------------------------
98 * \brief mblock used for QA. Compose two qa_bitset mblocks.
100 class qa_bitset2 : public mb_mblock
106 qa_bitset2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
109 qa_bitset2::qa_bitset2(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
110 : mb_mblock(runtime, instance_name, user_arg)
112 long bitno = pmt_to_long(user_arg); // The bit we are to set
114 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
115 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
117 define_component("bs0", "qa_bitset", pmt_from_long(bitno));
118 define_component("bs1", "qa_bitset", pmt_from_long(bitno + 1));
119 connect("self", "in", "bs0", "in");
120 connect("bs0", "out", "bs1", "in");
121 connect("bs1", "out", "self", "out");
124 REGISTER_MBLOCK_CLASS(qa_bitset2);
126 // ------------------------------------------------------------------------
129 * \brief mblock used for QA. Compose two qa_bitset2 mblocks.
131 class qa_bitset4 : public mb_mblock
137 qa_bitset4(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
140 qa_bitset4::qa_bitset4(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
141 : mb_mblock(runtime, instance_name, user_arg)
143 long bitno = pmt_to_long(user_arg); // The bit we are to set
145 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
146 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
148 define_component("bs0", "qa_bitset2", pmt_from_long(bitno));
149 define_component("bs1", "qa_bitset2", pmt_from_long(bitno + 2));
150 connect("self", "in", "bs0", "in");
151 connect("bs0", "out", "bs1", "in");
152 connect("bs1", "out", "self", "out");
155 REGISTER_MBLOCK_CLASS(qa_bitset4);
157 // ------------------------------------------------------------------------
160 * \brief mblock used for QA. Compose two qa_bitset4 mblocks.
162 class qa_bitset8 : public mb_mblock
168 qa_bitset8(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
171 qa_bitset8::qa_bitset8(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
172 : mb_mblock(runtime, instance_name, user_arg)
174 long bitno = pmt_to_long(user_arg); // The bit we are to set
176 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
177 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
179 define_component("bs0", "qa_bitset4", pmt_from_long(bitno));
180 define_component("bs1", "qa_bitset4", pmt_from_long(bitno + 4));
181 connect("self", "in", "bs0", "in");
182 connect("bs0", "out", "bs1", "in");
183 connect("bs1", "out", "self", "out");
186 REGISTER_MBLOCK_CLASS(qa_bitset8);
188 // ------------------------------------------------------------------------
191 * \brief mblock used for QA. Compose two qa_bitset8 mblocks.
193 class qa_bitset16 : public mb_mblock
199 qa_bitset16(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
202 qa_bitset16::qa_bitset16(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
203 : mb_mblock(runtime, instance_name, user_arg)
205 long bitno = pmt_to_long(user_arg); // The bit we are to set
207 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
208 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
210 define_component("bs0", "qa_bitset8", pmt_from_long(bitno));
211 define_component("bs1", "qa_bitset8", pmt_from_long(bitno + 8));
212 connect("self", "in", "bs0", "in");
213 connect("bs0", "out", "bs1", "in");
214 connect("bs1", "out", "self", "out");
217 REGISTER_MBLOCK_CLASS(qa_bitset16);
219 // ------------------------------------------------------------------------
222 * \brief mblock used for QA. Compose two qa_bitset16 mblocks.
224 class qa_bitset32 : public mb_mblock
230 qa_bitset32(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
233 qa_bitset32::qa_bitset32(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
234 : mb_mblock(runtime, instance_name, user_arg)
236 long bitno = pmt_to_long(user_arg); // The bit we are to set
238 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
239 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
241 define_component("bs0", "qa_bitset16", pmt_from_long(bitno));
242 define_component("bs1", "qa_bitset16", pmt_from_long(bitno + 16));
243 connect("self", "in", "bs0", "in");
244 connect("bs0", "out", "bs1", "in");
245 connect("bs1", "out", "self", "out");
248 REGISTER_MBLOCK_CLASS(qa_bitset32);
250 // ------------------------------------------------------------------------
252 class qa_bitset_src : public mb_mblock
254 mb_port_sptr d_cs_top;
259 long d_msg_number; // starting message number
260 long d_nmsgs_to_send; // # of messages to send
261 long d_batch_size; // # of messages to send per batch
264 qa_bitset_src(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
265 void handle_message(mb_message_sptr msg);
272 qa_bitset_src::qa_bitset_src(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
273 : mb_mblock(runtime, instance_name, user_arg)
275 d_msg_number = pmt_to_long(pmt_nth(0, user_arg));
276 d_nmsgs_to_send = pmt_to_long(pmt_nth(1, user_arg));
277 d_batch_size = pmt_to_long(pmt_nth(2, user_arg));
279 d_cs_top = define_port("cs_top", "qa-bitset-cs", true, mb_port::EXTERNAL);
280 d_cs = define_port("cs", "qa-bitset-cs", true, mb_port::EXTERNAL);
282 d_out = define_port("out", "qa-bitset", true, mb_port::EXTERNAL);
286 qa_bitset_src::handle_message(mb_message_sptr msg)
288 if ((pmt_eq(msg->port_id(), d_cs_top->port_symbol())
289 || pmt_eq(msg->port_id(), d_cs->port_symbol()))
290 && pmt_eq(msg->signal(), s_send_batch)){
296 qa_bitset_src::send_batch()
298 for (int i = 0; i < d_batch_size; i++)
303 qa_bitset_src::send_one()
305 if (d_nmsgs_to_send > 0){
306 pmt_t msg_number = pmt_from_long(d_msg_number++);
307 d_out->send(s_data, pmt_cons(msg_number, s_long0));
309 if (--d_nmsgs_to_send <= 0)
313 REGISTER_MBLOCK_CLASS(qa_bitset_src);
315 // ------------------------------------------------------------------------
317 class qa_bitset_sink : public mb_mblock
319 // Maximum number of messages we can track
320 static const size_t MAX_MSGS = 1 * 1024 * 1024;
332 long d_nmsgs_to_recv; // # of messages to receive
333 long d_batch_size; // # of messages to receive per batch
334 uint32_t d_expected_mask;
336 std::bitset<MAX_MSGS> d_bitset;
340 qa_bitset_sink(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
341 void handle_message(mb_message_sptr msg);
344 void receive_one(mb_message_sptr msg);
347 qa_bitset_sink::qa_bitset_sink(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
348 : mb_mblock(runtime, instance_name, user_arg),
351 d_nmsgs_to_recv = pmt_to_long(pmt_nth(0, user_arg));
352 d_batch_size = pmt_to_long(pmt_nth(1, user_arg));
353 d_expected_mask = pmt_to_long(pmt_nth(2, user_arg));
355 if (d_nmsgs_to_recv > (long) MAX_MSGS)
356 throw std::out_of_range("qa_bitset_sink: nmsgs_to_recv is too big");
358 if (d_batch_size < 1)
359 throw std::out_of_range("qa_bitset_sink: batch_size must be >= 1");
361 d_cs0 = define_port("cs0", "qa-bitset-cs", true, mb_port::EXTERNAL);
362 d_cs1 = define_port("cs1", "qa-bitset-cs", true, mb_port::EXTERNAL);
363 d_cs2 = define_port("cs2", "qa-bitset-cs", true, mb_port::EXTERNAL);
364 d_cs3 = define_port("cs3", "qa-bitset-cs", true, mb_port::EXTERNAL);
366 d_in0 = define_port("in0", "qa-bitset", false, mb_port::EXTERNAL);
367 d_in1 = define_port("in1", "qa-bitset", false, mb_port::EXTERNAL);
368 d_in2 = define_port("in2", "qa-bitset", false, mb_port::EXTERNAL);
369 d_in3 = define_port("in3", "qa-bitset", false, mb_port::EXTERNAL);
373 qa_bitset_sink::handle_message(mb_message_sptr msg)
375 if ((pmt_eq(msg->port_id(), d_in0->port_symbol())
376 || pmt_eq(msg->port_id(), d_in1->port_symbol())
377 || pmt_eq(msg->port_id(), d_in2->port_symbol())
378 || pmt_eq(msg->port_id(), d_in3->port_symbol()))
379 && pmt_eq(msg->signal(), s_data)){
386 qa_bitset_sink::receive_one(mb_message_sptr msg)
388 long msg_number = pmt_to_long(pmt_car(msg->data()));
389 uint32_t mask = pmt_to_long(pmt_cdr(msg->data()));
391 // std::cout << msg->data() << std::endl;
394 if (d_nrecvd % d_batch_size == d_batch_size - 1){
395 d_cs0->send(s_send_batch);
396 d_cs1->send(s_send_batch);
397 d_cs2->send(s_send_batch);
398 d_cs3->send(s_send_batch);
401 if (msg_number >= d_nmsgs_to_recv){
402 std::cerr << "qa_bitset_sink::receive_one: msg_number too big ("
403 << msg_number << ")\n";
407 if (mask != d_expected_mask){
409 "qa_bitset_sink::receive_one: Wrong mask. Expected 0x%08x, got 0x%08x\n",
410 d_expected_mask, mask);
415 if (d_bitset.test((size_t) msg_number)){
416 std::cerr << "qa_bitset_sink::receive_one: duplicate msg_number ("
417 << msg_number << ")\n";
422 d_bitset.set((size_t) msg_number);
423 if (d_nrecvd == d_nmsgs_to_recv)
424 shutdown_all(PMT_T); // we're done!
427 REGISTER_MBLOCK_CLASS(qa_bitset_sink);
429 // ------------------------------------------------------------------------
431 class qa_bitset_top : public mb_mblock
433 static const int NPIPES = 4;
435 std::vector<mb_port_sptr> d_cs;
437 long d_nmsgs; // # of messages to send
438 long d_batch_size; // # of messages to receive per batch
441 qa_bitset_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
442 void initial_transition();
445 qa_bitset_top::qa_bitset_top(mb_runtime *runtime,
446 const std::string &instance_name, pmt_t user_arg)
447 : mb_mblock(runtime, instance_name, user_arg)
449 d_nmsgs = pmt_to_long(pmt_nth(0, user_arg));
450 d_nmsgs = (d_nmsgs / NPIPES) * NPIPES;
451 d_batch_size = pmt_to_long(pmt_nth(1, user_arg));
454 * We build NPIPES sources which feed NPIPES pipelines, each of which
455 * consists of 8-mblocks. All pipelines feed into a single sink
456 * which keeps track the results.
458 for (int i = 0; i < NPIPES; i++){
459 d_cs.push_back(define_port("cs"+str(i), "qa-bitset-cs", false, mb_port::INTERNAL));
461 // sources of test messages
462 define_component("src"+str(i), "qa_bitset_src",
463 pmt_list3(pmt_from_long(i * d_nmsgs/NPIPES),
464 pmt_from_long(d_nmsgs/NPIPES),
465 pmt_from_long(d_batch_size)));
467 // 8-mblock processing pipelines
468 define_component("pipeline"+str(i), "qa_bitset8", pmt_from_long(0));
471 // sink for output of pipelines
472 define_component("sink", "qa_bitset_sink",
473 pmt_list3(pmt_from_long(d_nmsgs),
474 pmt_from_long(d_batch_size * NPIPES),
475 pmt_from_long(0x000000ff)));
477 for (int i = 0; i < NPIPES; i++){
478 connect("self", "cs"+str(i), "src"+str(i), "cs_top");
479 connect("src"+str(i), "out", "pipeline"+str(i), "in");
480 connect("src"+str(i), "cs", "sink", "cs"+str(i));
481 connect("pipeline"+str(i), "out", "sink", "in"+str(i));
486 qa_bitset_top::initial_transition()
488 for (int i = 0; i < NPIPES; i++){
489 d_cs[i]->send(s_send_batch); // prime the pump
490 d_cs[i]->send(s_send_batch);
494 REGISTER_MBLOCK_CLASS(qa_bitset_top);