From 7a6dace271a53faccec6d691477e2c5ccc5e9d49 Mon Sep 17 00:00:00 2001
From: Ruben Undheim <ruben.undheim@gmail.com>
Date: Wed, 26 Apr 2017 20:15:20 +0000
Subject: blocks: file_source ("file_begin" stream tag)

- Add a configurable "file_begin" stream tag
  which is especially useful in repeat mode.
- Added unit tests for new functionality
---
 gr-blocks/lib/file_source_impl.cc | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

(limited to 'gr-blocks/lib/file_source_impl.cc')

diff --git a/gr-blocks/lib/file_source_impl.cc b/gr-blocks/lib/file_source_impl.cc
index 1602b02416..2f626d0897 100644
--- a/gr-blocks/lib/file_source_impl.cc
+++ b/gr-blocks/lib/file_source_impl.cc
@@ -64,10 +64,15 @@ namespace gr {
 		      io_signature::make(0, 0, 0),
 		      io_signature::make(1, 1, itemsize)),
 	d_itemsize(itemsize), d_fp(0), d_new_fp(0), d_repeat(repeat),
-	d_updated(false)
+	d_updated(false), d_file_begin(true), d_repeat_cnt(0),
+       d_add_begin_tag(false)
     {
       open(filename, repeat);
       do_update();
+
+      std::stringstream str;
+      str << name() << unique_id();
+      _id = pmt::string_to_symbol(str.str());
     }
 
     file_source_impl::~file_source_impl()
@@ -139,9 +144,16 @@ namespace gr {
 	d_fp = d_new_fp;    // install new file pointer
 	d_new_fp = 0;
 	d_updated = false;
+       d_file_begin = true;
       }
     }
 
+    void
+    file_source_impl::set_begin_tag(bool val)
+    {
+      d_add_begin_tag = val;
+    }
+
     int
     file_source_impl::work(int noutput_items,
 			   gr_vector_const_void_star &input_items,
@@ -156,7 +168,14 @@ namespace gr {
 	throw std::runtime_error("work with file not open");
 
       gr::thread::scoped_lock lock(fp_mutex); // hold for the rest of this function
+
       while(size) {
+        // Add stream tag whenever the file starts again
+        if (d_file_begin && d_add_begin_tag) {
+          add_item_tag(0, nitems_written(0) + noutput_items - size, BEGIN_KEY, pmt::from_long(d_repeat_cnt), _id);
+          d_file_begin = false;
+        }
+
 	i = fread(o, d_itemsize, size, (FILE*)d_fp);
 
 	size -= i;
@@ -178,6 +197,10 @@ namespace gr {
 	  fprintf(stderr, "[%s] fseek failed\n", __FILE__);
 	  exit(-1);
 	}
+        if (d_add_begin_tag) {
+          d_file_begin = true;
+          d_repeat_cnt++;
+        }
       }
 
       if(size > 0) {	     		// EOF or error
-- 
cgit v1.2.3


From e3ef9bb387a17222fe390fac3b659199502e2efe Mon Sep 17 00:00:00 2001
From: Ruben Undheim <ruben.undheim@gmail.com>
Date: Fri, 19 May 2017 17:47:48 +0000
Subject: set_begin_tag now takes pmt::pmt instead of bool

---
 gr-blocks/grc/blocks_file_source.xml            | 13 +++----------
 gr-blocks/include/gnuradio/blocks/file_source.h |  2 +-
 gr-blocks/lib/file_source_impl.cc               | 10 +++++-----
 gr-blocks/lib/file_source_impl.h                |  6 ++----
 gr-blocks/python/blocks/qa_file_source_sink.py  |  5 +++--
 5 files changed, 14 insertions(+), 22 deletions(-)

(limited to 'gr-blocks/lib/file_source_impl.cc')

diff --git a/gr-blocks/grc/blocks_file_source.xml b/gr-blocks/grc/blocks_file_source.xml
index 22c5263171..1f09e95168 100644
--- a/gr-blocks/grc/blocks_file_source.xml
+++ b/gr-blocks/grc/blocks_file_source.xml
@@ -8,6 +8,7 @@
 	<name>File Source</name>
 	<key>blocks_file_source</key>
 	<import>from gnuradio import blocks</import>
+	<import>import pmt</import>
 	<make>blocks.file_source($type.size*$vlen, $file, $repeat)
 self.$(id).set_begin_tag($begin_tag)</make>
 	<callback>open($file, $repeat)</callback>
@@ -72,16 +73,8 @@ self.$(id).set_begin_tag($begin_tag)</make>
 	<param>
 		<name>Add begin tag</name>
 		<key>begin_tag</key>
-		<value>False</value>
-		<type>bool</type>
-		<option>
-			<name>Yes</name>
-			<key>True</key>
-		</option>
-		<option>
-			<name>No</name>
-			<key>False</key>
-		</option>
+		<value>pmt.PMT_NIL</value>
+		<type>raw</type>
 	</param>
 	<check>$vlen &gt; 0</check>
 	<source>
diff --git a/gr-blocks/include/gnuradio/blocks/file_source.h b/gr-blocks/include/gnuradio/blocks/file_source.h
index bba09ad502..c8138339fd 100644
--- a/gr-blocks/include/gnuradio/blocks/file_source.h
+++ b/gr-blocks/include/gnuradio/blocks/file_source.h
@@ -81,7 +81,7 @@ namespace gr {
       /*!
        * \brief Add a stream tag to the first sample of the file if true
        */
-      virtual void set_begin_tag(bool val) = 0;
+      virtual void set_begin_tag(pmt::pmt_t val) = 0;
     };
 
   } /* namespace blocks */
diff --git a/gr-blocks/lib/file_source_impl.cc b/gr-blocks/lib/file_source_impl.cc
index 2f626d0897..9dfd30c016 100644
--- a/gr-blocks/lib/file_source_impl.cc
+++ b/gr-blocks/lib/file_source_impl.cc
@@ -65,7 +65,7 @@ namespace gr {
 		      io_signature::make(1, 1, itemsize)),
 	d_itemsize(itemsize), d_fp(0), d_new_fp(0), d_repeat(repeat),
 	d_updated(false), d_file_begin(true), d_repeat_cnt(0),
-       d_add_begin_tag(false)
+       d_add_begin_tag(pmt::PMT_NIL)
     {
       open(filename, repeat);
       do_update();
@@ -149,7 +149,7 @@ namespace gr {
     }
 
     void
-    file_source_impl::set_begin_tag(bool val)
+    file_source_impl::set_begin_tag(pmt::pmt_t val)
     {
       d_add_begin_tag = val;
     }
@@ -171,8 +171,8 @@ namespace gr {
 
       while(size) {
         // Add stream tag whenever the file starts again
-        if (d_file_begin && d_add_begin_tag) {
-          add_item_tag(0, nitems_written(0) + noutput_items - size, BEGIN_KEY, pmt::from_long(d_repeat_cnt), _id);
+        if (d_file_begin && d_add_begin_tag != pmt::PMT_NIL) {
+          add_item_tag(0, nitems_written(0) + noutput_items - size, d_add_begin_tag, pmt::from_long(d_repeat_cnt), _id);
           d_file_begin = false;
         }
 
@@ -197,7 +197,7 @@ namespace gr {
 	  fprintf(stderr, "[%s] fseek failed\n", __FILE__);
 	  exit(-1);
 	}
-        if (d_add_begin_tag) {
+        if (d_add_begin_tag != pmt::PMT_NIL) {
           d_file_begin = true;
           d_repeat_cnt++;
         }
diff --git a/gr-blocks/lib/file_source_impl.h b/gr-blocks/lib/file_source_impl.h
index 3cde2bb11a..19f393fc1d 100644
--- a/gr-blocks/lib/file_source_impl.h
+++ b/gr-blocks/lib/file_source_impl.h
@@ -26,8 +26,6 @@
 #include <gnuradio/blocks/file_source.h>
 #include <boost/thread/mutex.hpp>
 
-static const pmt::pmt_t BEGIN_KEY = pmt::string_to_symbol("file_begin");
-
 namespace gr {
   namespace blocks {
 
@@ -41,7 +39,7 @@ namespace gr {
       bool d_updated;
       bool d_file_begin;
       long d_repeat_cnt;
-      bool d_add_begin_tag;
+      pmt::pmt_t d_add_begin_tag;
 
       boost::mutex fp_mutex;
       pmt::pmt_t _id;
@@ -60,7 +58,7 @@ namespace gr {
 	       gr_vector_const_void_star &input_items,
 	       gr_vector_void_star &output_items);
 
-      void set_begin_tag(bool val);
+      void set_begin_tag(pmt::pmt_t val);
     };
 
   } /* namespace blocks */
diff --git a/gr-blocks/python/blocks/qa_file_source_sink.py b/gr-blocks/python/blocks/qa_file_source_sink.py
index 081f58228f..32910cb4bc 100644
--- a/gr-blocks/python/blocks/qa_file_source_sink.py
+++ b/gr-blocks/python/blocks/qa_file_source_sink.py
@@ -23,6 +23,7 @@
 from gnuradio import gr, gr_unittest, blocks
 import os
 import tempfile
+import pmt
 
 class test_file_source_sink(gr_unittest.TestCase):
 
@@ -115,7 +116,7 @@ class test_file_source_sink(gr_unittest.TestCase):
             snk.set_unbuffered(True)
 
             src2 = blocks.file_source(gr.sizeof_float, temp.name)
-            src2.set_begin_tag(True)
+            src2.set_begin_tag(pmt.string_to_symbol("file_begin"))
 
             self.tb.connect(src, snk)
             self.tb.run()
@@ -141,7 +142,7 @@ class test_file_source_sink(gr_unittest.TestCase):
             snk.set_unbuffered(True)
 
             src2 = blocks.file_source(gr.sizeof_float, temp.name, True)
-            src2.set_begin_tag(True)
+            src2.set_begin_tag(pmt.string_to_symbol("file_begin"))
             hd = blocks.head(gr.sizeof_float, 2000)
 
             self.tb.connect(src, snk)
-- 
cgit v1.2.3