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