⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p802_15_4csmaca.cc

📁 Low Rate Wireless Personal Area Networks (LR-WPANs)in C
💻 CC
📖 第 1 页 / 共 2 页
字号:
		tmpf += t_transacTime;		if (tmpf > t_fCAP)			ok = false;		else			ok= true;	}	else	{		//in this case, we need to handle individual CAP 		ok = true;		t_fCAP = mac->getCAPbyType(1);		/* Linux floating number compatibility		if (CURRENT_TIME + wtime + t_transacTime > t_fCAP)		*/		double tmpf;		tmpf = CURRENT_TIME + wtime;		tmpf += t_transacTime;		if (tmpf > t_fCAP)			ok = false;		if (ok)		{			t_fCAP = mac->getCAPbyType(2);			t_transacTime += max_pDelay;						//one-way trip propagation delay (802.15.4 ignores this, but it should be there even though it is very small)			t_transacTime += 12/phy->getRate('s');					//transceiver turn-around time (receiver may need to do this to transmit next beacon)			t_transacTime += t_IFS;							//IFS time -- not only ensure that the sender can finish the transaction, but also the receiver			/* Linux floating number compatibility			if (CURRENT_TIME + wtime + t_transacTime > t_fCAP)			*/			double tmpf;			tmpf = CURRENT_TIME + wtime;			tmpf += t_transacTime;			if (tmpf > t_fCAP)				ok = false;		}		if (ok)		{			t_fCAP = mac->getCAPbyType(3);			t_transacTime -= t_IFS;							//the third node does not need to handle the transaction			/* Linux floating number compatibility			if (CURRENT_TIME + wtime + t_transacTime > t_fCAP)			*/			double tmpf;			tmpf = CURRENT_TIME + wtime;			tmpf += t_transacTime;			if (tmpf > t_fCAP)				ok = false;		}	}	//check if have enough CAP to finish the transaction	if (!ok)	{		bPeriodsLeft = 0;		if ((mac->mpib.macBeaconOrder == 15)		&&  (mac->macBeaconOrder2 == 15)		&&  (mac->macBeaconOrder3 != 15))		{			/* Linux floating number compatibility			bcnOtherTime = (mac->macBcnOtherRxTime + mac->sfSpec3.BI) / phy->getRate('s');			*/			{			double tmpf;			tmpf = (mac->macBcnOtherRxTime + mac->sfSpec3.BI);			bcnOtherTime = tmpf / phy->getRate('s');			}			while (bcnOtherTime < CURRENT_TIME)				bcnOtherTime += (mac->sfSpec3.BI / phy->getRate('s'));			bcnOtherT->start(bcnOtherTime - CURRENT_TIME);		}#ifdef DEBUG802_15_4	fprintf(stdout,"[%s::%s][%f](node %d) cannot proceed: orders = %d/%d/%d, type = %s, src = %d, dst = %d, uid = %d, mac_uid = %ld, size = %d\n",__FILE__,__FUNCTION__,CURRENT_TIME,mac->index_,mac->mpib.macBeaconOrder,mac->macBeaconOrder2,mac->macBeaconOrder3,wpan_pName(txPkt),p802_15_4macSA(txPkt),p802_15_4macDA(txPkt),ch->uid(),HDR_LRWPAN(txPkt)->uid,ch->size());#endif		if (mac->macBeaconOrder2 != 15)		if (!mac->bcnRxT->busy())			mac->bcnRxT->start();		waitNextBeacon = true;		return false;	}	else	{		bPeriodsLeft = -1;		return true;	}}void CsmaCA802_15_4::newBeacon(char trx){	//this function will be called by MAC each time a new beacon received or sent within the current PAN	double rate,wtime;	if (!mac->txAck)		mac->plme_set_trx_state_request(p_RX_ON);		if (bcnOtherT->busy())		bcnOtherT->stop();	//update values	beaconEnabled = ((mac->mpib.macBeaconOrder != 15)||(mac->macBeaconOrder2 != 15));	beaconOther = (mac->macBeaconOrder3 != 15);	reset();		rate = phy->getRate('s');	bcnTxTime = mac->macBcnTxTime / rate;	bcnRxTime = mac->macBcnRxTime / rate;	bPeriod = aUnitBackoffPeriod / rate;	if (waitNextBeacon)	if ((txPkt)        && (!backoffT->busy()))	{		assert(bPeriodsLeft >= 0);		if (bPeriodsLeft == 0)		{			wtime = adjustTime(0.0);			if (canProceed(wtime));				backoffHandler();	//no need to resume backoff		}		else		{			wtime = adjustTime(0.0);			wtime += bPeriodsLeft * bPeriod;			if (canProceed(wtime));				backoffT->start(wtime);		}	}	waitNextBeacon = false;}void CsmaCA802_15_4::start(bool firsttime,Packet *pkt,bool ackreq){	bool backoff;	double rate,wtime,BI2;	if (mac->txAck)	{		mac->backoffStatus = 0;		txPkt = 0;		return;	}	assert(backoffT->busy() == 0);	if (firsttime)	{		beaconEnabled = ((mac->mpib.macBeaconOrder != 15)||(mac->macBeaconOrder2 != 15));		beaconOther = (mac->macBeaconOrder3 != 15);		reset();			assert(txPkt == 0);		txPkt = pkt;		ackReq = ackreq;		rate = phy->getRate('s');		bPeriod = aUnitBackoffPeriod / rate;		if (beaconEnabled)		{			bcnTxTime = mac->macBcnTxTime / rate;			bcnRxTime = mac->macBcnRxTime / rate;			//it's possible we missed some beacons			BI2 = (mac->sfSpec2.BI / phy->getRate('s'));			if (mac->macBeaconOrder2 != 15)			while (bcnRxTime + BI2 < CURRENT_TIME)				bcnRxTime += BI2;		}	}	wtime = (Random::random() % (1<<BE)) * bPeriod;	wtime = adjustTime(wtime);	backoff = true;	if (beaconEnabled||beaconOther)	{		if (beaconEnabled)		if (firsttime)			wtime = mac->locateBoundary(mac->toParent(txPkt),wtime);		if (!canProceed(wtime))					backoff = false;	}	if (backoff)		backoffT->start(wtime);}void CsmaCA802_15_4::cancel(void){	if (bcnOtherT->busy())		bcnOtherT->stop();	else if (backoffT->busy())		backoffT->stop();	else if (deferCCAT->busy())		deferCCAT->stop();	else		mac->taskP.taskStatus(TP_CCA_csmaca) = false;	txPkt = 0;}void CsmaCA802_15_4::backoffHandler(void){	mac->taskP.taskStatus(TP_RX_ON_csmaca) = true;	mac->plme_set_trx_state_request(p_RX_ON);}void CsmaCA802_15_4::RX_ON_confirm(PHYenum status){	double now,wtime;	if (status != p_RX_ON)	{		if (status == p_BUSY_TX)			mac->taskP.taskStatus(TP_RX_ON_csmaca) = true;		else			backoffHandler();		return;	}	//locate backoff boundary if needed	now = CURRENT_TIME;	if (beaconEnabled)		wtime = mac->locateBoundary(mac->toParent(txPkt),0.0);	else		wtime = 0.0;	if (wtime == 0.0)	{		mac->taskP.taskStatus(TP_CCA_csmaca) = true;		phy->PLME_CCA_request();	}	else		deferCCAT->start(wtime);}void CsmaCA802_15_4::bcnOtherHandler(void){	newBeacon('R');}void CsmaCA802_15_4::deferCCAHandler(void){	mac->taskP.taskStatus(TP_CCA_csmaca) = true;	phy->PLME_CCA_request();}void CsmaCA802_15_4::CCA_confirm(PHYenum status){	//This function should be called when mac receiving CCA_confirm.	bool idle;	idle = (status == p_IDLE)?1:0;		if (idle)	{		if ((!beaconEnabled)&&(!beaconOther))		{			txPkt = 0;			mac->csmacaCallBack(p_IDLE);		}		else		{			if (beaconEnabled)				CW--;			else				CW = 0;			if (CW == 0)			{				//timing condition may not still hold -- check again				if (canProceed(0.0, true))				{					txPkt = 0;					mac->csmacaCallBack(p_IDLE);				}				else	//postpone until next beacon sent or received				{					if (beaconEnabled) CW = 2;					bPeriodsLeft = 0;				}			}			else	//perform CCA again				backoffHandler();		}	}	else	//busy	{		if (beaconEnabled) CW = 2;		NB++;		if (NB > mac->mpib.macMaxCSMABackoffs)		{			txPkt = 0;			mac->csmacaCallBack(p_BUSY);		}		else	//backoff again		{			BE++;			if (BE > aMaxBE)				BE = aMaxBE;			start(false);		}	}}// End of file: p802_15_4csmaca.cc

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -