]> git.gag.com Git - debian/gnuradio/blob - usrp/host/lib/inband/test_usrp_inband.cc
distcheck fix for gr-gcell
[debian/gnuradio] / usrp / host / lib / inband / test_usrp_inband.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <iostream>
29 #include <usrp_inband_usb_packet.h>
30 #include <mb_mblock.h>
31 #include <mb_runtime.h>
32 #include <mb_protocol_class.h>
33 #include <mb_class_registry.h>
34 #include <pmt.h>
35 #include "usrp_standard.h"
36
37 typedef usrp_inband_usb_packet transport_pkt; 
38
39 // Signal set for the USRP server
40 static pmt_t s_cmd_open = pmt_intern("cmd-open");
41 static pmt_t s_response_open = pmt_intern("response-open");
42 static pmt_t s_cmd_close = pmt_intern("cmd-close");
43 static pmt_t s_response_close = pmt_intern("response-close");
44 static pmt_t s_cmd_allocate_channel = pmt_intern("cmd-allocate-channel");
45 static pmt_t s_response_allocate_channel = pmt_intern("response-allocate-channel");
46 static pmt_t s_send_allocate_channel = pmt_intern("send-allocate-channel");
47 static pmt_t s_cmd_deallocate_channel = pmt_intern("cmd-deallocate-channel");
48 static pmt_t s_response_deallocate_channel = pmt_intern("response-deallocate-channel");
49 static pmt_t s_send_deallocate_channel = pmt_intern("send-deallocate-channel");
50 static pmt_t s_cmd_max_capacity = pmt_intern("cmd-max-capacity");
51 static pmt_t s_response_max_capacity = pmt_intern("response-max-capacity");
52 static pmt_t s_cmd_ntx_chan  = pmt_intern("cmd-ntx-chan");
53 static pmt_t s_cmd_nrx_chan  = pmt_intern("cmd-nrx-chan");
54 static pmt_t s_response_ntx_chan = pmt_intern("response-ntx-chan");
55 static pmt_t s_response_nrx_chan = pmt_intern("response-nrx-chan");
56 static pmt_t s_cmd_current_capacity_allocation  = pmt_intern("cmd-current-capacity-allocation");
57 static pmt_t s_response_current_capacity_allocation  = pmt_intern("response-current-capacity-allocation");
58 static pmt_t s_cmd_xmit_raw_frame  = pmt_intern("cmd-xmit-raw-frame");
59 static pmt_t s_response_xmit_raw_frame = pmt_intern("response-xmit-raw-frame");
60
61 bool loopback_p = false;
62 bool counting_p = false;
63 bool fake_usrp_p = false;
64 char *prog_name;
65
66 static void
67 set_progname (char *path)
68 {
69   char *p = strrchr (path, '/');
70   if (p != 0)
71     prog_name = p+1;
72   else
73     prog_name = path;
74 }
75
76 static void
77 usage()
78 {
79   fprintf (stderr, "usage: %s [-l]\n", prog_name);
80   fprintf (stderr, "  [-l] digital loopback in FPGA\n");
81   fprintf (stderr, "  [-c] counting in FPGA\n");
82   fprintf (stderr, "  [-f] fake usrp\n");
83
84   exit(1);
85 }
86
87 int
88 main(int argc, char **argv)
89 {
90   int ch;
91
92   set_progname(argv[0]);
93
94   mb_runtime_sptr rt = mb_make_runtime();
95   pmt_t result = PMT_T;
96
97   while ((ch = getopt(argc, argv, "flc")) != EOF) {
98     switch(ch) {
99     
100       case 'l':
101         loopback_p = true;
102         break;
103
104       case 'c':
105         counting_p = true;
106         break;
107       
108       case 'f':
109         fake_usrp_p = true;
110         break;
111
112       default:
113         usage();
114     }
115   }
116
117
118   std::cout << "[test_usrp_inband] Starting...\n";
119
120   rt->run("top", "test_usrp_inband_top", PMT_F, &result);
121 }
122
123 class test_usrp_inband_top : public mb_mblock
124 {
125   mb_port_sptr d_tx;
126   mb_port_sptr d_cs;
127
128   long d_tx_chan;
129
130  public:
131   test_usrp_inband_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
132   ~test_usrp_inband_top();
133   void initial_transition();
134   void handle_message(mb_message_sptr msg);
135
136  protected:
137   void open_usrp();
138   void close_usrp();
139   void check_message(mb_message_sptr msg);
140   void allocate_channel();
141   void send_packets();
142 };
143
144 test_usrp_inband_top::test_usrp_inband_top(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
145   : mb_mblock(runtime, instance_name, user_arg)
146
147   std::cout << "[TEST_USRP_INBAND_TOP] Initializing...\n";
148   
149   d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
150   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
151   
152   // Test the TX side
153
154   // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
155   pmt_t usrp_server_dict = pmt_make_dict();
156
157   if(fake_usrp_p)
158     pmt_dict_set(usrp_server_dict, pmt_intern("usrp-interface"), pmt_intern("usrp_usb_interface_stub"));
159
160   define_component("server", "usrp_server", usrp_server_dict);
161   connect("self", "tx0", "server", "tx0");
162   connect("self", "cs", "server", "cs");
163 }
164
165 test_usrp_inband_top::~test_usrp_inband_top()
166 {
167 }
168
169 void
170 test_usrp_inband_top::initial_transition()
171 {
172   open_usrp();
173 }
174
175 void
176 test_usrp_inband_top::handle_message(mb_message_sptr msg)
177 {
178   pmt_t event = msg->signal();          // the "name" of the message
179   pmt_t port_id = msg->port_id();       // which port it came in on
180   pmt_t data = msg->data();
181   pmt_t metadata = msg->metadata();
182   pmt_t status;
183
184   if (pmt_eq(port_id, d_cs->port_symbol())) {   // message came in on our control/status port
185
186     //---------- OPEN RESPONSE ----------//
187     if (pmt_eq(event, s_response_open)) {
188       status = pmt_nth(1, data);
189
190       if(pmt_eq(status, PMT_T)) {
191         std::cout << "[TEST_USRP_INBAND_TOP] Success opening USRP\n";
192       }
193       else {
194         std::cout << "[TEST_USRP_INBAND_TOP] Received error message opening USRP\n";
195         shutdown_all(PMT_F);
196       }
197
198       allocate_channel();
199
200       return;
201     }
202     //--------- CLOSE RESPONSE -----------//
203     else if (pmt_eq(event, s_response_close)) {
204       status = pmt_nth(1, data);
205       
206       if(pmt_eq(status, PMT_T)) {
207         std::cout << "[TEST_USRP_INBAND_TOP] Successfully closed USRP\n";
208       }
209       else {
210         std::cout << "[TEST_USRP_INBAND_TOP] Received error message closing USRP\n";
211         shutdown_all(PMT_F);
212       }
213
214       shutdown_all(PMT_T);
215
216       return;
217     }
218   }
219     
220   if (pmt_eq(port_id, d_tx->port_symbol())) {
221
222     //---------- ALLOCATE RESPONSE ---------//
223     if(pmt_eq(event, s_response_allocate_channel)) {
224       status = pmt_nth(1, data);
225       pmt_t channel = pmt_nth(2, data);
226
227       if(pmt_eq(status, PMT_T)) {
228         d_tx_chan = pmt_to_long(channel);
229         std::cout << "[TEST_USRP_INBAND_TOP] Received allocation on channel " << d_tx_chan << "\n";
230       }
231       else {
232         std::cout << "[TEST_USRP_INBAND_TOP] Error allocating channel\n";
233         shutdown_all(PMT_F);
234       }
235       
236       send_packets();
237
238       return;
239     }
240     //----------- XMIT RESPONSE ------------//
241     else if(pmt_eq(event, s_response_xmit_raw_frame)) {
242       status = pmt_nth(1, data);
243
244       if(pmt_eq(status, PMT_T)) {
245         std::cout << "[TEST_USRP_INBAND_TOP] Transmission successful\n";
246       }
247       else {
248         std::cout << "[TEST_USRP_INBAND_TOP] Failed transmission\n";
249         shutdown_all(PMT_F);
250       }
251
252       close_usrp();
253
254       return;
255     }
256   }
257
258   std::cout << "[TEST_USRP_INBAND_TOP] Received unhandled message: " << event << "\n";
259 }
260
261 void
262 test_usrp_inband_top::allocate_channel()
263 {
264   std::cout << "[TEST_USRP_INBAND_TOP] Requesting channel allocation...\n";
265   
266   d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(1)));
267 }
268
269 void
270 test_usrp_inband_top::send_packets()
271 {
272   std::cout << "[TEST_USRP_INBAND_TOP] Sending single packet..\n";
273   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)));
274   
275 }
276
277 void
278 test_usrp_inband_top::open_usrp()
279 {
280   pmt_t usrp = pmt_from_long(0);
281
282   long rx_mode = 0;
283
284   if(loopback_p)
285     rx_mode |= usrp_standard_rx::FPGA_MODE_LOOPBACK;
286   if(counting_p)
287     rx_mode |= usrp_standard_rx::FPGA_MODE_COUNTING;
288
289   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, usrp));
290 }
291
292 void
293 test_usrp_inband_top::close_usrp()
294 {
295   d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
296 }
297
298 REGISTER_MBLOCK_CLASS(test_usrp_inband_top);