summaryrefslogtreecommitdiff
path: root/usrp/host/lib/inband/test_usrp_inband.cc
blob: dd8d46f842e0bc94b2f87aa118ab8273edbd25f1 (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
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
/* -*- c++ -*- */
/*
 * Copyright 2007 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 <stdio.h>
#include <iostream>
#include <usrp_inband_usb_packet.h>
#include <mb_mblock.h>
#include <mb_runtime.h>
#include <mb_protocol_class.h>
#include <mb_class_registry.h>
#include <pmt.h>
#include "usrp_standard.h"

typedef usrp_inband_usb_packet transport_pkt; 

// Signal set for the USRP server
static pmt_t s_cmd_open = pmt_intern("cmd-open");
static pmt_t s_response_open = pmt_intern("response-open");
static pmt_t s_cmd_close = pmt_intern("cmd-close");
static pmt_t s_response_close = pmt_intern("response-close");
static pmt_t s_cmd_allocate_channel = pmt_intern("cmd-allocate-channel");
static pmt_t s_response_allocate_channel = pmt_intern("response-allocate-channel");
static pmt_t s_send_allocate_channel = pmt_intern("send-allocate-channel");
static pmt_t s_cmd_deallocate_channel = pmt_intern("cmd-deallocate-channel");
static pmt_t s_response_deallocate_channel = pmt_intern("response-deallocate-channel");
static pmt_t s_send_deallocate_channel = pmt_intern("send-deallocate-channel");
static pmt_t s_cmd_max_capacity = pmt_intern("cmd-max-capacity");
static pmt_t s_response_max_capacity = pmt_intern("response-max-capacity");
static pmt_t s_cmd_ntx_chan  = pmt_intern("cmd-ntx-chan");
static pmt_t s_cmd_nrx_chan  = pmt_intern("cmd-nrx-chan");
static pmt_t s_response_ntx_chan = pmt_intern("response-ntx-chan");
static pmt_t s_response_nrx_chan = pmt_intern("response-nrx-chan");
static pmt_t s_cmd_current_capacity_allocation  = pmt_intern("cmd-current-capacity-allocation");
static pmt_t s_response_current_capacity_allocation  = pmt_intern("response-current-capacity-allocation");
static pmt_t s_cmd_xmit_raw_frame  = pmt_intern("cmd-xmit-raw-frame");
static pmt_t s_response_xmit_raw_frame = pmt_intern("response-xmit-raw-frame");

bool loopback_p = false;
bool counting_p = false;
bool fake_usrp_p = false;
char *prog_name;

static void
set_progname (char *path)
{
  char *p = strrchr (path, '/');
  if (p != 0)
    prog_name = p+1;
  else
    prog_name = path;
}

static void
usage()
{
  fprintf (stderr, "usage: %s [-l]\n", prog_name);
  fprintf (stderr, "  [-l] digital loopback in FPGA\n");
  fprintf (stderr, "  [-c] counting in FPGA\n");
  fprintf (stderr, "  [-f] fake usrp\n");

  exit(1);
}

int
main(int argc, char **argv)
{
  int ch;

  set_progname(argv[0]);

  mb_runtime_sptr rt = mb_make_runtime();
  pmt_t result = PMT_T;

  while ((ch = getopt(argc, argv, "flc")) != EOF) {
    switch(ch) {
    
      case 'l':
        loopback_p = true;
        break;

      case 'c':
        counting_p = true;
        break;
      
      case 'f':
        fake_usrp_p = true;
        break;

      default:
        usage();
    }
  }


  std::cout << "[test_usrp_inband] Starting...\n";

  rt->run("top", "test_usrp_inband_top", PMT_F, &result);
}

class test_usrp_inband_top : public mb_mblock
{
  mb_port_sptr d_tx;
  mb_port_sptr d_cs;

  long d_tx_chan;

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

 protected:
  void open_usrp();
  void close_usrp();
  void check_message(mb_message_sptr msg);
  void allocate_channel();
  void send_packets();
};

test_usrp_inband_top::test_usrp_inband_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
  : mb_mblock(runtime, instance_name, user_arg)
{ 
  std::cout << "[TEST_USRP_INBAND_TOP] Initializing...\n";
  
  d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
  d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
  
  // Test the TX side

  // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
  pmt_t usrp_server_dict = pmt_make_dict();

  if(fake_usrp_p)
    pmt_dict_set(usrp_server_dict, pmt_intern("usrp-interface"), pmt_intern("usrp_usb_interface_stub"));

  define_component("server", "usrp_server", usrp_server_dict);
  connect("self", "tx0", "server", "tx0");
  connect("self", "cs", "server", "cs");
}

test_usrp_inband_top::~test_usrp_inband_top()
{
}

void
test_usrp_inband_top::initial_transition()
{
  open_usrp();
}

void
test_usrp_inband_top::handle_message(mb_message_sptr msg)
{
  pmt_t event = msg->signal();		// the "name" of the message
  pmt_t port_id = msg->port_id();	// which port it came in on
  pmt_t data = msg->data();
  pmt_t metadata = msg->metadata();
  pmt_t status;

  if (pmt_eq(port_id, d_cs->port_symbol())) {	// message came in on our control/status port

    //---------- OPEN RESPONSE ----------//
    if (pmt_eq(event, s_response_open)) {
      status = pmt_nth(1, data);

      if(pmt_eq(status, PMT_T)) {
        std::cout << "[TEST_USRP_INBAND_TOP] Success opening USRP\n";
      }
      else {
        std::cout << "[TEST_USRP_INBAND_TOP] Received error message opening USRP\n";
        shutdown_all(PMT_F);
      }

      allocate_channel();

      return;
    }
    //--------- CLOSE RESPONSE -----------//
    else if (pmt_eq(event, s_response_close)) {
      status = pmt_nth(1, data);
      
      if(pmt_eq(status, PMT_T)) {
        std::cout << "[TEST_USRP_INBAND_TOP] Successfully closed USRP\n";
      }
      else {
        std::cout << "[TEST_USRP_INBAND_TOP] Received error message closing USRP\n";
        shutdown_all(PMT_F);
      }

      shutdown_all(PMT_T);

      return;
    }
  }
    
  if (pmt_eq(port_id, d_tx->port_symbol())) {

    //---------- ALLOCATE RESPONSE ---------//
    if(pmt_eq(event, s_response_allocate_channel)) {
      status = pmt_nth(1, data);
      pmt_t channel = pmt_nth(2, data);

      if(pmt_eq(status, PMT_T)) {
        d_tx_chan = pmt_to_long(channel);
        std::cout << "[TEST_USRP_INBAND_TOP] Received allocation on channel " << d_tx_chan << "\n";
      }
      else {
        std::cout << "[TEST_USRP_INBAND_TOP] Error allocating channel\n";
        shutdown_all(PMT_F);
      }
      
      send_packets();

      return;
    }
    //----------- XMIT RESPONSE ------------//
    else if(pmt_eq(event, s_response_xmit_raw_frame)) {
      status = pmt_nth(1, data);

      if(pmt_eq(status, PMT_T)) {
        std::cout << "[TEST_USRP_INBAND_TOP] Transmission successful\n";
      }
      else {
        std::cout << "[TEST_USRP_INBAND_TOP] Failed transmission\n";
        shutdown_all(PMT_F);
      }

      close_usrp();

      return;
    }
  }

  std::cout << "[TEST_USRP_INBAND_TOP] Received unhandled message: " << event << "\n";
}

void
test_usrp_inband_top::allocate_channel()
{
  std::cout << "[TEST_USRP_INBAND_TOP] Requesting channel allocation...\n";
  
  d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(1)));
}

void
test_usrp_inband_top::send_packets()
{
  std::cout << "[TEST_USRP_INBAND_TOP] Sending single packet..\n";
  d_tx->send(s_cmd_xmit_raw_frame, pmt_list4(pmt_from_long(1), pmt_from_long(d_tx_chan), pmt_make_u32vector(transport_pkt::max_payload()/4, 0), pmt_from_long(0)));
  
}

void
test_usrp_inband_top::open_usrp()
{
  pmt_t usrp = pmt_from_long(0);

  long rx_mode = 0;

  if(loopback_p)
    rx_mode |= usrp_standard_rx::FPGA_MODE_LOOPBACK;
  if(counting_p)
    rx_mode |= usrp_standard_rx::FPGA_MODE_COUNTING;

  d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, usrp));
}

void
test_usrp_inband_top::close_usrp()
{
  d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
}

REGISTER_MBLOCK_CLASS(test_usrp_inband_top);