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