From f18d59c95a6ff6de8072df823492892fde22f54f Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Wed, 16 Mar 2016 16:26:53 +0100
Subject: [PATCH] - add channel estimation plot - add on/off command line
 options

---
 T_messages.txt    |  4 ++++
 tracer/Makefile   |  2 ++
 tracer/database.c | 33 +++++++++++++++++++++++++--
 tracer/defs.h     |  3 ++-
 tracer/main.c     | 57 +++++++++++++++++++++++++++++++++++++++++++----
 tracer/plot.c     | 10 +++++----
 6 files changed, 98 insertions(+), 11 deletions(-)

diff --git a/T_messages.txt b/T_messages.txt
index e5f0c916a3..043062c598 100644
--- a/T_messages.txt
+++ b/T_messages.txt
@@ -3,6 +3,10 @@ ID = ENB_INPUT_SIGNAL
     DESC = eNodeB received signal in the time domain for a duration of 1ms
     GROUP = PHY:GRAPHIC:HEAVY
     FORMAT = int,eNB_ID : int,frame : int,subframe : int,antenna : buffer,rxdata
+ID = ENB_UL_CHANNEL_ESTIMATE
+    DESC = eNodeB channel estimation in the time domain
+    GROUP = PHY:GRAPHIC:HEAVY
+    FORMAT = int,eNB_ID : int,UE_ID : int,frame : int,subframe : int,antenna : buffer,chest_t
 
 #legacy logs
 ID = LEGACY_MAC_INFO
diff --git a/tracer/Makefile b/tracer/Makefile
index 8388ec91e2..23309eb85e 100644
--- a/tracer/Makefile
+++ b/tracer/Makefile
@@ -1,6 +1,8 @@
 CC=gcc
 CFLAGS=-Wall -g -pthread
 
+CFLAGS += -O3 -ffast-math -fomit-frame-pointer
+
 LIBS=-lX11 -lm
 
 #comment those two lines to NOT use shared memory
diff --git a/tracer/database.c b/tracer/database.c
index 87e793b8c3..3172105250 100644
--- a/tracer/database.c
+++ b/tracer/database.c
@@ -9,6 +9,7 @@ typedef struct {
   char *desc;
   char **groups;
   int size;
+  int id;
 } id;
 
 typedef struct {
@@ -116,7 +117,7 @@ int string_cmp(const void *_p1, const void *_p2)
   return strcmp(*p1, *p2);
 }
 
-id *add_id(database *r, char *idname)
+id *add_id(database *r, char *idname, int i)
 {
   if (bsearch(&(id){name:idname}, r->i, r->isize, sizeof(id), id_cmp) != NULL)
     { printf("ERROR: ID '%s' declared more than once\n", idname); exit(1); }
@@ -129,6 +130,7 @@ id *add_id(database *r, char *idname)
   r->i[r->isize].desc = NULL;
   r->i[r->isize].groups = NULL;
   r->i[r->isize].size = 0;
+  r->i[r->isize].id = i;
   r->isize++;
   qsort(r->i, r->isize, sizeof(id), id_cmp);
   return (id*)bsearch(&(id){name:idname}, r->i, r->isize, sizeof(id), id_cmp);
@@ -221,6 +223,7 @@ void *parse_database(char *filename)
   database *r;
   char *name, *value;
   id *last_id = NULL;
+  int i;
 
   r = calloc(1, sizeof(*r)); if (r == NULL) abort();
   memset(&p, 0, sizeof(p));
@@ -229,11 +232,13 @@ void *parse_database(char *filename)
 
   in = fopen(filename, "r"); if (in == NULL) { perror(filename); abort(); }
 
+  i = 0;
+
   while (1) {
     get_line(&p, in, &name, &value);
     if (name == NULL) break;
 //printf("%s %s\n", name, value);
-    if (!strcmp(name, "ID")) last_id = add_id(r, value);
+    if (!strcmp(name, "ID")) { last_id = add_id(r, value, i); i++; }
     if (!strcmp(name, "GROUP")) add_groups(r, last_id, value);
     if (!strcmp(name, "DESC")) add_desc(last_id, value);
   }
@@ -280,3 +285,27 @@ void list_groups(void *_d)
   int i;
   for (i = 0; i < d->gsize; i++) printf("%s\n", d->g[i].name);
 }
+
+static void onoff_id(database *d, char *name, int *a, int onoff)
+{
+  id *i;
+  i = bsearch(&(id){name:name}, d->i, d->isize, sizeof(id), id_cmp);
+  if (i == NULL) return;
+  a[i->id] = onoff;
+  printf("turning %s %s\n", name, onoff ? "ON" : "OFF");
+}
+
+static void onoff_group(database *d, char *name, int *a, int onoff)
+{
+  group *g;
+  int i;
+  g = bsearch(&(group){name:name}, d->g, d->gsize, sizeof(group), group_cmp);
+  if (g == NULL) return;
+  for (i = 0; i < g->size; i++) onoff_id(d, g->ids[i], a, onoff);
+}
+
+void on_off(void *d, char *item, int *a, int onoff)
+{
+  onoff_group(d, item, a, onoff);
+  onoff_id(d, item, a, onoff);
+}
diff --git a/tracer/defs.h b/tracer/defs.h
index 6c2a026600..71b9dd0cfb 100644
--- a/tracer/defs.h
+++ b/tracer/defs.h
@@ -1,7 +1,7 @@
 #ifndef _TRACER_DEFS_H_
 #define _TRACER_DEFS_H_
 
-void *make_plot(int width, int height, int bufsize);
+void *make_plot(int width, int height, int bufsize, char *title);
 void plot_set(void *plot, float *data, int len, int pos);
 void iq_plot_set(void *plot, short *data, int len, int pos);
 
@@ -10,5 +10,6 @@ void *parse_database(char *filename);
 void dump_database(void *database);
 void list_ids(void *database);
 void list_groups(void *database);
+void on_off(void *d, char *item, int *a, int onoff);
 
 #endif /* _TRACER_DEFS_H_ */
diff --git a/tracer/main.c b/tracer/main.c
index fb5fcc99d9..37774be4e5 100644
--- a/tracer/main.c
+++ b/tracer/main.c
@@ -15,6 +15,7 @@
 #include "../T_defs.h"
 
 void *ul_plot;
+void *chest_plot;
 
 #ifdef T_USE_SHARED_MEMORY
 
@@ -209,11 +210,30 @@ void get_message(int s)
     GET(s, &antenna, sizeof(int));
     GET(s, &size, sizeof(int));
     GET(s, buf, size);
+#if 0
     printf("got T_ENB_INPUT_SIGNAL eNB %d frame %d subframe %d antenna %d size %d %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
            eNB, frame, subframe, antenna, size, buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);
+#endif
     if (size != 4 * 7680)
       {printf("bad T_ENB_INPUT_SIGNAL, only 7680 samples allowed\n");abort();}
-    iq_plot_set(ul_plot, (short*)buf, 7680, subframe*7680);
+    if (ul_plot) iq_plot_set(ul_plot, (short*)buf, 7680, subframe*7680);
+    break;
+  }
+  case T_ENB_UL_CHANNEL_ESTIMATE: {
+    unsigned char buf[T_BUFFER_MAX];
+    int size;
+    int eNB, UE, frame, subframe, antenna;
+    GET(s, &eNB, sizeof(int));
+    GET(s, &UE, sizeof(int));
+    GET(s, &frame, sizeof(int));
+    GET(s, &subframe, sizeof(int));
+    GET(s, &antenna, sizeof(int));
+    GET(s, &size, sizeof(int));
+    GET(s, buf, size);
+    if (size != 512*4)
+      {printf("bad T_ENB_UL_CHANNEL_ESTIMATE, only 512 samples allowed\n");
+       abort();}
+    if (chest_plot) iq_plot_set(chest_plot, (short*)buf, 512, 0);
     break;
   }
   case T_buf_test: {
@@ -265,6 +285,11 @@ void usage(void)
 "    -li                       print IDs in the database\n"
 "    -lg                       print GROUPs in the database\n"
 "    -dump                     dump the database\n"
+"    -x                        run with XFORMS (revisited)\n"
+"    -on <GROUP or ID>         turn log ON for given GROUP or ID\n"
+"    -off <GROUP or ID>        turn log OFF for given GROUP or ID\n"
+"note: you may pass several -on and -off, they will be processed in order\n"
+"      by default, all is off\n"
   );
   exit(1);
 }
@@ -280,6 +305,16 @@ int main(int n, char **v)
   int do_list_ids = 0;
   int do_list_groups = 0;
   int do_dump_database = 0;
+  int do_xforms = 0;
+  char **on_off_name;
+  int *on_off_action;
+  int on_off_n = 0;
+  int is_on[T_NUMBER_OF_IDS];
+
+  memset(is_on, 0, sizeof(is_on));
+
+  on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
+  on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();
 
   for (i = 1; i < n; i++) {
     if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
@@ -288,6 +323,12 @@ int main(int n, char **v)
     if (!strcmp(v[i], "-li")) { do_list_ids = 1; continue; }
     if (!strcmp(v[i], "-lg")) { do_list_groups = 1; continue; }
     if (!strcmp(v[i], "-dump")) { do_dump_database = 1; continue; }
+    if (!strcmp(v[i], "-x")) { do_xforms = 1; continue; }
+    if (!strcmp(v[i], "-on")) { if (i > n-2) usage();
+      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=1; continue; }
+    if (!strcmp(v[i], "-off")) { if (i > n-2) usage();
+      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=0; continue; }
+    printf("ERROR: unknown option %s\n", v[i]);
     usage();
   }
 
@@ -303,7 +344,13 @@ int main(int n, char **v)
   if (do_list_groups) { list_groups(database); return 0; }
   if (do_dump_database) { dump_database(database); return 0; }
 
-  ul_plot = make_plot(512, 100, 7680*2*10);
+  if (do_xforms) {
+    ul_plot = make_plot(512, 100, 7680*2*10, "UL Input Signal");
+    chest_plot = make_plot(512, 100, 512*2, "UL Channel Estimate UE 0");
+  }
+
+  for (i = 0; i < on_off_n; i++)
+    on_off(database, on_off_name[i], is_on, on_off_action[i]);
 
 #ifdef T_USE_SHARED_MEMORY
   init_shm();
@@ -312,10 +359,12 @@ int main(int n, char **v)
   /* send the first message - activate all traces */
   t = 0;
   if (write(s, &t, 1) != 1) abort();
-  l = T_NUMBER_OF_IDS;
+  l = 0;
+  for (i = 0; i < T_NUMBER_OF_IDS; i++) if (is_on[i]) l++;
   if (write(s, &l, sizeof(int)) != sizeof(int)) abort();
   for (l = 0; l < T_NUMBER_OF_IDS; l++)
-    if (write(s, &l, sizeof(int)) != sizeof(int)) abort();
+    if (is_on[l])
+      if (write(s, &l, sizeof(int)) != sizeof(int)) abort();
 
   /* read messages */
   while (1) {
diff --git a/tracer/plot.c b/tracer/plot.c
index ba203b0886..3cc0ad069f 100644
--- a/tracer/plot.c
+++ b/tracer/plot.c
@@ -44,15 +44,15 @@ static void *plot_thread(void *_p)
 
   {
     int i;
-    for (i = 0; i < 512*150; i++)
+    for (i = 0; i < p->bufsize/2; i++)
       p->buf[i] = 10*log10(1.0+(float)(p->iqbuf[2*i]*p->iqbuf[2*i]+
                                        p->iqbuf[2*i+1]*p->iqbuf[2*i+1]));
   }
     s = p->buf;
     for (i = 0; i < 512; i++) {
       v = 0;
-      for (j = 0; j < 150; j++, s++) v += *s;
-      v /= 150;
+      for (j = 0; j < p->bufsize/2/512; j++, s++) v += *s;
+      v /= p->bufsize/2/512;
       XDrawLine(p->d, p->p, DefaultGC(p->d, DefaultScreen(p->d)), i, 100, i, 100-v);
     }
 
@@ -79,7 +79,7 @@ static void new_thread(void *(*f)(void *), void *data)
     { fprintf(stderr, "pthread_attr_destroy err\n"); exit(1); }
 }
 
-void *make_plot(int width, int height, int bufsize)
+void *make_plot(int width, int height, int bufsize, char *title)
 {
   plot *p;
   Display *d;
@@ -92,6 +92,8 @@ void *make_plot(int width, int height, int bufsize)
   XSelectInput(d, w, ExposureMask);
   XMapWindow(d, w);
 
+  XStoreName(d, w, title);
+
   pm = XCreatePixmap(d, w, width, height, DefaultDepth(d, DefaultScreen(d)));
 
   p = malloc(sizeof(*p)); if (p == NULL) abort();
-- 
GitLab