Merged features/inband-usb -r6431:8293 into trunk.
[debian/gnuradio] / usrp / host / lib / inband / usrp_inband_usb_packet.cc
index ee4cb22f3f98c42171d616f709298287ceb39293..2f02ecc3ff4aabe54bf5085fe704ebc0db8f595b 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
+/*!
+ * \brief Aligns the packet payload on a 32 bit boundary.  This is essential to
+ * all control/status packets so that the inband FPGA code can parse them
+ * easily.
+ *
+ * \returns true if successful or if the packet was already aligned; false if it
+ * cannot be aligned.
+ */
 bool usrp_inband_usb_packet::align32()
 {
   int p_len = payload_len();
@@ -44,18 +52,20 @@ bool usrp_inband_usb_packet::align32()
   if((MAX_PAYLOAD - p_len) < bytes_needed)
     return false;
     
-  p_len += bytes_needed;
-
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = p_len;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(bytes_needed);
 
   return true;
 }
 
+/*!
+ * \brief Adds a ping command to the current control packet.
+ *
+ * The \p rid is the rid to be associated with the ping response and \p ping_val
+ * is currently unused.
+ *
+ * \returns true if adding the ping command was successful, false otherwise
+ * (i.e. no space in the current packet).
+ */
 bool usrp_inband_usb_packet::cs_ping(long rid, long ping_val)
 {
   if(!align32())
@@ -78,17 +88,20 @@ bool usrp_inband_usb_packet::cs_ping(long rid, long ping_val)
   *payload = host_to_usrp_u32(ping);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_PING_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_PING_LEN);
 
   return true;
 }
 
-
+/*!
+ * \brief Adds a ping response to the packet.  This is used by the fake USRP
+ * code to generate fake responses for pings.
+ *
+ * The \p rid is the RID to be associated with the response and \p ping_val is
+ * currently unused.
+ *
+ * \returns true if the ping reply was added successfully, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_ping_reply(long rid, long ping_val) 
 {
   if(!align32())
@@ -111,16 +124,20 @@ bool usrp_inband_usb_packet::cs_ping_reply(long rid, long ping_val)
   *payload = host_to_usrp_u32(ping);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_PING_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_PING_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds a write register command to the packet.
+ *
+ * The \p reg_num is the register number for which the value \p val will be
+ * written to.
+ *
+ * \returns true if the command was added to the packet successfully, false
+ * otherwise.
+ */
 bool usrp_inband_usb_packet::cs_write_reg(long reg_num, long val)
 {
   if(!align32())
@@ -149,16 +166,19 @@ bool usrp_inband_usb_packet::cs_write_reg(long reg_num, long val)
   *payload = host_to_usrp_u32((uint32_t) val);
   
   // Rebuild the header to update the payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_WRITEREG_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_WRITEREG_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds a write register masked command to the packet.
+ *
+ * The \p reg_num is the register number for which the value \p val will be
+ * written, masked by \p mask
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_write_reg_masked(long reg_num, long val, long mask)
 {
   if(!align32())
@@ -190,16 +210,19 @@ bool usrp_inband_usb_packet::cs_write_reg_masked(long reg_num, long val, long ma
   *payload = host_to_usrp_u32((uint32_t) mask);
   
   // Rebuild the header to update the payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_WRITEREGMASKED_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_WRITEREGMASKED_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds a read register message to the packet. 
+ *
+ * The \p rid will be the associated RID returned with the response, and \p
+ * reg_num is the register to be read.
+ * 
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_read_reg(long rid, long reg_num)
 {
   if(!align32())
@@ -222,16 +245,22 @@ bool usrp_inband_usb_packet::cs_read_reg(long rid, long reg_num)
   *payload = host_to_usrp_u32(read_reg);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_READREG_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_READREG_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds a read register reply response to the current packet.  This is
+ * used by the fake USRP code to generate fake register read responses for
+ * testing.
+ *
+ * The \p rid is the associated RID to be included in the response, \p reg_num
+ * is the register the read is coming from, and \p reg_val is the value of the
+ * read.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_read_reg_reply(long rid, long reg_num, long reg_val)
 {
   if(!align32())
@@ -258,16 +287,19 @@ bool usrp_inband_usb_packet::cs_read_reg_reply(long rid, long reg_num, long reg_
   *payload = host_to_usrp_u32((uint32_t) reg_val); 
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_READREGREPLY_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_READREGREPLY_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds a delay command to the current packet.
+ *
+ * The \p ticks parameter is the number of clock ticks the FPGA should delay
+ * parsing for, which is added to the packet.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_delay(long ticks)
 {
   if(!align32())
@@ -289,16 +321,16 @@ bool usrp_inband_usb_packet::cs_delay(long ticks)
   *payload = host_to_usrp_u32(delay);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_DELAY_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_DELAY_LEN);
 
   return true;
 }
 
+/*!
+ * \brief
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_i2c_write(long i2c_addr, uint8_t *i2c_data, size_t data_len)
 {
   if(!align32())
@@ -328,16 +360,20 @@ bool usrp_inband_usb_packet::cs_i2c_write(long i2c_addr, uint8_t *i2c_data, size
    memcpy(payload, i2c_data, data_len);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + i2c_len;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + i2c_len);
 
   return true;
 }
 
+/*!
+ * \brief Adds an I2C read command to the current packet.
+ *
+ * The \p rid is the associated RID to return with the read response, \p
+ * i2c_addr is the address to read from on the I2C bus, and \p n_bytes is the
+ * number of bytes to be read from the bus.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_i2c_read(long rid, long i2c_addr, long n_bytes)
 {
   if(!align32())
@@ -367,16 +403,20 @@ bool usrp_inband_usb_packet::cs_i2c_read(long rid, long i2c_addr, long n_bytes)
   *payload = host_to_usrp_u32(word1);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_I2CREAD_LEN;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + CS_I2CREAD_LEN);
 
   return true;
 }
 
+/*!
+ * \brief Adds an I2C read reply response to the current packet.  This is used
+ * by the fake USRP code to generate fake I2C responses.
+ *
+ * The \p rid is the RID to be associated with the response, \p i2c_addr is the
+ * address on the I2C bus that the \p i2c_data of \p i2c_data_len was read from.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_i2c_read_reply(long rid, long i2c_addr, uint8_t *i2c_data, long i2c_data_len)
 {
   if(!align32())
@@ -406,16 +446,16 @@ bool usrp_inband_usb_packet::cs_i2c_read_reply(long rid, long i2c_addr, uint8_t
   memcpy(payload, i2c_data, i2c_data_len);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + i2c_len;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + i2c_len);
 
   return true;
 }
 
+/*!
+ * \brief Adds a SPI write command to the current packet.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_spi_write(long enables, long format, long opt_header_bytes, uint8_t *spi_data, long spi_data_len)
 {
   if(!align32())
@@ -454,16 +494,16 @@ bool usrp_inband_usb_packet::cs_spi_write(long enables, long format, long opt_he
   memcpy(payload, spi_data, spi_data_len);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + spi_len;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + spi_len);
 
   return true;
 }
 
+/*!
+ * \brief Adds a SPI bus read command to the packet.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_spi_read(long rid, long enables, long format, long opt_header_bytes, long n_bytes)
 {
   if(!align32())
@@ -508,16 +548,17 @@ bool usrp_inband_usb_packet::cs_spi_read(long rid, long enables, long format, lo
   *payload = host_to_usrp_u32(word);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + CS_SPIREAD_LEN;
+  incr_header_len(CS_FIXED_LEN + CS_SPIREAD_LEN);
 
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
-  
   return true;
 }
 
+/*!
+ * \brief Adds an SPI read reply to the current packet.  This is used by the
+ * fake USRP code to generate fake responses for SPI reads.
+ *
+ * \returns true if the command was added to the packet, false otherwise.
+ */
 bool usrp_inband_usb_packet::cs_spi_read_reply(long rid, uint8_t *spi_data, long spi_data_len)
 {
   if(!align32())
@@ -546,30 +587,32 @@ bool usrp_inband_usb_packet::cs_spi_read_reply(long rid, uint8_t *spi_data, long
   memcpy(payload, spi_data, spi_data_len);
 
   // Update payload length
-  int h_flags = flags();
-  int h_chan = chan();
-  int h_tag = tag();
-  int h_payload_len = payload_len() + CS_FIXED_LEN + spi_len;
-
-  set_header(h_flags, h_chan, h_tag, h_payload_len);
+  incr_header_len(CS_FIXED_LEN + spi_len);
 
   return true;
 }
 
-// Takes an offset to the beginning of a subpacket and extracts the
-// length of the subpacket
+/*!
+ * \brief Since all control packets contain subpackets which have the length of
+ * the subpacket at a uniform location in the subpacket, this will return the
+ * subpacket length given a byte offset of the start of the subpacket from the beginning of the packet.
+ *
+ * \returns the length of the subpacket
+ */
 int usrp_inband_usb_packet::cs_len(int payload_offset) {
   uint32_t subpkt = usrp_to_host_u32(*((uint32_t *)(d_payload + payload_offset)));
   return (subpkt >> CS_LEN_SHIFT) & CS_LEN_MASK;
 }
 
-// The following method takes an offset within the packet payload to extract
-// a control/status subpacket and construct a pmt response which includes the
-// proper signal and arguments specified by usrp-low-level-cs.  The USRP
-// server could therefore use this to read subpackets and pass them responses
-// back up to the application.  It's arguable that only reply packets should
-// be parsed here, however we parse others for use in debugging or failure
-// reporting on the transmit side of packets.
+/*!
+ * \brief The following method takes an offset within the packet payload to
+ * extract a control/status subpacket and constructs a pmt response which
+ * includes the proper signal and arguments specified by usrp-low-level-cs.  The
+ * USRP server could therefore use this to read subpackets and pass them
+ * responses back up to the application.  It's arguable that only reply packets
+ * should be parsed here, however we parse others for use in debugging or
+ * failure reporting on the transmit side of packets.
+ */
 pmt_t usrp_inband_usb_packet::read_subpacket(int payload_offset) {
 
   uint32_t subpkt = usrp_to_host_u32(*((uint32_t *)(d_payload + payload_offset)));