summaryrefslogtreecommitdiff
path: root/mblock/src/lib/qa_disconnect.cc
blob: c7619bfc363a8ba31d42048c0fb609781763cd57 (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
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
/* -*- c++ -*- */
/*
 * Copyright 2007,2008 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mblock/mblock.h>
#include <mblock/protocol_class.h>
#include <mblock/message.h>
#include <mblock/class_registry.h>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <bitset>

using namespace pmt;

static pmt_t s_in = pmt_intern("in");
static pmt_t s_out = pmt_intern("out");
static pmt_t s_data = pmt_intern("data");
static pmt_t s_ack = pmt_intern("ack");
static pmt_t s_select_pipe = pmt_intern("select-pipe");
static pmt_t s_long0 = pmt_from_long(0);
static pmt_t s_sys_port = pmt_intern("%sys-port");
static pmt_t s_shutdown = pmt_intern("%shutdown");

class qa_disconnect_mux : public mb_mblock
{
  mb_port_sptr	d_in;
  mb_port_sptr	d_out;
  mb_port_sptr	d_cs;

public:
  qa_disconnect_mux(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
  void initial_transition();
  void handle_message(mb_message_sptr msg);
};

qa_disconnect_mux::qa_disconnect_mux(mb_runtime *runtime,
				     const std::string &instance_name,
				     pmt_t user_arg)
  : mb_mblock(runtime, instance_name, user_arg)
{
  d_in  = define_port("in", "qa-bitset", false, mb_port::RELAY);
  d_out = define_port("out", "qa-bitset", true, mb_port::RELAY);
  d_cs  = define_port("cs", "qa-disconnect-cs", true, mb_port::EXTERNAL);

  define_component("pipeline0", "qa_bitset8", pmt_from_long(0));
  define_component("pipeline1", "qa_bitset8", pmt_from_long(8));
}

void
qa_disconnect_mux::initial_transition(){}

void
qa_disconnect_mux::handle_message(mb_message_sptr msg)
{
  if (pmt_eq(msg->port_id(), d_cs->port_symbol())	// select-pipe on cs
      && pmt_eq(msg->signal(), s_select_pipe)){		

    long which_pipe = pmt_to_long(pmt_nth(0, msg->data()));

    disconnect_component("pipeline0");
    disconnect_component("pipeline1");

    switch(which_pipe){

    case 0:
      connect("self", "in",  "pipeline0", "in");
      connect("self", "out", "pipeline0", "out");
      break;

    case 1:
      connect("self", "in",  "pipeline1", "in");
      connect("self", "out", "pipeline1", "out");
      break;
    }

    d_cs->send(s_ack, msg->data());
    return;
  }
}

REGISTER_MBLOCK_CLASS(qa_disconnect_mux);

// ------------------------------------------------------------------------

class qa_disconnect_top : public mb_mblock
{
  enum state_t {
    UNINITIALIZED,
    WAIT_FOR_ACK,
    WAIT_FOR_DATA
  };

  state_t	d_state;
  int		d_msg_number;
  int		d_nmsgs_to_send;

  mb_port_sptr	d_in;
  mb_port_sptr	d_out;
  mb_port_sptr	d_cs;

  void check_pipe_send_next_msg();
  void send_next_msg();
  void select_pipe(int n);

  // alternate pipes every 128 messages
  static int  which_pipe(int msg_number) { return (msg_number >> 7) & 0x1; }
  bool time_to_switch() { return (d_msg_number & 0x7f) == 0; }
  
public:
  qa_disconnect_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
  void initial_transition();
  void handle_message(mb_message_sptr msg);
};

qa_disconnect_top::qa_disconnect_top(mb_runtime *runtime,
				     const std::string &instance_name,
				     pmt_t user_arg)
  : mb_mblock(runtime, instance_name, user_arg),
    d_state(UNINITIALIZED), d_msg_number(0)
{
  d_nmsgs_to_send = pmt_to_long(pmt_nth(0, user_arg));

  d_in  = define_port("in", "qa-bitset", false, mb_port::INTERNAL);
  d_out = define_port("out", "qa-bitset", true, mb_port::INTERNAL);
  d_cs  = define_port("cs", "qa-disconnect-cs", false, mb_port::INTERNAL);

  define_component("mux", "qa_disconnect_mux", PMT_F);

  connect("self", "cs",  "mux", "cs");
  connect("self", "out", "mux", "in");
  connect("self", "in",  "mux", "out");
}

void
qa_disconnect_top::initial_transition()
{
  check_pipe_send_next_msg();
}

void
qa_disconnect_top::handle_message(mb_message_sptr msg)
{
  if (0)
    std::cerr << "qa_disconnect_top::handle_msg state = "
	      << d_state << "\n  msg = " << msg << std::endl;

  if (pmt_eq(msg->port_id(), d_cs->port_symbol())	// ack on cs
      && pmt_eq(msg->signal(), s_ack)
      && d_state == WAIT_FOR_ACK){

    send_next_msg();
    return;
  }

  if (pmt_eq(msg->port_id(), d_in->port_symbol())	// data on in
      && pmt_eq(msg->signal(), s_data)
      && d_state == WAIT_FOR_DATA){

    /*	
     * Confirm that msg passed through the pipe that we expect...
     */
    static const long expected_mask[2] = { 0x000000ff, 0x0000ff00 };

    long msg_number = pmt_to_long(pmt_car(msg->data()));
    long mask = pmt_to_long(pmt_cdr(msg->data()));

    if (mask != expected_mask[which_pipe(msg_number)]){
      fprintf(stderr, "\nqa_disconnect_top: wrong mask in msg_number = 0x%08lx\n",
	      msg_number);
      fprintf(stderr, "  expected = 0x%08lx, actual = 0x%08lx\n",
	      expected_mask[which_pipe(msg_number)], mask);
      shutdown_all(PMT_F);
      return;
    }

    if (msg_number == d_nmsgs_to_send - 1){	// we're done (and were successful)
      shutdown_all(PMT_T);
      return;
    }

    check_pipe_send_next_msg();
    return;
  }

  if (pmt_eq(msg->port_id(), s_sys_port)	// ignore %shutdown on %sys-port
      && pmt_eq(msg->signal(), s_shutdown))
    return;

  std::cerr << "qa_disconnect_top: unhandled msg: state = "
	    << d_state << "\n  msg = " << msg << std::endl;
}

void
qa_disconnect_top::select_pipe(int n)
{
  d_cs->send(s_select_pipe, pmt_list1(pmt_from_long(n)));
  d_state = WAIT_FOR_ACK;
}

void
qa_disconnect_top::send_next_msg()
{
  d_state = WAIT_FOR_DATA;
  if (d_msg_number == d_nmsgs_to_send)	// we've sent all we're supposed to
    return;

  d_out->send(s_data, pmt_cons(pmt_from_long(d_msg_number), s_long0));
  d_msg_number++;
}

void
qa_disconnect_top::check_pipe_send_next_msg()
{
  if (time_to_switch())
    select_pipe(which_pipe(d_msg_number));
  else
    send_next_msg();
}

REGISTER_MBLOCK_CLASS(qa_disconnect_top);