eNB_scheduler_mch.c 29.2 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


11 12 13 14
    OpenAirInterface is distributed in the hope that 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.
15

16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
22 23 24 25 26 27 28
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

*******************************************************************************/
29 30

/*! \file eNB_scheduler_mch.c
31
 * \brief procedures related to eNB for the MCH transport channel
32 33 34
 * \author  Navid Nikaein and Raymond Knopp
 * \date 2012 - 2014
 * \email: navid.nikaein@eurecom.fr
35
 * \version 1.0
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * @ingroup _mac

 */

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

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

#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/proto.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"

//#include "LAYER2/MAC/pre_processor.c"
#include "pdcp.h"

#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

#define ENABLE_MAC_PAYLOAD_DEBUG
#define DEBUG_eNB_SCHEDULER 1

Lionel Gauthier's avatar
 
Lionel Gauthier committed
72
 extern inline unsigned int taus(void);
73 74

#ifdef Rel10
75
int8_t get_mbsfn_sf_alloction (module_id_t module_idP, uint8_t CC_id, uint8_t mbsfn_sync_area){
76 77 78 79 80
  // currently there is one-to-one mapping between sf allocation pattern and sync area
  if (mbsfn_sync_area > MAX_MBSFN_AREA){
      LOG_W(MAC,"[eNB %d] MBSFN synchronization area %d out of range\n ", module_idP, mbsfn_sync_area);
      return -1;
  }
81
  else if (eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[mbsfn_sync_area] != NULL)
82 83 84 85 86 87 88
    return mbsfn_sync_area;
  else {
      LOG_W(MAC,"[eNB %d] MBSFN Subframe Config pattern %d not found \n ", module_idP, mbsfn_sync_area);
      return -1;
  }
}

89
int schedule_MBMS(module_id_t module_idP, uint8_t CC_id, frame_t frameP, sub_frame_t subframeP) {
90 91 92 93

  int mcch_flag=0,mtch_flag=0, msi_flag=0;
  int mbsfn_period =0;// 1<<(eNB_mac_inst[module_idP].mbsfn_SubframeConfig[0]->radioframeAllocationPeriod);
  int mcch_period = 0;//32<<(eNB_mac_inst[module_idP].mbsfn_AreaInfo[0]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
94
  int mch_scheduling_period = 8<<(eNB_mac_inst[module_idP].common_channels[CC_id].pmch_Config[0]->mch_SchedulingPeriod_r9);
95 96 97 98 99 100 101 102 103 104 105 106
  unsigned char mcch_sdu_length;
  unsigned char header_len_mcch=0,header_len_msi=0,header_len_mtch=0, header_len_mtch_temp=0, header_len_mcch_temp=0, header_len_msi_temp=0;
  int ii=0, msi_pos=0;
  int mcch_mcs = -1;
  uint16_t TBS,j,padding=0,post_padding=0;
  mac_rlc_status_resp_t rlc_status;
  int num_mtch;
  int msi_length,i,k;
  unsigned char sdu_lcids[11], num_sdus=0, offset=0;
  uint16_t sdu_lengths[11], sdu_length_total=0;
  unsigned char mch_buffer[MAX_DLSCH_PAYLOAD_BYTES]; // check the max value, this is for dlsch only

107
  eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.Pdu_size=0;
108 109

  for (i=0;
110
      i< eNB_mac_inst[module_idP].common_channels[CC_id].num_active_mbsfn_area;
111 112
      i++ ){
      // assume, that there is always a mapping
113
    if ((j=get_mbsfn_sf_alloction(module_idP,CC_id,i)) == -1)
114 115
        return 0;

116 117
      mbsfn_period = 1<<(eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->radioframeAllocationPeriod);
      mcch_period = 32<<(eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_RepetitionPeriod_r9);
118 119
      msi_pos=0; ii=0;
      LOG_D(MAC,"[eNB %d] Frame %d subframeP %d : Checking MBSFN Sync Area %d/%d with SF allocation %d/%d for MCCH and MTCH (mbsfn period %d, mcch period %d)\n",
120 121
          module_idP,frameP, subframeP,i,eNB_mac_inst[module_idP].common_channels[CC_id].num_active_mbsfn_area,
          j,eNB_mac_inst[module_idP].common_channels[CC_id].num_sf_allocation_pattern,mbsfn_period,mcch_period);
122 123


124
      switch (eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.signallingMCS_r9) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      case 0:
        mcch_mcs = 2;
        break;
      case 1:
        mcch_mcs = 7;
        break;
      case 2:
        mcch_mcs = 13;
        break;
      case 3:
        mcch_mcs = 19;
        break;
      }

      // 1st: Check the MBSFN subframes from SIB2 info (SF allocation pattern i, max 8 non-overlapping patterns exist)
140 141
      if (frameP %  mbsfn_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->radioframeAllocationOffset){ // MBSFN frameP
          if (eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.present == MBSFN_SubframeConfig__subframeAllocation_PR_oneFrame){// one-frameP format
142 143

              //  Find the first subframeP in this MCH to transmit MSI
144
              if (frameP % mch_scheduling_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->radioframeAllocationOffset ) {
145
                  while (ii == 0) {
146
                      ii = eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & (0x80>>msi_pos);
147 148
                      msi_pos++;
                  }
149
                  LOG_D(MAC,"[eNB %d] Frame %d subframeP %d : sync area %d sf allocation pattern %d sf alloc %x msi pos is %d \n", module_idP,frameP, subframeP,i,j,eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0], msi_pos);
150 151 152 153 154 155
              }

              // Check if the subframeP is for MSI, MCCH or MTCHs and Set the correspoding flag to 1
              switch (subframeP) {
              case 1:
                if (mac_xface->lte_frame_parms->frame_type == FDD) {
156
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) {
157 158
                        if (msi_pos == 1)
                          msi_flag = 1;
159 160
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF1) == MBSFN_FDD_SF1) )
161 162 163 164 165 166 167 168
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 2:
                if (mac_xface->lte_frame_parms->frame_type == FDD){
169
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) {
170 171
                        if (msi_pos == 2)
                          msi_flag = 1;
172 173
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF2) == MBSFN_FDD_SF2) )
174 175 176 177 178 179 180 181
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 3:
                if (mac_xface->lte_frame_parms->frame_type == TDD){// TDD
182
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) {
183 184
                        if (msi_pos == 1)
                          msi_flag = 1;
185 186
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF3) == MBSFN_TDD_SF3) )
187 188 189 190 191
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                else {// FDD
192
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) {
193 194
                        if (msi_pos == 3)
                          msi_flag = 1;
195 196
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF3) == MBSFN_FDD_SF3) )
197 198 199 200 201 202 203 204
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 4:
                if (mac_xface->lte_frame_parms->frame_type == TDD){
205
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) {
206 207
                        if (msi_pos == 2)
                          msi_flag = 1;
208 209
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF4) == MBSFN_TDD_SF4) )
210 211 212 213 214 215 216 217
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 6:
                if (mac_xface->lte_frame_parms->frame_type == FDD){
218
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) {
219 220
                        if (msi_pos == 4)
                          msi_flag = 1;
221 222
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF6) == MBSFN_FDD_SF6) )
223 224 225 226 227 228 229 230
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 7:
                if (mac_xface->lte_frame_parms->frame_type == TDD){ // TDD
231
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) {
232 233
                        if (msi_pos == 3)
                          msi_flag = 1;
234 235
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF7) == MBSFN_TDD_SF7) )
236 237 238 239 240
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                else {// FDD
241
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) {
242 243
                        if (msi_pos == 5)
                          msi_flag = 1;
244 245
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF7) == MBSFN_FDD_SF7) )
246 247 248 249 250 251 252 253
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 8:
                if (mac_xface->lte_frame_parms->frame_type == TDD){ //TDD
254
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) {
255 256
                        if (msi_pos == 4)
                          msi_flag = 1;
257 258
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF8) == MBSFN_TDD_SF8) )
259 260 261 262 263
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                else{ // FDD
264
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) {
265 266
                        if (msi_pos == 6)
                          msi_flag = 1;
267 268
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_FDD_SF8) == MBSFN_FDD_SF8) )
269 270 271 272 273 274 275 276
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;

              case 9:
                if (mac_xface->lte_frame_parms->frame_type == TDD){
277
                    if ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_SubframeConfig[j]->subframeAllocation.choice.oneFrame.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) {
278 279
                        if (msi_pos == 5)
                          msi_flag = 1;
280 281
                        if ( (frameP % mcch_period == eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.mcch_Offset_r9) &&
                            ((eNB_mac_inst[module_idP].common_channels[CC_id].mbsfn_AreaInfo[i]->mcch_Config_r9.sf_AllocInfo_r9.buf[0] & MBSFN_TDD_SF9) == MBSFN_TDD_SF9) )
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                          mcch_flag = 1;
                        mtch_flag = 1;
                    }
                }
                break;
              }// end switch
              // sf allocation is non-overlapping
              if ((msi_flag==1) || (mcch_flag==1) || (mtch_flag==1)){
                  LOG_D(MAC,"[eNB %d] Frame %d Subframe %d: sync area %d SF alloc %d: msi flag %d, mcch flag %d, mtch flag %d\n",
                      module_idP, frameP, subframeP,i,j,msi_flag,mcch_flag,mtch_flag);
                  break;
              }
          }
          else {// four-frameP format
          }
      }
  } // end of for loop
299 300 301
  eNB_mac_inst[module_idP].common_channels[CC_id].msi_active=0;
  eNB_mac_inst[module_idP].common_channels[CC_id].mcch_active=0;
  eNB_mac_inst[module_idP].common_channels[CC_id].mtch_active=0;
302 303
  // Calculate the mcs
  if ((msi_flag==1) || (mcch_flag==1)) {
304
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcs = mcch_mcs;
305 306
  }
  else if (mtch_flag == 1) { // only MTCH in this subframeP
307
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcs = eNB_mac_inst[module_idP].common_channels[CC_id].pmch_Config[0]->dataMCS_r9;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  }


  // 2nd: Create MSI, get MCCH from RRC and MTCHs from RLC

  // there is MSI (MCH Scheduling Info)
  if (msi_flag == 1) {
      // Create MSI here
      uint16_t msi_control_element[29], *msi_ptr;

      msi_ptr = &msi_control_element[0];
      ((MSI_ELEMENT *) msi_ptr)->lcid = MCCH_LCHANID; //MCCH
      if (mcch_flag==1) {
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0;
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0;
      }
      else {                    // no mcch for this MSP
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0x7;// stop value is 2047
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xff;
      }
      msi_ptr+= sizeof(MSI_ELEMENT);

      //Header for MTCHs
331
      num_mtch = eNB_mac_inst[module_idP].common_channels[CC_id].mbms_SessionList[0]->list.count;
332
      for (k=0;k<num_mtch;k++) { // loop for all session in this MCH (MCH[0]) at this moment
333
          ((MSI_ELEMENT *) msi_ptr)->lcid = eNB_mac_inst[module_idP].common_channels[CC_id].mbms_SessionList[0]->list.array[k]->logicalChannelIdentity_r9;//mtch_lcid;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_MSB = 0; // last subframeP of this mtch (only one mtch now)
          ((MSI_ELEMENT *) msi_ptr)->stop_sf_LSB = 0xB;
          msi_ptr+=sizeof(MSI_ELEMENT);
      }
      msi_length = msi_ptr-msi_control_element;
      if (msi_length<128)
        header_len_msi = 2;
      else
        header_len_msi = 3;

      LOG_D(MAC,"[eNB %d] Frame %d : MSI->MCH, length of MSI is %d bytes \n",module_idP,frameP,msi_length);
      //LOG_D(MAC,"Scheduler: MSI is transmitted in this subframeP \n" );

      //   LOG_D(MAC,"Scheduler: MSI length is %d bytes\n",msi_length);
      // Store MSI data to mch_buffer[0]
      memcpy((char *)&mch_buffer[sdu_length_total],
          msi_control_element,
          msi_length);

      sdu_lcids[num_sdus] = MCH_SCHDL_INFO;
      sdu_lengths[num_sdus] = msi_length;
      sdu_length_total += sdu_lengths[num_sdus];
      LOG_I(MAC,"[eNB %d] Create %d bytes for MSI\n",module_idP,sdu_lengths[num_sdus]);
      num_sdus++;
358
      eNB_mac_inst[module_idP].common_channels[CC_id].msi_active=1;
359 360 361 362 363 364 365 366 367 368
  }

  // there is MCCH
  if (mcch_flag == 1) {
      LOG_D(MAC,"[eNB %d] Frame %d Subframe %d: Schedule MCCH MESSAGE (area %d, sfAlloc %d)\n",
          module_idP,frameP, subframeP, i, j);

      mcch_sdu_length = mac_rrc_data_req(module_idP,
          frameP,
          MCCH,1,
369
          &eNB_mac_inst[module_idP].common_channels[CC_id].MCCH_pdu.payload[0],
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
          1,// this is eNB
          module_idP, // index
          i); // this is the mbsfn sync area index

      if (mcch_sdu_length > 0) {
          LOG_D(MAC,"[eNB %d] Frame %d subframeP %d : MCCH->MCH, Received %d bytes from RRC \n",module_idP,frameP,subframeP,mcch_sdu_length);

          header_len_mcch = 2;
          if (mac_xface->lte_frame_parms->frame_type == TDD) {
              LOG_D(MAC,"[eNB %d] Frame %d subframeP %d: Scheduling MCCH->MCH (TDD) for MCCH message %d bytes (mcs %d )\n",
                  module_idP,
                  frameP,subframeP,
                  mcch_sdu_length,
                  mcch_mcs);
          }
          else {
              LOG_I(MAC,"[eNB %d] Frame %d subframeP %d: Scheduling MCCH->MCH (FDD) for MCCH message %d bytes (mcs %d)\n",
                  module_idP,
                  frameP, subframeP,
                  mcch_sdu_length,
                  mcch_mcs);
          }
392
          eNB_mac_inst[module_idP].common_channels[CC_id].mcch_active=1;
393 394

          memcpy((char *)&mch_buffer[sdu_length_total],
395
              &eNB_mac_inst[module_idP].common_channels[CC_id].MCCH_pdu.payload[0],
396 397 398 399 400 401 402 403 404 405
              mcch_sdu_length);
          sdu_lcids[num_sdus] = MCCH_LCHANID;
          sdu_lengths[num_sdus] = mcch_sdu_length;
          if (sdu_lengths[num_sdus]>128)
            header_len_mcch = 3;
          sdu_length_total += sdu_lengths[num_sdus];
          LOG_D(MAC,"[eNB %d] Got %d bytes for MCCH from RRC \n",module_idP,sdu_lengths[num_sdus]);
          num_sdus++;
      }
  }
406
  TBS = mac_xface->get_TBS_DL(eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcs, mac_xface->lte_frame_parms->N_RB_DL);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
#ifdef Rel10
  // do not let mcch and mtch multiplexing when relaying is active
  // for sync area 1, so not transmit data
  //if ((i == 0) && ((eNB_mac_inst[module_idP].MBMS_flag != multicast_relay) || (eNB_mac_inst[module_idP].mcch_active==0))) {
#endif
  // there is MTCHs, loop if there are more than 1
  if (mtch_flag == 1) {
      // Calculate TBS
      /* if ((msi_flag==1) || (mcch_flag==1)) {
       TBS = mac_xface->get_TBS(mcch_mcs, mac_xface->lte_frame_parms->N_RB_DL);
       }
       else { // only MTCH in this subframeP
       TBS = mac_xface->get_TBS(eNB_mac_inst[module_idP].pmch_Config[0]->dataMCS_r9, mac_xface->lte_frame_parms->N_RB_DL);
       }

    // get MTCH data from RLC (like for DTCH)
    LOG_D(MAC,"[eNB %d] Frame %d subframe %d: Schedule MTCH (area %d, sfAlloc %d)\n",Mod_id,frame,subframe,i,j);

    header_len_mtch = 3;
	  LOG_D(MAC,"[eNB %d], Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
	  Mod_id,frame,MTCH,TBS,
	  TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);

    rlc_status = mac_rlc_status_ind(Mod_id,frame,1,RLC_MBMS_YES,MTCH+ (maxDRB + 3) * MAX_MOBILES_PER_RG,
				    TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
    printf("frame %d, subframe %d,  rlc_status.bytes_in_buffer is %d\n",frame,subframe, rlc_status.bytes_in_buffer);

       */

      // get MTCH data from RLC (like for DTCH)
      LOG_D(MAC,"[eNB %d] Frame %d subframeP %d: Schedule MTCH (area %d, sfAlloc %d)\n",module_idP,frameP,subframeP,i,j);

      header_len_mtch = 3;
      LOG_D(MAC,"[eNB %d], Frame %d, MTCH->MCH, Checking RLC status (rab %d, tbs %d, len %d)\n",
          module_idP,frameP,MTCH,TBS,
          TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);

      rlc_status = mac_rlc_status_ind(module_idP,0,frameP,ENB_FLAG_YES,MBMS_FLAG_YES,MTCH,
          TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch);
      LOG_D(MAC,"e-MBMS log channel %u frameP %d, subframeP %d,  rlc_status.bytes_in_buffer is %d\n",
            MTCH,frameP,subframeP, rlc_status.bytes_in_buffer);

      if (rlc_status.bytes_in_buffer >0) {
          LOG_I(MAC,"[eNB %d][MBMS USER-PLANE], Frame %d, MTCH->MCH, Requesting %d bytes from RLC (header len mtch %d)\n",
              module_idP,frameP,TBS-header_len_mcch-header_len_msi-sdu_length_total-header_len_mtch,header_len_mtch);

          sdu_lengths[num_sdus] = mac_rlc_data_req(
              module_idP,
              0,
              frameP,
              ENB_FLAG_YES,
              MBMS_FLAG_YES,
              MTCH,
              (char*)&mch_buffer[sdu_length_total]);
          //sdu_lengths[num_sdus] = mac_rlc_data_req(module_idP,frameP, MBMS_FLAG_NO,  MTCH+(MAX_NUM_RB*(NUMBER_OF_UE_MAX+1)), (char*)&mch_buffer[sdu_length_total]);
          LOG_I(MAC,"[eNB %d][MBMS USER-PLANE] Got %d bytes for MTCH %d\n",module_idP,sdu_lengths[num_sdus],MTCH);
463
          eNB_mac_inst[module_idP].common_channels[CC_id].mtch_active=1;
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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
          sdu_lcids[num_sdus] = MTCH;
          sdu_length_total += sdu_lengths[num_sdus];
          if (sdu_lengths[num_sdus] < 128)
            header_len_mtch = 2;
          num_sdus++;
      }
      else {
          header_len_mtch = 0;
      }
  }
#ifdef Rel10
  //  }
#endif
  // FINAL STEP: Prepare and multiplexe MSI, MCCH and MTCHs
  if ((sdu_length_total + header_len_msi + header_len_mcch + header_len_mtch) >0) {
      // Adjust the last subheader
      /*                                 if ((msi_flag==1) || (mcch_flag==1)) {
                                         eNB_mac_inst[module_idP].MCH_pdu.mcs = mcch_mcs;
                                          }
                                        else if (mtch_flag == 1) { // only MTCH in this subframeP
                                       eNB_mac_inst[module_idP].MCH_pdu.mcs = eNB_mac_inst[module_idP].pmch_Config[0]->dataMCS_r9;
                                          }
       */
      header_len_mtch_temp = header_len_mtch;
      header_len_mcch_temp = header_len_mcch;
      header_len_msi_temp = header_len_msi;
      if (header_len_mtch>0)
        header_len_mtch=1;         // remove Length field in the  subheader for the last PDU
      else if (header_len_mcch>0)
        header_len_mcch=1;
      else header_len_msi=1;
      // Calculate the padding
      if ((TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total) < 0) {
	LOG_E(MAC,"Error in building MAC PDU, TBS %d < PDU %d \n", 
	      TBS, header_len_mtch + header_len_mcch + header_len_msi + sdu_length_total);
	return;
      }else if ((TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total) <= 2) {
          padding = (TBS - header_len_mtch - header_len_mcch - header_len_msi - sdu_length_total);
          post_padding = 0;
      }
      else {// using post_padding, give back the Length field of subheader  for the last PDU
          padding = 0;
          if (header_len_mtch>0)
            header_len_mtch = header_len_mtch_temp;
          else if (header_len_mcch>0)
            header_len_mcch = header_len_mcch_temp;
          else header_len_msi = header_len_msi_temp;
          post_padding = TBS - sdu_length_total - header_len_msi - header_len_mcch - header_len_mtch;
      }
      // Generate the MAC Header for MCH
      // here we use the function for DLSCH because DLSCH & MCH have the same Header structure
515
      offset = generate_dlsch_header((unsigned char*)eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.payload,
516 517 518 519 520 521 522 523 524
          num_sdus,
          sdu_lengths,
          sdu_lcids,
          255,    // no drx
          0,  // no timing advance
          NULL,  // no contention res id
          padding,
          post_padding);

525 526 527 528 529 530 531
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.Pdu_size=TBS;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.sync_area=i;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.msi_active= eNB_mac_inst[module_idP].common_channels[CC_id].msi_active;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcch_active= eNB_mac_inst[module_idP].common_channels[CC_id].mcch_active;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mtch_active= eNB_mac_inst[module_idP].common_channels[CC_id].mtch_active;
      LOG_D(MAC," MCS for this sf is %d (mcch active %d, mtch active %d)\n", eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcs,
          eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcch_active,eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mtch_active );
532
      LOG_I(MAC,"[eNB %d][MBMS USER-PLANE ] Generate header : sdu_length_total %d, num_sdus %d, sdu_lengths[0] %d, sdu_lcids[0] %d => payload offset %d,padding %d,post_padding %d (mcs %d, TBS %d), header MTCH %d, header MCCH %d, header MSI %d\n",
533
          module_idP,sdu_length_total,num_sdus,sdu_lengths[0],sdu_lcids[0],offset,padding,post_padding,eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcs,TBS,header_len_mtch, header_len_mcch, header_len_msi);
534
      // copy SDU to mch_pdu after the MAC Header
535
      memcpy(&eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.payload[offset],mch_buffer,sdu_length_total);
536 537
      // filling remainder of MCH with random data if necessery
      for (j=0;j<(TBS-sdu_length_total-offset);j++)
538
        eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.payload[offset+sdu_length_total+j] = (char)(taus()&0xff);
539 540 541 542

#if defined(USER_MODE) && defined(OAI_EMU)
      /* Tracing of PDU is done on UE side */
      if (oai_emulation.info.opt_enabled)
543
        trace_pdu(1, (uint8_t *)eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.payload,
544 545 546 547 548 549 550 551 552 553 554 555
            TBS, module_idP, 6, 0xffff,  // M_RNTI = 6 in wirehsark
            eNB_mac_inst[module_idP].subframe,0,0);
      LOG_D(OPT,"[eNB %d][MCH] Frame %d : MAC PDU with size %d\n",
          module_idP, frameP, TBS);
#endif
      /*
   for (j=0;j<sdu_length_total;j++)
      printf("%2x.",eNB_mac_inst[module_idP].MCH_pdu.payload[j+offset]);
      printf(" \n");*/
      return 1;
  }
  else {
556 557 558 559 560
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.Pdu_size=0;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.sync_area=0;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.msi_active=0;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mcch_active=0;
      eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu.mtch_active=0;
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
      // for testing purpose, fill with random data
      //for (j=0;j<(TBS-sdu_length_total-offset);j++)
      //  eNB_mac_inst[module_idP].MCH_pdu.payload[offset+sdu_length_total+j] = (char)(taus()&0xff);
      return 0;
  }
  //this is for testing
  /*
  if (mtch_flag == 1) {
  //  LOG_D(MAC,"DUY: mch_buffer length so far is : %ld\n", &mch_buffer[sdu_length_total]-&mch_buffer[0]);
  return 1;
  }
  else
  return 0;
   */
}
576
MCH_PDU *get_mch_sdu(uint8_t module_idP, uint8_t CC_id, uint32_t frameP, sub_frame_t subframeP) {
577 578
  //  eNB_mac_inst[module_idP].MCH_pdu.mcs=0;
  //LOG_D(MAC," MCH_pdu.mcs is %d\n", eNB_mac_inst[module_idP].MCH_pdu.mcs);
579 580
#warning "MCH pdu should take the CC_id index"
  return(&eNB_mac_inst[module_idP].common_channels[CC_id].MCH_pdu);
581 582 583
}

#endif