MACScheduler.cc 8.7 KB
Newer Older
C
x  
calincerchez 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// 

#include "MACScheduler.h"
C
calin cerchez 已提交
17
#include "RRC.h"
C
x  
calincerchez 已提交
18 19 20 21
#include "LTEControlInfo_m.h"

Define_Module(MACScheduler);

C
calin cerchez 已提交
22 23
MACScheduler::MACScheduler() {
	prachCfgIndex = 64;
C
calin cerchez 已提交
24
	dlBandwith = 6;
C
calin cerchez 已提交
25 26 27
	dlCyclPref = CTRL_CP_NORMAL;
	nrOfSubcarriers = 12;
	nrOfAntennas = 1;
C
calin cerchez 已提交
28 29 30 31 32 33
}

MACScheduler::~MACScheduler() {

}

C
x  
calincerchez 已提交
34 35 36 37 38 39
void MACScheduler::initialize(int stage) {
    // TODO - Generated method body
    if (stage == 3) {
        nb = NotificationBoardAccess().get();

        nb->subscribe(this, SCHED_DL_TRIGGER_REQ);
C
calin cerchez 已提交
40
        nb->subscribe(this, SCHED_UL_TRIGGER_REQ);
C
x  
calincerchez 已提交
41
        nb->subscribe(this, CSCHED_CELL_CONFIG_REQ);
C
calin cerchez 已提交
42
        nb->subscribe(this, SCHED_DL_RACH_INFO_REQ);
C
x  
calincerchez 已提交
43 44 45 46 47 48 49
    }
}

void MACScheduler::handleMessage(cMessage *msg) {
    // TODO - Generated method body
}

C
calin cerchez 已提交
50 51 52 53 54 55 56 57 58 59 60
unsigned char MACScheduler::getRBGSize() {
	if (dlBandwith < 11)
		return 1;
	else if (dlBandwith < 27)
		return 2;
	else if (dlBandwith < 64)
		return 3;
	else
		return 4;
}

C
calin cerchez 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
unsigned char MACScheduler::getReNrInReg(unsigned char l) {
	if (l == 0)
		return 6;
	else if (l == 1 && nrOfAntennas == 4)
		return 6;
	else if (l == 1 && nrOfAntennas != 4)
		return 4;
	else if (l == 2)
		return 4;
	else if (l == 3 && dlCyclPref == CTRL_CP_NORMAL)
		return 4;
	else if (l == 3 && dlCyclPref == CTRL_CP_EXTENDED)
		return 6;
	return 0;
}

C
calin cerchez 已提交
77 78 79 80 81 82 83
unsigned MACScheduler::getRIV(unsigned char lCRB, unsigned char rbStart) {
	if ((lCRB - 1) < floor(dlBandwith / 2))
		return dlBandwith * (lCRB - 1) + rbStart;
	else
		return dlBandwith * (dlBandwith - lCRB + 1) + (dlBandwith - 1 - rbStart);
}

C
calin cerchez 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
unsigned char MACScheduler::getNrOfDlSymbForDci(unsigned short nReg) {
	unsigned char l = 0;
	unsigned short nrOfResInSymb = dlBandwith * nrOfSubcarriers;
	unsigned short nrOfRegsInSymb = nrOfResInSymb / getReNrInReg(l);
	nrOfRegsInSymb -= 4; // first symbol has also PCFICH resource elements which don't count
	if (nReg < nrOfRegsInSymb)
		return 1;
	l++;

	for (; l < 4; l++) {
		nrOfRegsInSymb += nrOfResInSymb / getReNrInReg(l);
		if (nReg < nrOfRegsInSymb)
			return l + 1;
	}
	return 0;
}

C
calin cerchez 已提交
101
void MACScheduler::processScheduleDlTriggerRequest(SchedDlTriggerReq *triggReq) {
C
calincerchez 已提交
102 103 104
//	EV << "MACScheduler: Received SCHED_DL_TRIGGER_REQ. Processing the request.\n";
    unsigned char sf = triggReq->getSf();
    unsigned short sfn = triggReq->getSfn();
C
calin cerchez 已提交
105 106 107 108 109

    bool rbBitmap[dlBandwith];
    unsigned char rbStart = 0;
    memset(rbBitmap, false, dlBandwith);

C
calin cerchez 已提交
110 111
    unsigned short nReg = 0;

C
calin cerchez 已提交
112 113
    unsigned nrBldBcastList = 0;
    SchedDlConfigInd *cfgInd = new SchedDlConfigInd();
C
calincerchez 已提交
114 115
    cfgInd->setSf(sf);
    cfgInd->setSfn(sfn);
C
x  
calincerchez 已提交
116

C
calin cerchez 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 174 175 176
    // SystemInformationBlock1 scheduling
    if (sfn % 2 == 0 && sf == 5) {
    	unsigned char lCRB = 0;
    	unsigned siLen = siCfg.getSib1Length() * 8;

    	// find number of RBs needed to carry SIB1
    	unsigned char iTBS = 0;
    	unsigned char tpc = 0;
    	for (unsigned char iTBS = 0; iTBS < I_TBS_SIZE; iTBS++) {
    		if (tbSizeTable[1][iTBS] == siLen) {
    			lCRB = 2;
    			tpc = 0;
    			break;
    		} else if (tbSizeTable[2][iTBS] == siLen) {
    			lCRB = 3;
    			tpc = 1;
    			break;
    		}
    	}
    	unsigned char iMCS = iTBS;

    	// find position where the number of RBs needed are free
    	unsigned char rbStart = 0;
    	unsigned char freeRbs = 0;
    	for (unsigned char i = 0; i < dlBandwith; i++) {
    		if (rbBitmap[i] == false) {
    			freeRbs++;
    			if (freeRbs == lCRB) {
    				break;
    			}
    		} else {
    			freeRbs = 0;
    			rbStart = i;
    		}
    	}

    	// allocation possible
    	if (rbStart != (dlBandwith - 1)) {
    		// reserve the resource blocks in the bitmap
    		for (unsigned char i = rbStart; i < rbStart + lCRB; i++)
    			rbBitmap[i] = true;
    	}

        BuildBcastListElement bcastEl = BuildBcastListElement();
        bcastEl.setIndex(SIB1);

        DlDciListElement dciEl = DlDciListElement();
        dciEl.setRnti(65535);
        dciEl.setRbBitmap(getRIV(lCRB, rbStart));
        dciEl.setResAlloc(2);
        dciEl.setNrOfTbs(1);
        dciEl.setTbsSize(0, siCfg.getSib1Length());
        dciEl.setMcs(0, iMCS);
        dciEl.setRv(0, (unsigned)ceil(3 / 2 * ((sfn / 2) % 4)) % 4);
        dciEl.setFormat(DCI_FORMAT_1A);
        dciEl.setVrbFormat(VRB_LOCALIZED);
        dciEl.setTpc(tpc);
        dciEl.setTbsIdx(iTBS);
        bcastEl.setDci(dciEl);

C
calin cerchez 已提交
177 178
        nReg += 18;

C
calin cerchez 已提交
179 180 181
        cfgInd->setBldBcastListArraySize(++nrBldBcastList);
        cfgInd->setBldBcastList(nrBldBcastList - 1, bcastEl);
    }
C
calincerchez 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
//
//    // SystemInformationBlock scheduling
//    unsigned w = siCfg.getSiWdwLen();
//    for (unsigned n = 0; n < siCfg.getSiMsgListArraySize(); n++) {
//        SiMessageListElement msgEl = siCfg.getSiMsgList(0);
//        unsigned x = n * w;
//        unsigned T = msgEl.getPeriod();
//        if ((sfn % T == (unsigned)floor(x / 10)) && (tti % 10 == x)) {
//            BuildBcastListElement bcastEl = BuildBcastListElement();
//            bcastEl.setIndex(n + 1);
//            DlDciListElement dciEl = DlDciListElement();
//            dciEl.setRnti(65535);
//            bcastEl.setDci(dciEl);
//            cfgInd->setBldBcastListArraySize(++nrBldBcastList);
//            cfgInd->setBldBcastList(nrBldBcastList - 1, bcastEl);
//        }
//    }
//
//    // Random Access Response scheduling
//    RARBuffer::iterator i = rarBuffer.find(tti);
//    if (i != rarBuffer.end()) {
//    	BuildRarListElement rarEl = BuildRarListElement();
//    	rarEl.setGrant(0);	// TODO UL grant split into bits
//    	rarEl.setRnti(i->second);
////        DlDciListElement dciEl = DlDciListElement();
////        dciEl.setRnti(0);
////    	rarEl.setDci(dciEl);
//    	cfgInd->setBldRarListArraySize(1);
//    	cfgInd->setBldRarList(0, rarEl);
//    }
C
calin cerchez 已提交
212

C
calin cerchez 已提交
213 214
    cfgInd->setNrOfPdcchSymb(getNrOfDlSymbForDci(nReg));

C
calin cerchez 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    nb->fireChangeNotification(SCHED_DL_CONFIG_IND, cfgInd);
}

void MACScheduler::processScheduleUlTriggerRequest(SchedUlTriggerReq *triggReq) {
	EV << "MACScheduler: Received SCHED_UL_TRIGGER_REQ. Processing the request.\n";
    unsigned tti = triggReq->getTti();
    unsigned sfn = tti / 10;
    SchedUlConfigInd *cfgInd = new SchedUlConfigInd();
    cfgInd->setTti(tti);

    // RACH scheduling
    if (prachCfgIndex != 64 && !triggReq->getRapComplete()) {
    	switch(prachCfgIndex % 16) {
    	case 0:
    		if (tti % 10 == 1 && sfn % 2 == 0) {
    			cfgInd->setRaConfigListArraySize(1);
    			cfgInd->setRaConfigList(0, raCfg);
    		}
    		break;
    	case 1:
    		if (tti % 10 == 4 && sfn % 2 == 0) {
    			cfgInd->setRaConfigListArraySize(1);
    			cfgInd->setRaConfigList(0, raCfg);
    		}
    		break;
    	default:
    		break;
    	}
    }
C
x  
calincerchez 已提交
244

C
calin cerchez 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    nb->fireChangeNotification(SCHED_UL_CONFIG_IND, cfgInd);
}

void MACScheduler::receiveChangeNotification(int category, const cPolymorphic *details) {
    Enter_Method_Silent();

    if (category == SCHED_DL_TRIGGER_REQ) {
    	SchedDlTriggerReq *triggReq = check_and_cast<SchedDlTriggerReq*>(details);
    	processScheduleDlTriggerRequest(triggReq);
    } else if (category == SCHED_UL_TRIGGER_REQ) {
        SchedUlTriggerReq *triggReq = check_and_cast<SchedUlTriggerReq*>(details);
        processScheduleUlTriggerRequest(triggReq);
    } else if (category == SCHED_DL_RACH_INFO_REQ) {
    	EV << "MACScheduler: Received SCHED_DL_RACH_INFO_REQ. Processing the request.\n";
    	SchedDlRachInfoReq *rachReq = check_and_cast<SchedDlRachInfoReq*>(details);
    	for (unsigned i = 0; i < rachReq->getRachListArraySize(); i++) {	// normally it should be only one
    		RachListElement rachEl = rachReq->getRachList(i);
    		unsigned short schedTti = rachReq->getTti() + 3 + uniform(0, raCfg.getRaRespWdwSize());
    		rarBuffer[schedTti] = rachEl.getRnti();
    	}
C
x  
calincerchez 已提交
265
    } else if (category == CSCHED_CELL_CONFIG_REQ) {
C
calin cerchez 已提交
266
    	EV << "MACScheduler: Received CSCHED_CELL_CONFIG_REQ. Processing the request.\n";
C
x  
calincerchez 已提交
267 268
        CSchedCellConfigReq *cellCfg = check_and_cast<CSchedCellConfigReq*>(details);

C
calin cerchez 已提交
269 270 271 272
        dlCyclPref = cellCfg->getDlCyclPrefLen();

        nrOfAntennas = cellCfg->getAntennaPortsCount();

C
x  
calincerchez 已提交
273
        siCfg = cellCfg->getSiConfig();
C
calin cerchez 已提交
274

C
calin cerchez 已提交
275 276
        dlBandwith = cellCfg->getDlBandwith();

C
calin cerchez 已提交
277 278 279
        raCfg = cellCfg->getRaConfig();

        prachCfgIndex = cellCfg->getPrachCfgIndex();;
C
x  
calincerchez 已提交
280 281
    }
}