rar_tools.c 7.53 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    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.
9 10


11 12 13 14
    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.
15

16 17 18 19
    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/>.
20 21

  Contact Information
22 23 24 25
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27 28

*******************************************************************************/
29 30

/*! \file rar_tools.c
31
 * \brief random access tools
32 33
 * \author Raymond Knopp and navid nikaein
 * \date 2011 - 2014
34
 * \version 1.0
35
 * @ingroup _mac
36

37
 */
38 39

#include "defs.h"
40
#include "proto.h"
41 42 43 44 45 46 47
#include "extern.h"
#include "MAC_INTERFACE/defs.h"
#include "MAC_INTERFACE/extern.h"
#include "SIMULATION/TOOLS/defs.h"
#include "UTIL/LOG/log.h"
#include "OCG.h"
#include "OCG_extern.h"
48
#include "UTIL/OPT/opt.h"
49 50 51 52 53 54 55 56 57 58

#define DEBUG_RAR

extern unsigned int  localRIV2alloc_LUT25[512];
extern unsigned int  distRIV2alloc_LUT25[512];
extern unsigned short RIV2nb_rb_LUT25[512];
extern unsigned short RIV2first_rb_LUT25[512];

extern inline unsigned int taus(void);

59
unsigned short fill_rar(module_id_t module_idP,
knopp's avatar
knopp committed
60
			int CC_id,
61
                        frame_t frameP,
62 63 64
                        uint8_t *dlsch_buffer,
                        uint16_t N_RB_UL,
                        uint8_t input_buffer_length) {
65 66 67 68

  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *)dlsch_buffer;
  //  RAR_PDU *rar = (RAR_PDU *)(dlsch_buffer+1);
  uint8_t *rar = (uint8_t *)(dlsch_buffer+1);
69
  int i,ra_idx = -1;
70
  uint16_t rballoc;
71
  uint8_t mcs,TPC,ULdelay,cqireq;
72 73

  for (i=0;i<NB_RA_PROC_MAX;i++) {
knopp's avatar
knopp committed
74
      if (eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[i].generate_rar == 1) {
75
          ra_idx=i;
knopp's avatar
knopp committed
76
          eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[i].generate_rar = 0;
77 78
          break;
      }
79 80 81 82 83
  }

  // subheader fixed 
  rarh->E                     = 0; // First and last RAR
  rarh->T                     = 1; // 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
knopp's avatar
knopp committed
84
  rarh->RAPID                 = eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].preamble_index; // Respond to Preamble 0 only for the moment
85 86
  /*
  rar->R                      = 0;
knopp's avatar
knopp committed
87
  rar->Timing_Advance_Command = eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset/4;
88 89 90 91 92 93
  rar->hopping_flag           = 0;
  rar->rb_alloc               = mac_xface->computeRIV(N_RB_UL,12,2);  // 2 RB
  rar->mcs                    = 2;                                   // mcs 2
  rar->TPC                    = 4;   // 2 dB power adjustment
  rar->UL_delay               = 0;
  rar->cqi_req                = 1;
knopp's avatar
knopp committed
94
  rar->t_crnti                = eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti;
95
   */
knopp's avatar
knopp committed
96 97 98 99 100 101
  rar[4] = (uint8_t)(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti>>8);
  rar[5] = (uint8_t)(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti&0xff);
  eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset = 0;
  //eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset /= 16;
  rar[0] = (uint8_t)(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset>>(2+4)); // 7 MSBs of timing advance + divide by 4
  rar[1] = (uint8_t)(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset<<(4-2))&0xf0; // 4 LSBs of timing advance + divide by 4
102 103 104
  rballoc = mac_xface->computeRIV(N_RB_UL,1,1); // first PRB only for UL Grant
  rar[1] |= (rballoc>>7)&7; // Hopping = 0 (bit 3), 3 MSBs of rballoc
  rar[2] = ((uint8_t)(rballoc&0xff))<<1; // 7 LSBs of rballoc
knopp's avatar
knopp committed
105 106
  mcs = 10;
  TPC = 3;
107 108
  ULdelay = 0;
  cqireq = 0;
knopp's avatar
knopp committed
109 110
  rar[2] |= ((mcs&0x8)>>3);  // mcs 10
  rar[3] = (((mcs&0x7)<<5)) | ((TPC&7)<<2) | ((ULdelay&1)<<1) | (cqireq&1); 
111

112
  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Generating RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ra_idx %d, CRNTI %x,preamble %d/%d,TIMING OFFSET %d\n",module_idP,frameP,
113 114
      *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
      ra_idx,
knopp's avatar
knopp committed
115 116 117
      eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti,
      rarh->RAPID,eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[0].preamble_index,
      eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset);
118 119 120

#if defined(USER_MODE) && defined(OAI_EMU)
  if (oai_emulation.info.opt_enabled){
121 122 123
      trace_pdu(1, dlsch_buffer, input_buffer_length, module_idP, 2, 1,
          eNB_mac_inst[module_idP].subframe, 0, 0);
      LOG_I(OPT,"[eNB %d][RAPROC] RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
knopp's avatar
knopp committed
124
          module_idP, frameP, eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti,
125 126 127
          rarh->RAPID, input_buffer_length);
  } 
#endif 
knopp's avatar
knopp committed
128
  return(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti);
129 130
}

knopp's avatar
knopp committed
131
uint16_t ue_process_rar(module_id_t module_idP, int CC_id,frame_t frameP, uint8_t *dlsch_buffer,rnti_t *t_crnti,uint8_t preamble_index) {
132 133 134 135

  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *)dlsch_buffer;
  //  RAR_PDU *rar = (RAR_PDU *)(dlsch_buffer+1);
  uint8_t *rar = (uint8_t *)(dlsch_buffer+1);
136

knopp's avatar
knopp committed
137 138 139 140 141
  if (CC_id>0) {
    LOG_W(MAC,"Should not have received RAR on secondary CCs! \n");
    return(0xffff);
  }

142
  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Received RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for preamble %d/%d\n",module_idP,frameP,
143 144
      *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
      rarh->RAPID,preamble_index);
145
#ifdef DEBUG_RAR
146 147 148 149 150 151 152 153 154 155 156 157 158
  LOG_D(MAC,"[UE %d][RAPROC] rarh->E %d\n",module_idP,rarh->E);
  LOG_D(MAC,"[UE %d][RAPROC] rarh->T %d\n",module_idP,rarh->T);
  LOG_D(MAC,"[UE %d][RAPROC] rarh->RAPID %d\n",module_idP,rarh->RAPID);

  //  LOG_I(MAC,"[UE %d][RAPROC] rar->R %d\n",module_idP,rar->R);
  LOG_I(MAC,"[UE %d][RAPROC] rar->Timing_Advance_Command %d\n",module_idP,(((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->hopping_flag %d\n",module_idP,rar->hopping_flag);
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->rb_alloc %d\n",module_idP,rar->rb_alloc);
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->mcs %d\n",module_idP,rar->mcs);
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->TPC %d\n",module_idP,rar->TPC);
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->UL_delay %d\n",module_idP,rar->UL_delay);
  //  LOG_I(MAC,"[UE %d][RAPROC] rar->cqi_req %d\n",module_idP,rar->cqi_req);
  LOG_I(MAC,"[UE %d][RAPROC] rar->t_crnti %x\n",module_idP,(uint16_t)rar[5]+(rar[4]<<8));
159 160
#endif

161

162
  if (preamble_index == rarh->RAPID) {
163 164 165 166
      *t_crnti = (uint16_t)rar[5]+(rar[4]<<8);//rar->t_crnti;
      UE_mac_inst[module_idP].crnti = *t_crnti;//rar->t_crnti;
      //return(rar->Timing_Advance_Command);
      return((((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
167 168
  }
  else {
169 170
      UE_mac_inst[module_idP].crnti=0;
      return(0xffff);
171 172
  }
}