From abe278030d4050174c0ceee023ef8839faf4862c Mon Sep 17 00:00:00 2001
From: jiangx <jiangx@eurecom.fr>
Date: Mon, 9 Mar 2015 16:54:18 +0000
Subject: [PATCH] reactivated EMOS

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@6734 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 openair1/SCHED/phy_procedures_lte_eNb.c |  10 +-
 openair1/SCHED/phy_procedures_lte_ue.c  |  16 +--
 targets/RT/USER/lte-softmodem.c         | 144 +++++++++++++++---------
 targets/RT/USER/lte-ue.c                |   8 +-
 4 files changed, 111 insertions(+), 67 deletions(-)

diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index cc7f821ea57..792be8dd2a3 100755
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -369,7 +369,7 @@ int16_t get_target_ul_rx_power(module_id_t module_idP, uint8_t CC_id) {
 }
 
 #ifdef EMOS
-void phy_procedures_emos_eNB_TX(unsigned char next_slot, PHY_VARS_eNB *phy_vars_eNB) {
+void phy_procedures_emos_eNB_TX(unsigned char subframe, PHY_VARS_eNB *phy_vars_eNB) {
 
 }
 #endif
@@ -482,7 +482,7 @@ void phy_procedures_emos_eNB_RX(unsigned char subframe,PHY_VARS_eNB *phy_vars_eN
 
   if (subframe==4) {
     emos_dump_eNB.timestamp = rt_get_time_ns();
-    emos_dump_eNB.frame_tx = phy_vars_eNB->proc[subframe].frame;
+    emos_dump_eNB.frame_tx = phy_vars_eNB->proc[subframe].frame_rx;
     emos_dump_eNB.rx_total_gain_dB = phy_vars_eNB->rx_total_gain_eNB_dB;
     emos_dump_eNB.mimo_mode = phy_vars_eNB->transmission_mode[0];
     memcpy(&emos_dump_eNB.PHY_measurements_eNB,
@@ -494,12 +494,12 @@ void phy_procedures_emos_eNB_RX(unsigned char subframe,PHY_VARS_eNB *phy_vars_eN
     //bytes = rtf_put(CHANSOUNDER_FIFO_MINOR, "test", sizeof("test"));
     if (bytes!=sizeof(fifo_dump_emos_eNB)) {
       LOG_W(PHY,"[eNB %d] Frame %d, subframe %d, Problem writing EMOS data to FIFO (bytes=%d, size=%d)\n",
-            phy_vars_eNB->Mod_id,phy_vars_eNB->frame, subframe,bytes,sizeof(fifo_dump_emos_eNB));
+            phy_vars_eNB->Mod_id,phy_vars_eNB->proc[(subframe+1)%10].frame_rx, subframe,bytes,sizeof(fifo_dump_emos_eNB));
     }
     else {
       if (phy_vars_eNB->proc[(subframe+1)%10].frame_tx%100==0) {
         LOG_I(PHY,"[eNB %d] Frame %d (%d), subframe %d, Writing %d bytes EMOS data to FIFO\n",
-              phy_vars_eNB->Mod_id,phy_vars_eNB->proc[(subframe+1)%10]->frame_tx, ((fifo_dump_emos_eNB*)&emos_dump_eNB)->frame_tx, subframe, bytes);
+              phy_vars_eNB->Mod_id,phy_vars_eNB->proc[(subframe+1)%10].frame_rx, ((fifo_dump_emos_eNB*)&emos_dump_eNB)->frame_tx, subframe, bytes);
       }
     }
   }
@@ -2399,7 +2399,7 @@ void phy_procedures_eNB_TX(unsigned char sched_subframe,PHY_VARS_eNB *phy_vars_e
 
      
 #ifdef EMOS
-  phy_procedures_emos_eNB_TX(next_slot, phy_vars_eNB);
+  phy_procedures_emos_eNB_TX(subframe, phy_vars_eNB);
 #endif
 
 #ifndef EXMIMO
diff --git a/openair1/SCHED/phy_procedures_lte_ue.c b/openair1/SCHED/phy_procedures_lte_ue.c
index 0bb887d8510..8ededde4304 100755
--- a/openair1/SCHED/phy_procedures_lte_ue.c
+++ b/openair1/SCHED/phy_procedures_lte_ue.c
@@ -1589,7 +1589,7 @@ void phy_procedures_emos_UE_RX(PHY_VARS_UE *phy_vars_ue,uint8_t last_slot,uint8_
   
   if (last_slot==0) {
     emos_dump_UE.timestamp = rt_get_time_ns();
-    emos_dump_UE.frame_rx = phy_vars_ue->frame;
+    emos_dump_UE.frame_rx = phy_vars_ue->frame_rx;
     emos_dump_UE.UE_mode = phy_vars_ue->UE_mode[eNB_id];
     emos_dump_UE.mimo_mode = phy_vars_ue->transmission_mode[eNB_id];
     emos_dump_UE.freq_offset = phy_vars_ue->lte_ue_common_vars.freq_offset;
@@ -1623,11 +1623,11 @@ void phy_procedures_emos_UE_RX(PHY_VARS_UE *phy_vars_ue,uint8_t last_slot,uint8_
 
     bytes = rtf_put(CHANSOUNDER_FIFO_MINOR, &emos_dump_UE, sizeof(fifo_dump_emos_UE));
     if (bytes!=sizeof(fifo_dump_emos_UE)) {
-      LOG_W(PHY,"[UE  %d] frame %d, slot %d, Problem writing EMOS data to FIFO\n",Mod_id,phy_vars_ue->frame, last_slot);
+      LOG_W(PHY,"[UE  %d] frame %d, slot %d, Problem writing EMOS data to FIFO\n",Mod_id,phy_vars_ue->frame_rx, last_slot);
     }
     else {
-      if (phy_vars_ue->frame%100==0) {
-	LOG_I(PHY,"[UE  %d] frame %d, slot %d, Writing %d bytes EMOS data to FIFO\n",Mod_id,phy_vars_ue->frame, last_slot, bytes);
+      if (phy_vars_ue->frame_rx%100==0) {
+	LOG_I(PHY,"[UE  %d] frame %d, slot %d, Writing %d bytes EMOS data to FIFO\n",Mod_id,phy_vars_ue->frame_rx, last_slot, bytes);
       }
     }
   }
@@ -3281,6 +3281,11 @@ int lte_ue_pdcch_procedures(uint8_t eNB_id,PHY_VARS_UE *phy_vars_ue,uint8_t abst
       }
     }
   }
+
+#ifdef EMOS
+    phy_procedures_emos_UE_RX(phy_vars_ue,slot_rx,eNB_id);
+#endif
+
   vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_UE_RX, VCD_FUNCTION_OUT);
   stop_meas(&phy_vars_ue->phy_proc_rx);
   return (0);
@@ -3617,9 +3622,6 @@ void phy_UE_lte_check_measurement_thresholds(instance_t instanceP, ral_threshold
     if (phy_procedures_RN_UE_RX(slot_rx, slot_tx, r_type) != 0 )
 #endif 
       phy_procedures_UE_RX(phy_vars_ue,eNB_id,abstraction_flag,mode,r_type,phy_vars_rn);
-#ifdef EMOS
-    phy_procedures_emos_UE_RX(phy_vars_ue,slot_rx,eNB_id);
-#endif
   }
   if ((subframe_select(&phy_vars_ue->lte_frame_parms,subframe_tx)==SF_S) &&
       ((slot_tx&1)==1)) {
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index d8f99ae94d3..bf2a5cf90a9 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -65,6 +65,7 @@ static int hw_subframe;
 
 #ifdef EMOS
 #include <gps.h>
+struct gps_fix_t dummy_gps_data;
 #endif
 
 #include "PHY/types.h"
@@ -205,6 +206,7 @@ static pthread_t                forms_thread; //xforms
 #endif
 #ifdef EMOS
 static pthread_t                thread3; //emos
+static pthread_t                thread4; //GPS
 #endif
 
 openair0_device openair0;
@@ -555,6 +557,73 @@ static void *scope_thread(void *arg) {
 #ifdef EMOS
 #define NO_ESTIMATES_DISK 100 //No. of estimates that are aquired before dumped to disk
 
+void* gps_thread (void *arg)
+{
+
+  struct gps_data_t gps_data;
+  struct gps_data_t *gps_data_ptr = &gps_data;
+  struct sched_param sched_param;
+  int ret;
+
+  sched_param.sched_priority = sched_get_priority_min(SCHED_FIFO)+1; 
+  sched_setscheduler(0, SCHED_FIFO,&sched_param);
+  
+  printf("GPS thread has priority %d\n",sched_param.sched_priority);
+
+  memset(&dummy_gps_data,0,sizeof(struct gps_fix_t));
+  
+#if GPSD_API_MAJOR_VERSION>=5
+  ret = gps_open("127.0.0.1","2947",gps_data_ptr);
+  if (ret!=0)
+#else
+  gps_data_ptr = gps_open("127.0.0.1","2947");
+  if (gps_data_ptr == NULL) 
+#endif
+    {
+      printf("[EMOS] Could not open GPS\n");
+      pthread_exit((void*)arg);
+    }
+#if GPSD_API_MAJOR_VERSION>=4
+  else if (gps_stream(gps_data_ptr, WATCH_ENABLE,NULL) != 0)
+#else
+  else if (gps_query(gps_data_ptr, "w+x") != 0)
+#endif
+    {
+      printf("[EMOS] Error sending command to GPS\n");
+      pthread_exit((void*) arg);
+    }
+  else 
+    printf("[EMOS] Opened GPS, gps_data=%p\n", gps_data_ptr);
+  
+
+  while (!oai_exit)
+    {
+      printf("[EMOS] polling data from gps\n");
+#if GPSD_API_MAJOR_VERSION>=5
+      if (gps_waiting(gps_data_ptr,500)) {
+	if (gps_read(gps_data_ptr) <= 0) {
+#else
+      if (gps_waiting(gps_data_ptr)) {
+	if (gps_poll(gps_data_ptr) != 0) {
+#endif
+	  printf("[EMOS] problem polling data from gps\n");
+	}
+	else {
+	  memcpy(&dummy_gps_data,&(gps_data_ptr->fix),sizeof(struct gps_fix_t));
+	  printf("[EMOS] lat %g, lon %g\n",gps_data_ptr->fix.latitude,gps_data_ptr->fix.longitude);
+	}
+      } //gps_waiting
+      else {
+	printf("[EMOS] WARNING: No GPS data available, storing dummy packet\n");
+      }
+      //rt_sleep_ns(1000000000LL);
+      sleep(1);
+    } //oai_exit
+
+  pthread_exit((void*) arg);
+
+}
+
 void *emos_thread (void *arg)
 {
   char c;
@@ -567,14 +636,11 @@ void *emos_thread (void *arg)
   time_t starttime_tmp;
   struct tm starttime;
   
-  int channel_buffer_size;
+  int channel_buffer_size,ret;
   
   time_t timer;
   struct tm *now;
 
-  struct gps_data_t *gps_data = NULL;
-  struct gps_fix_t dummy_gps_data;
-
   struct sched_param sched_param;
   
   sched_param.sched_priority = sched_get_priority_max(SCHED_FIFO)-1; 
@@ -585,27 +651,6 @@ void *emos_thread (void *arg)
   timer = time(NULL);
   now = localtime(&timer);
 
-  memset(&dummy_gps_data,1,sizeof(struct gps_fix_t));
-  
-  gps_data = gps_open("127.0.0.1","2947");
-  if (gps_data == NULL) 
-    {
-      printf("[EMOS] Could not open GPS\n");
-      //exit(-1);
-    }
-#if GPSD_API_MAJOR_VERSION>=4
-  else if (gps_stream(gps_data, WATCH_ENABLE,NULL) != 0)
-#else
-  else if (gps_query(gps_data, "w+x") != 0)
-#endif
-    {
-      //sprintf(tmptxt,"Error sending command to GPS, gps_data = %x", gps_data);
-      printf("[EMOS] Error sending command to GPS\n");
-      //exit(-1);
-    }
-  else 
-    printf("[EMOS] Opened GPS, gps_data=%p\n");
-  
   if (UE_flag==0)
     channel_buffer_size = sizeof(fifo_dump_emos_eNB);
   else
@@ -642,17 +687,26 @@ void *emos_thread (void *arg)
     }
 
 
-  printf("[EMOS] starting dump, channel_buffer_size=%d ...\n",channel_buffer_size);
+  printf("[EMOS] starting dump, channel_buffer_size=%d, fifo %d\n",channel_buffer_size,fifo);
   while (!oai_exit)
     {
+      /*
       bytes = rtf_read_timed(fifo, fifo2file_ptr, channel_buffer_size,100);
       if (bytes==0)
 	continue;
-
+      */
+      bytes = rtf_read_all_at_once(fifo, fifo2file_ptr, channel_buffer_size);
+      if (bytes<=0) {
+	usleep(100);
+	continue;
+      }
+      if (bytes != channel_buffer_size) {
+	printf("[EMOS] ERROR! Only got %d bytes instead of %d!\n",bytes,channel_buffer_size);
+      }
       /*
-	if (UE_flag==0)
+      if (UE_flag==0)
 	printf("eNB: count %d, frame %d, read: %d bytes from the fifo\n",counter, ((fifo_dump_emos_eNB*)fifo2file_ptr)->frame_tx,bytes);
-	else
+      else
 	printf("UE: count %d, frame %d, read: %d bytes from the fifo\n",counter, ((fifo_dump_emos_UE*)fifo2file_ptr)->frame_rx,bytes);
       */
 
@@ -679,30 +733,13 @@ void *emos_thread (void *arg)
               fprintf(stderr, "[EMOS] Error writing to dumpfile\n");
               exit(EXIT_FAILURE);
             }
-	  if (gps_data)
+
+	  if (fwrite(&dummy_gps_data, sizeof(char), sizeof(struct gps_fix_t), dumpfile_id) != sizeof(struct gps_fix_t))
 	    {
-	      if (gps_poll(gps_data) != 0) {
-		printf("[EMOS] problem polling data from gps\n");
-	      }
-	      else {
-		printf("[EMOS] lat %g, lon %g\n",gps_data->fix.latitude,gps_data->fix.longitude);
-	      }
-	      if (fwrite(&(gps_data->fix), sizeof(char), sizeof(struct gps_fix_t), dumpfile_id) != sizeof(struct gps_fix_t))
-		{
-		  printf("[EMOS] Error writing to dumpfile, stopping recording\n");
-		  exit(EXIT_FAILURE);
-		}
+	      printf("[EMOS] Error writing to dumpfile, stopping recording\n");
+	      exit(EXIT_FAILURE);
 	    }
-	  else
-	    {
-	      printf("[EMOS] WARNING: No GPS data available, storing dummy packet\n");
-	      if (fwrite(&(dummy_gps_data), sizeof(char), sizeof(struct gps_fix_t), dumpfile_id) != sizeof(struct gps_fix_t))
-		{
-		  printf("[EMOS] Error writing to dumpfile, stopping recording\n");
-		  exit(EXIT_FAILURE);
-		}
-	    } 
-        }
+	}
     }
   
   free(fifo2file_buffer);
@@ -2756,6 +2793,8 @@ int main(int argc, char **argv) {
 #ifdef EMOS
   ret = pthread_create(&thread3, NULL, emos_thread, NULL);
   printf("EMOS thread created, ret=%d\n",ret);
+  ret = pthread_create(&thread4, NULL, gps_thread, NULL);
+  printf("GPS thread created, ret=%d\n",ret);
 #endif
 
   rt_sleep_ns(10*FRAME_PERIOD);
@@ -2938,6 +2977,9 @@ int main(int argc, char **argv) {
   printf("waiting for EMOS thread\n");
   pthread_cancel(thread3);
   pthread_join(thread3,&status);
+  printf("waiting for GPS thread\n");
+  pthread_cancel(thread4);
+  pthread_join(thread4,&status);
 #endif
 
 #ifdef EMOS
diff --git a/targets/RT/USER/lte-ue.c b/targets/RT/USER/lte-ue.c
index 444a98e5476..08936e7c46b 100644
--- a/targets/RT/USER/lte-ue.c
+++ b/targets/RT/USER/lte-ue.c
@@ -312,7 +312,7 @@ static void *UE_thread_synch(void *arg) {
     }  // mutex_lock      
 
     vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH,1); 
-    printf("Sync_mode %d\n",sync_mode);
+    //printf("Sync_mode %d\n",sync_mode);
     switch (sync_mode) {
       case pss:
 	
@@ -640,7 +640,7 @@ static void *UE_thread_rx(void *arg) {
   UE->instance_cnt_rx=-1;
 
 #ifdef RTAI
-  task = rt_task_init_schmod(nam2num("UE Thread RX"), 0, 0, 0, SCHED_FIFO, 0xF);
+  task = rt_task_init_schmod(nam2num("UE RX Thread"), 0, 0, 0, SCHED_FIFO, 0xF);
   if (task==NULL) {
     LOG_E(PHY,"[SCHED][UE] Problem starting UE RX thread!!!!\n");
     return 0;
@@ -1343,13 +1343,13 @@ void *UE_thread(void *arg) {
 
 
       // wait until we can lock mutex_synch
-      printf("Locking mutex_synch (UE_thread)\n");
+      //printf("Locking mutex_synch (UE_thread)\n");
       if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
 	LOG_E(PHY,"[SCHED][UE] error locking mutex for UE initial synch thread\n");
 	exit_fun("noting to add");
       }
       else {
-	printf("Before getting frame IC %d (UE_thread)\n",UE->instance_cnt_synch);
+	//printf("Before getting frame IC %d (UE_thread)\n",UE->instance_cnt_synch);
 	if (UE->instance_cnt_synch < 0) {
 
 	  wait_sync_cnt=0;
-- 
GitLab