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 using namespace gruel;
36 static pmt_t s_in = pmt_intern("in");
37 static pmt_t s_out = pmt_intern("out");
38 static pmt_t s_data = pmt_intern("data");
39 static pmt_t s_ack = pmt_intern("ack");
40 static pmt_t s_select_pipe = pmt_intern("select-pipe");
41 static pmt_t s_long0 = pmt_from_long(0);
42 static pmt_t s_sys_port = pmt_intern("%sys-port");
43 static pmt_t s_shutdown = pmt_intern("%shutdown");
45 class qa_disconnect_mux : public mb_mblock
52 qa_disconnect_mux(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
53 void initial_transition();
54 void handle_message(mb_message_sptr msg);
57 qa_disconnect_mux::qa_disconnect_mux(mb_runtime *runtime,
58 const std::string &instance_name,
60 : mb_mblock(runtime, instance_name, user_arg)
62 d_in = define_port("in", "qa-bitset", false, mb_port::RELAY);
63 d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
64 d_cs = define_port("cs", "qa-disconnect-cs", true, mb_port::EXTERNAL);
66 define_component("pipeline0", "qa_bitset8", pmt_from_long(0));
67 define_component("pipeline1", "qa_bitset8", pmt_from_long(8));
71 qa_disconnect_mux::initial_transition(){}
74 qa_disconnect_mux::handle_message(mb_message_sptr msg)
76 if (pmt_eq(msg->port_id(), d_cs->port_symbol()) // select-pipe on cs
77 && pmt_eq(msg->signal(), s_select_pipe)){
79 long which_pipe = pmt_to_long(pmt_nth(0, msg->data()));
81 disconnect_component("pipeline0");
82 disconnect_component("pipeline1");
87 connect("self", "in", "pipeline0", "in");
88 connect("self", "out", "pipeline0", "out");
92 connect("self", "in", "pipeline1", "in");
93 connect("self", "out", "pipeline1", "out");
97 d_cs->send(s_ack, msg->data());
102 REGISTER_MBLOCK_CLASS(qa_disconnect_mux);
104 // ------------------------------------------------------------------------
106 class qa_disconnect_top : public mb_mblock
122 void check_pipe_send_next_msg();
123 void send_next_msg();
124 void select_pipe(int n);
126 // alternate pipes every 128 messages
127 static int which_pipe(int msg_number) { return (msg_number >> 7) & 0x1; }
128 bool time_to_switch() { return (d_msg_number & 0x7f) == 0; }
131 qa_disconnect_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
132 void initial_transition();
133 void handle_message(mb_message_sptr msg);
136 qa_disconnect_top::qa_disconnect_top(mb_runtime *runtime,
137 const std::string &instance_name,
139 : mb_mblock(runtime, instance_name, user_arg),
140 d_state(UNINITIALIZED), d_msg_number(0)
142 d_nmsgs_to_send = pmt_to_long(pmt_nth(0, user_arg));
144 d_in = define_port("in", "qa-bitset", false, mb_port::INTERNAL);
145 d_out = define_port("out", "qa-bitset", true, mb_port::INTERNAL);
146 d_cs = define_port("cs", "qa-disconnect-cs", false, mb_port::INTERNAL);
148 define_component("mux", "qa_disconnect_mux", PMT_F);
150 connect("self", "cs", "mux", "cs");
151 connect("self", "out", "mux", "in");
152 connect("self", "in", "mux", "out");
156 qa_disconnect_top::initial_transition()
158 check_pipe_send_next_msg();
162 qa_disconnect_top::handle_message(mb_message_sptr msg)
165 std::cerr << "qa_disconnect_top::handle_msg state = "
166 << d_state << "\n msg = " << msg << std::endl;
168 if (pmt_eq(msg->port_id(), d_cs->port_symbol()) // ack on cs
169 && pmt_eq(msg->signal(), s_ack)
170 && d_state == WAIT_FOR_ACK){
176 if (pmt_eq(msg->port_id(), d_in->port_symbol()) // data on in
177 && pmt_eq(msg->signal(), s_data)
178 && d_state == WAIT_FOR_DATA){
181 * Confirm that msg passed through the pipe that we expect...
183 static const long expected_mask[2] = { 0x000000ff, 0x0000ff00 };
185 long msg_number = pmt_to_long(pmt_car(msg->data()));
186 long mask = pmt_to_long(pmt_cdr(msg->data()));
188 if (mask != expected_mask[which_pipe(msg_number)]){
189 fprintf(stderr, "\nqa_disconnect_top: wrong mask in msg_number = 0x%08lx\n",
191 fprintf(stderr, " expected = 0x%08lx, actual = 0x%08lx\n",
192 expected_mask[which_pipe(msg_number)], mask);
197 if (msg_number == d_nmsgs_to_send - 1){ // we're done (and were successful)
202 check_pipe_send_next_msg();
206 if (pmt_eq(msg->port_id(), s_sys_port) // ignore %shutdown on %sys-port
207 && pmt_eq(msg->signal(), s_shutdown))
210 std::cerr << "qa_disconnect_top: unhandled msg: state = "
211 << d_state << "\n msg = " << msg << std::endl;
215 qa_disconnect_top::select_pipe(int n)
217 d_cs->send(s_select_pipe, pmt_list1(pmt_from_long(n)));
218 d_state = WAIT_FOR_ACK;
222 qa_disconnect_top::send_next_msg()
224 d_state = WAIT_FOR_DATA;
225 if (d_msg_number == d_nmsgs_to_send) // we've sent all we're supposed to
228 d_out->send(s_data, pmt_cons(pmt_from_long(d_msg_number), s_long0));
233 qa_disconnect_top::check_pipe_send_next_msg()
235 if (time_to_switch())
236 select_pipe(which_pipe(d_msg_number));
241 REGISTER_MBLOCK_CLASS(qa_disconnect_top);