Once and for all, here is the properly updated Makefile.am for the apps
[debian/gnuradio] / usrp / limbo / apps-inband / test_usrp_inband_tx.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 <mblock/mblock.h>
27 #include <mblock/runtime.h>
28 #include <mblock/protocol_class.h>
29 #include <mblock/exception.h>
30 #include <mblock/msg_queue.h>
31 #include <mblock/message.h>
32 #include <mblock/msg_accepter.h>
33 #include <mblock/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 //  If unspecified, chooses center frequency from range
128 //  pmt_dict_set(usrp_dict,
129 //               pmt_intern("rf-freq"),
130 //               pmt_from_long(10e6));
131
132   define_component("server", "usrp_server", usrp_dict);
133
134   connect("self", "tx0", "server", "tx0");
135   connect("self", "cs", "server", "cs");
136
137   // initialize NCO
138   double freq = 100e3;
139   int interp = 32;                          // 32 -> 4MS/s
140   double sample_rate = 128e6 / interp;  
141   d_nco.set_freq(2*M_PI * freq/sample_rate);
142
143   // FIXME need to somehow set the interp rate in the USRP.
144   // for now, we'll have the low-level code hardwire it.
145 }
146
147 test_usrp_tx::~test_usrp_tx()
148 {
149 }
150
151 void
152 test_usrp_tx::initial_transition()
153 {
154   open_usrp();
155 }
156
157 void
158 test_usrp_tx::handle_message(mb_message_sptr msg)
159 {
160   pmt_t event = msg->signal();
161   pmt_t data = msg->data();
162
163   pmt_t handle = PMT_F;
164   pmt_t status = PMT_F;
165   std::string error_msg;
166   
167   //std::cout << msg << std::endl;
168
169   switch(d_state){
170   case OPENING_USRP:
171     if (pmt_eq(event, s_response_open)){
172       status = pmt_nth(1, data);
173       if (pmt_eq(status, PMT_T)){
174         allocate_channel();
175         return;
176       }
177       else {
178         error_msg = "failed to open usrp:";
179         goto bail;
180       }
181     }
182     goto unhandled;
183     
184   case ALLOCATING_CHANNEL:
185     if (pmt_eq(event, s_response_allocate_channel)){
186       status = pmt_nth(1, data);
187       d_tx_chan = pmt_nth(2, data);
188
189       if (pmt_eq(status, PMT_T)){
190         enter_transmitting();
191         return;
192       }
193       else {
194         error_msg = "failed to allocate channel:";
195         goto bail;
196       }
197     }
198     goto unhandled;
199
200   case TRANSMITTING:
201     if (pmt_eq(event, s_response_xmit_raw_frame)){
202       handle = pmt_nth(0, data);
203       status = pmt_nth(1, data);
204
205       if (pmt_eq(status, PMT_T)){
206         handle_xmit_response(handle);
207         return;
208       }
209       else {
210         error_msg = "bad response-xmit-raw-frame:";
211         goto bail;
212       }
213     }
214     goto unhandled;
215
216   case CLOSING_CHANNEL:
217     if (pmt_eq(event, s_response_deallocate_channel)){
218       status = pmt_nth(1, data);
219
220       if (pmt_eq(status, PMT_T)){
221         close_usrp();
222         return;
223       }
224       else {
225         error_msg = "failed to deallocate channel:";
226         goto bail;
227       }
228     }
229     goto unhandled;
230
231   case CLOSING_USRP:
232     if (pmt_eq(event, s_response_close)){
233       status = pmt_nth(1, data);
234
235       if (pmt_eq(status, PMT_T)){
236         shutdown_all(PMT_T);
237         return;
238       }
239       else {
240         error_msg = "failed to close USRP:";
241         goto bail;
242       }
243     }
244     goto unhandled;
245
246   default:
247     goto unhandled;
248   }
249   return;
250
251  bail:
252   std::cerr << error_msg << data
253             << "status = " << status << std::endl;
254   shutdown_all(PMT_F);
255   return;
256
257  unhandled:
258   std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
259             << "in state "<< d_state << std::endl;
260 }
261
262
263 void
264 test_usrp_tx::open_usrp()
265 {
266   pmt_t which_usrp = pmt_from_long(0);
267
268   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
269   d_state = OPENING_USRP;
270   
271   if(verbose)
272     std::cout << "[TEST_USRP_INBAND_TX] Opening the USRP\n";
273 }
274
275 void
276 test_usrp_tx::close_usrp()
277 {
278   d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
279   d_state = CLOSING_USRP;
280   
281   if(verbose)
282     std::cout << "[TEST_USRP_INBAND_TX] Closing the USRP\n";
283 }
284
285 void
286 test_usrp_tx::allocate_channel()
287 {
288   long capacity = (long) 16e6;
289   d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
290   d_state = ALLOCATING_CHANNEL;
291   
292   if(verbose)
293     std::cout << "[TEST_USRP_INBAND_TX] Requesting TX channel allocation\n";
294 }
295
296 void
297 test_usrp_tx::enter_transmitting()
298 {
299   d_state = TRANSMITTING;
300   d_nsamples_xmitted = 0;
301   
302   if(verbose)
303     std::cout << "[TEST_USRP_INBAND_TX] Transmitting...\n";
304   
305   build_and_send_next_frame();  // fire off 4 to start pipeline
306   build_and_send_next_frame();
307   build_and_send_next_frame();
308   build_and_send_next_frame();
309 }
310
311 void
312 test_usrp_tx::build_and_send_next_frame()
313 {
314   // allocate the uniform vector for the samples
315   // FIXME perhaps hold on to this between calls
316
317 #if 1
318   long nsamples_this_frame =
319     std::min(d_nsamples_to_send - d_nsamples_xmitted,
320              d_samples_per_frame);
321 #else
322   long nsamples_this_frame = d_samples_per_frame;
323 #endif
324
325   if (nsamples_this_frame == 0){
326     d_done_sending = true;
327     return;
328   }
329     
330
331   size_t nshorts = 2 * nsamples_this_frame;     // 16-bit I & Q
332   pmt_t uvec = pmt_make_s16vector(nshorts, 0);
333   size_t ignore;
334   int16_t *samples = pmt_s16vector_writable_elements(uvec, ignore);
335
336   // fill in the complex sinusoid
337
338   for (int i = 0; i < nsamples_this_frame; i++){
339
340     if (1){
341       gr_complex s;
342       d_nco.sincos(&s, 1, d_amplitude);
343       // write 16-bit i & q
344       samples[2*i] =   (int16_t) s.real();
345       samples[2*i+1] = (int16_t) s.imag();
346     }
347     else {
348       gr_complex s(d_amplitude, d_amplitude);
349
350       // write 16-bit i & q
351       samples[2*i] =   (int16_t) s.real();
352       samples[2*i+1] = (int16_t) s.imag();
353     }
354   }
355
356   pmt_t tx_properties = pmt_make_dict();
357
358   pmt_t timestamp = pmt_from_long(0xffffffff);  // NOW
359   d_tx->send(s_cmd_xmit_raw_frame,
360              pmt_list5(pmt_from_long(d_nframes_xmitted),  // invocation-handle
361                        d_tx_chan,                         // channel
362                        uvec,                              // the samples
363                        timestamp,
364            tx_properties));
365
366   d_nsamples_xmitted += nsamples_this_frame;
367   d_nframes_xmitted++;
368
369   if(verbose && 0)
370     std::cout << "[TEST_USRP_INBAND_TX] Transmitted frame\n";
371 }
372
373
374 void
375 test_usrp_tx::handle_xmit_response(pmt_t handle)
376 {
377   if (d_done_sending &&
378       pmt_to_long(handle) == (d_nframes_xmitted - 1)){
379     // We're done sending and have received all responses
380     enter_closing_channel();
381   }
382
383   build_and_send_next_frame();
384 }
385
386 void
387 test_usrp_tx::enter_closing_channel()
388 {
389   d_state = CLOSING_CHANNEL;
390   
391   d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
392   
393   if(verbose)
394     std::cout << "[TEST_USRP_INBAND_tX] Deallocating TX channel\n";
395 }
396
397 REGISTER_MBLOCK_CLASS(test_usrp_tx);
398
399
400 // ----------------------------------------------------------------
401
402 int
403 main (int argc, char **argv)
404 {
405   // handle any command line args here
406
407   mb_runtime_sptr rt = mb_make_runtime();
408   pmt_t result = PMT_NIL;
409
410   rt->run("top", "test_usrp_tx", PMT_F, &result);
411 }