rar_tools.c 7.4 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
23
 * \brief random access tools
24 25
 * \author Raymond Knopp and navid nikaein
 * \date 2011 - 2014
26
 * \version 1.0
27
 * @ingroup _mac
28

29
 */
30

31 32 33 34
#include "mac.h"
#include "mac_proto.h"
#include "mac_extern.h"
#include "SIMULATION/TOOLS/sim.h"
35
#include "common/utils/LOG/log.h"
36 37
#include "OCG.h"
#include "OCG_extern.h"
38
#include "UTIL/OPT/opt.h"
39
#include "common/ran_context.h"
40 41 42

#define DEBUG_RAR

43 44
extern unsigned int localRIV2alloc_LUT25[512];
extern unsigned int distRIV2alloc_LUT25[512];
45 46
extern unsigned short RIV2nb_rb_LUT25[512];
extern unsigned short RIV2first_rb_LUT25[512];
47
extern RAN_CONTEXT_t RC;
48

49
//------------------------------------------------------------------------------
50 51
unsigned short
fill_rar(const module_id_t module_idP,
52 53 54 55
         const int CC_id,
         RA_t *ra,
         const frame_t frameP,
         uint8_t *const dlsch_buffer,
yilmazt's avatar
yilmazt committed
56 57
         const uint16_t N_RB_UL,
         const uint8_t input_buffer_length)
58
//------------------------------------------------------------------------------
59
{
60 61 62 63 64 65 66 67 68 69
  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *) dlsch_buffer;
  uint8_t *rar = (uint8_t *) (dlsch_buffer + 1);
  // 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
  rarh->RAPID = ra->preamble_index; // Respond to Preamble 0 only for the moment
  rar[4] = (uint8_t) (ra->rnti >> 8);
  rar[5] = (uint8_t) (ra->rnti & 0xff);
  //ra->timing_offset = 0;
  ra->timing_offset /= 16;  //T_A = N_TA/16, where N_TA should be on a 30.72Msps
70 71 72 73
  //rar[0] = (uint8_t) (ra->timing_offset >> (2 + 4));  // 7 MSBs of timing advance + divide by 4
  //rar[1] = (uint8_t) (ra->timing_offset << (4 - 2)) & 0xf0; // 4 LSBs of timing advance + divide by 4
  rar[0] = (uint8_t) (ra->timing_offset >> (4));  // 7 MSBs of timing advance + divide by 4
  rar[1] = (uint8_t) (ra->timing_offset << (4)) & 0xf0; // 4 LSBs of timing advance + divide by 4
74 75 76 77 78 79 80 81 82
  COMMON_channels_t *cc = &RC.mac[module_idP]->common_channels[CC_id];

  if(N_RB_UL == 25) {
    ra->msg3_first_rb = 1;
  } else {
    if (cc->tdd_Config && N_RB_UL == 100) {
      ra->msg3_first_rb = 3;
    } else {
      ra->msg3_first_rb = 2;
83
    }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  }

  ra->msg3_nb_rb = 1;
  uint16_t rballoc = mac_computeRIV(N_RB_UL, ra->msg3_first_rb, ra->msg3_nb_rb);  // 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
  ra->msg3_mcs = 10;
  ra->msg3_TPC = 3;
  ra->msg3_ULdelay = 0;
  ra->msg3_cqireq = 0;
  ra->msg3_round = 0;
  rar[2] |= ((ra->msg3_mcs & 0x8) >> 3);  // mcs 10
  rar[3] =
    (((ra->msg3_mcs & 0x7) << 5)) | ((ra->msg3_TPC & 7) << 2) |
    ((ra->msg3_ULdelay & 1) << 1) | (ra->msg3_cqireq & 1);
laurent's avatar
laurent committed
99 100 101
  trace_pdu(DIRECTION_DOWNLINK, dlsch_buffer, input_buffer_length, module_idP,  WS_RA_RNTI, 1,
            RC.mac[module_idP]->frame, RC.mac[module_idP]->subframe,
            0, 0);
102 103

  return (ra->rnti);
104 105
}

106

107
//------------------------------------------------------------------------------
108 109 110
/*
 * Fill the RAR buffer (header + PDU) for LTE-M devices
 */
111
unsigned short fill_rar_br(eNB_MAC_INST *eNB,
112 113 114 115
                           int CC_id,
                           RA_t *ra,
                           const frame_t frameP,
                           const sub_frame_t subframeP,
116
                           uint8_t *const dlsch_buffer,
117
                           const uint8_t ce_level)
118 119
//------------------------------------------------------------------------------
{
120
  RA_HEADER_RAPID *rarh = (RA_HEADER_RAPID *) dlsch_buffer;
121
  COMMON_channels_t *cc = &eNB->common_channels[CC_id];
122 123 124 125 126 127 128 129
  uint8_t *rar = (uint8_t *)(dlsch_buffer + 1);
  uint32_t rballoc = 0;
  uint32_t ULdelay = 0;
  uint32_t cqireq = 0;
  uint32_t mpdcch_nb_index = 0;
  uint32_t TPC = 0;
  int input_buffer_length = 0;
  int N_NB_index = 0;
130
  AssertFatal(ra != NULL, "RA is null \n");
131
  /* Subheader fixed */
132 133 134
  rarh->E = 0;    // First and last RAR
  rarh->T = 1;    // 0 for E/T/R/R/BI subheader, 1 for E/T/RAPID subheader
  rarh->RAPID = ra->preamble_index; // Respond to Preamble
135 136
  /* RAR PDU */
  /* TA Command */
137 138 139
  ra->timing_offset /= 16;  // T_A = N_TA/16, where N_TA should be on a 30.72Msps
  rar[0] = (uint8_t) (ra->timing_offset >> 4) & 0x7f; // 7 MSBs of timing advance
  rar[1] = (uint8_t) (ra->timing_offset & 0x0f) << 4; // 4 LSBs of timing advance
140
  /* Copy the Msg2 narrowband */
141 142 143
  ra->msg34_narrowband = ra->msg2_narrowband;
  ra->msg3_first_rb    = 0;
  ra->msg3_nb_rb       = 2;
144

145
  if (ce_level < 2) { // CE Level 0, 1 (CEmodeA)
146 147
    input_buffer_length = 6;
    N_NB_index = get_numnarrowbandbits(cc->mib->message.dl_Bandwidth);
148
    /* UL Grant */
149
    ra->msg3_mcs = 7;
mohamed.said's avatar
mohamed.said committed
150
    TPC = 3; // no power increase
151 152
    ULdelay = 0;
    cqireq = 0;
mohamed.said's avatar
mohamed.said committed
153
    mpdcch_nb_index = 0;
154
    rballoc = mac_computeRIV(6, ra->msg3_first_rb, ra->msg3_nb_rb);
sharma's avatar
sharma committed
155
    uint32_t buffer = 0;
156
    buffer |= ra->msg34_narrowband << (16 + (4 - N_NB_index));
157
    buffer |= ((rballoc & 0x0F) << (12 + (4 - N_NB_index)));
158
    buffer |= ((ra->pusch_repetition_levels & 0x03) << (10 + (4 - N_NB_index)));
159
    buffer |= ((ra->msg3_mcs & 0x07) << (7 + (4 - N_NB_index)));
mohamed.said's avatar
mohamed.said committed
160 161 162
    buffer |= ((TPC & 0x07) << (4 + (4 - N_NB_index)));
    buffer |= ((cqireq & 0x01) << (3 + (4 - N_NB_index)));
    buffer |= ((ULdelay & 0x01) << (2 + (4 - N_NB_index)));
163
    buffer |= (mpdcch_nb_index << (4 - N_NB_index));
164 165 166 167 168 169 170 171 172 173 174
    rar[1] |= (uint8_t) (buffer >> 16) & 0x0F;
    rar[2] = (uint8_t) (buffer >> 8) & 0xFF;
    rar[3] = (uint8_t) buffer & 0xFF;
    /* RA CRNTI */
    rar[4] = (uint8_t)(ra->rnti >> 8);
    rar[5] = (uint8_t)(ra->rnti & 0xff);
  } else { // CE level 2, 3 (CEModeB)
    AssertFatal(1 == 0, "Shouldn't get here ...\n");
    input_buffer_length = 5;
    rar[3] = (uint8_t)(ra->rnti >> 8);
    rar[4] = (uint8_t)(ra->rnti & 0xff);
175
  }
176 177 178 179

  LOG_I(MAC, "[RAPROC] Frame %d Subframe %d : Generating RAR BR (%02x|%02x.%02x.%02x.%02x.%02x.%02x) for ce_level %d, CRNTI %x, preamble %d/%d, TIMING OFFSET %d\n",
        frameP,
        subframeP,
180
        *(uint8_t *) rarh,
181 182 183 184 185 186
        rar[0],
        rar[1],
        rar[2],
        rar[3],
        rar[4],
        rar[5],
187
        ce_level,
188
        ra->rnti,
189 190
        rarh->RAPID,
        ra->preamble_index,
191 192
        ra->timing_offset);

laurent's avatar
laurent committed
193 194 195 196 197 198 199 200 201 202
  trace_pdu(DIRECTION_DOWNLINK,
            dlsch_buffer,
            input_buffer_length,
            eNB->Mod_id,
            WS_RA_RNTI,
            1,
            eNB->frame,
            eNB->subframe,
            0,
            0);
203

204
  return (ra->rnti);
205
}