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