From 17ce406dcd5e3c83214b165295244a2b67678968 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Mon, 27 Oct 2014 08:06:08 +0100
Subject: [PATCH] more c decoder refactoring

---
 lib/c/labcomm_decoder.c | 92 ++++++++++++++++++++++++++---------------
 1 file changed, 58 insertions(+), 34 deletions(-)

diff --git a/lib/c/labcomm_decoder.c b/lib/c/labcomm_decoder.c
index 8fabdc8..b81937c 100644
--- a/lib/c/labcomm_decoder.c
+++ b/lib/c/labcomm_decoder.c
@@ -35,13 +35,13 @@ struct sample_entry {
   void *context;
 };
 
-static int internal_decoder_run(struct labcomm_decoder *d, 
+static int internal_decoder_run(struct labcomm_decoder *d,
 		                struct labcomm_decoder *registry);
-static int internal_decode_one(struct labcomm_decoder *d, 
+static int internal_decode_one(struct labcomm_decoder *d,
 		               struct labcomm_decoder *registry);
 //XXX move to pragma.ch
 //int default_pragma_handler(struct labcomm_decoder *d, char *pragma_type);
-int default_pragma_handler(struct labcomm_decoder *d, 
+int default_pragma_handler(struct labcomm_decoder *d,
 		           struct labcomm_decoder *registry,
 		           char *pragma_type)
 {
@@ -127,14 +127,19 @@ void labcomm_decoder_free(struct labcomm_decoder* d)
 }
 #endif
 
-static int decode_sample_def(struct labcomm_decoder *d, 
+/* d        - decoder to read from
+   registry - decoder to lookup signatures (registry != d only if
+                nesting decoders, e.g., when decoding pragma)
+   kind     - type to decode (sample 0x02, or TODO: typedef/binding )
+*/
+static int decode_sample_def(struct labcomm_decoder *d,
 		             struct labcomm_decoder *registry,
 			     int kind)
 {
   int result;
   struct labcomm_signature signature, *local_signature;
   int remote_index, local_index, i;
-  
+
   local_signature = NULL;
   local_index = 0;
   remote_index = labcomm_read_packed32(d->reader);
@@ -153,6 +158,7 @@ static int decode_sample_def(struct labcomm_decoder *d,
     goto free_signature_name;
   }
   signature.signature = labcomm_memory_alloc(d->memory, 1,  signature.size);
+  //XXX d->reader???
   if (d->reader->error < 0) {
     result = d->reader->error;
     goto free_signature_name;
@@ -164,14 +170,14 @@ static int decode_sample_def(struct labcomm_decoder *d,
       goto free_signature_signature;
     }
   }
-  labcomm_scheduler_data_lock(d->scheduler);
-  LABCOMM_SIGNATURE_ARRAY_FOREACH(d->local, struct sample_entry, i) {
+  labcomm_scheduler_data_lock(registry->scheduler);
+  LABCOMM_SIGNATURE_ARRAY_FOREACH(registry->local, struct sample_entry, i) {
     struct sample_entry *s;
     int *remote_to_local;
-      
+
     result = -ENOENT;
-    s = LABCOMM_SIGNATURE_ARRAY_REF(d->memory, 
-				      d->local,  struct sample_entry, i);
+    s = LABCOMM_SIGNATURE_ARRAY_REF(registry->memory,
+				      registry->local,  struct sample_entry, i);
     if (s->signature &&
         s->signature->size == signature.size &&
         strcmp(s->signature->name, signature.name) == 0 &&
@@ -180,15 +186,15 @@ static int decode_sample_def(struct labcomm_decoder *d,
       s->remote_index = remote_index;
       local_signature = s->signature;
       local_index = i;
-      remote_to_local = LABCOMM_SIGNATURE_ARRAY_REF(d->memory,
-                                                    d->remote_to_local, int,
+      remote_to_local = LABCOMM_SIGNATURE_ARRAY_REF(registry->memory,
+                                                    registry->remote_to_local, int,
                                                     remote_index);
       *remote_to_local = i;
       result = remote_index;
       break;
     }
   }
-  labcomm_scheduler_data_unlock(d->scheduler);
+  labcomm_scheduler_data_unlock(registry->scheduler);
   if (local_signature) {
     labcomm_reader_start(d->reader, d->reader->action_context,
                          local_index, remote_index, local_signature,
@@ -246,9 +252,9 @@ static int decode_version(struct labcomm_decoder *d,
       d->version_ok = 1;
     } else {
       result = -ECONNRESET;
-    }  
+    }
     labcomm_memory_free(d->memory, 1,  version);
-out:   
+out:
   return result;
 }
 
@@ -264,6 +270,11 @@ static int decoder_skip(struct labcomm_decoder *d, int len, int tag)
   }
   return tag;
 }
+/* d        - decoder to read from
+   registry - decoder to lookup signatures (registry != d only if
+                nesting decoders, e.g., when decoding pragma)
+   len      - length of the labcomm packet )
+*/
 static int decode_pragma(struct labcomm_decoder *d,
 		         struct labcomm_decoder *registry,
 		         int len)
@@ -299,7 +310,7 @@ static int decode_pragma(struct labcomm_decoder *d,
                                      d->reader->memory, pragma_data, psize);
      struct labcomm_decoder *pd = labcomm_decoder_new(
                                      pr, d->error, d->memory, d->scheduler);
-     pd->version_ok = 1;  
+     pd->version_ok = 1;
      printf("calling pragma_handler\n");
      result = d->pragma_handler(pd, pd, pragma_type);
      printf("returned from pragma_handler\n");
@@ -337,7 +348,12 @@ static labcomm_decoder_function lookup_h(struct labcomm_decoder *d,
   labcomm_scheduler_data_unlock(d->scheduler);
   return do_decode;
 }
-static int decode_and_handle(struct labcomm_decoder *d, 
+/* d            - decoder to read from
+   registry     - decoder to lookup signatures (registry != d only if
+                    nesting decoders, e.g., when decoding pragma)
+   remote_index -  received type index )
+*/
+static int decode_and_handle(struct labcomm_decoder *d,
 		             struct labcomm_decoder *registry,
 		             int remote_index)
 {
@@ -350,7 +366,7 @@ static int decode_and_handle(struct labcomm_decoder *d,
     .handler = NULL,
     .context = NULL,
   };
-  labcomm_decoder_function do_decode = lookup_h(d, &wrap, remote_index, &local_index);
+  labcomm_decoder_function do_decode = lookup_h(registry, &wrap, remote_index, &local_index);
   result = *local_index;
   if (do_decode) {
     do_decode(d->reader, call_handler, &wrap);
@@ -362,10 +378,14 @@ static int decode_and_handle(struct labcomm_decoder *d,
   }
   return result;
 }
-int labcomm_decoder_decode_one(struct labcomm_decoder *d) 
+int labcomm_decoder_decode_one(struct labcomm_decoder *d)
 {
 	return internal_decode_one(d,d);
 }
+/* d            - decoder to read from
+   registry     - decoder to lookup signatures (registry != d only if
+                    nesting decoders, e.g., when decoding pragma)
+*/
 int internal_decode_one(struct labcomm_decoder *d,
 		        struct labcomm_decoder *registry)
 {
@@ -388,17 +408,17 @@ int internal_decode_one(struct labcomm_decoder *d,
     fprintf(stderr, "No VERSION %d %d\n", remote_index, length);
     result = -ECONNRESET;
   } else if (remote_index == LABCOMM_SAMPLE_DEF) {
-    result = decode_sample_def(d, d, remote_index); 
+    result = decode_sample_def(d, registry, remote_index);
   } else if (remote_index == LABCOMM_PRAGMA ){
-    result = decode_pragma(d, d, length);
+    result = decode_pragma(d, registry, length);
   } else if (remote_index < LABCOMM_USER) {
     //fprintf(stderr, "SKIP %d %d\n", remote_index, length);
     //result = remote_index;
     result = decoder_skip(d, length, remote_index);
   } else {
-    result = decode_and_handle(d, d, remote_index);
+    result = decode_and_handle(d, registry, remote_index);
   }
-out:   
+out:
   return result;
 }
 
@@ -406,31 +426,35 @@ int labcomm_decoder_run(struct labcomm_decoder *d)
 {
 	return internal_decoder_run(d,d);
 }
-int internal_decoder_run(struct labcomm_decoder *d, 
+/* d            - decoder to read from
+   registry     - decoder to lookup signatures (registry != d only if
+                    nesting decoders, e.g., when decoding pragma)
+*/
+int internal_decoder_run(struct labcomm_decoder *d,
 		         struct labcomm_decoder *registry)
 {
-  int res;	
-  while ( (res=labcomm_decoder_decode_one(d)) > 0) {
+  int res;
+  while ( (res=internal_decode_one(d, registry)) > 0) {
   }
   return res;
 }
 
-int labcomm_decoder_ioctl(struct labcomm_decoder *d, 
+int labcomm_decoder_ioctl(struct labcomm_decoder *d,
 			  uint32_t action,
 			  ...)
 {
-  int result;  
+  int result;
   va_list va;
-    
+
   va_start(va, action);
-  result = labcomm_reader_ioctl(d->reader, 
+  result = labcomm_reader_ioctl(d->reader,
 				d->reader->action_context,
 				0, 0, NULL, action, va);
   va_end(va);
   return result;
 }
 
-int labcomm_internal_decoder_ioctl(struct labcomm_decoder *d, 
+int labcomm_internal_decoder_ioctl(struct labcomm_decoder *d,
 				   struct labcomm_signature *signature,
 				   uint32_t action, va_list va)
 {
@@ -445,7 +469,7 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *d,
 					     local_index)->remote_index;
   labcomm_scheduler_data_unlock(d->scheduler);
   result = labcomm_reader_ioctl(d->reader, d->reader->action_context,
-				local_index, remote_index, 
+				local_index, remote_index,
 				signature, action, va);
   return result;
 }
@@ -453,13 +477,13 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *d,
 int labcomm_internal_decoder_register(
   struct labcomm_decoder *d,
   struct labcomm_signature *signature,
-  labcomm_decoder_function decode, 
+  labcomm_decoder_function decode,
   labcomm_handler_function handler,
   void *context)
 {
   int local_index;
   struct sample_entry *entry;
- 
+
   reader_alloc(d);
   local_index = labcomm_get_local_index(signature);
   if (local_index <= 0) { goto out; }
-- 
GitLab