pucch_uci_ue_nr.c 60.6 KB
Newer Older
1
/* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
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 32 33 34 35
 * 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.1  (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
 */

/************************************************************************
*
* MODULE      :  PUCCH Packed Uplink Control Channel for UE NR
*                PUCCH is used to transmit Uplink Control Information UCI
*                which is composed of:
*                - SR Scheduling Request
*                - HARQ ACK/NACK
*                - CSI Channel State Information
*                UCI can also be transmitted on a PUSCH if it schedules.
*
* DESCRIPTION :  functions related to PUCCH UCI management
*                TS 38.213 9  UE procedure for reporting control information
*
**************************************************************************/

cig's avatar
cig committed
36
#include "executables/softmodem-common.h"
37 38
#include "PHY/NR_REFSIG/ss_pbch_nr.h"
#include "PHY/defs_nr_UE.h"
laurent's avatar
laurent committed
39
#include <openair1/SCHED/sched_common.h>
laurent's avatar
laurent committed
40
#include <openair1/PHY/NR_UE_TRANSPORT/pucch_nr.h>
41 42
#include "openair2/LAYER2/NR_MAC_UE/mac_proto.h"
#include "openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h"
43 44 45

#ifndef NO_RAT_NR

46
#include "SCHED_NR_UE/defs.h"
47 48 49
#include "SCHED_NR_UE/harq_nr.h"
#include "SCHED_NR_UE/pucch_power_control_ue_nr.h"

Hongzhi Wang's avatar
Hongzhi Wang committed
50
#define DEFINE_VARIABLES_PUCCH_UE_NR_H
51
#include "SCHED_NR_UE/pucch_uci_ue_nr.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
52
#undef DEFINE_VARIABLES_PUCCH_UE_NR_H
53 54 55

#endif

56 57


58 59
uint8_t nr_is_cqi_TXOp(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t gNB_id);
uint8_t nr_is_ri_TXOp(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t gNB_id);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

long
binary_search_float_nr(
  float elements[],
  long numElem,
  float value
)
//-----------------------------------------------------------------------------
{
  long first, last, middle;
  first = 0;
  last = numElem-1;
  middle = (first+last)/2;

  if(value < elements[0]) {
    return first;
  }

  if(value >= elements[last]) {
    return last;
  }

  while (last - first > 1) {
    if (elements[middle] > value) {
      last = middle;
    } else {
      first = middle;
    }

    middle = (first+last)/2;
  }

  if (first < 0 || first >= numElem) {
    LOG_E(RRC,"\n Error in binary search float!");
  }

  return first;
}
98 99 100 101 102
/*
void nr_generate_pucch0(int32_t **txdataF,
                        NR_DL_FRAME_PARMS *frame_parms,
                        PUCCH_CONFIG_DEDICATED *pucch_config_dedicated,
                        int16_t amp,
103
                        int nr_slot_tx,
104 105 106 107 108 109 110 111 112 113
                        uint8_t mcs,
                        uint8_t nrofSymbols,
                        uint8_t startingSymbolIndex,
                        uint16_t startingPRB);

void nr_generate_pucch1(int32_t **txdataF,
                        NR_DL_FRAME_PARMS *frame_parms,
                        PUCCH_CONFIG_DEDICATED *pucch_config_dedicated,
                        uint64_t payload,
                        int16_t amp,
114
                        int nr_slot_tx,
115 116 117 118 119 120 121 122 123 124 125 126
                        uint8_t nrofSymbols,
                        uint8_t startingSymbolIndex,
                        uint16_t startingPRB,
                        uint16_t startingPRB_intraSlotHopping,
                        uint8_t timeDomainOCC,
                        uint8_t nr_bit);

void nr_generate_pucch2(int32_t **txdataF,
                        NR_DL_FRAME_PARMS *frame_parms,
                        PUCCH_CONFIG_DEDICATED *pucch_config_dedicated,
                        uint64_t payload,
                        int16_t amp,
127
                        int nr_slot_tx,
128 129 130 131 132 133 134 135 136 137 138 139
                        uint8_t nrofSymbols,
                        uint8_t startingSymbolIndex,
                        uint8_t nrofPRB,
                        uint16_t startingPRB,
                        uint8_t nr_bit);

void nr_generate_pucch3_4(int32_t **txdataF,
                         NR_DL_FRAME_PARMS *frame_parms,
                         pucch_format_nr_t fmt,
                         PUCCH_CONFIG_DEDICATED *pucch_config_dedicated,
                         uint64_t payload,
                         int16_t amp,
140
                         int nr_slot_tx,
141 142 143 144 145 146 147 148
                         uint8_t nrofSymbols,
                         uint8_t startingSymbolIndex,
                         uint8_t nrofPRB,
                         uint16_t startingPRB,
                         uint8_t nr_bit,
                         uint8_t occ_length_format4,
                         uint8_t occ_index_format4);
*/
149 150 151 152 153
/**************** variables **************************************/


/**************** functions **************************************/

154 155
//extern uint8_t is_cqi_TXOp(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eNB_id);
//extern uint8_t is_ri_TXOp(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eNB_id);
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/*******************************************************************
*
* NAME :         pucch_procedures_ue_nr
*
* PARAMETERS :   ue context
*                processing slots of reception/transmission
*                gNB_id identifier
*
* RETURN :       bool TRUE  PUCCH will be transmitted
*                     FALSE No PUCCH to transmit
*
* DESCRIPTION :  determines UCI (uplink Control Information) payload
*                and PUCCH format and its parameters.
*                PUCCH is no transmitted if:
*                - there is no valid data to transmit
*                - Pucch parameters are not valid
*
* Below information is scanned in order to know what information should be transmitted to network.
*
* (SR Scheduling Request)   (HARQ ACK/NACK)    (CSI Channel State Information)
*          |                        |               - CQI Channel Quality Indicator
*          |                        |                - RI  Rank Indicator
*          |                        |                - PMI Primary Matrux Indicator
*          |                        |                - LI Layer Indicator
*          |                        |                - L1-RSRP
*          |                        |                - CSI-RS resource idicator
*          |                        V                    |
*          +-------------------- -> + <------------------
*                                   |
*                   +--------------------------------+
*                   | UCI Uplink Control Information |
*                   +--------------------------------+
*                                   V                                            PUCCH Configuration
*               +----------------------------------------+                   +--------------------------+
*               | Determine PUCCH  payload and its       |                   |     PUCCH Resource Set   |
*               +----------------------------------------+                   |     PUCCH Resource       |
*                                   V                                        |     Format parameters    |
*               +-----------------------------------------+                  |                          |
*               | Select PUCCH format with its parameters | <----------------+--------------------------+
*               +-----------------------------------------+
*                                   V
*                          +-----------------+
*                          |  Generate PUCCH |
*                          +-----------------+
*
* TS 38.213 9  UE procedure for reporting control information
*
*********************************************************************/

205 206
static int bwp_id = 1;

207 208 209 210 211 212 213
bool pucch_procedures_ue_nr(PHY_VARS_NR_UE *ue, uint8_t gNB_id, UE_nr_rxtx_proc_t *proc, bool reset_harq)
{
  uint8_t   sr_payload = 0;
  uint32_t  pucch_ack_payload = 0; /* maximum number of bits for pucch payload is supposed to be 32 */
  uint64_t  pucch_payload = 0;
  uint32_t  csi_payload = 0;
  int       frame_tx = proc->frame_tx;
214
  int       nr_slot_tx = proc->nr_slot_tx;
215 216 217 218 219 220 221 222 223 224 225
  int       Mod_id = ue->Mod_id;
  int       CC_id = ue->CC_id;

  int       O_SR = 0;
  int       O_ACK = 0;
  int       O_CSI = 0;      /* channel state information */
  int       N_UCI = 0;      /* size in bits for Uplink Control Information */
  int       cqi_status = 0;
  int       ri_status = 0;
  int       csi_status = 0;

Agustin's avatar
Agustin committed
226
  int       initial_pucch_id = NB_INITIAL_PUCCH_RESOURCE;
227 228 229 230
  int       pucch_resource_set = MAX_NB_OF_PUCCH_RESOURCE_SETS;
  int       pucch_resource_id = MAX_NB_OF_PUCCH_RESOURCES;
  int       pucch_resource_indicator = MAX_PUCCH_RESOURCE_INDICATOR;
  int       n_HARQ_ACK;
231

232
  int dmrs_scrambling_id=0,data_scrambling_id=0;
233

234 235
  NR_UE_MAC_INST_t *mac = get_mac_inst(0);
  NR_PUCCH_Resource_t *pucch_resource;
cig's avatar
cig committed
236
  uint16_t crnti = mac->crnti;
237 238 239

  /* update current context */

240
  int subframe_number = proc->nr_slot_rx / ue->frame_parms.slots_per_subframe;
241 242
  nb_pucch_format_4_in_subframes[subframe_number] = 0; /* reset pucch format 4 counter at current rx position */

243
  int dl_harq_pid = ue->dlsch[proc->thread_id][gNB_id][0]->current_harq_pid;
244

245
  if (dl_harq_pid < ue->dlsch[proc->thread_id][gNB_id][0]->number_harq_processes_for_pdsch) {
246
    /* pucch indicator can be reseted in function get_downlink_ack so it should be get now */
247
    pucch_resource_indicator = ue->dlsch[proc->thread_id][gNB_id][0]->harq_processes[dl_harq_pid]->harq_ack.pucch_resource_indicator;
248 249 250
  }

  /* Part - I
251
   * Collect feedback that should be transmitted at this nr_slot_tx :
252 253 254 255 256 257 258 259 260 261
   * - ACK/NACK, SR, CSI (CQI, RI, ...)
   */

  sr_payload = 0;

  if (trigger_periodic_scheduling_request( ue, gNB_id, proc ) == 1) {
    O_SR = 1; /* sr should be transmitted */
    if (ue->mac_enabled == 1) {

      /* sr_payload = 1 means that this is a positive SR, sr_payload = 0 means that it is a negative SR */
262
      sr_payload = nr_ue_get_SR(Mod_id,
263 264 265 266 267
                                CC_id,
                                frame_tx,
                                gNB_id,
                                0,//ue->pdcch_vars[proc->thread_id][gNB_id]->crnti,
                                nr_slot_tx); // nr_slot_rx used for meas gap
268 269 270 271 272 273 274 275 276 277
    }
    else {
      sr_payload = 1;
    }
  }

  O_ACK = get_downlink_ack( ue, gNB_id, proc, &pucch_ack_payload,
                            &n_HARQ_ACK, reset_harq); // 1 to reset ACK/NACK status : 0 otherwise

  cqi_status = ((ue->cqi_report_config[gNB_id].CQI_ReportPeriodic.cqi_PMI_ConfigIndex>0) &&
278
                                                         (nr_is_cqi_TXOp(ue,proc,gNB_id) == 1));
279 280

  ri_status = ((ue->cqi_report_config[gNB_id].CQI_ReportPeriodic.ri_ConfigIndex>0) &&
281
                                                         (nr_is_ri_TXOp(ue,proc,gNB_id) == 1));
282

283 284 285 286 287
  
  NR_CSI_MeasConfig_t *csi_MeasConfig = mac->scg->spCellConfig->spCellConfigDedicated->csi_MeasConfig->choice.setup;
  
  uint16_t report_slot_csi =csi_MeasConfig->csi_ReportConfigToAddModList->list.array[0]->reportConfigType.choice.periodic->reportSlotConfig.choice.slots320;

Hongzhi Wang's avatar
Hongzhi Wang committed
288
  //if (mac->csirc->reportQuantity.choice.ssb_Index_RSRP){ 
289
	  if (report_slot_csi == proc->nr_slot_tx)
290 291 292
		csi_status = get_csi_nr(mac, ue, gNB_id, &csi_payload);
	  else
	    csi_status = 0;
Hongzhi Wang's avatar
Hongzhi Wang committed
293
  //}
294 295 296 297 298 299 300 301 302

  O_CSI = cqi_status + ri_status + csi_status;

  /* Part - II */
  /* if payload is empty or only negative SR -> no pucch transmission */

  if(O_ACK == 0) {
    N_UCI = O_SR + O_CSI;
    if ((N_UCI == 0) || ((O_CSI == 0) && (sr_payload == 0))) {   /* TS 38.213 9.2.4 UE procedure for reporting SR */
303 304
      NR_TST_PHY_PRINTF("PUCCH No feedback AbsSubframe %d.%d \n", frame_tx%1024, nr_slot_tx);
      LOG_D(PHY,"PUCCH No feedback AbsSubframe %d.%d \n", frame_tx%1024, nr_slot_tx);
305 306 307 308
      return (FALSE);
    }
    else {
      /* a resource set and a resource should be find according to payload size */
309
      pucch_resource_set = find_pucch_resource_set( mac, gNB_id, N_UCI);
310 311
      if (pucch_resource_set != MAX_NB_OF_PUCCH_RESOURCE_SETS) {
        pucch_resource_indicator = 0;
312
        pucch_resource_id = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set]->resourceList.list.array[pucch_resource_indicator][0]; /* get the first resource of the set */
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
      }
      else {
        LOG_W(PHY,"PUCCH no resource set found for CSI at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        O_CSI = 0;
        csi_payload = 0;
      }

      if (O_CSI == 0) {
        /* only SR has to be send */
        /* in this case there is no DCI related to PUCCH parameters so pucch resource should be get from sr configuration */
        /* TS 38.213 9.2.4 UE procedure for reporting SR */
        pucch_resource_set = 0; /* force it to a valid value */
        if (ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[ue->scheduling_request_config_nr[gNB_id].active_sr_id] != NULL) {
         pucch_resource_id = ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[ue->scheduling_request_config_nr[gNB_id].active_sr_id]->resource;
        }
        else {
         LOG_E(PHY,"PUCCH No scheduling request configuration : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
         return(FALSE);
        }
      }
    }
  }
  else {
    N_UCI = O_SR + O_ACK + O_CSI;
  }

  /* Part - III */
  /* Choice PUCCH format and its related parameters */
  pucch_format_nr_t format = pucch_format0_nr;
laurent's avatar
laurent committed
342
  uint8_t  starting_symbol_index=0;
343 344 345
  uint8_t nb_symbols_total = 0;
  uint8_t  nb_symbols = 0;
  uint16_t starting_prb = 0;;  /* it can be considered as first  hop on case of pucch hopping */
346
  uint16_t second_hop = 0;     /* second part for pucch for hopping */
347
  uint8_t  nb_of_prbs = 0;
348 349
  int m_0 = 0;                 /* format 0 only */
  int m_CS = 0;                /* for all format except for format 0 */
350 351 352 353 354 355
  int index_additional_dmrs = I_PUCCH_NO_ADDITIONAL_DMRS;
  int index_hopping = I_PUCCH_NO_HOPPING;
  int time_domain_occ = 0;
  int occ_length = 0;
  int occ_Index = 0;

356
  NR_UE_HARQ_STATUS_t *harq_status = &ue->dlsch[proc->thread_id][gNB_id][0]->harq_processes[dl_harq_pid]->harq_ack;
357

358
  if (select_pucch_resource(ue, mac, gNB_id, N_UCI, pucch_resource_indicator, &initial_pucch_id, &pucch_resource_set,
359 360 361 362 363 364 365 366 367
                            &pucch_resource_id, harq_status) == TRUE) {
    /* use of initial pucch configuration provided by system information 1 */
    /***********************************************************************/
    if (initial_pucch_id != NB_INITIAL_PUCCH_RESOURCE) {
      format = initial_pucch_resource[initial_pucch_id].format;
      starting_symbol_index = initial_pucch_resource[initial_pucch_id].startingSymbolIndex;
      nb_symbols_total = initial_pucch_resource[initial_pucch_id].nrofSymbols;

      int N_CS = initial_pucch_resource[initial_pucch_id].nb_CS_indexes;
Jacques's avatar
Jacques committed
368
      /* see TS 38213 Table 9.2.1-1: PUCCH resource sets before dedicated PUCCH resource configuration */
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
      int RB_BWP_offset;
      if (initial_pucch_id == 15) {
        RB_BWP_offset = ue->systemInformationBlockType1_nr.N_BWP_SIZE/4;
      }
      else
      {
        RB_BWP_offset = initial_pucch_resource[initial_pucch_id].PRB_offset;
      }
      if (initial_pucch_id/8 == 0) {
        starting_prb = RB_BWP_offset + (initial_pucch_id/N_CS);
        second_hop = ue->systemInformationBlockType1_nr.N_BWP_SIZE - 1 - RB_BWP_offset - (initial_pucch_id/N_CS);
        m_0 = initial_pucch_resource[initial_pucch_id].initial_CS_indexes[initial_pucch_id%N_CS];
      }
      else if (initial_pucch_id/8 == 1)
      {
        starting_prb = RB_BWP_offset + (initial_pucch_id/N_CS);
        second_hop = ue->systemInformationBlockType1_nr.N_BWP_SIZE - 1 - RB_BWP_offset - ((initial_pucch_id - 8)/N_CS);
        m_0 =  initial_pucch_resource[initial_pucch_id].initial_CS_indexes[(initial_pucch_id - 8)%N_CS];
      }
      if ((ue->UE_mode[gNB_id] != PUSCH) && (O_ACK > 1)) {
        O_ACK = 1;
        pucch_ack_payload &= 0x1; /* take only first ack */
391
        LOG_W(PHY,"PUCCH ue is not expected to generate more than one HARQ-ACK at AbsSubframe %d.%d \n", frame_tx%1024, nr_slot_tx);
392 393 394 395 396 397 398
      }
      NR_TST_PHY_PRINTF("PUCCH common configuration with index %d \n", initial_pucch_id);
    }
    /* use dedicated pucch resource configuration */
    /**********************************************/
    else if ((pucch_resource_set != MAX_NB_OF_PUCCH_RESOURCE_SETS) && (pucch_resource_id != MAX_NB_OF_PUCCH_RESOURCES)) {
      /* check that current configuration is supported */
399
      if ((mac->scg->physicalCellGroupConfig->harq_ACK_SpatialBundlingPUCCH != NULL)
400
         || (mac->scg->physicalCellGroupConfig->pdsch_HARQ_ACK_Codebook != 1)) {
401 402 403
        LOG_E(PHY,"PUCCH Unsupported cell group configuration : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        return(FALSE);
      }
404
      else if (mac->scg->spCellConfig->spCellConfigDedicated->pdsch_ServingCellConfig->choice.setup->codeBlockGroupTransmission != NULL) {
405 406 407
        LOG_E(PHY,"PUCCH Unsupported code block group for serving cell config : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        return(FALSE);
      }
408
      pucch_resource = select_resource_by_id(pucch_resource_id, mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup);
409 410
      format = pucch_resource->format.present;
      nb_symbols_total = get_nb_symbols_pucch(pucch_resource, format);
411 412
      starting_symbol_index = get_starting_symb_idx(pucch_resource, format);
      starting_prb = pucch_resource->startingPRB;
413
      second_hop = starting_prb;
414 415 416 417 418 419
      if (format==pucch_format1_nr)
        time_domain_occ = pucch_resource->format.choice.format1->timeDomainOCC;
      if (format==pucch_format4_nr) {
        occ_length = pucch_resource->format.choice.format4->occ_Length;
        occ_Index  = pucch_resource->format.choice.format4->occ_Index;
      }
420

421
      m_0 = get_ics_pucch(pucch_resource, format);
Sakthivel Velumani's avatar
Sakthivel Velumani committed
422
      AssertFatal(m_0 >= 0, "Invalid m_0\n");
423 424 425 426
      if (format == pucch_format3_nr) {
        if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format3->choice.setup->additionalDMRS[0] == 1) {
          index_additional_dmrs = I_PUCCH_ADDITIONAL_DMRS;
        }
427
      }
428 429
      else if (format == pucch_format4_nr) {
        if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format4->choice.setup->additionalDMRS[0] == 1) {
430 431
          index_additional_dmrs = I_PUCCH_ADDITIONAL_DMRS;
        }
432
      }
433

434 435
      if ((format == pucch_format3_nr) || (format == pucch_format4_nr)) {
        if (pucch_resource->intraSlotFrequencyHopping[0] == 1) {
436 437 438 439 440 441 442 443
          index_hopping = I_PUCCH_HOPING;
        }
      }

      NR_TST_PHY_PRINTF("PUCCH dedicated configuration with resource index %d \n", pucch_resource_id);
    }
  }
  else {
444
    LOG_W(PHY,"PUCCH No PUCCH resource found at AbsSubframe %d.%d \n", frame_tx%1024, nr_slot_tx);
445 446 447
    return (FALSE);
  }

Francesco Mani's avatar
Francesco Mani committed
448
  int max_code_rate = 0;
Hongzhi WANG's avatar
Hongzhi WANG committed
449
  //int Q_m = BITS_PER_SYMBOL_QPSK; /* default pucch modulation type is QPSK with 2 bits per symbol */
450 451 452 453
  int N_sc_ctrl_RB = 0;
  int O_CRC = 0;

  nb_symbols = nb_symbols_total; /* by default, it can be reduced due to symbols reserved for dmrs */
454
  pucch_resource = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList->list.array[pucch_resource_id];
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

  switch(format) {
    case pucch_format0_nr:
    {
      nb_of_prbs = 1;
      N_sc_ctrl_RB = N_SC_RB;
      break;
    }
    case pucch_format1_nr:
    {
      nb_of_prbs = 1;
      N_sc_ctrl_RB = N_SC_RB;
      break;
    }
    case pucch_format2_nr:
    {
471
      nb_of_prbs = pucch_resource->format.choice.format2->nrofPRBs;
472 473 474 475 476
      N_sc_ctrl_RB = N_SC_RB - 4;
      break;
    }
    case pucch_format3_nr:
    {
477
      nb_of_prbs = pucch_resource->format.choice.format3->nrofPRBs;
Hongzhi WANG's avatar
Hongzhi WANG committed
478 479 480
      //if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format3->choice.setup->pi2BPSK[0] == 1) {
      //  Q_m = BITS_PER_SYMBOL_BPSK; /* set bpsk modulation type with 1 bit per modulation symbol */
      //}
481 482 483 484 485 486
      N_sc_ctrl_RB = N_SC_RB;
      nb_symbols = nb_symbols_excluding_dmrs[nb_symbols_total-4][index_additional_dmrs][index_hopping];
      break;
    }
    case pucch_format4_nr:
    {
Hongzhi WANG's avatar
Hongzhi WANG committed
487 488 489
      //if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format4->choice.setup->pi2BPSK[0] == 1) {
      //  Q_m = BITS_PER_SYMBOL_BPSK; /* set bpsk modulation type with 1 bit per modulation symbol */
      //}
490 491
      nb_symbols = nb_symbols_excluding_dmrs[nb_symbols_total-4][index_additional_dmrs][index_hopping];
      nb_of_prbs = 1;
492
      subframe_number = nr_slot_tx / ue->frame_parms.slots_per_subframe;
493 494 495 496 497 498 499 500 501 502 503
      nb_pucch_format_4_in_subframes[subframe_number]++; /* increment number of transmit pucch 4 in current subframe */
      NR_TST_PHY_PRINTF("PUCCH Number of pucch format 4 in subframe %d is %d \n", subframe_number, nb_pucch_format_4_in_subframes[subframe_number]);
      N_sc_ctrl_RB = N_SC_RB/(nb_pucch_format_4_in_subframes[subframe_number]);
      break;
    }
  }

  /* TS 38.213 9.2.5.2 UE procedure for multiplexing HARQ-ACK/SR and CSI */
  /* drop CSI report if simultaneous HARQ-ACK/SR and periodic/semi-periodic CSI cannot be transmitted at the same time */
  if (format !=  pucch_format0_nr) {

504 505
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format1 != NULL) {
      max_code_rate = code_rate_r_time_100[mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format1->choice.setup->maxCodeRate[0]]; /* it is code rate * 10 */
506

507
      if ((O_ACK != 0) && (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format1->choice.setup->simultaneousHARQ_ACK_CSI[0] == 0)) {
508 509 510 511 512 513 514
        N_UCI = N_UCI - O_CSI;
        O_CSI = cqi_status = ri_status = 0;
        csi_payload = 0; /* csi should be dropped in this case */
      }
    }

    /* TS 38.212 6.3.1.2  Code block segmentation and CRC attachment */
515
    /* crc attachment can be done depending of payload size */
516 517 518 519 520 521 522 523 524
//    if (N_UCI < 11) {
//      O_CRC = 0;  /* no additional crc bits */
//    }
//    else if ((N_UCI >= 12) && (N_UCI <= 19)) {
//      O_CRC = 6;  /* number of additional crc bits */
//    }
//   else if (N_UCI >= 20) {
//      O_CRC = 11; /* number of additional crc bits */
//    }
525 526 527 528

    N_UCI = N_UCI + O_CRC;

    /* for format 2 and 3, number of prb should be adjusted to minimum value which cope to information size */
Hongzhi Wang's avatar
Hongzhi Wang committed
529
    /*if (nb_of_prbs > 1 ) {
530 531 532 533
      int nb_prb_min = 0;
      int payload_in_bits;
      do {
        nb_prb_min++;
Hongzhi Wang's avatar
Hongzhi Wang committed
534 535 536
        payload_in_bits = (nb_prb_min * N_sc_ctrl_RB * nb_symbols * Q_m * max_code_rate)/100; */ /* code rate has been multiplied by 100 */
        
        /*NR_TST_PHY_PRINTF("PUCCH Adjust number of prb : (N_UCI : %d ) (payload_in_bits : %d) (N_sc_ctrl_RB : %d) (nb_symbols : %d) (Q_m : %d) (max_code_rate*100 : %d) \n",
537 538 539 540 541 542 543 544 545 546
                                               N_UCI,        payload_in_bits,       N_sc_ctrl_RB,       nb_symbols,       Q_m,       max_code_rate);
      } while (N_UCI > payload_in_bits);

      if (nb_prb_min > nb_of_prbs) {
        LOG_E(PHY,"PUCCH Number of prbs too small for current pucch bits to transmit : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        return (FALSE);
      }
      else {
        nb_of_prbs = nb_prb_min;
      }
Hongzhi Wang's avatar
Hongzhi Wang committed
547
    }*/
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

    /* TS 38.213 9.2.4 for a positive SR transmission, payload b(0) = 0 */
    if ((O_SR == 1) && (format ==  pucch_format1_nr)) {
      sr_payload = 0;
    }
  }
  else {  /* only format 0 here */
    if ((O_SR == 0) && (O_CSI == 0)) {  /* only ack is transmitted TS 36.213 9.2.3 UE procedure for reporting HARQ-ACK */
      if (O_ACK == 1) {
        m_CS = sequence_cyclic_shift_1_harq_ack_bit[pucch_ack_payload & 0x1];   /* only harq of 1 bit */
      }
      else {
        m_CS = sequence_cyclic_shift_2_harq_ack_bits[pucch_ack_payload & 0x3];  /* only harq with 2 bits */
      }
    }
    else if ((O_SR == 1) && (O_CSI == 0)) { /* SR + eventually ack are transmitted TS 36.213 9.2.5.1 UE procedure for multiplexing HARQ-ACK or CSI and SR */
      if (sr_payload == 1) {                /* positive scheduling request */
        if (O_ACK == 1) {
          m_CS = sequence_cyclic_shift_1_harq_ack_bit_positive_sr[pucch_ack_payload & 0x1];   /* positive SR and harq of 1 bit */
        }
        else if (O_ACK == 2) {
          m_CS = sequence_cyclic_shift_2_harq_ack_bits_positive_sr[pucch_ack_payload & 0x3];  /* positive SR and harq with 2 bits */
        }
        else {
          m_CS = 0;  /* only positive SR */
        }
      }
    }
    N_UCI = O_SR = O_ACK = 0;
    pucch_payload = sr_payload = pucch_ack_payload = 0; /* no data for format 0 */
  }

  /* TS 38.212 6.3.1  Uplink control information on PUCCH                                       */
  /* information concatenation of payload                                                       */
  /*                                                   CSI           SR          HARQ-ACK       */
  /* bit order of payload of size n :           a(n)....................................a(0)    */
  /* a(0) is the LSB and a(n) the MSB   <--------><--------------><------------><---------->    */
  /*                                       O_CRC        O_CSI           O_SR         O_ACK      */
  /*                                                                                            */
  /* remark: crc is not part of payload, it is later added by block coding.                     */

  if (N_UCI > (sizeof(uint64_t)*8)) {
    LOG_E(PHY,"PUCCH number of UCI bits exceeds payload size : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return(0);
  }

  pucch_payload = pucch_payload | (csi_payload << (O_ACK + O_SR)) |  (sr_payload << O_ACK) | pucch_ack_payload;

  NR_TST_PHY_PRINTF("PUCCH ( AbsSubframe : %d.%d ) ( total payload size %d data 0x%02x ) ( ack length %d data 0x%02x ) ( sr length %d value %d ) ( csi length %d data : 0x%02x ) \n",
597
                         frame_tx%1024, nr_slot_tx, N_UCI,  pucch_payload, O_ACK, pucch_ack_payload, O_SR, sr_payload, csi_status, csi_payload);
598

599
  NR_TST_PHY_PRINTF("PUCCH ( format : %d ) ( modulation : %s ) ( nb prb : %d ) ( nb symbols total: %d ) ( nb symbols : %d ) ( max code rate*100 : %d ) ( starting_symbol_index : %d ) \n",
Jacques's avatar
Jacques committed
600
                             format, (Q_m == BITS_PER_SYMBOL_QPSK ? " QPSK " : " BPSK "), nb_of_prbs, nb_symbols_total, nb_symbols, max_code_rate, starting_symbol_index);
601 602

  NR_TST_PHY_PRINTF("PUCCH ( starting_prb : %d ) ( second_hop : %d ) ( m_0 : %d ) ( m_CS : %d ) ( time_domain_occ %d ) (occ_length : %d ) ( occ_Index : %d ) \n",
Jacques's avatar
Jacques committed
603
                             starting_prb,         second_hop,         m_0,         m_CS,         time_domain_occ,      occ_length,         occ_Index);
604 605 606 607 608 609 610 611 612

  /* Part - IV */
  /* Generate PUCCH signal according to its format and parameters */
  ue->generate_ul_signal[gNB_id] = 1;

  int16_t pucch_tx_power = get_pucch_tx_power_ue( ue, gNB_id, proc, format,
                                                  nb_of_prbs, N_sc_ctrl_RB, nb_symbols, N_UCI, O_SR, O_CSI, O_ACK,
                                                  O_CRC, n_HARQ_ACK);

613
  /* set tx power */
614 615
  ue->tx_power_dBm[nr_slot_tx] = pucch_tx_power;
  ue->tx_total_RE[nr_slot_tx] = nb_of_prbs*N_SC_RB;
616

617 618 619 620
  int tx_amp;

#if defined(EXMIMO) || defined(OAI_USRP) || defined(OAI_BLADERF) || defined(OAI_LMSSDR) || defined(OAI_ADRV9371_ZC706)

621
  tx_amp = nr_get_tx_amp(pucch_tx_power,
622 623
                      ue->tx_power_max_dBm,
                      ue->frame_parms.N_RB_UL,
624
                      nb_of_prbs);
625 626 627
#else
  tx_amp = AMP;
#endif
628 629 630 631

  switch(format) {
    case pucch_format0_nr:
    {
632
      nr_generate_pucch0(ue,ue->common_vars.txdataF,
633
                         &ue->frame_parms,
634 635
                         mac->ULbwp[bwp_id-1]->bwp_Common->pucch_ConfigCommon->choice.setup->pucch_GroupHopping,
                         mac->ULbwp[bwp_id-1]->bwp_Common->pucch_ConfigCommon->choice.setup->hoppingId[0],
636
                         tx_amp,
637
                         nr_slot_tx,
638
                         (uint8_t)m_0,
639
                         (uint8_t)m_CS,
640
                         nb_symbols_total,
641 642 643 644 645 646
                         starting_symbol_index,
                         starting_prb);
      break;
    }
    case pucch_format1_nr:
    {
647
      nr_generate_pucch1(ue,ue->common_vars.txdataF,
648
                         &ue->frame_parms,
laurent's avatar
laurent committed
649
                         &ue->pucch_config_dedicated[gNB_id],
650
                         pucch_payload,
651
                         tx_amp,
652
                         nr_slot_tx,
653 654
                         (uint8_t)m_0,
                         nb_symbols_total,
655 656 657 658 659 660 661 662 663
                         starting_symbol_index,
                         starting_prb,
                         second_hop,
                         (uint8_t)time_domain_occ,
                         (uint8_t)N_UCI);
      break;
    }
    case pucch_format2_nr:
    {
Agustin's avatar
Agustin committed
664
      nr_generate_pucch2(ue,
665 666 667
                         crnti,
			 dmrs_scrambling_id,
			 data_scrambling_id,
Agustin's avatar
Agustin committed
668
                         ue->common_vars.txdataF,
669
                         &ue->frame_parms,
laurent's avatar
laurent committed
670
                         &ue->pucch_config_dedicated[gNB_id],
671
                         pucch_payload,
672
                         tx_amp,
673
                         nr_slot_tx,
674
                         nb_symbols_total,
675 676 677 678 679 680 681 682 683
                         starting_symbol_index,
                         nb_of_prbs,
                         starting_prb,
                         (uint8_t)N_UCI);
      break;
    }
    case pucch_format3_nr:
    case pucch_format4_nr:
    {
Agustin's avatar
Agustin committed
684
      nr_generate_pucch3_4(ue,
685
                           0,//ue->pdcch_vars[proc->thread_id][gNB_id]->crnti,
Agustin's avatar
Agustin committed
686
                           ue->common_vars.txdataF,
687 688
                           &ue->frame_parms,
                           format,
laurent's avatar
laurent committed
689
                           &ue->pucch_config_dedicated[gNB_id],
690
                           pucch_payload,
691
                           tx_amp,
692
                           nr_slot_tx,
693
                           nb_symbols_total,
694 695 696
                           starting_symbol_index,
                           nb_of_prbs,
                           starting_prb,
697
                           second_hop,
698 699 700 701 702 703
                           (uint8_t)N_UCI,
                           (uint8_t)occ_length,
                           (uint8_t)occ_Index);
      break;
    }
  }
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  return (TRUE);
}

/*******************************************************************
*
* NAME :         get_downlink_ack
*
* PARAMETERS :   ue context
*                processing slots of reception/transmission
*                gNB_id identifier
*
* RETURN :       o_ACK acknowledgment data
*                o_ACK_number_bits number of bits for acknowledgment
*
* DESCRIPTION :  return acknowledgment value
*                TS 38.213 9.1.3 Type-2 HARQ-ACK codebook determination
*
*          --+--------+-------+--------+-------+---  ---+-------+--
*            | PDCCH1 |       | PDCCH2 |PDCCH3 |        | PUCCH |
*          --+--------+-------+--------+-------+---  ---+-------+--
*    DAI_DL      1                 2       3              ACK for
*                V                 V       V        PDCCH1, PDDCH2 and PCCH3
*                |                 |       |               ^
*                +-----------------+-------+---------------+
*
*                PDCCH1, PDCCH2 and PDCCH3 are PDCCH monitoring occasions
*                M is the total of monitoring occasions
*
*********************************************************************/

uint8_t get_downlink_ack(PHY_VARS_NR_UE *ue, uint8_t gNB_id,  UE_nr_rxtx_proc_t *proc,
                         uint32_t *o_ACK, int *n_HARQ_ACK,
                         bool do_reset) // 1 to reset ACK/NACK status : 0 otherwise
{
  NR_UE_HARQ_STATUS_t *harq_status;
  uint32_t ack_data[NR_DL_MAX_NB_CW][NR_DL_MAX_DAI] = {{0},{0}};
  uint32_t dai[NR_DL_MAX_NB_CW][NR_DL_MAX_DAI] = {{0},{0}};       /* for serving cell */
  uint32_t dai_total[NR_DL_MAX_NB_CW][NR_DL_MAX_DAI] = {{0},{0}}; /* for multiple cells */
  int number_harq_feedback = 0;
  uint32_t dai_current = 0;
  uint32_t dai_max = 0;
745
  int number_pid_dl = ue->dlsch[proc->thread_id][gNB_id][0]->number_harq_processes_for_pdsch;
746 747 748 749 750
  bool two_transport_blocks = FALSE;
  int number_of_code_word = 1;
  int U_DAI_c = 0;
  int N_m_c_rx = 0;
  int V_DAI_m_DL = 0;
751
  NR_UE_MAC_INST_t *mac = get_mac_inst(0);
752

Sakthivel Velumani's avatar
Sakthivel Velumani committed
753
  if (mac->DLbwp[0] == NULL) return 0;
754

755
  if (mac->DLbwp[0]->bwp_Dedicated->pdsch_Config->choice.setup->maxNrofCodeWordsScheduledByDCI[0] == 2) {
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    two_transport_blocks = TRUE;
    number_of_code_word = 2;
  }
  else {
    number_of_code_word = 1;
  }

  if (ue->n_connected_eNB > 1) {
    LOG_E(PHY,"PUCCH ACK feedback is not implemented for mutiple gNB cells : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return (0);
  }

  /* look for dl acknowledgment which should be done on current uplink slot */
  for (int code_word = 0; code_word < number_of_code_word; code_word++) {

    for (int dl_harq_pid = 0; dl_harq_pid < number_pid_dl; dl_harq_pid++) {

Sakthivel Velumani's avatar
Sakthivel Velumani committed
773
      for (int thread_idx = 0; thread_idx < RX_NB_TH; thread_idx++) {
774

Sakthivel Velumani's avatar
Sakthivel Velumani committed
775
        harq_status = &ue->dlsch[thread_idx][gNB_id][code_word]->harq_processes[dl_harq_pid]->harq_ack;
776

Sakthivel Velumani's avatar
Sakthivel Velumani committed
777
        /* check if current tx slot should transmit downlink acknowlegment */
778
        if (harq_status->slot_for_feedback_ack == proc->nr_slot_tx) {
779

Sakthivel Velumani's avatar
Sakthivel Velumani committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793
          if (harq_status->ack == DL_ACKNACK_NO_SET) {
            LOG_E(PHY,"PUCCH Downlink acknowledgment has not been set : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
            return (0);
          }
          else if (harq_status->vDAI_DL == DL_DAI_NO_SET) {
            LOG_E(PHY,"PUCCH Downlink DAI has not been set : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
            return (0);
          }
          else if (harq_status->vDAI_DL > NR_DL_MAX_DAI) {
            LOG_E(PHY,"PUCCH Downlink DAI has an invalid value : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
            return (0);
          }
          else if (harq_status->send_harq_status == 0) {
            LOG_E(PHY,"PUCCH Downlink ack can not be transmitted : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
794 795
            return(0);
          }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
796
          else {
797

Sakthivel Velumani's avatar
Sakthivel Velumani committed
798
            dai_current = harq_status->vDAI_DL+1; // DCI DAI to counter DAI conversion
Sakthivel Velumani's avatar
Sakthivel Velumani committed
799 800 801 802 803 804 805 806 807 808 809 810

            if (dai_current == 0) {
              LOG_E(PHY,"PUCCH Downlink dai is invalid : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
              return(0);
            } else if (dai_current > dai_max) {
              dai_max = dai_current;
            }

            number_harq_feedback++;
            ack_data[code_word][dai_current - 1] = harq_status->ack;
            dai[code_word][dai_current - 1] = dai_current;
          }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
811 812 813
          if (do_reset == TRUE) {
            init_downlink_harq_status(ue->dlsch[thread_idx][gNB_id][code_word]->harq_processes[dl_harq_pid]);
          }
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
        }
      }
    }
  }

  /* no any ack to transmit */
  if (number_harq_feedback == 0) {
    *n_HARQ_ACK = 0;
    return(0);
  }
  else  if (number_harq_feedback > (sizeof(uint32_t)*8)) {
    LOG_E(PHY,"PUCCH number of ack bits exceeds payload size : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return(0);
  }

  /* for computing n_HARQ_ACK for power */
   V_DAI_m_DL = dai_max;
   U_DAI_c = number_harq_feedback/number_of_code_word;
   N_m_c_rx = number_harq_feedback;
   int N_SPS_c = 0; /* FFS TODO_NR multicells and SPS are not supported at the moment */
834
   if (mac->scg->physicalCellGroupConfig->harq_ACK_SpatialBundlingPUCCH == NULL) {
835
     int N_TB_max_DL = mac->DLbwp[0]->bwp_Dedicated->pdsch_Config->choice.setup->maxNrofCodeWordsScheduledByDCI[0];
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
     *n_HARQ_ACK = (((V_DAI_m_DL - U_DAI_c)%4) * N_TB_max_DL) + N_m_c_rx + N_SPS_c;
     NR_TST_PHY_PRINTF("PUCCH power n(%d) = ( V(%d) - U(%d) )mod4 * N_TB(%d) + N(%d) \n", *n_HARQ_ACK, V_DAI_m_DL, U_DAI_c, N_TB_max_DL, N_m_c_rx);
   }

  /*
  * For a monitoring occasion of a PDCCH with DCI format 1_0 or DCI format 1_1 in at least one serving cell,
  * when a UE receives a PDSCH with one transport block and the value of higher layer parameter maxNrofCodeWordsScheduledByDCI is 2,
  * the HARQ-ACK response is associated with the first transport block and the UE generates a NACK for the second transport block
  * if spatial bundling is not applied (HARQ-ACK-spatial-bundling-PUCCH = FALSE) and generates HARQ-ACK value of ACK for the second
  * transport block if spatial bundling is applied.
  */

  for (int code_word = 0; code_word < number_of_code_word; code_word++) {
    for (uint32_t i = 0; i < dai_max ; i++ ) {
      if (dai[code_word][i] != i + 1) { /* fill table with consistent value for each dai */
        dai[code_word][i] = i + 1;      /* it covers case for which PDCCH DCI has not been successfully decoded and so it has been missed */
        ack_data[code_word][i] = 0;     /* nack data transport block which has been missed */
        number_harq_feedback++;
      }
      if (two_transport_blocks == TRUE) {
        dai_total[code_word][i] = dai[code_word][i]; /* for a single cell, dai_total is the same as dai of first cell */
      }
    }
  }

  int M = dai_max;
  int j = 0;
  uint32_t V_temp = 0;
  uint32_t V_temp2 = 0;
  int O_ACK = 0;
  int O_bit_number_cw0 = 0;
  int O_bit_number_cw1 = 0;

  for (int m = 0; m < M ; m++) {

    if (dai[0][m] <= V_temp) {
      j = j + 1;
    }

    V_temp = dai[0][m]; /* value of the counter DAI for format 1_0 and format 1_1 on serving cell c */

    if (dai_total[0][m] == 0) {
      V_temp2 = dai[0][m];
    } else {
      V_temp2 = dai[1][m];         /* second code word has been received */
      O_bit_number_cw1 = (8 * j) + 2*(V_temp - 1) + 1;
      *o_ACK = *o_ACK | (ack_data[1][m] << O_bit_number_cw1);
    }

    if (two_transport_blocks == TRUE) {
      O_bit_number_cw0 = (8 * j) + 2*(V_temp - 1);
    }
    else {
      O_bit_number_cw0 = (4 * j) + (V_temp - 1);
    }

    *o_ACK = *o_ACK | (ack_data[0][m] << O_bit_number_cw0);
  }

  if (V_temp2 < V_temp) {
    j = j + 1;
  }

  if (two_transport_blocks == TRUE) {
    O_ACK = 2 * ( 4 * j + V_temp2);  /* for two transport blocks */
  }
  else {
    O_ACK = 4 * j + V_temp2;         /* only one transport block */
  }

  if (number_harq_feedback != O_ACK) {
    LOG_E(PHY,"PUCCH Error for number of bits for acknowledgment : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return (0);
  }

  return(number_harq_feedback);
}

/*******************************************************************
*
* NAME :         select_pucch_format
*
* PARAMETERS :   ue context
*                processing slots of reception/transmission
*                gNB_id identifier
*
* RETURN :       TRUE a valid resource has been found
*
* DESCRIPTION :  return tx harq process identifier for given transmission slot
*                TS 38.213 9.2.1  PUCCH Resource Sets
*                TS 38.213 9.2.2  PUCCH Formats for UCI transmission
*                In the case of pucch for scheduling request only, resource is already get from scheduling request configuration
*
*********************************************************************/

931
boolean_t select_pucch_resource(PHY_VARS_NR_UE *ue, NR_UE_MAC_INST_t *mac, uint8_t gNB_id, int uci_size, int pucch_resource_indicator, 
932 933 934 935 936 937 938
                                int *initial_pucch_id, int *resource_set_id, int *resource_id, NR_UE_HARQ_STATUS_t *harq_status)
{
  boolean_t resource_set_found = FALSE;
  int nb_symbols_for_tx = 0;
  int current_resource_id = MAX_NB_OF_PUCCH_RESOURCES;
  pucch_format_nr_t format_pucch;
  int ready_pucch_resource_id = FALSE; /* in the case that it is already given */
939
  NR_PUCCH_Resource_t *pucch_resource;
940 941 942 943 944 945

  /* ini values to unset */
  *initial_pucch_id = NB_INITIAL_PUCCH_RESOURCE;
  //*resource_set_id = MAX_NB_OF_PUCCH_RESOURCE_SETS;
  //*resource_id = MAX_NB_OF_PUCCH_RESOURCES;

946
  if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[0] == NULL) {
947 948 949 950

    /* No resource set has been already configured so pucch_configCommon from Sib1 should be used in this case */

    if (ue->UE_mode[gNB_id] != PUSCH) {
Sakthivel Velumani's avatar
Sakthivel Velumani committed
951
      *initial_pucch_id = mac->ULbwp[bwp_id-1]->bwp_Common->pucch_ConfigCommon->choice.setup->pucch_ResourceCommon[0];
952 953 954 955 956 957 958 959 960
      if (*initial_pucch_id >= NB_INITIAL_PUCCH_RESOURCE) {
        LOG_E(PHY,"PUCCH Invalid initial resource index : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        *initial_pucch_id = NB_INITIAL_PUCCH_RESOURCE;
        return (FALSE);
      }
    }
    else  {
      /* see TS 38.213 9.2.1  PUCCH Resource Sets */
      int delta_PRI = harq_status->pucch_resource_indicator;
Agustin's avatar
Agustin committed
961 962 963 964
      // n_CCE can be obtained from ue->dci_ind.dci_list[i].n_CCE. FIXME!!!
      // N_CCE can be obtained from ue->dci_ind.dci_list[i].N_CCE. FIXME!!!
      //int n_CCE = ue->dci_ind.dci_list[0].n_CCE;
      //int N_CCE = ue->dci_ind.dci_list[0].N_CCE;
965 966 967 968 969 970 971 972 973 974 975
      int n_CCE_0 = harq_status->n_CCE;
      int N_CCE_0 = harq_status->N_CCE;
      if (N_CCE_0 == 0) {
        LOG_E(PHY,"PUCCH No compatible pucch format found : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        return (FALSE);
      }
      int r_PUCCH = ((2 * n_CCE_0)/N_CCE_0) + (2 * delta_PRI);
      *initial_pucch_id = r_PUCCH;
    }
    nb_symbols_for_tx = initial_pucch_resource[*initial_pucch_id].nrofSymbols;
    format_pucch = initial_pucch_resource[*initial_pucch_id].format;
976
    if (check_pucch_format(mac, gNB_id, format_pucch, nb_symbols_for_tx, uci_size) == TRUE) {
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
      return (TRUE);
    }
    else {
      LOG_E(PHY,"PUCCH No compatible pucch format found : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
      return (FALSE);
    }
  }
  else {
    /* dedicated resources have been configured */
    int pucch_resource_set_id = 0;
    if (*resource_set_id == MAX_NB_OF_PUCCH_RESOURCE_SETS) {
      /* from TS 38.331 field maxPayloadMinus1
        -- Maximum number of payload bits minus 1 that the UE may transmit using this PUCCH resource set. In a PUCCH occurrence, the UE
        -- chooses the first of its PUCCH-ResourceSet which supports the number of bits that the UE wants to transmit.
        -- The field is not present in the first set (Set0) since the maximum Size of Set0 is specified to be 3 bit.
        -- The field is not present in the last configured set since the UE derives its maximum payload size as specified in 38.213.
        -- This field can take integer values that are multiples of 4. Corresponds to L1 parameter 'N_2' or 'N_3' (see 38.213, section 9.2)
      */
      /* look for the first resource set which supports uci_size number of bits for payload */
996
      pucch_resource_set_id = find_pucch_resource_set(mac, gNB_id, uci_size);
997 998 999 1000 1001 1002 1003 1004
      if (pucch_resource_set_id != MAX_NB_OF_PUCCH_RESOURCE_SETS) {
        resource_set_found = TRUE;
      }
    }
    else {
      /* a valid resource has already be found outside this function */
      resource_set_found = TRUE;
      ready_pucch_resource_id = TRUE;
yilmazt's avatar
yilmazt committed
1005
      //pucch_resource_indicator = pucch_resource_indicator;
1006 1007 1008 1009
    }

    if (resource_set_found == TRUE) {
      if (pucch_resource_indicator < MAX_PUCCH_RESOURCE_INDICATOR) {
1010 1011 1012 1013 1014 1015
        // Verify that the value of pucch_resource_indicator is valid
        if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id]->resourceList.list.count <= pucch_resource_indicator)
        {
          LOG_E(PHY, "Value of pucch_resource_indicator is out of bounds! Possibly due to a false DCI. \n");
          return (FALSE);
        }
1016
        /* check if resource indexing by pucch_resource_indicator of this set is compatible */
1017
        if ((ready_pucch_resource_id == TRUE) || (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id]->resourceList.list.array[pucch_resource_indicator][0] != MAX_NB_OF_PUCCH_RESOURCES)) {
1018 1019 1020 1021 1022

          if (ready_pucch_resource_id == TRUE) {
            current_resource_id = *resource_id;
          }
          else {
1023
            int R_PUCCH = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id]->resourceList.list.count;
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
            /* is it the first resource and its size exceeds 8 */
            if ((pucch_resource_set_id == 0)
             && (R_PUCCH > MAX_NB_OF_PUCCH_RESOURCES_PER_SET_NOT_0)) {
              /* see TS 38.213 9.2.3  UE procedure for reporting HARQ-ACK */
              int delta_PRI = pucch_resource_indicator;
              int n_CCE_p = harq_status->n_CCE;
              int N_CCE_p = harq_status->N_CCE;
              int r_PUCCH;
              if (N_CCE_p == 0) {
                LOG_E(PHY,"PUCCH No compatible pucch format found : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
                return (FALSE);
              }
              if (pucch_resource_set_id < (R_PUCCH%8)) {
                r_PUCCH = ((n_CCE_p * (R_PUCCH/8))/N_CCE_p) + (delta_PRI*(R_PUCCH/8));
              }
              else {
                r_PUCCH = ((n_CCE_p * (R_PUCCH/8))/N_CCE_p) + (delta_PRI*(R_PUCCH/8)) + (R_PUCCH%8);
              }
              current_resource_id = r_PUCCH;
            }
            else {
Hongzhi Wang's avatar
Hongzhi Wang committed
1045 1046 1047 1048
		if (pucch_resource_set_id !=0 )
			current_resource_id = 3; //TBC mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id]->resourceList.list.array[pucch_resource_indicator][0];
		else
			current_resource_id = 1;
1049 1050 1051
            }
          }

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
          /*uint8_t pucch_resource_count = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList.list.count;
          for (uint8_t i=0; i<pucch_resource_count; i++) {
            if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList.list.array[i]->pucch_ResourceId == current_resource_id)
              pucch_resource = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList.list.array[i];
          }*/

          pucch_resource = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList->list.array[current_resource_id];
          if (pucch_resource != NULL) {
            format_pucch = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList->list.array[current_resource_id]->format.present;
            nb_symbols_for_tx = get_nb_symbols_pucch(pucch_resource, format_pucch);
1062

1063
            if (check_pucch_format(mac, gNB_id, format_pucch, nb_symbols_for_tx, uci_size) == TRUE) {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
              *resource_set_id = pucch_resource_set_id;
              *resource_id = current_resource_id;
              return (TRUE);
            }
            else {
              LOG_E(PHY,"PUCCH Found format no compatible with payload size and symbol length : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
              return (FALSE);
            }
          }
        }
        else {
          LOG_E(PHY,"PUCCH Undefined Resource related to pucch resource indicator: at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
          return (FALSE);
        }
      }
      else {
        LOG_E(PHY,"PUCCH Invalid pucch resource indicator: at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
        return (FALSE);
      }
    }

    /* check that a resource has been found */
    if (*resource_set_id == MAX_NB_OF_PUCCH_RESOURCES) {
      LOG_E(PHY,"PUCCH No compatible pucch format found : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
      return (FALSE);
    }
  }
  return (FALSE);
}

/*******************************************************************
*
* NAME :         find_pucch_resource_set
*
* PARAMETERS :   ue context
*                gNB_id identifier
*
*
* RETURN :       harq process identifier
*
* DESCRIPTION :  return tx harq process identifier for given transmission slot
*                YS 38.213 9.2.2  PUCCH Formats for UCI transmission
*
*********************************************************************/

1109
int find_pucch_resource_set(NR_UE_MAC_INST_t *mac, uint8_t gNB_id, int uci_size)
1110 1111
{
  int pucch_resource_set_id = 0;
Hongzhi WANG's avatar
Hongzhi WANG committed
1112
  //long *pucch_max_pl_bits = NULL;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

  /* from TS 38.331 field maxPayloadMinus1
    -- Maximum number of payload bits minus 1 that the UE may transmit using this PUCCH resource set. In a PUCCH occurrence, the UE
    -- chooses the first of its PUCCH-ResourceSet which supports the number of bits that the UE wants to transmit.
    -- The field is not present in the first set (Set0) since the maximum Size of Set0 is specified to be 3 bit.
    -- The field is not present in the last configured set since the UE derives its maximum payload size as specified in 38.213.
    -- This field can take integer values that are multiples of 4. Corresponds to L1 parameter 'N_2' or 'N_3' (see 38.213, section 9.2)
  */
  /* look for the first resource set which supports uci_size number of bits for payload */
  while (pucch_resource_set_id < MAX_NB_OF_PUCCH_RESOURCE_SETS) {
1123
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id] != NULL) {
Hongzhi WANG's avatar
Hongzhi WANG committed
1124
      //pucch_max_pl_bits = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->resourceSetToAddModList->list.array[pucch_resource_set_id]->maxPayloadMinus1;
Hongzhi Wang's avatar
Hongzhi Wang committed
1125
      if (uci_size <= 2) { //TBC rrc (((pucch_max_pl_bits != NULL) ? *pucch_max_pl_bits : 1706) + 1)) {
Hongzhi WANG's avatar
Hongzhi WANG committed
1126
        //NR_TST_PHY_PRINTF("PUCCH found resource set %d max bits %d\n",  pucch_resource_set_id, pucch_max_pl_bits);
Hongzhi Wang's avatar
Hongzhi Wang committed
1127 1128 1129 1130 1131
        pucch_resource_set_id = 0;
        return (pucch_resource_set_id);
        break;
      }
      else {
1132
        pucch_resource_set_id = 1;
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
        return (pucch_resource_set_id);
        break;
      }
    }
    pucch_resource_set_id++;
  }

  pucch_resource_set_id = MAX_NB_OF_PUCCH_RESOURCE_SETS;

  return (pucch_resource_set_id);
}

/*******************************************************************
*
* NAME :         check_pucch_format
*
* PARAMETERS :   ue context
*                processing slots of reception/transmission
*                gNB_id identifier
*
* RETURN :       harq process identifier
*
* DESCRIPTION :  return tx harq process identifier for given transmission slot
*                YS 38.213 9.2.2  PUCCH Formats for UCI transmission
*
*********************************************************************/

1160
boolean_t check_pucch_format(NR_UE_MAC_INST_t *mac, uint8_t gNB_id, pucch_format_nr_t format_pucch, int nb_symbols_for_tx, int uci_size)
1161 1162 1163
{
  pucch_format_nr_t selected_pucch_format;
  pucch_format_nr_t selected_pucch_format_second;
Hongzhi WANG's avatar
Hongzhi WANG committed
1164
  /*NR_SetupRelease_PUCCH_FormatConfig_t *identified_format = NULL;
1165

Sakthivel Velumani's avatar
Sakthivel Velumani committed
1166 1167 1168 1169 1170
  switch (format_pucch) {
    case pucch_format1_nr:
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format1 != NULL)
      identified_format = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format1;
    break;
1171

Sakthivel Velumani's avatar
Sakthivel Velumani committed
1172 1173 1174 1175
    case pucch_format2_nr:
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format2 != NULL)
      identified_format = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format2;
    break;
1176

Sakthivel Velumani's avatar
Sakthivel Velumani committed
1177 1178 1179 1180
    case pucch_format3_nr:
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format3 != NULL)
      identified_format = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format3;
    break;
1181

Sakthivel Velumani's avatar
Sakthivel Velumani committed
1182 1183 1184 1185
    case pucch_format4_nr:
    if (mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format4 != NULL)
      identified_format = mac->ULbwp[bwp_id-1]->bwp_Dedicated->pucch_Config->choice.setup->format4;
    break;
1186

Sakthivel Velumani's avatar
Sakthivel Velumani committed
1187 1188
    default:
    break;
Hongzhi WANG's avatar
Hongzhi WANG committed
1189
  }*/
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1190

Hongzhi Wang's avatar
Hongzhi Wang committed
1191
 /* if ((identified_format != NULL) && (identified_format->choice.setup->nrofSlots[0] != 1)) {
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1192 1193
    LOG_E(PHY,"PUCCH not implemented multislots transmission : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return (FALSE);
Hongzhi Wang's avatar
Hongzhi Wang committed
1194
  }*/
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

  if (nb_symbols_for_tx <= 2) {
    if (uci_size <= 2) {
      selected_pucch_format = pucch_format0_nr;
      selected_pucch_format_second = selected_pucch_format;
    }
    else {
      selected_pucch_format = pucch_format2_nr;
      selected_pucch_format_second = selected_pucch_format;
    }
  }
  else {
    if (nb_symbols_for_tx >= 4) {
      if (uci_size <= 2) {
        selected_pucch_format = pucch_format1_nr;
        selected_pucch_format_second = selected_pucch_format;
      }
      else {
        selected_pucch_format = pucch_format3_nr;  /* in this case choice can be done between two formats */
        selected_pucch_format_second = pucch_format4_nr;
      }
    }
    else {
      LOG_D(PHY,"PUCCH Undefined PUCCH format : set PUCCH to format 4 : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
      return (FALSE);
    }
  }

1223
  NR_TST_PHY_PRINTF("PUCCH format %d nb symbols total %d uci size %d selected format %d \n", format_pucch, nb_symbols_for_tx, uci_size, selected_pucch_format);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284

  if (format_pucch != selected_pucch_format) {
    if (format_pucch != selected_pucch_format_second) {
      NR_TST_PHY_PRINTF("PUCCH mismatched of selected format: at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
      LOG_D(PHY,"PUCCH format mismatched of selected format: at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
      return (FALSE);
    }
    else {
      return (TRUE);
    }
  }
  else {
    return (TRUE);
  }
}

/*******************************************************************
*
* NAME :         trigger_periodic_scheduling_request
*
* PARAMETERS :   pointer to resource set
*
* RETURN :       1 if peridic scheduling request is triggered
*                0 no periodic scheduling request
*
* DESCRIPTION :  TS 38.213 9.2.4 UE procedure for reporting SR
*
*********************************************************************/

int trigger_periodic_scheduling_request(PHY_VARS_NR_UE *ue, uint8_t gNB_id, UE_nr_rxtx_proc_t *proc)
{
  const int max_sr_periodicity[NB_NUMEROLOGIES_NR] = { 80, 160, 320, 640, 640 };

  int active_scheduling_request = ue->scheduling_request_config_nr[gNB_id].active_sr_id;

  /* is there any valid scheduling request configuration */
  if (ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[active_scheduling_request] == NULL) {
    return (0);
  }

  if (ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[active_scheduling_request]->periodicity < 2) {
    LOG_W(PHY,"PUCCH Not supported scheduling request period smaller than 1 slot : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return (0);
  }

  int16_t SR_periodicity = scheduling_request_periodicity[ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[active_scheduling_request]->periodicity];
  uint16_t SR_offset = ue->scheduling_request_config_nr[gNB_id].sr_ResourceConfig[active_scheduling_request]->offset;

  if (SR_periodicity > max_sr_periodicity[ue->frame_parms.numerology_index]) {
    LOG_W(PHY,"PUCCH Invalid scheduling request period : at line %d in function %s of file %s \n", LINE_FILE , __func__, FILE_NAME);
    return (0);
  }

  if (SR_offset > SR_periodicity) {
    LOG_E(PHY,"PUCCH SR offset %d is greater than SR periodicity %d : at line %d in function %s of file %s \n", SR_offset, SR_periodicity, LINE_FILE , __func__, FILE_NAME);
    return (0);
  }
  else if (SR_periodicity == 1) {
    return (1); /* period is slot */
  }

1285
  int16_t N_slot_frame = ue->frame_parms.slots_per_frame;
1286
  if (((proc->frame_tx * N_slot_frame) + proc->nr_slot_tx - SR_offset)%SR_periodicity == 0) {
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    return (1);
  }
  else {
    return (0);
  }
}

/*******************************************************************
*
* NAME :         get_csi_nr
* PARAMETERS :   ue context
*                processing slots of reception/transmission
*                gNB_id identifier
*
* RETURN :       size of csi payload
*
* DESCRIPTION :  CSI management is not already implemented
*                so it has been simulated thank to two functions:
*                - set_csi_nr
*                - get_csi_nr
*
*********************************************************************/

Hongzhi Wang's avatar
Hongzhi Wang committed
1310
int      dummy_csi_status = 0;
1311 1312 1313 1314
uint32_t dummy_csi_payload = 0;

/* FFS TODO_NR code that should be developed */

1315
uint16_t get_nr_csi_bitlen(NR_UE_MAC_INST_t *mac) {
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

  uint16_t csi_bitlen =0;
  uint16_t rsrp_bitlen = 0;
  uint16_t diff_rsrp_bitlen = 0;
  uint16_t nb_ssbri_cri = 0; 
  uint16_t cri_ssbri_bitlen = 0;
  
  NR_CSI_MeasConfig_t *csi_MeasConfig = mac->scg->spCellConfig->spCellConfigDedicated->csi_MeasConfig->choice.setup;
  struct NR_CSI_ResourceConfig__csi_RS_ResourceSetList__nzp_CSI_RS_SSB * nzp_CSI_RS_SSB = csi_MeasConfig->csi_ResourceConfigToAddModList->list.array[0]->csi_RS_ResourceSetList.choice.nzp_CSI_RS_SSB;

  uint16_t nb_csi_ssb_report = nzp_CSI_RS_SSB->csi_SSB_ResourceSetList!=NULL ? nzp_CSI_RS_SSB->csi_SSB_ResourceSetList->list.count:0;
  
  if (0 != nb_csi_ssb_report){
	  uint8_t nb_ssb_resources =0;
	  
  if (NULL != csi_MeasConfig->csi_ReportConfigToAddModList->list.array[0]->groupBasedBeamReporting.choice.disabled->nrofReportedRS)
      nb_ssbri_cri = *(csi_MeasConfig->csi_ReportConfigToAddModList->list.array[0]->groupBasedBeamReporting.choice.disabled->nrofReportedRS)+1;
  else
      nb_ssbri_cri = 1;
  
  nb_ssb_resources = csi_MeasConfig->csi_SSB_ResourceSetToAddModList->list.array[0]->csi_SSB_ResourceList.list.count;
  
  if (nb_ssb_resources){
	cri_ssbri_bitlen =ceil(log2 (nb_ssb_resources));
	rsrp_bitlen = 7;
	diff_rsrp_bitlen = 4;
	}
  else{
	cri_ssbri_bitlen =0;
	rsrp_bitlen = 0;
	diff_rsrp_bitlen = 0;
	}
  
  csi_bitlen = ((cri_ssbri_bitlen * nb_ssbri_cri) + rsrp_bitlen +(diff_rsrp_bitlen *(nb_ssbri_cri -1 ))) *nb_csi_ssb_report;
               
1351
  //printf("get csi bitlen %d nb_ssbri_cri %d nb_csi_report %d nb_resources %d\n", csi_bitlen,nb_ssbri_cri ,nb_csi_ssb_report, nb_ssb_resources);
1352 1353 1354 1355 1356
  }
  return csi_bitlen;
}

int get_csi_nr(NR_UE_MAC_INST_t *mac, PHY_VARS_NR_UE *ue, uint8_t gNB_id, uint32_t *csi_payload)
1357 1358 1359
{
  VOID_PARAMETER ue;
  VOID_PARAMETER gNB_id;
1360 1361 1362
  float rsrp_db[7];
  int nElem = 98;
  int rsrp_offset = 17;
1363
  int csi_status = 0;
1364
  
1365
  csi_status = get_nr_csi_bitlen(mac);
1366 1367
  rsrp_db[0] = get_nr_RSRP(0,0,0);

1368

Hongzhi Wang's avatar
Hongzhi Wang committed
1369
  if (csi_status == 0) {
1370 1371 1372
    *csi_payload = 0;
  }
  else {
1373
    *csi_payload = binary_search_float_nr(RSRP_meas_mapping_nr,nElem, rsrp_db[0]) + rsrp_offset;
1374 1375
  }

Hongzhi Wang's avatar
Hongzhi Wang committed
1376
  return (csi_status);
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
}

/* FFS TODO_NR code that should be removed */

void set_csi_nr(int csi_status, uint32_t csi_payload)
{
  dummy_csi_status = csi_status;

  if (dummy_csi_status == 0) {
    dummy_csi_payload = 0;
  }
  else {
    dummy_csi_payload = csi_payload;
  }
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
uint8_t get_nb_symbols_pucch(NR_PUCCH_Resource_t *pucch_resource, pucch_format_nr_t format_type)
{
  switch (format_type) {
    case pucch_format0_nr:
      return pucch_resource->format.choice.format0->nrofSymbols;

    case pucch_format1_nr:
      return pucch_resource->format.choice.format1->nrofSymbols;

    case pucch_format2_nr:
      return pucch_resource->format.choice.format2->nrofSymbols;

    case pucch_format3_nr:
      return pucch_resource->format.choice.format3->nrofSymbols;

    case pucch_format4_nr:
      return pucch_resource->format.choice.format4->nrofSymbols;
  }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1411
  return 0;
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
}

uint16_t get_starting_symb_idx(NR_PUCCH_Resource_t *pucch_resource, pucch_format_nr_t format_type)
{
  switch (format_type) {
    case pucch_format0_nr:
      return pucch_resource->format.choice.format0->startingSymbolIndex;

    case pucch_format1_nr:
      return pucch_resource->format.choice.format1->startingSymbolIndex;

    case pucch_format2_nr:
      return pucch_resource->format.choice.format2->startingSymbolIndex;

    case pucch_format3_nr:
      return pucch_resource->format.choice.format3->startingSymbolIndex;

    case pucch_format4_nr:
      return pucch_resource->format.choice.format4->startingSymbolIndex;
  }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1432
  return 0;
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
}

int get_ics_pucch(NR_PUCCH_Resource_t *pucch_resource, pucch_format_nr_t format_type)
{
  switch (format_type) {
    case pucch_format0_nr:
      return pucch_resource->format.choice.format0->initialCyclicShift;

    case pucch_format1_nr:
      return pucch_resource->format.choice.format1->initialCyclicShift;
Hongzhi Wang's avatar
Hongzhi Wang committed
1443 1444 1445
      
    case pucch_format2_nr:
      return 0;
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1446 1447 1448

    default:
      return -1;
1449
  }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
1450
  return -1;
1451
}
1452

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
NR_PUCCH_Resource_t *select_resource_by_id(int resource_id, NR_PUCCH_Config_t *pucch_config)
{
  int n_list = pucch_config->resourceToAddModList->list.count; 
  NR_PUCCH_Resource_t *pucchres;
  AssertFatal(n_list>0,"PUCCH resourceToAddModList is empty\n");

  for (int i=0; i<n_list; i++) {
    pucchres = pucch_config->resourceToAddModList->list.array[i];
    if (pucchres->pucch_ResourceId == resource_id)
      return pucchres;
  }
  return NULL;
}