eNB_scheduler_dlsch_NB_IoT.c 40.4 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
/*! \file eNB_scheduler_dlsch_NB_IoT.c
22
 * \brief handle DL UE-specific scheduling
23 24 25
 * \author  NTUST BMW Lab./Xavier LIU
 * \date 2017 - 2018
 * \email: sephiroth7277@gmail.com 
26
 * \version 1.0
27
 *
28
 */
29 30 31

#include "defs_NB_IoT.h"
#include "proto_NB_IoT.h"
32 33
#include "extern_NB_IoT.h"   

Nick Ho's avatar
Nick Ho committed
34 35 36 37 38 39 40 41 42
uint8_t from_R_dl_to_idx(uint8_t R)
{
	int it;
	for(it=0; it<16;it++)
	{
		if(R==R_dl_table[it])
			return it;
	}
	return -1;
43

Nick Ho's avatar
Nick Ho committed
44
}
Nick Ho's avatar
Nick Ho committed
45
/*DL scheduler*/
46
int schedule_DL_NB_IoT(module_id_t module_id, eNB_MAC_INST_NB_IoT *mac_inst, UE_TEMPLATE_NB_IoT *UE_info, uint32_t hyperSF_start, uint32_t frame_start, uint32_t subframe_start, UE_SCHED_CTRL_NB_IoT_t *UE_sched_ctrl_info)
47
{
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
48
        //number of candidate
49 50 51 52 53 54 55
	int cdd_num;
	//Transport block size
	int TBS;
	//Scheduling result buffer
	sched_temp_DL_NB_IoT_t *NPDCCH_info = (sched_temp_DL_NB_IoT_t*)malloc(sizeof(sched_temp_DL_NB_IoT_t));
	sched_temp_DL_NB_IoT_t *NPDSCH_info = (sched_temp_DL_NB_IoT_t*)malloc(sizeof(sched_temp_DL_NB_IoT_t));
	sched_temp_UL_NB_IoT_t *HARQ_info = (sched_temp_UL_NB_IoT_t*)malloc(sizeof(sched_temp_UL_NB_IoT_t));
56
	
57
	//DCI N1
58 59
	//DCIFormatN1_t *DCI_N1 = (DCIFormatN1_t*)malloc(sizeof(DCIFormatN1_t));
	
60
	//RLC Status
61 62 63
	
	//mac_rlc_status_resp_NB_IoT_t rlc_status;
	
64 65 66 67 68 69 70 71 72 73 74 75 76
	/*Index in DCI_N1*/
	uint32_t I_mcs, I_tbs, I_delay, I_sf;
	/*value for corresponding index*/
	/*Number of subframe per repetition*/
	int n_sf;
	/*flag*/
	int end_flagCCH=0;
	int end_flagSCH=0;
	int end_flagHARQ=0;
	int flag_retransmission=0;

	int HARQ_delay=0;
	uint32_t data_size;
Nick Ho's avatar
Nick Ho committed
77
	uint32_t mac_sdu_size; //
Nick Ho's avatar
Nick Ho committed
78
	uint32_t mac_sdu_size2; //
79

Nick Ho's avatar
Nick Ho committed
80
	uint8_t sdu_temp[SCH_PAYLOAD_SIZE_MAX_NB_IoT]; //
Nick Ho's avatar
Nick Ho committed
81 82
	uint8_t sdu_temp2[SCH_PAYLOAD_SIZE_MAX_NB_IoT]; //

83
	logical_chan_id_t logical_channel; //
84

85
	uint32_t subheader_length=3;
86
	
87
	uint32_t payload_offset; //
88
	
89 90

	uint32_t search_space_end_sf, h_temp, f_temp, sf_temp;
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
91
        mac_rlc_status_resp_t rlc_status; //Declare rlc_status
Nick Ho's avatar
Nick Ho committed
92
        mac_rlc_status_resp_t rlc_status2; //Declare rlc_status
93

94 95
	I_mcs = get_I_mcs(UE_info->CE_level);
	//I_mcs = 6;
96 97 98
	I_tbs = I_mcs;
	//get max TBS
	TBS = get_max_tbs(I_tbs);
Nick Ho's avatar
Nick Ho committed
99

100 101 102 103 104 105 106 107 108 109 110
	if(UE_info->HARQ_round==0)
	{
		flag_retransmission=0;
		data_size=UE_sched_ctrl_info->total_sdu_size;
	}
	else
	{
		flag_retransmission=1;
		data_size=UE_info->DLSCH_pdu_size;
	}

Nick Ho's avatar
Nick Ho committed
111
	LOG_I(MAC,"[%04d][DLSchedulerUSS] Max TBS %d MCS index %d TBS index %d\n", mac_inst->current_subframe, TBS, I_mcs, I_tbs);
112 113
	/*set UE data information*/
	/*New transmission*/
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
114
#if 1
115 116
	if(UE_info->HARQ_round==0)
	{
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
117
		//Get RLC status	
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
118
		rlc_status = mac_rlc_status_ind(
119 120 121 122 123 124 125 126
										module_id,
										UE_info->rnti,
										module_id,
										frame_start,
										subframe_start,
										1,
										0,
										DCCH0_NB_IoT,
127
										TBS-subheader_length);
128
		data_size = rlc_status.bytes_in_buffer;
Nick Ho's avatar
Nick Ho committed
129
		LOG_N(MAC,"[NB-IoT] RLC indicate to MAC that the data size is : %d\n",data_size);
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
130

Nick Ho's avatar
Nick Ho committed
131 132 133
		mac_sdu_size = mac_rlc_data_req(
					      module_id,
					      UE_info->rnti,
134
					      module_id,
Nick Ho's avatar
Nick Ho committed
135 136 137 138 139 140 141 142 143
					      frame_start,
					      1,
					      0,
					      DCCH0_NB_IoT,
						  TBS, //not used
					      (char *)&sdu_temp[0]);


            LOG_I(MAC,"[NB-IoT][DCCH]  Got %d bytes from RLC\n",mac_sdu_size);
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
144

Nick Ho's avatar
Nick Ho committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		//Get RLC status	
		rlc_status2 = mac_rlc_status_ind(
										module_id,
										UE_info->rnti,
										module_id,
										frame_start,
										subframe_start,
										1,
										0,
										DCCH0_NB_IoT,
										TBS-subheader_length-mac_sdu_size);
         mac_sdu_size2 = mac_rlc_data_req(
					      module_id,
					      UE_info->rnti,
					      module_id,
					      frame_start,
					      1,
					      0,
					      DCCH0_NB_IoT,
						  TBS, //not used
					      (char *)&sdu_temp2[0]);

          	printf("print the second RLC DATA PDU payload, we have  %d byte \n",mac_sdu_size2);
            int y;
            for (y=0;y<mac_sdu_size2;y++){
            printf("%02x ",sdu_temp2[y]);
            }
            printf("\n");

174
		    //Generate header
Nick Ho's avatar
Nick Ho committed
175
		    payload_offset = generate_dlsch_header_NB_IoT(UE_info->DLSCH_pdu.payload, 1, &logical_channel, &mac_sdu_size2, 0, 0, TBS);
176
		    //Complete MAC PDU
Nick Ho's avatar
Nick Ho committed
177 178
		    memcpy(UE_info->DLSCH_pdu.payload+payload_offset, sdu_temp, mac_sdu_size);		    
		    memcpy(UE_info->DLSCH_pdu.payload+payload_offset+mac_sdu_size, sdu_temp2, mac_sdu_size2);
179 180


181
            printf("print the MAC DATA PDU including length payload, we have header %d byte \n",payload_offset);
Nick Ho's avatar
Nick Ho committed
182 183 184
            //int y;
            for (y=0;y<mac_sdu_size+payload_offset+mac_sdu_size2;y++){
            //for (y=0;y<payload_offset+mac_sdu_size2;y++){
185 186 187
            printf("%02x ",UE_info->DLSCH_pdu.payload[y]);
            }
            printf("\n");
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
188

Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
189
  	}
190
	/*Retransmission*/
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
191
	else 
192 193 194 195 196
	{
		data_size = UE_info->DLSCH_pdu_size;
		flag_retransmission = 1;
		if((UE_info->HARQ_round>0)&&(TBS<data_size))
		{
Nick Ho's avatar
Nick Ho committed
197
			LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] TBS is not enough for retransmission\n", mac_inst->current_subframe);
Nick Ho's avatar
Nick Ho committed
198
			return -1;
199 200
		}
	}
Nick Ho's avatar
Nick Ho committed
201 202
#endif

Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
203
	//data_size=200;	//for testing
Nick Ho's avatar
Nick Ho committed
204

Nick Ho's avatar
Nick Ho committed
205
	//LOG_I(MAC,"[%04d][DLSchedulerUSS] UE data size %d\n", mac_inst->current_subframe, data_size);
206 207 208
	//Have DCCH data
	if(data_size == 0)
	{
Nick Ho's avatar
Nick Ho committed
209
		LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] No data in DCCH0_NB_IoT\n", mac_inst->current_subframe);
Nick Ho's avatar
Nick Ho committed
210
		return -1;
211 212 213 214 215 216 217 218
	}
	if(data_size>127)
	{
		subheader_length=3;
	}
	if(TBS > data_size+subheader_length)
	{
		TBS = get_tbs(data_size, I_tbs, &I_sf);
219
		LOG_I(MAC,"[%04d][DLSchedulerUSS] TBS change to %d because data size is smaller than previous TBS\n", mac_inst->current_subframe, TBS);
220 221
	}

Nick Ho's avatar
Nick Ho committed
222 223 224
  	search_space_end_sf=cal_num_dlsf(mac_inst, hyperSF_start, frame_start, subframe_start, &h_temp, &f_temp, &sf_temp, UE_info->R_max);
  	LOG_D(MAC,"[%04d][DLSchedulerUSS] Search_space_start_sf %d Search_space_end_sf %d\n", convert_system_number_sf(hyperSF_start, frame_start, subframe_start), mac_inst->current_subframe, search_space_end_sf);
  	//LOG_D(MAC,"[%04d][DLSchedulerUSS][%d] Search_space_start_sf %d Search_space_end_sf %d\n", mac_inst->current_subframe, UE_info->rnti, mac_inst->current_subframe, convert_system_number_sf(hyperSF_start, frame_start, subframe_start), search_space_end_sf);
225
	/*Loop all NPDCCH candidate position*/
226
	for(cdd_num=0;cdd_num<UE_info->R_max/UE_sched_ctrl_info->R_dci;++cdd_num)
227
	{
Nick Ho's avatar
Nick Ho committed
228
		LOG_I(MAC,"[%04d][DLSchedulerUSS] Candidate num %d DCI Rep %d DCI Rmax: %d rep : %d\n",mac_inst->current_subframe, cdd_num, UE_sched_ctrl_info->R_dci,UE_info->R_max,UE_sched_ctrl_info->R_dl_data);
229 230 231 232 233 234 235
		/*Check NPDCCH Resource*/
		end_flagCCH = check_resource_NPDCCH_NB_IoT(mac_inst, hyperSF_start, frame_start, subframe_start, NPDCCH_info, cdd_num, UE_info->R_dci);

		//This candidate position is available
		/*Check NPDSCH Resource*/
		if(end_flagCCH!=-1)
		{
236
		  //LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate num %d allocate success\n",mac_inst->current_subframe, cdd_num);
Nick Ho's avatar
Nick Ho committed
237
			//LOG_D(MAC,"[%04d][DLSchedulerUSS] Allocate NPDCCH subframe %d to subframe %d cdd index %d\n", mac_inst->current_subframe, NPDCCH_info->sf_start, NPDCCH_info->sf_end, cdd_num);
Nick Ho's avatar
Nick Ho committed
238
			/*
239 240 241 242
			//Max DL TBS
			if(TBS > data_size+subheader_length)
			{
				TBS = get_tbs(data_size, I_tbs, &I_sf);
243
				LOG_D(MAC,"[%04d][DLSchedulerUSS] [%d] data_size %d TBS change to %d \n", mac_inst->current_subframe,UE_info->rnti, data_size, TBS);
244
			}
Nick Ho's avatar
Nick Ho committed
245
			*/
246

247 248
			//Get number of subframe this UE need per repetition
			n_sf = get_num_sf(I_sf);
249 250
			//LOG_D(MAC,"[%04d][DLSchedulerUSS] Number SF %d index SF %d\n",mac_inst->current_subframe, n_sf, I_sf);
			//LOG_D(MAC,"[%04d][DLSchedulerUSS] Require total %d DL SF Rep %d\n", n_sf*UE_sched_ctrl_info->R_dl, UE_sched_ctrl_info->R_dl);
251 252 253 254 255 256
			//Check have enough NPDSCH resource or not
			//loop 8 scheduling delay index
			for(I_delay=0;I_delay<8;++I_delay)
			{
		        if(search_space_end_sf<NPDCCH_info->sf_end+get_scheduling_delay(I_delay, UE_info->R_max)+5)
		        {
257
		          end_flagSCH = check_resource_NPDSCH_NB_IoT(mac_inst, NPDSCH_info, NPDCCH_info->sf_end, I_delay, UE_info->R_max, UE_sched_ctrl_info->R_dl_data, n_sf);
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
258
			  int x;
259
			  for (x=0;x<data_size;x++){
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
260 261 262 263
				printf("%02x ",UE_info->DLSCH_pdu.payload[x]);
			  }
			  printf("\n");
			  //Have available resource
264 265 266
		          /*Check HARQ resource*/
		          if(end_flagSCH!=-1)
		          {
Nick Ho's avatar
Nick Ho committed
267 268
		            //LOG_D(MAC,"[%04d][DLSchedulerUSS] Scheduling delay index: %d value: %d + 4 allocate success\n", mac_inst->current_subframe, I_delay, get_scheduling_delay(I_delay, UE_info->R_max));
		            //LOG_D(MAC,"[%04d][DLSchedulerUSS] Allocate NPDSCH subframe %d to subframe %d\n", mac_inst->current_subframe, NPDSCH_info->sf_start, NPDSCH_info->sf_end);
269 270
		            for(HARQ_delay=0;HARQ_delay<4;++HARQ_delay)
		            {
Nick Ho's avatar
Nick Ho committed
271
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ delay %d\n", mac_inst->current_subframe,get_HARQ_delay(1, HARQ_delay) );
272
		              end_flagHARQ=Check_UL_resource(NPDSCH_info->sf_end+get_HARQ_delay(1, HARQ_delay), UE_sched_ctrl_info->R_dl_harq, HARQ_info, 0, 1);
273 274
		              if(end_flagHARQ!=-1)
		              {
Nick Ho's avatar
Nick Ho committed
275
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] Allocate HARQ feedback subframe %d to subframe %d\n", mac_inst->current_subframe, HARQ_info->sf_start, HARQ_info->sf_end);
276 277 278 279 280 281 282
		                HARQ_info->ACK_NACK_resource_field=get_resource_field_value(HARQ_info->subcarrier_indication, get_scheduling_delay(HARQ_delay, UE_info->R_max));
		                //toggle NDI
		                if(flag_retransmission==0)
		                {
		                  UE_info->oldNDI_DL=(UE_info->oldNDI_DL+1)%2;
		                  //New transmission need to request data from RLC and generate new MAC PDU
		                  UE_info->I_mcs_dl = I_mcs;
Nick Ho's avatar
Nick Ho committed
283

284
/*               
285
		                  //Request data from RLC layer
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
286
		                  rlc_status = mac_rlc_status_ind(
287 288 289 290 291 292 293 294 295
		                        module_id,
		                        UE_info->rnti,
		                        module_id,
		                        frame_start,
		                        subframe_start,
		                        1,
		                        0,
		                        DCCH0_NB_IoT,
		                        TBS-subheader_length);
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
296 297 298 299
				
				 data_size = rlc_status.bytes_in_buffer;
               			 LOG_N(MAC,"[NB-IoT] RLC indicate to MAC that the data size is : %d\n",data_size);

300
		                  mac_sdu_size = mac_rlc_data_req(module_id, UE_info->rnti, module_id, frame_start, 1, 0, DCCH0_NB_IoT, TBS, (char *)&sdu_temp[0]);
301
*/		                  
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
		                  //channel=DCCH0_NB_IoT;

		                  //UE_info->DLSCH_pdu.pdu_size=TBS;
		                  UE_sched_ctrl_info->NPDCCH_sf_end=NPDCCH_info->sf_end;
							UE_sched_ctrl_info->NPDCCH_sf_start=NPDCCH_info->sf_start;
							UE_sched_ctrl_info->NPDSCH_sf_end=NPDSCH_info->sf_end;
							UE_sched_ctrl_info->NPDSCH_sf_start=NPDSCH_info->sf_start;
							UE_sched_ctrl_info->HARQ_sf_end=HARQ_info->sf_end;
							UE_sched_ctrl_info->HARQ_sf_start=HARQ_info->sf_start;
							UE_sched_ctrl_info->TBS=TBS;
						  	UE_sched_ctrl_info->dci_n1_index_mcs=I_mcs;
						  	UE_sched_ctrl_info->index_tbs=I_tbs;
						  	UE_sched_ctrl_info->dci_n1_index_sf=I_sf;
							UE_sched_ctrl_info->dci_n1_n_sf=n_sf;
							UE_sched_ctrl_info->dci_n1_index_delay=I_delay;
							UE_sched_ctrl_info->dci_n1_index_ack_nack=HARQ_info->ACK_NACK_resource_field;
							UE_sched_ctrl_info->total_data_size_dl=data_size;
Nick Ho's avatar
Nick Ho committed
319
							UE_sched_ctrl_info->dci_n1_index_R_data = from_R_dl_to_idx(UE_sched_ctrl_info->R_dl_data);
320
		                }
Nick Ho's avatar
Nick Ho committed
321
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d][Success] Complete scheduling with data size %d\n", mac_inst->current_subframe, UE_info->rnti, data_size);
Nick Ho's avatar
Nick Ho committed
322
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] RNTI %d\n", mac_inst->current_subframe, UE_info->rnti);
Nick Ho's avatar
Nick Ho committed
323 324 325 326 327 328 329 330
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d][Success] Allocate NPDCCH subframe %d to subframe %d candidate index %d\n", mac_inst->current_subframe, UE_info->rnti, NPDCCH_info->sf_start, NPDCCH_info->sf_end, cdd_num);
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d][Success] Scheduling delay index: %d value: %d + 4\n", mac_inst->current_subframe, UE_info->rnti, I_delay, get_scheduling_delay(I_delay, UE_info->R_max));
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d][Success] Allocate NPDSCH subframe %d to subframe %d\n", mac_inst->current_subframe, UE_info->rnti, NPDSCH_info->sf_start, NPDSCH_info->sf_end);
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d][Success] Allocate HARQ feedback subframe %d to subframe %d\n", mac_inst->current_subframe, UE_info->rnti, HARQ_info->sf_start, HARQ_info->sf_end);
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d] Allocate NPDCCH subframe %d to subframe %d candidate index %d\n", mac_inst->current_subframe, UE_info->rnti, NPDCCH_info->sf_start, NPDCCH_info->sf_end, cdd_num);
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d] Scheduling delay index: %d value: %d + 4\n", mac_inst->current_subframe, UE_info->rnti, I_delay, get_scheduling_delay(I_delay, UE_info->R_max));
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d] Allocate NPDSCH subframe %d to subframe %d\n", mac_inst->current_subframe, UE_info->rnti, NPDSCH_info->sf_start, NPDSCH_info->sf_end);
		                LOG_I(MAC,"[%04d][DLSchedulerUSS][%d] Allocate HARQ feedback subframe %d to subframe %d\n", mac_inst->current_subframe, UE_info->rnti, HARQ_info->sf_start, HARQ_info->sf_end);
331
		                //Store PDU in UE template for retransmission
332
		                //fill_DCI_N1(DCI_N1, UE_info, I_delay, I_sf, HARQ_info->ACK_NACK_resource_field);
Nick Ho's avatar
Nick Ho committed
333
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ index %d\n", HARQ_info->ACK_NACK_resource_field);
334 335 336
		                
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS][%d] DCI N1 type:%d order:%d MCS:%d HARQ index:%d R:%d RscAssign:%d scheddly:%d DCI_R:%d\n", mac_inst->current_subframe, UE_info->rnti, DCI_N1->type, DCI_N1->orderIndicator, DCI_N1->mcs, DCI_N1->HARQackRes, DCI_N1->RepNum, DCI_N1->ResAssign, DCI_N1->Scheddly, DCI_N1->DCIRep);

337
		                //Generate Scheduling result for this UE
Nick Ho's avatar
Nick Ho committed
338
		                //generate_scheduling_result_DL(NPDCCH_info->sf_start, NPDSCH_info->sf_start, HARQ_info->sf_start, DCI_N1, UE_info->rnti, TBS, UE_info->DLSCH_pdu.payload);
339
		                //generate_scheduling_result_DL(NPDCCH_info, NPDSCH_info, HARQ_info, DCI_N1, UE_info->rnti, TBS, UE_info->DLSCH_pdu.payload);
Nick Ho's avatar
Nick Ho committed
340
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] finish generate scheduling result\n");
341 342
		                //matain DL avialable resource
		                maintain_resource_DL(mac_inst, NPDCCH_info, NPDSCH_info);
Nick Ho's avatar
Nick Ho committed
343
		                //available_resource_DL_t *temp = available_resource_DL;
344 345 346
		                /*
		                while(temp!=NULL)
		                {
Nick Ho's avatar
Nick Ho committed
347
		                  LOG_D(MAC,"[%04d][DLSchedulerUSS] Available resource node subframe start %d end %d\n", mac_inst->current_subframe, temp->start_subframe, temp->end_subframe);
348 349 350 351 352
		                  temp=temp->next;
		                }
		                */
		                //Do maintain UL resource
		                adjust_UL_resource_list(HARQ_info);
Nick Ho's avatar
Nick Ho committed
353 354
		                LOG_D(MAC,"[%04d][DLSchedulerUSS] Complete DL scheduling\n", mac_inst->current_subframe);
		                //Change the UE state to idle
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
355
		                //UE_info->direction = -1;
356

Nick Ho's avatar
Nick Ho committed
357
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] RNTI %d complete scheduling\n", mac_inst->current_subframe, UE_info->rnti);
358

Nick Ho's avatar
Nick Ho committed
359
		                return 0;
360 361 362 363 364
		              }
		            }
		            /*harq resource fail*/
		            if(end_flagHARQ==-1)
		            {
Nick Ho's avatar
Nick Ho committed
365
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]HARQ_delay %d HARQ Resource fail\n", mac_inst->current_subframe, HARQ_delay);
366 367
		            }
		          }
Nick Ho's avatar
Nick Ho committed
368
		          //LOG_D(MAC,"[%04d][DLSchedulerUSS] Scheduling delay index %d allocate fail\n", mac_inst->current_subframe, I_delay);
369 370 371 372 373
		        }
			}
			/*NPDSCH resource fail*/
			if(end_flagSCH==-1)
			{
Nick Ho's avatar
Nick Ho committed
374
				//LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]I_delay %d NPDSCH Resource fail\n", mac_inst->current_subframe, I_delay);
375 376
			}
		}
Nick Ho's avatar
Nick Ho committed
377
		//LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate %d no resource\n", mac_inst->current_subframe, cdd_num);
378 379

	}
380 381
	UE_sched_ctrl_info->flag_schedule_success=0;

382 383 384
	/*Resource allocate fail*/
	if((end_flagCCH==-1)||(end_flagSCH==-1)||(end_flagHARQ==-1))
	{
Nick Ho's avatar
Nick Ho committed
385 386
		LOG_D(MAC,"[%04d][DLSchedulerUSS][%d][Fail] Resource allocate fail\n", mac_inst->current_subframe, UE_info->rnti);
		LOG_D(MAC,"[%04d][DLSchedulerUSS][%d][Fail] Resource allocate fail\n", mac_inst->current_subframe, UE_info->rnti);
387
	}
388
	return -1;
389 390 391 392 393
}

int check_resource_NPDCCH_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, uint32_t hyperSF_start, uint32_t frame_start, uint32_t subframe_start, sched_temp_DL_NB_IoT_t *NPDCCH_info, uint32_t cdd_num, uint32_t dci_rep)
{
	NPDCCH_info->sf_start = cal_num_dlsf(mac_inst, hyperSF_start, frame_start, subframe_start, &(NPDCCH_info->start_h), &(NPDCCH_info->start_f), &(NPDCCH_info->start_sf), dci_rep*cdd_num+1);
Nick Ho's avatar
Nick Ho committed
394 395
	//LOG_D(MAC,"[%04d][check_resource_NPDCCH_NB_IoT]  NPDCCH sf start %d\n", mac_inst->current_subframe, NPDCCH_info->sf_start);
	//LOG_D(MAC,"[check_resource_NPDCCH_NB_IoT]abs start : %d\n", NPDCCH_info->sf_start);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	return check_resource_DL_NB_IoT(mac_inst, NPDCCH_info->start_h, NPDCCH_info->start_f, NPDCCH_info->start_sf, dci_rep, NPDCCH_info);
}

int check_resource_NPDSCH_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, sched_temp_DL_NB_IoT_t *NPDSCH_info, uint32_t sf_end, uint32_t I_delay, uint32_t R_max, uint32_t R_dl, uint32_t n_sf)
{
	int sf_temp = sf_end+get_scheduling_delay(I_delay, R_max)+5;
	while(is_dlsf(mac_inst, sf_temp)!=1)
  {
    ++sf_temp;
  }
	NPDSCH_info->sf_start = sf_temp;
	//transform sf into Hyper SF, Frame and subframe
	convert_system_number(NPDSCH_info->sf_start,&(NPDSCH_info->start_h), &(NPDSCH_info->start_f), &(NPDSCH_info->start_sf));
	//check this position available or not
	return check_resource_DL_NB_IoT(mac_inst, NPDSCH_info->start_h, NPDSCH_info->start_f, NPDSCH_info->start_sf, R_dl*n_sf, NPDSCH_info);
}

/*Check the available resource is enough or not from input starting position*/
/*return 0:success\1:fail*/
int check_resource_DL_NB_IoT(eNB_MAC_INST_NB_IoT *mac_inst, uint32_t hyperSF_start, uint32_t frame_start, uint32_t subframe_start, uint32_t dlsf_require, sched_temp_DL_NB_IoT_t *schedule_info)
{

	uint32_t node_start_sf, node_end_sf;
	uint32_t rsc_start_sf, rsc_end_sf;

	/*calculate the last subframe number for this transmission*/
	schedule_info->sf_end= cal_num_dlsf(mac_inst, hyperSF_start, frame_start, subframe_start, &(schedule_info->end_h), &(schedule_info->end_f), &(schedule_info->end_sf), dlsf_require);
Nick Ho's avatar
Nick Ho committed
423
	//LOG_D(MAC,"abs_end = %d\n", schedule_info->sf_end);
424 425 426 427 428 429 430 431 432 433
	rsc_start_sf = schedule_info->sf_start;
	if(schedule_info->sf_start<=schedule_info->sf_end)
	{
		rsc_end_sf = schedule_info->sf_end;
	}
	else
	{
		/*input position + Upper bound of subframe*/
		rsc_end_sf = schedule_info->sf_end+(1024*1024*10);
	}
Nick Ho's avatar
Nick Ho committed
434
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 1\n");
435 436
	/*initialize*/
	schedule_info->node = available_resource_DL;
Nick Ho's avatar
Nick Ho committed
437
	//LOG_D(MAC,"rsc need start subframe %d end subframe %d\n", rsc_start_sf, rsc_end_sf);
438 439 440 441
	/*Check available resource nodes to find the appropriate resource position*/
	while(schedule_info->node!=NULL)
	{
		//schedule_info->node->start_subframe <= schedule_info->sf_end
Nick Ho's avatar
Nick Ho committed
442
		//LOG_D(MAC,"check_resource_DL_NB_IoT flag 2\n");
443 444 445 446 447 448 449 450 451 452
		node_start_sf = schedule_info->node->start_subframe;
		if(schedule_info->node->start_subframe<=schedule_info->node->end_subframe)
		{
			node_end_sf = schedule_info->node->end_subframe;
		}
		else
		{
			/*input position + Upper bound of subframe*/
			node_end_sf = schedule_info->node->end_subframe+(1024*1024*10);
		}
Nick Ho's avatar
Nick Ho committed
453
		//LOG_D(MAC,"node start %d node end %d\n", node_start_sf, node_end_sf);
454 455 456 457 458 459
        if((node_start_sf<=rsc_start_sf)&&(node_end_sf>=rsc_end_sf))
        {
            return 0;
        }
        schedule_info->node = schedule_info->node->next;
	}
Nick Ho's avatar
Nick Ho committed
460
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 3\n");
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	return -1;
}

uint32_t generate_dlsch_header_NB_IoT(uint8_t *pdu, uint32_t num_sdu, logical_chan_id_t *logical_channel, uint32_t *sdu_length, uint8_t flag_drx, uint8_t flag_ta, uint32_t TBS)
{
	int i;
	uint32_t total_sdu_size=0;
	//number of control element
	uint32_t num_ce=0;
	uint32_t num_subheader=0;
	uint32_t num_sdu_L_15;
	int32_t padding_size;
	uint8_t flag_end_padding=0;
	SCH_SUBHEADER_FIXED_NB_IoT *mac_header=(SCH_SUBHEADER_FIXED_NB_IoT*)pdu;
	uint32_t offset=0;

	for(i=0;i<num_sdu;++i)
	{
Nick Ho's avatar
Nick Ho committed
479
		LOG_D(MAC,"index %d sdu size %d\n", i, sdu_length[i]);
480 481 482 483 484 485 486 487 488 489 490 491 492 493
		if(sdu_length[i]>127)
		{
			num_sdu_L_15++;
		}
		total_sdu_size+=sdu_length[i];
	}
	if(flag_drx==1)
		num_ce++;
	if(flag_ta==1)
		num_ce++;
	num_subheader=num_ce+num_sdu;
	padding_size = TBS-total_sdu_size-num_ce;
	if(padding_size<0)
	{
Nick Ho's avatar
Nick Ho committed
494
		LOG_D(MAC,"[ERROR]TBS less than require subheader and control element\n");
495 496
		return -1;
	}
Nick Ho's avatar
Nick Ho committed
497 498
	LOG_D(MAC,"total SDU size %d\n", total_sdu_size);
	LOG_D(MAC,"padding size %d\n", padding_size);
499 500 501 502 503 504 505 506
	if(padding_size>2)
	{
		flag_end_padding=1;
	}
	if((padding_size<=2)&&(padding_size>0))
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
Nick Ho's avatar
Nick Ho committed
507
		//mac_header->F2=0;
508 509 510 511 512 513 514 515
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(padding_size==2)
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
Nick Ho's avatar
Nick Ho committed
516
		//mac_header->F2=0;
517 518 519 520 521 522 523 524
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(flag_drx==1)
	{
		mac_header->LCID=DRX_COMMAND;
		mac_header->E=1;
Nick Ho's avatar
Nick Ho committed
525
		//mac_header->F2=0;
526 527 528 529 530 531 532 533 534 535 536
		mac_header->R=0;
		mac_header++;
		num_subheader--;
		offset++;
	}
	for(i=0;i<num_sdu;++i)
	{
		if((num_subheader==1)&&(flag_end_padding!=1))
        {
            mac_header->E=0;
            mac_header->LCID = logical_channel[i];
Nick Ho's avatar
Nick Ho committed
537
            //mac_header->F2=0;
538 539
            mac_header->R=0;
            offset++;
Nick Ho's avatar
Nick Ho committed
540
            LOG_D(MAC,"last sdu\n");
541 542
        }
        else
543
        {		
544 545
            if(sdu_length[i]<128)
            {
546 547 548 549 550 551 552 553 554 555
            	LOG_N(MAC,"Pack the header here\n");
           		/*mac_header->E=0;
            	mac_header->LCID = DCCH0_NB_IoT;
            	//mac_header->LCID = 1;

            	//mac_header->F2=0;
            	mac_header->R=0;
            	offset++;
            	mac_header+=1;*/

Nick Ho's avatar
Nick Ho committed
556 557 558 559 560 561 562 563 564 565 566
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->LCID = DCCH0_NB_IoT;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->F2=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->R=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->E=1;
                //((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->E=1;
               
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->F=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->L=2;
                //num_subheader--;
                mac_header+=2;
                offset+=2;
567
            	
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
568
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->LCID = DCCH0_NB_IoT;
569 570
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->F2=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->R=0;
571
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->E=1;
Nick Ho's avatar
Nick Ho committed
572 573
                //((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->E=1;
               
574 575 576 577 578
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->F=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->L=(uint8_t)sdu_length[i];
                num_subheader--;
                mac_header+=2;
                offset+=2;
579 580
                

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
            }
            else
            {
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->LCID = logical_channel[i];
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->F2=0;
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->R=0;
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->F=1;
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->E=1;
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->L_MSB=(uint8_t)(sdu_length[i]/256);
                ((SCH_SUBHEADER_LONG_NB_IoT*)mac_header)->L_LSB=(uint8_t)(sdu_length[i]%256);
                mac_header+=3;
                num_subheader--;
                offset+=3;
            }
        }
	}
597
	
598 599 600 601
	if(flag_end_padding==1)
	{
		mac_header->LCID=PADDING;
		mac_header->E=0;
Nick Ho's avatar
Nick Ho committed
602
		//mac_header->F2=0;
603 604 605 606
		mac_header->R=0;
		mac_header++;
		offset++;
	}
607
	
608 609
	return offset;
}
610
void fill_DCI_N1(DCIFormatN1_t *DCI_N1, UE_TEMPLATE_NB_IoT *UE_info, UE_SCHED_CTRL_NB_IoT_t *UE_sched_ctrl_info)
611 612 613
{
	DCI_N1->type=1;
	DCI_N1->orderIndicator = 0;
614 615 616
	DCI_N1->Scheddly = UE_sched_ctrl_info->dci_n1_index_delay;
	DCI_N1->ResAssign =UE_sched_ctrl_info->dci_n1_index_sf;
	DCI_N1->mcs = UE_sched_ctrl_info->dci_n1_index_mcs;
Nick Ho's avatar
Nick Ho committed
617
	DCI_N1->RepNum = UE_sched_ctrl_info->dci_n1_index_R_data;
618
	DCI_N1->HARQackRes = UE_sched_ctrl_info->dci_n1_index_ack_nack;
619
	//DCI_N1->DCIRep = 3-UE_info->R_max/UE_info->R_dci/2;
620 621
	DCI_N1->DCIRep=get_DCI_REP(UE_sched_ctrl_info->R_dci, UE_info->R_max);
	LOG_D(MAC,"[fill_DCI_N1] Type %d order %d I_delay %d I_SF %d I_mcs %d I_rep %d I_harq %d I_dci %d\n", DCI_N1->type, DCI_N1->orderIndicator, DCI_N1->Scheddly, DCI_N1->ResAssign, DCI_N1->mcs, DCI_N1->RepNum, DCI_N1->HARQackRes, DCI_N1->DCIRep);
622
}
623
	
624

625
void generate_scheduling_result_DL(uint32_t NPDCCH_sf_end, uint32_t NPDCCH_sf_start, uint32_t NPDSCH_sf_end, uint32_t NPDSCH_sf_start, uint32_t HARQ_sf_end, uint32_t HARQ_sf_start, DCIFormatN1_t *DCI_pdu, rnti_t rnti, uint32_t TBS, uint8_t *DLSCH_pdu)
626 627 628 629 630 631 632 633 634
{
	// create the schedule result node for this time transmission
	schedule_result_t *NPDCCH_result = (schedule_result_t*)malloc(sizeof(schedule_result_t));
	schedule_result_t *NPDSCH_result = (schedule_result_t*)malloc(sizeof(schedule_result_t));
	schedule_result_t *HARQ_result = (schedule_result_t*)malloc(sizeof(schedule_result_t));

	schedule_result_t *tmp, *tmp1;
	/*fill NPDCCH result*/
	NPDCCH_result->rnti=rnti;
635 636
	NPDCCH_result->output_subframe = NPDCCH_sf_start;
	NPDCCH_result->end_subframe = NPDCCH_sf_end;
637
	NPDCCH_result->sdu_length = 0;
638 639 640
	NPDCCH_result->direction = 1;
	NPDCCH_result->rnti_type = 3;
	NPDCCH_result->DLSCH_pdu = NULL;
641
	NPDCCH_result->DCI_pdu = (void*)DCI_pdu;
642 643
	NPDCCH_result->DCI_release = 0;
	NPDCCH_result->channel = NPDCCH;
644 645
	//NPDCCH_result->debug_str = (uint8_t*)malloc(6*sizeof(uint8_t));
	//NPDCCH_result->debug_str = dl_str1;
646 647 648
	NPDCCH_result->next = NULL;
	/*fill NPDSCH result*/
	NPDSCH_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
649
	//NPDSCH_result->output_subframe = NPDSCH_subframe;
650 651
	NPDSCH_result->output_subframe = NPDSCH_sf_start;
	NPDSCH_result->end_subframe = NPDSCH_sf_end;
652
	NPDSCH_result->sdu_length = TBS*8;
CHIN-WEI, KANG's avatar
CHIN-WEI, KANG committed
653 654
	NPDSCH_result->DLSCH_pdu = DLSCH_pdu;
	//NPDSCH_result->DLSCH_pdu = NULL;
655 656
	NPDSCH_result->direction = 1;
	NPDSCH_result->rnti_type = 3;
657
	NPDSCH_result->DCI_pdu = (void*)DCI_pdu;
658 659
	NPDSCH_result->DCI_release = 0;
	NPDSCH_result->channel = NPDSCH;
660 661
	//NPDSCH_result->debug_str = (uint8_t*)malloc(7*sizeof(uint8_t));
	//NPDSCH_result->debug_str = dl_str2;
662 663 664
	NPDSCH_result->next = NULL;
	/*fill HARQ result*/
	HARQ_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
665
	//HARQ_result->output_subframe = HARQ_subframe;
666 667
	HARQ_result->output_subframe = HARQ_sf_start;
	HARQ_result->end_subframe = HARQ_sf_end;
668
	HARQ_result->sdu_length = 0;
Nick Ho's avatar
Nick Ho committed
669
	HARQ_result->direction = 0;
670 671
	HARQ_result->rnti_type = 3;
	HARQ_result->DLSCH_pdu = NULL;
672
	HARQ_result->DCI_pdu = (void*)DCI_pdu;
673 674 675
	HARQ_result->DCI_release = 1;
	HARQ_result->channel = NPUSCH;
	HARQ_result->npusch_format = 1;
676 677
	//HARQ_result->debug_str = (uint8_t*)malloc(7*sizeof(uint8_t));
	//HARQ_result->debug_str = dl_str3;
678
	HARQ_result->next = NULL;
679
	//DEBUG("[generate_scheduling_result_DL] Generate NPDCCH node\n");
680 681 682 683 684
	/*NPDCCH scheduling result*/
	// be the first node of the DL scheduling result
	
	tmp = NULL;
	tmp1 = NULL;
685

686 687 688 689
	if(schedule_result_list_DL == NULL)
	{
		//schedule_result_list_DL = (schedule_result_t*)malloc(sizeof(schedule_result_t));
		schedule_result_list_DL = NPDCCH_result;
690
		//DEBUG("[generate_scheduling_result_DL] Generate NPDCCH node at head\n");
691 692 693 694 695 696
	}
	else
	{
		tmp = schedule_result_list_DL;
		while(tmp!=NULL)
		{
697
			if(NPDCCH_sf_start < tmp->output_subframe)
698 699 700 701 702
			{
				break;
			}
			tmp1 = tmp;
			tmp = tmp->next;
703
			//DEBUG("[generate_scheduling_result_DL] node output subframe %d at NPDCCH part\n", tmp->output_subframe);
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		}
		/*tail*/
		if(tmp==NULL)
		{
			tmp1->next = NPDCCH_result;
		}
		else
		{
			NPDCCH_result->next = tmp;
			if(tmp1)
			{
				tmp1->next = NPDCCH_result;
			}
			else
			{
				schedule_result_list_DL = NPDCCH_result;
			}
		}
	}
723 724
	
	//DEBUG("[generate_scheduling_result_DL] Generate NPDCSH node\n");
725 726 727 728 729
	/*NPDSCH scheduling result*/
	tmp1 = NULL;
	tmp = schedule_result_list_DL;
	while(tmp!=NULL)
	{
730
		if(NPDSCH_sf_start < tmp->output_subframe)
731 732 733
		{
			break;
		}
734
		//DEBUG("[generate_scheduling_result_DL] node output subframe %d at NPDSCH part\n", tmp->output_subframe);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		tmp1 = tmp;
		tmp = tmp->next;
	}
	if(tmp==NULL)
	{
		tmp1->next = NPDSCH_result;
	}
	else
	{
		NPDSCH_result->next = tmp;
		if(tmp1)
		{
			tmp1->next = NPDSCH_result;
		}
		else
		{
			schedule_result_list_DL = NPDSCH_result;
		}
	}
754
	//DEBUG("[generate_scheduling_result_DL] Generate HARQ node\n");
755 756 757
	/*HARQ scheduling result*/
	// be the first node of UL
	// be the first node of UL
758
#if 1
759
	tmp1 = NULL;
760
	tmp = NULL;
761 762
	if(schedule_result_list_UL == NULL)
	{
763
		//DEBUG("[generate_scheduling_result_DL] LIST NULL, Generate HARQ at HEAD\n");
764 765 766 767 768 769 770 771
	  //schedule_result_list_UL = (schedule_result_t*)malloc(sizeof(schedule_result_t));
	  schedule_result_list_UL = HARQ_result;
	}
	else
	{
		tmp = schedule_result_list_UL;
		while(tmp!=NULL)
		{
772
			if(HARQ_sf_start < tmp->output_subframe)
773 774 775
			{
				break;
			}
776
			//DEBUG("[generate_scheduling_result_DL] node output subframe %d at HARQ part\n", tmp->output_subframe);
777 778 779 780 781
			tmp1 = tmp;
			tmp = tmp->next;
		}
		if(tmp==NULL)
		{
782
			//DEBUG("[generate_scheduling_result_DL] Generate HARQ at Tail\n");
783 784 785 786 787 788 789
			tmp1->next = HARQ_result;
		}
		else
		{
			HARQ_result->next = tmp;
			if(tmp1)
			{
790
				//DEBUG("[generate_scheduling_result_DL] Generate HARQ in normal\n");
791 792 793
				tmp1->next = HARQ_result;
			}else
			{
794
				//DEBUG("[generate_scheduling_result_DL] Generate HARQ at Head\n");
795 796 797 798
				schedule_result_list_UL = HARQ_result;
			}
		}
	}
799
#endif
800
}
801

802 803 804 805 806 807
void maintain_resource_DL(eNB_MAC_INST_NB_IoT *mac_inst, sched_temp_DL_NB_IoT_t *NPDCCH_info, sched_temp_DL_NB_IoT_t *NPDSCH_info)
{
	available_resource_DL_t *temp;
	uint8_t flag_same=0;
	int align_left;
	int align_right;
808
	uint32_t H_temp, f_temp, sf_temp;
Nick Ho's avatar
Nick Ho committed
809
  	uint32_t H_temp_r, f_temp_r, sf_temp_r;
810 811 812 813 814 815 816 817 818 819 820 821

	if(NPDSCH_info==NULL)
	{
		/****Maintain NPDCCH node*******/
		//	divided into two node
		//	keep one node(align left or right)
		//	delete node
		convert_system_number(NPDCCH_info->node->start_subframe, &H_temp, &f_temp, &sf_temp);
		//align_left = (calculate_DLSF(mac_inst, NPDCCH_info->node->start_subframe, NPDCCH_info->sf_start) == 0);
		align_left=(cal_num_dlsf(mac_inst, H_temp, f_temp, sf_temp, &H_temp_r, &f_temp_r, &sf_temp_r, 1)==NPDCCH_info->sf_start);
		align_right = ((calculate_DLSF(mac_inst, NPDCCH_info->sf_end, NPDCCH_info->node->end_subframe) == 0)||(NPDCCH_info->sf_end==NPDCCH_info->node->end_subframe));
		//align_left = (calculate_DLSF(mac_inst, NPDCCH_info->node->start_subframe, NPDCCH_info->sf_start) == 0);
Nick Ho's avatar
Nick Ho committed
822
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
823 824 825
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
826 827 828 829
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
830
				temp = (available_resource_DL_t *)malloc(sizeof(available_resource_DL_t));
Nick Ho's avatar
Nick Ho committed
831 832 833 834 835 836 837 838
				
				if((available_resource_DL_t *)0 == NPDCCH_info->node->next){
					available_resource_DL_last = temp;
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
                
                
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
				temp->next = NPDCCH_info->node->next;
				temp->prev = NPDCCH_info->node;
				NPDCCH_info->node->next = temp;

				temp->start_subframe = NPDCCH_info->sf_end+1;
				temp->end_subframe = NPDCCH_info->node->end_subframe;

				NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start - 1;
				break;
			case 1:
				//	keep one node
				if(align_left)
				{
					NPDCCH_info->node->start_subframe = NPDCCH_info->sf_end+1;
				}
				else
				{
					NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start-1 ;
				}
Nick Ho's avatar
Nick Ho committed
858
				
859 860 861
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
				
				//  process next node element.
				if(NPDCCH_info->node->prev==NULL){
				    //  first node of list
                    available_resource_DL = NPDCCH_info->node->next;
                }else{
                    NPDCCH_info->node->prev->next = NPDCCH_info->node->next;
                }
                
                //  process prev node element.
                if(NPDCCH_info->node->next!=NULL){
                    NPDCCH_info->node->next->prev = NPDCCH_info->node->prev;
                }else{
                    //  end node of list
                    available_resource_DL_last = NPDCCH_info->node->prev;
                }
                
879 880 881 882 883 884 885 886 887 888 889 890 891
				free(NPDCCH_info->node);
				break;
			default:
				//error
				break;
      free(NPDCCH_info);
		}
	}
	else
	{
    if(NPDCCH_info->node==NPDSCH_info->node)
		{
			flag_same=1;
Nick Ho's avatar
Nick Ho committed
892
			LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH and NPDSCH using the same node\n", mac_inst->current_subframe);
893 894 895 896 897 898 899 900 901 902
		}
		/****Maintain NPDCCH node*******/
		//	divided into two node
		//	keep one node(align left or right)
		//	delete node

		convert_system_number(NPDCCH_info->node->start_subframe, &H_temp, &f_temp, &sf_temp);
		//align_left = (calculate_DLSF(mac_inst, NPDCCH_info->node->start_subframe, NPDCCH_info->sf_start) == 0);
		align_left=(cal_num_dlsf(mac_inst, H_temp, f_temp, sf_temp, &H_temp_r, &f_temp_r, &sf_temp_r, 1)==NPDCCH_info->sf_start);
		align_right = ((calculate_DLSF(mac_inst, NPDCCH_info->sf_end, NPDCCH_info->node->end_subframe) == 0)||(NPDCCH_info->sf_end==NPDCCH_info->node->end_subframe));
Nick Ho's avatar
Nick Ho committed
903
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
904 905 906
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
907 908 909 910
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
911 912 913 914
				temp = (available_resource_DL_t *)malloc(sizeof(available_resource_DL_t));
				
				if((available_resource_DL_t *)0 == NPDCCH_info->node->next){
					available_resource_DL_last = temp;
Nick Ho's avatar
Nick Ho committed
915 916 917
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
				
				temp->next = NPDCCH_info->node->next;
				temp->prev = NPDCCH_info->node;
				NPDCCH_info->node->next = temp;

				temp->start_subframe = NPDCCH_info->sf_end+1;
				temp->end_subframe = NPDCCH_info->node->end_subframe;

				NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start - 1;
				if(flag_same==1)
				{
					NPDSCH_info->node = temp;
				}
				break;
			case 1:
				//	keep one node
				if(align_left)
				{
					NPDCCH_info->node->start_subframe = NPDCCH_info->sf_end+1;
Nick Ho's avatar
Nick Ho committed
937
					LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH keep one node\n", mac_inst->current_subframe);
938 939 940 941 942 943 944 945
				}
				else
				{
					NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start-1 ;
				}
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
946
				LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH delete node\n", mac_inst->current_subframe);
947 948
				
				//	calvin add
Nick Ho's avatar
Nick Ho committed
949
				//	delete
950
				
Nick Ho's avatar
Nick Ho committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
				//  process next node element.
				if(NPDCCH_info->node->prev==NULL){
				    //  first node of list
                    available_resource_DL = NPDCCH_info->node->next;
                }else{
                    NPDCCH_info->node->prev->next = NPDCCH_info->node->next;
                }
                
                //  process prev node element.
                if(NPDCCH_info->node->next!=NULL){
                    NPDCCH_info->node->next->prev = NPDCCH_info->node->prev;
                }else{
                    //  end node of list
                    available_resource_DL_last = NPDCCH_info->node->prev;
                }
                
967 968 969 970 971 972 973 974 975 976 977 978
				free(NPDCCH_info->node);
				break;
			default:
				//error
				break;
		}
		/****Maintain NPDSCH node*******/
		align_left = (calculate_DLSF(mac_inst, NPDSCH_info->node->start_subframe, NPDSCH_info->sf_start) == 0);
		align_right = ((calculate_DLSF(mac_inst, NPDSCH_info->sf_end, NPDSCH_info->node->end_subframe) == 0)||(NPDSCH_info->sf_end==NPDSCH_info->node->end_subframe));
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
979 980 981 982
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
983 984 985 986
				temp = (available_resource_DL_t *)malloc(sizeof(available_resource_DL_t));
				
				if((available_resource_DL_t *)0 == NPDSCH_info->node->next){
					available_resource_DL_last = temp;
Nick Ho's avatar
Nick Ho committed
987 988 989
				}else{
				    NPDSCH_info->node->next->prev = temp;
                }
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
				
				temp->next = NPDSCH_info->node->next;
				temp->prev = NPDSCH_info->node;
				NPDSCH_info->node->next = temp;

				temp->start_subframe = NPDSCH_info->sf_end+1;
				temp->end_subframe = NPDSCH_info->node->end_subframe;

				NPDSCH_info->node->end_subframe = NPDSCH_info->sf_start - 1;

				break;
			case 1:
				//	keep one node
				if(align_left)
				{
					NPDSCH_info->node->start_subframe = NPDSCH_info->sf_end+1;
				}
				else
				{
					NPDSCH_info->node->end_subframe = NPDSCH_info->sf_start-1 ;
				}
				break;
			case 2:
				//	delete
				
Nick Ho's avatar
Nick Ho committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
				if(NPDSCH_info->node->prev==NULL){
				    //  first node of list
                    available_resource_DL = NPDSCH_info->node->next;
                }else{
                    NPDSCH_info->node->prev->next = NPDSCH_info->node->next;
                }
                
                //  process prev node element.
                if(NPDSCH_info->node->next!=NULL){
                    NPDSCH_info->node->next->prev = NPDSCH_info->node->prev;
                }else{
                    //  end node of list
                    available_resource_DL_last = NPDSCH_info->node->prev;
                }

1030 1031 1032 1033 1034 1035 1036 1037 1038
				free(NPDSCH_info->node);
				break;
			default:
				//error
				break;
		}
		free(NPDCCH_info);
		free(NPDSCH_info);
	}
Nick Ho's avatar
Nick Ho committed
1039 1040

}
Matthieu Kanj's avatar
Matthieu Kanj committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

//// get I_TBS for any NPUSCH config

uint8_t I_TBS_index_single_tone[11]= {0,2,1,3,4,5,6,7,8,9,10};

uint8_t get_UL_I_TBS_from_MCS_NB_IoT(uint8_t I_mcs, uint8_t N_sc_RU, uint8_t Msg3_flag)
{

    if(Msg3_flag == 1)
    {
        return I_mcs;

    } else {

        if(N_sc_RU == 1)
        {
            return I_TBS_index_single_tone[I_mcs];
        } else {
            return I_mcs;
        }

    }

}
Matthieu Kanj's avatar
Matthieu Kanj committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
/////////////////
///////////////////////////////////////////////
//// function to test if configuration is single or multi-tone //// 1 for single and 0 for multi-tone
uint8_t test_signle_tone_UL_NB_IoT(uint8_t subcarrier_spacing, uint8_t I_sc, uint8_t npush_format) 
{
			
	if(npush_format == 0)  // format 1
	{
		if(subcarrier_spacing == 0)  // 15 KHz
		{

			if(I_sc >= 0 && I_sc < 12)
			{
				return 1;
			} else if (I_sc >= 12 && I_sc < 16) {
				return 0;
			} else if (I_sc >= 16 && I_sc < 18) {
				return 0;
			} else if (I_sc == 18) {
				return 0;
			} else {
				return 1;
			}
		} else {
			return 1;
		}

	} else {          /// format 2
		return 1;
	}

}
//////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1098

Nick Ho's avatar
Nick Ho committed
1099 1100 1101 1102 1103 1104 1105 1106 1107
/*Get MCS index*/
uint32_t get_I_mcs(int CE_level)
{
	if(CE_level==0)
	{
		return 13;
	}
	else if(CE_level==1)
	{
Nick Ho's avatar
Nick Ho committed
1108
		return 10;
Nick Ho's avatar
Nick Ho committed
1109 1110 1111
	}
	else
	{
Nick Ho's avatar
Nick Ho committed
1112
		return 4;
Nick Ho's avatar
Nick Ho committed
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	}
}

uint32_t get_max_tbs(uint32_t I_tbs)
{
	return MAC_TBStable_NB_IoT[I_tbs][7]/8;
}

uint32_t get_tbs(uint32_t data_size, uint32_t I_tbs, uint32_t *I_sf)
{
	for((*I_sf)=0;(*I_sf)<8;++(*I_sf))
	{
1125
		
Nick Ho's avatar
Nick Ho committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
		//LOG_D(MAC,"[get_tbs]TBS %d SF index %d\n", TBStable_NB_IoT[I_tbs][(*I_sf)], *I_sf);
		if(MAC_TBStable_NB_IoT[I_tbs][(*I_sf)]>=data_size*8)
		{
			return MAC_TBStable_NB_IoT[I_tbs][(*I_sf)]/8;
		}
	}
	LOG_D(MAC,"error\n");
	return 0;
}

uint32_t get_num_sf(uint32_t I_sf)
{
	if(I_sf==6)
	{
		return 8;
	}
	else if(I_sf==7)
	{
		return 10;
	}
	else
	{
		return I_sf+1;
	}
}

/*Subcarrier_spacing 0:3.75kHz \ 1 : 15kHz*/
uint32_t get_HARQ_delay(int subcarrier_spacing, uint32_t HARQ_delay_index)
{
	if(subcarrier_spacing==1)
	{
		if(HARQ_delay_index==0)
			return 13;
		else if(HARQ_delay_index==1)
			return 15;
		else if(HARQ_delay_index==2)
			return 17;
		else
			return 18;
	}
	else
	{
		if((HARQ_delay_index==0)&&(HARQ_delay_index==1))
			return 13;
		else
			return 21;
	}
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
}

uint8_t get_index_Rep_dl(uint16_t R)
{
  int i;
  if(R<=128)
  {
    for(i=0;i<16;++i)
    {
      if(R==R_dl_table[i])
      {
        return i;
      }
    }
Nick Ho's avatar
Nick Ho committed
1187
    LOG_D(MAC,"[get_index_Rep] error\n");
1188
  }
Nick Ho's avatar
Nick Ho committed
1189 1190
  return 0;
}