if4_tools.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr

  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

 *******************************************************************************/

/*! \file PHY/LTE_TRANSPORT/if4_tools.c
* \brief 
32
* \author Fredrik Skretteberg, Tobias Schuster, Mauricio Gunther, S. Sandeep Kumar, Raymond Knopp
33 34 35
* \date 2016
* \version 0.1
* \company Eurecom
36
* \email: knopp@eurecom.fr 
37 38 39 40
* \note
* \warning
*/

41
#include <stdint.h>
42

43
#include "PHY/defs.h"
44
#include "PHY/LTE_TRANSPORT/if4_tools.h"
45
#include "PHY/TOOLS/ALAW/alaw_lut.h"
46

47 48 49
#include "targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h"


50
// --- Careful to handle buffer memory --- RAW/UDP modes --- PRACH variables and data
51
void send_IF4(PHY_VARS_eNB *eNB, int frame, int subframe, uint16_t packet_type, int k) {
52 53
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  int32_t **txdataF = eNB->common_vars.txdataF[0];
54
  int32_t **rxdataF = eNB->common_vars.rxdataF[0];
55
  int16_t **rxsigF = eNB->prach_vars.rxsigF;  
56
      
57
  uint16_t symbol_id=0, element_id=0;
58
  uint16_t db_fulllength, db_halflength; 
59
  int slotoffsetF=0, blockoffsetF=0; 
60

61
  void *tx_buffer=NULL;
62
  int16_t *data_block=NULL;
63

64
  if (packet_type == IF4_PDLFFT) {
65 66
    db_fulllength = 12*fp->N_RB_DL;
    db_halflength = (db_fulllength)>>1;
67
    slotoffsetF = (subframe)*(fp->ofdm_symbol_size)*((fp->Ncp==1) ? 12 : 14) + 1;
68
    blockoffsetF = slotoffsetF + fp->ofdm_symbol_size - db_halflength; 
69

70 71 72
    tx_buffer = malloc(MAC_HEADER_SIZE_BYTES + sizeof_IF4_dl_header_t + db_fulllength*sizeof(int16_t));
    IF4_dl_header_t *dl_header = (IF4_dl_header_t *)(tx_buffer + MAC_HEADER_SIZE_BYTES);
    data_block = (int16_t*)(tx_buffer + MAC_HEADER_SIZE_BYTES + sizeof_IF4_dl_header_t);
73

74
    gen_IF4_dl_header(dl_header, frame, subframe);
75 76
		    
    for (symbol_id=0; symbol_id<fp->symbols_per_tti; symbol_id++) {
77 78
      // Do compression of the two parts and generate data blocks			
      for (element_id=0; element_id<db_halflength; element_id++) {
79 80
        data_block[element_id]  = lin2alaw[ (txdataF[0][blockoffsetF+element_id] & 0xffff) + 32768 ];          
        data_block[element_id] |= lin2alaw[ (txdataF[0][blockoffsetF+element_id]>>16) + 32768 ]<<8;  
81
        
82 83
        data_block[element_id+db_halflength]  = lin2alaw[ (txdataF[0][slotoffsetF+element_id] & 0xffff) + 32768 ];     
        data_block[element_id+db_halflength] |= lin2alaw[ (txdataF[0][slotoffsetF+element_id]>>16) + 32768 ]<<8;  
84 85
      }
				 		
86
      // Update information in generated packet
87
      dl_header->frame_status.sym_num = symbol_id; 
88
			
89
      // Write the packet to the fronthaul
90
      if ((eNB->ifdevice.trx_write_func(&eNB->ifdevice,
91
                                        symbol_id,
92
                                        &tx_buffer,
93 94 95 96
                                        db_fulllength,
      			                            1,
                                        IF4_PDLFFT)) < 0) {
        perror("ETHERNET write for IF4_PDLFFT\n");
97
      }
98 99 100
      
      slotoffsetF  += fp->ofdm_symbol_size;
      blockoffsetF += fp->ofdm_symbol_size;    
101
    }
102
  } else if (packet_type == IF4_PULFFT) {
103 104
    db_fulllength = 12*fp->N_RB_UL;
    db_halflength = (db_fulllength)>>1;
105
    slotoffsetF = (subframe)*(fp->ofdm_symbol_size)*((fp->Ncp==1) ? 12 : 14) + 1;
106
    blockoffsetF = slotoffsetF + fp->ofdm_symbol_size - db_halflength; 
107

108
    tx_buffer = malloc(MAC_HEADER_SIZE_BYTES + sizeof_IF4_ul_header_t + db_fulllength*sizeof(int16_t));
109 110
    IF4_ul_header_t *ul_header = (IF4_ul_header_t *)(tx_buffer + MAC_HEADER_SIZE_BYTES);
    data_block = (int16_t*)(tx_buffer + MAC_HEADER_SIZE_BYTES + sizeof_IF4_ul_header_t);
111

112
    gen_IF4_ul_header(ul_header, frame, subframe);
113

114
    for (symbol_id=0; symbol_id<fp->symbols_per_tti; symbol_id++) {			
115
      // Do compression of the two parts and generate data blocks - rxdataF		
116
      for (element_id=0; element_id<db_halflength; element_id++) {
117 118
        data_block[element_id]  = lin2alaw[ (rxdataF[0][blockoffsetF+element_id] & 0xffff) + 32768 ];          
        data_block[element_id] |= lin2alaw[ (rxdataF[0][blockoffsetF+element_id]>>16) + 32768 ]<<8;  
119
        
120 121
        data_block[element_id+db_halflength]  = lin2alaw[ (rxdataF[0][slotoffsetF+element_id] & 0xffff) + 32768 ];     
        data_block[element_id+db_halflength] |= lin2alaw[ (rxdataF[0][slotoffsetF+element_id]>>16) + 32768 ]<<8;  
122 123
      }
       			
124
      // Update information in generated packet
125
      ul_header->frame_status.sym_num = symbol_id; 
126 127
			
      // Write the packet(s) to the fronthaul 
128
      if ((eNB->ifdevice.trx_write_func(&eNB->ifdevice,
129
                                        symbol_id,
130
                                        &tx_buffer,
131 132 133 134 135
                                        db_fulllength,
      			                            1,
                                        IF4_PULFFT)) < 0) {
        perror("ETHERNET write for IF4_PULFFT\n");
      }
136

137 138
      slotoffsetF  += fp->ofdm_symbol_size;
      blockoffsetF += fp->ofdm_symbol_size;    
139
    }		
140
  } else if (packet_type == IF4_PRACH) {
141
    // FIX: hard coded prach samples length
142
    db_fulllength = 839*2;
143

144 145 146
    tx_buffer = malloc(MAC_HEADER_SIZE_BYTES + sizeof_IF4_prach_header_t + db_fulllength*sizeof(int16_t));
    IF4_prach_header_t *prach_header = (IF4_prach_header_t *)(tx_buffer + MAC_HEADER_SIZE_BYTES);
    data_block = (int16_t*)(tx_buffer + MAC_HEADER_SIZE_BYTES + sizeof_IF4_prach_header_t);
147

148
    gen_IF4_prach_header(prach_header, frame, subframe);
149
		    
150 151 152 153 154 155 156 157
    // Generate uncompressed data blocks
    memcpy(data_block, (rxsigF[0]+k), db_fulllength*sizeof(int16_t));
    			
    //for (element_id=0; element_id<db_fulllength; element_id++) {
    //  data_block[element_id]  = rxsigF[0][prachoffsetF];          
    //  data_block[element_id] |= rxsigF[0][prachoffsetF+1]<<16;
    //  prachoffsetF += 2;  
    //}
158 159 160
              
    // Write the packet to the fronthaul
    if ((eNB->ifdevice.trx_write_func(&eNB->ifdevice,
161
                                      symbol_id,
162
                                      &tx_buffer,
163 164 165 166 167
                                      db_fulllength,
                                      1,
                                      IF4_PRACH)) < 0) {
      perror("ETHERNET write for IF4_PRACH\n");
    }      
168 169 170 171
  } else {    
    AssertFatal(1==0, "send_IF4 - Unknown packet_type %x", packet_type);     
  }
  
172
  free(tx_buffer);
173
  return;  		    
174
}
175

176

177
void recv_IF4(PHY_VARS_eNB *eNB, int frame, int subframe, uint16_t *packet_type, uint32_t *symbol_number, uint16_t expected_packet) {
178 179 180
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  int32_t **txdataF = eNB->common_vars.txdataF[0];
  int32_t **rxdataF = eNB->common_vars.rxdataF[0];
181
  int16_t **rxsigF = eNB->prach_vars.rxsigF;  
182 183

  uint16_t element_id;
184
  uint16_t db_fulllength, db_halflength; 
185
  int slotoffsetF, blockoffsetF; 
186
  
187
  if (expected_packet == IF4_PDLFFT) {
188
    db_fulllength = (12*fp->N_RB_DL); 
189
  } else {
190
    db_fulllength = (12*fp->N_RB_UL);     
191 192 193
  }  
  db_halflength = db_fulllength>>1;

194
  int64_t *ret_type=(int64_t*)malloc(sizeof(int64_t)); 
195
  void *rx_buffer=NULL;
196
  int16_t *data_block=NULL;
197 198
   
  // Read packet(s) from the fronthaul    
199
  if (eNB->ifdevice.trx_read_func(&eNB->ifdevice,
200
                                  ret_type,
201 202
                                  &rx_buffer,
                                  db_fulllength,
203
                                  0) < 0) {
204 205
    perror("ETHERNET read");
  }
206 207
  
  *packet_type = *ret_type;
208 209
  
  if (*packet_type == IF4_PDLFFT) {
210
    data_block = (int16_t*) (rx_buffer+sizeof_IF4_dl_header_t);
211
          
212
    // Calculate from received packet
213
    slotoffsetF = (subframe)*(fp->ofdm_symbol_size)*((fp->Ncp==1) ? 12 : 14) + 1;
214
    blockoffsetF = slotoffsetF + fp->ofdm_symbol_size - db_halflength; 
215
    
216
    // Do decompression of the two parts and generate txdataF			
217 218 219 220 221 222 223 224 225
    for (element_id=0; element_id<db_halflength; element_id++) {
      txdataF[0][blockoffsetF+element_id]  = alaw2lin[ (data_block[element_id] & 0xff) ];
      txdataF[0][blockoffsetF+element_id] |= alaw2lin[ (data_block[element_id]>>8) ]<<16;

      txdataF[0][slotoffsetF+element_id]  = alaw2lin[ (data_block[element_id+db_halflength] & 0xff) ];
      txdataF[0][slotoffsetF+element_id] |= alaw2lin[ (data_block[element_id+db_halflength]>>8) ]<<16;
    }
		
    // Find and return symbol_number		 		
226
    *symbol_number = ((IF4_dl_header_t*)(rx_buffer))->frame_status.sym_num;      
227
        
228
  } else if (*packet_type == IF4_PULFFT) {
229
    data_block = (int16_t*) (rx_buffer+sizeof_IF4_ul_header_t);
230
          
231
    // Calculate from received packet
232
    slotoffsetF = (subframe)*(fp->ofdm_symbol_size)*((fp->Ncp==1) ? 12 : 14) + 1;
233 234
    blockoffsetF = slotoffsetF + fp->ofdm_symbol_size - db_halflength; 
    
235
    // Do decompression of the two parts and generate rxdataF
236 237 238 239 240 241 242 243 244
    for (element_id=0; element_id<db_halflength; element_id++) {
      rxdataF[0][blockoffsetF+element_id]  = alaw2lin[ (data_block[element_id] & 0xff) ];
      rxdataF[0][blockoffsetF+element_id] |= alaw2lin[ (data_block[element_id]>>8) ]<<16;

      rxdataF[0][slotoffsetF+element_id]  = alaw2lin[ (data_block[element_id+db_halflength] & 0xff) ];
      rxdataF[0][slotoffsetF+element_id] |= alaw2lin[ (data_block[element_id+db_halflength]>>8) ]<<16;
    }
		
    // Find and return symbol_number		 		
245
    *symbol_number = ((IF4_ul_header_t*)(rx_buffer))->frame_status.sym_num;      
246 247
    
  } else if (*packet_type == IF4_PRACH) {
248
    data_block = (int16_t*) (rx_buffer+sizeof_IF4_prach_header_t);
249 250
    
    db_fulllength = 839;  // hard coded 
251 252 253 254
       
  } else {
    AssertFatal(1==0, "recv_IF4 - Unknown packet_type %x", *packet_type);            
  }
255 256
  
  free(rx_buffer);
257
  return;   
258 259
}

260 261

void gen_IF4_dl_header(IF4_dl_header_t *dl_packet, int frame, int subframe) {      
262
  // Set Type and Sub-Type
263
  dl_packet->type = IF4_PACKET_TYPE; 
264
  dl_packet->sub_type = IF4_PDLFFT;
265 266

  // Leave reserved as it is 
267
  dl_packet->rsvd = 0;
268 269
  
  // Set frame status
270 271
  dl_packet->frame_status.ant_num = 0;
  dl_packet->frame_status.ant_start = 0;
272 273
  dl_packet->frame_status.rf_num = frame;
  dl_packet->frame_status.sf_num = subframe;
274 275 276
  dl_packet->frame_status.sym_num = 0;
  dl_packet->frame_status.rsvd = 0;

277
  // Set frame check sequence
278

279 280
}

281 282

void gen_IF4_ul_header(IF4_ul_header_t *ul_packet, int frame, int subframe) {  
283
  // Set Type and Sub-Type
284
  ul_packet->type = IF4_PACKET_TYPE; 
285
  ul_packet->sub_type = IF4_PULFFT;
286 287

  // Leave reserved as it is 
288
  ul_packet->rsvd = 0;
289 290
  
  // Set frame status
291 292
  ul_packet->frame_status.ant_num = 0;
  ul_packet->frame_status.ant_start = 0;
293 294
  ul_packet->frame_status.rf_num = frame;
  ul_packet->frame_status.sf_num = subframe;
295 296 297 298 299 300
  ul_packet->frame_status.sym_num = 0;
  ul_packet->frame_status.rsvd = 0;
    
  // Set antenna specific gain *** set other antenna gain ***
  ul_packet->gain0.exponent = 0;
  ul_packet->gain0.rsvd = 0;
301 302
    
  // Set frame check sequence
303

304 305
}

306 307

void gen_IF4_prach_header(IF4_prach_header_t *prach_packet, int frame, int subframe) {
308
  // Set Type and Sub-Type
309
  prach_packet->type = IF4_PACKET_TYPE; 
310
  prach_packet->sub_type = IF4_PRACH;
311 312

  // Leave reserved as it is 
313
  prach_packet->rsvd = 0;
314 315
  
  // Set LTE Prach configuration
316 317
  prach_packet->prach_conf.rsvd = 0;
  prach_packet->prach_conf.ant = 0;
318 319
  prach_packet->prach_conf.rf_num = frame;
  prach_packet->prach_conf.sf_num = subframe;
320 321
  prach_packet->prach_conf.exponent = 0;  
        
322
  // Set frame check sequence
323

324
}