Merged features/inband-usb -r6431:8293 into trunk.
[debian/gnuradio] / usrp / host / lib / inband / usrp_rx.cc
index caa2d717540db09f2d11eb805c0ee9436f2b220a..71c042a50e9f96369b8ff25882683b6d13196f44 100644 (file)
@@ -40,25 +40,30 @@ typedef usrp_inband_usb_packet transport_pkt;
 
 static const bool verbose = false;
 
+bool usrp_rx_stop;
+
 usrp_rx::usrp_rx(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
   : mb_mblock(rt, instance_name, user_arg),
-    d_disk_write(false)
+    d_disk_write(false),
+    d_disk_write_pkt(false)   // if true, writes full packet, else just the payload
 {
   d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
   
-  //d_disk_write=true;
-  
   if(d_disk_write) {
-    d_ofile.open("rx_data.dat",std::ios::binary|std::ios::out);
+    d_ofile0.open("rx_data_chan0.dat",std::ios::binary|std::ios::out);
+    d_ofile1.open("rx_data_chan1.dat",std::ios::binary|std::ios::out);
     d_cs_ofile.open("rx_cs.dat",std::ios::binary|std::ios::out);
   }
+  
+  usrp_rx_stop = false;
 
 }
 
 usrp_rx::~usrp_rx() 
 {
   if(d_disk_write) {
-    d_ofile.close();
+    d_ofile0.close();
+    d_ofile1.close();
     d_cs_ofile.close();
   }
 }
@@ -69,6 +74,12 @@ usrp_rx::initial_transition()
   
 }
 
+/*!
+ * \brief Handles incoming signals to to the m-block, wihch should only ever be
+ * a single message: cmd-usrrp-rx-start-reading.  There is no signal to stop
+ * reading as the m-block goes in to a forever loop to read inband packets from
+ * the bus.
+ */
 void
 usrp_rx::handle_message(mb_message_sptr msg)
 {
@@ -85,6 +96,17 @@ usrp_rx::handle_message(mb_message_sptr msg)
   }
 }
 
+/*!
+ * \brief Performs the actual reading of data from the USB bus, called by
+ * handle_message() when a cmd-usrp-rx-start-reading signal is received.  
+ *
+ * The method enters a forever loop where it continues to read data from the bus
+ * and generate read responses to the higher layer.  Currently, shared memory is
+ * used to exit this loop.
+ *
+ * The \p data parameter is a PMT list which contains only a single element, an
+ * invocation handle which will be returned with all read respones.
+ */
 void
 usrp_rx::read_and_respond(pmt_t data)
 {
@@ -104,7 +126,7 @@ usrp_rx::read_and_respond(pmt_t data)
     std::cout << "[usrp_rx] Waiting for packets..\n";
 
   // Read by 512 which is packet size and send them back up
-  while(1) {
+  while(!usrp_rx_stop) {
 
     pmt_t v_pkt = pmt_make_u8vector(pkt_size, 0);
     transport_pkt *pkt = 
@@ -124,19 +146,38 @@ usrp_rx::read_and_respond(pmt_t data)
 
     d_cs->send(s_response_usrp_rx_read, 
                pmt_list3(PMT_NIL, PMT_T, v_pkt));
-    if(verbose)
+    if(verbose && 0)
       std::cout << "[usrp_rx] Read 1 packet\n";
     
     if(d_disk_write) {
-      if(pkt->chan() == 0x1f)
+      if(pkt->chan() == CONTROL_CHAN)
         d_cs_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
-      else
-        d_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
+      else {
+        if(d_disk_write_pkt) {
+          if(pkt->chan() == 0)
+            d_ofile0.write((const char *)pkt, transport_pkt::max_pkt_size());
+          else if(pkt->chan() == 1)
+            d_ofile1.write((const char *)pkt, transport_pkt::max_pkt_size());
+        } else {
+          if(pkt->chan() == 0)
+            d_ofile0.write((const char *)pkt->payload(), transport_pkt::max_payload());
+          else if(pkt->chan() == 1)
+            d_ofile1.write((const char *)pkt->payload(), transport_pkt::max_payload());
+        }
+      }
 
       d_cs_ofile.flush();
-      d_ofile.flush();
+      d_ofile0.flush();
+      d_ofile1.flush();
     }
   }
+  
+  usrp_rx_stop = false;
+
+  if(verbose) {
+    std::cout << "[USRP_RX] Stopping...\n";
+    fflush(stdout);
+  }
 }
 
 REGISTER_MBLOCK_CLASS(usrp_rx);