eNB_scheduler_dlsch_NB_IoT.c 37.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"   

34

Nick Ho's avatar
Nick Ho committed
35
/*DL scheduler*/
36
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)
37
{
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
38
        //number of candidate
39 40 41 42 43 44 45
	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));
46
	
47
	//DCI N1
48 49
	//DCIFormatN1_t *DCI_N1 = (DCIFormatN1_t*)malloc(sizeof(DCIFormatN1_t));
	
50
	//RLC Status
51 52 53
	
	//mac_rlc_status_resp_NB_IoT_t rlc_status;
	
54 55 56 57 58 59 60 61 62 63 64 65 66
	/*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;
67
	//uint32_t mac_sdu_size;
68

69
	//uint8_t sdu_temp[SCH_PAYLOAD_SIZE_MAX_NB_IoT];
70
	
Nick Ho's avatar
Nick Ho committed
71
	//logical_chan_id_t logical_channel;
72 73

	uint32_t subheader_length=2;
74
	
75
	//uint32_t payload_offset;
76
	
77 78

	uint32_t search_space_end_sf, h_temp, f_temp, sf_temp;
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
79
        mac_rlc_status_resp_t rlc_status; //Declare rlc_status
80

Nick Ho's avatar
Nick Ho committed
81
	I_mcs = get_I_mcs(UE_info->CE_level);
82 83 84
	I_tbs = I_mcs;
	//get max TBS
	TBS = get_max_tbs(I_tbs);
Nick Ho's avatar
Nick Ho committed
85

86 87 88 89 90 91 92 93 94 95 96
	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
97
	LOG_D(MAC,"[%04d][DLSchedulerUSS] Max TBS %d MCS index %d TBS index %d\n", mac_inst->current_subframe, TBS, I_mcs, I_tbs);
98 99
	/*set UE data information*/
	/*New transmission*/
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
100
#if 1
101 102
	if(UE_info->HARQ_round==0)
	{
103
		//Get RLC status	
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
104
		rlc_status = mac_rlc_status_ind(
105 106 107 108 109 110 111 112 113 114
										module_id,
										UE_info->rnti,
										module_id,
										frame_start,
										subframe_start,
										1,
										0,
										DCCH0_NB_IoT,
										0);
		data_size = rlc_status.bytes_in_buffer;
115 116

		// for testing
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
117
		/*data_size = 200;
Nick Ho's avatar
Nick Ho committed
118 119 120 121 122 123
		data_size=0;
		int ue_index;
		for(ue_index=0;ue_index<UE_NUM_SIM;++ue_index)
		{
			if(UE_info_sim[ue_index].tc_rnti==UE_info->rnti)
				data_size = UE_info_sim[ue_index].data_size;
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
124
		}*/
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
125
  	}
126
	/*Retransmission*/
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
127
	else 
128 129 130 131 132
	{
		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
133
			LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] TBS is not enough for retransmission\n", mac_inst->current_subframe);
134 135 136
			return;
		}
	}
Nick Ho's avatar
Nick Ho committed
137 138
#endif

Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
139
	//data_size=200;	//for testing
Nick Ho's avatar
Nick Ho committed
140 141

	LOG_D(MAC,"[%04d][DLSchedulerUSS] UE data size %d\n", mac_inst->current_subframe, data_size);
142 143 144
	//Have DCCH data
	if(data_size == 0)
	{
Nick Ho's avatar
Nick Ho committed
145
		LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] No data in DCCH0_NB_IoT\n", mac_inst->current_subframe);
146 147 148 149 150 151 152 153 154
		return;
	}
	if(data_size>127)
	{
		subheader_length=3;
	}
	if(TBS > data_size+subheader_length)
	{
		TBS = get_tbs(data_size, I_tbs, &I_sf);
Nick Ho's avatar
Nick Ho committed
155
		LOG_D(MAC,"[%04d][DLSchedulerUSS] TBS change to %d because data size is smaller than previous TBS\n", mac_inst->current_subframe, TBS);
156 157
	}

Nick Ho's avatar
Nick Ho committed
158 159 160
  	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);
161
	/*Loop all NPDCCH candidate position*/
162
	for(cdd_num=0;cdd_num<UE_info->R_max/UE_sched_ctrl_info->R_dci;++cdd_num)
163
	{
164
		//LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate num %d DCI Rep %d\n",mac_inst->current_subframe, cdd_num, UE_sched_ctrl_info->R_dci);
165 166 167 168 169 170 171
		/*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)
		{
172
		  //LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate num %d allocate success\n",mac_inst->current_subframe, cdd_num);
Nick Ho's avatar
Nick Ho committed
173
			//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);
174
			
175 176 177 178
			//Max DL TBS
			if(TBS > data_size+subheader_length)
			{
				TBS = get_tbs(data_size, I_tbs, &I_sf);
179
				LOG_D(MAC,"[%04d][DLSchedulerUSS] [%d] data_size %d TBS change to %d \n", mac_inst->current_subframe,UE_info->rnti, data_size, TBS);
180
			}
181 182
			

183 184
			//Get number of subframe this UE need per repetition
			n_sf = get_num_sf(I_sf);
185 186
			//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);
187 188 189 190 191 192
			//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)
		        {
193
		          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);
194 195 196 197
		          //Have available resource
		          /*Check HARQ resource*/
		          if(end_flagSCH!=-1)
		          {
Nick Ho's avatar
Nick Ho committed
198 199
		            //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);
200 201
		            for(HARQ_delay=0;HARQ_delay<4;++HARQ_delay)
		            {
Nick Ho's avatar
Nick Ho committed
202
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ delay %d\n", mac_inst->current_subframe,get_HARQ_delay(1, HARQ_delay) );
203
		              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);
204 205
		              if(end_flagHARQ!=-1)
		              {
Nick Ho's avatar
Nick Ho committed
206
		                //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);
207 208 209 210 211 212 213
		                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;
214
		                  /*.......
215
		                  //Request data from RLC layer
Wolfgang A. Mozart's avatar
Wolfgang A. Mozart committed
216
		                  rlc_status = mac_rlc_status_ind(
217 218 219 220 221 222 223 224 225 226 227
		                        module_id,
		                        UE_info->rnti,
		                        module_id,
		                        frame_start,
		                        subframe_start,
		                        1,
		                        0,
		                        DCCH0_NB_IoT,
		                        TBS-subheader_length);
		                  */
		                  //mac_sdu_size = mac_rlc_data_req_eNB_NB_IoT(module_id, UE_info->rnti, 0, frame_start, 0, DCCH0_NB_IoT, sdu_temp);
228 229 230
		                  
		                  //channel=DCCH0_NB_IoT;

231 232 233 234
		                  //Generate header
		                  //payload_offset = generate_dlsch_header_NB_IoT(UE_info->DLSCH_pdu.payload, 1, &logical_channel, &mac_sdu_size, 0, 0, TBS);
		                  //Complete MAC PDU
		                  //memcpy(UE_info->DLSCH_pdu.payload+payload_offset, sdu_temp, mac_sdu_size);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		                  
		                  //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;
251
		                }
Nick Ho's avatar
Nick Ho committed
252 253 254 255 256 257 258 259 260 261
		                LOG_D(MAC,"[%04d][DLSchedulerUSS][%d][Success] Complete scheduling with data size %d\n", mac_inst->current_subframe, UE_info->rnti, data_size);
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] RNTI %d\n", mac_inst->current_subframe, UE_info->rnti);
		                LOG_D(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_D(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_D(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_D(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_D(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_D(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_D(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_D(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);
262
		                //Store PDU in UE template for retransmission
263
		                //fill_DCI_N1(DCI_N1, UE_info, I_delay, I_sf, HARQ_info->ACK_NACK_resource_field);
Nick Ho's avatar
Nick Ho committed
264
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ index %d\n", HARQ_info->ACK_NACK_resource_field);
265 266 267
		                
		                //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);

268
		                //Generate Scheduling result for this UE
Nick Ho's avatar
Nick Ho committed
269
		                //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);
270
		                //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
271
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] finish generate scheduling result\n");
272 273
		                //matain DL avialable resource
		                maintain_resource_DL(mac_inst, NPDCCH_info, NPDSCH_info);
Nick Ho's avatar
Nick Ho committed
274
		                //available_resource_DL_t *temp = available_resource_DL;
275 276 277
		                /*
		                while(temp!=NULL)
		                {
Nick Ho's avatar
Nick Ho committed
278
		                  LOG_D(MAC,"[%04d][DLSchedulerUSS] Available resource node subframe start %d end %d\n", mac_inst->current_subframe, temp->start_subframe, temp->end_subframe);
279 280 281 282 283
		                  temp=temp->next;
		                }
		                */
		                //Do maintain UL resource
		                adjust_UL_resource_list(HARQ_info);
Nick Ho's avatar
Nick Ho committed
284 285
		                LOG_D(MAC,"[%04d][DLSchedulerUSS] Complete DL scheduling\n", mac_inst->current_subframe);
		                //Change the UE state to idle
286
		                //UE_info->direction = -1;
287

Nick Ho's avatar
Nick Ho committed
288
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] RNTI %d complete scheduling\n", mac_inst->current_subframe, UE_info->rnti);
289 290 291 292 293 294 295

		                return;
		              }
		            }
		            /*harq resource fail*/
		            if(end_flagHARQ==-1)
		            {
Nick Ho's avatar
Nick Ho committed
296
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]HARQ_delay %d HARQ Resource fail\n", mac_inst->current_subframe, HARQ_delay);
297 298
		            }
		          }
Nick Ho's avatar
Nick Ho committed
299
		          //LOG_D(MAC,"[%04d][DLSchedulerUSS] Scheduling delay index %d allocate fail\n", mac_inst->current_subframe, I_delay);
300 301 302 303 304
		        }
			}
			/*NPDSCH resource fail*/
			if(end_flagSCH==-1)
			{
Nick Ho's avatar
Nick Ho committed
305
				//LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]I_delay %d NPDSCH Resource fail\n", mac_inst->current_subframe, I_delay);
306 307
			}
		}
Nick Ho's avatar
Nick Ho committed
308
		//LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate %d no resource\n", mac_inst->current_subframe, cdd_num);
309 310

	}
311 312
	UE_sched_ctrl_info->flag_schedule_success=0;

313 314 315
	/*Resource allocate fail*/
	if((end_flagCCH==-1)||(end_flagSCH==-1)||(end_flagHARQ==-1))
	{
Nick Ho's avatar
Nick Ho committed
316 317
		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);
318
	}
319
	return -1;
320 321 322 323 324
}

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
325 326
	//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);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	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
354
	//LOG_D(MAC,"abs_end = %d\n", schedule_info->sf_end);
355 356 357 358 359 360 361 362 363 364
	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
365
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 1\n");
366 367
	/*initialize*/
	schedule_info->node = available_resource_DL;
Nick Ho's avatar
Nick Ho committed
368
	//LOG_D(MAC,"rsc need start subframe %d end subframe %d\n", rsc_start_sf, rsc_end_sf);
369 370 371 372
	/*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
373
		//LOG_D(MAC,"check_resource_DL_NB_IoT flag 2\n");
374 375 376 377 378 379 380 381 382 383
		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
384
		//LOG_D(MAC,"node start %d node end %d\n", node_start_sf, node_end_sf);
385 386 387 388 389 390
        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
391
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 3\n");
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	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
410
		LOG_D(MAC,"index %d sdu size %d\n", i, sdu_length[i]);
411 412 413 414 415 416 417 418 419 420 421 422 423 424
		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
425
		LOG_D(MAC,"[ERROR]TBS less than require subheader and control element\n");
426 427
		return -1;
	}
Nick Ho's avatar
Nick Ho committed
428 429
	LOG_D(MAC,"total SDU size %d\n", total_sdu_size);
	LOG_D(MAC,"padding size %d\n", padding_size);
430 431 432 433 434 435 436 437
	if(padding_size>2)
	{
		flag_end_padding=1;
	}
	if((padding_size<=2)&&(padding_size>0))
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
438
		//mac_header->F2=0;
439 440 441 442 443 444 445 446
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(padding_size==2)
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
447
		//mac_header->F2=0;
448 449 450 451 452 453 454 455
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(flag_drx==1)
	{
		mac_header->LCID=DRX_COMMAND;
		mac_header->E=1;
456
		//mac_header->F2=0;
457 458 459 460 461 462 463 464 465 466 467
		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];
468
            //mac_header->F2=0;
469 470
            mac_header->R=0;
            offset++;
Nick Ho's avatar
Nick Ho committed
471
            LOG_D(MAC,"last sdu\n");
472 473
        }
        else
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
            if(sdu_length[i]<128)
            {
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->LCID = logical_channel[i];
                ((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)->F=0;
                ((SCH_SUBHEADER_SHORT_NB_IoT*)mac_header)->L=(uint8_t)sdu_length[i];
                num_subheader--;
                mac_header+=2;
                offset+=2;
            }
            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;
            }
        }
	}
	if(flag_end_padding==1)
	{
		mac_header->LCID=PADDING;
		mac_header->E=0;
506
		//mac_header->F2=0;
507 508 509 510 511 512
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	return offset;
}
513
void fill_DCI_N1(DCIFormatN1_t *DCI_N1, UE_TEMPLATE_NB_IoT *UE_info, UE_SCHED_CTRL_NB_IoT_t *UE_sched_ctrl_info)
514 515 516
{
	DCI_N1->type=1;
	DCI_N1->orderIndicator = 0;
517 518 519 520 521
	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;
	DCI_N1->RepNum = UE_sched_ctrl_info->dci_n1_index_sf;
	DCI_N1->HARQackRes = UE_sched_ctrl_info->dci_n1_index_ack_nack;
522
	//DCI_N1->DCIRep = 3-UE_info->R_max/UE_info->R_dci/2;
523 524
	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);
525
}
526
	
527

528
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)
529 530 531 532 533 534 535 536 537
{
	// 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;
538 539
	NPDCCH_result->output_subframe = NPDCCH_sf_start;
	NPDCCH_result->end_subframe = NPDCCH_sf_end;
540 541 542 543
	NPDCCH_result->sdu_length = TBS;
	NPDCCH_result->direction = 1;
	NPDCCH_result->rnti_type = 3;
	NPDCCH_result->DLSCH_pdu = NULL;
544
	NPDCCH_result->DCI_pdu = (void*)DCI_pdu;
545 546
	NPDCCH_result->DCI_release = 0;
	NPDCCH_result->channel = NPDCCH;
547 548
	//NPDCCH_result->debug_str = (uint8_t*)malloc(6*sizeof(uint8_t));
	//NPDCCH_result->debug_str = dl_str1;
549 550 551
	NPDCCH_result->next = NULL;
	/*fill NPDSCH result*/
	NPDSCH_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
552
	//NPDSCH_result->output_subframe = NPDSCH_subframe;
553 554
	NPDSCH_result->output_subframe = NPDSCH_sf_start;
	NPDSCH_result->end_subframe = NPDSCH_sf_end;
555 556 557 558 559
	NPDSCH_result->sdu_length = TBS;
	//NPDSCH_result->DLSCH_pdu = DLSCH_pdu;
	NPDSCH_result->DLSCH_pdu = NULL;
	NPDSCH_result->direction = 1;
	NPDSCH_result->rnti_type = 3;
560
	NPDSCH_result->DCI_pdu = (void*)DCI_pdu;
561 562
	NPDSCH_result->DCI_release = 0;
	NPDSCH_result->channel = NPDSCH;
563 564
	//NPDSCH_result->debug_str = (uint8_t*)malloc(7*sizeof(uint8_t));
	//NPDSCH_result->debug_str = dl_str2;
565 566 567
	NPDSCH_result->next = NULL;
	/*fill HARQ result*/
	HARQ_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
568
	//HARQ_result->output_subframe = HARQ_subframe;
569 570
	HARQ_result->output_subframe = HARQ_sf_start;
	HARQ_result->end_subframe = HARQ_sf_end;
571
	HARQ_result->sdu_length = 0;
Nick Ho's avatar
Nick Ho committed
572
	HARQ_result->direction = 0;
573 574
	HARQ_result->rnti_type = 3;
	HARQ_result->DLSCH_pdu = NULL;
575
	HARQ_result->DCI_pdu = (void*)DCI_pdu;
576 577 578
	HARQ_result->DCI_release = 1;
	HARQ_result->channel = NPUSCH;
	HARQ_result->npusch_format = 1;
579 580
	//HARQ_result->debug_str = (uint8_t*)malloc(7*sizeof(uint8_t));
	//HARQ_result->debug_str = dl_str3;
581
	HARQ_result->next = NULL;
582
	//DEBUG("[generate_scheduling_result_DL] Generate NPDCCH node\n");
583 584 585 586 587
	/*NPDCCH scheduling result*/
	// be the first node of the DL scheduling result
	
	tmp = NULL;
	tmp1 = NULL;
588

589 590 591 592
	if(schedule_result_list_DL == NULL)
	{
		//schedule_result_list_DL = (schedule_result_t*)malloc(sizeof(schedule_result_t));
		schedule_result_list_DL = NPDCCH_result;
593
		//DEBUG("[generate_scheduling_result_DL] Generate NPDCCH node at head\n");
594 595 596 597 598 599
	}
	else
	{
		tmp = schedule_result_list_DL;
		while(tmp!=NULL)
		{
600
			if(NPDCCH_sf_start < tmp->output_subframe)
601 602 603 604 605
			{
				break;
			}
			tmp1 = tmp;
			tmp = tmp->next;
606
			//DEBUG("[generate_scheduling_result_DL] node output subframe %d at NPDCCH part\n", tmp->output_subframe);
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		}
		/*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;
			}
		}
	}
626 627
	
	//DEBUG("[generate_scheduling_result_DL] Generate NPDCSH node\n");
628 629 630 631 632
	/*NPDSCH scheduling result*/
	tmp1 = NULL;
	tmp = schedule_result_list_DL;
	while(tmp!=NULL)
	{
633
		if(NPDSCH_sf_start < tmp->output_subframe)
634 635 636
		{
			break;
		}
637
		//DEBUG("[generate_scheduling_result_DL] node output subframe %d at NPDSCH part\n", tmp->output_subframe);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		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;
		}
	}
657
	//DEBUG("[generate_scheduling_result_DL] Generate HARQ node\n");
658 659 660
	/*HARQ scheduling result*/
	// be the first node of UL
	// be the first node of UL
661
#if 1
662
	tmp1 = NULL;
663
	tmp = NULL;
664 665
	if(schedule_result_list_UL == NULL)
	{
666
		//DEBUG("[generate_scheduling_result_DL] LIST NULL, Generate HARQ at HEAD\n");
667 668 669 670 671 672 673 674
	  //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)
		{
675
			if(HARQ_sf_start < tmp->output_subframe)
676 677 678
			{
				break;
			}
679
			//DEBUG("[generate_scheduling_result_DL] node output subframe %d at HARQ part\n", tmp->output_subframe);
680 681 682 683 684
			tmp1 = tmp;
			tmp = tmp->next;
		}
		if(tmp==NULL)
		{
685
			//DEBUG("[generate_scheduling_result_DL] Generate HARQ at Tail\n");
686 687 688 689 690 691 692
			tmp1->next = HARQ_result;
		}
		else
		{
			HARQ_result->next = tmp;
			if(tmp1)
			{
693
				//DEBUG("[generate_scheduling_result_DL] Generate HARQ in normal\n");
694 695 696
				tmp1->next = HARQ_result;
			}else
			{
697
				//DEBUG("[generate_scheduling_result_DL] Generate HARQ at Head\n");
698 699 700 701
				schedule_result_list_UL = HARQ_result;
			}
		}
	}
702
#endif
703
}
704

705 706 707 708 709 710
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;
711
	uint32_t H_temp, f_temp, sf_temp;
Nick Ho's avatar
Nick Ho committed
712
  	uint32_t H_temp_r, f_temp_r, sf_temp_r;
713 714 715 716 717 718 719 720 721 722 723 724

	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
725
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
726 727 728
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
729 730 731 732
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
733
				temp = (available_resource_DL_t *)malloc(sizeof(available_resource_DL_t));
Nick Ho's avatar
Nick Ho committed
734 735 736 737 738 739 740 741
				
				if((available_resource_DL_t *)0 == NPDCCH_info->node->next){
					available_resource_DL_last = temp;
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
                
                
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
				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
761
				
762 763 764
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
				
				//  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;
                }
                
782 783 784 785 786 787 788 789 790 791 792 793 794
				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
795
			LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH and NPDSCH using the same node\n", mac_inst->current_subframe);
796 797 798 799 800 801 802 803 804 805
		}
		/****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
806
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
807 808 809
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
810 811 812 813
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
814 815 816 817
				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
818 819 820
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
				
				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
840
					LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH keep one node\n", mac_inst->current_subframe);
841 842 843 844 845 846 847 848
				}
				else
				{
					NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start-1 ;
				}
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
849
				LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH delete node\n", mac_inst->current_subframe);
850 851
				
				//	calvin add
Nick Ho's avatar
Nick Ho committed
852
				//	delete
853
				
Nick Ho's avatar
Nick Ho committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
				//  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;
                }
                
870 871 872 873 874 875 876 877 878 879 880 881
				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
882 883 884 885
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
886 887 888 889
				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
890 891 892
				}else{
				    NPDSCH_info->node->next->prev = temp;
                }
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
				
				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
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
				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;
                }

933 934 935 936 937 938 939 940 941
				free(NPDSCH_info->node);
				break;
			default:
				//error
				break;
		}
		free(NPDCCH_info);
		free(NPDSCH_info);
	}
Nick Ho's avatar
Nick Ho committed
942 943

}
Matthieu Kanj's avatar
Matthieu Kanj committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

//// 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
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
/////////////////
///////////////////////////////////////////////
//// 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
1001

Nick Ho's avatar
Nick Ho committed
1002 1003 1004 1005 1006 1007 1008 1009 1010
/*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
1011
		return 10;
Nick Ho's avatar
Nick Ho committed
1012 1013 1014
	}
	else
	{
Nick Ho's avatar
Nick Ho committed
1015
		return 4;
Nick Ho's avatar
Nick Ho committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	}
}

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))
	{
1028
		
Nick Ho's avatar
Nick Ho committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		//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;
	}
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
}

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
1090
    LOG_D(MAC,"[get_index_Rep] error\n");
1091
  }
Nick Ho's avatar
Nick Ho committed
1092 1093
  return 0;
}