eNB_scheduler_dlsch_NB_IoT.c 34.3 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 32 33

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

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

64
	//uint8_t sdu_temp[SCH_PAYLOAD_SIZE_MAX_NB_IoT];
Nick Ho's avatar
Nick Ho committed
65
	//logical_chan_id_t logical_channel;
66 67

	uint32_t subheader_length=2;
68
	//uint32_t payload_offset;
69 70 71

	uint32_t search_space_end_sf, h_temp, f_temp, sf_temp;

Nick Ho's avatar
Nick Ho committed
72
	I_mcs = get_I_mcs(UE_info->CE_level);
73 74 75
	I_tbs = I_mcs;
	//get max TBS
	TBS = get_max_tbs(I_tbs);
Nick Ho's avatar
Nick Ho committed
76 77

	LOG_D(MAC,"[%04d][DLSchedulerUSS] Max TBS %d MCS index %d TBS index %d\n", mac_inst->current_subframe, TBS, I_mcs, I_tbs);
78 79
	/*set UE data information*/
	/*New transmission*/
Nick Ho's avatar
Nick Ho committed
80
#if 0
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if(UE_info->HARQ_round==0)
	{
		//Get RLC status
		/*
		rlc_status = mac_rlc_status_ind_NB_IoT(
										module_id,
										UE_info->rnti,
										module_id,
										frame_start,
										subframe_start,
										1,
										0,
										DCCH0_NB_IoT,
										0);
		data_size = rlc_status.bytes_in_buffer;
		*/
Nick Ho's avatar
Nick Ho committed
97 98 99 100 101 102 103 104
		//data_size = 200;
		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;
		}
105 106 107 108 109 110 111 112
	}
	/*Retransmission*/
	else
	{
		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
113
			LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] TBS is not enough for retransmission\n", mac_inst->current_subframe);
114 115 116
			return;
		}
	}
Nick Ho's avatar
Nick Ho committed
117 118 119 120 121
#endif

	data_size=200;	//for testing

	LOG_D(MAC,"[%04d][DLSchedulerUSS] UE data size %d\n", mac_inst->current_subframe, data_size);
122 123 124
	//Have DCCH data
	if(data_size == 0)
	{
Nick Ho's avatar
Nick Ho committed
125
		LOG_D(MAC,"[%04d][DLSchedulerUSS][Fail] No data in DCCH0_NB_IoT\n", mac_inst->current_subframe);
126 127 128 129 130 131 132 133 134
		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
135
		LOG_D(MAC,"[%04d][DLSchedulerUSS] TBS change to %d because data size is smaller than previous TBS\n", mac_inst->current_subframe, TBS);
136 137 138
	}

  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);
Nick Ho's avatar
Nick Ho committed
139 140
  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);
141 142 143
	/*Loop all NPDCCH candidate position*/
	for(cdd_num=0;cdd_num<UE_info->R_max/UE_info->R_dci;++cdd_num)
	{
Nick Ho's avatar
Nick Ho committed
144
		//LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate num %d DCI Rep %d\n", cdd_num, UE_info->R_dci);
145 146 147 148 149 150 151
		/*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)
		{
Nick Ho's avatar
Nick Ho committed
152 153
		  //LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate num %d allocate success\n", cdd_num);
			//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);
154 155 156 157 158
			/*
			//Max DL TBS
			if(TBS > data_size+subheader_length)
			{
				TBS = get_tbs(data_size, I_tbs, &I_sf);
Nick Ho's avatar
Nick Ho committed
159
				LOG_D(MAC,"[%04d][DLSchedulerUSS] TBS change to %d because data size is smaller than previous TBS\n", mac_inst->current_subframe, TBS);
160 161 162 163
			}
			*/
			//Get number of subframe this UE need per repetition
			n_sf = get_num_sf(I_sf);
Nick Ho's avatar
Nick Ho committed
164 165
			//LOG_D(MAC,"[%04d][DLSchedulerUSS] Number SF %d index SF %d\n", n_sf, I_sf);
			//LOG_D(MAC,"[%04d][DLSchedulerUSS] Require total %d DL SF Rep %d\n", n_sf*UE_info->R_dl, UE_info->R_dl);
166 167 168 169 170 171 172 173 174 175 176
			//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)
		        {
		          end_flagSCH = check_resource_NPDSCH_NB_IoT(mac_inst, NPDSCH_info, NPDCCH_info->sf_end, I_delay, UE_info->R_max, UE_info->R_dl, n_sf);
		          //Have available resource
		          /*Check HARQ resource*/
		          if(end_flagSCH!=-1)
		          {
Nick Ho's avatar
Nick Ho committed
177 178
		            //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);
179 180
		            for(HARQ_delay=0;HARQ_delay<4;++HARQ_delay)
		            {
Nick Ho's avatar
Nick Ho committed
181
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ delay %d\n", mac_inst->current_subframe,get_HARQ_delay(1, HARQ_delay) );
182 183 184
		              end_flagHARQ=Check_UL_resource(NPDSCH_info->sf_end+get_HARQ_delay(1, HARQ_delay), UE_info->R_harq, HARQ_info, 0, 1);
		              if(end_flagHARQ!=-1)
		              {
Nick Ho's avatar
Nick Ho committed
185
		                //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);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		                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;
		                  /*
		                  //Request data from RLC layer
		                  rlc_status = mac_rlc_status_ind_NB_IoT(
		                        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);
Nick Ho's avatar
Nick Ho committed
207
		                  //logical_channel=DCCH0_NB_IoT;
208 209 210 211 212 213
		                  //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);
		                  UE_info->DLSCH_pdu.pdu_size=TBS;
		                }
Nick Ho's avatar
Nick Ho committed
214 215 216 217 218 219 220 221 222 223
		                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);
224 225
		                //Store PDU in UE template for retransmission
		                fill_DCI_N1(DCI_N1, UE_info, I_delay, I_sf, HARQ_info->ACK_NACK_resource_field);
Nick Ho's avatar
Nick Ho committed
226 227
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] HARQ index %d\n", HARQ_info->ACK_NACK_resource_field);
		                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);
228
		                //Generate Scheduling result for this UE
Nick Ho's avatar
Nick Ho committed
229 230 231
		                //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);
		                generate_scheduling_result_DL(NPDCCH_info, NPDSCH_info, HARQ_info, DCI_N1, UE_info->rnti, TBS, UE_info->DLSCH_pdu.payload);
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] finish generate scheduling result\n");
232 233
		                //matain DL avialable resource
		                maintain_resource_DL(mac_inst, NPDCCH_info, NPDSCH_info);
Nick Ho's avatar
Nick Ho committed
234
		                //available_resource_DL_t *temp = available_resource_DL;
235 236 237
		                /*
		                while(temp!=NULL)
		                {
Nick Ho's avatar
Nick Ho committed
238
		                  LOG_D(MAC,"[%04d][DLSchedulerUSS] Available resource node subframe start %d end %d\n", mac_inst->current_subframe, temp->start_subframe, temp->end_subframe);
239 240 241 242 243
		                  temp=temp->next;
		                }
		                */
		                //Do maintain UL resource
		                adjust_UL_resource_list(HARQ_info);
Nick Ho's avatar
Nick Ho committed
244 245 246
		                LOG_D(MAC,"[%04d][DLSchedulerUSS] Complete DL scheduling\n", mac_inst->current_subframe);
		                //Change the UE state to idle
		                UE_info->direction = -1;
247

Nick Ho's avatar
Nick Ho committed
248
		                //LOG_D(MAC,"[%04d][DLSchedulerUSS] RNTI %d complete scheduling\n", mac_inst->current_subframe, UE_info->rnti);
249 250 251 252 253 254 255

		                return;
		              }
		            }
		            /*harq resource fail*/
		            if(end_flagHARQ==-1)
		            {
Nick Ho's avatar
Nick Ho committed
256
		              //LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]HARQ_delay %d HARQ Resource fail\n", mac_inst->current_subframe, HARQ_delay);
257 258
		            }
		          }
Nick Ho's avatar
Nick Ho committed
259
		          //LOG_D(MAC,"[%04d][DLSchedulerUSS] Scheduling delay index %d allocate fail\n", mac_inst->current_subframe, I_delay);
260 261 262 263 264
		        }
			}
			/*NPDSCH resource fail*/
			if(end_flagSCH==-1)
			{
Nick Ho's avatar
Nick Ho committed
265
				//LOG_D(MAC,"[%04d][DLSchedulerUSS] [Fail]I_delay %d NPDSCH Resource fail\n", mac_inst->current_subframe, I_delay);
266 267
			}
		}
Nick Ho's avatar
Nick Ho committed
268
		//LOG_D(MAC,"[%04d][DLSchedulerUSS] Candidate %d no resource\n", mac_inst->current_subframe, cdd_num);
269 270 271 272 273

	}
	/*Resource allocate fail*/
	if((end_flagCCH==-1)||(end_flagSCH==-1)||(end_flagHARQ==-1))
	{
Nick Ho's avatar
Nick Ho committed
274 275
		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);
276 277 278 279 280 281
	}
}

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
282 283
	//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);
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	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
311
	//LOG_D(MAC,"abs_end = %d\n", schedule_info->sf_end);
312 313 314 315 316 317 318 319 320 321
	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
322
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 1\n");
323 324
	/*initialize*/
	schedule_info->node = available_resource_DL;
Nick Ho's avatar
Nick Ho committed
325
	//LOG_D(MAC,"rsc need start subframe %d end subframe %d\n", rsc_start_sf, rsc_end_sf);
326 327 328 329
	/*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
330
		//LOG_D(MAC,"check_resource_DL_NB_IoT flag 2\n");
331 332 333 334 335 336 337 338 339 340
		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
341
		//LOG_D(MAC,"node start %d node end %d\n", node_start_sf, node_end_sf);
342 343 344 345 346 347
        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
348
	//LOG_D(MAC,"check_resource_DL_NB_IoT flag 3\n");
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	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
367
		LOG_D(MAC,"index %d sdu size %d\n", i, sdu_length[i]);
368 369 370 371 372 373 374 375 376 377 378 379 380 381
		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
382
		LOG_D(MAC,"[ERROR]TBS less than require subheader and control element\n");
383 384
		return -1;
	}
Nick Ho's avatar
Nick Ho committed
385 386
	LOG_D(MAC,"total SDU size %d\n", total_sdu_size);
	LOG_D(MAC,"padding size %d\n", padding_size);
387 388 389 390 391 392 393 394 395 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 423 424 425 426 427
	if(padding_size>2)
	{
		flag_end_padding=1;
	}
	if((padding_size<=2)&&(padding_size>0))
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
		mac_header->F2=0;
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(padding_size==2)
	{
		mac_header->LCID=PADDING;
		mac_header->E=1;
		mac_header->F2=0;
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	if(flag_drx==1)
	{
		mac_header->LCID=DRX_COMMAND;
		mac_header->E=1;
		mac_header->F2=0;
		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];
            mac_header->F2=0;
            mac_header->R=0;
            offset++;
Nick Ho's avatar
Nick Ho committed
428
            LOG_D(MAC,"last sdu\n");
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        }
        else
        {
            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;
		mac_header->F2=0;
		mac_header->R=0;
		mac_header++;
		offset++;
	}
	return offset;
}
void fill_DCI_N1(DCIFormatN1_t *DCI_N1, UE_TEMPLATE_NB_IoT *UE_info, uint32_t scheddly, uint32_t I_sf, uint32_t I_harq)
{
	DCI_N1->type=1;
	DCI_N1->orderIndicator = 0;
	DCI_N1->Scheddly = scheddly;
	DCI_N1->ResAssign = I_sf;
	DCI_N1->mcs = UE_info->I_mcs_dl;
	DCI_N1->RepNum = get_index_Rep_dl(UE_info->R_dl);
	DCI_N1->HARQackRes = I_harq;
	//DCI_N1->DCIRep = 3-UE_info->R_max/UE_info->R_dci/2;
	DCI_N1->DCIRep = UE_info->R_dci;
}

Nick Ho's avatar
Nick Ho committed
483
void generate_scheduling_result_DL(sched_temp_DL_NB_IoT_t* DCI_info, sched_temp_DL_NB_IoT_t* NPDSCH_info, sched_temp_UL_NB_IoT_t* HARQ_info, DCIFormatN1_t *DCI_inst, rnti_t rnti, uint32_t TBS, uint8_t *DLSCH_pdu)
484 485 486 487 488 489 490 491 492
{
	// 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;
Nick Ho's avatar
Nick Ho committed
493 494
	NPDCCH_result->output_subframe = DCI_info->sf_start;
	NPDCCH_result->end_subframe = DCI_info->sf_end;
495 496 497 498
	NPDCCH_result->sdu_length = TBS;
	NPDCCH_result->direction = 1;
	NPDCCH_result->rnti_type = 3;
	NPDCCH_result->DLSCH_pdu = NULL;
Nick Ho's avatar
Nick Ho committed
499
	NPDCCH_result->DCI_pdu = DCI;
500 501
	NPDCCH_result->DCI_release = 0;
	NPDCCH_result->channel = NPDCCH;
Nick Ho's avatar
Nick Ho committed
502 503

//	NPDCCH_result->printf_str = dl_str1;
504 505 506
	NPDCCH_result->next = NULL;
	/*fill NPDSCH result*/
	NPDSCH_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
507 508 509
	//NPDSCH_result->output_subframe = NPDSCH_subframe;
	NPDSCH_result->output_subframe = NPDSCH_info->sf_start;
	NPDSCH_result->end_subframe = NPDSCH_info->sf_end;
510 511 512 513 514
	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;
Nick Ho's avatar
Nick Ho committed
515
	NPDSCH_result->DCI_pdu = DCI;
516 517
	NPDSCH_result->DCI_release = 0;
	NPDSCH_result->channel = NPDSCH;
Nick Ho's avatar
Nick Ho committed
518 519

	//NPDSCH_result->printf_str = dl_str2;
520 521 522
	NPDSCH_result->next = NULL;
	/*fill HARQ result*/
	HARQ_result->rnti=rnti;
Nick Ho's avatar
Nick Ho committed
523 524 525
	//HARQ_result->output_subframe = HARQ_subframe;
	HARQ_result->output_subframe = HARQ_info->sf_start;
	HARQ_result->end_subframe = HARQ_info->sf_end;
526
	HARQ_result->sdu_length = 0;
Nick Ho's avatar
Nick Ho committed
527
	HARQ_result->direction = 0;
528 529
	HARQ_result->rnti_type = 3;
	HARQ_result->DLSCH_pdu = NULL;
Nick Ho's avatar
Nick Ho committed
530
	HARQ_result->DCI_pdu = DCI;
531 532 533
	HARQ_result->DCI_release = 1;
	HARQ_result->channel = NPUSCH;
	HARQ_result->npusch_format = 1;
Nick Ho's avatar
Nick Ho committed
534 535

//	HARQ_result->printf_str = dl_str3;
536
	HARQ_result->next = NULL;
Nick Ho's avatar
Nick Ho committed
537
  //LOG_D(MAC,"[generate_scheduling_result_DL] Generate NPDCCH node\n");
538 539 540 541 542 543 544 545 546 547
	/*NPDCCH scheduling result*/
	// be the first node of the DL scheduling result
	
	tmp = NULL;
	tmp1 = NULL;
	
	if(schedule_result_list_DL == NULL)
	{
		//schedule_result_list_DL = (schedule_result_t*)malloc(sizeof(schedule_result_t));
		schedule_result_list_DL = NPDCCH_result;
Nick Ho's avatar
Nick Ho committed
548
		LOG_D(MAC,"[generate_scheduling_result_DL] Generate NPDCCH node at head\n");
549 550 551 552 553 554
	}
	else
	{
		tmp = schedule_result_list_DL;
		while(tmp!=NULL)
		{
Nick Ho's avatar
Nick Ho committed
555
			if(DCI_info->sf_start < tmp->output_subframe)
556 557 558 559 560
			{
				break;
			}
			tmp1 = tmp;
			tmp = tmp->next;
Nick Ho's avatar
Nick Ho committed
561
			//LOG_D(MAC,"[generate_scheduling_result_DL] node output subframe %d at NPDCCH part\n", tmp->output_subframe);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
		}
		/*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;
			}
		}
	}
Nick Ho's avatar
Nick Ho committed
581
	//LOG_D(MAC,"[generate_scheduling_result_DL] Generate NPDCSH node\n");
582 583 584 585 586
	/*NPDSCH scheduling result*/
	tmp1 = NULL;
	tmp = schedule_result_list_DL;
	while(tmp!=NULL)
	{
Nick Ho's avatar
Nick Ho committed
587
		if(NPDSCH_info->sf_start < tmp->output_subframe)
588 589 590
		{
			break;
		}
Nick Ho's avatar
Nick Ho committed
591
		//LOG_D(MAC,"[generate_scheduling_result_DL] node output subframe %d at NPDSCH part\n", tmp->output_subframe);
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
		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;
		}
	}
Nick Ho's avatar
Nick Ho committed
611
  //LOG_D(MAC,"[generate_scheduling_result_DL] Generate HARQ node\n");
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	/*HARQ scheduling result*/
	// be the first node of UL
	// be the first node of UL
	
	tmp1 = NULL;
	if(schedule_result_list_UL == NULL)
	{
	  //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)
		{
Nick Ho's avatar
Nick Ho committed
627
			if(HARQ_info->sf_start < tmp->output_subframe)
628 629 630
			{
				break;
			}
Nick Ho's avatar
Nick Ho committed
631
			//LOG_D(MAC,"[generate_scheduling_result_DL] node output subframe %d at HARQ part\n", tmp->output_subframe);
632
			tmp1 = tmp;
Nick Ho's avatar
Nick Ho committed
633
			
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
			tmp = tmp->next;
		}
		if(tmp==NULL)
		{
			tmp1->next = HARQ_result;
		}
		else
		{
			HARQ_result->next = tmp;
			if(tmp1)
			{
				tmp1->next = HARQ_result;
			}else
			{
				schedule_result_list_UL = HARQ_result;
			}
		}
	}
	/*
Nick Ho's avatar
Nick Ho committed
653
	LOG_D(MAC,"---[generate_scheduling_result_DL] schedule result after generate---\n");
654 655 656
	tmp = schedule_result_list_DL;
	while(tmp!=NULL)
	{
Nick Ho's avatar
Nick Ho committed
657
		LOG_D(MAC,"[generate_scheduling_result_DL] node output subframe %d\n", tmp->output_subframe);
658 659 660 661 662 663 664 665 666 667
		tmp = tmp->next;
	}
	*/
}
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;
668
	uint32_t H_temp, f_temp, sf_temp;
Nick Ho's avatar
Nick Ho committed
669
  	uint32_t H_temp_r, f_temp_r, sf_temp_r;
670 671 672 673 674 675 676 677 678 679 680 681

	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
682
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
683 684 685
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
686 687 688 689
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
690
				temp = (available_resource_DL_t *)malloc(sizeof(available_resource_DL_t));
Nick Ho's avatar
Nick Ho committed
691 692 693 694 695 696 697 698
				
				if((available_resource_DL_t *)0 == NPDCCH_info->node->next){
					available_resource_DL_last = temp;
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
                
                
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
				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
718
				
719 720 721
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
				
				//  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;
                }
                
739 740 741 742 743 744 745 746 747 748 749 750 751
				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
752
			LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH and NPDSCH using the same node\n", mac_inst->current_subframe);
753 754 755 756 757 758 759 760 761 762
		}
		/****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
763
		//LOG_D(MAC,"[maintain_resource_DL] align left %d align right %d\n", align_left, align_right);
764 765 766
		switch(align_left+align_right)
		{
			case 0:
Nick Ho's avatar
Nick Ho committed
767 768 769 770
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
771 772 773 774
				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
775 776 777
				}else{
				    NPDCCH_info->node->next->prev = temp;
                }
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
				
				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
797
					LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH keep one node\n", mac_inst->current_subframe);
798 799 800 801 802 803 804 805
				}
				else
				{
					NPDCCH_info->node->end_subframe = NPDCCH_info->sf_start-1 ;
				}
				break;
			case 2:
				//	delete
Nick Ho's avatar
Nick Ho committed
806
				LOG_D(MAC,"[%04d][maintain_resource_DL] NPDCCH delete node\n", mac_inst->current_subframe);
807 808
				
				//	calvin add
Nick Ho's avatar
Nick Ho committed
809
				//	delete
810
				
Nick Ho's avatar
Nick Ho committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
				//  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;
                }
                
827 828 829 830 831 832 833 834 835 836 837 838
				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
839 840 841 842
				//  divided into two nodes, insert after oritinal node.
				//	A | node | B
		      	//	A | node | temp | B
				
843 844 845 846
				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
847 848 849
				}else{
				    NPDSCH_info->node->next->prev = temp;
                }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
				
				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
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
				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;
                }

890 891 892 893 894 895 896 897 898
				free(NPDSCH_info->node);
				break;
			default:
				//error
				break;
		}
		free(NPDCCH_info);
		free(NPDSCH_info);
	}
Nick Ho's avatar
Nick Ho committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

}
/*Get MCS index*/
uint32_t get_I_mcs(int CE_level)
{
	if(CE_level==0)
	{
		return 13;
	}
	else if(CE_level==1)
	{
		return 8;
	}
	else
	{
		return 2;
	}
}

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))
	{
		//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;
	}
974 975 976 977 978 979 980 981 982 983 984 985 986 987
}

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
988
    LOG_D(MAC,"[get_index_Rep] error\n");
989
  }
Nick Ho's avatar
Nick Ho committed
990 991
  return 0;
}