From a8def02fbf3d4e0d5b25fd409db88c406e759699 Mon Sep 17 00:00:00 2001
From: Sandeep Kumar <ee13b1025@iith.ac.in>
Date: Mon, 20 Jun 2016 18:36:17 +0200
Subject: [PATCH] starting to add if4 specific eth read, write

---
 targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c | 94 ++++++++++++++++++-
 .../ETHERNET/USERSPACE/LIB/ethernet_lib.c     | 21 ++++-
 .../ETHERNET/USERSPACE/LIB/ethernet_lib.h     | 10 ++
 3 files changed, 119 insertions(+), 6 deletions(-)

diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
index a360f26556..14561a25e7 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
@@ -140,7 +140,7 @@ int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, voi
   //sendto_flag|=flags;
 
   eth->tx_nsamps=nsamps;
-
+  
   for (i=0;i<cc;i++) {	
     /* buff[i] points to the position in tx buffer where the payload to be sent is
        buff2 points to the position in tx buffer where the packet header will be placed */
@@ -198,6 +198,48 @@ int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, voi
 }
 
 
+
+int trx_eth_write_raw_IF4(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {
+
+  int nblocks = nsamps;  
+  int bytes_sent = 0;
+  
+  eth_state_t *eth = (eth_state_t*)device->priv;
+  int Mod_id = device->Mod_id;  
+  
+  ssize_t packet_size;
+  
+  if (flags == IF4_PDLFFT) {
+    packet_size = RAW_IF4_PDLFFT_SIZE_BYTES(nblocks);    
+  } else if (flags == IF4_PULFFT) {
+    packet_size = RAW_IF4_PULFFT_SIZE_BYTES(nblocks);    
+  } else {
+    packet_size = RAW_IF4_PRACH_SIZE_BYTES(nblocks);
+  }
+  
+  eth->tx_nsamps = nblocks;
+  
+  memcpy(buff[0], (void*)&eh, MAC_HEADER_SIZE_BYTES);	
+         
+  bytes_sent = send(eth->sockfd[Mod_id],
+                    buff[0], 
+                    packet_size,
+                    0);
+  
+  if (bytes_sent == -1) {
+    eth->num_tx_errors++;
+    perror("ETHERNET WRITE: ");
+    exit(-1);
+  } else {
+    eth->tx_actual_nsamps = bytes_sent>>1;
+    eth->tx_count++;
+  }
+  
+  return (bytes_sent-MAC_HEADER_SIZE_BYTES);  	  
+}
+
+
+
 int trx_eth_read_raw(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc) {
       
   int bytes_received=0;
@@ -256,7 +298,55 @@ int trx_eth_read_raw(openair0_device *device, openair0_timestamp *timestamp, voi
   return (bytes_received-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES)>>2;
 }
 
- 
+
+
+int trx_eth_read_raw_IF4(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc) {
+
+  int bytes_received=0;
+  int i=0;
+  eth_state_t *eth = (eth_state_t*)device->priv;
+  int Mod_id = device->Mod_id;
+  int rcvfrom_flag =0;
+  
+  eth->rx_nsamps=nsamps;
+
+      /* buff[i] points to the position in rx buffer where the payload to be received will be placed
+	 buff2 points to the position in rx buffer where the packet header will be placed */
+      void *buff2 = (void*)(buff[i]-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES); 
+      
+      /* we don't want to ovewrite with the header info the previous rx buffer data so we store it*/
+      struct ether_header temp =  *(struct ether_header *)buff2;
+      int32_t temp0 = *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES);
+      openair0_timestamp  temp1 = *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t));
+      
+      bytes_received=0;
+      
+      while(bytes_received < RAW_PACKET_SIZE_BYTES(nsamps)) {
+	bytes_received +=recv(eth->sockfd[Mod_id],
+			      buff2,
+			      RAW_PACKET_SIZE_BYTES(nsamps),
+			      rcvfrom_flag);
+	
+	if (bytes_received ==-1) {
+	  eth->num_rx_errors++;
+	  perror("ETHERNET READ: ");
+	  exit(-1);	
+	} else {
+	  /* store the timestamp value from packet's header */
+	  *timestamp =  *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t));  
+	  eth->rx_actual_nsamps=bytes_received>>2;   
+	  eth->rx_count++;
+	}
+      }
+      
+     /* tx buffer values restored */  
+      *(struct ether_header *)buff2 = temp;
+      *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES) = temp0;
+      *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)) = temp1;
+
+  return (bytes_received-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES)>>2;
+}
+
 
 
 int eth_set_dev_conf_raw(openair0_device *device) {
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
index f15f2d98eb..7537d77e2e 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
@@ -313,8 +313,14 @@ int transport_init(openair0_device *device, openair0_config_t *openair0_cfg, eth
 
   if (eth_params->transp_preference == 1) {
     eth->flags = ETH_RAW_MODE;
-  } else {
+  } else if (eth_params->transp_preference == 0) {
     eth->flags = ETH_UDP_MODE;
+  } else if (eth_params->transp_preference == 3) {
+    eth->flags = ETH_RAW_IF4_MODE;
+  } else if (eth_params->transp_preference == 2) {
+    eth->flags = ETH_UDP_IF4_MODE;
+  } else {
+    AssertFatal(0==4, "transport_init: Unknown transport preference %d", eth_params->transp_preference);
   }
   
   printf("[ETHERNET]: Initializing openair0_device for %s ...\n", ((device->host_type == BBU_HOST) ? "BBU": "RRH"));
@@ -330,14 +336,21 @@ int transport_init(openair0_device *device, openair0_config_t *openair0_cfg, eth
   device->trx_set_freq_func = trx_eth_set_freq;
   device->trx_set_gains_func = trx_eth_set_gains;
 
-  if ((eth->flags & ETH_RAW_MODE) != 0 ) {
+  /// handle if4 eth read and write functions
+  if (eth->flags == ETH_RAW_MODE) {
     device->trx_write_func   = trx_eth_write_raw;
     device->trx_read_func    = trx_eth_read_raw;     
-  } else {
+  } else if (eth->flags == ETH_UDP_MODE) {
     device->trx_write_func   = trx_eth_write_udp;
     device->trx_read_func    = trx_eth_read_udp;     
+  } else if (eth->flags == ETH_RAW_IF4_MODE) {
+    device->trx_write_func   = trx_eth_write_raw_IF4;
+    device->trx_read_func    = trx_eth_read_raw_IF4;     
+  } else {
+    device->trx_write_func   = trx_eth_write_udp_IF4;
+    device->trx_read_func    = trx_eth_read_udp_IF4;     
   }
-
+    
   eth->if_name[device->Mod_id] = eth_params->local_if_name;
   device->priv = eth;
  	
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
index b4618bb825..beb324cc59 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
@@ -54,6 +54,8 @@
 
 #define ETH_RAW_MODE        1
 #define ETH_UDP_MODE        0
+#define ETH_RAW_IF4_MODE    3
+#define ETH_UDP_IF4_MODE    2
 
 #define TX_FLAG	        1
 #define RX_FLAG 	0
@@ -65,6 +67,10 @@
 #define UDP_PACKET_SIZE_BYTES(nsamps) (APP_HEADER_SIZE_BYTES + PAYLOAD_SIZE_BYTES(nsamps))
 #define RAW_PACKET_SIZE_BYTES(nsamps) (APP_HEADER_SIZE_BYTES + MAC_HEADER_SIZE_BYTES + PAYLOAD_SIZE_BYTES(nsamps))
 
+#define DATA_BLOCK_SIZE_BYTES(scaled_nblocks) (2*scaled_nblocks)
+#define RAW_IF4_PDLFFT_SIZE_BYTES(nblocks) (MAC_HEADER_SIZE_BYTES + sizeof_IF4_dl_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
+#define RAW_IF4_PULFFT_SIZE_BYTES(nblocks) (MAC_HEADER_SIZE_BYTES + sizeof_IF4_ul_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
+#define RAW_IF4_PRACH_SIZE_BYTES(nblocks) (MAC_HEADER_SIZE_BYTES + sizeof_IF4_prach_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
 
 /*!\brief opaque ethernet data structure */
 typedef struct {
@@ -212,6 +218,8 @@ int ethernet_tune(openair0_device *device, unsigned int option, int value);
 int eth_socket_init_udp(openair0_device *device);
 int trx_eth_write_udp(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
 int trx_eth_read_udp(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
+//int trx_eth_write_udp_IF4(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
+//int trx_eth_read_udp_IF4(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
 int eth_get_dev_conf_udp(openair0_device *device);
 
 /*! \fn static int eth_set_dev_conf_udp(openair0_device *device)
@@ -226,6 +234,8 @@ int eth_set_dev_conf_udp(openair0_device *device);
 int eth_socket_init_raw(openair0_device *device);
 int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
 int trx_eth_read_raw(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
+//int trx_eth_write_raw_IF4(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
+//int trx_eth_read_raw_IF4(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
 int eth_get_dev_conf_raw(openair0_device *device);
 int eth_set_dev_conf_raw(openair0_device *device);
 
-- 
2.26.2