From c837d18c4af6f5a70a67293b01c674a76572bfee Mon Sep 17 00:00:00 2001
From: Glenn Bradford <glenn.bradford@unimelb.edu.au>
Date: Wed, 30 Mar 2022 14:13:25 +1100
Subject: [PATCH] disable debug messages, sink handles TSB tag

---
 grc/gen_bladerf_blocks.py     | 13 ++++--
 lib/bladerf/bladerf_common.h  |  2 +-
 lib/bladerf/bladerf_sink_c.cc | 87 +++++++++++++++++++++++++++++++++--
 lib/bladerf/bladerf_sink_c.h  |  5 ++
 4 files changed, 99 insertions(+), 8 deletions(-)

diff --git a/grc/gen_bladerf_blocks.py b/grc/gen_bladerf_blocks.py
index 4bcde97..871a364 100644
--- a/grc/gen_bladerf_blocks.py
+++ b/grc/gen_bladerf_blocks.py
@@ -117,7 +117,13 @@ parameters:
   default: False
   options: ['False', 'True']
   hide: part  
-  
+
+% if sourk == 'sink':
+- id: tsb_tag_name
+  label: TSB Tag Name
+  dtype: string
+  default: ""
+% endif
 
 - id: xb200
   category: x40/x115
@@ -231,8 +237,9 @@ templates:
              + ",trigger_signal1="+'${'$'}{trigger_signal1}'
              + ",bias_tee0="+'${'$'}{bias_tee0}'
              + ",bias_tee1="+'${'$'}{bias_tee1}'
-             
-             
+             % if sourk == 'sink':
+             + ",length_tag_name=" + str(${'$'}{tsb_tag_name})
+             % endif
     )
     self.${'$'}{id}.set_sample_rate(${'$'}{sample_rate})
     self.${'$'}{id}.set_center_freq(${'$'}{freq},0)
diff --git a/lib/bladerf/bladerf_common.h b/lib/bladerf/bladerf_common.h
index f2d8749..bd397b6 100644
--- a/lib/bladerf/bladerf_common.h
+++ b/lib/bladerf/bladerf_common.h
@@ -40,7 +40,7 @@
 typedef ptrdiff_t ssize_t;
 #endif //_MSC_VER
 
-#define BLADERF_DEBUG_ENABLE
+//#define BLADERF_DEBUG_ENABLE
 
 typedef std::shared_ptr<struct bladerf> bladerf_sptr;
 
diff --git a/lib/bladerf/bladerf_sink_c.cc b/lib/bladerf/bladerf_sink_c.cc
index 97bfdf4..222b506 100644
--- a/lib/bladerf/bladerf_sink_c.cc
+++ b/lib/bladerf/bladerf_sink_c.cc
@@ -44,6 +44,9 @@
 
 using namespace boost::assign;
 
+const pmt::pmt_t SOB_KEY = pmt::mp("tx_sob");
+const pmt::pmt_t EOB_KEY = pmt::mp("tx_eob");
+
 const pmt::pmt_t CMD_PORT     = pmt::mp("command");
 const pmt::pmt_t CMD_TIME_KEY = pmt::mp("time");
 const pmt::pmt_t CMD_CHAN_KEY = pmt::mp("chan");
@@ -87,7 +90,9 @@ bladerf_sink_c::bladerf_sink_c(const std::string &args) :
   _16icbuf(NULL),
   _32fcbuf(NULL),
   _in_burst(false),
-  _running(false)
+  _running(false),
+  _length_tag_key(pmt::PMT_NIL),
+  _packet_count(0)
 {
   setup_blade_messaging();
   
@@ -104,6 +109,14 @@ bladerf_sink_c::bladerf_sink_c(const std::string &args) :
   /* Perform src/sink agnostic initializations */
   init(dict, BLADERF_TX);
 
+  /* Set _length_tag_key */
+  if (dict.count("length_tag_name")) {
+    dict_t::const_iterator it = dict.find("length_tag_name");
+    if (!(it->second.empty())) {
+      _length_tag_key = pmt::intern(it->second);
+    }
+  }
+
   /* Check for RX-only params */
   if (dict.count("loopback")) {
     BLADERF_WARNING("Warning: 'loopback' has been specified on a bladeRF "
@@ -285,8 +298,11 @@ int bladerf_sink_c::work(int noutput_items,
                             SCALING_FACTOR, 2*noutput_items);
 
   // transmit the samples from the temp buffer
-  if (BLADERF_FORMAT_SC16_Q11_META == _format) {
+  if (BLADERF_FORMAT_SC16_Q11_META == _format &&
+      _length_tag_key == pmt::PMT_NIL) {
     status = transmit_with_tags(_16icbuf, noutput_items);
+  } else if (BLADERF_FORMAT_SC16_Q11_META == _format) {
+    status = transmit_burst(_16icbuf, noutput_items);
   } else {
     status = bladerf_sync_tx(_dev.get(), static_cast<void const *>(_16icbuf),
                              noutput_items, NULL, _stream_timeout);
@@ -317,6 +333,69 @@ int bladerf_sink_c::work(int noutput_items,
   return noutput_items;
 }
 
+int bladerf_sink_c::transmit_burst(int16_t const *samples,
+                                   int noutput_items)
+{
+  int status = 0;
+
+  struct bladerf_metadata meta;
+  memset(&meta, 0, sizeof(meta));
+  int16_t const zeros[8] = { 0 };
+
+  // get _length_tag_name tags
+  std::vector<gr::tag_t> tags;
+  get_tags_in_window(tags, 0, 0, noutput_items, _length_tag_key);
+
+  int index = 0;
+  while (index < noutput_items) {
+
+    if (_packet_count > 0) {
+      int count = std::min(_packet_count, noutput_items - index);
+      status = bladerf_sync_tx(_dev.get(),
+                               static_cast<void const *>(&samples[2*index]),
+                               count, &meta, _stream_timeout);
+      if (status != 0) return status;
+      index += count;
+      _packet_count -= count;
+
+      // flush - may not be necessary
+      if (_packet_count == 0) {
+        meta.flags &= ~(BLADERF_META_FLAG_TX_NOW | BLADERF_META_FLAG_TX_BURST_START);
+        meta.flags |= BLADERF_META_FLAG_TX_BURST_END;
+        status = bladerf_sync_tx(_dev.get(),
+                                 static_cast<void const *>(zeros),
+                                 4, &meta, _stream_timeout);
+        if(status) return status;
+      }
+      memset(&meta, 0, sizeof(meta));
+
+    } else if(!tags.empty()) {
+      
+      int offset = tags[0].offset - nitems_read(0);
+
+      if (offset > index) {
+        BLADERF_DEBUG("skipping " << (offset - index) << " samples");
+      }
+
+      if (offset >= index) {
+        index = offset;
+        _packet_count = pmt::to_long(tags[0].value);
+        assert(_packet_count > 0);
+        meta.flags |= BLADERF_META_FLAG_TX_NOW;
+        meta.flags |= BLADERF_META_FLAG_TX_BURST_START;
+        tags.erase(tags.begin());
+      } else {
+        BLADERF_WARNING("overlapping transmit bursts");
+      } 
+    } else {
+      BLADERF_DEBUG("skipping " << (noutput_items - index) << " samples");
+      index = noutput_items;
+    }
+  }
+
+  return status;
+}
+
 int bladerf_sink_c::transmit_with_tags(int16_t const *samples,
                                         int noutput_items)
 {
@@ -363,7 +442,7 @@ int bladerf_sink_c::transmit_with_tags(int16_t const *samples,
     // Upon seeing an SOB tag, update our offset. We'll TX the start of the
     // burst when we see an EOB or at the end of this function - whichever
     // occurs first.
-    if (pmt::symbol_to_string(tag.key) == "tx_sob") {
+    if (tag.key == SOB_KEY) {
       if (_in_burst) {
         BLADERF_WARNING("Got SOB while already within a burst");
 
@@ -377,7 +456,7 @@ int bladerf_sink_c::transmit_with_tags(int16_t const *samples,
         _in_burst = true;
       }
 
-    } else if (pmt::symbol_to_string(tag.key) == "tx_eob") {
+    } else if (tag.key == EOB_KEY) {
       if (!_in_burst) {
         BLADERF_WARNING("Got EOB while not in burst");
         return BLADERF_ERR_INVAL;
diff --git a/lib/bladerf/bladerf_sink_c.h b/lib/bladerf/bladerf_sink_c.h
index 6d858e4..670be98 100644
--- a/lib/bladerf/bladerf_sink_c.h
+++ b/lib/bladerf/bladerf_sink_c.h
@@ -120,6 +120,7 @@ public:
 
 private:
   int transmit_with_tags(int16_t const *samples, int noutput_items);
+  int transmit_burst(int16_t const *samples, int noutput_items);
 
   // Sample-handling buffers
   int16_t *_16icbuf;              /**< raw samples to bladeRF */
@@ -134,6 +135,10 @@ private:
   /* Scaling factor used when converting from float to int16_t */
   const float SCALING_FACTOR = 2048.0f;
 
+  /* transmit bursts */
+  pmt::pmt_t _length_tag_key;
+  int _packet_count;
+
   /* command port */
   void handle_command(const pmt::pmt_t& msg);
 };
-- 
GitLab