Merged eb/usrp-la r6317:6320 into trunk. This deborks the dependency
[debian/gnuradio] / usrp / host / apps-inband / test_usrp_inband_registers.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 <stdio.h>
38 #include <string.h>
39 #include <sys/time.h>
40 #include <iostream>
41
42 // Include the symbols needed for communication with USRP server
43 #include <symbols_usrp_server_cs.h>
44 #include <symbols_usrp_channel.h>
45 #include <symbols_usrp_low_level_cs.h>
46 #include <symbols_usrp_tx.h>
47 #include <symbols_usrp_rx.h>
48
49 static bool verbose = true;
50
51 class test_usrp_inband_registers : public mb_mblock
52 {
53
54   mb_port_sptr  d_tx;   // Ports connected to the USRP server
55   mb_port_sptr  d_rx;
56   mb_port_sptr  d_cs;
57
58   pmt_t   d_tx_chan;    // Returned channel from TX allocation
59   pmt_t   d_rx_chan;    // Returned channel from RX allocation
60
61   pmt_t   d_which_usrp; // The USRP to use for the test
62
63   long    d_warm_msgs;  // The number of messages to 'warm' the USRP
64   long    d_warm_recvd; // The number of msgs received in the 'warm' state
65
66   // Keep track of current state
67   enum state_t {
68     INIT,
69     OPENING_USRP,
70     ALLOCATING_CHANNELS,
71     WRITE_REGISTER,
72     READ_REGISTER,
73     CLOSING_CHANNELS,
74     CLOSING_USRP,
75   };
76   state_t d_state;
77
78  public:
79   test_usrp_inband_registers(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
80   ~test_usrp_inband_registers();
81   void initial_transition();
82   void handle_message(mb_message_sptr msg);
83
84  protected:
85   void opening_usrp();
86   void allocating_channels();
87   void write_register();
88   void read_register();
89   void closing_channels();
90   void closing_usrp();
91   void enter_receiving();
92   void build_and_send_ping();
93 };
94
95
96 int
97 main (int argc, char **argv)
98 {
99   // handle any command line args here
100
101   mb_runtime_sptr rt = mb_make_runtime();
102   pmt_t result = PMT_NIL;
103
104   rt->run("top", "test_usrp_inband_registers", PMT_F, &result);
105 }
106
107
108 test_usrp_inband_registers::test_usrp_inband_registers(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
109   : mb_mblock(runtime, instance_name, user_arg),
110   d_tx_chan(PMT_NIL),
111   d_rx_chan(PMT_NIL),
112   d_which_usrp(pmt_from_long(0)),
113   d_state(INIT)
114 {
115   
116   // A dictionary is used to pass parameters to the USRP
117   pmt_t usrp_dict = pmt_make_dict();
118
119   // Specify the RBF to use
120   pmt_dict_set(usrp_dict,
121                pmt_intern("rbf"),
122                pmt_intern("boe2.rbf"));
123
124   // Set TX and RX interpolations
125   pmt_dict_set(usrp_dict,
126                pmt_intern("interp-tx"),
127                pmt_from_long(128));
128
129   pmt_dict_set(usrp_dict,
130                pmt_intern("interp-rx"),
131                pmt_from_long(16));
132   
133   d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
134   d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
135   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
136
137   // Create an instance of USRP server and connect ports
138   define_component("server", "usrp_server", usrp_dict);
139   connect("self", "tx0", "server", "tx0");
140   connect("self", "rx0", "server", "rx0");
141   connect("self", "cs", "server", "cs");
142
143 }
144
145 test_usrp_inband_registers::~test_usrp_inband_registers()
146 {
147 }
148
149 void
150 test_usrp_inband_registers::initial_transition()
151 {
152   opening_usrp();
153 }
154
155 // Handle message reads all incoming messages from USRP server which will be
156 // initialization and ping responses.  We perform actions based on the current
157 // state and the event (ie, ping response)
158 void
159 test_usrp_inband_registers::handle_message(mb_message_sptr msg)
160 {
161   pmt_t event = msg->signal();
162   pmt_t data = msg->data();
163   pmt_t port_id = msg->port_id();
164
165   pmt_t handle = PMT_F;
166   pmt_t status = PMT_F;
167   std::string error_msg;
168
169   // Dispatch based on state
170   switch(d_state) {
171
172     //----------------------------- OPENING_USRP ----------------------------//
173     // We only expect a response from opening the USRP which should be succesful
174     // or failed.
175     case OPENING_USRP:
176       
177       if(pmt_eq(event, s_response_open)) {
178
179         status = pmt_nth(1, data);          // failed/succes
180         
181         if(pmt_eq(status, PMT_T)) {
182           allocating_channels();
183           return;
184         }
185         else {
186           error_msg = "failed to open usrp:";
187           goto bail;
188         }
189
190       }
191
192       goto unhandled;   // all other messages not handled in this state
193       
194     
195     //----------------------- ALLOCATING CHANNELS --------------------//
196     // When allocating channels, we need to wait for 2 responses from
197     // USRP server: one for TX and one for RX.  Both are initialized to
198     // NIL so we know to continue to the next state once both are set.
199     case ALLOCATING_CHANNELS:
200
201       // A TX allocation response
202       if(pmt_eq(event, s_response_allocate_channel)
203           && pmt_eq(d_tx->port_symbol(), port_id)) 
204       {
205         status = pmt_nth(1, data);
206         
207         // If successful response, extract the channel
208         if(pmt_eq(status, PMT_T)) {
209           
210           d_tx_chan = pmt_nth(2, data);
211
212           if(verbose)
213             std::cout << "[TEST_USRP_INBAND_PING] Received TX allocation"
214                       << " on channel " << d_tx_chan << std::endl;
215
216           // If the RX has also been allocated already, we can continue
217           if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
218             enter_receiving();
219             write_register();
220           }
221
222           return;
223         }
224         else {  // TX allocation failed
225           error_msg = "failed to allocate TX channel:";
226           goto bail;
227         }
228       }
229       
230       // A RX allocation response
231       if(pmt_eq(event, s_response_allocate_channel)
232           && pmt_eq(d_rx->port_symbol(), port_id)) 
233       {
234         status = pmt_nth(1, data);
235         
236         // If successful response, extract the channel
237         if(pmt_eq(status, PMT_T)) {
238           
239           d_rx_chan = pmt_nth(2, data);
240
241           if(verbose)
242             std::cout << "[TEST_USRP_INBAND_PING] Received RX allocation"
243                       << " on channel " << d_rx_chan << std::endl;
244
245           // If the TX has also been allocated already, we can continue
246           if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
247             enter_receiving();
248             write_register();
249           }
250
251           return;
252         }
253         else {  // RX allocation failed
254           error_msg = "failed to allocate RX channel:";
255           goto bail;
256         }
257       }
258
259       goto unhandled;
260
261     //-------------------------- WRITE REGISTER ----------------------------//
262     // In the write register state, we do not expect to receive any messages
263     // since the write does not directly generate a response until the USRP
264     // responds.
265     case WRITE_REGISTER:
266       goto unhandled;
267
268     //-------------------------- READ REGISTER ----------------------------//
269     // In the read register state, we only expect a read register response back
270     // that has the value we expect to have in it.  We read the response, ensure
271     // that the read was successful and display the register value.
272     case READ_REGISTER:
273
274       if(pmt_eq(event, s_response_from_control_channel)
275           && pmt_eq(d_tx->port_symbol(), port_id))
276       {
277         status = pmt_nth(1, data);
278
279         // If the read was successful, we extract the subpacket information
280         if(pmt_eq(status, PMT_T)) {
281           
282           pmt_t subp = pmt_nth(2, data);      // subpacket should be the read reg reply
283
284           pmt_t subp_sig  = pmt_nth(0, subp);
285           pmt_t subp_data = pmt_nth(1, subp);
286
287           if(!pmt_eqv(subp_sig, s_op_read_reg_reply)) {
288             error_msg = "received improper subpacket when expecting reg reply.";
289             goto bail;
290           }
291
292           pmt_t rid     = pmt_nth(0, subp_data);
293           pmt_t reg_num = pmt_nth(1, subp_data);
294           pmt_t reg_val = pmt_nth(2, subp_data);
295
296           if(verbose)
297             std::cout << "[TEST_USRP_INBAND_REGISTERS] Received read reg reply "
298                       << "("
299                       << "RID: " << rid << ", " 
300                       << "Reg: " << reg_num << ", "
301                       << "Val: " << reg_val
302                       << ")\n";
303           
304           // read_register();  FIX ME STATE TRANSITION
305           return;
306
307         } else {  // bail on unsuccessful write
308           error_msg = "failed to write to register.";
309           goto bail;
310         }
311       }
312       goto unhandled;
313
314     case CLOSING_CHANNELS:
315       goto unhandled;
316
317     case CLOSING_USRP:
318       goto unhandled;
319
320     case INIT:
321       goto unhandled;
322
323   }
324  
325  // An error occured, print it, and shutdown all m-blocks
326  bail:
327   std::cerr << error_msg << data
328             << "status = " << status << std::endl;
329   shutdown_all(PMT_F);
330   return;
331
332  // Received an unhandled message for a specific state
333  unhandled:
334   if(verbose && !pmt_eq(event, s_response_recv_raw_samples))
335     std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
336               << "in state "<< d_state << std::endl;
337
338 }
339
340
341 // Sends a command to USRP server to open up a connection to the
342 // specified USRP, which is defaulted to USRP 0 on the system
343 void
344 test_usrp_inband_registers::opening_usrp()
345 {
346
347   if(verbose)
348     std::cout << "[TEST_USRP_INBAND_PING] Opening USRP " 
349               << d_which_usrp << std::endl;
350
351   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
352   d_state = OPENING_USRP;
353 }
354
355 // RX and TX channels must be allocated so that the USRP server can
356 // properly share bandwidth across multiple USRPs.  No commands will be
357 // successful to the USRP through the USRP server on the TX or RX channels until
358 // a bandwidth allocation has been received.
359 void
360 test_usrp_inband_registers::allocating_channels()
361 {
362   d_state = ALLOCATING_CHANNELS;
363
364   long capacity = (long) 16e6;
365   d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
366   d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
367 }
368
369 // After allocating the channels, a write register command will be sent to the
370 // USRP.
371 void
372 test_usrp_inband_registers::write_register()
373 {
374   d_state = WRITE_REGISTER;
375
376   long reg = 0;
377
378   d_tx->send(s_cmd_to_control_channel,    // C/S packet
379              pmt_list2(PMT_NIL,           // invoc handle
380                        pmt_list1(
381                             pmt_list2(s_op_write_reg, 
382                                       pmt_list2(
383                                       pmt_from_long(reg), 
384                                       pmt_from_long(0xbeef))))));
385
386   if(verbose)
387     std::cout << "[TEST_USRP_INBAND_REGISTERS] Writing 0xbeef to " 
388               << reg << std::endl;
389
390   read_register();  // immediately transition to read the register
391 }
392
393 // Temporary: for testing pings
394 void
395 test_usrp_inband_registers::build_and_send_ping()
396 {
397   
398   d_tx->send(s_cmd_to_control_channel,
399              pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
400                                                     pmt_list2(pmt_from_long(0),
401                                                               pmt_from_long(0))))));
402
403   std::cout << "[TEST_USRP_INBAND_CS] Ping sent" << std::endl;
404 }
405
406 // After writing to the register, we want to read the value back and ensure that
407 // it is the same value that we wrote.
408 void
409 test_usrp_inband_registers::read_register()
410 {
411   d_state = READ_REGISTER;
412
413   long reg = 9;
414
415   d_tx->send(s_cmd_to_control_channel,    // C/S packet
416              pmt_list2(PMT_NIL,           // invoc handle
417                        pmt_list1(
418                             pmt_list2(s_op_read_reg, 
419                                       pmt_list2(
420                                       pmt_from_long(0),   // rid 
421                                       pmt_from_long(reg))))));
422   if(verbose)
423     std::cout << "[TEST_USRP_INBAND_REGISTERS] Reading from register " 
424               << reg << std::endl;
425 }
426
427 // Used to enter the receiving state
428 void
429 test_usrp_inband_registers::enter_receiving()
430 {
431   d_rx->send(s_cmd_start_recv_raw_samples,
432              pmt_list2(PMT_F,
433                        d_rx_chan));
434 }
435
436 REGISTER_MBLOCK_CLASS(test_usrp_inband_registers);