eNB_scheduler.c 224 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2010 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crêtes, 06560 Valbonne Sophia Antipolis, France

gauthier's avatar
gauthier committed
28
 *******************************************************************************/
29
/*! \file eNB_scheduler.c
30 31 32
 * \brief procedures related to UE
 * \author Raymond Knopp, Navid Nikaein
 * \date 2011
33
 * \email: navid.nikaein@eurecom.fr
34 35
 * \version 0.5
 * @ingroup _mac
36

37
 */
38

39
#include "assertions.h"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "PHY/defs.h"
#include "PHY/extern.h"

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

#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/defs.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/from_grlib_softregs.h"

#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"

60
//#include "LAYER2/MAC/pre_processor.c"
61
#include "pdcp.h"
62

63 64 65 66
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

67 68 69 70 71 72 73 74 75
#define ENABLE_MAC_PAYLOAD_DEBUG
#define DEBUG_eNB_SCHEDULER 1
//#define DEBUG_HEADER_PARSING 1
//#define DEBUG_PACKET_TRACE 1

/*
  #ifndef USER_MODE
  #define msg debug_msg
  #endif
gauthier's avatar
gauthier committed
76
 */
77 78 79 80 81

extern inline unsigned int taus(void);


void init_ue_sched_info(void){
gauthier's avatar
gauthier committed
82
  module_id_t i,j;
83
  for (i=0;i<NUMBER_OF_eNB_MAX;i++){
gauthier's avatar
gauthier committed
84 85 86 87 88 89 90 91 92 93 94
      for (j=0;j<NUMBER_OF_UE_MAX;j++){
          // init DL
          eNB_dlsch_info[i][j].weight           = 0;
          eNB_dlsch_info[i][j].subframe         = 0;
          eNB_dlsch_info[i][j].serving_num      = 0;
          eNB_dlsch_info[i][j].status           = S_DL_NONE;
          // init UL
          eNB_ulsch_info[i][j].subframe         = 0;
          eNB_ulsch_info[i][j].serving_num      = 0;
          eNB_ulsch_info[i][j].status           = S_UL_NONE;
      }
95 96 97
  }
}

gauthier's avatar
gauthier committed
98
void add_ue_ulsch_info(module_id_t module_idP, module_id_t ue_mod_idP, sub_frame_t subframeP, UE_ULSCH_STATUS status){
99

gauthier's avatar
gauthier committed
100 101 102
  eNB_ulsch_info[module_idP][ue_mod_idP].rnti             = find_UE_RNTI(module_idP,ue_mod_idP);
  eNB_ulsch_info[module_idP][ue_mod_idP].subframe         = subframeP;
  eNB_ulsch_info[module_idP][ue_mod_idP].status           = status;
103

gauthier's avatar
gauthier committed
104
  eNB_ulsch_info[module_idP][ue_mod_idP].serving_num++;
105 106

}
gauthier's avatar
gauthier committed
107
void add_ue_dlsch_info(module_id_t module_idP, module_id_t ue_mod_idP, sub_frame_t subframeP, UE_DLSCH_STATUS status){
108

gauthier's avatar
gauthier committed
109 110 111 112
  eNB_dlsch_info[module_idP][ue_mod_idP].rnti             = find_UE_RNTI(module_idP,ue_mod_idP);
  //  eNB_dlsch_info[module_idP][ue_mod_idP].weight           = weight;
  eNB_dlsch_info[module_idP][ue_mod_idP].subframe         = subframeP;
  eNB_dlsch_info[module_idP][ue_mod_idP].status           = status;
113

gauthier's avatar
gauthier committed
114
  eNB_dlsch_info[module_idP][ue_mod_idP].serving_num++;
115 116 117

}

gauthier's avatar
gauthier committed
118
unsigned char get_ue_weight(module_id_t module_idP, module_id_t ue_mod_idP){
119

gauthier's avatar
gauthier committed
120
  return(eNB_dlsch_info[module_idP][ue_mod_idP].weight);
121 122 123 124

}

// return is rnti ???
gauthier's avatar
gauthier committed
125
module_id_t schedule_next_ulue(module_id_t module_idP, module_id_t ue_mod_idP, sub_frame_t subframeP){
126

gauthier's avatar
gauthier committed
127
  module_id_t next_ue;
128 129

  // first phase: scheduling for ACK
gauthier's avatar
gauthier committed
130 131
  switch (subframeP) {
  // scheduling for subframeP 2: for scheduled user during subframeP 5 and 6
132
  case 8:
gauthier's avatar
gauthier committed
133 134 135 136 137
    if  ((eNB_dlsch_info[module_idP][ue_mod_idP].status == S_DL_SCHEDULED) &&
        (eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 5 || eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 6)){
        // set the downlink status
        eNB_dlsch_info[module_idP][ue_mod_idP].status = S_DL_BUFFERED;
        return ue_mod_idP;
138 139
    }
    break;
gauthier's avatar
gauthier committed
140
    // scheduling for subframeP 3: for scheduled user during subframeP 7 and 8
141
  case 9:
gauthier's avatar
gauthier committed
142 143 144 145
    if  ((eNB_dlsch_info[module_idP][ue_mod_idP].status == S_DL_SCHEDULED) &&
        (eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 7 || eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 8)){
        eNB_dlsch_info[module_idP][ue_mod_idP].status = S_DL_BUFFERED;
        return ue_mod_idP;
146 147
    }
    break;
gauthier's avatar
gauthier committed
148
    // scheduling UL subframeP 4: for scheduled user during subframeP 9 and 0
149
  case 0 :
gauthier's avatar
gauthier committed
150 151 152 153
    if  ((eNB_dlsch_info[module_idP][ue_mod_idP].status == S_DL_SCHEDULED) &&
        (eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 9 || eNB_dlsch_info[module_idP][ue_mod_idP].subframe == 0)){
        eNB_dlsch_info[module_idP][ue_mod_idP].status = S_DL_BUFFERED;
        return ue_mod_idP;
154 155 156 157 158 159 160 161 162
    }
    break;
  default:
    break;
  }

  // second phase
  for (next_ue=0; next_ue <NUMBER_OF_UE_MAX; next_ue++ ){

gauthier's avatar
gauthier committed
163 164 165 166 167
      if  (eNB_ulsch_info[module_idP][next_ue].status == S_UL_WAITING )
        return next_ue;
      else if (eNB_ulsch_info[module_idP][next_ue].status == S_UL_SCHEDULED){
          eNB_ulsch_info[module_idP][next_ue].status = S_UL_BUFFERED;
      }
168 169
  }
  for (next_ue=0; next_ue <NUMBER_OF_UE_MAX; next_ue++ ){
gauthier's avatar
gauthier committed
170 171
      if (eNB_ulsch_info[module_idP][next_ue].status != S_UL_NONE )// do this just for active UEs
        eNB_ulsch_info[module_idP][next_ue].status = S_UL_WAITING;
172 173 174 175 176 177
  }
  next_ue = 0;
  return next_ue;

}

gauthier's avatar
gauthier committed
178
module_id_t schedule_next_dlue(module_id_t module_idP, sub_frame_t subframeP){
179

gauthier's avatar
gauthier committed
180
  module_id_t next_ue;
181

gauthier's avatar
gauthier committed
182 183 184
  for (next_ue=0; next_ue < NUMBER_OF_UE_MAX; next_ue++ ){
      if  (eNB_dlsch_info[module_idP][next_ue].status == S_DL_WAITING)
        return next_ue;
185 186
  }
  for (next_ue=0; next_ue <NUMBER_OF_UE_MAX; next_ue++ )
gauthier's avatar
gauthier committed
187 188
    if  (eNB_dlsch_info[module_idP][next_ue].status == S_DL_BUFFERED) {
        eNB_dlsch_info[module_idP][next_ue].status = S_DL_WAITING;
189 190
    }
  // next_ue = -1;
gauthier's avatar
gauthier committed
191
  return (module_id_t)(-1);//next_ue;
192 193 194

}

gauthier's avatar
gauthier committed
195
void initiate_ra_proc(module_id_t module_idP, frame_t frameP, uint16_t preamble_index,int16_t timing_offset,uint8_t sect_id,sub_frame_t subframeP,uint8_t f_id) {
196

gauthier's avatar
gauthier committed
197
  uint8_t i;
198

gauthier's avatar
gauthier committed
199
  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Initiating RA procedure for preamble index %d\n",module_idP,frameP,preamble_index);
200 201

  for (i=0;i<NB_RA_PROC_MAX;i++) {
gauthier's avatar
gauthier committed
202 203
      if (eNB_mac_inst[module_idP].RA_template[i].RA_active==FALSE) {
          eNB_mac_inst[module_idP].RA_template[i].RA_active=TRUE;
gauthier's avatar
gauthier committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217
          eNB_mac_inst[module_idP].RA_template[i].generate_rar=1;
          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].timing_offset=timing_offset;
          // Put in random rnti (to be replaced with proper procedure!!)
          eNB_mac_inst[module_idP].RA_template[i].rnti = taus();
          eNB_mac_inst[module_idP].RA_template[i].RA_rnti = 1+subframeP+(10*f_id);
          eNB_mac_inst[module_idP].RA_template[i].preamble_index = preamble_index;
          LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Activating RAR generation for process %d, rnti %x, RA_active %d\n",
              module_idP,frameP,i,eNB_mac_inst[module_idP].RA_template[i].rnti,
              eNB_mac_inst[module_idP].RA_template[i].RA_active);

          return;
      }
218 219 220
  }
}

gauthier's avatar
gauthier committed
221
void cancel_ra_proc(module_id_t module_idP, frame_t frameP, rnti_t rnti) {
222
  unsigned char i;
gauthier's avatar
gauthier committed
223
  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Cancelling RA procedure for UE rnti %x\n",module_idP,frameP,rnti);
224 225

  for (i=0;i<NB_RA_PROC_MAX;i++) {
gauthier's avatar
gauthier committed
226
      if (rnti == eNB_mac_inst[module_idP].RA_template[i].rnti) {
gauthier's avatar
gauthier committed
227
          eNB_mac_inst[module_idP].RA_template[i].RA_active=FALSE;
gauthier's avatar
gauthier committed
228 229 230 231 232 233 234
          eNB_mac_inst[module_idP].RA_template[i].generate_rar=0;
          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].timing_offset=0;
          eNB_mac_inst[module_idP].RA_template[i].RRC_timer=20;
          eNB_mac_inst[module_idP].RA_template[i].rnti = 0;
      }
235 236 237
  }
}

gauthier's avatar
gauthier committed
238
void terminate_ra_proc(module_id_t module_idP,frame_t frameP,rnti_t rnti,unsigned char *msg3, uint16_t msg3_len) {
239 240 241

  unsigned char rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
  unsigned char rx_lcids[NB_RB_MAX];
gauthier's avatar
gauthier committed
242 243
  uint16_t rx_lengths[NB_RB_MAX];
  int8_t UE_id;
244 245

  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Received msg3 %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
gauthier's avatar
gauthier committed
246 247
      module_idP,frameP,
      msg3[3],msg3[4],msg3[5],msg3[6],msg3[7], msg3[8], rnti);
248 249

  for (i=0;i<NB_RA_PROC_MAX;i++) {
gauthier's avatar
gauthier committed
250 251 252 253
      LOG_D(MAC,"[RAPROC] Checking proc %d : rnti (%x, %x), active %d\n",i,
          eNB_mac_inst[module_idP].RA_template[i].rnti, rnti,
          eNB_mac_inst[module_idP].RA_template[i].RA_active);
      if ((eNB_mac_inst[module_idP].RA_template[i].rnti==rnti) &&
gauthier's avatar
gauthier committed
254
          (eNB_mac_inst[module_idP].RA_template[i].RA_active==TRUE)) {
gauthier's avatar
gauthier committed
255 256 257 258

          payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
          LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Received CCCH: length %d, offset %d\n",
              module_idP,frameP,rx_lengths[0],payload_ptr-msg3);
259
          if (/*(num_ce == 0) &&*/ (num_sdu==1) && (rx_lcids[0] == CCCH)) { // This is an RRCConnectionRequest/Restablishment
gauthier's avatar
gauthier committed
260 261 262 263 264 265 266 267 268 269 270 271 272
              memcpy(&eNB_mac_inst[module_idP].RA_template[i].cont_res_id[0],payload_ptr,6);
              LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Received CCCH: length %d, offset %d\n",
                  module_idP,frameP,rx_lengths[0],payload_ptr-msg3);
              UE_id=add_new_ue(module_idP,eNB_mac_inst[module_idP].RA_template[i].rnti);
              if (UE_id==-1) {
                  mac_xface->macphy_exit("[MAC][eNB] Max user count reached\n");
              }
              else {
                  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Added user with rnti %x => UE %d\n",
                      module_idP,frameP,eNB_mac_inst[module_idP].RA_template[i].rnti,UE_id);
              }

              if (Is_rrc_registered == 1)
gauthier's avatar
gauthier committed
273
                mac_rrc_data_ind(module_idP,frameP,CCCH,(uint8_t *)payload_ptr,rx_lengths[0],1,module_idP,0);
gauthier's avatar
gauthier committed
274 275 276 277 278 279 280 281 282 283 284 285 286
              // add_user.  This is needed to have the rnti for configuring UE (PHY). The UE is removed if RRC
              // doesn't provide a CCCH SDU

          }
          else if (num_ce >0) {  // handle msg3 which is not RRCConnectionRequest
              //	process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
          }

          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4 = 1;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4 = 0;

          return;
      } // if process is active
287 288 289 290

  } // loop on RA processes
}

gauthier's avatar
gauthier committed
291
DCI_PDU *get_dci_sdu(module_id_t module_idP, frame_t frameP, sub_frame_t subframeP) {
292

gauthier's avatar
gauthier committed
293
  return(&eNB_mac_inst[module_idP].DCI_pdu);
294 295 296

}

gauthier's avatar
gauthier committed
297
module_id_t find_UE_id(module_id_t module_idP, rnti_t rnti) {
298

gauthier's avatar
gauthier committed
299
  module_id_t ue_mod_id;
300

gauthier's avatar
gauthier committed
301
  for (ue_mod_id=0;ue_mod_id<NUMBER_OF_UE_MAX;ue_mod_id++) {
302
    //   if (mac_get_rrc_status(module_idP,1,ue_mod_id) >= RRC_CONNECTED) {
gauthier's avatar
gauthier committed
303
      if (eNB_mac_inst[module_idP].UE_template[ue_mod_id].rnti==rnti) {
304
	return(ue_mod_id);
gauthier's avatar
gauthier committed
305
      }
306
  }
gauthier's avatar
gauthier committed
307
  return(module_id_t)(-1);
308 309 310

}

gauthier's avatar
gauthier committed
311
rnti_t find_UE_RNTI(module_id_t module_idP, module_id_t ue_mod_idP) {
312

gauthier's avatar
gauthier committed
313
  return (eNB_mac_inst[module_idP].UE_template[ue_mod_idP].rnti);
314 315

}
gauthier's avatar
gauthier committed
316
boolean_t is_UE_active(module_id_t module_idP, module_id_t ue_mod_idP ){
gauthier's avatar
gauthier committed
317
  if (eNB_mac_inst[module_idP].UE_template[ue_mod_idP].rnti !=0 )
gauthier's avatar
gauthier committed
318
    return TRUE;
319
  else
gauthier's avatar
gauthier committed
320
    return FALSE ;
321
}
gauthier's avatar
gauthier committed
322
uint8_t find_active_UEs(module_id_t module_idP){
323

gauthier's avatar
gauthier committed
324 325
  module_id_t        ue_mod_id      = 0;
  rnti_t        rnti         = 0;
gauthier's avatar
gauthier committed
326
  uint8_t            nb_active_ue = 0;
327

gauthier's avatar
gauthier committed
328
  for (ue_mod_id=0;ue_mod_id<NUMBER_OF_UE_MAX;ue_mod_id++) {
329

gauthier's avatar
gauthier committed
330
      if (((rnti=eNB_mac_inst[module_idP].UE_template[ue_mod_id].rnti) !=0)&&(eNB_mac_inst[module_idP].UE_template[ue_mod_id].ul_active==TRUE)){
331

gauthier's avatar
gauthier committed
332 333 334 335 336 337
          if (mac_xface->get_eNB_UE_stats(module_idP,rnti) != NULL){ // check at the phy enb_ue state for this rnti
              nb_active_ue++;
          }
          else { // this ue is removed at the phy => remove it at the mac as well
              mac_remove_ue(module_idP, ue_mod_id);
          }
338 339 340 341 342 343
      }
  }
  return(nb_active_ue);
}

// function for determining which active ue should be granted resources in uplink based on BSR+QoS
gauthier's avatar
gauthier committed
344
uint8_t find_ulgranted_UEs(module_id_t module_idP){
345 346

  // all active users should be granted
gauthier's avatar
gauthier committed
347
  return(find_active_UEs(module_idP));
348 349 350
}

// function for determining which active ue should be granted resources in downlink based on CQI, SI, and BSR
gauthier's avatar
gauthier committed
351
uint8_t find_dlgranted_UEs(module_id_t module_idP){
352 353

  // all active users should be granted
gauthier's avatar
gauthier committed
354
  return(find_active_UEs(module_idP));
355 356
}
// get aggregatiob form phy for a give UE
gauthier's avatar
gauthier committed
357
unsigned char process_ue_cqi (module_id_t module_idP, module_id_t ue_mod_idP) {
358 359 360 361 362
  unsigned char aggregation=2;
  // check the MCS and SNR and set the aggregation accordingly
  return aggregation;
}
#ifdef CBA
gauthier's avatar
gauthier committed
363
uint8_t find_num_active_UEs_in_cbagroup(module_id_t module_idP, unsigned char group_id){
364

gauthier's avatar
gauthier committed
365 366
  module_id_t    UE_id;
  rnti_t    rnti;
367 368
  unsigned char nb_ue_in_pusch=0;
  LTE_eNB_UE_stats* eNB_UE_stats;
gauthier's avatar
gauthier committed
369 370 371 372

  for (UE_id=group_id;UE_id<NUMBER_OF_UE_MAX;UE_id+=eNB_mac_inst[module_idP].num_active_cba_groups) {

      if (((rnti=eNB_mac_inst[module_idP].UE_template[UE_id].rnti) !=0) &&
gauthier's avatar
gauthier committed
373
          (eNB_mac_inst[module_idP].UE_template[UE_id].ul_active==TRUE)    &&
gauthier's avatar
gauthier committed
374 375 376 377 378 379 380 381
          (mac_get_rrc_status(module_idP,1,UE_id) > RRC_CONNECTED)){
          //  && (UE_is_to_be_scheduled(module_idP,UE_id)))
          // check at the phy enb_ue state for this rnti
          if ((eNB_UE_stats= mac_xface->get_eNB_UE_stats(module_idP,rnti)) != NULL){
              if ((eNB_UE_stats->mode == PUSCH) && (UE_is_to_be_scheduled(module_idP,UE_id) == 0)){
                  nb_ue_in_pusch++;
              }
          }
382 383 384 385
      }
  }
  return(nb_ue_in_pusch);
}
386
#endif
gauthier's avatar
gauthier committed
387
int8_t add_new_ue(module_id_t enb_mod_idP, rnti_t rntiP) {
gauthier's avatar
gauthier committed
388
  module_id_t ue_mod_id;
gauthier's avatar
gauthier committed
389
  int         j;
gauthier's avatar
gauthier committed
390 391 392 393 394 395 396 397 398 399 400

  for (ue_mod_id=0;ue_mod_id<NUMBER_OF_UE_MAX;ue_mod_id++) {
      if (eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].rnti == 0) {
          eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].rnti = rntiP;
          for (j=0;j<8;j++) {
              eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].oldNDI[j]    = 0;
              eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].oldNDI_UL[j] = 0;
          }
          eNB_ulsch_info[enb_mod_idP][ue_mod_id].status = S_UL_WAITING;
          eNB_dlsch_info[enb_mod_idP][ue_mod_id].status = S_UL_WAITING;
          LOG_D(MAC,"[eNB] Add UE_id %d : rnti %x\n",ue_mod_id,eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].rnti);
gauthier's avatar
gauthier committed
401
          return((int8_t)ue_mod_id);
402
      }
403 404 405 406
  }
  return(-1);
}

gauthier's avatar
gauthier committed
407
int8_t mac_remove_ue(module_id_t enb_mod_idP, module_id_t ue_mod_idP) {
408

gauthier's avatar
gauthier committed
409
  LOG_I(MAC,"Removing UE %d (rnti %x)\n",ue_mod_idP,eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].rnti);
410 411

  // clear all remaining pending transmissions
gauthier's avatar
gauthier committed
412 413 414 415
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].bsr_info[LCGID0]  = 0;
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].bsr_info[LCGID1]  = 0;
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].bsr_info[LCGID2]  = 0;
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].bsr_info[LCGID3]  = 0;
416

gauthier's avatar
gauthier committed
417 418
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].ul_SR             = 0;
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].rnti              = 0;
gauthier's avatar
gauthier committed
419
  eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_idP].ul_active         = FALSE;
gauthier's avatar
gauthier committed
420 421 422 423
  eNB_ulsch_info[enb_mod_idP][ue_mod_idP].rnti                        = 0;
  eNB_ulsch_info[enb_mod_idP][ue_mod_idP].status                      = S_UL_NONE;
  eNB_dlsch_info[enb_mod_idP][ue_mod_idP].rnti                        = 0;
  eNB_dlsch_info[enb_mod_idP][ue_mod_idP].status                      = S_DL_NONE;
424

gauthier's avatar
gauthier committed
425
  rrc_eNB_free_UE_index(enb_mod_idP,ue_mod_idP);
426 427 428 429

  return(1);
}

gauthier's avatar
gauthier committed
430
unsigned char *get_dlsch_sdu(module_id_t module_idP, frame_t frameP, rnti_t rntiP, uint8_t TBindex) {
431

gauthier's avatar
gauthier committed
432
  module_id_t ue_mod_id;
433

gauthier's avatar
gauthier committed
434 435
  if (rntiP==SI_RNTI) {
      LOG_D(MAC,"[eNB %d] Frame %d Get DLSCH sdu for BCCH \n",module_idP,frameP);
436

gauthier's avatar
gauthier committed
437
      return((unsigned char *)&eNB_mac_inst[module_idP].BCCH_pdu.payload[0]);
438
  }
439 440 441 442 443
  else if ((ue_mod_id = find_UE_id(module_idP,rntiP)) != UE_INDEX_INVALID ){
    LOG_D(MAC,"[eNB %d] Frame %d:  Get DLSCH sdu for rnti %x => UE_id %d\n",module_idP,frameP,rntiP,ue_mod_id);
    return((unsigned char *)&eNB_mac_inst[module_idP].DLSCH_pdu[ue_mod_id][TBindex].payload[0]);
  } else {
    LOG_E(MAC,"[eNB %d] Frame %d: UE with RNTI %x does not exist\n", module_idP,frameP,rntiP);
gauthier's avatar
gauthier committed
444
    return NULL;
445 446 447 448 449
  }

}

unsigned char *parse_ulsch_header(unsigned char *mac_header,
gauthier's avatar
gauthier committed
450 451 452 453 454 455
    unsigned char *num_ce,
    unsigned char *num_sdu,
    unsigned char *rx_ces,
    unsigned char *rx_lcids,
    unsigned short *rx_lengths,
    unsigned short tb_length) {
456 457 458 459 460 461 462

  unsigned char not_done=1,num_ces=0,num_sdus=0,lcid,num_sdu_cnt;
  unsigned char *mac_header_ptr = mac_header;
  unsigned short length, ce_len=0;

  while (not_done==1) {

gauthier's avatar
gauthier committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
      if (((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E == 0)
        not_done = 0;

      lcid = ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID;
      if (lcid < EXTENDED_POWER_HEADROOM) {
          if (not_done==0) { // last MAC SDU, length is implicit
              mac_header_ptr++;
              length = tb_length-(mac_header_ptr-mac_header)-ce_len;
              for (num_sdu_cnt=0; num_sdu_cnt < num_sdus ; num_sdu_cnt++)
                length -= rx_lengths[num_sdu_cnt];
          }
          else {
              if (((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F == 0) {
                  length = ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L;
                  mac_header_ptr += 2;//sizeof(SCH_SUBHEADER_SHORT);
              }
              else { // F = 1
                  length = ((((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB & 0x7f ) << 8 ) | (((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB & 0xff);
                  mac_header_ptr += 3;//sizeof(SCH_SUBHEADER_LONG);
              }
          }
          LOG_D(MAC,"[eNB] sdu %d lcid %d tb_length %d length %d (offset now %d)\n",
              num_sdus,lcid,tb_length, length,mac_header_ptr-mac_header);
          rx_lcids[num_sdus] = lcid;
          rx_lengths[num_sdus] = length;
          num_sdus++;
489
      }
gauthier's avatar
gauthier committed
490 491 492 493 494 495 496 497 498
      else {  // This is a control element subheader POWER_HEADROOM, BSR and CRNTI
          if (lcid == SHORT_PADDING) {
              mac_header_ptr++;
          }
          else {
              rx_ces[num_ces] = lcid;
              num_ces++;
              mac_header_ptr++;
              if (lcid==LONG_BSR)
499
                ce_len+=3;
gauthier's avatar
gauthier committed
500 501 502 503
              else if (lcid==CRNTI)
                ce_len+=2;
              else if ((lcid==POWER_HEADROOM) || (lcid==TRUNCATED_BSR)|| (lcid== SHORT_BSR))
                ce_len++;
504 505 506 507
	      else {
		LOG_E(MAC,"unknown CE %d \n", lcid);
		exit(-1);
	      }
gauthier's avatar
gauthier committed
508
          }
509 510 511 512 513 514 515 516
      }
  }
  *num_ce = num_ces;
  *num_sdu = num_sdus;

  return(mac_header_ptr);
}

gauthier's avatar
gauthier committed
517
void SR_indication(module_id_t enb_mod_idP, frame_t frameP, rnti_t rntiP, sub_frame_t subframeP) {
518

519
  module_id_t ue_mod_id = find_UE_id(enb_mod_idP, rntiP);
520
  
521
  if (ue_mod_id  != UE_INDEX_INVALID ) {
gauthier's avatar
gauthier committed
522 523
      LOG_D(MAC,"[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d \n",enb_mod_idP,rntiP,frameP,subframeP, ue_mod_id);
      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].ul_SR = 1;
gauthier's avatar
gauthier committed
524
      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].ul_active = TRUE;
gauthier's avatar
gauthier committed
525
  } else {
526
    //     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
gauthier's avatar
gauthier committed
527
    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
gauthier's avatar
gauthier committed
528
  }
529 530
}

gauthier's avatar
gauthier committed
531
void rx_sdu(module_id_t enb_mod_idP,frame_t frameP,rnti_t rntiP,uint8_t *sdu, uint16_t sdu_len) {
532

gauthier's avatar
gauthier committed
533 534
  unsigned char  rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
  unsigned char  rx_lcids[NB_RB_MAX];
535
  unsigned short rx_lengths[NB_RB_MAX];
536
  module_id_t    ue_mod_id = find_UE_id(enb_mod_idP,rntiP);
537
  int ii,j;
538
  start_meas(&eNB_mac_inst[enb_mod_idP].rx_ulsch_sdu);
539 540 541
  
  if ((ue_mod_id >  NUMBER_OF_UE_MAX) || (ue_mod_id == -1) || (ue_mod_id == 255) )
  
542
  for(ii=0; ii<NB_RB_MAX; ii++) rx_lengths[ii] = 0;
gauthier's avatar
gauthier committed
543

544 545
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,1);

gauthier's avatar
gauthier committed
546
  LOG_D(MAC,"[eNB %d] Received ULSCH sdu from PHY (rnti %x, UE_id %d), parsing header\n",enb_mod_idP,rntiP,ue_mod_id);
547 548 549 550 551
  payload_ptr = parse_ulsch_header(sdu,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,sdu_len);

  // control element
  for (i=0;i<num_ce;i++) {

552 553 554 555 556 557 558 559
    switch (rx_ces[i]) { // implement and process BSR + CRNTI +
    case POWER_HEADROOM:
      if (ue_mod_id != UE_INDEX_INVALID ){
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].phr_info =  (payload_ptr[0] & 0x3f);// - PHR_MAPPING_OFFSET;
	LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received PHR PH = %d (db)\n", rx_ces[i], eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].phr_info);
      }
      payload_ptr+=sizeof(POWER_HEADROOM_CMD);
      break;
gauthier's avatar
gauthier committed
560 561 562 563
      case CRNTI:
        LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received CRNTI %d \n", rx_ces[i], payload_ptr[0]);
        payload_ptr+=1;
        break;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    case TRUNCATED_BSR:
    case SHORT_BSR: {
      if (ue_mod_id  != UE_INDEX_INVALID ){
	uint8_t lcgid;
	lcgid = (payload_ptr[0] >> 6);
	LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
	      rx_ces[i], lcgid, payload_ptr[0] & 0x3f);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[lcgid] = (payload_ptr[0] & 0x3f);
      }
      payload_ptr += 1;//sizeof(SHORT_BSR); // fixme
    } break;
    case LONG_BSR:
      if (ue_mod_id  != UE_INDEX_INVALID ){
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID0] = ((payload_ptr[0] & 0xFC) >> 2);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID1] =
	  ((payload_ptr[0] & 0x03) << 4) | ((payload_ptr[1] & 0xF0) >> 4);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID2] =
	  ((payload_ptr[1] & 0x0F) << 2) | ((payload_ptr[2] & 0xC0) >> 6);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID3] = (payload_ptr[2] & 0x3F);
	LOG_D(MAC, "[eNB] MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
	      "%u LCGID2 = %u LCGID3 = %u\n",
	      rx_ces[i],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID0],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID1],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID2],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID3]);
gauthier's avatar
gauthier committed
590
      }
591 592 593 594 595 596
      payload_ptr += 3;////sizeof(LONG_BSR);
      break;
    default:
      LOG_E(MAC, "[eNB] Received unknown MAC header (0x%02x)\n", rx_ces[i]);
      break;
    }
597
  }
598
  
599
  for (i=0;i<num_sdu;i++) {
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    LOG_D(MAC,"SDU Number %d MAC Subheader SDU_LCID %d, length %d\n",i,rx_lcids[i],rx_lengths[i]);
    
    switch (rx_lcids[i]) {
    case CCCH : 
      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Received CCCH:  %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
	    enb_mod_idP,frameP,
	    payload_ptr[0],payload_ptr[1],payload_ptr[2],payload_ptr[3],payload_ptr[4], payload_ptr[5], rntiP);

      for (ii=0;ii<NB_RA_PROC_MAX;ii++) {
	LOG_D(MAC,"[RAPROC] Checking proc %d : rnti (%x, %x), active %d\n",ii,
	      eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti, rntiP,
	      eNB_mac_inst[enb_mod_idP].RA_template[ii].RA_active);
	
	if ((eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti==rntiP) &&
	    (eNB_mac_inst[enb_mod_idP].RA_template[ii].RA_active==TRUE)) {
	  
          //payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
	  
	  if (ue_mod_id == UE_INDEX_INVALID) {
	    memcpy(&eNB_mac_inst[enb_mod_idP].RA_template[ii].cont_res_id[0],payload_ptr,6);
	    LOG_I(MAC,"[eNB %d][RAPROC] Frame %d CCCH: Received RRCConnectionRequest: length %d, offset %d\n",
                  enb_mod_idP,frameP,rx_lengths[ii],payload_ptr-sdu);
	    if ((ue_mod_id=add_new_ue(enb_mod_idP,eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti)) == -1 )
	      mac_xface->macphy_exit("[MAC][eNB] Max user count reached\n");
	    else 
	      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Added user with rnti %x => UE %d\n",
		    enb_mod_idP,frameP,eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti,ue_mod_id);
	  } else {
	     LOG_I(MAC,"[eNB %d][RAPROC] Frame %d CCCH: Received RRCConnectionReestablishment from UE %d: length %d, offset %d\n",
		   enb_mod_idP,frameP,ue_mod_id,rx_lengths[ii],payload_ptr-sdu);
	  }
	  
	  if (Is_rrc_registered == 1)
	    mac_rrc_data_ind(enb_mod_idP,frameP,CCCH,(uint8_t *)payload_ptr,rx_lengths[ii],1,enb_mod_idP,0);
	  
	  
          if (num_ce >0) {  // handle msg3 which is not RRCConnectionRequest
	    //	process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
	  }
	  
	  eNB_mac_inst[enb_mod_idP].RA_template[ii].generate_Msg4 = 1;
	  eNB_mac_inst[enb_mod_idP].RA_template[ii].wait_ack_Msg4 = 0;
	  
	  
	} // if process is active
	
      } // loop on RA processes
      
      break;
    case  DCCH : 
    case DCCH1 :
      //      if(eNB_mac_inst[module_idP].Dcch_lchan[UE_id].Active==1){
      
653
#if defined(ENABLE_MAC_PAYLOAD_DEBUG)
654 655 656 657
      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sdu));
      for (j=0;j<32;j++)
	LOG_T(MAC,"%x ",payload_ptr[j]);
      LOG_T(MAC,"\n");
658
#endif
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
      
      //  This check is just to make sure we didn't get a bogus SDU length, to be removed ...
      if (rx_lengths[i]<CCCH_PAYLOAD_SIZE_MAX) {
	LOG_D(MAC,"[eNB %d] Frame %d : ULSCH -> UL-DCCH, received %d bytes form UE %d on LCID %d(%d) \n",
	      enb_mod_idP,frameP, rx_lengths[i], ue_mod_id, rx_lcids[i], rx_lcids[i]);
	
	mac_rlc_data_ind(enb_mod_idP,ue_mod_id, frameP,ENB_FLAG_YES,MBMS_FLAG_NO,
			 rx_lcids[i],
			 (char *)payload_ptr,
			 rx_lengths[i],
			 1,
			 NULL);//(unsigned int*)crc_status);
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_pdu_rx[rx_lcids[i]]+=1;
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
	
      }
      //      }
      break;
    case DTCH: // default DRB 
      //      if(eNB_mac_inst[module_idP].Dcch_lchan[UE_id].Active==1){
	
680
#if defined(ENABLE_MAC_PAYLOAD_DEBUG)
681 682 683 684
      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sdu));
      for (j=0;j<32;j++)
	LOG_T(MAC,"%x ",payload_ptr[j]);
      LOG_T(MAC,"\n");
685
#endif
686 687 688 689 690 691 692 693 694 695 696 697 698 699
      
      LOG_D(MAC,"[eNB %d] Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d (%d)\n",
	    enb_mod_idP,frameP, rx_lengths[i], ue_mod_id,rx_lcids[i],rx_lcids[i]);
      
      if ((rx_lengths[i] <SCH_PAYLOAD_SIZE_MAX) &&  (rx_lengths[i] > 0) ) {   // MAX SIZE OF transport block
	mac_rlc_data_ind(enb_mod_idP,ue_mod_id, frameP,ENB_FLAG_YES,MBMS_FLAG_NO,
			 DTCH,
			 (char *)payload_ptr,
			   rx_lengths[i],
			 1,
			 NULL);//(unsigned int*)crc_status);
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_pdu_rx[rx_lcids[i]]+=1;
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
	  
700
      }
701 702 703 704 705 706 707 708 709
	//      }
      break;
    default :  //if (rx_lcids[i] >= DTCH) {
      eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_errors_rx+=1;
      LOG_E(MAC,"[eNB %d] received unsupported or unknown LCID %d from UE %d ", rx_lcids[i], ue_mod_id);
      break;
    }
    payload_ptr+=rx_lengths[i];
    
710 711
  }

712 713 714
  eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].total_pdu_bytes_rx+=sdu_len;
  eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].total_num_pdus_rx+=1;
  
715
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,0);
716
  stop_meas(&eNB_mac_inst[enb_mod_idP].rx_ulsch_sdu);
717 718 719
}

unsigned char generate_dlsch_header(unsigned char *mac_header,
gauthier's avatar
gauthier committed
720 721 722 723 724 725 726 727
    unsigned char num_sdus,
    unsigned short *sdu_lengths,
    unsigned char *sdu_lcids,
    unsigned char drx_cmd,
    short timing_advance_cmd,
    unsigned char *ue_cont_res_id,
    unsigned char short_padding,
    unsigned short post_padding) {
728 729

  SCH_SUBHEADER_FIXED *mac_header_ptr = (SCH_SUBHEADER_FIXED *)mac_header;
gauthier's avatar
gauthier committed
730 731
  uint8_t first_element=0,last_size=0,i;
  uint8_t mac_header_control_elements[16],*ce_ptr;
732 733 734 735

  ce_ptr = &mac_header_control_elements[0];

  // compute header components
gauthier's avatar
gauthier committed
736

737
  if ((short_padding == 1) || (short_padding == 2)) {
gauthier's avatar
gauthier committed
738 739 740 741 742
      mac_header_ptr->R    = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = SHORT_PADDING;
      first_element=1;
      last_size=1;
743 744
  }
  if (short_padding == 2) {
gauthier's avatar
gauthier committed
745 746 747 748 749 750
      mac_header_ptr->E = 1;
      mac_header_ptr++;
      mac_header_ptr->R = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = SHORT_PADDING;
      last_size=1;
751 752 753
  }

  if (drx_cmd != 255) {
gauthier's avatar
gauthier committed
754 755 756 757 758 759 760 761 762 763 764
      if (first_element>0) {
          mac_header_ptr->E = 1;
          mac_header_ptr++;
      }
      else {
          first_element=1;
      }
      mac_header_ptr->R = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = DRX_CMD;
      last_size=1;
765 766 767
  }

  if (timing_advance_cmd != 0) {
gauthier's avatar
gauthier committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
      if (first_element>0) {
          mac_header_ptr->E = 1;
          mac_header_ptr++;
      }
      else {
          first_element=1;
      }
      mac_header_ptr->R = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = TIMING_ADV_CMD;
      last_size=1;
      //    msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
      ((TIMING_ADVANCE_CMD *)ce_ptr)->R=0;
      ((TIMING_ADVANCE_CMD *)ce_ptr)->TA=timing_advance_cmd&0x3f;
      LOG_I(MAC,"timing advance =%d (%d)\n",timing_advance_cmd,((TIMING_ADVANCE_CMD *)ce_ptr)->TA);
      ce_ptr+=sizeof(TIMING_ADVANCE_CMD);
      //msg("offset %d\n",ce_ptr-mac_header_control_elements);
785 786 787
  }

  if (ue_cont_res_id) {
gauthier's avatar
gauthier committed
788 789 790
      if (first_element>0) {
          mac_header_ptr->E = 1;
          /*
791 792 793 794
	   printf("[eNB][MAC] last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
	   ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
	   ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
	   ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
gauthier's avatar
gauthier committed
795 796 797 798 799 800
           */
          mac_header_ptr++;
      }
      else {
          first_element=1;
      }
801

gauthier's avatar
gauthier committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
      mac_header_ptr->R = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = UE_CONT_RES;
      last_size=1;

      LOG_T(MAC,"[eNB ][RAPROC] Generate contention resolution msg: %x.%x.%x.%x.%x.%x\n",
          ue_cont_res_id[0],
          ue_cont_res_id[1],
          ue_cont_res_id[2],
          ue_cont_res_id[3],
          ue_cont_res_id[4],
          ue_cont_res_id[5]);

      memcpy(ce_ptr,ue_cont_res_id,6);
      ce_ptr+=6;
      // msg("(cont_res) : offset %d\n",ce_ptr-mac_header_control_elements);
818 819 820 821 822
  }

  //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);

  for (i=0;i<num_sdus;i++) {
gauthier's avatar
gauthier committed
823
      LOG_T(MAC,"[eNB] Generate DLSCH header num sdu %d len sdu %d\n",num_sdus, sdu_lengths[i]);
824

gauthier's avatar
gauthier committed
825 826 827
      if (first_element>0) {
          mac_header_ptr->E = 1;
          /*msg("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
828 829 830
	((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
	((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
	((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);
gauthier's avatar
gauthier committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
           */
          mac_header_ptr+=last_size;
          //msg("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
      }
      else {
          first_element=1;
      }
      if (sdu_lengths[i] < 128) {
          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = sdu_lcids[i];
          ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = (unsigned char)sdu_lengths[i];
          last_size=2;
      }
      else {
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = sdu_lcids[i];
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB    = ((unsigned short) sdu_lengths[i]>>8)&0x7f;
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB    = (unsigned short) sdu_lengths[i]&0xff;
          ((SCH_SUBHEADER_LONG *)mac_header_ptr)->padding   = 0x00;
          last_size=3;
855
#ifdef DEBUG_HEADER_PARSING
gauthier's avatar
gauthier committed
856 857 858 859
          LOG_D(MAC,"[eNB] generate long sdu, size %x (MSB %x, LSB %x)\n",
              sdu_lengths[i],
              ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_MSB,
              ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L_LSB);
860
#endif
gauthier's avatar
gauthier committed
861
      }
862 863 864
  }
  /*

865
    printf("last_size %d,mac_header_ptr %p\n",last_size,mac_header_ptr);
gauthier's avatar
gauthier committed
866

867 868 869 870 871 872 873 874 875 876 877 878
    printf("last subheader : %x (R%d,E%d,LCID%d)\n",*(unsigned char*)mac_header_ptr,
    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->R,
    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->E,
    ((SCH_SUBHEADER_FIXED *)mac_header_ptr)->LCID);


    if (((SCH_SUBHEADER_FIXED*)mac_header_ptr)->LCID < UE_CONT_RES) {
    if (((SCH_SUBHEADER_SHORT*)mac_header_ptr)->F == 0)
    printf("F = 0, sdu len (L field) %d\n",(((SCH_SUBHEADER_SHORT*)mac_header_ptr)->L));
    else
    printf("F = 1, sdu len (L field) %d\n",(((SCH_SUBHEADER_LONG*)mac_header_ptr)->L));
    }
gauthier's avatar
gauthier committed
879
   */
880
  if (post_padding>0) {// we have lots of padding at the end of the packet
gauthier's avatar
gauthier committed
881 882 883 884 885 886 887
      mac_header_ptr->E = 1;
      mac_header_ptr+=last_size;
      // add a padding element
      mac_header_ptr->R    = 0;
      mac_header_ptr->E    = 0;
      mac_header_ptr->LCID = SHORT_PADDING;
      mac_header_ptr++;
888 889
  }
  else { // no end of packet padding
gauthier's avatar
gauthier committed
890 891
      // last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
      mac_header_ptr++;
892 893
  }

gauthier's avatar
gauthier committed
894
  //msg("After subheaders %d\n",(uint8_t*)mac_header_ptr - mac_header);
gauthier's avatar
gauthier committed
895

896
  if ((ce_ptr-mac_header_control_elements) > 0) {
gauthier's avatar
gauthier committed
897 898 899
      // printf("Copying %d bytes for control elements\n",ce_ptr-mac_header_control_elements);
      memcpy((void*)mac_header_ptr,mac_header_control_elements,ce_ptr-mac_header_control_elements);
      mac_header_ptr+=(unsigned char)(ce_ptr-mac_header_control_elements);
900
  }
gauthier's avatar
gauthier committed
901
  //msg("After CEs %d\n",(uint8_t*)mac_header_ptr - mac_header);
902 903 904 905 906 907

  return((unsigned char*)mac_header_ptr - mac_header);

}

/*
908 909 910 911 912 913 914 915
  #ifdef Rel10
  unsigned char generate_mch_header( unsigned char *mac_header,
  unsigned char num_sdus,
  unsigned short *sdu_lengths,
  unsigned char *sdu_lcids,
  unsigned char msi,
  unsigned char short_padding,
  unsigned short post_padding) {
916 917

  SCH_SUBHEADER_FIXED *mac_header_ptr = (SCH_SUBHEADER_FIXED *)mac_header;
gauthier's avatar
gauthier committed
918 919
  uint8_t first_element=0,last_size=0,i;
  uint8_t mac_header_control_elements[2*num_sdus],*ce_ptr;
920 921 922 923

  ce_ptr = &mac_header_control_elements[0];

  if ((short_padding == 1) || (short_padding == 2)) {
924 925 926 927 928
  mac_header_ptr->R    = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  first_element=1;
  last_size=1;
929 930
  }
  if (short_padding == 2) {
931 932 933 934 935 936
  mac_header_ptr->E = 1;
  mac_header_ptr++;
  mac_header_ptr->R = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  last_size=1;
937 938 939 940
  }

  // SUBHEADER for MSI CE
  if (msi != 0) {// there is MSI MAC Control Element
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
  if (first_element>0) {
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  }
  else {
  first_element = 1;
  }
  if (num_sdus*2 < 128) {
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = MCH_SCHDL_INFO;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = num_sdus*2;
  last_size=2;
  }
  else {
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = MCH_SCHDL_INFO;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L    = (num_sdus*2)&0x7fff;
  last_size=3;
  }
  // Create the MSI MAC Control Element here
965
  }
gauthier's avatar
gauthier committed
966

967 968
  // SUBHEADER for MAC SDU (MCCH+MTCHs)
  for (i=0;i<num_sdus;i++) {
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
  if (first_element>0) {
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  }
  else {
  first_element = 1;
  }
  if (sdu_lengths[i] < 128) {
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->F    = 0;
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->LCID = sdu_lcids[i];
  ((SCH_SUBHEADER_SHORT *)mac_header_ptr)->L    = (unsigned char)sdu_lengths[i];
  last_size=2;
  }
  else {
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->R    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->E    = 0;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->F    = 1;
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->LCID = sdu_lcids[i];
  ((SCH_SUBHEADER_LONG *)mac_header_ptr)->L    = (unsigned short) sdu_lengths[i]&0x7fff;
  last_size=3;
  }
992 993 994
  }

  if (post_padding>0) {// we have lots of padding at the end of the packet
995 996 997 998 999 1000 1001
  mac_header_ptr->E = 1;
  mac_header_ptr+=last_size;
  // add a padding element
  mac_header_ptr->R    = 0;
  mac_header_ptr->E    = 0;
  mac_header_ptr->LCID = SHORT_PADDING;
  mac_header_ptr++;
1002 1003
  }
  else { // no end of packet padding
1004 1005
  // last SDU subhead is of fixed type (sdu length implicitly to be computed at UE)
  mac_header_ptr++;
1006 1007 1008 1009
  }

  // Copy MSI Control Element to the end of the MAC Header if it presents
  if ((ce_ptr-mac_header_control_elements) > 0) {
1010 1011 1012
  // printf("Copying %d bytes for control elements\n",ce_ptr-mac_header_control_elements);
  memcpy((void*)mac_header_ptr,mac_header_control_elements,ce_ptr-mac_header_control_elements);
  mac_header_ptr+=(unsigned char)(ce_ptr-mac_header_control_elements);
1013 1014 1015
  }

  return((unsigned char*)mac_header_ptr - mac_header);
1016 1017
  }
  #endif
gauthier's avatar
gauthier committed
1018
 */
1019
void add_common_dci(DCI_PDU *DCI_pdu,
gauthier's avatar
gauthier committed
1020 1021 1022 1023 1024 1025
    void *pdu,
    rnti_t rnti,
    unsigned char dci_size_bytes,
    unsigned char aggregation,
    unsigned char dci_size_bits,
    unsigned char dci_fmt,
gauthier's avatar
gauthier committed
1026
    uint8_t ra_flag) {
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

  memcpy(&DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].dci_pdu[0],pdu,dci_size_bytes);
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].dci_length = dci_size_bits;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].L          = aggregation;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].rnti       = rnti;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].format     = dci_fmt;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci].ra_flag    = ra_flag;


  DCI_pdu->Num_common_dci++;
gauthier's avatar
gauthier committed
1037
  LOG_D(MAC,"add common dci format %d for rnti %d \n",dci_fmt,rnti);
1038 1039
}

gauthier's avatar
gauthier committed
1040
void add_ue_spec_dci(DCI_PDU *DCI_pdu,void *pdu,rnti_t rnti,unsigned char dci_size_bytes,unsigned char aggregation,unsigned char dci_size_bits,unsigned char dci_fmt,uint8_t ra_flag) {
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

  memcpy(&DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].dci_pdu[0],pdu,dci_size_bytes);
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].dci_length = dci_size_bits;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].L          = aggregation;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].rnti       = rnti;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].format     = dci_fmt;
  DCI_pdu->dci_alloc[DCI_pdu->Num_common_dci+DCI_pdu->Num_ue_spec_dci].ra_flag    = ra_flag;

  DCI_pdu->Num_ue_spec_dci++;
}

gauthier's avatar
gauthier committed
1052
void schedule_SI(module_id_t module_idP,frame_t frameP, unsigned char *nprb,unsigned int *nCCE) {
1053

1054
  start_meas(&eNB_mac_inst[module_idP].schedule_si);
1055

1056
  unsigned char bcch_sdu_length;
winckel's avatar
winckel committed
1057
  int mcs = -1;
gauthier's avatar
gauthier committed
1058 1059 1060 1061 1062 1063 1064 1065 1066
  void *BCCH_alloc_pdu=(void*)&eNB_mac_inst[module_idP].BCCH_alloc_pdu;

  bcch_sdu_length = mac_rrc_data_req(module_idP,
      frameP,
      BCCH,1,
      &eNB_mac_inst[module_idP].BCCH_pdu.payload[0],
      1,
      module_idP,
      0); // not used in this case
1067
  if (bcch_sdu_length > 0) {
gauthier's avatar
gauthier committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
      LOG_D(MAC,"[eNB %d] Frame %d : BCCH->DLSCH, Received %d bytes \n",module_idP,frameP,bcch_sdu_length);


      if (bcch_sdu_length <= (mac_xface->get_TBS_DL(0,3)))
        mcs=0;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(1,3)))
        mcs=1;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(2,3)))
        mcs=2;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(3,3)))
        mcs=3;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(4,3)))
        mcs=4;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(5,3)))
        mcs=5;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(6,3)))
        mcs=6;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(7,3)))
        mcs=7;
      else if (bcch_sdu_length <= (mac_xface->get_TBS_DL(8,3)))
        mcs=8;
1089

gauthier's avatar
gauthier committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
      if (mac_xface->lte_frame_parms->frame_type == TDD) {
          switch (mac_xface->lte_frame_parms->N_RB_DL) {
          case 6:
            ((DCI1A_1_5MHz_TDD_1_6_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 25:
            ((DCI1A_5MHz_TDD_1_6_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 50:
            ((DCI1A_10MHz_TDD_1_6_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 100:
            ((DCI1A_20MHz_TDD_1_6_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;

          }
1106
      }
gauthier's avatar
gauthier committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
      else {
          switch (mac_xface->lte_frame_parms->N_RB_DL) {
          case 6:
            ((DCI1A_1_5MHz_FDD_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 25:
            ((DCI1A_5MHz_FDD_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 50:
            ((DCI1A_10MHz_FDD_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;
          case 100:
            ((DCI1A_20MHz_FDD_t*)BCCH_alloc_pdu)->mcs = mcs;
            break;

          }