Merged r7478:7608 from michaelld/t186 into trunk. Adds ability to compile GNU Radio...
[debian/gnuradio] / usrp / host / apps-inband / test_usrp_inband_cs.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 <mb_mblock.h>
27 #include <mb_runtime.h>
28 #include <mb_protocol_class.h>
29 #include <mb_exception.h>
30 #include <mb_msg_queue.h>
31 #include <mb_message.h>
32 #include <mb_msg_accepter.h>
33 #include <mb_class_registry.h>
34 #include <pmt.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include <iostream>
39
40 #include <ui_nco.h>
41 #include <symbols_usrp_server_cs.h>
42 #include <symbols_usrp_channel.h>
43 #include <symbols_usrp_low_level_cs.h>
44 #include <symbols_usrp_tx.h>
45 #include <symbols_usrp_rx.h>
46
47 #define NBPING  10
48
49 static bool verbose = false;
50
51 class test_usrp_cs : public mb_mblock
52 {
53   mb_port_sptr  d_tx;
54   mb_port_sptr  d_rx;
55   mb_port_sptr  d_cs;
56   pmt_t         d_tx_chan;      // returned tx channel handle
57   pmt_t         d_rx_chan;      // returned tx channel handle
58
59   struct timeval times[NBPING];
60
61   enum state_t {
62     INIT,
63     OPENING_USRP,
64     ALLOCATING_CHANNEL,
65     TRANSMITTING,
66     CLOSING_CHANNEL,
67     CLOSING_USRP,
68   };
69
70   state_t       d_state;
71   long          d_nsamples_to_send;
72   long          d_nsamples_xmitted;
73   long          d_nframes_xmitted;
74   long          d_samples_per_frame;
75   bool          d_done_sending;
76
77   // for generating sine wave output
78   ui_nco<float,float>   d_nco;
79   double                d_amplitude;
80
81  public:
82   test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
83   ~test_usrp_cs();
84   void initial_transition();
85   void handle_message(mb_message_sptr msg);
86
87  protected:
88   void open_usrp();
89   void close_usrp();
90   void allocate_channel();
91   void send_packets();
92   void enter_receiving();
93   void enter_transmitting();
94   void build_and_send_ping();
95   void build_and_send_next_frame();
96   void handle_xmit_response(pmt_t invocation_handle);
97   void enter_closing_channel();
98 };
99
100 test_usrp_cs::test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
101   : mb_mblock(runtime, instance_name, user_arg),
102     d_tx_chan(PMT_NIL),
103     d_rx_chan(PMT_NIL),
104     d_state(INIT), d_nsamples_to_send((long) 40e6),
105     d_nsamples_xmitted(0),
106     d_nframes_xmitted(0),
107     //d_samples_per_frame((long)(126)),
108     //d_samples_per_frame((long)(126 * 3.5)),   // non-full packet
109     d_samples_per_frame((long)(126 * 4)),       // full packet
110     d_done_sending(false),
111     d_amplitude(16384)
112
113   if(verbose)
114     std::cout << "[TEST_USRP_INBAND_CS] Initializing...\n";
115   
116   d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
117   d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
118   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
119   
120   //bool fake_usrp_p = true;
121   bool fake_usrp_p = false;
122
123   // Test the TX side
124
125   pmt_t usrp_dict = pmt_make_dict();
126
127   if(fake_usrp_p) {
128     pmt_dict_set(usrp_dict, 
129                  pmt_intern("fake-usrp"),
130                              PMT_T);
131   }
132
133   // Set TX and RX interpolations
134   pmt_dict_set(usrp_dict,
135                pmt_intern("interp-tx"),
136                pmt_from_long(128));
137
138   pmt_dict_set(usrp_dict,
139                pmt_intern("decim-rx"),
140                pmt_from_long(16));
141
142   // Specify the RBF to use
143   pmt_dict_set(usrp_dict,
144                pmt_intern("rbf"),
145                pmt_intern("boe.rbf"));
146
147   define_component("server", "usrp_server", usrp_dict);
148
149   connect("self", "tx0", "server", "tx0");
150   connect("self", "rx0", "server", "rx0");
151   connect("self", "cs", "server", "cs");
152
153   // initialize NCO
154   double freq = 100e3;
155   int interp = 32;                          // 32 -> 4MS/s
156   double sample_rate = 128e6 / interp;  
157   d_nco.set_freq(2*M_PI * freq/sample_rate);
158
159   // FIXME need to somehow set the interp rate in the USRP.
160   // for now, we'll have the low-level code hardwire it.
161 }
162
163 test_usrp_cs::~test_usrp_cs()
164 {
165 }
166
167 void
168 test_usrp_cs::initial_transition()
169 {
170   open_usrp();
171 }
172
173 void
174 test_usrp_cs::handle_message(mb_message_sptr msg)
175 {
176   pmt_t event = msg->signal();
177   pmt_t data = msg->data();
178   pmt_t port_id = msg->port_id();
179
180   pmt_t handle = PMT_F;
181   pmt_t status = PMT_F;
182   std::string error_msg;
183   
184   //std::cout << msg << std::endl;
185
186   switch(d_state){
187   case OPENING_USRP:
188     if (pmt_eq(event, s_response_open)){
189       status = pmt_nth(1, data);
190       if (pmt_eq(status, PMT_T)){
191         allocate_channel();
192         return;
193       }
194       else {
195         error_msg = "failed to open usrp:";
196         goto bail;
197       }
198     }
199     goto unhandled;
200     
201   case ALLOCATING_CHANNEL:
202     if (pmt_eq(event, s_response_allocate_channel)){
203
204       if(pmt_eq(d_tx->port_symbol(), port_id)) {
205         status = pmt_nth(1, data);
206         d_tx_chan = pmt_nth(2, data);
207
208         if (pmt_eq(status, PMT_T)){
209
210           if(verbose)
211             std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
212
213           if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
214             enter_receiving();
215             enter_transmitting();
216           }
217           return;
218         }
219         else {
220           error_msg = "failed to allocate channel:";
221           goto bail;
222         }
223       }
224       
225       if(pmt_eq(d_rx->port_symbol(), port_id)) {
226         status = pmt_nth(1, data);
227         d_rx_chan = pmt_nth(2, data);
228
229         if (pmt_eq(status, PMT_T)){
230
231           if(verbose)
232             std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
233           
234           if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
235             enter_receiving();
236             enter_transmitting();
237           }
238           return;
239         }
240         else {
241           error_msg = "failed to allocate channel:";
242           goto bail;
243         }
244       }
245     }
246     goto unhandled;
247
248   case TRANSMITTING:
249     if (pmt_eq(event, s_response_xmit_raw_frame)){
250       handle = pmt_nth(0, data);
251       status = pmt_nth(1, data);
252
253       if (pmt_eq(status, PMT_T)){
254         handle_xmit_response(handle);
255         return;
256       }
257       else {
258         error_msg = "bad response-xmit-raw-frame:";
259         goto bail;
260       }
261     }
262     goto unhandled;
263
264   case CLOSING_CHANNEL:
265     if (pmt_eq(event, s_response_deallocate_channel)){
266       status = pmt_nth(1, data);
267
268       if (pmt_eq(status, PMT_T)){
269         close_usrp();
270         return;
271       }
272       else {
273         error_msg = "failed to deallocate channel:";
274         goto bail;
275       }
276     }
277     goto unhandled;
278
279   case CLOSING_USRP:
280     if (pmt_eq(event, s_response_close)){
281       status = pmt_nth(1, data);
282
283       if (pmt_eq(status, PMT_T)){
284         shutdown_all(PMT_T);
285         return;
286       }
287       else {
288         error_msg = "failed to close USRP:";
289         goto bail;
290       }
291     }
292     goto unhandled;
293
294   default:
295     goto unhandled;
296   }
297   return;
298
299  bail:
300   std::cerr << error_msg << data
301             << "status = " << status << std::endl;
302   shutdown_all(PMT_F);
303   return;
304
305  unhandled:
306   if(verbose)
307     std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
308               << "in state "<< d_state << std::endl;
309 }
310
311
312 void
313 test_usrp_cs::open_usrp()
314 {
315   pmt_t which_usrp = pmt_from_long(0);
316
317   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
318   d_state = OPENING_USRP;
319 }
320
321 void
322 test_usrp_cs::close_usrp()
323 {
324   d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
325   d_state = CLOSING_USRP;
326
327   if(verbose)
328     std::cout << "[TEST_USRP_INBAND_CS] Closing USRP\n";
329 }
330
331 void
332 test_usrp_cs::allocate_channel()
333 {
334   long capacity = (long) 16e6;
335   d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
336   d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
337   d_state = ALLOCATING_CHANNEL;
338 }
339
340 void
341 test_usrp_cs::enter_receiving()
342 {
343   d_rx->send(s_cmd_start_recv_raw_samples,
344              pmt_list2(PMT_F,
345                        d_rx_chan));
346 }
347
348 void
349 test_usrp_cs::enter_transmitting()
350 {
351   d_state = TRANSMITTING;
352   d_nsamples_xmitted = 0;
353
354   if(verbose)
355     std::cout << "[TEST_USRP_INBAND_CS] Beginning transmission\n";
356
357   sleep(1);
358
359 //  build_and_send_next_frame();        // fire off 4 to start pipeline
360
361   build_and_send_ping();
362   build_and_send_ping();
363   build_and_send_ping();
364   
365 }
366
367 void
368 test_usrp_cs::build_and_send_ping()
369 {
370   
371   d_tx->send(s_cmd_to_control_channel,
372              pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
373                                                     pmt_list2(pmt_from_long(0),
374                                                               pmt_from_long(0))))));
375
376   std::cout << "[TEST_USRP_INBAND_CS] Ping sent" << std::endl;
377 }
378
379 void
380 test_usrp_cs::build_and_send_next_frame()
381 {
382   // allocate the uniform vector for the samples
383   // FIXME perhaps hold on to this between calls
384
385 #if 1
386   long nsamples_this_frame =
387     std::min(d_nsamples_to_send - d_nsamples_xmitted,
388              d_samples_per_frame);
389 #else
390   long nsamples_this_frame = d_samples_per_frame;
391 #endif
392
393   if (nsamples_this_frame == 0){
394     d_done_sending = true;
395     return;
396   }
397     
398
399   size_t nshorts = 2 * nsamples_this_frame;     // 16-bit I & Q
400   pmt_t uvec = pmt_make_s16vector(nshorts, 0);
401   size_t ignore;
402   int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
403
404   // fill in the complex sinusoid
405
406   for (int i = 0; i < nsamples_this_frame; i++){
407
408     if (1){
409       gr_complex s;
410       d_nco.sincos(&s, 1, d_amplitude);
411       // write 16-bit i & q
412       samples[2*i] =   (int16_t) s.real();
413       samples[2*i+1] = (int16_t) s.imag();
414     }
415     else {
416       gr_complex s(d_amplitude, d_amplitude);
417
418       // write 16-bit i & q
419       samples[2*i] =   (int16_t) s.real();
420       samples[2*i+1] = (int16_t) s.imag();
421     }
422   }
423
424   pmt_t timestamp = pmt_from_long(0xffffffff);  // NOW
425   d_tx->send(s_cmd_xmit_raw_frame,
426              pmt_list4(pmt_from_long(d_nframes_xmitted),  // invocation-handle
427                        d_tx_chan,                         // channel
428                        uvec,                              // the samples
429                        timestamp));
430
431   d_nsamples_xmitted += nsamples_this_frame;
432   d_nframes_xmitted++;
433
434   if(verbose)
435     std::cout << "[TEST_USRP_INBAND_CS] Transmitted frame\n";
436 }
437
438
439 void
440 test_usrp_cs::handle_xmit_response(pmt_t handle)
441 {
442   if (d_done_sending &&
443       pmt_to_long(handle) == (d_nframes_xmitted - 1)){
444     // We're done sending and have received all responses
445     enter_closing_channel();
446   }
447
448   //build_and_send_next_frame();
449 }
450
451 void
452 test_usrp_cs::enter_closing_channel()
453 {
454   d_state = CLOSING_CHANNEL;
455   
456   d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
457
458   if(verbose)
459     std::cout << "[TEST_USRP_INBAND_CS] Closing channel\n";
460 }
461
462 REGISTER_MBLOCK_CLASS(test_usrp_cs);
463
464
465 // ----------------------------------------------------------------
466
467 int
468 main (int argc, char **argv)
469 {
470   // handle any command line args here
471
472   mb_runtime_sptr rt = mb_make_runtime();
473   pmt_t result = PMT_NIL;
474
475   rt->run("top", "test_usrp_cs", PMT_F, &result);
476 }