From d4d7a74e540315e16ef5e0905f13d46f547c2bef Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 14 Feb 2014 12:27:08 +0100
Subject: [PATCH] moved version to labcomm.h and added check (sketch) in reader
 and writer

---
 lib/c/2006/labcomm2006.c           |  2 --
 lib/c/2006/labcomm2006.h           |  2 ++
 lib/c/2006/labcomm2006_decoder.c   |  2 --
 lib/c/2006/labcomm2006_encoder.c   |  2 --
 lib/c/2006/labcomm2006_fd_reader.c | 25 +++++++++----------------
 lib/c/2006/labcomm2006_fd_writer.c | 16 ++++++++++------
 6 files changed, 21 insertions(+), 28 deletions(-)

diff --git a/lib/c/2006/labcomm2006.c b/lib/c/2006/labcomm2006.c
index 34c5b06..34a13ec 100644
--- a/lib/c/2006/labcomm2006.c
+++ b/lib/c/2006/labcomm2006.c
@@ -37,8 +37,6 @@
 #include "labcomm2006_ioctl.h"
 #include "labcomm2006_dynamic_buffer_writer.h"
 
-#define LABCOMM_VERSION "LabComm2006"
-
 /* Unwrapping reader/writer functions */
 #define UNWRAP_ac(rw, ac, ...) ac
 #define UNWRAP(func, ...)	     \
diff --git a/lib/c/2006/labcomm2006.h b/lib/c/2006/labcomm2006.h
index eb7ad2c..20db98e 100644
--- a/lib/c/2006/labcomm2006.h
+++ b/lib/c/2006/labcomm2006.h
@@ -23,6 +23,8 @@
 #ifndef _LABCOMM_H_
 #define _LABCOMM_H_
 
+#define LABCOMM_VERSION "LabComm2006"
+
 #include <stdarg.h>
 #include <stdint.h>
 #include <unistd.h>
diff --git a/lib/c/2006/labcomm2006_decoder.c b/lib/c/2006/labcomm2006_decoder.c
index 3ee2f3c..a936f3e 100644
--- a/lib/c/2006/labcomm2006_decoder.c
+++ b/lib/c/2006/labcomm2006_decoder.c
@@ -18,8 +18,6 @@
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-#define LABCOMM_VERSION "LabComm2006"
-
 #include <errno.h>
 #include "labcomm2006.h"
 #include "labcomm2006_private.h"
diff --git a/lib/c/2006/labcomm2006_encoder.c b/lib/c/2006/labcomm2006_encoder.c
index a84f82a..06eb4dc 100644
--- a/lib/c/2006/labcomm2006_encoder.c
+++ b/lib/c/2006/labcomm2006_encoder.c
@@ -18,8 +18,6 @@
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-#define LABCOMM_VERSION "LabComm2006"
-
 #include <errno.h>
 #include "labcomm2006.h"
 #include "labcomm2006_private.h"
diff --git a/lib/c/2006/labcomm2006_fd_reader.c b/lib/c/2006/labcomm2006_fd_reader.c
index a8100ff..7c05960 100644
--- a/lib/c/2006/labcomm2006_fd_reader.c
+++ b/lib/c/2006/labcomm2006_fd_reader.c
@@ -40,6 +40,15 @@ static int fd_alloc(struct labcomm2006_reader *r,
 		    char *version)
 {
   int result = 0;
+// in-band version is not included in version 2006.
+// This may be a place for version checking and/or printing
+// a warning message
+  if (version && version[0]) {
+    if (strcmp(LABCOMM_VERSION, version) != 0) {
+      fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION);
+      return -EINVAL;
+    } 
+  }
   
   r->count = 0;
   r->pos = 0;
@@ -51,22 +60,6 @@ static int fd_alloc(struct labcomm2006_reader *r,
 
     r->data_size = BUFFER_SIZE;
     result = r->data_size;
-#if 0
-// in-band version is not included in version 2006.
-// This may be a place for version checking and/or printing
-// a warning message
-    if (version && version[0]) {
-      char *tmp;
-      
-      tmp = labcomm2006_read_string(r);
-      if (strcmp(tmp, version) != 0) {
-	result = -EINVAL;
-      } else {
-	result = r->data_size;
-      }
-      labcomm2006_memory_free(r->memory, 1, tmp);
-    }
-#endif
   }
   return result;
 }
diff --git a/lib/c/2006/labcomm2006_fd_writer.c b/lib/c/2006/labcomm2006_fd_writer.c
index 0b1d474..d93c63b 100644
--- a/lib/c/2006/labcomm2006_fd_writer.c
+++ b/lib/c/2006/labcomm2006_fd_writer.c
@@ -43,6 +43,16 @@ static int fd_alloc(struct labcomm2006_writer *w,
 		    struct labcomm2006_writer_action_context *action_context, 
 		    char *version)
 {
+// in-band version is not included in version 2006.
+// This may be a place for version checking and/or printing
+// a warning message
+  if (version && version[0]) {
+    if (strcmp(LABCOMM_VERSION, version) != 0) {
+      fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION);
+      return -EINVAL;
+    } 
+  }
+  
   w->data = labcomm2006_memory_alloc(w->memory, 0, BUFFER_SIZE);
   if (! w->data) {
     w->error = -ENOMEM;
@@ -53,12 +63,6 @@ static int fd_alloc(struct labcomm2006_writer *w,
     w->data_size = BUFFER_SIZE;
     w->count = BUFFER_SIZE;
     w->pos = 0;
-    if (version && version[0]) {
-#if 0 
-      labcomm2006_write_string(w, version);
-      fd_flush(w, action_context);
-#endif
-    }
   }
 
   return w->error;
-- 
GitLab