phy_procedures_lte_eNb.c 168 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 23

/*! \file phy_procedures_lte_eNB.c
 * \brief Implementation of eNB procedures from 36.213 LTE specifications
24
 * \author R. Knopp, F. Kaltenberger, N. Nikaein, X. Foukas
25 26 27
 * \date 2011
 * \version 0.1
 * \company Eurecom
28
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr,navid.nikaein@eurecom.fr, x.foukas@sms.ed.ac.uk
Matthieu Kanj's avatar
Matthieu Kanj committed
29 30 31
 * \last changes: M. Kanj, V. Savaux
 * \date: 2018
 * \company: b<>com
Matthieu Kanj's avatar
Matthieu Kanj committed
32
 * \email: matthieu.kanj@b-com.com, vincent.savaux@b-com.com
33 34 35 36 37 38 39 40 41
 * \note
 * \warning
 */

#include "PHY/defs.h"
#include "PHY/extern.h"
#include "SCHED/defs.h"
#include "SCHED/extern.h"

Matthieu Kanj's avatar
Matthieu Kanj committed
42
///////// NB-IoT testing /////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
43 44
#include "PHY/defs_NB_IoT.h"
#include "PHY/vars_NB_IoT.h"
Matthieu Kanj's avatar
Matthieu Kanj committed
45
/////////////////////////////////////////////
46
#include "PHY/LTE_TRANSPORT/if4_tools.h"
Sandeep Kumar's avatar
Sandeep Kumar committed
47
#include "PHY/LTE_TRANSPORT/if5_tools.h"
48
#include "openair1/SCHED/defs_NB_IoT.h"
49

50 51 52 53
#ifdef EMOS
#include "SCHED/phy_procedures_emos.h"
#endif

Matthieu Kanj's avatar
Matthieu Kanj committed
54 55 56 57 58
////// NB-IoT testing /////////////
#include "PHY/CODING/extern.h"
#include "PHY/LTE_ESTIMATION/defs_NB_IoT.h"
#include "PHY/LTE_TRANSPORT/proto_NB_IoT.h"
////////////////////////////////////
59
//#define DEBUG_PHY_PROC (Already defined in cmake)
60 61 62 63 64 65 66
//#define DEBUG_ULSCH

#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/defs.h"
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"

67 68
#include "T.h"

69
#include "assertions.h"
Lionel Gauthier's avatar
Lionel Gauthier committed
70
#include "msc.h"
71

72 73
#include <time.h>

74
#if defined(ENABLE_ITTI)
Lionel Gauthier's avatar
Lionel Gauthier committed
75
#   include "intertask_interface.h"
76 77
#endif

78

79
#if defined(FLEXRAN_AGENT_SB_IF)
80
//Agent-related headers
81
#include "ENB_APP/flexran_agent_extern.h"
82
#include "ENB_APP/CONTROL_MODULES/MAC/flexran_agent_mac.h"
83
#include "LAYER2/MAC/flexran_agent_mac_proto.h"
84
#endif
85

86 87 88 89 90 91
//#define DIAG_PHY

#define NS_PER_SLOT 500000

#define PUCCH 1

92 93
void exit_fun(const char* s);

94
extern int exit_openair;
95 96
struct timespec start_fh, start_fh_prev;
int start_fh_sf, start_fh_prev_sf;
97 98
// Fix per CC openair rf/if device update
// extern openair0_device openair0;
99

100 101 102
unsigned char dlsch_input_buffer[2700] __attribute__ ((aligned(32)));
int eNB_sync_buffer0[640*6] __attribute__ ((aligned(32)));
int eNB_sync_buffer1[640*6] __attribute__ ((aligned(32)));
103 104
int *eNB_sync_buffer[2] = {eNB_sync_buffer0, eNB_sync_buffer1};

105
extern uint16_t hundred_times_log10_NPRB[100];
106

107
unsigned int max_peak_val;
108
int max_sync_pos;
109

110 111 112
int harq_pid_updated[NUMBER_OF_UE_MAX][8] = {{0}};
int harq_pid_round[NUMBER_OF_UE_MAX][8] = {{0}};

113 114 115 116 117 118
//DCI_ALLOC_t dci_alloc[8];

#ifdef EMOS
fifo_dump_emos_eNB emos_dump_eNB;
#endif

119
#if defined(SMBV) 
120 121
extern const char smbv_fname[];
extern unsigned short config_frames[4];
122
extern uint8_t smbv_frame_cnt;
123 124 125 126 127 128
#endif

#ifdef DIAG_PHY
extern int rx_sig_fifo;
#endif

Matthieu Kanj's avatar
Matthieu Kanj committed
129

130
uint8_t is_SR_subframe(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,uint8_t UE_id)
131
{
Raymond Knopp's avatar
 
Raymond Knopp committed
132

133 134
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;
135

136
  LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking for SR TXOp(sr_ConfigIndex %d)\n",
137 138
        eNB->Mod_id,eNB->ulsch[UE_id]->rnti,frame,subframe,
        eNB->scheduling_request_config[UE_id].sr_ConfigIndex);
139

140 141
  if (eNB->scheduling_request_config[UE_id].sr_ConfigIndex <= 4) {        // 5 ms SR period
    if ((subframe%5) == eNB->scheduling_request_config[UE_id].sr_ConfigIndex)
142
      return(1);
143 144
  } else if (eNB->scheduling_request_config[UE_id].sr_ConfigIndex <= 14) { // 10 ms SR period
    if (subframe==(eNB->scheduling_request_config[UE_id].sr_ConfigIndex-5))
145
      return(1);
146 147
  } else if (eNB->scheduling_request_config[UE_id].sr_ConfigIndex <= 34) { // 20 ms SR period
    if ((10*(frame&1)+subframe) == (eNB->scheduling_request_config[UE_id].sr_ConfigIndex-15))
148
      return(1);
149 150
  } else if (eNB->scheduling_request_config[UE_id].sr_ConfigIndex <= 74) { // 40 ms SR period
    if ((10*(frame&3)+subframe) == (eNB->scheduling_request_config[UE_id].sr_ConfigIndex-35))
151
      return(1);
152 153
  } else if (eNB->scheduling_request_config[UE_id].sr_ConfigIndex <= 154) { // 80 ms SR period
    if ((10*(frame&7)+subframe) == (eNB->scheduling_request_config[UE_id].sr_ConfigIndex-75))
154
      return(1);
155 156 157 158
  }

  return(0);
}
159

160

161
int32_t add_ue(int16_t rnti, PHY_VARS_eNB *eNB)
162
{
163
  uint8_t i;
164

165 166

  LOG_D(PHY,"[eNB %d/%d] Adding UE with rnti %x\n",
167 168
        eNB->Mod_id,
        eNB->CC_id,
169
        (uint16_t)rnti);
170 171

  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
172
    if ((eNB->dlsch[i]==NULL) || (eNB->ulsch[i]==NULL)) {
173
      MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed add ue %"PRIx16" (ENOMEM)", rnti);
174 175
      LOG_E(PHY,"Can't add UE, not enough memory allocated\n");
      return(-1);
176
    } else {
177
      if (eNB->UE_stats[i].crnti==0) {
178
        MSC_LOG_EVENT(MSC_PHY_ENB, "0 Add ue %"PRIx16" ", rnti);
179
        LOG_D(PHY,"UE_id %d associated with rnti %x\n",i, (uint16_t)rnti);
180 181 182
        eNB->dlsch[i][0]->rnti = rnti;
        eNB->ulsch[i]->rnti = rnti;
        eNB->UE_stats[i].crnti = rnti;
183

184 185 186 187 188 189 190
  eNB->UE_stats[i].Po_PUCCH1_below = 0;
  eNB->UE_stats[i].Po_PUCCH1_above = (int32_t)pow(10.0,.1*(eNB->frame_parms.ul_power_control_config_common.p0_NominalPUCCH+eNB->rx_total_gain_dB));
  eNB->UE_stats[i].Po_PUCCH        = (int32_t)pow(10.0,.1*(eNB->frame_parms.ul_power_control_config_common.p0_NominalPUCCH+eNB->rx_total_gain_dB));
  LOG_D(PHY,"Initializing Po_PUCCH: p0_NominalPUCCH %d, gain %d => %d\n",
        eNB->frame_parms.ul_power_control_config_common.p0_NominalPUCCH,
        eNB->rx_total_gain_dB,
        eNB->UE_stats[i].Po_PUCCH);
191
  
192
        return(i);
193
      }
194
    }
195 196 197 198
  }
  return(-1);
}

199
int mac_phy_remove_ue(module_id_t Mod_idP,rnti_t rntiP) {
200
  uint8_t i;
201
  int CC_id;
202
  PHY_VARS_eNB *eNB;
203 204

  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
205
    eNB = PHY_vars_eNB_g[Mod_idP][CC_id];
206
    for (i=0; i<NUMBER_OF_UE_MAX; i++) {
207
      if ((eNB->dlsch[i]==NULL) || (eNB->ulsch[i]==NULL)) {
208 209 210
  MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (ENOMEM)", rntiP);
  LOG_E(PHY,"Can't remove UE, not enough memory allocated\n");
  return(-1);
211
      } else {
212 213
  if (eNB->UE_stats[i].crnti==rntiP) {
    MSC_LOG_EVENT(MSC_PHY_ENB, "0 Removed ue %"PRIx16" ", rntiP);
214

215
    LOG_D(PHY,"eNB %d removing UE %d with rnti %x\n",eNB->Mod_id,i,rntiP);
216

217 218 219 220 221 222 223
    //LOG_D(PHY,("[PHY] UE_id %d\n",i);
    clean_eNb_dlsch(eNB->dlsch[i][0]);
    clean_eNb_ulsch(eNB->ulsch[i]);
    //eNB->UE_stats[i].crnti = 0;
    memset(&eNB->UE_stats[i],0,sizeof(LTE_eNB_UE_stats));
    //  mac_exit_wrapper("Removing UE");
    
224

225 226
    return(i);
  }
227
      }
228
    }
229
  }
230
  MSC_LOG_EVENT(MSC_PHY_ENB, "0 Failed remove ue %"PRIx16" (not found)", rntiP);
231 232 233
  return(-1);
}

234
int8_t find_next_ue_index(PHY_VARS_eNB *eNB)
235
{
236
  uint8_t i;
237

238
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
239 240
    if (eNB->UE_stats[i].crnti==0) {
      /*if ((eNB->dlsch[i]) &&
241 242
  (eNB->dlsch[i][0]) &&
  (eNB->dlsch[i][0]->rnti==0))*/
243 244 245
      LOG_D(PHY,"Next free UE id is %d\n",i);
      return(i);
    }
246
  }
247

248 249 250
  return(-1);
}

251
int get_ue_active_harq_pid(const uint8_t Mod_id,const uint8_t CC_id,const uint16_t rnti, const int frame, const uint8_t subframe,uint8_t *harq_pid,uint8_t *round,const uint8_t harq_flag)
252 253 254 255
{
  LTE_eNB_DLSCH_t *DLSCH_ptr;
  LTE_eNB_ULSCH_t *ULSCH_ptr;
  uint8_t ulsch_subframe,ulsch_frame;
256
  int i;
Raymond Knopp's avatar
 
Raymond Knopp committed
257
  int8_t UE_id = find_ue(rnti,PHY_vars_eNB_g[Mod_id][CC_id]);
258 259

  if (UE_id==-1) {
260
    LOG_D(PHY,"Cannot find UE with rnti %x (Mod_id %d, CC_id %d)\n",rnti, Mod_id, CC_id);
261 262
    *round=0;
    return(-1);
263 264
  }

265 266
  if ((harq_flag == openair_harq_DL) || (harq_flag == openair_harq_RA))  {// this is a DL request

267
    DLSCH_ptr = PHY_vars_eNB_g[Mod_id][CC_id]->dlsch[(uint32_t)UE_id][0];
268

269 270
    if (harq_flag == openair_harq_RA) {
      if (DLSCH_ptr->harq_processes[0] != NULL) {
271 272 273
  *harq_pid = 0;
  *round = DLSCH_ptr->harq_processes[0]->round;
  return 0;
274
      } else {
275
  return -1;
276 277 278
      }
    }

279 280
    /* let's go synchronous for the moment - maybe we can change at some point */
    i = (frame * 10 + subframe) % 8;
281

282 283 284 285 286
    if (DLSCH_ptr->harq_processes[i]->status == ACTIVE) {
      *harq_pid = i;
      *round = DLSCH_ptr->harq_processes[i]->round;
    } else if (DLSCH_ptr->harq_processes[i]->status == SCH_IDLE) {
      *harq_pid = i;
287
      *round = 0;
288 289 290
    } else {
      printf("%s:%d: bad state for harq process - PLEASE REPORT!!\n", __FILE__, __LINE__);
      abort();
291
    }
292
  } else { // This is a UL request
293

294 295 296
    ULSCH_ptr = PHY_vars_eNB_g[Mod_id][CC_id]->ulsch[(uint32_t)UE_id];
    ulsch_subframe = pdcch_alloc2ul_subframe(&PHY_vars_eNB_g[Mod_id][CC_id]->frame_parms,subframe);
    ulsch_frame    = pdcch_alloc2ul_frame(&PHY_vars_eNB_g[Mod_id][CC_id]->frame_parms,frame,subframe);
297
    // Note this is for TDD configuration 3,4,5 only
298
    *harq_pid = subframe2harq_pid(&PHY_vars_eNB_g[Mod_id][CC_id]->frame_parms,
299 300
                                  ulsch_frame,
                                  ulsch_subframe);
301
    *round    = ULSCH_ptr->harq_processes[*harq_pid]->round;
302
    LOG_T(PHY,"[eNB %d][PUSCH %d] Frame %d subframe %d Checking HARQ, round %d\n",Mod_id,*harq_pid,frame,subframe,*round);
303
  }
304

305 306 307
  return(0);
}

308
int16_t get_target_pusch_rx_power(const module_id_t module_idP, const uint8_t CC_id)
309
{
310
  return PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.ul_power_control_config_common.p0_NominalPUSCH;
311 312
}

313 314
int16_t get_target_pucch_rx_power(const module_id_t module_idP, const uint8_t CC_id)
{
315
  return PHY_vars_eNB_g[module_idP][CC_id]->frame_parms.ul_power_control_config_common.p0_NominalPUCCH;
316 317
}

318
#ifdef EMOS
319
void phy_procedures_emos_eNB_TX(unsigned char subframe, PHY_VARS_eNB *eNB)
320
{
321 322 323 324

}
#endif

325
void phy_procedures_eNB_S_RX(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,relaying_type_t r_type)
326 327
{
  UNUSED(r_type);
328
  int subframe = proc->subframe_rx;
329

330
#ifdef DEBUG_PHY_PROC
331
  LOG_D(PHY,"[eNB %d] Frame %d: Doing phy_procedures_eNB_S_RX(%d)\n", eNB->Mod_id,proc->frame_rx, subframe);
332
#endif
333

334

335
  if (eNB->abstraction_flag == 0) {
336
    lte_eNB_I0_measurements(eNB,
337
          subframe,
338
                            0,
339
                            eNB->first_run_I0_measurements);
Raymond Knopp's avatar
 
Raymond Knopp committed
340
  }
341

342
#ifdef PHY_ABSTRACTION
Raymond Knopp's avatar
 
Raymond Knopp committed
343
  else {
344
    lte_eNB_I0_measurements_emul(eNB,
345
                                 0);
346
  }
347

Raymond Knopp's avatar
 
Raymond Knopp committed
348 349
#endif

350

351 352
}

Raymond Knopp's avatar
 
Raymond Knopp committed
353 354


355
#ifdef EMOS
356
void phy_procedures_emos_eNB_RX(unsigned char subframe,PHY_VARS_eNB *eNB)
357 358
{

359 360
  uint8_t aa;
  uint16_t last_subframe_emos;
361
  uint16_t pilot_pos1 = 3 - eNB->frame_parms.Ncp, pilot_pos2 = 10 - 2*eNB->frame_parms.Ncp;
362
  uint32_t bytes;
363 364 365

  last_subframe_emos=0;

Raymond Knopp's avatar
 
Raymond Knopp committed
366

Raymond Knopp's avatar
 
Raymond Knopp committed
367

Raymond Knopp's avatar
 
Raymond Knopp committed
368

369
#ifdef EMOS_CHANNEL
370

371
  //if (last_slot%2==1) // this is for all UL subframes
372
  if (subframe==3)
373 374 375 376 377 378 379
    for (aa=0; aa<eNB->frame_parms.nb_antennas_rx; aa++) {
      memcpy(&emos_dump_eNB.channel[aa][last_subframe_emos*2*eNB->frame_parms.N_RB_UL*12],
             &eNB->pusch_vars[0]->drs_ch_estimates[0][aa][eNB->frame_parms.N_RB_UL*12*pilot_pos1],
             eNB->frame_parms.N_RB_UL*12*sizeof(int));
      memcpy(&emos_dump_eNB.channel[aa][(last_subframe_emos*2+1)*eNB->frame_parms.N_RB_UL*12],
             &eNB->pusch_vars[0]->drs_ch_estimates[0][aa][eNB->frame_parms.N_RB_UL*12*pilot_pos2],
             eNB->frame_parms.N_RB_UL*12*sizeof(int));
380
    }
381

382 383
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
384
  if (subframe==4) {
385
    emos_dump_eNB.timestamp = rt_get_time_ns();
386 387 388 389 390
    emos_dump_eNB.frame_tx = eNB->proc[subframe].frame_rx;
    emos_dump_eNB.rx_total_gain_dB = eNB->rx_total_gain_dB;
    emos_dump_eNB.mimo_mode = eNB->transmission_mode[0];
    memcpy(&emos_dump_eNB.measurements,
           &eNB->measurements[0],
391
           sizeof(PHY_MEASUREMENTS_eNB));
392
    memcpy(&emos_dump_eNB.UE_stats[0],&eNB->UE_stats[0],NUMBER_OF_UE_MAX*sizeof(LTE_eNB_UE_stats));
393 394

    bytes = rtf_put(CHANSOUNDER_FIFO_MINOR, &emos_dump_eNB, sizeof(fifo_dump_emos_eNB));
395

396 397
    //bytes = rtf_put(CHANSOUNDER_FIFO_MINOR, "test", sizeof("test"));
    if (bytes!=sizeof(fifo_dump_emos_eNB)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
398
      LOG_W(PHY,"[eNB %d] Frame %d, subframe %d, Problem writing EMOS data to FIFO (bytes=%d, size=%d)\n",
399
            eNB->Mod_id,eNB->proc[(subframe+1)%10].frame_rx, subframe,bytes,sizeof(fifo_dump_emos_eNB));
400
    } else {
401
      if (eNB->proc[(subframe+1)%10].frame_tx%100==0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
402
        LOG_I(PHY,"[eNB %d] Frame %d (%d), subframe %d, Writing %d bytes EMOS data to FIFO\n",
403
              eNB->Mod_id,eNB->proc[(subframe+1)%10].frame_rx, ((fifo_dump_emos_eNB*)&emos_dump_eNB)->frame_tx, subframe, bytes);
404
      }
405
    }
406 407 408 409 410 411 412
  }
}
#endif


#define AMP_OVER_SQRT2 ((AMP*ONE_OVER_SQRT2_Q15)>>15)
#define AMP_OVER_2 (AMP>>1)
413 414
int QPSK[4]= {AMP_OVER_SQRT2|(AMP_OVER_SQRT2<<16),AMP_OVER_SQRT2|((65536-AMP_OVER_SQRT2)<<16),((65536-AMP_OVER_SQRT2)<<16)|AMP_OVER_SQRT2,((65536-AMP_OVER_SQRT2)<<16)|(65536-AMP_OVER_SQRT2)};
int QPSK2[4]= {AMP_OVER_2|(AMP_OVER_2<<16),AMP_OVER_2|((65536-AMP_OVER_2)<<16),((65536-AMP_OVER_2)<<16)|AMP_OVER_2,((65536-AMP_OVER_2)<<16)|(65536-AMP_OVER_2)};
415

Lionel Gauthier's avatar
Lionel Gauthier committed
416

Raymond Knopp's avatar
 
Raymond Knopp committed
417

Cedric Roux's avatar
Cedric Roux committed
418
unsigned int taus(void);
419
DCI_PDU DCI_pdu_tmp;
Raymond Knopp's avatar
 
Raymond Knopp committed
420

421

422
void pmch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,PHY_VARS_RN *rn,relaying_type_t r_type) {
423 424


Cedric Roux's avatar
Cedric Roux committed
425
#if defined(Rel10) || defined(Rel14)
426 427
  MCH_PDU *mch_pduP;
  MCH_PDU  mch_pdu;
428
  //  uint8_t sync_area=255;
429
#endif
430

431
  int subframe = proc->subframe_tx;
432

433
  if (eNB->abstraction_flag==0) {
434 435
    // This is DL-Cell spec pilots in Control region
    generate_pilots_slot(eNB,
436 437 438
       eNB->common_vars.txdataF[0],
       AMP,
       subframe<<1,1);
Raymond Knopp's avatar
 
Raymond Knopp committed
439
  }
440
  
Cedric Roux's avatar
Cedric Roux committed
441
#if defined(Rel10) || defined(Rel14)
442 443 444
  // if mcch is active, send regardless of the node type: eNB or RN
  // when mcch is active, MAC sched does not allow MCCH and MTCH multiplexing
  mch_pduP = mac_xface->get_mch_sdu(eNB->Mod_id,
445 446 447
            eNB->CC_id,
            proc->frame_tx,
            subframe);
448 449 450 451 452
  
  switch (r_type) {
  case no_relay:
    if ((mch_pduP->Pdu_size > 0) && (mch_pduP->sync_area == 0)) // TEST: only transmit mcch for sync area 0
      LOG_I(PHY,"[eNB%"PRIu8"] Frame %d subframe %d : Got MCH pdu for MBSFN (MCS %"PRIu8", TBS %d) \n",
453 454
      eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->mcs,
      eNB->dlsch_MCH->harq_processes[0]->TBS>>3);
455 456
    else {
      LOG_D(PHY,"[DeNB %"PRIu8"] Frame %d subframe %d : Do not transmit MCH pdu for MBSFN sync area %"PRIu8" (%s)\n",
457 458
      eNB->Mod_id,proc->frame_tx,subframe,mch_pduP->sync_area,
      (mch_pduP->Pdu_size == 0)? "Empty MCH PDU":"Let RN transmit for the moment");
459
      mch_pduP = NULL;
Lionel Gauthier's avatar
Lionel Gauthier committed
460
    }
461
    
Raymond Knopp's avatar
 
Raymond Knopp committed
462
    break;
463 464 465 466
    
  case multicast_relay:
    if ((mch_pduP->Pdu_size > 0) && ((mch_pduP->mcch_active == 1) || mch_pduP->msi_active==1)) {
      LOG_I(PHY,"[RN %"PRIu8"] Frame %d subframe %d: Got the MCH PDU for MBSFN  sync area %"PRIu8" (MCS %"PRIu8", TBS %"PRIu16")\n",
467 468
      rn->Mod_id,rn->frame, subframe,
      mch_pduP->sync_area,mch_pduP->mcs,mch_pduP->Pdu_size);
469 470 471
    } else if (rn->mch_avtive[subframe%5] == 1) { // SF2 -> SF7, SF3 -> SF8
      mch_pduP= &mch_pdu;
      memcpy(&mch_pduP->payload, // could be a simple copy
472 473
       rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->b,
       rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->TBS>>3);
474 475 476
      mch_pduP->Pdu_size = (uint16_t) (rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->TBS>>3);
      mch_pduP->mcs = rn->dlsch_rn_MCH[subframe%5]->harq_processes[0]->mcs;
      LOG_I(PHY,"[RN %"PRIu8"] Frame %d subframe %d: Forward the MCH PDU for MBSFN received on SF %d sync area %"PRIu8" (MCS %"PRIu8", TBS %"PRIu16")\n",
477 478
      rn->Mod_id,rn->frame, subframe,subframe%5,
      rn->sync_area[subframe%5],mch_pduP->mcs,mch_pduP->Pdu_size);
Raymond Knopp's avatar
 
Raymond Knopp committed
479
    } else {
480
      mch_pduP=NULL;
Raymond Knopp's avatar
 
Raymond Knopp committed
481
    }
482 483
    
    rn->mch_avtive[subframe]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
484
    break;
485
    
486
  default:
487
    LOG_W(PHY,"[eNB %"PRIu8"] Frame %d subframe %d: unknown relaying type %d \n",
488
    eNB->Mod_id,proc->frame_tx,subframe,r_type);
489 490 491 492 493
    mch_pduP=NULL;
    break;
  }// switch
  
  if (mch_pduP) {
494
    fill_eNB_dlsch_MCH(eNB,mch_pduP->mcs,1,0);
495
    // Generate PMCH
496
    generate_mch(eNB,proc,(uint8_t*)mch_pduP->payload);
497
  } else {
498
    LOG_D(PHY,"[eNB/RN] Frame %d subframe %d: MCH not generated \n",proc->frame_tx,subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
499
  }
500
  
501
#endif
502
}
503

Matthieu Kanj's avatar
Matthieu Kanj committed
504 505 506
int16_t buffer_npusch[153600];
int16_t buffer_npusch_ext[153600];

507
void common_signal_procedures (PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc) {
508

509 510
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  int **txdataF = eNB->common_vars.txdataF[0];
Matthieu Kanj's avatar
Matthieu Kanj committed
511
  //////////////////////////////////////////////////////// to uncomment for LTE,      uint8_t *pbch_pdu=&eNB->pbch_pdu[0];
512 513
  int subframe = proc->subframe_tx;
  int frame = proc->frame_tx;
Matthieu Kanj's avatar
Matthieu Kanj committed
514
  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&eNB_mac_inst[eNB->Mod_id].common_channels[eNB->CC_id].RA_template[0];
Matthieu Kanj's avatar
Matthieu Kanj committed
515
  //int                     With_NSSS=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
  int framerx = proc->frame_rx; 
  int subframerx = proc->subframe_rx;
  //int                     With_NSSS=0; 
  //printf("\n in eNB_fep_full in frame_tx = %d \n",frame);

  /*if (proc->flag_msg3==1 && framerx==proc->frame_msg3 && subframerx==9)
  {
  printf("\n\n\n\n in writing in frame %d \n\n\n\n",frame);
    int16_t *Rx_buffer;
    FILE *fich = fopen("xyzabc.txt","w"); 
  
  Rx_buffer = &eNB->common_vars.rxdata[0][0][0]; // get the whole frame 
  memcpy(&buffer_npusch[0],&Rx_buffer[0],307200); 

    int pp; 
    for (pp=0;pp<153600;pp++)
  {
    fprintf(fich," %d ",buffer_npusch[pp]);
    }
  fclose(fich);
  exit(0); 
  }*/

Matthieu Kanj's avatar
Matthieu Kanj committed
539 540
  /////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////  RX NPUSH  //////////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
541

Matthieu Kanj's avatar
Matthieu Kanj committed
542
  if(subframe==proc->subframe_real && proc->flag_msg3==1 && frame==proc->frame_msg3 &&  proc->counter_msg3>0) ///&& frame == ????
Matthieu Kanj's avatar
Matthieu Kanj committed
543
  {
Matthieu Kanj's avatar
Matthieu Kanj committed
544 545 546 547 548 549 550 551 552 553 554 555
       proc->subframe_real++;
      if(proc->subframe_real==10) ///&& frame == ????
      {
          proc->subframe_real=0;
          proc->frame_msg3++;
      }
      if (proc->counter_msg3 ==8)
      {
        proc->frame_dscr_msg3 = framerx; 
        proc->subframe_dscr_msg3 = subframerx;
      }
      //printf("frame %d in demod NPUSCH = \n",frame);
Matthieu Kanj's avatar
Matthieu Kanj committed
556 557 558 559
      ///////////////////////////////////////////////////  NPUSH DEMOD ////////////////////////////////////
      // LTE_eNB_COMMON      *common_vars  =  &eNB->common_vars;
      LTE_eNB_PUSCH       *pusch_vars   =  eNB->pusch_vars[0];
      //NB_IoT_DL_FRAME_PARMS  *frame_parms  =  &eNB->frame_parms; 
Matthieu Kanj's avatar
Matthieu Kanj committed
560 561 562 563 564
      NB_IoT_eNB_NULSCH_t    **ulsch_NB_IoT   =  &eNB->ulsch_NB_IoT;//[0][0]; 
      int l; //i;
      //int32_t avgU[2]; 
      //int32_t    avgs;
      //uint8_t    log2_maxh = 0;
Matthieu Kanj's avatar
Matthieu Kanj committed
565 566 567
      uint8_t pilot_pos1 = 3, pilot_pos2 = 10; // holds for npusch format 1, and 15 kHz subcarrier bandwidth
      int16_t    *llrp;
      uint8_t nb_rb=1;       //ulsch_NB_IoT[0]->harq_process->nb_rb,  //////////////// high level parameter
Matthieu Kanj's avatar
Matthieu Kanj committed
568

Matthieu Kanj's avatar
Matthieu Kanj committed
569
      //buffer_npusch = common_vars->rxdataF[0]; 
Matthieu Kanj's avatar
Matthieu Kanj committed
570
      //printf("\n\n frame_msg3=%d  subframe = %d\n",frame,proc->subframe_real); 
Matthieu Kanj's avatar
Matthieu Kanj committed
571 572 573 574 575 576 577 578 579

      for (l=0; l<fp->symbols_per_tti; l++)
      { 

          ulsch_extract_rbs_single_NB_IoT(eNB->common_vars.rxdataF[0],
                                          pusch_vars->rxdataF_ext[0],
                                          // ulsch[UE_id]->harq_process->first_rb, 
                                          22,    //ulsch[UE_id]->harq_process->UL_RB_ID_NB_IoT, // index of UL NB_IoT resource block 
                                          1, //ulsch_NB_IoT[0]->harq_process->N_sc_RU, // number of subcarriers in UL  //////////////// high level parameter
Matthieu Kanj's avatar
Matthieu Kanj committed
580
                                          (uint8_t)proc->subframe_delay,//subframe,// ulsch[UE_id]->harq_process->I_sc, // subcarrier indication field
Matthieu Kanj's avatar
Matthieu Kanj committed
581 582 583 584
                                          nb_rb, 
                                          l%(fp->symbols_per_tti/2),
                                          l/(fp->symbols_per_tti/2),
                                          fp);
Matthieu Kanj's avatar
Matthieu Kanj committed
585 586 587 588 589 590 591
            /// Channel Estimation
    ul_chest_tmp_NB_IoT(pusch_vars->rxdataF_ext[0],
      pusch_vars->drs_ch_estimates[0],
      l%(fp->symbols_per_tti/2), //symbol within slot 
      l/(fp->symbols_per_tti/2),
      proc->counter_msg3,
      fp); 
Matthieu Kanj's avatar
Matthieu Kanj committed
592 593

      }
Matthieu Kanj's avatar
Matthieu Kanj committed
594 595
      for (l=0; l<fp->symbols_per_tti; l++)
      { 
Matthieu Kanj's avatar
Matthieu Kanj committed
596

Matthieu Kanj's avatar
Matthieu Kanj committed
597 598 599 600 601 602 603
    /// Equalization
    ul_chequal_tmp_NB_IoT(pusch_vars->rxdataF_ext[0],
      pusch_vars->rxdataF_comp[0],
      pusch_vars->drs_ch_estimates[0],
      l%(fp->symbols_per_tti/2), //symbol within slot 
      l/(fp->symbols_per_tti/2),
      fp);
Matthieu Kanj's avatar
Matthieu Kanj committed
604 605

      }
Matthieu Kanj's avatar
Matthieu Kanj committed
606 607
      for (l=0; l<fp->symbols_per_tti; l++)
      { 
Matthieu Kanj's avatar
Matthieu Kanj committed
608

Matthieu Kanj's avatar
Matthieu Kanj committed
609 610 611 612 613 614 615 616
         /// In case of 1 subcarrier: BPSK and QPSK should be rotated by pi/2 and pi/4, respectively 
          rotate_single_carrier_NB_IoT(eNB, 
                                       fp, 
                                       pusch_vars->rxdataF_comp[0], 
                                       0, // UE ID
                                       l, 
               proc->counter_msg3,
                                       2); // Qm
Matthieu Kanj's avatar
Matthieu Kanj committed
617

Matthieu Kanj's avatar
Matthieu Kanj committed
618
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
619

Matthieu Kanj's avatar
Matthieu Kanj committed
620 621 622 623
        proc->subframe_delay++;
  if(proc->subframe_delay==10) ///&& frame == ????
      {
  proc->subframe_delay=0; 
Matthieu Kanj's avatar
Matthieu Kanj committed
624
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
625 626
  proc->subframe_delay++; 

Matthieu Kanj's avatar
Matthieu Kanj committed
627 628 629



Matthieu Kanj's avatar
Matthieu Kanj committed
630 631 632 633 634 635 636
      ///////// IDFT inverse precoding is done over the whole subframe of 14 - 2 (pilots) symbols
      //lte_idft_NB_IoT(fp,
      //                (uint32_t*)pusch_vars->rxdataF_ext[0][0],
      //                12);  // IDFT size 

      llrp = (int16_t*)&pusch_vars->llr[0+ (8-proc->counter_msg3)*24]; 
  int ii=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
637 638 639 640 641 642
      for (l=0; l<fp->symbols_per_tti; l++)
      {
          if (l==pilot_pos1 || l==pilot_pos2)   // skip pilots
          {
              l++;
          }
Matthieu Kanj's avatar
Matthieu Kanj committed
643
 
Matthieu Kanj's avatar
Matthieu Kanj committed
644 645 646 647
          ulsch_qpsk_llr_NB_IoT(eNB, 
                                fp,
                                pusch_vars->rxdataF_comp[0],
                                pusch_vars->llr,
Matthieu Kanj's avatar
Matthieu Kanj committed
648
                                l, 
Matthieu Kanj's avatar
Matthieu Kanj committed
649
                                0, // UE ID
Matthieu Kanj's avatar
Matthieu Kanj committed
650 651 652
                                &llrp[ii*2]); //// !!! Pensez à créer un buffer de longueur 8 subframes 
  ii++;
  
Matthieu Kanj's avatar
Matthieu Kanj committed
653
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
654 655 656 657 658
  /*printf("\n");
  for (l=0;l<24;l++){
    printf("  llr = %d  ",pusch_vars->llr[(8-proc->counter_msg3)*24+l]);  
  }*/
  
Matthieu Kanj's avatar
Matthieu Kanj committed
659 660 661
      ///////////////////////////////////////////////////  NPUSH DECOD ////////////////////////////////////
      if(proc->counter_msg3==1)
      {
Matthieu Kanj's avatar
Matthieu Kanj committed
662
  //printf("\nframe %d in decod NPUSCH = \n",frame);
Matthieu Kanj's avatar
Matthieu Kanj committed
663 664 665 666 667 668 669 670
          int16_t                 *ulsch_llr    = eNB->pusch_vars[0]->llr;  //UE_id=0
          //NB_IoT_DL_FRAME_PARMS   *frame_parms  = &eNB->frame_parms;
          //NB_IoT_eNB_NULSCH_t     *ulsch        = eNB->ulsch[0];
          NB_IoT_UL_eNB_HARQ_t    *ulsch_harq = ulsch_NB_IoT[0]->harq_process;

          //uint8_t         harq_pid;
          unsigned int    A;
          uint8_t         Q_m;
Matthieu Kanj's avatar
Matthieu Kanj committed
671
          unsigned int    j,j2; //i2,
Matthieu Kanj's avatar
Matthieu Kanj committed
672 673 674 675 676 677
          int             iprime;
          //unsigned int    ret = 0;
          int             r,Kr;
          unsigned int    sumKr=0;
          unsigned int    G,H,Hprime,Hpp,Cmux,Rmux_prime;     // Q_CQI,Q_RI=0
          uint32_t        x1, x2, s=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
678
          //int16_t           c;
Matthieu Kanj's avatar
Matthieu Kanj committed
679 680
          int16_t         y[6*14*1200] __attribute__((aligned(32)));
          uint8_t         ytag[14*1200];
Matthieu Kanj's avatar
Matthieu Kanj committed
681 682
          //int16_t         cseq[6*14*1200];
          //uint32_t         subframe = 1;        // first subframe of Msg3 received   // required for scrambling
Matthieu Kanj's avatar
Matthieu Kanj committed
683
          uint32_t     rnti_tmp= 65522; // true rnti should be used
Matthieu Kanj's avatar
Matthieu Kanj committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
    uint8_t      reset; 

    /*int16_t *Rx_buffer;
    FILE *fich = fopen("xyzabc.txt","w"); 
  
  Rx_buffer = &ulsch_llr[0]; // get the whole frame 
  memcpy(&buffer_npusch[0],&Rx_buffer[0],2*2*8*12); 

    int pp; 
    for (pp=0;pp<2*8*12;pp++)
  {
    fprintf(fich," %d ",buffer_npusch[pp]);
    }
  fclose(fich);
  exit(0); */

Matthieu Kanj's avatar
Matthieu Kanj committed
700 701 702 703
          // NB-IoT ///////////////////////////////////////////////
          // x1 is set in lte_gold_generic
          // x2 should not reinitialized each subframe
          // x2 should be reinitialized according to 36.211 Sections 10.1.3.1 and 10.1.3.6
Matthieu Kanj's avatar
Matthieu Kanj committed
704
          x2         =  (rnti_tmp<<14) + (proc->subframe_dscr_msg3<<9) + ((proc->frame_dscr_msg3%2)<<13) + fp->Nid_cell; //this is c_init in 36.211 Sec 10.1.3.1
Matthieu Kanj's avatar
Matthieu Kanj committed
705 706 707 708 709
          //ulsch_harq =  ulsch[0]->harq_process;
          A     =    88; // ulsch_harq->TBS;
          Q_m   = get_Qm_ul_NB_IoT(2,1);            ///// ulsch_harq->mcs,ulsch_harq->N_sc_RU
          G     = (1 * Q_m) * 6 * 16; // Vincent : see 36.212, Section 5.1.4.1.2   
          //G = ulsch_harq->N_sc_RU * Q_m) * ulsch_harq->Nsymb_UL * ulsch_harq->Nslot_UL;   (= number of RE * 2 - pilots)
Matthieu Kanj's avatar
Matthieu Kanj committed
710 711
  
  //printf("ulsch_harq->round = %d",ulsch_harq->round);
Matthieu Kanj's avatar
Matthieu Kanj committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725
          if (ulsch_harq->round == 0)
          {
              // This is a new packet, so compute quantities regarding segmentation
              ulsch_harq->B = A+24;
              lte_segmentation_NB_IoT(NULL,
                                      NULL,
                                      ulsch_harq->B,
                                      &ulsch_harq->C,
                                      &ulsch_harq->Cplus,
                                      &ulsch_harq->Cminus,
                                      &ulsch_harq->Kplus,
                                      &ulsch_harq->Kminus,
                                      &ulsch_harq->F);
              //  CLEAR LLR's HERE for first packet in process
Matthieu Kanj's avatar
Matthieu Kanj committed
726
    //printf("\nin segmentation\n");
Matthieu Kanj's avatar
Matthieu Kanj committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
          }

          sumKr = 0;

          for (r=0; r<ulsch_harq->C; r++)
          {
              if (r<ulsch_harq->Cminus)
              {
                  Kr = ulsch_harq->Kminus;
              }else{

                  Kr = ulsch_harq->Kplus;
              }
              sumKr += Kr;
          }

          ulsch_harq->G = G;
          H      = G ;
          Hprime = H/Q_m;
          // Demultiplexing/Deinterleaving of PUSCH/ACK/RI/CQI
          Hpp        = Hprime;
          // Cmux       = (ulsch_harq->Nsymb_UL-1)*ulsch_harq->Nslot_UL; // see definition in 36.212, Section 6.3.2, but not consistent with definition
          // of RU in 36.211, Section 10.1.2.3. Maybe prefer the following: 
          Cmux       =   6*16; //////////////(ulsch_harq->Nsymb_UL)*ulsch_harq->Nslot_UL; 
          Rmux_prime = Hpp/Cmux;
          // Clear "tag" interleaving matrix to allow for CQI/DATA identification
          memset(ytag,0,Cmux*Rmux_prime);
          memset(y,LTE_NULL_NB_IoT,Q_m*Hpp);
          // read in buffer and unscramble llrs for everything but placeholder bits
          // llrs stored per symbol correspond to columns of interleaving matrix

Matthieu Kanj's avatar
Matthieu Kanj committed
758
    reset = 1; 
Matthieu Kanj's avatar
Matthieu Kanj committed
759 760 761 762 763
          switch (Q_m)
          {
              case 1: 
                  for (j=0; j<Cmux; j++)
                  { 
Matthieu Kanj's avatar
Matthieu Kanj committed
764
                      //y[j] = cseq[j]*ulsch_llr[j]; /// To be defined for bpsk
Matthieu Kanj's avatar
Matthieu Kanj committed
765 766 767
                  }
              break; 
              case 2:
Matthieu Kanj's avatar
Matthieu Kanj committed
768
                  for (j=0; j<Cmux*2; j++)
Matthieu Kanj's avatar
Matthieu Kanj committed
769
                  {
Matthieu Kanj's avatar
Matthieu Kanj committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783

            if (j%32==0) 
      {
              s = lte_gold_generic(&x1, &x2, reset);
              //      printf("lte_gold[%d]=%x\n",i,s);
              reset = 0;
          }
      if (((s>>(j%32))&1)==0)
      {
              y[j] = -ulsch_llr[j];
          }else
      {
        y[j] = ulsch_llr[j];
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
784 785 786
                  }
              break;
          }
Matthieu Kanj's avatar
Matthieu Kanj committed
787 788 789 790
  printf("\n \n");
         /*for (j=0;j<140;j++){
    printf(" y = %d    ",y[j]); 
  }*/
Matthieu Kanj's avatar
Matthieu Kanj committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
          // CQI and Data bits
          j  = 0;
          j2 = 0;
          int16_t *yp,*ep;
          for (iprime=0,yp=&y[j2],ep=&ulsch_harq->e[0]; iprime<G; iprime+=8,j2+=8,ep+=8,yp+=8)
          {
               ep[0] = yp[0];
               ep[1] = yp[1];
               ep[2] = yp[2];
               ep[3] = yp[3];
               ep[4] = yp[4];
               ep[5] = yp[5];
               ep[6] = yp[6];
               ep[7] = yp[7];
          }
Matthieu Kanj's avatar
Matthieu Kanj committed
806 807 808 809 810 811

      /*printf("\n \n");
            for (j=0;j<140;j++){
        printf(" e = %d    ",ulsch_harq->e[j]); 
      }*/

Matthieu Kanj's avatar
Matthieu Kanj committed
812
          // Do ULSCH Decoding for data portion
Matthieu Kanj's avatar
Matthieu Kanj committed
813

Matthieu Kanj's avatar
Matthieu Kanj committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 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
          r=0;
          Kr=0;
          unsigned int r_offset=0,Kr_bytes,iind;
          uint8_t crc_type;
          int offset = 0;
          int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS_NB_IoT][3*(6144+64)];
          //NB_IoT_UL_eNB_HARQ_t *ulsch_harq = ulsch_NB_IoT[0]->harq_process;  ///
          int ret = 1;
          unsigned int E; 

          uint8_t (*tc)(int16_t *y,
                        uint8_t *,
                        uint16_t,
                        uint16_t,
                        uint16_t,
                        uint8_t,
                        uint8_t,
                        uint8_t,
                        time_stats_t *,
                        time_stats_t *,
                        time_stats_t *,
                        time_stats_t *,
                        time_stats_t *,
                        time_stats_t *,
                        time_stats_t *);

          tc = phy_threegpplte_turbo_decoder16;

          for (r=0; r<ulsch_harq->C; r++)
          {
              // Get Turbo interleaver parameters
              if (r<ulsch_harq->Cminus)
              {
                  Kr = ulsch_harq->Kminus;
              } else{
                  Kr = ulsch_harq->Kplus;
              }

              Kr_bytes = Kr>>3;

              if (Kr_bytes<=64)
              {
                  iind = (Kr_bytes-5);

              } else if (Kr_bytes <=128) { 
                  
                  iind = 59 + ((Kr_bytes-64)>>1);

              } else if (Kr_bytes <= 256) {
                  
                  iind = 91 + ((Kr_bytes-128)>>2);

              } else if (Kr_bytes <= 768) { 
                  
                  iind = 123 + ((Kr_bytes-256)>>3);

              } else {
                  LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
                  //return(-1);
              }

              memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
              ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                                    (uint8_t*)&dummy_w[r][0],
                                                    (r==0) ? ulsch_harq->F : 0);

              if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
                                             G,
                                             ulsch_harq->w[r],
                                             (uint8_t*) &dummy_w[r][0],
                                             ulsch_harq->e+r_offset,
                                             ulsch_harq->C,
                                             1, //////////////////////////////// not used
                                             0,   //Uplink
                                             1,
Matthieu Kanj's avatar
Matthieu Kanj committed
889
                                             0,//ulsch_harq->rvidx,
Matthieu Kanj's avatar
Matthieu Kanj committed
890 891 892 893 894 895 896 897 898
                                             (ulsch_harq->round==0)?1:0,  // clear
                                             2,//get_Qm_ul(ulsch_harq->mcs),
                                             1,
                                             r,
                                             &E)==-1) 
              {
                  LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
                  //return(-1);
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
899 900 901 902 903 904
              r_offset += E; 

      /*printf("\n \n");
            for (j=0;j<140;j++){
        printf(" w = %d    ",ulsch_harq->w[r][j]);  
      }*/   
Matthieu Kanj's avatar
Matthieu Kanj committed
905 906 907
  
              sub_block_deinterleaving_turbo(4+Kr,
                                             &ulsch_harq->d[r][96],
Matthieu Kanj's avatar
Matthieu Kanj committed
908 909 910 911 912 913
                                             ulsch_harq->w[r]); 

      /*printf("\n \n");
            for (j=0;j<140;j++){
        printf(" d = %d    ",ulsch_harq->d[r][96+j]); 
      }*/
914

Matthieu Kanj's avatar
Matthieu Kanj committed
915 916 917 918 919 920
              if (ulsch_harq->C == 1)
              { 
                  crc_type = CRC24_A;
              }else{
                  crc_type = CRC24_B;
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
921
    // turbo decoding and CRC 
Matthieu Kanj's avatar
Matthieu Kanj committed
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
              ret = tc(&ulsch_harq->d[r][96],
                       ulsch_harq->c[r],
                       Kr,
                       f1f2mat_old[iind*2],
                       f1f2mat_old[(iind*2)+1],
                       ulsch_NB_IoT[0]->max_turbo_iterations, //MAX_TURBO_ITERATIONS,
                       crc_type,
                       (r==0) ? ulsch_harq->F : 0,
                       &eNB->ulsch_tc_init_stats,
                       &eNB->ulsch_tc_alpha_stats,
                       &eNB->ulsch_tc_beta_stats,
                       &eNB->ulsch_tc_gamma_stats,
                       &eNB->ulsch_tc_ext_stats,
                       &eNB->ulsch_tc_intl1_stats,
                       &eNB->ulsch_tc_intl2_stats); 

Matthieu Kanj's avatar
Matthieu Kanj committed
938 939 940 941

             if (ret != (1+ulsch_NB_IoT[0]->max_turbo_iterations))
             {   
    //printf("\n                      in last cdn \n");
Matthieu Kanj's avatar
Matthieu Kanj committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
                  if (r<ulsch_harq->Cminus)
                  {
                      Kr = ulsch_harq->Kminus;
                  } else {
                      Kr = ulsch_harq->Kplus; 
                      Kr_bytes = Kr>>3;
                  }
                  if (r==0)
                  {
                      memcpy(ulsch_harq->b,
                            &ulsch_harq->c[0][(ulsch_harq->F>>3)],
                            Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0));
                            offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
                  } else {
                      memcpy(ulsch_harq->b+offset,
                             ulsch_harq->c[r],
                             Kr_bytes - ((ulsch_harq->C>1)?3:0));
                             offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
                  }  
              } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
962
      //printf("\n                      in last cdn  break \n");
Matthieu Kanj's avatar
Matthieu Kanj committed
963 964 965 966
                    break;
              }
          } //  r loop end

Matthieu Kanj's avatar
Matthieu Kanj committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
          uint8_t *msg3 = &eNB->msg3_pdu[0];
          printf("pdu[0] = %d \n",ulsch_harq->b[0]);
          printf("pdu[1] = %d \n",ulsch_harq->b[1]);
          printf("pdu[2] = %d \n",ulsch_harq->b[2]);
          printf("pdu[3] = %d \n",ulsch_harq->b[3]);
    printf("pdu[2] = %d \n",ulsch_harq->b[4]);
          printf("pdu[3] = %d \n",ulsch_harq->b[5]);
    printf("pdu[3] = %d \n",ulsch_harq->b[6]);
          printf("pdu[3] = %d \n",ulsch_harq->b[7]);
    printf("pdu[2] = %d \n",ulsch_harq->b[8]);
          printf("pdu[3] = %d \n",ulsch_harq->b[9]);

          uint8_t xo = 128;
          int m =0;
          for(m=0; m<6;m++)
          { 
            if((ulsch_harq->b[2+m]<<7) == 128)
            {
                msg3[m]= (ulsch_harq->b[3+m]>>1) ^ xo;
            } else {
                msg3[m]= (ulsch_harq->b[3+m]>>1);
            }
          }
Matthieu Kanj's avatar
Matthieu Kanj committed
990

Matthieu Kanj's avatar
Matthieu Kanj committed
991 992 993
          proc->flag_DCI_msg4 ==1 ;
          proc->counter_DCI_msg4=4;

Matthieu Kanj's avatar
Matthieu Kanj committed
994 995 996 997 998
      }  // NPUSH decode end 
    proc->counter_msg3--;

  }

Matthieu Kanj's avatar
Matthieu Kanj committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013


//////////////////////////////////////////////////////////////////////////////////////////////////
if(proc->flag_msg4 == 1 && proc->counter_msg4 > 0)
{
    
    if(subframe==9)
    {
        proc->subframe_msg4=1;
        proc->frame_msg4= frame+1;

    } else {

        if(frame == proc->frame_msg4 && subframe == proc->subframe_msg4)
        {
Matthieu Kanj's avatar
Matthieu Kanj committed
1014
                 NB_IoT_DL_eNB_RAR_t  *rar  =  &eNB->ndlsch_rar.content_rar;
Matthieu Kanj's avatar
Matthieu Kanj committed
1015
                uint8_t   tab_rar[15];
Matthieu Kanj's avatar
Matthieu Kanj committed
1016
                //uint8_t   tab_rar[7];
Matthieu Kanj's avatar
Matthieu Kanj committed
1017
                uint8_t *nas_id = &eNB->msg3_pdu[0];
Matthieu Kanj's avatar
Matthieu Kanj committed
1018 1019 1020 1021 1022
                //uint8_t   *NAS_tab = &eNB->tab_nas;
                // avoid subframe 9 and subframe 0 of next frame
                tab_rar[0]=63;
                tab_rar[1]=60;
                tab_rar[2]=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1023 1024 1025 1026 1027 1028
                tab_rar[3]=0;  // NAS part 1 
                tab_rar[4]=nas_id[0];  // NAS part 2
                tab_rar[5]=nas_id[1]; // NAS part 3 
                tab_rar[6]=nas_id[2];  // NAS part 4
                tab_rar[7]=nas_id[3];   // NAS part 5
                tab_rar[8]=nas_id[4];   // NAS part 6
Matthieu Kanj's avatar
Matthieu Kanj committed
1029 1030 1031 1032 1033 1034 1035
                tab_rar[9]=30;
                tab_rar[10]=3;
                tab_rar[11]=0;
                tab_rar[12]=18;  
                tab_rar[13]=91;
                tab_rar[14]=8;

Matthieu Kanj's avatar
Matthieu Kanj committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    /*tab_rar[0]=28;
                tab_rar[1]=0; // NAS part 1 
                tab_rar[2]=nas_id[0]; // NAS part 2
                tab_rar[3]=nas_id[1];  // NAS part 3 
                tab_rar[4]=nas_id[2];  // NAS part 4
                tab_rar[5]=nas_id[3]; // NAS part 5 
                tab_rar[6]=nas_id[4];  // NAS part 6*/

    /*tab_rar[0]=28;
                tab_rar[1]=nas_id[0]; // NAS part 2
                tab_rar[2]=nas_id[1];  // NAS part 3 
                tab_rar[3]=nas_id[2];  // NAS part 4
                tab_rar[4]=nas_id[3]; // NAS part 5 
                tab_rar[5]=nas_id[4];  // NAS part 6
                tab_rar[6]=0;*/

    /*tab_rar[0]=28;
                tab_rar[1]=nas_id[4]; // NAS part 2
                tab_rar[2]=nas_id[3];  // NAS part 3 
                tab_rar[3]=nas_id[2];  // NAS part 4
                tab_rar[4]=nas_id[1]; // NAS part 5 
                tab_rar[5]=nas_id[0];  // NAS part 6
                tab_rar[6]=0;*/
        


    printf("ms4pdu[0] = %d \n",nas_id[0]);
          printf("ms4pdu[1] = %d \n",nas_id[1]);
          printf("ms4pdu[2] = %d \n",nas_id[2]);
          printf("ms4pdu[3] = %d \n",nas_id[3]);
        printf("ms4pdu[2] = %d \n",nas_id[4]);
       

Matthieu Kanj's avatar
Matthieu Kanj committed
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
                if(proc->flag_scrambling ==0)
                {

                      dlsch_encoding_rar_NB_IoT(tab_rar,
                                                rar,
                                                8,                      ///// number_of_subframes_required
                                                236,
                                                2);                   //////////// G*2   // option =2 for msg4


                       dlsch_sib_scrambling_rar_NB_IoT(fp,
                                                       rar,
                                                       236,
                                                       frame,
                                                       subframe*2,
                                                       65522);        //RA_template[0].RA_rnti
                }
                proc->flag_scrambling =1;
                printf("\n RAR sentttttt frame %d, subframe %d", frame, subframe);
                dlsch_modulation_rar_NB_IoT(txdataF,
                                            AMP,
                                            fp,
                                            3,                          // control region size for LTE , values between 0..3, (0 for stand-alone / 1, 2 or 3 for in-band)
                                            rar,
                                            236,                       // number of bits per subframe
                                            frame,  // unrequired
                                            subframe,       
                                            22);

                 proc->counter_msg4--;
                 proc->subframe_msg4 =subframe+1;

        }         
    }

}

Matthieu Kanj's avatar
Matthieu Kanj committed
1106
////////////////////////////////////////////////////////////////////////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1107 1108 1109 1110 1111 1112 1113 1114 1115
if(proc->flag_DCI_msg4 ==1 && proc->counter_DCI_msg4>0)
{ 
     if(proc->guard>0)
     {
        proc->guard--;
     }

     NB_IoT_eNB_NPDCCH_temp_t   *npdcch_struct_x = &eNB->npdcch_tmp;

Matthieu Kanj's avatar
Matthieu Kanj committed
1116
    if(proc->SP2 == 1)
Matthieu Kanj's avatar
Matthieu Kanj committed
1117 1118
     {
          if(proc->subframe_DCI_msg4==subframe && frame == proc->frame_DCI_msg4)
Matthieu Kanj's avatar
Matthieu Kanj committed
1119 1120 1121 1122 1123
          { 
              if(subframe==9)
              { 
                  proc->subframe_DCI_msg4 =1;
                  proc->frame_DCI_msg4=frame+1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1124
              } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
                  printf("\n xxxxxxxxxxxxxxxx DCI 2 for msg4 xxxxxxxxxxxxx frame %d, subframe %d", frame, subframe);
                  dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct_x->npdcch_e[0],1,2,22,subframe);
                  proc->counter_DCI_msg4--;
                  if(subframe==9)                             /// tester le cas ou subframe==4 && 5
                  {
                       proc->subframe_DCI_msg4 =1;
                       proc->frame_DCI_msg4=frame+1;
                  } else {
                      proc->subframe_DCI_msg4 =subframe+1;
                  } 
Matthieu Kanj's avatar
Matthieu Kanj committed
1135

Matthieu Kanj's avatar
Matthieu Kanj committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
                  if(proc->counter_DCI_msg4 == 0)
                  {
                       proc->flag_msg4=1;
                       proc->counter_msg4=4;
                      if(subframe+5>9)                 
                      {
                         proc->subframe_msg4 = (subframe+5)-10;
                         proc->frame_msg4= frame+1;

                      } else {
                            proc->subframe_msg4= subframe+5;
                            proc->frame_msg4= frame;
                      }
                  }
            }
        }
Matthieu Kanj's avatar
Matthieu Kanj committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
     }

     if(((10*frame +subframe) % 8)==2 && subframe != 0 && subframe != 5 && proc->SP2 !=1 && proc->guard==0)
     {
          printf("\n xxxxxxxxxxxxxxxx DCI 1 for msg4 xxxxxxxxxxxxx frame %d, subframe %d", frame, subframe);
          proc->SP2 =1;
          proc->flag_scrambling =0;   ///   for msg4 PDU
          
             int32_t numbits = 23;
              uint8_t       RCC;
              uint16_t npdcch_D  = 16 + 23;
             uint16_t  npdcch_E  = 236;
             uint8_t    agr_level = 2;
           //  uint8_t    dci_number=1; 
             uint8_t   tab_a[3];
             tab_a[0]= 128;
             tab_a[1]= 66;
             tab_a[2]= 4;
            // int           RB_ID_IoT=22 ; 
                 
              bzero(npdcch_struct_x->npdcch_e[0],npdcch_E);            
              memset(npdcch_struct_x->npdcch_d[0],LTE_NULL_NB_IoT,96);  

             ccode_encode_NB_IoT(numbits,2,tab_a,npdcch_struct_x->npdcch_d[0]+96,65522);
             RCC = sub_block_interleaving_cc_NB_IoT(npdcch_D,npdcch_struct_x->npdcch_d[0]+96,npdcch_struct_x->npdcch_w[0]);
             lte_rate_matching_cc_NB_IoT(RCC,npdcch_E,npdcch_struct_x->npdcch_w[0],npdcch_struct_x->npdcch_e[0]);

             npdcch_scrambling_NB_IoT(fp,npdcch_struct_x->npdcch_e[0],npdcch_E,(uint8_t)subframe*2,1,agr_level);
            
             dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct_x->npdcch_e[0],1,2,22,subframe);

             proc->counter_DCI_msg4--;
             ////////////////////////////////////////////if (subframe ==9 )
             proc->subframe_DCI_msg4 = subframe+1;

             proc->frame_DCI_msg4 = frame;
          

     } 

}
Matthieu Kanj's avatar
Matthieu Kanj committed
1193 1194

///////////////////////////////////////////////////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1195
  common_signal_procedures_NB_IoT(eNB,proc);
1196

Matthieu Kanj's avatar
Matthieu Kanj committed
1197
/////////////////// NB-IoT broadcast channel //////////////////////////
1198
//RA_template[i].RA_active=TRUE;
Matthieu Kanj's avatar
Matthieu Kanj committed
1199

Matthieu Kanj's avatar
Matthieu Kanj committed
1200

Matthieu Kanj's avatar
Matthieu Kanj committed
1201 1202 1203
 ///if( frame%2 ==0 && subframe ==1)

 if(subframe ==7)
Matthieu Kanj's avatar
Matthieu Kanj committed
1204 1205 1206 1207 1208 1209 1210 1211
 {
    if( RA_template[0].generate_rar == 1 && proc->sheduling_info_rar != 1)
    {
        proc->ra_rnti_computed = RA_template[0].RA_rnti;
        proc->dci_to_transmit =1;
        proc->remaining_dci =4;
        proc->sheduling_info_rar =1;
        proc->rar_to_transmit =0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1212
        proc->SP =0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1213
        proc->SP2 =0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1214 1215 1216 1217
        proc->flag_msg3=0;
        proc->frame_msg3=0;
        proc->counter_msg3=0;
        proc->flag_scrambling =0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1218 1219 1220 1221

        proc->subframe_real=-1;
        proc->subframe_delay=-1;

Matthieu Kanj's avatar
Matthieu Kanj committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
        proc->flag_msg4 =0;
        proc->subframe_SP2 =0;
        proc->subframe_SP =0;
        proc->flag_DCI_msg4=0;
        proc->flag_msg4=0;
        proc->counter_DCI_msg4=0;
        proc->counter_msg4=0;
        proc->frame_DCI_msg4=0;
        proc->frame_msg4=0;
        proc->guard=5;
        proc->subframe_DCI_msg4=0;
        proc->subframe_msg4=0;

Matthieu Kanj's avatar
Matthieu Kanj committed
1235 1236 1237
        //printf("detection ok in TX !!!!!!!!!!!!!!!!!");
        //RA_template[0].generate_rar = 0;
        //RA_template[0].RA_active = FALSE;
Matthieu Kanj's avatar
Matthieu Kanj committed
1238
        eNB->npdcch_tmp.pdu[0][0]=128; //90
Matthieu Kanj's avatar
Matthieu Kanj committed
1239 1240 1241 1242
        eNB->npdcch_tmp.pdu[0][1]=66; //42
        eNB->npdcch_tmp.pdu[0][2]=4; //4
    }
  }
1243
 
Matthieu Kanj's avatar
Matthieu Kanj committed
1244 1245 1246 1247 1248 1249 1250
  //if(frame%4==0 && proc->dci_to_transmit ==1 && proc->remaining_dci >0)
  if(proc->dci_to_transmit ==1 && proc->remaining_dci >0)
  { 

    if(((10*frame +subframe) % 8)==2 && subframe != 0 && subframe != 4 && subframe != 6 && proc->SP !=1)
    {     
       printf("\n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxframe %d, subframe %dxxxxxxxxxxxxxxxxxxxxxxxxxxxx", frame, subframe);
Matthieu Kanj's avatar
Matthieu Kanj committed
1251
          proc->SP =1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
          NB_IoT_eNB_NPDCCH_temp_t   *npdcch_struct = &eNB->npdcch_tmp;
          

          if(subframe ==2)
          {  proc->subframe_SP =0;
              printf("\n DCI 2 sentttttt frame %d, subframe %d", frame, subframe);
              printf("\n xxxxxxxxx RA-RNTI = %d xxxxxxxxxxxxxxx", RA_template[0].RA_rnti);
              int32_t numbits = 23;
              uint8_t       RCC;
              uint16_t npdcch_D  = 16 + 23;
             uint16_t  npdcch_E  = 236;
             uint8_t    agr_level = 2;
Matthieu Kanj's avatar
Matthieu Kanj committed
1264
           //  uint8_t    dci_number=1; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1265 1266 1267 1268
             uint8_t   tab_a[3];
             tab_a[0]= 128;
             tab_a[1]= 66;
             tab_a[2]= 4;
Matthieu Kanj's avatar
Matthieu Kanj committed
1269
            // int           RB_ID_IoT=22 ; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1270 1271 1272 1273
                 
              bzero(npdcch_struct->npdcch_e[0],npdcch_E);            
              memset(npdcch_struct->npdcch_d[0],LTE_NULL_NB_IoT,96);  

Matthieu Kanj's avatar
Matthieu Kanj committed
1274
             ccode_encode_NB_IoT(numbits,2,tab_a,npdcch_struct->npdcch_d[0]+96,RA_template[0].RA_rnti);
Matthieu Kanj's avatar
Matthieu Kanj committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
             RCC = sub_block_interleaving_cc_NB_IoT(npdcch_D,npdcch_struct->npdcch_d[0]+96,npdcch_struct->npdcch_w[0]);
             lte_rate_matching_cc_NB_IoT(RCC,npdcch_E,npdcch_struct->npdcch_w[0],npdcch_struct->npdcch_e[0]);

             npdcch_scrambling_NB_IoT(fp,npdcch_struct->npdcch_e[0],npdcch_E,(uint8_t)subframe*2,1,agr_level);
            
             dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);

             proc->remaining_dci--;

          } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1285
                     proc->subframe_SP =1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
                     if( ((frame)%2==1) && ((frame)%64<16))
                     {
                         proc->there_is_sib23 =8;
                     } else {

                        printf("\n DCI 8 sentttttt frame %d, subframe %d", frame, subframe);
                        printf("\n xxxxxxxxx RA-RNTI = %d xxxxxxxxxxxxxxx", RA_template[0].RA_rnti);
                         int32_t numbits = 23;
                        uint8_t       RCC;
                         uint16_t npdcch_D  = 16 + 23;
                        uint16_t  npdcch_E  = 236;
                        uint8_t    agr_level = 2;
Matthieu Kanj's avatar
Matthieu Kanj committed
1298
                        //uint8_t    dci_number=1; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1299 1300 1301 1302
                        uint8_t   tab_a[3];
                        tab_a[0]= 128;
                        tab_a[1]= 66;
                        tab_a[2]= 4;
Matthieu Kanj's avatar
Matthieu Kanj committed
1303
                       // int           RB_ID_IoT=22 ; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1304 1305 1306 1307
                 
                        bzero(npdcch_struct->npdcch_e[0],npdcch_E);            
                        memset(npdcch_struct->npdcch_d[0],LTE_NULL_NB_IoT,96);  

Matthieu Kanj's avatar
Matthieu Kanj committed
1308
                        ccode_encode_NB_IoT(numbits,2,tab_a,npdcch_struct->npdcch_d[0]+96,RA_template[0].RA_rnti);
Matthieu Kanj's avatar
Matthieu Kanj committed
1309 1310 1311 1312 1313 1314 1315 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 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
                        RCC = sub_block_interleaving_cc_NB_IoT(npdcch_D,npdcch_struct->npdcch_d[0]+96,npdcch_struct->npdcch_w[0]);
                        lte_rate_matching_cc_NB_IoT(RCC,npdcch_E,npdcch_struct->npdcch_w[0],npdcch_struct->npdcch_e[0]);

                        npdcch_scrambling_NB_IoT(fp,npdcch_struct->npdcch_e[0],npdcch_E,(uint8_t)subframe*2,1,agr_level);
            
                        dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);

                        proc->remaining_dci--;

                     }

          }

    }


    if(proc->subframe_SP ==0 && proc->SP ==1)
    {
      NB_IoT_eNB_NPDCCH_temp_t   *npdcch_struct = &eNB->npdcch_tmp;
      if(subframe ==3)
       {
         printf("\n DCI 3 sentttttt frame %d, subframe %d", frame, subframe);
         dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
         proc->remaining_dci--;
       }
       if((subframe == 4)  && (frame%2==0) && (frame%32>=16) )
       {
           printf("\n DCI 4 sentttttt frame %d, subframe %d", frame, subframe);
           dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
           proc->remaining_dci--;
       } 
       if(subframe ==6)
       {
           printf("\n DCI 6 sentttttt  frame %d, subframe %d", frame, subframe);
           dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
           proc->remaining_dci--;
           if(proc->remaining_dci == 0)
           {
               proc->dci_to_transmit =0;
               proc->rar_to_transmit =1;
               proc->remaining_rar =4;
               printf("xxxxx ERROR %d XXXXXX",proc->remaining_dci);

              //  proc->next_frame_tx=frame+2;
              //   proc->next_subframe_tx=1;
                                 if(((frame+1)%2==1) && ((frame+1)%64<16))
              { 
            
            proc->next_frame_tx=frame+2;
                                        proc->next_subframe_tx=1;
          }else {
           proc->next_frame_tx=frame+1;
                        proc->next_subframe_tx=2;
          }

           }
       }
       if(subframe ==7)
       {
           printf("\n DCI 7 sentttttt frame %d, subframe %d", frame, subframe);
           dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
           proc->remaining_dci--;
           if(proc->remaining_dci == 0)
           {
              proc->dci_to_transmit =0;
              proc->rar_to_transmit =1;
              proc->remaining_rar =4;

Matthieu Kanj's avatar
Matthieu Kanj committed
1377 1378 1379
              //      proc->next_frame_tx=frame+2;
              //     proc->next_subframe_tx=1;
              if(((frame+1)%2==1) && ((frame+1)%64<16))
Matthieu Kanj's avatar
Matthieu Kanj committed
1380 1381
              { 
            
Matthieu Kanj's avatar
Matthieu Kanj committed
1382 1383 1384 1385
                  proc->next_frame_tx=frame+2;
                  proc->next_subframe_tx=1;
              }else {
                    proc->next_frame_tx=frame+1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1386
                        proc->next_subframe_tx=3;
Matthieu Kanj's avatar
Matthieu Kanj committed
1387
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406

           }
       } 
    }


    if(proc->subframe_SP ==1 && proc->SP ==1)
    {
          if(proc->there_is_sib23==8)
          {             
                  NB_IoT_eNB_NPDCCH_temp_t   *npdcch_struct = &eNB->npdcch_tmp;
                  if(subframe ==1)
                   {     printf("\n DCI 1 sentttttt frame %d, subframe %d", frame, subframe);
                        printf("\n xxxxxxxxx RA-RNTI = %d xxxxxxxxxxxxxxx", RA_template[0].RA_rnti);
                         int32_t numbits = 23;
                        uint8_t       RCC;
                         uint16_t npdcch_D  = 16 + 23;
                        uint16_t  npdcch_E  = 236;
                        uint8_t    agr_level = 2;
Matthieu Kanj's avatar
Matthieu Kanj committed
1407
                       // uint8_t    dci_number=1; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1408 1409 1410 1411
                        uint8_t   tab_a[3];
                        tab_a[0]= 128;
                        tab_a[1]= 66;
                        tab_a[2]= 4;
Matthieu Kanj's avatar
Matthieu Kanj committed
1412
                       // int           RB_ID_IoT=22 ; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1413 1414 1415 1416
                 
                        bzero(npdcch_struct->npdcch_e[0],npdcch_E);            
                        memset(npdcch_struct->npdcch_d[0],LTE_NULL_NB_IoT,96);  

Matthieu Kanj's avatar
Matthieu Kanj committed
1417
                        ccode_encode_NB_IoT(numbits,2,tab_a,npdcch_struct->npdcch_d[0]+96,RA_template[0].RA_rnti);
Matthieu Kanj's avatar
Matthieu Kanj committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
                        RCC = sub_block_interleaving_cc_NB_IoT(npdcch_D,npdcch_struct->npdcch_d[0]+96,npdcch_struct->npdcch_w[0]);
                        lte_rate_matching_cc_NB_IoT(RCC,npdcch_E,npdcch_struct->npdcch_w[0],npdcch_struct->npdcch_e[0]);

                        npdcch_scrambling_NB_IoT(fp,npdcch_struct->npdcch_e[0],npdcch_E,(uint8_t)subframe*2,1,agr_level);
            
                        dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);

                        proc->remaining_dci--;
                      }
                      if(subframe ==2)
                      {
                        printf("\n DCI 2 sentttttt frame %d, subframe %d", frame, subframe);
                        dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                        proc->remaining_dci--;
                      }
                      if(subframe ==3)
                      {
                        printf("\n DCI 3 sentttttt frame %d, subframe %d", frame, subframe);
                        dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                        proc->remaining_dci--;
                      }
                      if((subframe == 4)  && (frame%2==0) && (frame%32>=16) )
                      {
                           printf("\n DCI 4 sentttttt frame %d, subframe %d", frame, subframe);
                           dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                           proc->remaining_dci--;
                           if(proc->remaining_dci == 0)
                            {
                               proc->dci_to_transmit =0;
                               proc->rar_to_transmit =1;
                                proc->remaining_rar =4;
                               printf("xxxxx ERROR %d XXXXXX",proc->remaining_dci);
                               
                                     //proc->next_frame_tx=frame+1;
                                    // proc->next_subframe_tx=8;
              if(((frame+1)%2==1) && ((frame+1)%64<16))
                                {
                                  proc->next_frame_tx=frame+2;
                                     proc->next_subframe_tx=1;
                                 } else {
                                    
             proc->next_frame_tx=frame+1;
                                     proc->next_subframe_tx=1;
                                }
                                
                           }
                       } 
                     if((subframe == 6))
                      {
                           printf("\n DCI 6 sentttttt frame %d, subframe %d", frame, subframe);
                           dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                           proc->remaining_dci--;
                           if(proc->remaining_dci == 0)
                            {
                               proc->dci_to_transmit =0;
                               proc->rar_to_transmit =1;
                                proc->remaining_rar =4;
                               printf("xxxxx ERROR %d XXXXXX",proc->remaining_dci);
                                 
                                  // proc->next_frame_tx=frame+2;
                                   // proc->next_subframe_tx=1;
        if(((frame+1)%2==1) && ((frame+1)%64<16))
                                {
                                  proc->next_frame_tx=frame+2;
                                     proc->next_subframe_tx=1;
                                 } else {
                                    
             proc->next_frame_tx=frame+1;
                                     proc->next_subframe_tx=2;
                                }
                                
                          }
                     } 

      } else {

              NB_IoT_eNB_NPDCCH_temp_t   *npdcch_struct = &eNB->npdcch_tmp;
              if(subframe ==9)
              {
                 printf("\n DCI 9 sentttttt frame %d, subframe %d", frame, subframe);
                 dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                proc->remaining_dci--;
              }
              if(subframe == 1 )
             {
                   printf("\n DCI 1 sentttttt frame %d, subframe %d", frame, subframe);
                   dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                   proc->remaining_dci--;
             } 
             if(subframe ==2)
             {
                printf("\n DCI 2 sentttttt  frame %d, subframe %d", frame, subframe);
                dci_modulation_NB_IoT(txdataF,AMP,fp,3,npdcch_struct->npdcch_e[0],1,2,22,subframe);
                 proc->remaining_dci--;
                if(proc->remaining_dci == 0)
                {
                    proc->dci_to_transmit =0;
                    proc->rar_to_transmit =1;
                    proc->remaining_rar =4;
                    printf("xxxxx ERROR %d XXXXXX",proc->remaining_dci);

              
                      proc->next_frame_tx=frame;
                      proc->next_subframe_tx=7;


               }
Matthieu Kanj's avatar
Matthieu Kanj committed
1525
            }
Matthieu Kanj's avatar
Matthieu Kanj committed
1526 1527 1528 1529 1530

         }

    }

Matthieu Kanj's avatar
Matthieu Kanj committed
1531
}
Matthieu Kanj's avatar
Matthieu Kanj committed
1532

Matthieu Kanj's avatar
Matthieu Kanj committed
1533

Matthieu Kanj's avatar
Matthieu Kanj committed
1534
if(subframe !=5 && subframe !=0)
Matthieu Kanj's avatar
Matthieu Kanj committed
1535 1536
  {
 if(proc->rar_to_transmit ==1 && proc->remaining_rar >0)
1537
 {
Matthieu Kanj's avatar
Matthieu Kanj committed
1538 1539
    NB_IoT_DL_eNB_RAR_t  *rar          = &eNB->ndlsch_rar.content_rar;
    uint8_t   tab_rar[7];
Matthieu Kanj's avatar
Matthieu Kanj committed
1540
    // printf("xxxxx index verif %d XXXXXX",RA_template[0].preamble_index);
Matthieu Kanj's avatar
Matthieu Kanj committed
1541
      tab_rar[0]=64 + RA_template[0].preamble_index;
Matthieu Kanj's avatar
Matthieu Kanj committed
1542
      tab_rar[1]=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1543
      tab_rar[2]=9;
Matthieu Kanj's avatar
Matthieu Kanj committed
1544
      tab_rar[3]=96;
Matthieu Kanj's avatar
Matthieu Kanj committed
1545
      tab_rar[4]=64;
Matthieu Kanj's avatar
Matthieu Kanj committed
1546 1547
      tab_rar[5]=255; // 16
      tab_rar[6]=242;     // 5*/
Matthieu Kanj's avatar
Matthieu Kanj committed
1548 1549

       
Matthieu Kanj's avatar
Matthieu Kanj committed
1550
           if(frame == proc->next_frame_tx && subframe == proc->next_subframe_tx)
Matthieu Kanj's avatar
Matthieu Kanj committed
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
            {
                  
                  if(subframe == 4)
                  {
                     uint8_t sib1_exist =0;

                        if( ((frame)%2==0) && ((frame)%32<16))
                        {
                          sib1_exist =1;
                        }

                        if(sib1_exist == 1)
                        {
                            
                             proc->next_subframe_tx =subframe+2;
                          } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        
                             if(proc->flag_scrambling ==0)
                             {

                                dlsch_encoding_rar_NB_IoT(tab_rar,
                                        rar,
                                       8,                      ///// number_of_subframes_required
                                        236,
                                       1);                   //////////// G*2
        
        
                                   dlsch_sib_scrambling_rar_NB_IoT(fp,
                                         rar,
                                         236,
                                         frame,
                                         subframe*2,
                                         RA_template[0].RA_rnti);
                            }
                            proc->flag_scrambling =1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
                            printf("\n RAR sentttttt frame %d, subframe %d", frame, subframe);
                            dlsch_modulation_rar_NB_IoT(txdataF,
                                          AMP,
                                         fp,
                                         3,                          // control region size for LTE , values between 0..3, (0 for stand-alone / 1, 2 or 3 for in-band)
                                         rar,
                                         236,                       // number of bits per subframe
                                        frame,  // unrequired
                                        subframe,       
                                        22);

                             proc->remaining_rar--;
                             proc->next_subframe_tx =subframe+2;
Matthieu Kanj's avatar
Matthieu Kanj committed
1599 1600 1601 1602 1603
                             if(proc->remaining_rar == 0)
                             {
                                proc->flag_msg3 =1;
                                proc->frame_msg3=frame+1;
                                proc->counter_msg3 = 8;
Matthieu Kanj's avatar
Matthieu Kanj committed
1604 1605
                                proc->subframe_real=9;
                                proc->subframe_delay=5;
Matthieu Kanj's avatar
Matthieu Kanj committed
1606
                             }
Matthieu Kanj's avatar
Matthieu Kanj committed
1607 1608 1609
                          }
                  
                  } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
                            if( ((frame)%2==0) && (subframe ==9))
                            {
                               proc->next_subframe_tx =1;
                                proc->next_frame_tx = proc->next_frame_tx+1;
                            } else {

                                 if(proc->flag_scrambling ==0)
                                  {

                                      dlsch_encoding_rar_NB_IoT(tab_rar,
                                                           rar,
                                                             8,                      ///// number_of_subframes_required
                                                          236,
                                                          1);                   //////////// G*2
Matthieu Kanj's avatar
Matthieu Kanj committed
1624 1625
        
        
Matthieu Kanj's avatar
Matthieu Kanj committed
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
                                        dlsch_sib_scrambling_rar_NB_IoT(fp,
                                                              rar,
                                                                236,
                                                              frame,
                                                               subframe*2,
                                                            RA_template[0].RA_rnti);
                               }
                               proc->flag_scrambling =1;

                              printf("\n RAR sentttttt frame %d, subframe %d", frame, subframe);
                              dlsch_modulation_rar_NB_IoT(txdataF,
Matthieu Kanj's avatar
Matthieu Kanj committed
1637 1638 1639 1640 1641 1642 1643 1644 1645
                                      AMP,
                                      fp,
                                      3,                          // control region size for LTE , values between 0..3, (0 for stand-alone / 1, 2 or 3 for in-band)
                                      rar,
                                      236,                       // number of bits per subframe
                                     frame,  // unrequired
                                     subframe,       
                                     22);

Matthieu Kanj's avatar
Matthieu Kanj committed
1646 1647
                              proc->remaining_rar--;
                              proc->next_subframe_tx =subframe+1;
Matthieu Kanj's avatar
Matthieu Kanj committed
1648 1649 1650 1651 1652
                              if(proc->remaining_rar == 0)
                             {
                                proc->flag_msg3 =1;
                                proc->frame_msg3=frame+1;
                                proc->counter_msg3 = 8;
Matthieu Kanj's avatar
Matthieu Kanj committed
1653 1654
                                proc->subframe_delay=5;
                                proc->subframe_real=9;
Matthieu Kanj's avatar
Matthieu Kanj committed
1655
                             }
Matthieu Kanj's avatar
Matthieu Kanj committed
1656
                        }
Matthieu Kanj's avatar
Matthieu Kanj committed
1657 1658 1659 1660 1661 1662 1663

                  }


              }

        
Matthieu Kanj's avatar
Matthieu Kanj committed
1664
  }
1665
 }
Matthieu Kanj's avatar
Matthieu Kanj committed
1666

Matthieu Kanj's avatar
Matthieu Kanj committed
1667

Matthieu Kanj's avatar
Matthieu Kanj committed
1668
/////////////////////////// END ///////////////////////////////////////  
1669

1670
/*
1671
  // generate Cell-Specific Reference Signals for both slots
1672
  if (eNB->abstraction_flag==0) {
1673 1674
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,1);
    generate_pilots_slot(eNB,
1675 1676 1677
       txdataF,
       AMP,
       subframe<<1,0);
1678 1679 1680
    // check that 2nd slot is for DL
    if (subframe_select(fp,subframe) == SF_DL)
      generate_pilots_slot(eNB,
1681 1682 1683
         txdataF,
         AMP,
         (subframe<<1)+1,0);
1684 1685 1686
    
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_RS_TX,0);
  }    
1687
*/
1688 1689
//////////////////////////// for NB-IoT testing /////////////////////////////
/*
1690
  // First half of PSS/SSS (FDD, slot 0)
Raymond Knopp's avatar
 
Raymond Knopp committed
1691
  if (subframe == 0) {
1692
    if ((fp->frame_type == FDD) &&
1693
  (eNB->abstraction_flag==0)) {
1694
      generate_pss(txdataF,
1695 1696 1697 1698
       AMP,
       fp,
       (fp->Ncp==NORMAL) ? 6 : 5,
       0);
1699
      generate_sss(txdataF,
1700 1701 1702 1703
       AMP,
       fp,
       (fp->Ncp==NORMAL) ? 5 : 4,
       0);
1704
      
1705
    }
1706 1707 1708 1709 1710
  */
  ///////////////////////////////////////////////////////////////////////////////////// 

  //////////////////////////// for NB-IoT testing /////////////////////////////
  /*
1711
    // generate PBCH (Physical Broadcast CHannel) info
1712
    if ((frame&3) == 0) {
1713
      pbch_pdu[2] = 0;
1714
      
1715
      // FIXME setting pbch_pdu[2] to zero makes the switch statement easier: remove all the or-operators
1716
      switch (fp->N_RB_DL) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1717
      case 6:
1718 1719 1720
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (0<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1721
      case 15:
1722 1723 1724
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (1<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1725
      case 25:
1726 1727 1728
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (2<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1729
      case 50:
1730 1731 1732
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (3<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1733
      case 75:
1734 1735 1736
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (4<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1737
      case 100:
1738 1739 1740
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (5<<5);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1741
      default:
1742 1743 1744
  // FIXME if we get here, this should be flagged as an error, right?
  pbch_pdu[2] = (pbch_pdu[2]&0x1f) | (2<<5);
  break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1745
      }
1746
      
1747
      pbch_pdu[2] = (pbch_pdu[2]&0xef) |
1748
  ((fp->phich_config_common.phich_duration << 4)&0x10);
1749 1750
      
      switch (fp->phich_config_common.phich_resource) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1751
      case oneSixth:
1752 1753 1754
  pbch_pdu[2] = (pbch_pdu[2]&0xf3) | (0<<2);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1755
      case half:
1756 1757 1758
  pbch_pdu[2] = (pbch_pdu[2]&0xf3) | (1<<2);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1759
      case one:
1760 1761 1762
  pbch_pdu[2] = (pbch_pdu[2]&0xf3) | (2<<2);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1763
      case two:
1764 1765 1766
  pbch_pdu[2] = (pbch_pdu[2]&0xf3) | (3<<2);
  break;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1767
      default:
1768 1769
  // unreachable
  break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1770
      }
1771
      
1772 1773
      pbch_pdu[2] = (pbch_pdu[2]&0xfc) | ((frame>>8)&0x3);
      pbch_pdu[1] = frame&0xfc;
1774
      pbch_pdu[0] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1775
    }
1776 1777 1778 1779
     */
  ////////////////////////////////////////////////////////////////END////////////////////////////////////////////////    
   /*
  //////////////////////////// for NB-IoT testing /////////////////////////////
1780 1781 1782
    /// First half of SSS (TDD, slot 1)
    
    if ((fp->frame_type == TDD)&&
1783
  (eNB->abstraction_flag==0)){
1784
      generate_sss(txdataF,
1785 1786 1787 1788
       AMP,
       fp,
       (fp->Ncp==NORMAL) ? 6 : 5,
       1);
1789
    }
1790

1791
    /// generate PBCH
1792
    if (eNB->abstraction_flag==0) {
1793
      generate_pbch(&eNB->pbch,
1794 1795 1796 1797
                    txdataF,
                    AMP,
                    fp,
                    pbch_pdu,
1798
                    frame&3);
1799
    }
1800 1801 1802
#ifdef PHY_ABSTRACTION
    else {
      generate_pbch_emul(eNB,pbch_pdu);
1803
    }
1804
#endif
1805

1806
  }
1807
  else if ((subframe == 1) &&
1808 1809
     (fp->frame_type == TDD)&&
     (eNB->abstraction_flag==0)) {
1810
    generate_pss(txdataF,
1811 1812 1813 1814
     AMP,
     fp,
     2,
     2);
1815
  }
1816 1817 1818
  
  // Second half of PSS/SSS (FDD, slot 10)
  else if ((subframe == 5) && 
1819 1820
     (fp->frame_type == FDD) &&
     (eNB->abstraction_flag==0)) {
1821
    generate_pss(txdataF,
1822 1823 1824 1825
     AMP,
     &eNB->frame_parms,
     (fp->Ncp==NORMAL) ? 6 : 5,
     10);
1826
    generate_sss(txdataF,
1827 1828 1829 1830
     AMP,
     &eNB->frame_parms,
     (fp->Ncp==NORMAL) ? 5 : 4,
     10);
1831

1832
  }
1833

1834 1835
  //  Second-half of SSS (TDD, slot 11)
  else if ((subframe == 5) &&
1836 1837
     (fp->frame_type == TDD) &&
     (eNB->abstraction_flag==0)) {
1838
    generate_sss(txdataF,
1839 1840 1841 1842
     AMP,
     fp,
     (fp->Ncp==NORMAL) ? 6 : 5,
     11);
Raymond Knopp's avatar
 
Raymond Knopp committed
1843
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1844

1845 1846
  // Second half of PSS (TDD, slot 12)
  else if ((subframe == 6) &&
1847 1848
     (fp->frame_type == TDD) &&
     (eNB->abstraction_flag==0)) {
1849
    generate_pss(txdataF,
1850 1851 1852 1853
     AMP,
     fp,
     2,
     12);
1854
  }
1855 1856
  */
  /////////////////////////////////////////////////////////////////////////
1857
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1858

1859
void generate_eNB_dlsch_params(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,DCI_ALLOC_t *dci_alloc,const int UE_id) {
1860

1861
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
1862 1863
  int frame = proc->frame_tx;
  int subframe = proc->subframe_tx;
1864 1865 1866 1867 1868

  // if we have SI_RNTI, configure dlsch parameters and CCE index
  if (dci_alloc->rnti == SI_RNTI) {
    LOG_D(PHY,"Generating dlsch params for SI_RNTI\n");
    generate_eNB_dlsch_params_from_dci(frame,
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
               subframe,
               &dci_alloc->dci_pdu[0],
               dci_alloc->rnti,
               dci_alloc->format,
               &eNB->dlsch_SI,
               fp,
               NULL,
               SI_RNTI,
               0,
               P_RNTI,
               eNB->UE_stats[0].DL_pmi_single,
               0);
1881 1882
    
    
1883
    eNB->dlsch_SI->nCCE[subframe] = dci_alloc->firstCCE;
1884
    
1885
    LOG_T(PHY,"[eNB %"PRIu8"] Frame %d subframe %d : CCE resource for common DCI (SI)  => %"PRIu8"\n",eNB->Mod_id,frame,subframe,
1886
    eNB->dlsch_SI->nCCE[subframe]);
1887 1888 1889 1890
    
#if defined(SMBV) 
    
    // configure SI DCI
1891 1892
    if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
      LOG_D(PHY,"[SMBV] Frame %3d, SI in SF %d DCI %"PRIu32"\n",frame,subframe,i);
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
      smbv_configure_common_dci(smbv_fname,(smbv_frame_cnt*10) + (subframe), "SI", dci_alloc, i);
    }
    
#endif
    
    
  } else if (dci_alloc->ra_flag == 1) {  // This is format 1A allocation for RA
    // configure dlsch parameters and CCE index
    LOG_D(PHY,"Generating dlsch params for RA_RNTI\n");    
    generate_eNB_dlsch_params_from_dci(frame,
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
               subframe,
               &dci_alloc->dci_pdu[0],
               dci_alloc->rnti,
               dci_alloc->format,
               &eNB->dlsch_ra,
               fp,
               NULL,
               SI_RNTI,
               dci_alloc->rnti,
               P_RNTI,
               eNB->UE_stats[0].DL_pmi_single,
               0);
1915 1916
    
    
1917
    eNB->dlsch_ra->nCCE[subframe] = dci_alloc->firstCCE;
1918
    
1919
    LOG_D(PHY,"[eNB %"PRIu8"] Frame %d subframe %d : CCE resource for common DCI (RA)  => %"PRIu8"\n",eNB->Mod_id,frame,subframe,
1920
    eNB->dlsch_ra->nCCE[subframe]);
1921 1922 1923
#if defined(SMBV) 
    
    // configure RA DCI
1924 1925
    if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
      LOG_D(PHY,"[SMBV] Frame %3d, RA in SF %d DCI %"PRIu32"\n",frame,subframe,i);
1926
      smbv_configure_common_dci(smbv_fname,(smbv_frame_cnt*10) + (subframe), "RA", dci_alloc, i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1927
    }
1928 1929 1930 1931 1932 1933
    
#endif
    
  }
  
  else if ((dci_alloc->format != format0)&&
1934 1935 1936
     (dci_alloc->format != format3)&&
     (dci_alloc->format != format3A)&&
     (dci_alloc->format != format4)){ // this is a normal DLSCH allocation
1937
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1938

1939 1940 1941 1942
    
    if (UE_id>=0) {
#if defined(SMBV) 
      // Configure this user
1943
      if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
1944
  LOG_D(PHY,"[SMBV] Frame %3d, SF %d (SMBV SF %d) Configuring user %d with RNTI %"PRIu16" in TM%"PRIu8"\n",frame,subframe,(smbv_frame_cnt*10) + (subframe),UE_id+1,
1945
              dci_alloc->rnti,eNB->transmission_mode[(uint8_t)UE_id]);
1946
  smbv_configure_user(smbv_fname,UE_id+1,eNB->transmission_mode[(uint8_t)UE_id],dci_alloc->rnti);
Raymond Knopp's avatar
 
Raymond Knopp committed
1947
      }
1948 1949
      
#endif
1950

1951 1952
      LOG_D(PHY,"Generating dlsch params for RNTI %x\n",dci_alloc->rnti);
      //fill the dlsch structures
1953
      generate_eNB_dlsch_params_from_dci(frame,
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
           subframe,
           &dci_alloc->dci_pdu[0],
           dci_alloc->rnti,
           dci_alloc->format,
           eNB->dlsch[(uint8_t)UE_id],
           fp,
           &eNB->pdsch_config_dedicated[UE_id],
           SI_RNTI,
           0,
           P_RNTI,
           eNB->UE_stats[(uint8_t)UE_id].DL_pmi_single,
           eNB->transmission_mode[(uint8_t)UE_id]<7?0:eNB->transmission_mode[(uint8_t)UE_id]);
1966
      LOG_D(PHY,"[eNB %"PRIu8"][PDSCH %"PRIx16"/%"PRIu8"] Frame %d subframe %d: Generated dlsch params\n",
1967
      eNB->Mod_id,dci_alloc->rnti,eNB->dlsch[(uint8_t)UE_id][0]->current_harq_pid,frame,subframe);
1968 1969
      
      
Cedric Roux's avatar
Cedric Roux committed
1970 1971
      T(T_ENB_PHY_DLSCH_UE_DCI, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(UE_id),
        T_INT(dci_alloc->rnti), T_INT(dci_alloc->format),
Cedric Roux's avatar
Cedric Roux committed
1972
        T_INT(eNB->dlsch[(int)UE_id][0]->current_harq_pid),
1973 1974
        T_INT(eNB->dlsch[(int)UE_id][0]->harq_processes[eNB->dlsch[(int)UE_id][0]->current_harq_pid]->mcs),
        T_INT(eNB->dlsch[(int)UE_id][0]->harq_processes[eNB->dlsch[(int)UE_id][0]->current_harq_pid]->TBS));
1975

1976
      eNB->dlsch[(uint8_t)UE_id][0]->nCCE[subframe] = dci_alloc->firstCCE;
1977
      
Cedric Roux's avatar
Cedric Roux committed
1978
      LOG_D(PHY,"[eNB %"PRIu8"] Frame %d subframe %d : CCE resource for ue DCI (PDSCH %"PRIx16")  => %"PRIu8"\n",eNB->Mod_id,frame,subframe,
1979
      dci_alloc->rnti,eNB->dlsch[(uint8_t)UE_id][0]->nCCE[subframe]);
1980 1981 1982 1983
      
#if defined(SMBV) 
      
      // configure UE-spec DCI
1984
      if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
1985 1986
  LOG_D(PHY,"[SMBV] Frame %3d, PDSCH in SF %d DCI %"PRIu32"\n",frame,subframe,i);
  smbv_configure_ue_spec_dci(smbv_fname,(smbv_frame_cnt*10) + (subframe), UE_id+1,dci_alloc, i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1987
      }
1988
      
1989
#endif
1990 1991
      
      LOG_D(PHY,"[eNB %"PRIu8"][DCI][PDSCH %"PRIx16"] Frame %d subframe %d UE_id %"PRId8" Generated DCI format %d, aggregation %d\n",
1992 1993 1994 1995
      eNB->Mod_id, dci_alloc->rnti,
      frame, subframe,UE_id,
      dci_alloc->format,
      1<<dci_alloc->L);
Raymond Knopp's avatar
 
Raymond Knopp committed
1996
    } else {
1997
      LOG_D(PHY,"[eNB %"PRIu8"][PDSCH] Frame %d : No UE_id with corresponding rnti %"PRIx16", dropping DLSCH\n",
1998
      eNB->Mod_id,frame,dci_alloc->rnti);
1999
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2000
  }
2001 2002
  
}
2003

2004
void generate_eNB_ulsch_params(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,DCI_ALLOC_t *dci_alloc,const int UE_id) {
2005

2006
  int harq_pid;
2007
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
2008
  int frame = proc->frame_tx;
Raymond Knopp's avatar
Raymond Knopp committed
2009
  int subframe = proc->subframe_tx;
2010
  /*
2011 2012 2013
  uint16_t srsPeriodicity=0;
  uint16_t srsOffset=0;
  uint16_t srsConfigIndex=0;
2014
  uint16_t do_srs=0;
2015
  */
2016
  uint16_t is_srs_pos=0;
2017

2018
  LOG_D(PHY,
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
  "[eNB %"PRIu8"][PUSCH %"PRIu8"] Frame %d subframe %d UL Frame %"PRIu32", UL Subframe %"PRIu8", Generated ULSCH (format0) DCI (rnti %"PRIx16", dci %"PRIx8"), aggregation %d\n",
  eNB->Mod_id,
  subframe2harq_pid(fp,
        pdcch_alloc2ul_frame(fp,frame,subframe),
        pdcch_alloc2ul_subframe(fp,subframe)),
  frame,
  subframe,
  pdcch_alloc2ul_frame(fp,frame,subframe),
  pdcch_alloc2ul_subframe(fp,subframe),
  dci_alloc->rnti,
  dci_alloc->dci_pdu[0],
  1<<dci_alloc->L);
2031
  
2032
  is_srs_pos = is_srs_occasion_common(fp,pdcch_alloc2ul_frame(fp,frame,subframe),pdcch_alloc2ul_subframe(fp,subframe));
2033
  /*
2034 2035 2036 2037
  if (is_srs_pos && eNB->soundingrs_ul_config_dedicated[UE_id].srsConfigDedicatedSetup) {
    srsConfigIndex = eNB->soundingrs_ul_config_dedicated[UE_id].srs_ConfigIndex;
    compute_srs_pos(fp->frame_type, srsConfigIndex, &srsPeriodicity, &srsOffset);
    if ((((10*pdcch_alloc2ul_frame(fp,frame,subframe)+pdcch_alloc2ul_subframe(fp,subframe)) % srsPeriodicity) == srsOffset)) {
2038
      do_srs = 1;
2039
    }
2040
  }
2041
      LOG_D(PHY,"frame %d (%d), subframe %d (%d), UE_id %d: is_srs_pos %d, do_SRS %d, index %d, period %d, offset %d \n",
2042 2043
      frame,pdcch_alloc2ul_frame(fp,frame,subframe),subframe,pdcch_alloc2ul_subframe(fp,subframe),
      UE_id,is_srs_pos,do_srs,srsConfigIndex,srsPeriodicity,srsOffset);
2044
  */
2045

2046
  generate_eNB_ulsch_params_from_dci(eNB,
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
             proc,
             &dci_alloc->dci_pdu[0],
             dci_alloc->rnti,
             format0,
             UE_id,
             SI_RNTI,
             0,
             P_RNTI,
             CBA_RNTI,
             is_srs_pos);  
2057 2058
  
  LOG_T(PHY,"[eNB %"PRIu8"] Frame %d subframe %d : CCE resources for UE spec DCI (PUSCH %"PRIx16") => %d\n",
2059 2060
  eNB->Mod_id,frame,subframe,dci_alloc->rnti,
  dci_alloc->firstCCE);
2061 2062 2063 2064
  
#if defined(SMBV) 
  
  // configure UE-spec DCI for UL Grant
2065 2066
  if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
    LOG_D(PHY,"[SMBV] Frame %3d, SF %d UL DCI %"PRIu32"\n",frame,subframe,i);
2067
    smbv_configure_ue_spec_dci(smbv_fname,(smbv_frame_cnt*10) + (subframe), UE_id+1, &DCI_pdu->dci_alloc[i], i);
Raymond Knopp's avatar
 
Raymond Knopp committed
2068
  }
2069 2070 2071 2072 2073 2074
  
#endif
  
  
  // get the hard_pid for this subframe 
  harq_pid = subframe2harq_pid(fp,
2075 2076
             pdcch_alloc2ul_frame(fp,frame,subframe),
             pdcch_alloc2ul_subframe(fp,subframe));
2077 2078
  
  if (harq_pid==255) { // should not happen, log an error and exit, this is a fatal error
2079
    LOG_E(PHY,"[eNB %"PRIu8"] Frame %d: Bad harq_pid for ULSCH allocation\n",eNB->Mod_id,frame);
2080 2081 2082 2083
    mac_xface->macphy_exit("FATAL\n"); 
  }
  
  if ((dci_alloc->rnti  >= CBA_RNTI) && (dci_alloc->rnti < P_RNTI))
2084
    eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->subframe_cba_scheduling_flag = 1;
2085
  else
2086
    eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->subframe_scheduling_flag = 1;
2087
  
Cedric Roux's avatar
Cedric Roux committed
2088
  T(T_ENB_PHY_ULSCH_UE_DCI, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(UE_id),
Cedric Roux's avatar
Cedric Roux committed
2089 2090 2091 2092 2093
    T_INT(dci_alloc->rnti), T_INT(harq_pid),
    T_INT(eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->mcs),
    T_INT(eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->round),
    T_INT(eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->first_rb),
    T_INT(eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->nb_rb),
2094 2095 2096
    T_INT(eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->TBS),
    T_INT(dci_alloc->L),
    T_INT(dci_alloc->firstCCE));
2097
}
2098

2099
void pdsch_procedures(PHY_VARS_eNB *eNB,
2100 2101 2102 2103 2104 2105
            eNB_rxtx_proc_t *proc,
            LTE_eNB_DLSCH_t *dlsch,
            LTE_eNB_DLSCH_t *dlsch1,
            LTE_eNB_UE_stats *ue_stats,
            int ra_flag,
            int num_pdcch_symbols) {
2106

2107 2108
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
2109 2110 2111
  int harq_pid = dlsch->current_harq_pid;
  LTE_DL_eNB_HARQ_t *dlsch_harq=dlsch->harq_processes[harq_pid];
  int input_buffer_length = dlsch_harq->TBS/8;
2112
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
2113
  uint8_t *DLSCH_pdu=NULL;
2114
  uint8_t DLSCH_pdu_tmp[input_buffer_length+4]; //[768*8];
2115 2116
  uint8_t DLSCH_pdu_rar[256];
  int i;
2117

2118
  LOG_D(PHY,
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
  "[eNB %"PRIu8"][PDSCH %"PRIx16"/%"PRIu8"] Frame %d, subframe %d: Generating PDSCH/DLSCH with input size = %"PRIu16", G %d, nb_rb %"PRIu16", mcs %"PRIu8", pmi_alloc %"PRIx64", rv %"PRIu8" (round %"PRIu8")\n",
  eNB->Mod_id, dlsch->rnti,harq_pid,
  frame, subframe, input_buffer_length,
  get_G(fp,
        dlsch_harq->nb_rb,
        dlsch_harq->rb_alloc,
        get_Qm(dlsch_harq->mcs),
        dlsch_harq->Nl,
        num_pdcch_symbols,
        frame,
        subframe,
        dlsch_harq->mimo_mode==TM7?7:0),
  dlsch_harq->nb_rb,
  dlsch_harq->mcs,
  pmi2hex_2Ar1(dlsch_harq->pmi_alloc),
  dlsch_harq->rvidx,
  dlsch_harq->round);
2136

2137
#if defined(MESSAGE_CHART_GENERATOR_PHY)
2138
  MSC_LOG_TX_MESSAGE(
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
         MSC_PHY_ENB,MSC_PHY_UE,
         NULL,0,
         "%05u:%02u PDSCH/DLSCH input size = %"PRIu16", G %d, nb_rb %"PRIu16", mcs %"PRIu8", pmi_alloc %"PRIx16", rv %"PRIu8" (round %"PRIu8")",
         frame, subframe,
         input_buffer_length,
         get_G(fp,
         dlsch_harq->nb_rb,
         dlsch_harq->rb_alloc,
         get_Qm(dlsch_harq->mcs),
         dlsch_harq->Nl,
         num_pdcch_symbols,
         frame,
         subframe,
         dlsch_harq->mimo_mode==TM7?7:0),
         dlsch_harq->nb_rb,
         dlsch_harq->mcs,
         pmi2hex_2Ar1(dlsch_harq->pmi_alloc),
         dlsch_harq->rvidx,
         dlsch_harq->round);
2158 2159 2160 2161 2162 2163 2164 2165
#endif

  if (ue_stats) ue_stats->dlsch_sliding_cnt++;

  if (dlsch_harq->round == 0) {

    if (ue_stats)
      ue_stats->dlsch_trials[harq_pid][0]++;
2166

2167 2168
    if (eNB->mac_enabled==1) {
      if (ra_flag == 0) {
2169 2170 2171 2172 2173
  DLSCH_pdu = mac_xface->get_dlsch_sdu(eNB->Mod_id,
               eNB->CC_id,
               frame,
               dlsch->rnti,
               0);
Raymond Knopp's avatar
 
Raymond Knopp committed
2174
      }
2175
      else {
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
  int16_t crnti = mac_xface->fill_rar(eNB->Mod_id,
              eNB->CC_id,
              frame,
              DLSCH_pdu_rar,
              fp->N_RB_UL,
              input_buffer_length);
  DLSCH_pdu = DLSCH_pdu_rar;

  int UE_id;

  if (crnti!=0) 
    UE_id = add_ue(crnti,eNB);
  else 
    UE_id = -1;
      
  if (UE_id==-1) {
    LOG_W(PHY,"[eNB] Max user count reached.\n");
    mac_xface->cancel_ra_proc(eNB->Mod_id,
            eNB->CC_id,
            frame,
            crnti);
  } else {
    eNB->UE_stats[(uint32_t)UE_id].mode = RA_RESPONSE;
    // Initialize indicator for first SR (to be cleared after ConnectionSetup is acknowledged)
    eNB->first_sr[(uint32_t)UE_id] = 1;
        

    /*
     * In FAPI style we don-t need to process the RAR because we have all the parameters for getting the MSG3 given by the
     * UL_CONFIG.request
     * 1) this data are given at the same time with the DLSCH PDU containing the RAR
     * 2) wee need to do a mapping of this parameters OAI->FAPI
     */


    /*this for MSG3*/
    //the problem of OAI is that i don't have any DCI0 for scheduling the info for getting Msg3 (because is direclty given in RAR)
    //so i should gather the information directly form RAR before sending it
    //In FAPI style apporach so we directly get it from the next UL-config given in the Sched_rsp
    //so most probably i don't care about it because i will receive all the information for get Msg3
    generate_eNB_ulsch_params_from_rar(DLSCH_pdu,
               frame,
               subframe,
               eNB->ulsch[(uint32_t)UE_id],
               fp);
        
    LOG_D(PHY,"[eNB][RAPROC] Frame %d subframe %d, Activated Msg3 demodulation for UE %"PRId8" in frame %"PRIu32", subframe %"PRIu8"\n",
    frame,
    subframe,
    UE_id,
    eNB->ulsch[(uint32_t)UE_id]->Msg3_frame,
    eNB->ulsch[(uint32_t)UE_id]->Msg3_subframe);
Cedric Roux's avatar
Cedric Roux committed
2228

2229 2230 2231 2232 2233 2234 2235 2236 2237
          /* TODO: get rid of this hack. The problem is that the eNodeB may
           * sometimes wrongly generate PHICH because somewhere 'phich_active' was
           * not reset to 0, due to an unidentified reason. When adding this
           * resetting here the problem seems to disappear completely.
           */
          LOG_D(PHY, "hack: set phich_active to 0 for UE %d fsf %d %d all HARQs\n", UE_id, frame, subframe);
          for (i = 0; i < 8; i++)
            eNB->ulsch[(uint32_t)UE_id]->harq_processes[i]->phich_active = 0;

2238 2239 2240
          mac_xface->set_msg3_subframe(eNB->Mod_id, eNB->CC_id, frame, subframe, (uint16_t)crnti,
                                       eNB->ulsch[UE_id]->Msg3_frame, eNB->ulsch[UE_id]->Msg3_subframe);

Cedric Roux's avatar
Cedric Roux committed
2241 2242 2243
          T(T_ENB_PHY_MSG3_ALLOCATION, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe),
            T_INT(UE_id), T_INT((uint16_t)crnti), T_INT(1 /* 1 is for initial transmission*/),
            T_INT(eNB->ulsch[UE_id]->Msg3_frame), T_INT(eNB->ulsch[UE_id]->Msg3_subframe));
2244 2245
  }
  if (ue_stats) ue_stats->total_TBS_MAC += dlsch_harq->TBS;
2246 2247 2248 2249
      }
    }
    else {
      DLSCH_pdu = DLSCH_pdu_tmp;
2250
    
2251
      for (i=0; i<input_buffer_length; i++)
2252
  DLSCH_pdu[i] = (unsigned char)(taus()&0xff);
2253
    }
2254
  
2255
#if defined(SMBV) 
2256

2257 2258 2259 2260 2261
    // Configures the data source of allocation (allocation is configured by DCI)
    if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
      LOG_D(PHY,"[SMBV] Frame %3d, Configuring PDSCH payload in SF %d alloc %"PRIu8"\n",frame,(smbv_frame_cnt*10) + (subframe),smbv_alloc_cnt);
      //          smbv_configure_datalist_for_user(smbv_fname, find_ue(dlsch->rnti,eNB)+1, DLSCH_pdu, input_buffer_length);
    }
2262

2263
#endif
2264 2265 2266



2267 2268
#ifdef DEBUG_PHY_PROC
#ifdef DEBUG_DLSCH
2269
    LOG_T(PHY,"eNB DLSCH SDU: \n");
2270

Cedric Roux's avatar
Cedric Roux committed
2271
    //eNB->dlsch[(uint8_t)UE_id][0]->nCCE[subframe] = DCI_pdu->dci_alloc[i].firstCCE;
2272

2273
    LOG_D(PHY,"[eNB %"PRIu8"] Frame %d subframe %d : CCE resource for ue DCI (PDSCH %"PRIx16")  => %"PRIu8"/%u\n",eNB->Mod_id,eNB->proc[sched_subframe].frame_tx,subframe,
2274
    DCI_pdu->dci_alloc[i].rnti,eNB->dlsch[(uint8_t)UE_id][0]->nCCE[subframe],DCI_pdu->dci_alloc[i].firstCCE);
2275

2276

2277 2278
    for (i=0; i<dlsch_harq->TBS>>3; i++)
      LOG_T(PHY,"%"PRIx8".",DLSCH_pdu[i]);
2279

2280
    LOG_T(PHY,"\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2281
#endif
2282
#endif
2283 2284
  } else {
    ue_stats->dlsch_trials[harq_pid][dlsch_harq->round]++;
2285 2286
#ifdef DEBUG_PHY_PROC
#ifdef DEBUG_DLSCH
2287
    LOG_D(PHY,"[eNB] This DLSCH is a retransmission\n");
2288 2289
#endif
#endif
2290
  }
2291

2292
  if (eNB->abstraction_flag==0) {
2293

2294 2295 2296
    LOG_D(PHY,"Generating DLSCH/PDSCH %d\n",ra_flag);
    // 36-212
    start_meas(&eNB->dlsch_encoding_stats);
2297
    //encoding
2298
    eNB->te(eNB,
2299 2300 2301 2302 2303 2304 2305
      DLSCH_pdu,
      num_pdcch_symbols,
      dlsch,
      frame,subframe,
      &eNB->dlsch_rate_matching_stats,
      &eNB->dlsch_turbo_encoding_stats,
      &eNB->dlsch_interleaving_stats);
2306 2307 2308
    stop_meas(&eNB->dlsch_encoding_stats);
    // 36-211
    start_meas(&eNB->dlsch_scrambling_stats);
2309
    //scrambling
2310
    dlsch_scrambling(fp,
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
         0,
         dlsch,
         get_G(fp,
         dlsch_harq->nb_rb,
         dlsch_harq->rb_alloc,
         get_Qm(dlsch_harq->mcs),
         dlsch_harq->Nl,
         num_pdcch_symbols,
         frame,subframe,
         0),
         0,
         subframe<<1);
2323
    stop_meas(&eNB->dlsch_scrambling_stats);
2324

2325 2326 2327
    start_meas(&eNB->dlsch_modulation_stats);


2328
    //modulation
2329
    dlsch_modulation(eNB,
2330 2331 2332 2333 2334 2335 2336
         eNB->common_vars.txdataF[0],
         AMP,
         subframe,
         num_pdcch_symbols,
         dlsch,
         dlsch1);
  
2337 2338
    stop_meas(&eNB->dlsch_modulation_stats);
  }
2339 2340


2341
#ifdef PHY_ABSTRACTION
2342 2343 2344
  else {
    start_meas(&eNB->dlsch_encoding_stats);
    dlsch_encoding_emul(eNB,
2345 2346
      DLSCH_pdu,
      dlsch);
2347 2348
    stop_meas(&eNB->dlsch_encoding_stats);
  }
2349

Raymond Knopp's avatar
 
Raymond Knopp committed
2350
#endif
2351
  dlsch->active = 0;
2352
}
2353

2354
void phy_procedures_eNB_TX(PHY_VARS_eNB *eNB,
2355
         eNB_rxtx_proc_t *proc,
2356
                           relaying_type_t r_type,
2357 2358 2359
         PHY_VARS_RN *rn,
         int do_meas,
         int do_pdcch_flag)
2360 2361
{
  UNUSED(rn);
2362 2363
  int frame=proc->frame_tx;
  int subframe=proc->subframe_tx;
2364
  //  uint16_t input_buffer_length;
Matthieu Kanj's avatar
Matthieu Kanj committed
2365
  uint32_t i,aa;  //j;
2366 2367 2368
  uint8_t harq_pid;
  DCI_PDU *DCI_pdu;
  DCI_PDU DCI_pdu_tmp;
2369
  int8_t UE_id=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
2370
 // uint8_t num_pdcch_symbols=0;
2371 2372
  uint8_t ul_subframe;
  uint32_t ul_frame;
2373
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
Matthieu Kanj's avatar
Matthieu Kanj committed
2374
 // DCI_ALLOC_t *dci_alloc=(DCI_ALLOC_t *)NULL;
2375

2376
  int offset = eNB->CC_id;//proc == &eNB->proc.proc_rxtx[0] ? 0 : 1;
2377

2378 2379 2380
#if defined(SMBV) 
  // counts number of allocations in subframe
  // there is at least one allocation for PDCCH
2381
  uint8_t smbv_alloc_cnt = 1;Exiting eNB thread RXn_TXnp4
2382

2383
#endif
2384

2385
  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)==SF_UL)) return;
2386

2387 2388
 // VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+offset,1);
 // if (do_meas==1) start_meas(&eNB->phy_proc_tx);
2389

Cedric Roux's avatar
Cedric Roux committed
2390
  T(T_ENB_PHY_DL_TICK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe));
2391

2392 2393
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    // If we've dropped the UE, go back to PRACH mode for this UE
2394 2395
    if ((frame==0)&&(subframe==0)) {
      if (eNB->UE_stats[i].crnti > 0) {
2396
  LOG_I(PHY,"UE %d : rnti %x\n",i,eNB->UE_stats[i].crnti);
2397
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2398
    }
2399
    if (eNB->UE_stats[i].ulsch_consecutive_errors == ULSCH_max_consecutive_errors) {
2400
      LOG_W(PHY,"[eNB %d, CC %d] frame %d, subframe %d, UE %d: ULSCH consecutive error count reached %u, triggering UL Failure\n",
2401 2402
            eNB->Mod_id,eNB->CC_id,frame,subframe, i, eNB->UE_stats[i].ulsch_consecutive_errors);
      eNB->UE_stats[i].ulsch_consecutive_errors=0;
2403
      mac_xface->UL_failure_indication(eNB->Mod_id,
2404 2405 2406 2407 2408
               eNB->CC_id,
               frame,
               eNB->UE_stats[i].crnti,
               subframe);
               
Raymond Knopp's avatar
 
Raymond Knopp committed
2409
    }
2410
  
2411

Raymond Knopp's avatar
 
Raymond Knopp committed
2412
  }
2413

2414

2415
  // Get scheduling info for next subframe
2416
  // This is called only for the CC_id = 0 and triggers scheduling for all CC_id's
2417
 /* if (eNB->mac_enabled==1) {
2418
    if (eNB->CC_id == 0) {
2419
      mac_xface->eNB_dlsch_ulsch_scheduler(eNB->Mod_id,0,frame,subframe);//,1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2420
    }
2421 2422
  }*/

2423
  // clear the transmit data array for the current subframe
2424
  if (eNB->abstraction_flag==0) {
2425
    for (aa=0; aa<fp->nb_antenna_ports_eNB; aa++) {      
2426
      memset(&eNB->common_vars.txdataF[0][aa][subframe*fp->ofdm_symbol_size*(fp->symbols_per_tti)],
2427
             0,fp->ofdm_symbol_size*(fp->symbols_per_tti)*sizeof(int32_t));
Raymond Knopp's avatar
 
Raymond Knopp committed
2428
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2429
  }
2430

2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445

///////////////////////////////////////////////////////////////////////////////////
// test if  there is detection, 
  //if yes proceed to setting flag to indicate that there is something to transmit 
  // another flag to indicate that DCI is transmitted 
  // flag to encode DCI
  // store the PDU of DCI
  // add two variable for frame and subframe , in order to know next transmission
  // varible to indicate the remaining repetition to transmit
  



  /////////////////////////////////////////////////////////////////////////////////////////////

2446 2447 2448 2449
 // if (is_pmch_subframe(frame,subframe,fp)) {
   // pmch_procedures(eNB,proc,rn,r_type);
//  }
 // else {
2450
    // this is not a pmch subframe, so generate PSS/SSS/PBCH
2451
    common_signal_procedures(eNB,proc);
2452 2453
 // }
/*
2454
#if defined(SMBV) 
2455

Raymond Knopp's avatar
 
Raymond Knopp committed
2456
  // PBCH takes one allocation
2457
  if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
2458
    if (subframe==0)
Raymond Knopp's avatar
 
Raymond Knopp committed
2459 2460
      smbv_alloc_cnt++;
  }
2461

2462
#endif
2463 2464
*/
 if (eNB->mac_enabled==1) {
2465 2466
    // Parse DCI received from MAC
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,1);
2467
    DCI_pdu = mac_xface->get_dci_sdu(eNB->Mod_id,
2468 2469 2470
             eNB->CC_id,
             frame,
             subframe);
2471 2472 2473
  }
  else {
    DCI_pdu = &DCI_pdu_tmp;
2474

2475
#ifdef EMOS_CHANNEL
2476
   // fill_dci_emos(DCI_pdu,eNB);
2477
#else
2478
  //  fill_dci(DCI_pdu,eNB,proc);
2479
    // clear previous allocation information for all UEs
2480
 /*   for (i=0; i<NUMBER_OF_UE_MAX; i++) {
2481 2482 2483 2484 2485
      if (eNB->dlsch[i][0]){
        for (j=0; j<8; j++)
          eNB->dlsch[i][0]->harq_processes[j]->round = 0;
      }
    }
2486
*/
2487
#endif
2488
  }
2489

Raymond Knopp's avatar
 
Raymond Knopp committed
2490
  // clear existing ulsch dci allocations before applying info from MAC  (this is table
2491 2492 2493
//  ul_subframe = pdcch_alloc2ul_subframe(fp,subframe);
 // ul_frame = pdcch_alloc2ul_frame(fp,frame,subframe);
/*
2494 2495 2496
  if ((subframe_select(fp,ul_subframe)==SF_UL) ||
      (fp->frame_type == FDD)) {
    harq_pid = subframe2harq_pid(fp,ul_frame,ul_subframe);
2497

2498
    // clear DCI allocation maps for new subframe
2499
    for (i=0; i<NUMBER_OF_UE_MAX; i++)
2500 2501 2502
      if (eNB->ulsch[i]) {
        eNB->ulsch[i]->harq_processes[harq_pid]->dci_alloc=0;
        eNB->ulsch[i]->harq_processes[harq_pid]->rar_alloc=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2503
      }
2504 2505
  }*/
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
2506
  // clear previous allocation information for all UEs
2507
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
2508 2509
    if (eNB->dlsch[i][0])
      eNB->dlsch[i][0]->subframe_tx[subframe] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2510
  }
2511
*/
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
  /* save old HARQ information needed for PHICH generation */
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
    if (eNB->ulsch[i]) {
      /* Store first_rb and n_DMRS for correct PHICH generation below.
       * For PHICH generation we need "old" values of last scheduling
       * for this HARQ process. 'generate_eNB_dlsch_params' below will
       * overwrite first_rb and n_DMRS and 'generate_phich_top', done
       * after 'generate_eNB_dlsch_params', would use the "new" values
       * instead of the "old" ones.
       *
       * This has been tested for FDD only, may be wrong for TDD.
       *
       * TODO: maybe we should restructure the code to be sure it
       *       is done correctly. The main concern is if the code
       *       changes and first_rb and n_DMRS are modified before
       *       we reach here, then the PHICH processing will be wrong,
       *       using wrong first_rb and n_DMRS values to compute
       *       ngroup_PHICH and nseq_PHICH.
       *
       * TODO: check if that works with TDD.
       */
      if ((subframe_select(fp,ul_subframe)==SF_UL) ||
          (fp->frame_type == FDD)) {
        harq_pid = subframe2harq_pid(fp,ul_frame,ul_subframe);
        eNB->ulsch[i]->harq_processes[harq_pid]->previous_first_rb =
            eNB->ulsch[i]->harq_processes[harq_pid]->first_rb;
        eNB->ulsch[i]->harq_processes[harq_pid]->previous_n_DMRS =
            eNB->ulsch[i]->harq_processes[harq_pid]->n_DMRS;
      }
    }
  }

2544
/*
2545
  // loop over all DCIs for this subframe to generate DLSCH allocations
2546 2547 2548 2549 2550
  for (i=0; i<DCI_pdu->Num_common_dci + DCI_pdu->Num_ue_spec_dci ; i++) {
    LOG_D(PHY,"[eNB] Subframe %d: DCI %d/%d : rnti %x, CCEind %d\n",subframe,i,DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci,DCI_pdu->dci_alloc[i].rnti,DCI_pdu->dci_alloc[i].firstCCE);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,DCI_pdu->dci_alloc[i].rnti);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,DCI_pdu->dci_alloc[i].format);
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,DCI_pdu->dci_alloc[i].firstCCE);
2551
    dci_alloc = &DCI_pdu->dci_alloc[i];
2552

2553
    if ((dci_alloc->rnti<= P_RNTI) && 
2554
  (dci_alloc->ra_flag!=1)) {
2555
      if (eNB->mac_enabled==1)
2556
  UE_id = find_ue((int16_t)dci_alloc->rnti,eNB);
2557
      else
2558
  UE_id = i;
Raymond Knopp's avatar
 
Raymond Knopp committed
2559
    }
2560 2561
    else UE_id=0;
    
2562
   // generate_eNB_dlsch_params(eNB,proc,dci_alloc,UE_id);
2563

Raymond Knopp's avatar
 
Raymond Knopp committed
2564
  }
2565
*/
2566
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,(frame*10)+subframe);
2567
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
2568
  // Apply physicalConfigDedicated if needed
2569 2570
  // This is for UEs that have received this IE, which changes these DL and UL configuration, we apply after a delay for the eNodeB UL parameters
  phy_config_dedicated_eNB_step2(eNB);
2571

2572
  // Now loop again over the DCIs for UL configuration
2573
  for (i=0; i<DCI_pdu->Num_common_dci + DCI_pdu->Num_ue_spec_dci ; i++) {
Cedric Roux's avatar
Cedric Roux committed
2574
    dci_alloc = &DCI_pdu->dci_alloc[i];
Raymond Knopp's avatar
 
Raymond Knopp committed
2575

2576 2577
    if (dci_alloc->format == format0) {  // this is a ULSCH allocation
      if (eNB->mac_enabled==1)
2578
  UE_id = find_ue((int16_t)dci_alloc->rnti,eNB);
2579
      else
2580
  UE_id = i;
2581
      
2582
      if (UE_id<0) { // should not happen, log an error and exit, this is a fatal error
2583 2584
  LOG_E(PHY,"[eNB %"PRIu8"] Frame %d: Unknown UE_id for rnti %"PRIx16"\n",eNB->Mod_id,frame,dci_alloc->rnti);
  mac_xface->macphy_exit("FATAL\n"); 
2585
      }
2586
      //generate_eNB_ulsch_params(eNB,proc,dci_alloc,UE_id);
Raymond Knopp's avatar
 
Raymond Knopp committed
2587
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2588
  }
2589

2590
*/
2591

Lionel Gauthier's avatar
Lionel Gauthier committed
2592 2593


Raymond Knopp's avatar
 
Raymond Knopp committed
2594 2595
  // if we have DCI to generate do it now
  if ((DCI_pdu->Num_common_dci + DCI_pdu->Num_ue_spec_dci)>0) {
Lionel Gauthier's avatar
Lionel Gauthier committed
2596

2597 2598

  } else { // for emulation!!
2599 2600
    eNB->num_ue_spec_dci[(subframe)&1]=0;
    eNB->num_common_dci[(subframe)&1]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2601
  }
2602

2603
/*
2604
  if (eNB->abstraction_flag == 0) {
2605 2606
    if (do_pdcch_flag) {
      if (DCI_pdu->Num_ue_spec_dci+DCI_pdu->Num_common_dci > 0) {
2607 2608
  LOG_D(PHY,"[eNB %"PRIu8"] Frame %d, subframe %d: Calling generate_dci_top (pdcch) (common %"PRIu8",ue_spec %"PRIu8")\n",eNB->Mod_id,frame, subframe,
        DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci);
2609
      }
2610

2611
      num_pdcch_symbols = generate_dci_top(DCI_pdu->Num_ue_spec_dci,
2612 2613 2614 2615 2616 2617 2618
             DCI_pdu->Num_common_dci,
             DCI_pdu->dci_alloc,
             0,
             AMP,
             fp,
             eNB->common_vars.txdataF[0],
             subframe);
2619 2620 2621 2622
    }
    else {
      num_pdcch_symbols = DCI_pdu->num_pdcch_symbols;
      LOG_D(PHY,"num_pdcch_symbols %"PRIu8" (dci common %"PRIu8", dci uespec %"PRIu8")\n",num_pdcch_symbols,
2623
      DCI_pdu->Num_common_dci,DCI_pdu->Num_ue_spec_dci);
2624
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2625
  }
2626

2627
#ifdef PHY_ABSTRACTION // FIXME this ifdef seems suspicious
Raymond Knopp's avatar
 
Raymond Knopp committed
2628
  else {
2629
    LOG_D(PHY,"[eNB %"PRIu8"] Frame %d, subframe %d: Calling generate_dci_to_emul\n",eNB->Mod_id,frame, subframe);
2630
    num_pdcch_symbols = generate_dci_top_emul(eNB,DCI_pdu->Num_ue_spec_dci,DCI_pdu->Num_common_dci,DCI_pdu->dci_alloc,subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
2631
  }
2632

2633
#endif
2634
*/
2635 2636
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_DCI_INFO,DCI_pdu->num_pdcch_symbols);

2637
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PDCCH_TX,0);
2638
/*
2639 2640 2641 2642 2643
#if defined(SMBV) 
  // Sets up PDCCH and DCI table
  if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4) && ((DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci)>0)) {
    LOG_D(PHY,"[SMBV] Frame %3d, SF %d PDCCH, number of DCIs %d\n",frame,subframe,DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci);
    dump_dci(fp,&DCI_pdu->dci_alloc[0]);
2644
   // smbv_configure_pdcch(smbv_fname,(smbv_frame_cnt*10) + (subframe),num_pdcch_symbols,DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci);
2645 2646 2647
  }
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2648
  // Check for SI activity
2649
  //MP: eNB->dlsch_SI->active is set by the function generate_dlsch_params_from_dci (depending on the DCI content)
2650
  if ((eNB->dlsch_SI) && (eNB->dlsch_SI->active == 1)) {
2651

2652
   // pdsch_procedures(eNB,proc,eNB->dlsch_SI,(LTE_eNB_DLSCH_t*)NULL,(LTE_eNB_UE_stats*)NULL,0,num_pdcch_symbols);
2653

2654
#if defined(SMBV) 
2655

Raymond Knopp's avatar
 
Raymond Knopp committed
2656
    // Configures the data source of allocation (allocation is configured by DCI)
2657 2658
    if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
      LOG_D(PHY,"[SMBV] Frame %3d, Configuring SI payload in SF %d alloc %"PRIu8"\n",frame,(smbv_frame_cnt*10) + (subframe),smbv_alloc_cnt);
2659
     // smbv_configure_datalist_for_alloc(smbv_fname, smbv_alloc_cnt++, (smbv_frame_cnt*10) + (subframe), DLSCH_pdu, input_buffer_length);
Raymond Knopp's avatar
 
Raymond Knopp committed
2660
    }
2661 2662

#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2663
  }
2664
*/
Raymond Knopp's avatar
 
Raymond Knopp committed
2665
  // Check for RA activity
2666
  if ((eNB->dlsch_ra) && (eNB->dlsch_ra->active == 1)) {
2667
/*
2668
#if defined(SMBV) 
2669

2670
    // Configures the data source of allocation (allocation is configured by DCI)
2671 2672
    if (smbv_is_config_frame(frame) && (smbv_frame_cnt < 4)) {
      LOG_D(PHY,"[SMBV] Frame %3d, Configuring RA payload in SF %d alloc %"PRIu8"\n",frame,(smbv_frame_cnt*10) + (subframe),smbv_alloc_cnt);
2673 2674 2675
      smbv_configure_datalist_for_alloc(smbv_fname, smbv_alloc_cnt++, (smbv_frame_cnt*10) + (subframe), dlsch_input_buffer, input_buffer_length);
    }
    
2676
#endif
2677
*/    
2678
    
2679
    LOG_D(PHY,"[eNB %"PRIu8"][RAPROC] Frame %d, subframe %d: Calling generate_dlsch (RA),Msg3 frame %"PRIu32", Msg3 subframe %"PRIu8"\n",
2680 2681 2682 2683
    eNB->Mod_id,
    frame, subframe,
    eNB->ulsch[(uint32_t)UE_id]->Msg3_frame,
    eNB->ulsch[(uint32_t)UE_id]->Msg3_subframe);
2684
    
2685
   // pdsch_procedures(eNB,proc,eNB->dlsch_ra,(LTE_eNB_DLSCH_t*)NULL,(LTE_eNB_UE_stats*)NULL,1,num_pdcch_symbols);
2686 2687
    
    
2688
    eNB->dlsch_ra->active = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2689
  }
2690

Raymond Knopp's avatar
 
Raymond Knopp committed
2691
  // Now scan UE specific DLSCH
2692
  for (UE_id=0; UE_id<NUMBER_OF_UE_MAX; UE_id++)
2693 2694
    {
      if ((eNB->dlsch[(uint8_t)UE_id][0])&&
2695 2696
    (eNB->dlsch[(uint8_t)UE_id][0]->rnti>0)&&
    (eNB->dlsch[(uint8_t)UE_id][0]->active == 1)) {
2697

2698
  //pdsch_procedures(eNB,proc,eNB->dlsch[(uint8_t)UE_id][0],eNB->dlsch[(uint8_t)UE_id][1],&eNB->UE_stats[(uint32_t)UE_id],0,num_pdcch_symbols);
2699 2700


Raymond Knopp's avatar
 
Raymond Knopp committed
2701
      }
2702

2703
      else if ((eNB->dlsch[(uint8_t)UE_id][0])&&
2704 2705
         (eNB->dlsch[(uint8_t)UE_id][0]->rnti>0)&&
         (eNB->dlsch[(uint8_t)UE_id][0]->active == 0)) {
2706

2707 2708
  // clear subframe TX flag since UE is not scheduled for PDSCH in this subframe (so that we don't look for PUCCH later)
  //eNB->dlsch[(uint8_t)UE_id][0]->subframe_tx[subframe]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2709
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2710
    }
2711

2712 2713


Raymond Knopp's avatar
 
Raymond Knopp committed
2714
  // if we have PHICH to generate
2715

2716
  if (is_phich_subframe(fp,subframe))
2717
    {
2718 2719 2720 2721
     /* generate_phich_top(eNB,
       proc,
       AMP,
       0);*/
2722
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2723

2724 2725 2726 2727 2728 2729 2730
  /*
  if (frame>=10 && subframe>=9) {
    write_output("/tmp/txsigF0.m","txsF0", &eNB->common_vars.txdataF[0][0][0],120*eNB->frame_parms.ofdm_symbol_size,1,1);
    write_output("/tmp/txsigF1.m","txsF1", &eNB->common_vars.txdataF[0][0][0],120*eNB->frame_parms.ofdm_symbol_size,1,1);
    abort();
  }
  */
2731

2732
#ifdef EMOS
2733
 // phy_procedures_emos_eNB_TX(subframe, eNB);
Raymond Knopp's avatar
 
Raymond Knopp committed
2734
#endif
2735

2736
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_TX+offset,0);
2737
  if (do_meas==1) stop_meas(&eNB->phy_proc_tx);
2738
  
2739 2740
}

2741
void process_Msg3(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,uint8_t UE_id, uint8_t harq_pid)
2742
{
Raymond Knopp's avatar
 
Raymond Knopp committed
2743
  // this prepares the demodulation of the first PUSCH of a new user, containing Msg3
2744 2745
  int subframe = proc->subframe_rx;
  int frame = proc->frame_rx;
Raymond Knopp's avatar
 
Raymond Knopp committed
2746

Raymond Knopp's avatar
 
Raymond Knopp committed
2747
  LOG_D(PHY,"[eNB %d][RAPROC] frame %d : subframe %d : process_Msg3 UE_id %d (active %d, subframe %d, frame %d)\n",
2748
        eNB->Mod_id,
2749
        frame,subframe,
2750 2751 2752 2753
        UE_id,eNB->ulsch[(uint32_t)UE_id]->Msg3_active,
        eNB->ulsch[(uint32_t)UE_id]->Msg3_subframe,
        eNB->ulsch[(uint32_t)UE_id]->Msg3_frame);
  eNB->ulsch[(uint32_t)UE_id]->Msg3_flag = 0;
2754

2755 2756 2757
  if ((eNB->ulsch[(uint32_t)UE_id]->Msg3_active == 1) &&
      (eNB->ulsch[(uint32_t)UE_id]->Msg3_subframe == subframe) &&
      (eNB->ulsch[(uint32_t)UE_id]->Msg3_frame == (uint32_t)frame))   {
2758

2759
    //    harq_pid = 0;
2760

2761 2762 2763
    eNB->ulsch[(uint32_t)UE_id]->Msg3_active = 0;
    eNB->ulsch[(uint32_t)UE_id]->Msg3_flag = 1;
    eNB->ulsch[(uint32_t)UE_id]->harq_processes[harq_pid]->subframe_scheduling_flag=1;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2764
    LOG_D(PHY,"[eNB %d][RAPROC] frame %d, subframe %d: Setting subframe_scheduling_flag (Msg3) for UE %d\n",
2765
          eNB->Mod_id,
2766
          frame,subframe,UE_id);
2767 2768 2769 2770 2771 2772 2773 2774 2775
  }
}


// This function retrieves the harq_pid of the corresponding DLSCH process
// and updates the error statistics of the DLSCH based on the received ACK
// info from UE along with the round index.  It also performs the fine-grain
// rate-adaptation based on the error statistics derived from the ACK/NAK process

2776
void process_HARQ_feedback(uint8_t UE_id,
2777
                           PHY_VARS_eNB *eNB,
2778
         eNB_rxtx_proc_t *proc,
2779 2780 2781 2782 2783
                           uint8_t pusch_flag,
                           uint8_t *pucch_payload,
                           uint8_t pucch_sel,
                           uint8_t SR_payload)
{
2784

2785
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2786
  uint8_t dl_harq_pid[8],dlsch_ACK[8],dl_subframe;
2787 2788
  LTE_eNB_DLSCH_t *dlsch             =  eNB->dlsch[(uint32_t)UE_id][0];
  LTE_eNB_UE_stats *ue_stats         =  &eNB->UE_stats[(uint32_t)UE_id];
2789
  LTE_DL_eNB_HARQ_t *dlsch_harq_proc;
2790
  uint8_t subframe_m4,M,m;
2791 2792
  int mp;
  int all_ACKed=1,nb_alloc=0,nb_ACK=0;
2793 2794
  int frame = proc->frame_rx;
  int subframe = proc->subframe_rx;
2795
  int harq_pid = subframe2harq_pid( fp,frame,subframe);
2796

2797
  if (fp->frame_type == FDD) { //FDD
2798
    subframe_m4 = (subframe<4) ? subframe+6 : subframe-4;
2799

2800 2801
    dl_harq_pid[0] = dlsch->harq_ids[subframe_m4];
    M=1;
2802

2803
    if (pusch_flag == 1) {
2804
      dlsch_ACK[0] = eNB->ulsch[(uint8_t)UE_id]->harq_processes[harq_pid]->o_ACK[0];
2805
      if (dlsch->subframe_tx[subframe_m4]==1)
2806 2807
  LOG_D(PHY,"[eNB %d] Frame %d: Received ACK/NAK %d on PUSCH for subframe %d\n",eNB->Mod_id,
        frame,dlsch_ACK[0],subframe_m4);
2808 2809
    }
    else {
2810
      dlsch_ACK[0] = pucch_payload[0];
2811
      LOG_D(PHY,"[eNB %d] Frame %d: Received ACK/NAK %d on PUCCH for subframe %d\n",eNB->Mod_id,
2812
      frame,dlsch_ACK[0],subframe_m4);
kaltenbe's avatar
kaltenbe committed
2813
      /*
2814 2815
  if (dlsch_ACK[0]==0)
  AssertFatal(0,"Exiting on NAK on PUCCH\n");
kaltenbe's avatar
kaltenbe committed
2816
      */
2817
    }
2818

Lionel Gauthier's avatar
Lionel Gauthier committed
2819 2820 2821

#if defined(MESSAGE_CHART_GENERATOR_PHY)
    MSC_LOG_RX_MESSAGE(
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
           MSC_PHY_ENB,MSC_PHY_UE,
           NULL,0,
           "%05u:%02u %s received %s  rnti %x harq id %u  tx SF %u",
           frame,subframe,
           (pusch_flag == 1)?"PUSCH":"PUCCH",
           (dlsch_ACK[0])?"ACK":"NACK",
           dlsch->rnti,
           dl_harq_pid[0],
           subframe_m4
           );
Lionel Gauthier's avatar
Lionel Gauthier committed
2832
#endif
2833 2834
  } else { // TDD Handle M=1,2 cases only

2835
    M=ul_ACK_subframe2_M(fp,
2836 2837
                         subframe);

2838 2839 2840 2841 2842 2843 2844 2845
    // Now derive ACK information for TDD
    if (pusch_flag == 1) { // Do PUSCH ACK/NAK first
      // detect missing DAI
      //FK: this code is just a guess
      //RK: not exactly, yes if scheduled from PHICH (i.e. no DCI format 0)
      //    otherwise, it depends on how many of the PDSCH in the set are scheduled, we can leave it like this,
      //    but we have to adapt the code below.  For example, if only one out of 2 are scheduled, only 1 bit o_ACK is used

2846
      dlsch_ACK[0] = eNB->ulsch[(uint8_t)UE_id]->harq_processes[harq_pid]->o_ACK[0];
2847
      dlsch_ACK[1] = (eNB->pucch_config_dedicated[UE_id].tdd_AckNackFeedbackMode == bundling)
2848
  ?eNB->ulsch[(uint8_t)UE_id]->harq_processes[harq_pid]->o_ACK[0]:eNB->ulsch[(uint8_t)UE_id]->harq_processes[harq_pid]->o_ACK[1];
2849
    }
2850

2851
    else {  // PUCCH ACK/NAK
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
      if ((SR_payload == 1)&&(pucch_sel!=2)) {  // decode Table 7.3 if multiplexing and SR=1
        nb_ACK = 0;

        if (M == 2) {
          if ((pucch_payload[0] == 1) && (pucch_payload[1] == 1)) // b[0],b[1]
            nb_ACK = 1;
          else if ((pucch_payload[0] == 1) && (pucch_payload[1] == 0))
            nb_ACK = 2;
        } else if (M == 3) {
          if ((pucch_payload[0] == 1) && (pucch_payload[1] == 1))
            nb_ACK = 1;
          else if ((pucch_payload[0] == 1) && (pucch_payload[1] == 0))
            nb_ACK = 2;
          else if ((pucch_payload[0] == 0) && (pucch_payload[1] == 1))
            nb_ACK = 3;
        }
      } else if (pucch_sel == 2) { // bundling or M=1
        dlsch_ACK[0] = pucch_payload[0];
        dlsch_ACK[1] = pucch_payload[0];
      } else { // multiplexing with no SR, this is table 10.1
        if (M==1)
          dlsch_ACK[0] = pucch_payload[0];
        else if (M==2) {
          if (((pucch_sel == 1) && (pucch_payload[0] == 1) && (pucch_payload[1] == 1)) ||
              ((pucch_sel == 0) && (pucch_payload[0] == 0) && (pucch_payload[1] == 1)))
            dlsch_ACK[0] = 1;
          else
            dlsch_ACK[0] = 0;

          if (((pucch_sel == 1) && (pucch_payload[0] == 1) && (pucch_payload[1] == 1)) ||
              ((pucch_sel == 1) && (pucch_payload[0] == 0) && (pucch_payload[1] == 0)))
            dlsch_ACK[1] = 1;
          else
            dlsch_ACK[1] = 0;
        }
2887
      }
2888
    }
2889 2890 2891 2892
  }

  // handle case where positive SR was transmitted with multiplexing
  if ((SR_payload == 1)&&(pucch_sel!=2)&&(pusch_flag == 0)) {
2893
    nb_alloc = 0;
2894 2895

    for (m=0; m<M; m++) {
2896
      dl_subframe = ul_ACK_subframe2_dl_subframe(fp,
2897 2898
             subframe,
             m);
2899 2900 2901

      if (dlsch->subframe_tx[dl_subframe]==1)
        nb_alloc++;
2902
    }
2903

2904 2905
    if (nb_alloc == nb_ACK)
      all_ACKed = 1;
2906
    else
2907
      all_ACKed = 0;
2908 2909 2910
  }


2911
  for (m=0,mp=-1; m<M; m++) {
2912

2913
    dl_subframe = ul_ACK_subframe2_dl_subframe(fp,
2914 2915
                 subframe,
                 m);
2916

2917 2918
    if (dlsch->subframe_tx[dl_subframe]==1) {
      if (pusch_flag == 1)
2919
        mp++;
2920
      else
2921
        mp = m;
2922

2923
      dl_harq_pid[m]     = dlsch->harq_ids[dl_subframe];
2924
      harq_pid_updated[UE_id][dl_harq_pid[m]] = 1;
2925

2926
      if ((pucch_sel != 2)&&(pusch_flag == 0)) { // multiplexing
2927 2928 2929 2930
        if ((SR_payload == 1)&&(all_ACKed == 1))
          dlsch_ACK[m] = 1;
        else
          dlsch_ACK[m] = 0;
2931
      }
2932

2933
      if (dl_harq_pid[m]<dlsch->Mdlharq) {
2934 2935
        dlsch_harq_proc = dlsch->harq_processes[dl_harq_pid[m]];
#ifdef DEBUG_PHY_PROC
2936
        LOG_D(PHY,"[eNB %d][PDSCH %x/%d] subframe %d, status %d, round %d (mcs %d, rv %d, TBS %d)\n",eNB->Mod_id,
2937 2938 2939 2940 2941 2942 2943 2944 2945
              dlsch->rnti,dl_harq_pid[m],dl_subframe,
              dlsch_harq_proc->status,dlsch_harq_proc->round,
              dlsch->harq_processes[dl_harq_pid[m]]->mcs,
              dlsch->harq_processes[dl_harq_pid[m]]->rvidx,
              dlsch->harq_processes[dl_harq_pid[m]]->TBS);

        if (dlsch_harq_proc->status==DISABLED)
          LOG_E(PHY,"dlsch_harq_proc is disabled? \n");

2946
#endif
2947 2948 2949 2950 2951 2952 2953 2954

        if ((dl_harq_pid[m]<dlsch->Mdlharq) &&
            (dlsch_harq_proc->status == ACTIVE)) {
          // dl_harq_pid of DLSCH is still active

          if ( dlsch_ACK[mp]==0) {
            // Received NAK
#ifdef DEBUG_PHY_PROC
2955
            LOG_D(PHY,"[eNB %d][PDSCH %x/%d] M = %d, m= %d, mp=%d NAK Received in round %d, requesting retransmission\n",eNB->Mod_id,
2956
                  dlsch->rnti,dl_harq_pid[m],M,m,mp,dlsch_harq_proc->round);
2957
#endif
2958

2959
            T(T_ENB_PHY_DLSCH_UE_NACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(UE_id), T_INT(dlsch->rnti),
2960
              T_INT(dl_harq_pid[m]));
2961

2962 2963 2964 2965 2966 2967 2968 2969 2970
            if (dlsch_harq_proc->round == 0)
              ue_stats->dlsch_NAK_round0++;

            ue_stats->dlsch_NAK[dl_harq_pid[m]][dlsch_harq_proc->round]++;


            // then Increment DLSCH round index
            dlsch_harq_proc->round++;

2971

2972
            if (dlsch_harq_proc->round == dlsch->Mlimit) {
2973 2974
              // This was the last round for DLSCH so reset round and increment l2_error counter
#ifdef DEBUG_PHY_PROC
2975
              LOG_W(PHY,"[eNB %d][PDSCH %x/%d] DLSCH retransmissions exhausted, dropping packet\n",eNB->Mod_id,
2976
                    dlsch->rnti,dl_harq_pid[m]);
2977
#endif
Lionel Gauthier's avatar
Lionel Gauthier committed
2978
#if defined(MESSAGE_CHART_GENERATOR_PHY)
2979
              MSC_LOG_EVENT(MSC_PHY_ENB, "0 HARQ DLSCH Failed RNTI %"PRIx16" round %u",
2980 2981
                            dlsch->rnti,
                            dlsch_harq_proc->round);
Lionel Gauthier's avatar
Lionel Gauthier committed
2982
#endif
Lionel Gauthier's avatar
Lionel Gauthier committed
2983

2984 2985 2986 2987 2988 2989 2990
              dlsch_harq_proc->round = 0;
              ue_stats->dlsch_l2_errors[dl_harq_pid[m]]++;
              dlsch_harq_proc->status = SCH_IDLE;
              dlsch->harq_ids[dl_subframe] = dlsch->Mdlharq;
            }
          } else {
#ifdef DEBUG_PHY_PROC
2991
            LOG_D(PHY,"[eNB %d][PDSCH %x/%d] ACK Received in round %d, resetting process\n",eNB->Mod_id,
2992
                  dlsch->rnti,dl_harq_pid[m],dlsch_harq_proc->round);
2993
#endif
2994

2995
            T(T_ENB_PHY_DLSCH_UE_ACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(UE_id), T_INT(dlsch->rnti),
2996
              T_INT(dl_harq_pid[m]));
2997

2998 2999 3000 3001 3002 3003 3004 3005
            ue_stats->dlsch_ACK[dl_harq_pid[m]][dlsch_harq_proc->round]++;

            // Received ACK so set round to 0 and set dlsch_harq_pid IDLE
            dlsch_harq_proc->round  = 0;
            dlsch_harq_proc->status = SCH_IDLE;
            dlsch->harq_ids[dl_subframe] = dlsch->Mdlharq;

            ue_stats->total_TBS = ue_stats->total_TBS +
3006
        eNB->dlsch[(uint8_t)UE_id][0]->harq_processes[dl_harq_pid[m]]->TBS;
3007 3008
            /*
              ue_stats->total_transmitted_bits = ue_stats->total_transmitted_bits +
3009
              eNB->dlsch[(uint8_t)UE_id][0]->harq_processes[dl_harq_pid[m]]->TBS;
3010 3011
            */
          }
3012
   
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
          // Do fine-grain rate-adaptation for DLSCH
          if (ue_stats->dlsch_NAK_round0 > dlsch->error_threshold) {
            if (ue_stats->dlsch_mcs_offset == 1)
              ue_stats->dlsch_mcs_offset=0;
            else
              ue_stats->dlsch_mcs_offset=-1;
          }

#ifdef DEBUG_PHY_PROC
          LOG_D(PHY,"[process_HARQ_feedback] Frame %d Setting round to %d for pid %d (subframe %d)\n",frame,
                dlsch_harq_proc->round,dl_harq_pid[m],subframe);
3024
#endif
3025
    harq_pid_round[UE_id][dl_harq_pid[m]] = dlsch_harq_proc->round;
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
          // Clear NAK stats and adjust mcs offset
          // after measurement window timer expires
          if (ue_stats->dlsch_sliding_cnt == dlsch->ra_window_size) {
            if ((ue_stats->dlsch_mcs_offset == 0) && (ue_stats->dlsch_NAK_round0 < 2))
              ue_stats->dlsch_mcs_offset = 1;

            if ((ue_stats->dlsch_mcs_offset == 1) && (ue_stats->dlsch_NAK_round0 > 2))
              ue_stats->dlsch_mcs_offset = 0;

            if ((ue_stats->dlsch_mcs_offset == 0) && (ue_stats->dlsch_NAK_round0 > 2))
              ue_stats->dlsch_mcs_offset = -1;

            if ((ue_stats->dlsch_mcs_offset == -1) && (ue_stats->dlsch_NAK_round0 < 2))
              ue_stats->dlsch_mcs_offset = 0;

            ue_stats->dlsch_NAK_round0 = 0;
            ue_stats->dlsch_sliding_cnt = 0;
          }
        }
3045
      }
3046
    }
3047 3048 3049
  }
}

3050
void get_n1_pucch_eNB(PHY_VARS_eNB *eNB,
3051
          eNB_rxtx_proc_t *proc,
3052 3053 3054 3055 3056 3057
                      uint8_t UE_id,
                      int16_t *n1_pucch0,
                      int16_t *n1_pucch1,
                      int16_t *n1_pucch2,
                      int16_t *n1_pucch3)
{
3058

3059
  LTE_DL_FRAME_PARMS *frame_parms=&eNB->frame_parms;
3060
  uint8_t nCCE0,nCCE1;
3061
  int sf;
3062 3063
  int frame = proc->frame_rx;
  int subframe = proc->subframe_rx;
3064 3065

  if (frame_parms->frame_type == FDD ) {
3066
    sf = (subframe<4) ? (subframe+6) : (subframe-4);
3067

3068 3069
    if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[sf]>0) {
      *n1_pucch0 = frame_parms->pucch_config_common.n1PUCCH_AN + eNB->dlsch[(uint32_t)UE_id][0]->nCCE[sf];
3070
      *n1_pucch1 = -1;
3071
    } else {
3072 3073 3074
      *n1_pucch0 = -1;
      *n1_pucch1 = -1;
    }
3075
  } else {
3076

3077 3078 3079
    switch (frame_parms->tdd_config) {
    case 1:  // DL:S:UL:UL:DL:DL:S:UL:UL:DL
      if (subframe == 2) {  // ACK subframes 5 and 6
3080
        /*  if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[6]>0) {
3081 3082 3083 3084 3085
      nCCE1 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[6];
      *n1_pucch1 = get_Np(frame_parms->N_RB_DL,nCCE1,1) + nCCE1 + frame_parms->pucch_config_common.n1PUCCH_AN;
      }
      else
      *n1_pucch1 = -1;*/
3086

3087 3088
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[5]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[5];
3089 3090 3091 3092 3093 3094 3095
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0+ frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;

        *n1_pucch1 = -1;
      } else if (subframe == 3) { // ACK subframe 9

3096 3097
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[9]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[9];
3098 3099 3100 3101 3102 3103 3104 3105 3106
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0 +frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;

        *n1_pucch1 = -1;

      } else if (subframe == 7) { // ACK subframes 0 and 1
        //harq_ack[0].nCCE;
        //harq_ack[1].nCCE;
3107 3108
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[0]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[0];
3109 3110 3111 3112 3113 3114 3115
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0 + frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;

        *n1_pucch1 = -1;
      } else if (subframe == 8) { // ACK subframes 4
        //harq_ack[4].nCCE;
3116 3117
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[4]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[4];
3118 3119 3120 3121 3122 3123 3124
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0 + frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;

        *n1_pucch1 = -1;
      } else {
        LOG_D(PHY,"[eNB %d] frame %d: phy_procedures_lte.c: get_n1pucch, illegal subframe %d for tdd_config %d\n",
3125
              eNB->Mod_id,
3126 3127 3128
              frame,
              subframe,frame_parms->tdd_config);
        return;
3129
      }
3130

3131
      break;
3132

3133 3134
    case 3:  // DL:S:UL:UL:UL:DL:DL:DL:DL:DL
      if (subframe == 2) {  // ACK subframes 5,6 and 1 (S in frame-2), forget about n-11 for the moment (S-subframe)
3135 3136
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[6]>0) {
          nCCE1 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[6];
3137 3138 3139 3140
          *n1_pucch1 = get_Np(frame_parms->N_RB_DL,nCCE1,1) + nCCE1 + frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch1 = -1;

3141 3142
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[5]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[5];
3143 3144 3145 3146 3147
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0+ frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;
      } else if (subframe == 3) { // ACK subframes 7 and 8
        LOG_D(PHY,"get_n1_pucch_eNB : subframe 3, subframe_tx[7] %d, subframe_tx[8] %d\n",
3148
              eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[7],eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[8]);
3149

3150 3151
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[8]>0) {
          nCCE1 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[8];
3152 3153 3154 3155 3156
          *n1_pucch1 = get_Np(frame_parms->N_RB_DL,nCCE1,1) + nCCE1 + frame_parms->pucch_config_common.n1PUCCH_AN;
          LOG_D(PHY,"nCCE1 %d, n1_pucch1 %d\n",nCCE1,*n1_pucch1);
        } else
          *n1_pucch1 = -1;

3157 3158
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[7]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[7];
3159 3160 3161 3162 3163
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0 +frame_parms->pucch_config_common.n1PUCCH_AN;
          LOG_D(PHY,"nCCE0 %d, n1_pucch0 %d\n",nCCE0,*n1_pucch0);
        } else
          *n1_pucch0 = -1;
      } else if (subframe == 4) { // ACK subframes 9 and 0
3164 3165
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[0]>0) {
          nCCE1 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[0];
3166 3167 3168 3169
          *n1_pucch1 = get_Np(frame_parms->N_RB_DL,nCCE1,1) + nCCE1 + frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch1 = -1;

3170 3171
        if (eNB->dlsch[(uint32_t)UE_id][0]->subframe_tx[9]>0) {
          nCCE0 = eNB->dlsch[(uint32_t)UE_id][0]->nCCE[9];
3172 3173 3174 3175 3176
          *n1_pucch0 = get_Np(frame_parms->N_RB_DL,nCCE0,0) + nCCE0 +frame_parms->pucch_config_common.n1PUCCH_AN;
        } else
          *n1_pucch0 = -1;
      } else {
        LOG_D(PHY,"[eNB %d] Frame %d: phy_procedures_lte.c: get_n1pucch, illegal subframe %d for tdd_config %d\n",
3177
              eNB->Mod_id,frame,subframe,frame_parms->tdd_config);
3178
        return;
3179
      }
3180

3181
      break;
3182 3183
    }  // switch tdd_config

3184 3185 3186
    // Don't handle the case M>2
    *n1_pucch2 = -1;
    *n1_pucch3 = -1;
3187 3188 3189
  }
}

3190
void prach_procedures(PHY_VARS_eNB *eNB) {
3191

Matthieu Kanj's avatar
Matthieu Kanj committed
3192
 // LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
3193 3194
 // uint16_t preamble_energy_list[64],preamble_delay_list[64];
 // uint16_t preamble_max,preamble_energy_max;
Matthieu Kanj's avatar
Matthieu Kanj committed
3195 3196 3197
  // uint16_t preamble_max=0;
 // uint16_t i;
 // int8_t UE_id;
3198 3199
  int subframe = eNB->proc.subframe_prach;
  int frame = eNB->proc.frame_prach;
Matthieu Kanj's avatar
Matthieu Kanj committed
3200
 // uint8_t CC_id = eNB->CC_id;
3201

3202 3203 3204

  uint32_t detection=0;
  uint16_t estimated_TA=2;
3205
  /*VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,1);
3206
  memset(&preamble_energy_list[0],0,64*sizeof(uint16_t));
3207
  memset(&preamble_delay_list[0],0,64*sizeof(uint16_t));*/
3208

3209
  if (eNB->abstraction_flag == 0) {
3210
    /*LOG_D(PHY,"[eNB %d][RAPROC] Frame %d, Subframe %d : PRACH RX Signal Power : %d dBm\n",eNB->Mod_id, 
3211
          frame,subframe,dB_fixed(signal_energy(&eNB->common_vars.rxdata[0][0][subframe*fp->samples_per_tti],512)) - eNB->rx_total_gain_dB);
3212

3213

3214
    rx_prach(eNB,
3215 3216 3217
             preamble_energy_list,
             preamble_delay_list,
             frame,
3218 3219
             0);*/
  //usleep(100);
3220 3221
   detection = nprach_procedures_NB_IoT(eNB);
 /* } else {
3222 3223
    for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {

3224
      LOG_D(PHY,"[RAPROC] UE_id %d (%p), generate_prach %d, UE RSI %d, eNB RSI %d preamble index %d\n",
3225
            UE_id,PHY_vars_UE_g[UE_id][CC_id],PHY_vars_UE_g[UE_id][CC_id]->generate_prach,
3226
            PHY_vars_UE_g[UE_id][CC_id]->frame_parms.prach_config_common.rootSequenceIndex,
3227
            fp->prach_config_common.rootSequenceIndex,
3228 3229
            PHY_vars_UE_g[UE_id][CC_id]->prach_PreambleIndex);

Raymond Knopp's avatar
 
Raymond Knopp committed
3230
      if ((PHY_vars_UE_g[UE_id][CC_id]->generate_prach==1) &&
3231
          (PHY_vars_UE_g[UE_id][CC_id]->frame_parms.prach_config_common.rootSequenceIndex ==
3232
           fp->prach_config_common.rootSequenceIndex) ) {
3233 3234 3235
        preamble_energy_list[PHY_vars_UE_g[UE_id][CC_id]->prach_PreambleIndex] = 800;
        preamble_delay_list[PHY_vars_UE_g[UE_id][CC_id]->prach_PreambleIndex] = 5;

3236 3237
      }
    } */
3238
  }
3239

3240
  /*preamble_energy_max = preamble_energy_list[0];
3241 3242
  preamble_max = 0;

3243
  for (i=1; i<64; i++) {
3244 3245 3246 3247
    if (preamble_energy_max < preamble_energy_list[i]) {
      preamble_energy_max = preamble_energy_list[i];
      preamble_max = i;
    }
3248 3249 3250
  }

#ifdef DEBUG_PHY_PROC
3251
  LOG_D(PHY,"[RAPROC] Most likely preamble %d, energy %d dB delay %d\n",
3252 3253 3254
        preamble_max,
        preamble_energy_list[preamble_max],
        preamble_delay_list[preamble_max]);
3255 3256
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
3257
  if (preamble_energy_list[preamble_max] > 580) {
Raymond Knopp's avatar
 
Raymond Knopp committed
3258

3259
    UE_id = find_next_ue_index(eNB);
3260
 
3261
    if (UE_id>=0) {
3262
      eNB->UE_stats[(uint32_t)UE_id].UE_timing_offset = preamble_delay_list[preamble_max]&0x1FFF; //limit to 13 (=11+2) bits
3263

3264
      eNB->UE_stats[(uint32_t)UE_id].sector = 0;
3265
      LOG_D(PHY,"[eNB %d/%d][RAPROC] Frame %d, subframe %d Initiating RA procedure (UE_id %d) with preamble %d, energy %d.%d dB, delay %d\n",
3266 3267
            eNB->Mod_id,
            eNB->CC_id,
3268 3269
            frame,
            subframe,
3270
      UE_id,
3271 3272 3273 3274
            preamble_max,
            preamble_energy_max/10,
            preamble_energy_max%10,
            preamble_delay_list[preamble_max]);
3275

Cedric Roux's avatar
Cedric Roux committed
3276 3277 3278
      T(T_ENB_PHY_INITIATE_RA_PROCEDURE, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(UE_id),
        T_INT(preamble_max), T_INT(preamble_energy_max), T_INT(preamble_delay_list[preamble_max]));

3279
      if (eNB->mac_enabled==1) {
3280
        uint8_t update_TA  = 4;
3281
  uint8_t update_TA2 = 1;
3282
        switch (fp->N_RB_DL) {
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294
        case 6:
          update_TA = 16;
          break;

        case 25:
          update_TA = 4;
          break;

        case 50:
          update_TA = 2;
          break;

3295 3296 3297
  case 75:
    update_TA  = 3;
    update_TA2 = 2;
3298
        case 100:
3299
          update_TA  = 1;
3300 3301 3302
          break;
        }

3303 3304
        */
  /////////////////////////////////////////// NB-IoT testing //////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
3305
 if(detection == 1)
3306 3307 3308 3309
  {
    mac_xface->initiate_ra_proc(eNB->Mod_id,
            eNB->CC_id,
            frame,
Matthieu Kanj's avatar
Matthieu Kanj committed
3310
            eNB->preamble_index_NB_IoT,
3311 3312 3313 3314 3315 3316
            estimated_TA,
            0,subframe,0);      

  }
  /////////////////////////////////////////////////////////////////////////////////////
/*
3317 3318 3319 3320 3321 3322
  mac_xface->initiate_ra_proc(eNB->Mod_id,
            eNB->CC_id,
            frame,
            preamble_max,
            preamble_delay_list[preamble_max]*update_TA/update_TA2,
            0,subframe,0);
3323 3324
      }      

3325
    } else {
3326
      MSC_LOG_EVENT(MSC_PHY_ENB, "0 RA Failed add user, too many");
3327
      LOG_I(PHY,"[eNB %d][RAPROC] frame %d, subframe %d: Unable to add user, max user count reached\n",
3328
            eNB->Mod_id,frame, subframe);
3329
    }
3330
  }
3331
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_PRACH_RX,0);*/
3332 3333
}

3334
void pucch_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,int UE_id,int harq_pid,uint8_t do_srs)
Cedric Roux's avatar
Cedric Roux committed
3335
{
3336
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
3337
  uint8_t SR_payload = 0,*pucch_payload=NULL,pucch_payload0[2]= {0,0},pucch_payload1[2]= {0,0};
Cedric Roux's avatar
Cedric Roux committed
3338
  int16_t n1_pucch0 = -1, n1_pucch1 = -1, n1_pucch2 = -1, n1_pucch3 = -1;
3339 3340
  uint8_t do_SR = 0;
  uint8_t pucch_sel = 0;
3341
  int32_t metric0=0,metric1=0,metric0_SR=0;
3342 3343
  ANFBmode_t bundling_flag;
  PUCCH_FMT_t format;
3344 3345
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;
3346

3347 3348
  if ((eNB->dlsch[UE_id][0]) &&
      (eNB->dlsch[UE_id][0]->rnti>0) &&
Cedric Roux's avatar
Cedric Roux committed
3349
      (eNB->ulsch[UE_id]->harq_processes[harq_pid]->subframe_scheduling_flag==0)) {
3350

3351
    // check SR availability
3352 3353
    do_SR = is_SR_subframe(eNB,proc,UE_id);
    //      do_SR = 0;
Cedric Roux's avatar
Cedric Roux committed
3354

3355 3356
    // Now ACK/NAK
    // First check subframe_tx flag for earlier subframes
3357

3358
    get_n1_pucch_eNB(eNB,
Cedric Roux's avatar
Cedric Roux committed
3359 3360 3361 3362 3363 3364 3365
                     proc,
                     UE_id,
                     &n1_pucch0,
                     &n1_pucch1,
                     &n1_pucch2,
                     &n1_pucch3);

3366
    LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d, subframe %d Checking for PUCCH (%d,%d,%d,%d) SR %d\n",
Cedric Roux's avatar
Cedric Roux committed
3367 3368 3369 3370
          eNB->Mod_id,eNB->dlsch[UE_id][0]->rnti,
          frame,subframe,
          n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,do_SR);

3371 3372 3373
    if ((n1_pucch0==-1) && (n1_pucch1==-1) && (do_SR==0)) {  // no TX PDSCH that have to be checked and no SR for this UE_id
    } else {
      // otherwise we have some PUCCH detection to do
Cedric Roux's avatar
Cedric Roux committed
3374

3375 3376 3377
      // Null out PUCCH PRBs for noise measurement
      switch(fp->N_RB_UL) {
      case 6:
Cedric Roux's avatar
Cedric Roux committed
3378 3379
        eNB->rb_mask_ul[0] |= (0x1 | (1<<5)); //position 5
        break;
3380
      case 15:
Cedric Roux's avatar
Cedric Roux committed
3381 3382
        eNB->rb_mask_ul[0] |= (0x1 | (1<<14)); // position 14
        break;
3383
      case 25:
Cedric Roux's avatar
Cedric Roux committed
3384 3385
        eNB->rb_mask_ul[0] |= (0x1 | (1<<24)); // position 24
        break;
3386
      case 50:
Cedric Roux's avatar
Cedric Roux committed
3387 3388 3389
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[1] |= (1<<17); // position 49 (49-32)
        break;
3390
      case 75:
Cedric Roux's avatar
Cedric Roux committed
3391 3392 3393
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[2] |= (1<<10); // position 74 (74-64)
        break;
3394
      case 100:
Cedric Roux's avatar
Cedric Roux committed
3395 3396 3397
        eNB->rb_mask_ul[0] |= 0x1;
        eNB->rb_mask_ul[3] |= (1<<3); // position 99 (99-96)
        break;
3398
      default:
Cedric Roux's avatar
Cedric Roux committed
3399 3400
        LOG_E(PHY,"Unknown number for N_RB_UL %d\n",fp->N_RB_UL);
        break;
3401
      }
3402

3403
      if (do_SR == 1) {
Cedric Roux's avatar
Cedric Roux committed
3404 3405 3406 3407 3408 3409 3410 3411 3412
        eNB->UE_stats[UE_id].sr_total++;


        if (eNB->abstraction_flag == 0) {
          metric0_SR = rx_pucch(eNB,
                                pucch_format1,
                                UE_id,
                                eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
                                0, // n2_pucch
3413
                                do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
                                &SR_payload,
                                frame,
                                subframe,
                                PUCCH1_THRES);
          LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR is %d (SR n1pucch is %d)\n",
                eNB->Mod_id,
                eNB->ulsch[UE_id]->rnti,
                frame,
                subframe,
                SR_payload,
                eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex);
        }
3426
#ifdef PHY_ABSTRACTION
Cedric Roux's avatar
Cedric Roux committed
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
        else {
          metric0_SR = rx_pucch_emul(eNB,
                                     proc,
                                     UE_id,
                                     pucch_format1,
                                     0,
                                     &SR_payload);
          LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Checking SR (UE SR %d/%d)\n",eNB->Mod_id,
                eNB->ulsch[UE_id]->rnti,frame,subframe,SR_payload,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex);
        }
3437
#endif
3438
      }// do_SR==1
Cedric Roux's avatar
Cedric Roux committed
3439

3440
      if ((n1_pucch0==-1) && (n1_pucch1==-1)) { // just check for SR
Cedric Roux's avatar
Cedric Roux committed
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
      } else if (fp->frame_type==FDD) { // FDD
        // if SR was detected, use the n1_pucch from SR, else use n1_pucch0
        //          n1_pucch0 = (SR_payload==1) ? eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex:n1_pucch0;

        LOG_D(PHY,"Demodulating PUCCH for ACK/NAK: n1_pucch0 %d (%d), SR_payload %d\n",n1_pucch0,eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,SR_payload);

        if (eNB->abstraction_flag == 0) {
          metric0 = rx_pucch(eNB,
                             pucch_format1a,
                             UE_id,
                             (uint16_t)n1_pucch0,
                             0, //n2_pucch
3453
                             do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3454 3455 3456 3457 3458
                             pucch_payload0,
                             frame,
                             subframe,
                             PUCCH1a_THRES);
        }
3459
#ifdef PHY_ABSTRACTION
Cedric Roux's avatar
Cedric Roux committed
3460 3461 3462 3463 3464 3465 3466 3467
        else {
          metric0 = rx_pucch_emul(eNB,
                                  proc,
                                  UE_id,
                                  pucch_format1a,
                                  0,
                                  pucch_payload0);
        }
3468
#endif
Cedric Roux's avatar
Cedric Roux committed
3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482

        /* cancel SR detection if reception on n1_pucch0 is better than on SR PUCCH resource index */
        if (do_SR && metric0 > metric0_SR) SR_payload = 0;

        if (do_SR && metric0 <= metric0_SR) {
          /* when transmitting ACK/NACK on SR PUCCH resource index, SR payload is always 1 */
          SR_payload = 1;

          if (eNB->abstraction_flag == 0) {
            metric0=rx_pucch(eNB,
                             pucch_format1a,
                             UE_id,
                             eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
                             0, //n2_pucch
3483
                             do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
                             pucch_payload0,
                             frame,
                             subframe,
                             PUCCH1a_THRES);
          }
#ifdef PHY_ABSTRACTION
          else {
            metric0 = rx_pucch_emul(eNB,
                                    proc,
                                    UE_id,
                                    pucch_format1a,
                                    0,
                                    pucch_payload0);
          }
#endif
        }

3501
#ifdef DEBUG_PHY_PROC
Cedric Roux's avatar
Cedric Roux committed
3502 3503 3504 3505 3506
        LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d pucch1a (FDD) payload %d (metric %d)\n",
            eNB->Mod_id,
            eNB->dlsch[UE_id][0]->rnti,
            frame,subframe,
            pucch_payload0[0],metric0);
3507
#endif
3508

Cedric Roux's avatar
Cedric Roux committed
3509 3510 3511 3512 3513 3514 3515
        process_HARQ_feedback(UE_id,eNB,proc,
                            0,// pusch_flag
                            pucch_payload0,
                            2,
                            SR_payload);
      } // FDD
      else {  //TDD
3516

Cedric Roux's avatar
Cedric Roux committed
3517
        bundling_flag = eNB->pucch_config_dedicated[UE_id].tdd_AckNackFeedbackMode;
3518

Cedric Roux's avatar
Cedric Roux committed
3519
        // fix later for 2 TB case and format1b
Raymond Knopp's avatar
Raymond Knopp committed
3520

Cedric Roux's avatar
Cedric Roux committed
3521 3522
        if ((fp->frame_type==FDD) ||
          (bundling_flag==bundling)    ||
gabrielC's avatar
gabrielC committed
3523
          ((fp->frame_type==TDD)&&(fp->tdd_config==1)&&((subframe!=2)&&(subframe!=7)))) {
Cedric Roux's avatar
Cedric Roux committed
3524 3525 3526 3527
          format = pucch_format1a;
        } else {
          format = pucch_format1b;
        }
3528

Cedric Roux's avatar
Cedric Roux committed
3529 3530
        // if SR was detected, use the n1_pucch from SR
        if (SR_payload==1) {
3531
#ifdef DEBUG_PHY_PROC
Cedric Roux's avatar
Cedric Roux committed
3532 3533 3534 3535
          LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d with SR\n",eNB->Mod_id,
                eNB->dlsch[UE_id][0]->rnti,
                frame,subframe,
                n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
3536
#endif
3537

Cedric Roux's avatar
Cedric Roux committed
3538 3539 3540 3541 3542 3543
          if (eNB->abstraction_flag == 0)
            metric0 = rx_pucch(eNB,
                               format,
                               UE_id,
                               eNB->scheduling_request_config[UE_id].sr_PUCCH_ResourceIndex,
                               0, //n2_pucch
3544
                               do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3545 3546 3547 3548 3549
                               pucch_payload0,
                               frame,
                               subframe,
                               PUCCH1a_THRES);
          else {
3550
#ifdef PHY_ABSTRACTION
Cedric Roux's avatar
Cedric Roux committed
3551 3552 3553 3554 3555
            metric0 = rx_pucch_emul(eNB,proc,
                                    UE_id,
                                    format,
                                    0,
                                    pucch_payload0);
3556
#endif
Cedric Roux's avatar
Cedric Roux committed
3557 3558
          }
        } else { //using n1_pucch0/n1_pucch1 resources
3559
#ifdef DEBUG_PHY_PROC
Cedric Roux's avatar
Cedric Roux committed
3560 3561 3562 3563
          LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d Checking ACK/NAK (%d,%d,%d,%d) format %d\n",eNB->Mod_id,
                eNB->dlsch[UE_id][0]->rnti,
                frame,subframe,
                n1_pucch0,n1_pucch1,n1_pucch2,n1_pucch3,format);
3564
#endif
Cedric Roux's avatar
Cedric Roux committed
3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
          metric0=0;
          metric1=0;

          // Check n1_pucch0 metric
          if (n1_pucch0 != -1) {
            if (eNB->abstraction_flag == 0)
              metric0 = rx_pucch(eNB,
                                 format,
                                 UE_id,
                                 (uint16_t)n1_pucch0,
                                 0, // n2_pucch
3576
         do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3577 3578 3579 3580 3581
                                 pucch_payload0,
                                 frame,
                                 subframe,
                                 PUCCH1a_THRES);
            else {
3582
#ifdef PHY_ABSTRACTION
Cedric Roux's avatar
Cedric Roux committed
3583 3584 3585 3586 3587 3588
              metric0 = rx_pucch_emul(eNB,
                                      proc,
                                      UE_id,
                                      format,
                                      0,
                                      pucch_payload0);
3589
#endif
Cedric Roux's avatar
Cedric Roux committed
3590 3591
            }
          }
3592

Cedric Roux's avatar
Cedric Roux committed
3593 3594 3595 3596 3597 3598 3599 3600
          // Check n1_pucch1 metric
          if (n1_pucch1 != -1) {
            if (eNB->abstraction_flag == 0)
              metric1 = rx_pucch(eNB,
                                 format,
                                 UE_id,
                                 (uint16_t)n1_pucch1,
                                 0, //n2_pucch
3601
                                 do_srs, // shortened format
Cedric Roux's avatar
Cedric Roux committed
3602 3603 3604 3605 3606
                                 pucch_payload1,
                                 frame,
                                 subframe,
                                 PUCCH1a_THRES);
            else {
3607
#ifdef PHY_ABSTRACTION
Cedric Roux's avatar
Cedric Roux committed
3608 3609 3610 3611 3612 3613
              metric1 = rx_pucch_emul(eNB,
                                      proc,
                                      UE_id,
                                      format,
                                      1,
                                      pucch_payload1);
3614
#endif
Cedric Roux's avatar
Cedric Roux committed
3615 3616 3617
            }
          }
        }
3618

Cedric Roux's avatar
Cedric Roux committed
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
        if (SR_payload == 1) {
          pucch_payload = pucch_payload0;

          if (bundling_flag == bundling)
            pucch_sel = 2;
        } else if (bundling_flag == multiplexing) { // multiplexing + no SR
          pucch_payload = (metric1>metric0) ? pucch_payload1 : pucch_payload0;
          pucch_sel     = (metric1>metric0) ? 1 : 0;
        } else { // bundling + no SR
          if (n1_pucch1 != -1)
            pucch_payload = pucch_payload1;
          else if (n1_pucch0 != -1)
            pucch_payload = pucch_payload0;

          pucch_sel = 2;  // indicate that this is a bundled ACK/NAK
        }
3635

3636
#ifdef DEBUG_PHY_PROC
Cedric Roux's avatar
Cedric Roux committed
3637 3638 3639 3640
        LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d ACK/NAK metric 0 %d, metric 1 %d, sel %d, (%d,%d)\n",eNB->Mod_id,
              eNB->dlsch[UE_id][0]->rnti,
              frame,subframe,
              metric0,metric1,pucch_sel,pucch_payload[0],pucch_payload[1]);
3641
#endif
Cedric Roux's avatar
Cedric Roux committed
3642 3643 3644 3645 3646 3647
        process_HARQ_feedback(UE_id,eNB,proc,
                              0,// pusch_flag
                              pucch_payload,
                              pucch_sel,
                              SR_payload);
      } // TDD
3648
    }
Cedric Roux's avatar
Cedric Roux committed
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671

    if (SR_payload == 1) {
      LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d Got SR for PUSCH, transmitting to MAC\n",eNB->Mod_id,
            eNB->ulsch[UE_id]->rnti,frame,subframe);
      eNB->UE_stats[UE_id].sr_received++;

      if (eNB->first_sr[UE_id] == 1) { // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
        eNB->first_sr[UE_id] = 0;
        eNB->dlsch[UE_id][0]->harq_processes[0]->round=0;
        eNB->dlsch[UE_id][0]->harq_processes[0]->status=SCH_IDLE;
        LOG_D(PHY,"[eNB %d][SR %x] Frame %d subframe %d First SR\n",
              eNB->Mod_id,
              eNB->ulsch[UE_id]->rnti,frame,subframe);
      }

      if (eNB->mac_enabled==1) {
        mac_xface->SR_indication(eNB->Mod_id,
                                 eNB->CC_id,
                                 frame,
                                 eNB->dlsch[UE_id][0]->rnti,subframe);
      }
    }
  }
3672
}
3673

3674

3675
void cba_procedures(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,int UE_id,int harq_pid) {
3676

3677 3678
  uint8_t access_mode;
  int num_active_cba_groups;
3679 3680
  const int subframe = proc->subframe_rx;
  const int frame = proc->frame_rx;
3681 3682
  uint16_t rnti=0;
  int ret=0;
3683
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
3684

3685 3686
  if (eNB->ulsch[UE_id]==NULL) return;

3687
  num_active_cba_groups = eNB->ulsch[UE_id]->num_active_cba_groups;
3688 3689
 
  if ((num_active_cba_groups > 0) &&
3690 3691
      (eNB->ulsch[UE_id]->cba_rnti[UE_id%num_active_cba_groups]>0) &&
      (eNB->ulsch[UE_id]->harq_processes[harq_pid]->subframe_cba_scheduling_flag==1)) {
3692 3693 3694 3695
    rnti=0;
    
#ifdef DEBUG_PHY_PROC
    LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d Checking PUSCH/ULSCH CBA Reception for UE %d with cba rnti %x mode %s\n",
3696 3697 3698
    eNB->Mod_id,harq_pid,
    frame,subframe,
    UE_id, (uint16_t)eNB->ulsch[UE_id]->cba_rnti[UE_id%num_active_cba_groups],mode_string[eNB->UE_stats[UE_id].mode]);
3699
#endif
3700
    
3701
    if (eNB->abstraction_flag==0) {
3702
      rx_ulsch(eNB,proc,
3703 3704 3705 3706
         eNB->UE_stats[UE_id].sector,  // this is the effective sector id
         UE_id,
         eNB->ulsch,
         0);
3707 3708 3709 3710
    }
    
#ifdef PHY_ABSTRACTION
    else {
3711
      rx_ulsch_emul(eNB,proc,
3712 3713
        eNB->UE_stats[UE_id].sector,  // this is the effective sector id
        UE_id);
3714 3715
    }
    
3716
#endif
3717
    
3718
    if (eNB->abstraction_flag == 0) {
3719
      ret = ulsch_decoding(eNB,proc,
3720 3721 3722 3723
         UE_id,
         0, // control_only_flag
         eNB->ulsch[UE_id]->harq_processes[harq_pid]->V_UL_DAI,
         eNB->ulsch[UE_id]->harq_processes[harq_pid]->nb_rb>20 ? 1 : 0);
3724 3725 3726 3727
    }
    
#ifdef PHY_ABSTRACTION
    else {
3728
      ret = ulsch_decoding_emul(eNB,
3729 3730 3731
        proc,
        UE_id,
        &rnti);
3732 3733 3734 3735
    }
    
#endif
    
3736
    if (eNB->ulsch[UE_id]->harq_processes[harq_pid]->cqi_crc_status == 1) {
3737
#ifdef DEBUG_PHY_PROC
3738
      
3739
      print_CQI(eNB->ulsch[UE_id]->harq_processes[harq_pid]->o,eNB->ulsch[UE_id]->harq_processes[harq_pid]->uci_format,0,fp->N_RB_DL);
3740 3741
#endif
      access_mode = UNKNOWN_ACCESS;
3742
      extract_CQI(eNB->ulsch[UE_id]->harq_processes[harq_pid]->o,
3743 3744 3745 3746
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->uci_format,
      &eNB->UE_stats[UE_id],
      fp->N_RB_DL,
      &rnti, &access_mode);
3747
      eNB->UE_stats[UE_id].rank = eNB->ulsch[UE_id]->harq_processes[harq_pid]->o_RI[0];
3748 3749
    }
    
3750 3751
    eNB->ulsch[UE_id]->harq_processes[harq_pid]->subframe_cba_scheduling_flag=0;
    eNB->ulsch[UE_id]->harq_processes[harq_pid]->status= SCH_IDLE;
3752
      
3753
    if ((num_active_cba_groups > 0) &&
3754 3755 3756
  (UE_id + num_active_cba_groups < NUMBER_OF_UE_MAX) &&
  (eNB->ulsch[UE_id+num_active_cba_groups]->cba_rnti[UE_id%num_active_cba_groups] > 0 ) &&
  (eNB->ulsch[UE_id+num_active_cba_groups]->num_active_cba_groups> 0)) {
3757
#ifdef DEBUG_PHY_PROC
3758
      LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d UE %d harq_pid %d resetting the subframe_scheduling_flag for Ue %d cba groups %d members\n",
3759 3760
      eNB->Mod_id,harq_pid,frame,subframe,UE_id,harq_pid,
      UE_id+num_active_cba_groups, UE_id%eNB->ulsch[UE_id]->num_active_cba_groups);
3761
#endif
3762 3763 3764 3765
      eNB->ulsch[UE_id+num_active_cba_groups]->harq_processes[harq_pid]->subframe_cba_scheduling_flag=1;
      eNB->ulsch[UE_id+num_active_cba_groups]->harq_processes[harq_pid]->status= CBA_ACTIVE;
      eNB->ulsch[UE_id+num_active_cba_groups]->harq_processes[harq_pid]->TBS=eNB->ulsch[UE_id]->harq_processes[harq_pid]->TBS;
    }
3766

3767 3768 3769 3770 3771 3772 3773 3774
    if (ret == (1+MAX_TURBO_ITERATIONS)) {
      eNB->UE_stats[UE_id].ulsch_round_errors[harq_pid][eNB->ulsch[UE_id]->harq_processes[harq_pid]->round]++;
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->phich_active = 1;
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->phich_ACK = 0;
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->round++;
    } // ulsch in error
    else {
      LOG_D(PHY,"[eNB %d][PUSCH %d] Frame %d subframe %d ULSCH received, setting round to 0, PHICH ACK\n",
3775 3776
      eNB->Mod_id,harq_pid,
      frame,subframe);
3777 3778 3779 3780 3781

      eNB->ulsch[UE_id]->harq_processes[harq_pid]->phich_active = 1;
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->phich_ACK = 1;
      eNB->ulsch[UE_id]->harq_processes[harq_pid]->round = 0;
      eNB->UE_stats[UE_id].ulsch_consecutive_errors = 0;
3782 3783
#ifdef DEBUG_PHY_PROC
#ifdef DEBUG_ULSCH
3784
      LOG_D(PHY,"[eNB] Frame %d, Subframe %d : ULSCH SDU (RX harq_pid %d) %d bytes:",
3785 3786
      frame,subframe,
      harq_pid,eNB->ulsch[UE_id]->harq_processes[harq_pid]->TBS>>3);
3787 3788

      for (j=0; j<eNB->ulsch[UE_id]->harq_processes[harq_pid]->TBS>>3; j++)
3789
  LOG_T(PHY,"%x.",eNB->ulsch[UE_id]->harq_processes[harq_pid]->b[j]);
3790 3791 3792 3793 3794 3795

      LOG_T(PHY,"\n");
#endif
#endif

      if (access_mode > UNKNOWN_ACCESS) {
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
  LOG_D(PHY,"[eNB %d] Frame %d, Subframe %d : received ULSCH SDU from CBA transmission, UE (%d,%x), CBA (group %d, rnti %x)\n",
        eNB->Mod_id, frame,subframe,
        UE_id, eNB->ulsch[UE_id]->rnti,
        UE_id % eNB->ulsch[UE_id]->num_active_cba_groups, eNB->ulsch[UE_id]->cba_rnti[UE_id%num_active_cba_groups]);

  // detect if there is a CBA collision
  if ((eNB->cba_last_reception[UE_id%num_active_cba_groups] == 0 ) && 
      (eNB->mac_enabled==1)) {
    mac_xface->rx_sdu(eNB->Mod_id,
          eNB->CC_id,
          frame,subframe,
          eNB->ulsch[UE_id]->rnti,
          eNB->ulsch[UE_id]->harq_processes[harq_pid]->b,
          eNB->ulsch[UE_id]->harq_processes[harq_pid]->TBS>>3,
          harq_pid,
          NULL);

    eNB->cba_last_reception[UE_id%num_active_cba_groups]+=1;//(subframe);
  } else {
    if (eNB->cba_last_reception[UE_id%num_active_cba_groups] == 1 )
      LOG_N(PHY,"[eNB%d] Frame %d subframe %d : first CBA collision detected \n ",
      eNB->Mod_id,frame,subframe);

    LOG_N(PHY,"[eNB%d] Frame %d subframe %d : CBA collision set SR for UE %d in group %d \n ",
    eNB->Mod_id,frame,subframe,
    eNB->cba_last_reception[UE_id%num_active_cba_groups],UE_id%num_active_cba_groups );

    eNB->cba_last_reception[UE_id%num_active_cba_groups]+=1;

    mac_xface->SR_indication(eNB->Mod_id,
           eNB->CC_id,
           frame,
           eNB->dlsch[UE_id][0]->rnti,subframe);
  }
3830 3831
      } // UNKNOWN_ACCESS
    } // ULSCH CBA not in error
3832
  }
3833

3834
}
3835

3836 3837 3838 3839
typedef struct {
  PHY_VARS_eNB *eNB;
  int slot;
} fep_task;
3840

3841
void fep0(PHY_VARS_eNB *eNB,int slot) {
3842

3843 3844 3845
  eNB_proc_t *proc       = &eNB->proc;
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  int l;
3846

Raymond Knopp's avatar
Raymond Knopp committed
3847
  //  printf("fep0: slot %d\n",slot);
3848

3849 3850 3851
  remove_7_5_kHz(eNB,(slot&1)+(proc->subframe_rx<<1));
  for (l=0; l<fp->symbols_per_tti/2; l++) {
    slot_fep_ul(fp,
3852 3853 3854 3855 3856 3857
    &eNB->common_vars,
    l,
    (slot&1)+(proc->subframe_rx<<1),
    0,
    0
    );
3858 3859
  }
}
3860

3861

Raymond Knopp's avatar
Raymond Knopp committed
3862

3863 3864 3865 3866
extern int oai_exit;

static void *fep_thread(void *param) {

3867
  pthread_setname_np( pthread_self(), "UEfep");
3868 3869 3870
  PHY_VARS_eNB *eNB = (PHY_VARS_eNB *)param;
  eNB_proc_t *proc  = &eNB->proc;
  while (!oai_exit) {
Raymond Knopp's avatar
Raymond Knopp committed
3871

3872 3873 3874
    if (wait_on_condition(&proc->mutex_fep,&proc->cond_fep,&proc->instance_cnt_fep,"fep thread")<0) break;  
    fep0(eNB,0);
    if (release_thread(&proc->mutex_fep,&proc->instance_cnt_fep,"fep thread")<0) break;
Raymond Knopp's avatar
Raymond Knopp committed
3875 3876 3877 3878

    if (pthread_cond_signal(&proc->cond_fep) != 0) {
      printf("[eNB] ERROR pthread_cond_signal for fep thread exit\n");
      exit_fun( "ERROR pthread_cond_signal" );
3879
      return NULL;
Raymond Knopp's avatar
Raymond Knopp committed
3880
    }
3881
  }
Raymond Knopp's avatar
Raymond Knopp committed
3882 3883 3884 3885 3886 3887



  return(NULL);
}

3888
void init_fep_thread(PHY_VARS_eNB *eNB,pthread_attr_t *attr_fep) {
Raymond Knopp's avatar
Raymond Knopp committed
3889

3890
  eNB_proc_t *proc = &eNB->proc;
Raymond Knopp's avatar
Raymond Knopp committed
3891

3892 3893 3894 3895 3896 3897
  proc->instance_cnt_fep         = -1;
    
  pthread_mutex_init( &proc->mutex_fep, NULL);
  pthread_cond_init( &proc->cond_fep, NULL);

  pthread_create(&proc->pthread_fep, attr_fep, fep_thread, (void*)eNB);
Raymond Knopp's avatar
Raymond Knopp committed
3898 3899


3900 3901
}

3902
/*
3903 3904 3905
extern void *td_thread(void*);

void init_td_thread(PHY_VARS_eNB *eNB,pthread_attr_t *attr_td) {
3906 3907 3908

  eNB_proc_t *proc = &eNB->proc;

3909 3910
  proc->tdp.eNB = eNB;
  proc->instance_cnt_td         = -1;
3911
    
3912 3913
  pthread_mutex_init( &proc->mutex_td, NULL);
  pthread_cond_init( &proc->cond_td, NULL);
3914

3915 3916 3917 3918
  pthread_create(&proc->pthread_td, attr_td, td_thread, (void*)&proc->tdp);

}

3919

3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
extern void *te_thread(void*);

void init_te_thread(PHY_VARS_eNB *eNB,pthread_attr_t *attr_te) {

  eNB_proc_t *proc = &eNB->proc;

  proc->tep.eNB = eNB;
  proc->instance_cnt_te         = -1;
    
  pthread_mutex_init( &proc->mutex_te, NULL);
  pthread_cond_init( &proc->cond_te, NULL);

  printf("Creating te_thread\n");
  pthread_create(&proc->pthread_te, attr_te, te_thread, (void*)&proc->tep);
3934 3935

}
3936
*/
Raymond Knopp's avatar
Raymond Knopp committed
3937

3938
void eNB_fep_full_2thread(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc_rxtx) {
3939 3940

  eNB_proc_t *proc = &eNB->proc;
Raymond Knopp's avatar
Raymond Knopp committed
3941

3942
  struct timespec wait;
3943

3944 3945 3946 3947 3948 3949 3950
  wait.tv_sec=0;
  wait.tv_nsec=5000000L;

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_SLOT_FEP,1);
  start_meas(&eNB->ofdm_demod_stats);

  if (pthread_mutex_timedlock(&proc->mutex_fep,&wait) != 0) {
3951
    printf("[eNB] ERROR pthread_mutex_lock for fep thread (IC %d)\n", proc->instance_cnt_fep);
3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
    exit_fun( "error locking mutex_fep" );
    return;
  }

  if (proc->instance_cnt_fep==0) {
    printf("[eNB] FEP thread busy\n");
    exit_fun("FEP thread busy");
    pthread_mutex_unlock( &proc->mutex_fep );
    return;
  }
  
  ++proc->instance_cnt_fep;

3965

3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
  if (pthread_cond_signal(&proc->cond_fep) != 0) {
    printf("[eNB] ERROR pthread_cond_signal for fep thread\n");
    exit_fun( "ERROR pthread_cond_signal" );
    return;
  }
  
  pthread_mutex_unlock( &proc->mutex_fep );

  // call second slot in this symbol
  fep0(eNB,1);

Raymond Knopp's avatar
Raymond Knopp committed
3977
  wait_on_busy_condition(&proc->mutex_fep,&proc->cond_fep,&proc->instance_cnt_fep,"fep thread");  
3978

3979
  stop_meas(&eNB->ofdm_demod_stats);
3980 3981
}

Raymond Knopp's avatar
Raymond Knopp committed
3982 3983


3984
void eNB_fep_full(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc_rxtx) {
3985

3986
  int l;
3987
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
3988

3989
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_SLOT_FEP,1);
3990
  start_meas(&eNB->ofdm_demod_stats);
3991 3992
  remove_7_5_kHz(eNB,proc_rxtx->subframe_rx<<1);
  remove_7_5_kHz(eNB,1+(proc_rxtx->subframe_rx<<1));
3993 3994
  for (l=0; l<fp->symbols_per_tti/2; l++) {
    slot_fep_ul(fp,
3995 3996 3997 3998 3999 4000
    &eNB->common_vars,
    l,
    (proc_rxtx->subframe_rx)<<1,
    0,
    0
    );
4001
    slot_fep_ul(fp,
4002 4003 4004 4005 4006 4007
    &eNB->common_vars,
    l,
    1+((proc_rxtx->subframe_rx)<<1),
    0,
    0
    );
4008
  }
4009
  stop_meas(&eNB->ofdm_demod_stats);
4010 4011 4012 4013 4014
  
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_SLOT_FEP,0);
  
  if (eNB->node_function == NGFI_RRU_IF4p5) {
    /// **** send_IF4 of rxdataF to RCC (no prach now) **** ///
4015
    LOG_D(PHY,"send_IF4p5 (PULFFT): frame %d, subframe %d\n",proc_rxtx->frame_rx,proc_rxtx->subframe_rx);
4016
    send_IF4p5(eNB, proc_rxtx->frame_rx, proc_rxtx->subframe_rx, IF4p5_PULFFT, 0);
4017 4018 4019 4020
    start_fh_prev = start_fh;
    start_fh_prev_sf = start_fh_sf;
    clock_gettime( CLOCK_MONOTONIC, &start_fh);
    start_fh_sf = proc_rxtx->subframe_rx;
Raymond Knopp's avatar
Raymond Knopp committed
4021
  }    
4022 4023
}

4024
void eNB_fep_rru_if5(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc_rxtx) {
4025

Raymond Knopp's avatar
Raymond Knopp committed
4026
  eNB_proc_t *proc=&eNB->proc;
4027 4028 4029 4030 4031
  uint8_t seqno=0;

  /// **** send_IF5 of rxdata to BBU **** ///       
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 1 );  
  send_IF5(eNB, proc->timestamp_rx, proc->subframe_rx, &seqno, IF5_RRH_GW_UL);
4032 4033 4034 4035
  start_fh_prev = start_fh;
  start_fh_prev_sf = start_fh_sf;
  clock_gettime( CLOCK_MONOTONIC, &start_fh);
  start_fh_sf = proc->subframe_rx;
4036 4037 4038 4039
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_SEND_IF5, 0 );          

}

4040
void do_prach(PHY_VARS_eNB *eNB,int frame,int subframe) {
4041

Raymond Knopp's avatar
Raymond Knopp committed
4042
  eNB_proc_t *proc = &eNB->proc;
Matthieu Kanj's avatar
Matthieu Kanj committed
4043
 // LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
4044

4045 4046
if(frame%2==0 && subframe==9)
{
4047
  // check if we have to detect PRACH first
4048
  //if (is_prach_subframe(fp,frame,subframe)>0) { 
4049 4050 4051
    /* accept some delay in processing - up to 5ms */
    int i;
    for (i = 0; i < 10 && proc->instance_cnt_prach == 0; i++) {
4052
      LOG_W(PHY,"[eNB] Frame %d Subframe %d, eNB PRACH thread busy (IC %d)!!\n", frame,subframe,proc->instance_cnt_prach);
4053 4054 4055 4056 4057 4058 4059 4060 4061
      usleep(500);
    }
    if (proc->instance_cnt_prach == 0) {
      exit_fun( "PRACH thread busy" );
      return;
    }
    
    // wake up thread for PRACH RX
    if (pthread_mutex_lock(&proc->mutex_prach) != 0) {
Cedric Roux's avatar
Cedric Roux committed
4062
      LOG_E( PHY, "[eNB] ERROR pthread_mutex_lock for eNB PRACH thread %d (IC %d)\n", proc->thread_index, proc->instance_cnt_prach);
4063 4064 4065 4066 4067 4068
      exit_fun( "error locking mutex_prach" );
      return;
    }
    
    ++proc->instance_cnt_prach;
    // set timing for prach thread
4069 4070
    proc->frame_prach = frame;
    proc->subframe_prach = subframe;
4071 4072 4073 4074 4075 4076 4077 4078 4079
    
    // the thread can now be woken up
    if (pthread_cond_signal(&proc->cond_prach) != 0) {
      LOG_E( PHY, "[eNB] ERROR pthread_cond_signal for eNB PRACH thread %d\n", proc->thread_index);
      exit_fun( "ERROR pthread_cond_signal" );
      return;
    }
    
    pthread_mutex_unlock( &proc->mutex_prach );
4080 4081
  //}
 }
4082

4083
}
4084

4085
void phy_procedures_eNB_common_RX(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc){
4086 4087


4088
  //  eNB_proc_t *proc       = &eNB->proc;
Raymond Knopp's avatar
Raymond Knopp committed
4089 4090 4091
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  const int subframe     = proc->subframe_rx;
  const int frame        = proc->frame_rx;
Matthieu Kanj's avatar
Matthieu Kanj committed
4092
  // int offset             = (eNB->single_thread_flag==1) ? 0 : (subframe&1);
Sandeep Kumar's avatar
Sandeep Kumar committed
4093

4094 4095
 /// VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_ENB+offset, proc->frame_rx );
 /// VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_ENB+offset, proc->subframe_rx );
4096

Raymond Knopp's avatar
Raymond Knopp committed
4097 4098 4099 4100 4101 4102 4103 4104
  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)!=SF_UL)) {

    if (eNB->node_function == NGFI_RRU_IF4p5) {
      /// **** in TDD during DL send_IF4 of ULTICK to RCC **** ///
      send_IF4p5(eNB, proc->frame_rx, proc->subframe_rx, IF4p5_PULTICK, 0);
    }    
    return;
  }
4105

4106

4107 4108
  ///VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_COMMON+offset, 1 ); 
  /// start_meas(&eNB->phy_proc_rx);
4109
  LOG_D(PHY,"[eNB %d] Frame %d: Doing phy_procedures_eNB_common_RX(%d)\n",eNB->Mod_id,frame,subframe);
4110

Raymond Knopp's avatar
Raymond Knopp committed
4111

4112
  if (eNB->fep) eNB->fep(eNB,proc);
4113

4114
  ///VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_COMMON+offset, 0 );
4115 4116
}

4117

4118
void phy_procedures_eNB_uespec_RX(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,const relaying_type_t r_type)
4119
{
4120
  //RX processing for ue-specific resources (i
4121
  UNUSED(r_type);
4122
  uint32_t ret=0,i,j,k;
4123 4124 4125 4126 4127
  uint32_t harq_pid, harq_idx, round;
  uint8_t nPRS;
  int sync_pos;
  uint16_t rnti=0;
  uint8_t access_mode;
4128
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
4129

4130
  const int subframe = proc->subframe_rx;
Raymond Knopp's avatar
Raymond Knopp committed
4131
  const int frame    = proc->frame_rx;
4132
  int offset         = eNB->CC_id;//(proc == &eNB->proc.proc_rxtx[0]) ? 0 : 1;
4133

4134 4135 4136 4137 4138
  uint16_t srsPeriodicity;
  uint16_t srsOffset;
  uint16_t do_srs=0;
  uint16_t is_srs_pos=0;

4139 4140 4141 4142 4143
  T(T_ENB_PHY_UL_TICK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe));

  T(T_ENB_PHY_INPUT_SIGNAL, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(0),
    T_BUFFER(&eNB->common_vars.rxdata[0][0][subframe*eNB->frame_parms.samples_per_tti],
             eNB->frame_parms.samples_per_tti * 4));
4144

4145
  if ((fp->frame_type == TDD) && (subframe_select(fp,subframe)!=SF_UL)) return;
4146

Raymond Knopp's avatar
Raymond Knopp committed
4147
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_UESPEC+offset, 1 );
4148

4149
#ifdef DEBUG_PHY_PROC
4150
  LOG_D(PHY,"[eNB %d] Frame %d: Doing phy_procedures_eNB_uespec_RX(%d)\n",eNB->Mod_id,frame, subframe);
Lionel Gauthier's avatar
Lionel Gauthier committed
4151
#endif
4152

4153

4154 4155 4156 4157
  eNB->rb_mask_ul[0]=0;
  eNB->rb_mask_ul[1]=0;
  eNB->rb_mask_ul[2]=0;
  eNB->rb_mask_ul[3]=0;
4158

4159
  // Check for active processes in current subframe
4160
  harq_pid = subframe2harq_pid(fp,
4161
                               frame,subframe);
4162

4163 4164
  // reset the cba flag used for collision detection
  for (i=0; i < NUM_MAX_CBA_GROUP; i++) {
4165
    eNB->cba_last_reception[i]=0;
4166
  }
4167

4168 4169
  is_srs_pos = is_srs_occasion_common(fp,frame,subframe);
  
4170
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
4171

4172 4173 4174
    // Do SRS processing 
    // check if there is SRS and we have to use shortened format
    // TODO: check for exceptions in transmission of SRS together with ACK/NACK
4175
    do_srs=0;
4176 4177
    if (is_srs_pos && eNB->soundingrs_ul_config_dedicated[i].srsConfigDedicatedSetup ) {
      compute_srs_pos(fp->frame_type, eNB->soundingrs_ul_config_dedicated[i].srs_ConfigIndex, &srsPeriodicity, &srsOffset);
4178
      if (((10*frame+subframe) % srsPeriodicity) == srsOffset) {
4179
  do_srs = 1;
4180 4181 4182 4183 4184
      }
    }

    if (do_srs==1) {
      if (lte_srs_channel_estimation(fp,
4185 4186 4187 4188 4189 4190
             &eNB->common_vars,
             &eNB->srs_vars[i],
             &eNB->soundingrs_ul_config_dedicated[i],
             subframe,
             0/*eNB_id*/)) {
  LOG_E(PHY,"problem processing SRS\n");
4191 4192 4193 4194 4195 4196 4197
      }
    }

    // Do PUCCH processing 

    pucch_procedures(eNB,proc,i,harq_pid, do_srs);

4198

4199
    // check for Msg3
4200
    if (eNB->mac_enabled==1) {
4201
      if (eNB->UE_stats[i].mode == RA_RESPONSE) {
4202
  process_Msg3(eNB,proc,i,harq_pid);
4203 4204
      }
    }
4205 4206


4207 4208 4209
    eNB->pusch_stats_rb[i][(frame*10)+subframe] = -63;
    eNB->pusch_stats_round[i][(frame*10)+subframe] = 0;
    eNB->pusch_stats_mcs[i][(frame*10)+subframe] = -63;
4210

4211 4212 4213
    if ((eNB->ulsch[i]) &&
        (eNB->ulsch[i]->rnti>0) &&
        (eNB->ulsch[i]->harq_processes[harq_pid]->subframe_scheduling_flag==1)) {
4214
      // UE is has ULSCH scheduling
4215
      round = eNB->ulsch[i]->harq_processes[harq_pid]->round;
4216
 
4217
      for (int rb=0;
4218
           rb<=eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb;
4219 4220 4221
     rb++) {
  int rb2 = rb+eNB->ulsch[i]->harq_processes[harq_pid]->first_rb;
  eNB->rb_mask_ul[rb2>>5] |= (1<<(rb2&31));
4222
      }
4223 4224


4225
      if (eNB->ulsch[i]->Msg3_flag == 1) {
4226
        LOG_D(PHY,"[eNB %d] frame %d, subframe %d: Scheduling ULSCH Reception for Msg3 in Sector %d\n",
4227
              eNB->Mod_id,
4228 4229
              frame,
              subframe,
4230
              eNB->UE_stats[i].sector);
4231
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_MSG3,1);
4232
      } else {
4233

4234
        LOG_D(PHY,"[eNB %d] frame %d, subframe %d: Scheduling ULSCH Reception for UE %d Mode %s\n",
4235
              eNB->Mod_id,
4236 4237 4238
              frame,
              subframe,
              i,
4239
              mode_string[eNB->UE_stats[i].mode]);
4240
      }
4241

4242

4243
      nPRS = fp->pusch_config_common.ul_ReferenceSignalsPUSCH.nPRS[subframe<<1];
4244

4245
      eNB->ulsch[i]->cyclicShift = (eNB->ulsch[i]->harq_processes[harq_pid]->n_DMRS2 + fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift +
4246
            nPRS)%12;
4247

4248
      if (fp->frame_type == FDD ) {
4249
        int sf = (subframe<4) ? (subframe+6) : (subframe-4);
4250

4251 4252
        if (eNB->dlsch[i][0]->subframe_tx[sf]>0) { // we have downlink transmission
          eNB->ulsch[i]->harq_processes[harq_pid]->O_ACK = 1;
4253
        } else {
4254
          eNB->ulsch[i]->harq_processes[harq_pid]->O_ACK = 0;
4255
        }
4256
      }
4257

4258 4259
      LOG_D(PHY,
            "[eNB %d][PUSCH %d] Frame %d Subframe %d Demodulating PUSCH: dci_alloc %d, rar_alloc %d, round %d, first_rb %d, nb_rb %d, mcs %d, TBS %d, rv %d, cyclic_shift %d (n_DMRS2 %d, cyclicShift_common %d, nprs %d), O_ACK %d \n",
4260
            eNB->Mod_id,harq_pid,frame,subframe,
4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
            eNB->ulsch[i]->harq_processes[harq_pid]->dci_alloc,
            eNB->ulsch[i]->harq_processes[harq_pid]->rar_alloc,
            eNB->ulsch[i]->harq_processes[harq_pid]->round,
            eNB->ulsch[i]->harq_processes[harq_pid]->first_rb,
            eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb,
            eNB->ulsch[i]->harq_processes[harq_pid]->mcs,
            eNB->ulsch[i]->harq_processes[harq_pid]->TBS,
            eNB->ulsch[i]->harq_processes[harq_pid]->rvidx,
            eNB->ulsch[i]->cyclicShift,
            eNB->ulsch[i]->harq_processes[harq_pid]->n_DMRS2,
4271
            fp->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift,
4272
            nPRS,
4273 4274 4275 4276
            eNB->ulsch[i]->harq_processes[harq_pid]->O_ACK);
      eNB->pusch_stats_rb[i][(frame*10)+subframe] = eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb;
      eNB->pusch_stats_round[i][(frame*10)+subframe] = eNB->ulsch[i]->harq_processes[harq_pid]->round;
      eNB->pusch_stats_mcs[i][(frame*10)+subframe] = eNB->ulsch[i]->harq_processes[harq_pid]->mcs;
4277
      start_meas(&eNB->ulsch_demodulation_stats);
4278

4279
      if (eNB->abstraction_flag==0) {
4280
        rx_ulsch(eNB,proc,
4281
                 eNB->UE_stats[i].sector,  // this is the effective sector id
4282
                 i,
4283
                 eNB->ulsch,
4284 4285
                 0);
      }
4286

4287 4288
#ifdef PHY_ABSTRACTION
      else {
4289
        rx_ulsch_emul(eNB,proc,
4290
                      eNB->UE_stats[i].sector,  // this is the effective sector id
4291 4292
                      i);
      }
4293 4294

#endif
4295
      stop_meas(&eNB->ulsch_demodulation_stats);
4296

4297

4298
      start_meas(&eNB->ulsch_decoding_stats);
4299

4300
      if (eNB->abstraction_flag == 0) {
4301
        ret = ulsch_decoding(eNB,proc,
4302 4303
                             i,
                             0, // control_only_flag
4304
                             eNB->ulsch[i]->harq_processes[harq_pid]->V_UL_DAI,
4305
           eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb>20 ? 1 : 0);
4306
      }
4307

4308
#ifdef PHY_ABSTRACTION
4309
      else {
4310
        ret = ulsch_decoding_emul(eNB,
4311
          proc,
4312 4313 4314
                                  i,
                                  &rnti);
      }
4315

4316
#endif
4317
      stop_meas(&eNB->ulsch_decoding_stats);
4318

4319
      LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d RNTI %x RX power (%d,%d) RSSI (%d,%d) N0 (%d,%d) dB ACK (%d,%d), decoding iter %d\n",
4320
            eNB->Mod_id,harq_pid,
4321
            frame,subframe,
4322 4323 4324 4325 4326 4327 4328 4329 4330
            eNB->ulsch[i]->rnti,
            dB_fixed(eNB->pusch_vars[i]->ulsch_power[0]),
            dB_fixed(eNB->pusch_vars[i]->ulsch_power[1]),
            eNB->UE_stats[i].UL_rssi[0],
            eNB->UE_stats[i].UL_rssi[1],
            eNB->measurements->n0_power_dB[0],
            eNB->measurements->n0_power_dB[1],
            eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[0],
            eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[1],
4331
            ret);
4332

4333
      //compute the expected ULSCH RX power (for the stats)
4334
      eNB->ulsch[(uint32_t)i]->harq_processes[harq_pid]->delta_TF =
4335
        get_hundred_times_delta_IF_eNB(eNB,i,harq_pid, 0); // 0 means bw_factor is not considered
4336

4337
      eNB->UE_stats[i].ulsch_decoding_attempts[harq_pid][eNB->ulsch[i]->harq_processes[harq_pid]->round]++;
4338 4339
#ifdef DEBUG_PHY_PROC
      LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d UE %d harq_pid %d Clearing subframe_scheduling_flag\n",
4340
            eNB->Mod_id,harq_pid,frame,subframe,i,harq_pid);
4341
#endif
4342
      eNB->ulsch[i]->harq_processes[harq_pid]->subframe_scheduling_flag=0;
4343

4344
      if (eNB->ulsch[i]->harq_processes[harq_pid]->cqi_crc_status == 1) {
4345
#ifdef DEBUG_PHY_PROC
4346
        //if (((frame%10) == 0) || (frame < 50))
4347
        print_CQI(eNB->ulsch[i]->harq_processes[harq_pid]->o,eNB->ulsch[i]->harq_processes[harq_pid]->uci_format,0,fp->N_RB_DL);
4348
#endif
4349 4350 4351
        extract_CQI(eNB->ulsch[i]->harq_processes[harq_pid]->o,
                    eNB->ulsch[i]->harq_processes[harq_pid]->uci_format,
                    &eNB->UE_stats[i],
4352
                    fp->N_RB_DL,
4353
                    &rnti, &access_mode);
4354
        eNB->UE_stats[i].rank = eNB->ulsch[i]->harq_processes[harq_pid]->o_RI[0];
4355

4356
      }
4357

4358
      if (eNB->ulsch[i]->Msg3_flag == 1)
4359
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_MSG3,0);
4360

4361
      if (ret == (1+MAX_TURBO_ITERATIONS)) {
4362
        T(T_ENB_PHY_ULSCH_UE_NACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(i), T_INT(eNB->ulsch[i]->rnti),
4363
          T_INT(harq_pid));
4364

4365 4366 4367 4368
        eNB->UE_stats[i].ulsch_round_errors[harq_pid][eNB->ulsch[i]->harq_processes[harq_pid]->round]++;
        eNB->ulsch[i]->harq_processes[harq_pid]->phich_active = 1;
        eNB->ulsch[i]->harq_processes[harq_pid]->phich_ACK = 0;
        eNB->ulsch[i]->harq_processes[harq_pid]->round++;
4369

4370
        LOG_D(PHY,"[eNB][PUSCH %d] Increasing to round %d\n",harq_pid,eNB->ulsch[i]->harq_processes[harq_pid]->round);
4371

4372
        if (eNB->ulsch[i]->Msg3_flag == 1) {
4373

4374
          LOG_D(PHY,"[eNB %d/%d][RAPROC] frame %d, subframe %d, UE %d: Error receiving ULSCH (Msg3), round %d/%d\n",
4375 4376
                eNB->Mod_id,
                eNB->CC_id,
4377
                frame,subframe, i,
4378
                eNB->ulsch[i]->harq_processes[harq_pid]->round-1,
4379
                fp->maxHARQ_Msg3Tx-1);
4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
    /*dump_ulsch(eNB,proc,i);
      exit(-1);*/

    LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d RNTI %x RX power (%d,%d) RSSI (%d,%d) N0 (%d,%d) dB ACK (%d,%d), decoding iter %d\n",
    eNB->Mod_id,harq_pid,
    frame,subframe,
    eNB->ulsch[i]->rnti,
    dB_fixed(eNB->pusch_vars[i]->ulsch_power[0]),
    dB_fixed(eNB->pusch_vars[i]->ulsch_power[1]),
    eNB->UE_stats[i].UL_rssi[0],
    eNB->UE_stats[i].UL_rssi[1],
    eNB->measurements->n0_power_dB[0],
    eNB->measurements->n0_power_dB[1],
    eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[0],
    eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[1],
    ret);
4396

4397
          if (eNB->ulsch[i]->harq_processes[harq_pid]->round ==
4398
              fp->maxHARQ_Msg3Tx) {
4399
            LOG_D(PHY,"[eNB %d][RAPROC] maxHARQ_Msg3Tx reached, abandoning RA procedure for UE %d\n",
4400
                  eNB->Mod_id, i);
4401
            eNB->UE_stats[i].mode = PRACH;
4402 4403 4404 4405 4406 4407
      if (eNB->mac_enabled==1) {
        mac_xface->cancel_ra_proc(eNB->Mod_id,
          eNB->CC_id,
          frame,
          eNB->UE_stats[i].crnti);
      }
4408
            mac_phy_remove_ue(eNB->Mod_id,eNB->UE_stats[i].crnti);
4409

4410 4411
            eNB->ulsch[(uint32_t)i]->Msg3_active = 0;
            //eNB->ulsch[i]->harq_processes[harq_pid]->phich_active = 0;
4412 4413

          } else {
4414
            // activate retransmission for Msg3 (signalled to UE PHY by PHICH (not MAC/DCI)
4415
            eNB->ulsch[(uint32_t)i]->Msg3_active = 1;
4416

4417
            get_Msg3_alloc_ret(fp,
4418
                               subframe,
4419
                               frame,
4420 4421
                               &eNB->ulsch[i]->Msg3_frame,
                               &eNB->ulsch[i]->Msg3_subframe);
Cedric Roux's avatar
Cedric Roux committed
4422

4423 4424 4425
            mac_xface->set_msg3_subframe(eNB->Mod_id, eNB->CC_id, frame, subframe, eNB->ulsch[i]->rnti,
                                         eNB->ulsch[i]->Msg3_frame, eNB->ulsch[i]->Msg3_subframe);

Cedric Roux's avatar
Cedric Roux committed
4426 4427 4428
            T(T_ENB_PHY_MSG3_ALLOCATION, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe),
              T_INT(i), T_INT(eNB->ulsch[i]->rnti), T_INT(0 /* 0 is for retransmission*/),
              T_INT(eNB->ulsch[i]->Msg3_frame), T_INT(eNB->ulsch[i]->Msg3_subframe));
4429
          }
4430 4431
          LOG_D(PHY,"[eNB] Frame %d, Subframe %d: Msg3 in error, i = %d \n", frame,subframe,i);
        } // This is Msg3 error
4432

4433 4434
        else { //normal ULSCH
          LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d UE %d Error receiving ULSCH, round %d/%d (ACK %d,%d)\n",
4435
                eNB->Mod_id,harq_pid,
4436
                frame,subframe, i,
4437 4438 4439 4440
                eNB->ulsch[i]->harq_processes[harq_pid]->round-1,
                eNB->ulsch[i]->Mlimit,
                eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[0],
                eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[1]);
4441 4442 4443

#if defined(MESSAGE_CHART_GENERATOR_PHY)
          MSC_LOG_RX_DISCARDED_MESSAGE(
4444 4445 4446 4447 4448 4449 4450
               MSC_PHY_ENB,MSC_PHY_UE,
               NULL,0,
               "%05u:%02u ULSCH received rnti %x harq id %u round %d",
               frame,subframe,
               eNB->ulsch[i]->rnti,harq_pid,
               eNB->ulsch[i]->harq_processes[harq_pid]->round-1
               );
4451
#endif
4452

4453
          if (eNB->ulsch[i]->harq_processes[harq_pid]->round== eNB->ulsch[i]->Mlimit) {
4454
            LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d UE %d ULSCH Mlimit %d reached\n",
4455
                  eNB->Mod_id,harq_pid,
4456
                  frame,subframe, i,
4457
                  eNB->ulsch[i]->Mlimit);
4458

4459 4460 4461 4462
            eNB->ulsch[i]->harq_processes[harq_pid]->round=0;
            eNB->ulsch[i]->harq_processes[harq_pid]->phich_active=0;
            eNB->UE_stats[i].ulsch_errors[harq_pid]++;
            eNB->UE_stats[i].ulsch_consecutive_errors++;
4463

4464 4465 4466
     /*if (eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb > 20) {
    dump_ulsch(eNB,proc,i);
    exit(-1);
Younes's avatar
Younes committed
4467
           }*/
4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
      // indicate error to MAC
      if (eNB->mac_enabled == 1)
        mac_xface->rx_sdu(eNB->Mod_id,
        eNB->CC_id,
        frame,subframe,
        eNB->ulsch[i]->rnti,
        NULL,
        0,
        harq_pid,
        &eNB->ulsch[i]->Msg3_flag);
4478 4479 4480 4481
          }
        }
      }  // ulsch in error
      else {
4482 4483 4484



4485
        T(T_ENB_PHY_ULSCH_UE_ACK, T_INT(eNB->Mod_id), T_INT(frame), T_INT(subframe), T_INT(i), T_INT(eNB->ulsch[i]->rnti),
4486 4487
          T_INT(harq_pid));

4488
        if (eNB->ulsch[i]->Msg3_flag == 1) {
4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
    LOG_D(PHY,"[eNB %d][PUSCH %d] Frame %d subframe %d ULSCH received, setting round to 0, PHICH ACK\n",
    eNB->Mod_id,harq_pid,
    frame,subframe);
    LOG_D(PHY,"[eNB %d][PUSCH %d] frame %d subframe %d RNTI %x RX power (%d,%d) RSSI (%d,%d) N0 (%d,%d) dB ACK (%d,%d), decoding iter %d\n",
    eNB->Mod_id,harq_pid,
    frame,subframe,
    eNB->ulsch[i]->rnti,
    dB_fixed(eNB->pusch_vars[i]->ulsch_power[0]),
    dB_fixed(eNB->pusch_vars[i]->ulsch_power[1]),
    eNB->UE_stats[i].UL_rssi[0],
    eNB->UE_stats[i].UL_rssi[1],
    eNB->measurements->n0_power_dB[0],
    eNB->measurements->n0_power_dB[1],
    eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[0],
    eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[1],
    ret);
  }
4506 4507
#if defined(MESSAGE_CHART_GENERATOR_PHY)
        MSC_LOG_RX_MESSAGE(
4508 4509 4510 4511 4512 4513
         MSC_PHY_ENB,MSC_PHY_UE,
         NULL,0,
         "%05u:%02u ULSCH received rnti %x harq id %u",
         frame,subframe,
         eNB->ulsch[i]->rnti,harq_pid
         );
4514
#endif
4515
        for (j=0; j<fp->nb_antennas_rx; j++)
4516
          //this is the RSSI per RB
4517
          eNB->UE_stats[i].UL_rssi[j] =
4518
      
4519 4520
            dB_fixed(eNB->pusch_vars[i]->ulsch_power[j]*
                     (eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb*12)/
4521
                     fp->ofdm_symbol_size) -
4522 4523
            eNB->rx_total_gain_dB -
            hundred_times_log10_NPRB[eNB->ulsch[i]->harq_processes[harq_pid]->nb_rb-1]/100 -
4524
            get_hundred_times_delta_IF_eNB(eNB,i,harq_pid, 0)/100;
4525
      
4526 4527 4528 4529
        eNB->ulsch[i]->harq_processes[harq_pid]->phich_active = 1;
        eNB->ulsch[i]->harq_processes[harq_pid]->phich_ACK = 1;
        eNB->ulsch[i]->harq_processes[harq_pid]->round = 0;
        eNB->UE_stats[i].ulsch_consecutive_errors = 0;
4530

4531
        if (eNB->ulsch[i]->Msg3_flag == 1) {
4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
    if (eNB->mac_enabled==1) {

      LOG_I(PHY,"[eNB %d][RAPROC] Frame %d Terminating ra_proc for harq %d, UE %d\n",
      eNB->Mod_id,
      frame,harq_pid,i);
      if (eNB->mac_enabled)
        mac_xface->rx_sdu(eNB->Mod_id,
        eNB->CC_id,
        frame,subframe,
        eNB->ulsch[i]->rnti,
        eNB->ulsch[i]->harq_processes[harq_pid]->b,
        eNB->ulsch[i]->harq_processes[harq_pid]->TBS>>3,
        harq_pid,
        &eNB->ulsch[i]->Msg3_flag);
      
      // one-shot msg3 detection by MAC: empty PDU (e.g. CRNTI)
      if (eNB->ulsch[i]->Msg3_flag == 0 ) {
        eNB->UE_stats[i].mode = PRACH;
        mac_xface->cancel_ra_proc(eNB->Mod_id,
          eNB->CC_id,
          frame,
          eNB->UE_stats[i].crnti);
        mac_phy_remove_ue(eNB->Mod_id,eNB->UE_stats[i].crnti);
        eNB->ulsch[(uint32_t)i]->Msg3_active = 0;
      } // Msg3_flag == 0
      
    } // mac_enabled==1
4559

4560 4561
          eNB->UE_stats[i].mode = PUSCH;
          eNB->ulsch[i]->Msg3_flag = 0;
4562

4563
    LOG_D(PHY,"[eNB %d][RAPROC] Frame %d : RX Subframe %d Setting UE %d mode to PUSCH\n",eNB->Mod_id,frame,subframe,i);
4564

4565
          for (k=0; k<8; k++) { //harq_processes
4566 4567 4568 4569
            for (j=0; j<eNB->dlsch[i][0]->Mlimit; j++) {
              eNB->UE_stats[i].dlsch_NAK[k][j]=0;
              eNB->UE_stats[i].dlsch_ACK[k][j]=0;
              eNB->UE_stats[i].dlsch_trials[k][j]=0;
4570
            }
4571

4572 4573 4574
            eNB->UE_stats[i].dlsch_l2_errors[k]=0;
            eNB->UE_stats[i].ulsch_errors[k]=0;
            eNB->UE_stats[i].ulsch_consecutive_errors=0;
4575

4576 4577 4578 4579 4580
            for (j=0; j<eNB->ulsch[i]->Mlimit; j++) {
              eNB->UE_stats[i].ulsch_decoding_attempts[k][j]=0;
              eNB->UE_stats[i].ulsch_decoding_attempts_last[k][j]=0;
              eNB->UE_stats[i].ulsch_round_errors[k][j]=0;
              eNB->UE_stats[i].ulsch_round_fer[k][j]=0;
4581
            }
4582 4583
          }

4584 4585 4586
          eNB->UE_stats[i].dlsch_sliding_cnt=0;
          eNB->UE_stats[i].dlsch_NAK_round0=0;
          eNB->UE_stats[i].dlsch_mcs_offset=0;
4587
        } // Msg3_flag==1
4588
  else {  // Msg3_flag == 0
4589

4590
#ifdef DEBUG_PHY_PROC
4591 4592
#ifdef DEBUG_ULSCH
          LOG_D(PHY,"[eNB] Frame %d, Subframe %d : ULSCH SDU (RX harq_pid %d) %d bytes:",frame,subframe,
4593
                harq_pid,eNB->ulsch[i]->harq_processes[harq_pid]->TBS>>3);
4594

4595 4596
          for (j=0; j<eNB->ulsch[i]->harq_processes[harq_pid]->TBS>>3; j++)
            LOG_T(PHY,"%x.",eNB->ulsch[i]->harq_processes[harq_pid]->b[j]);
4597 4598 4599

          LOG_T(PHY,"\n");
#endif
4600
#endif
4601

4602
    if (eNB->mac_enabled==1) {
4603

4604 4605 4606 4607 4608 4609 4610 4611
      mac_xface->rx_sdu(eNB->Mod_id,
            eNB->CC_id,
            frame,subframe,
            eNB->ulsch[i]->rnti,
            eNB->ulsch[i]->harq_processes[harq_pid]->b,
            eNB->ulsch[i]->harq_processes[harq_pid]->TBS>>3,
            harq_pid,
            NULL);
4612

4613
#ifdef LOCALIZATION
4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
      start_meas(&eNB->localization_stats);
      aggregate_eNB_UE_localization_stats(eNB,
            i,
            frame,
            subframe,
            get_hundred_times_delta_IF_eNB(eNB,i,harq_pid, 1)/100);
      stop_meas(&eNB->localization_stats);
#endif
      
    } // mac_enabled==1
4624
        } // Msg3_flag == 0
4625

4626
        // estimate timing advance for MAC
4627
        if (eNB->abstraction_flag == 0) {
4628
          sync_pos = lte_est_timing_advance_pusch(eNB,i);
4629
          eNB->UE_stats[i].timing_advance_update = sync_pos - fp->nb_prefix_samples/4; //to check
4630
        }
4631

4632 4633
#ifdef DEBUG_PHY_PROC
        LOG_D(PHY,"[eNB %d] frame %d, subframe %d: user %d: timing advance = %d\n",
4634
              eNB->Mod_id,
4635 4636
              frame, subframe,
              i,
4637
              eNB->UE_stats[i].timing_advance_update);
4638
#endif
4639 4640


4641 4642 4643
      }  // ulsch not in error

      // process HARQ feedback
4644
#ifdef DEBUG_PHY_PROC
4645
      LOG_D(PHY,"[eNB %d][PDSCH %x] Frame %d subframe %d, Processing HARQ feedback for UE %d (after PUSCH)\n",eNB->Mod_id,
4646
            eNB->dlsch[i][0]->rnti,
4647 4648
            frame,subframe,
            i);
4649
#endif
4650
      process_HARQ_feedback(i,
4651
                            eNB,proc,
4652 4653 4654 4655 4656 4657 4658
                            1, // pusch_flag
                            0,
                            0,
                            0);

#ifdef DEBUG_PHY_PROC
      LOG_D(PHY,"[eNB %d] Frame %d subframe %d, sect %d: received ULSCH harq_pid %d for UE %d, ret = %d, CQI CRC Status %d, ACK %d,%d, ulsch_errors %d/%d\n",
4659
            eNB->Mod_id,frame,subframe,
4660
            eNB->UE_stats[i].sector,
4661 4662 4663
            harq_pid,
            i,
            ret,
4664 4665 4666 4667 4668
            eNB->ulsch[i]->harq_processes[harq_pid]->cqi_crc_status,
            eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[0],
            eNB->ulsch[i]->harq_processes[harq_pid]->o_ACK[1],
            eNB->UE_stats[i].ulsch_errors[harq_pid],
            eNB->UE_stats[i].ulsch_decoding_attempts[harq_pid][0]);
4669 4670 4671 4672
#endif
      
      // dump stats to VCD
      if (i==0) {
4673 4674 4675 4676 4677 4678
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_MCS0+harq_pid,eNB->pusch_stats_mcs[0][(frame*10)+subframe]);
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_RB0+harq_pid,eNB->pusch_stats_rb[0][(frame*10)+subframe]);
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_ROUND0+harq_pid,eNB->pusch_stats_round[0][(frame*10)+subframe]);
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_RSSI0+harq_pid,dB_fixed(eNB->pusch_vars[0]->ulsch_power[0]));
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_RES0+harq_pid,ret);
  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_SFN0+harq_pid,(frame*10)+subframe);
Lionel Gauthier's avatar
Lionel Gauthier committed
4679
      }
4680
    } // ulsch[0] && ulsch[0]->rnti>0 && ulsch[0]->subframe_scheduling_flag == 1
4681 4682


4683
    // update ULSCH statistics for tracing
4684
    if ((frame % 100 == 0) && (subframe == 4)) {
4685
      for (harq_idx=0; harq_idx<8; harq_idx++) {
4686 4687 4688 4689 4690 4691 4692 4693
        for (round=0; round<eNB->ulsch[i]->Mlimit; round++) {
          if ((eNB->UE_stats[i].ulsch_decoding_attempts[harq_idx][round] -
               eNB->UE_stats[i].ulsch_decoding_attempts_last[harq_idx][round]) != 0) {
            eNB->UE_stats[i].ulsch_round_fer[harq_idx][round] =
              (100*(eNB->UE_stats[i].ulsch_round_errors[harq_idx][round] -
                    eNB->UE_stats[i].ulsch_round_errors_last[harq_idx][round]))/
              (eNB->UE_stats[i].ulsch_decoding_attempts[harq_idx][round] -
               eNB->UE_stats[i].ulsch_decoding_attempts_last[harq_idx][round]);
4694
          } else {
4695
            eNB->UE_stats[i].ulsch_round_fer[harq_idx][round] = 0;
4696 4697
          }

4698 4699 4700 4701
          eNB->UE_stats[i].ulsch_decoding_attempts_last[harq_idx][round] =
            eNB->UE_stats[i].ulsch_decoding_attempts[harq_idx][round];
          eNB->UE_stats[i].ulsch_round_errors_last[harq_idx][round] =
            eNB->UE_stats[i].ulsch_round_errors[harq_idx][round];
4702
        }
4703 4704
      }
    }
4705

Florian Kaltenberger's avatar
Florian Kaltenberger committed
4706
    if ((frame % 100 == 0) && (subframe==4)) {
4707
      eNB->UE_stats[i].dlsch_bitrate = (eNB->UE_stats[i].total_TBS -
4708
          eNB->UE_stats[i].total_TBS_last);
4709

4710
      eNB->UE_stats[i].total_TBS_last = eNB->UE_stats[i].total_TBS;
4711
    }
4712

4713
    // CBA (non-LTE)
4714
    cba_procedures(eNB,proc,i,harq_pid);
4715 4716
  } // loop i=0 ... NUMBER_OF_UE_MAX-1

4717
  if (eNB->abstraction_flag == 0) {
4718
    lte_eNB_I0_measurements(eNB,
4719 4720 4721
          subframe,
          0,
          eNB->first_run_I0_measurements);
4722
    eNB->first_run_I0_measurements = 0;
4723
  }
4724

4725
#ifdef PHY_ABSTRACTION
4726 4727
  else {
    lte_eNB_I0_measurements_emul(eNB,
4728
         0);
4729
  }
4730

4731
#endif
4732
  //}
4733 4734

#ifdef EMOS
4735
  phy_procedures_emos_eNB_RX(subframe,eNB);
4736 4737
#endif

4738 4739 4740 4741 4742 4743 4744 4745 4746
#if defined(FLEXRAN_AGENT_SB_IF)
#ifndef DISABLE_SF_TRIGGER
  //Send subframe trigger to the controller
  if (mac_agent_registered[eNB->Mod_id]) {
    agent_mac_xface[eNB->Mod_id]->flexran_agent_send_sf_trigger(eNB->Mod_id);
  }
#endif
#endif

Raymond Knopp's avatar
Raymond Knopp committed
4747
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_PROCEDURES_ENB_RX_UESPEC+offset, 0 );
4748

4749
  stop_meas(&eNB->phy_proc_rx);
4750

4751 4752 4753 4754
}

#undef DEBUG_PHY_PROC

Cedric Roux's avatar
Cedric Roux committed
4755
#if defined(Rel10) || defined(Rel14)
4756 4757 4758
int phy_procedures_RN_eNB_TX(unsigned char last_slot, unsigned char next_slot, relaying_type_t r_type)
{

4759
  int do_proc=0;// do nothing
4760 4761

  switch(r_type) {
4762
  case no_relay:
4763
    do_proc= no_relay; // perform the normal eNB operation
4764
    break;
4765

4766
  case multicast_relay:
4767
    if (((next_slot >>1) < 6) || ((next_slot >>1) > 8))
4768
      do_proc = 0; // do nothing
4769 4770 4771
    else // SF#6, SF#7 and SF#8
      do_proc = multicast_relay; // do PHY procedures eNB TX

4772
    break;
4773

4774 4775
  default: // should'not be here
    LOG_W(PHY,"Not supported relay type %d, do nothing\n", r_type);
4776
    do_proc=0;
4777 4778
    break;
  }
4779

4780 4781
  return do_proc;
}
4782
#endif