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

📄 mac-802_16.cc

📁 WIMAX code for NS-2, v1.05
💻 CC
📖 第 1 页 / 共 3 页
字号:
/**************************************************************************************
* *Copyright (c) 2006 Regents of the University of Chang Gung 						*
* *All rights reserved.													*
 *																*
 * Redistribution and use in source and binary forms, with or without						*
 * modification, are permitted provided that the following conditions					*
 * are met: 															*
 * 1. Redistributions of source code must retain the above copyright						*
 *    notice, this list of conditions and the following disclaimer.						*
 * 2. Redistributions in binary form must reproduce the above copyright					*
 *    notice, this list of conditions and the following disclaimer in the						*
 *    documentation and/or other materials provided with the distribution.					*
 * 3. All advertising materials mentioning features or use of this software					*
 *    must display the following acknowledgement:									*
 *	This product includes software developed by the Computer Systems					*
 *	Engineering Group at Lawrence Berkeley Laboratory.							*
 * 4. Neither the name of the University nor of the Laboratory may be used					*
 *    to endorse or promote products derived from this software without					*
 *    specific prior written permission.										*
 *5. If you have any problem about these codes, 									*
       please mail to antibanish@gmail.com or b9229008@stmail.cgu.edu.tw                    			*
**************************************************************************************/
#include "delay.h"
#include "connector.h"
#include "packet.h"
#include "random.h"
#include "mobilenode.h"

#include "arp.h" 
#include "ll.h"
#include "mac.h"
#include "mac-802_16.h"
#include "timer-802_16.h"
#include "packet-802_16.h"
#include "cmu-trace.h"
   
#include "agent.h"
#include "basetrace.h"

#define PDUSIZE 500;
 
double BS_UL_BW=0,BS_DL_BW=0,Tmp_UL_BW=0,Tmp_DL_BW=0,CAC_UL=0;
int UL_burst_profile=0,DL_burst_profile=0;
BCID_List* BCID_head=NULL;
BCID_List* BCID_tail=NULL;
Lock* check_list=new Lock;
SS_Queue* ss_buf=new SS_Queue;
Queue_List* Queue_list=new Queue_List;
MacStatus State_=MIDLE;
int BSID=0;
int BCID_count=0;
int SFID_count1=0;
int SFID_count2=0;
int SFID_count3=0;
int SFID_count4=0;
int SFID_count5=0;
int SFID_count6=0;
int SFID_count7=0;
int SFID_count8=0;
int SFID_count9=0;
int SFID_count10=0;
int ULMAP_SS[11],DLMAP_SS[13];
u_int8_t channelNO_=10,dataSize_;
long double dlmapDuration_=0.0025,ulmapDuration_=0.0025,frameDuration_=0.005,symboleRate_;
long double symbolDuration_;
UL_MAP_IE ULMAP_IE[11];
DL_MAP_IE DLMAP_IE[13];

double allocation_start_time;

Mac802_16::Mac802_16() :Mac(),PreambleTimer(this),ulmaptimer(this),dlmaptimer(this),mhSend_(this),mhRecv_(this),mhDefer_(this),BFt(this),resumeSSBurst(this),resumeBSBurst(this),SendUL(this),SendDL(this),SendBW(this){
	creatid=index_;
	pktBr_=0;
	pktRNGREQ_=0;
	pktRNGRSP_=0;
	pktULMAP_=0;
	pktDLMAP_=0;
	pktTx_=0;
	pktBurst_=0;
	pktBSBurst_=0;	
	tx_state_ = rx_state_ = MAC_IDLE;
	state_ = MAC_IDLE;
	rx_state_ = MAC_IDLE;
	cw_=cw_bw=CW_MIN;
	
	Tcl& tcl = Tcl::instance();
	tcl.evalf("Mac/802_16 set channelSize_");
	if (strcmp(tcl.result(), "0") != 0) 
		bind_bw("channelSize_", &channelSize_);
	else
		channelSize_ = 20;	
		
	tcl.evalf("Mac/802_16 set modulation_");
	if (strcmp(tcl.result(), "0") != 0) 
		bind_bw("modulation_", &modulation_);
	else
		modulation_ = 0;
	
	tcl.evalf("Mac/802_16 set codeRate_");
	if (strcmp(tcl.result(), "0") != 0) 
		bind_bw("codeRate_", &codeRate_);
	else
		codeRate_ = 0;	

	if(creatid==0){
		if(channelSize_==20){
			if(modulation_==0 && codeRate_==0){		//20MHz QPSK 1/2
				symboleRate_=16.0;
				dataSize_=24; 
				symbolDuration_=1.0/(symboleRate_*1000000.0);
				frameDuration_ += 4.0*symbolDuration_;
				BS_DL_BW = dlmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("Available Downlink Bandwidth of BS %f\n",BS_DL_BW);
				BS_UL_BW = ulmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("Available Uplink Bandwidth of BS %f\n",BS_UL_BW);
			}
		}
		/* future work, coming soon!! */
		/*else if(channelSize_==25){
			if(modulation_==0 && codeRate_==0){		//25MHz QPSK 1/2
				symboleRate_=20.0;
				dataSize_=24.0;
				BS_DL_BW = dlmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("BS_DL_BW %f\n",BS_DL_BW);
				BS_UL_BW = ulmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("BS_UL_BW %f\n",BS_UL_BW);
			}
		}
		else if(channelSize_==28){
			if(modulation_==0 && codeRate_==0){		//28MHz QPSK 1/2
				symboleRate_=22.4;
				dataSize_=6.0;
				BS_DL_BW = dlmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("BS_DL_BW %f\n",BS_DL_BW);
				BS_UL_BW = ulmapDuration_*symboleRate_*dataSize_*channelNO_*1000000.0;
				printf("BS_UL_BW %f\n",BS_UL_BW);
			}
		}*/
		Tmp_UL_BW = CAC_UL = BS_UL_BW;	
		PreambleTimer.start(0);
	}
}  

void Mac802_16::recv(Packet* p, Handler* h)
{
	struct hdr_cmn *hdr = HDR_CMN(p);
	struct hdr_mac *mac_ = HDR_MAC(p);
	struct hdr_mac802_16 *mac16_ = HDR_MAC802_16(p);

	if(creatid!=0)
	{
		if(check_list->check(creatid)==2)
			goto Jump_;
		else if(check_list->check(creatid)==0)
		{		
			check_list->enque(creatid);
			check_list->unlock(creatid);		   		
			ss_buf->enque(creatid);
			Queue_list->enque(creatid);	
			sendRNGREQ(creatid);				
		}
	}	
Jump_:

	if(hdr->direction() == hdr_cmn::UP)
	{
		if(creatid==BSID)
		{
			if(mac16_->HT==0)
			{
				if(hdr->ptype()==PT_UGS||hdr->ptype()==PT_rtPS
					||hdr->ptype()==PT_nrtPS||hdr->ptype()==PT_ertPS||hdr->ptype()==PT_BE)
				{
					if(mac_->macDA_==BSID)
					{
						printf("BS receive SS %d packet at %f\n",mac_->macSA_,Scheduler::instance().clock());	
						uptarget_->recv(p, (Handler*) 0); 	
						return;
					}
				}				
				/*switch(mac16_->mmt)
				{
					case 2:	
						Packet::free(p);
						return;
						break;
					case 3: 
						Packet::free(p);
						return;
						break;
					case 4:	
						struct rng_req_t *rngreq = HDR_MAC802_16_RNGREQ(p);									
						sendRNGRSP(rngreq->ssMACAddr, Insert_BCID(rngreq->ssMACAddr));
						state_=MAC_IDLE;		
						mac_log(p);
						return;
						break;			
					case 5:
						Packet::free(p);
						return;
						break;
					default: 
						break;
				}*/
				if(mac16_->mmt==2)
				{	
					Packet::free(p);
					return;
				}
				else if(mac16_->mmt==3) 
				{
					Packet::free(p);
					return;
				}
				else if(mac16_->mmt==4)	
				{
					struct rng_req_t *rngreq = HDR_MAC802_16_RNGREQ(p);									
					sendRNGRSP(rngreq->ssMACAddr, Insert_BCID(rngreq->ssMACAddr));
					state_=MAC_IDLE;		
					mac_log(p);
					return;		
				}
				else if(mac16_->mmt==5)
				{
					Packet::free(p);
					return;
				}
			}
			else if(mac16_->HT==1)
			{
				struct bw_req_t* bwreq = HDR_MAC802_16_BWREQ(p);
				int SFID_tmp;
				switch(bwreq->Type)
				{
					case 0x0:	
						if(Call_Admission_Control(bwreq->CID,PT_UGS,bwreq->BR)==true)
						{
							SFID_tmp=Insert_SFID(bwreq->CID,PT_UGS,UL);
							Search_SFID(SFID_tmp)->BW=bwreq->BR;
							//Search_SFID(SFID_tmp)->destIP=p->destIP;
							mac_log(p);
							return;
						}
						else
						{
							printf("BR fail\n");
							return;
						}
						break;
					case 0x1:	
						if(Call_Admission_Control(bwreq->CID,PT_ertPS,bwreq->BR)==true)
						{
							SFID_tmp=Insert_SFID(bwreq->CID,PT_ertPS,UL);
							Search_SFID(SFID_tmp)->BW=bwreq->BR;
							//Search_SFID(SFID_tmp)->destIP=p->destIP;									
							mac_log(p);
							return;
						}
						else
						{
							printf("BR fail\n");
							return;
						}
						break;
					case 0x2:	
						if(Call_Admission_Control(bwreq->CID,PT_rtPS,bwreq->BR)==true)
						{
							SFID_tmp=Insert_SFID(bwreq->CID,PT_rtPS,UL);
							Search_SFID(SFID_tmp)->BW=bwreq->BR;
							//Search_SFID(SFID_tmp)->destIP=p->destIP;									
							mac_log(p);
							return;
						}
						else
						{
							printf("BR fail\n");
							return;
						}
						break;
					case 0x3:	
						if(Call_Admission_Control(bwreq->CID,PT_nrtPS,bwreq->BR)==true)
						{
							SFID_tmp=Insert_SFID(bwreq->CID,PT_nrtPS,UL);
							Search_SFID(SFID_tmp)->BW=bwreq->BR;
							//Search_SFID(SFID_tmp)->destIP=p->destIP;								
							mac_log(p);
							return;
						}
						else
						{
							printf("BR fail\n");
							return;
						}
						break;
					case 0x4:	
						if(Call_Admission_Control(bwreq->CID,PT_BE,bwreq->BR)==true)
						{
							SFID_tmp=Insert_SFID(bwreq->CID,PT_BE,UL);
							Search_SFID(SFID_tmp)->BW=bwreq->BR;
							//Search_SFID(SFID_tmp)->destIP=p->destIP;									
							mac_log(p);
							return;
						}
						else
						{
							printf("BR fail\n");
							return;
						}
						break;
					default:   
						printf("Bandwidth request type doesn't specify\n"); 
						return;	
						break;		
				}
			}	
		}
		else
		{
			if(mac16_->HT==1)
			{
				Packet::free(p);	
				return;
			}
			else if(mac16_->HT==0)
			{
				if(hdr->ptype()==PT_UGS||hdr->ptype()==PT_rtPS
					||hdr->ptype()==PT_nrtPS||hdr->ptype()==PT_ertPS||hdr->ptype()==PT_BE)
				{
					if(mac_->macSA_==BSID && Search_BCID(mac16_->CID)->srcIP==creatid){
						printf("SS %d Receive BS Packet at %f\n",creatid,Scheduler::instance().clock());
						uptarget_->recv(p, (Handler*) 0);					
						return;
					}
					else
					{ 
						Packet::free(p);
						return;
					}
				}
				/*switch(mac16_->mmt)
				{		
					case 2:	
						mac_log(p);
						return;
						break;
					case 3:	
						struct UL_MAP* ulmap=HDR_MAC802_16_UL_MAP(p);		
						if(mac_->macSA_==BSID)
						{
							for(int i=0;i<UL_burst_profile;i++)
							{
								if(ULMAP_IE[i].uiuc!=0xc && Search_BCID(ULMAP_IE[i].cid)->srcIP==creatid)
								{
									if(allocation_start_time-Scheduler::instance().clock()>=0)	
									{
										ulmaptimer.start(allocation_start_time-Scheduler::instance().clock()
											,ULMAP_IE[i].cid,ULMAP_IE[i].OFDMA_Symbol_number);
									}
									else
										printf("Uplink Time Fail\n");
								}
							}
							mac_log(p);								
						}
						return;
						break;
					case 4: 
						if(creatid==mac16_->CID)
							Packet::free(p);					
						return;
						break;
					case 5:	
						struct rng_rsp_t *rngrsp = HDR_MAC802_16_RNGRSP(p);						
						if(Search_BCID(mac16_->CID)->srcIP==creatid)
						{
							check_list->set_response(rngrsp->SSMAC);
							mac_log(p);
						}
						return;
						break;
					default:
						break;
				}*/		
				if(mac16_->mmt==2)	
				{
					mac_log(p);
					return;
				}
				else if(mac16_->mmt==3)	
				{
					//struct UL_MAP* ulmap=HDR_MAC802_16_UL_MAP(p);		
					if(mac_->macSA_==BSID)
					{
						for(int i=0;i<UL_burst_profile;i++)
						{
							if(ULMAP_IE[i].uiuc!=0xc && Search_BCID(ULMAP_IE[i].cid)->srcIP==creatid)
							{
								if(allocation_start_time-Scheduler::instance().clock()>=0)	
								{
									ulmaptimer.start(allocation_start_time-Scheduler::instance().clock()
										,ULMAP_IE[i].cid,ULMAP_IE[i].OFDMA_Symbol_number);
								}
								else
									printf("Uplink Time Fail\n");
							}
						}
						mac_log(p);								
					}
					return;
				}
				else if(mac16_->mmt==4)	
				{
					if(creatid==mac16_->CID)
						Packet::free(p);					
					return;
				}
				else if(mac16_->mmt==5)	
				{
					struct rng_rsp_t *rngrsp = HDR_MAC802_16_RNGRSP(p);						
					if(Search_BCID(mac16_->CID)->srcIP==creatid)
					{
						check_list->set_response(rngrsp->SSMAC);
						mac_log(p);
					}
					return;
				}
			}	
		}
		sendUp(p);  
		return;
	}
	else{
		callback_ = h;
		if(creatid==BSID)
		{
			if(hdr->ptype()==PT_UGS||hdr->ptype()==PT_rtPS
				||hdr->ptype()==PT_nrtPS||hdr->ptype()==PT_ertPS||hdr->ptype()==PT_BE)
			{
				Queue_list->lookup(mac_->macDA_)->BCID=Search_BCID_macSA(mac_->macDA_)->BCID;
				switch(hdr->ptype())
				{
					case PT_UGS: 	
						Queue_list->lookup(mac_->macDA_)->UGS.enque(p->copy());		
						if(Queue_list->lookup(mac_->macDA_)->UGS.Lock==0)
						{
							int SFID_tmp=Insert_SFID(Search_BCID_macSA(mac_->macDA_)->BCID,PT_UGS,DL);
							Search_SFID(SFID_tmp)->BW=Queue_list->lookup(mac_->macDA_)->UGS.byteLength()*10;
							Search_SFID(SFID_tmp)->destIP=mac_->macDA_;
							Queue_list->lookup(mac_->macDA_)->UGS.Lock=1;
						}
						break;
					case PT_rtPS: 	
						Queue_list->lookup(mac_->macDA_)->rtPS.enque(p->copy());
						if(Queue_list->lookup(mac_->macDA_)->rtPS.Lock==0)
						{
							int SFID_tmp=Insert_SFID(Search_BCID_macSA(mac_->macDA_)->BCID,PT_rtPS,DL);
							Search_SFID(SFID_tmp)->BW=Queue_list->lookup(mac_->macDA_)->rtPS.byteLength()*10;
							Search_SFID(SFID_tmp)->destIP=mac_->macDA_;
							Queue_list->lookup(mac_->macDA_)->rtPS.Lock=1;
						}
						break;
					case PT_nrtPS:	
						Queue_list->lookup(mac_->macDA_)->nrtPS.enque(p->copy());
						if(Queue_list->lookup(mac_->macDA_)->nrtPS.Lock==0)
						{
							int SFID_tmp=Insert_SFID(Search_BCID_macSA(mac_->macDA_)->BCID,PT_nrtPS,DL);
							Search_SFID(SFID_tmp)->BW=Queue_list->lookup(mac_->macDA_)->nrtPS.byteLength()*10;
							Search_SFID(SFID_tmp)->destIP=mac_->macDA_;
							Queue_list->lookup(mac_->macDA_)->nrtPS.Lock=1;
						}
						break;
					case PT_ertPS:	
						Queue_list->lookup(mac_->macDA_)->ertPS.enque(p->copy());
						if(Queue_list->lookup(mac_->macDA_)->ertPS.Lock==0)
						{
							int SFID_tmp=Insert_SFID(Search_BCID_macSA(mac_->macDA_)->BCID,PT_ertPS,DL);
							Search_SFID(SFID_tmp)->BW=Queue_list->lookup(mac_->macDA_)->ertPS.byteLength()*10;
							Search_SFID(SFID_tmp)->destIP=mac_->macDA_;
							Queue_list->lookup(mac_->macDA_)->ertPS.Lock=1;
						}
						break;
					case PT_BE:		

⌨️ 快捷键说明

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