rar_tools.c 9.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22

/*! \file rar_tools.c
gauthier's avatar
gauthier committed
23
 * \brief random access tools
24 25
 * \author Raymond Knopp and navid nikaein
 * \date 2011 - 2014
nikaeinn's avatar
nikaeinn committed
26
 * \version 1.0
gauthier's avatar
gauthier committed
27
 * @ingroup _mac
28

gauthier's avatar
gauthier committed
29
 */
30 31

#include "defs.h"
32
#include "proto.h"
33 34 35 36 37
#include "extern.h"
#include "SIMULATION/TOOLS/defs.h"
#include "UTIL/LOG/log.h"
#include "OCG.h"
#include "OCG_extern.h"
38
#include "UTIL/OPT/opt.h"
39 40 41 42 43 44 45 46

#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];

47 48 49 50 51 52 53 54 55 56
//------------------------------------------------------------------------------
unsigned short fill_rar(
  const module_id_t module_idP,
  const int         CC_id,
  const frame_t     frameP,
  uint8_t*    const dlsch_buffer,
  const uint16_t    N_RB_UL,
  const uint8_t input_buffer_length
)
//------------------------------------------------------------------------------
57
{
58 59 60 61

  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);
62
  int i,ra_idx = -1;
63
  uint16_t rballoc;
64
  uint8_t mcs,TPC,ULdelay,cqireq;
65
  AssertFatal(CC_id < MAX_NUM_CCs, "CC_id %u < MAX_NUM_CCs %u", CC_id, MAX_NUM_CCs);
66

67 68 69 70 71 72
  for (i=0; i<NB_RA_PROC_MAX; i++) {
    if (eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[i].generate_rar == 1) {
      ra_idx=i;
      eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[i].generate_rar = 0;
      break;
    }
73 74
  }

75 76 77
  //DevAssert( ra_idx != -1 );
  if (ra_idx==-1)
    return(0);
78

79
  // subheader fixed
80 81
  rarh->E                     = 0; // First and last RAR
  rarh->T                     = 1; // 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
82
  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
83 84
  /*
  rar->R                      = 0;
85
  rar->Timing_Advance_Command = eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset/4;
86 87 88 89 90 91
  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;
92
  rar->t_crnti                = eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti;
gauthier's avatar
gauthier committed
93
   */
94 95
  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);
96
  //eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset = 0;
knopp's avatar
knopp committed
97
  eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].timing_offset /= 16; //T_A = N_TA/16, where N_TA should be on a 30.72Msps
98 99
  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
100
  rballoc = mac_xface->computeRIV(N_RB_UL,26,1); // first PRB only for UL Grant
101 102
  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
103 104
  mcs = 10;
  TPC = 3;
105 106
  ULdelay = 0;
  cqireq = 0;
knopp's avatar
knopp committed
107
  rar[2] |= ((mcs&0x8)>>3);  // mcs 10
108
  rar[3] = (((mcs&0x7)<<5)) | ((TPC&7)<<2) | ((ULdelay&1)<<1) | (cqireq&1);
109

110
  LOG_D(MAC,"[eNB %d][RAPROC] CC_id %d Frame %d Generating RAR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ra_idx %d, CRNTI %x,preamble %d/%d,TIMING OFFSET %d\n",
111
        module_idP, CC_id,
112
        frameP,
113 114 115 116 117
        *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
        ra_idx,
        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

gauthier's avatar
gauthier committed
119
  if (opt_enabled) {
120
    trace_pdu(1, dlsch_buffer, input_buffer_length, module_idP, 2, 1,
121
        eNB_mac_inst[module_idP].frame, eNB_mac_inst[module_idP].subframe, 0, 0);
122 123
    LOG_D(OPT,"[eNB %d][RAPROC] CC_id %d RAR Frame %d trace pdu for rnti %x and  rapid %d size %d\n",
          module_idP, CC_id, frameP, eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti,
124
          rarh->RAPID, input_buffer_length);
125
  }
126

127
  return(eNB_mac_inst[module_idP].common_channels[CC_id].RA_template[ra_idx].rnti);
128 129
}

130 131 132 133 134 135
//------------------------------------------------------------------------------
uint16_t
ue_process_rar(
  const module_id_t module_idP,
  const int CC_id,
  const frame_t frameP,
136
  const rnti_t ra_rnti,
137 138
  uint8_t* const dlsch_buffer,
  rnti_t* const t_crnti,
139 140
  const uint8_t preamble_index,
  uint8_t* selected_rar_buffer // output argument for storing the selected RAR header and RAR payload
141 142
)
//------------------------------------------------------------------------------
143
{
144
	uint16_t ret = 0; // return value
145 146 147 148

  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);
gauthier's avatar
gauthier committed
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        // get the last RAR payload for working with CMW500
	uint8_t n_rarpy = 0; // number of RAR payloads
	uint8_t n_rarh = 0; // number of MAC RAR subheaders
	uint8_t best_rx_rapid = -1; // the closest RAPID receive from all RARs
	while (1) {
		n_rarh++;
		if (rarh->T == 1) {
			n_rarpy++;
			LOG_D(MAC, "RAPID %d\n", rarh->RAPID);
		}

		if (rarh->RAPID == preamble_index) {
			LOG_D(PHY, "Found RAR with the intended RAPID %d\n", rarh->RAPID);
			rar = (uint8_t *)(dlsch_buffer+n_rarh + (n_rarpy-1)*6);
			break;
		}

		if (abs((int)rarh->RAPID - (int)preamble_index) < abs((int)best_rx_rapid - (int)preamble_index)) {
			best_rx_rapid = rarh->RAPID;
			rar = (uint8_t *)(dlsch_buffer+n_rarh + (n_rarpy-1)*6);
		}

		if (rarh->E == 0) {
			LOG_I(PHY, "No RAR found with the intended RAPID. The closest RAPID in all RARs is %d\n", best_rx_rapid);
			break;
		} else {
			rarh++;
		}
	};
	LOG_D(MAC, "number of RAR subheader %d; number of RAR pyloads %d\n", n_rarh, n_rarpy);

181 182 183 184 185
  if (CC_id>0) {
    LOG_W(MAC,"Should not have received RAR on secondary CCs! \n");
    return(0xffff);
  }

186
  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,
187 188
        *(uint8_t*)rarh,rar[0],rar[1],rar[2],rar[3],rar[4],rar[5],
        rarh->RAPID,preamble_index);
189
#ifdef DEBUG_RAR
gauthier's avatar
gauthier committed
190 191 192 193 194
  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);
195
  LOG_D(MAC,"[UE %d][RAPROC] rar->Timing_Advance_Command %d\n",module_idP,(((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
gauthier's avatar
gauthier committed
196 197 198 199 200 201
  //  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);
202
  LOG_D(MAC,"[UE %d][RAPROC] rar->t_crnti %x\n",module_idP,(uint16_t)rar[5]+(rar[4]<<8));
203 204
#endif

205 206 207
  if (opt_enabled) {
    LOG_D(OPT,"[UE %d][RAPROC] CC_id %d RAR Frame %d trace pdu for ra-RNTI %x\n",
          module_idP, CC_id, frameP, ra_rnti);
208
    trace_pdu(1, (uint8_t*)dlsch_buffer, n_rarh + n_rarpy*6, module_idP, 2, ra_rnti,
209 210
        UE_mac_inst[module_idP].rxFrame, UE_mac_inst[module_idP].rxSubframe, 0, 0);
  }
gauthier's avatar
gauthier committed
211

212
  if (preamble_index == rarh->RAPID) {
213 214 215
    *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);
216
    ret = ((((uint16_t)(rar[0]&0x7f))<<4) + (rar[1]>>4));
217 218
  } else {
    UE_mac_inst[module_idP].crnti=0;
219
    ret = (0xffff);
220
  }
221 222 223 224 225 226 227

  // move the selected RAR to the front of the RA_PDSCH buffer
  memcpy(selected_rar_buffer+0, (uint8_t*)rarh, 1);
  memcpy(selected_rar_buffer+1, (uint8_t*)rar , 6);

  return ret;

228
}