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

📄 mac-802_11.cc

📁 多信道多接口无线mesh网络MAC协议MMAC的ns2仿真源码
💻 CC
📖 第 1 页 / 共 5 页
字号:
		if((u_int32_t)ETHER_ADDR(mh->dh_da) != MAC_BROADCAST)			timeout = txtime(pktTx_)				+ DSSS_MaxPropagationDelay		// XXX				+ sifs_				+ txtime(ETHER_ACK_LEN, basicRate_)				+ DSSS_MaxPropagationDelay;		// XXX		else			timeout = txtime(pktTx_);		break;	default:		fprintf(stderr, "check_pktTx:Invalid MAC Control subtype\n");		exit(1);	}		if(dca_) {		if((u_int32_t)ETHER_ADDR(mh->dh_da) != MAC_BROADCAST) {			if(active_channel_ == DEFAULT_CHANNEL || active_channel_ < 0) {				printf("ERROR: Cannot send data on this channel!\n");				exit(1);			} else {				mh->dh_fc.fc_channel	= active_channel_;			}		} else {			mh->dh_fc.fc_channel = DEFAULT_CHANNEL;		}	} else {		if(active_channel_ != -1)			mh->dh_fc.fc_channel	= active_channel_;		else			mh->dh_fc.fc_channel	= DEFAULT_CHANNEL;	}	if(dca_) {		if((u_int32_t)ETHER_ADDR(mh->dh_da) != MAC_BROADCAST) {			//TRANSMIT(pktRSH_, 0.0);		}	}        TRANSMIT(pktTx_, timeout);	return 0;}//JUNGMINintMac802_11::check_pktPCF(){	struct hdr_mac802_11 *mh;	double timeout;	//assert(mhBackoff_.busy() == 0);	if(pktPCF_ == 0)		return -1;		mh = HDR_MAC802_11(pktPCF_);	int len = HDR_CMN(pktPCF_)->size();	switch(mh->dh_fc.fc_type) {	case MAC_Type_Management:		switch(mh->dh_fc.fc_subtype) {		case MAC_Subtype_Beacon:				if(!is_idle())				return -1;			struct beacon_frame *beacon = (struct beacon_frame *)pktPCF_->access(hdr_mac::offset_);			timeout = txtime(pktPCF_);			SET_TX_STATE(MAC_BEACONING);		}		break;	default:		fprintf(stderr, "check_pktPCF:Invalid MAC (sub)type\n");		exit(1);	}	TRANSMIT(pktPCF_, timeout);	//set a flag saying I sent a beacon	//It is also set when I receive the beacon	//This is to start sending ATIMs	if(ready_for_atim_ == 0) {		ready_for_atim_ = 1;		ll_->ifq()->StartSendingATIM();	}	return 0;}//end of JUNGMIN/* * Low-level transmit functions that actually place the packet onto * the channel. */voidMac802_11::sendRTS(int dst){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct rts_frame *rf = (struct rts_frame*)p->access(hdr_mac::offset_);	//just for checking	struct hdr_mac802_11* dh = HDR_MAC802_11(p);		// sendDATA should be called before sendRTS	// this is to make sure that RTS be sent only when DATA is ready to be sent.	assert(pktTx_);	assert(pktRTS_ == 0);	/*	 *  If the size of the packet is larger than the	 *  RTSThreshold, then perform the RTS/CTS exchange.	 *	 *  XXX: also skip if destination is a broadcast	 */	if( (u_int32_t) HDR_CMN(pktTx_)->size() < macmib_->RTSThreshold ||	    (u_int32_t) dst == MAC_BROADCAST) {		Packet::free(p);		//p = 0;		return;	}	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_RTS_LEN;	ch->iface() = -2;	ch->error() = 0;	bzero(rf, MAC_HDR_LEN);	rf->rf_fc.fc_protocol_version = MAC_ProtocolVersion; 	rf->rf_fc.fc_type	= MAC_Type_Control; 	rf->rf_fc.fc_subtype	= MAC_Subtype_RTS; 	rf->rf_fc.fc_to_ds	= 0; 	rf->rf_fc.fc_from_ds	= 0; 	rf->rf_fc.fc_more_frag	= 0; 	rf->rf_fc.fc_retry	= 0; 	rf->rf_fc.fc_pwr_mgt	= 0; 	rf->rf_fc.fc_more_data	= 0; 	rf->rf_fc.fc_wep	= 0; 	rf->rf_fc.fc_order	= 0;	if(active_channel_ != -1) {		rf->rf_fc.fc_channel	= active_channel_;	}	else		rf->rf_fc.fc_channel	= DEFAULT_CHANNEL;	//rf->rf_duration = RTS_DURATION(pktTx_);	STORE4BYTE(&dst, (rf->rf_ra));		/* store rts tx time */ 	ch->txtime() = txtime(ch->size(), basicRate_);		STORE4BYTE(&index_, (rf->rf_ta));	/* calculate rts duration field */	rf->rf_duration = usec(sifs_			       + txtime(ETHER_CTS_LEN, basicRate_)			       + sifs_			       + txtime(pktTx_)			       + sifs_			       + txtime(ETHER_ACK_LEN, basicRate_));		pktRTS_ = p;}voidMac802_11::sendDRTS(int dst){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct dca_rts_frame *drts = (struct dca_rts_frame*)p->access(hdr_mac::offset_);	assert(pktTx_);	assert(pktRTS_);	if( (u_int32_t) HDR_CMN(pktTx_)->size() < macmib_->RTSThreshold ||            (u_int32_t) dst == MAC_BROADCAST) {		Packet::free(p);		return;	}	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_DRTS_LEN;	ch->iface() = -2;	ch->error() = 0;	bzero(drts, MAC_HDR_LEN);	drts->drts_fc.fc_protocol_version = MAC_ProtocolVersion;	drts->drts_fc.fc_type 		= MAC_Type_Control;	drts->drts_fc.fc_subtype	= MAC_Subtype_DRTS;	drts->drts_fc.fc_to_ds		= 0;	drts->drts_fc.fc_from_ds	= 0;	drts->drts_fc.fc_more_frag	= 0;	drts->drts_fc.fc_retry		= 0;	drts->drts_fc.fc_pwr_mgt	= 0;	drts->drts_fc.fc_more_data	= 0;	drts->drts_fc.fc_wep		= 0;	drts->drts_fc.fc_order		= 0;	drts->drts_fc.fc_channel	= DEFAULT_CHANNEL;			STORE4BYTE(&dst, (drts->drts_da));	ch->txtime() = txtime(ch->size(), basicRate_);	STORE4BYTE(&index_, (drts->drts_sa));	drts->drts_duration = usec(sifs_				+ txtime(ETHER_DCTS_LEN, basicRate_)				+ sifs_				+ txtime(ETHER_DRSH_LEN, basicRate_));	drts->drts_data_transfer_time = txtime(pktTx_);	pktRTS_ = p;}//JUNGMINvoidMac802_11::InsertDestState(int dst){	if(dst < 0 || dst > MAX_NODES) return;	dest_state_[dst] = 1;	return;}voidMac802_11::InsertNeighborChannelState(int dst, int channel){	if(dst < 0 || dst > MAX_NODES) return;	channel_state_[dst] = channel;	return;}	intMac802_11::LookupDestState(int dst){	if(dst < 0) return 0;	return dest_state_[dst];}intMac802_11::NearBeacon(Packet* p, int afterbackoff){	//if it is near beacon, return 1	if(pc_ == 0) return 0;	//if afterbackoff is 1, then exclude the first difs	hdr_cmn* ch = HDR_CMN(p);	int size = ch->size() + ETHER_HDR_LEN11;	double left_time = next_beacon_time_ - NOW;	double estimated_time = txtime(ETHER_RTS_LEN, basicRate_)				+ sifs_				+ txtime(ETHER_CTS_LEN, basicRate_)				+ sifs_				+ txtime(size, basicRate_)				+ sifs_				+ txtime(ETHER_ACK_LEN, basicRate_);	if(afterbackoff == 0) estimated_time += difs_;	if(estimated_time > left_time) {		return 1;	}	else return 0;}void Mac802_11::sendATIM(int dst, Handler* h){	//see if it has previously sent an ATIM to that particular destination	int prev = LookupDestState(dst);	if(prev == 1) return;	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct atim_frame *atf = (struct atim_frame*)p->access(hdr_mac::offset_);	assert(pktATIM_ == 0);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_ATIM_LEN;	ch->iface() = -2;	ch->error() = 0;	bzero(atf, MAC_HDR_LEN);	atf->atf_fc.fc_protocol_version = MAC_ProtocolVersion;	atf->atf_fc.fc_type		= MAC_Type_Management;	atf->atf_fc.fc_subtype		= MAC_Subtype_ATIM;	atf->atf_fc.fc_to_ds		= 0;	atf->atf_fc.fc_from_ds		= 0;	atf->atf_fc.fc_more_frag	= 0;	atf->atf_fc.fc_retry		= 0;	atf->atf_fc.fc_pwr_mgt		= 0;	atf->atf_fc.fc_more_data	= 0;	atf->atf_fc.fc_wep		= 0;	atf->atf_fc.fc_order		= 0;	atf->atf_fc.fc_channel	= DEFAULT_CHANNEL;	STORE4BYTE(&dst, (atf->atf_da));	ch->txtime() = txtime(ch->size(), basicRate_);		STORE4BYTE(&index_, (atf->atf_sa));	atf->atf_duration = usec(sifs_				+ txtime(ETHER_ATIMACK_LEN, basicRate_)				+ sifs_				+ txtime(ETHER_ATIMRSH_LEN, basicRate_));	//set callback to get back to the queue	callback_ = h;	pktATIM_ = p;	EnergyModel *em = netif_->node()->energy_model();	if(em && em->sleep()) {		em->set_node_sleep(0);		em->set_node_state(EnergyModel::INROUTE);	}	if(mhBackoff_.busy() == 0) {		if(is_idle()) {			if(mhDefer_.busy() == 0)				mhDefer_.start(difs_);		}			else {			mhBackoff_.start(cw_, is_idle());		}	}}//end of JUNGMINvoidMac802_11::sendCTS(int dst, double rts_duration){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct cts_frame *cf = (struct cts_frame*)p->access(hdr_mac::offset_);	assert(pktCTRL_ == 0);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_CTS_LEN;	ch->iface() = -2;	ch->error() = 0;	//ch->direction() = hdr_cmn::DOWN;	bzero(cf, MAC_HDR_LEN);	cf->cf_fc.fc_protocol_version = MAC_ProtocolVersion;	cf->cf_fc.fc_type	= MAC_Type_Control;	cf->cf_fc.fc_subtype	= MAC_Subtype_CTS; 	cf->cf_fc.fc_to_ds	= 0; 	cf->cf_fc.fc_from_ds	= 0; 	cf->cf_fc.fc_more_frag	= 0; 	cf->cf_fc.fc_retry	= 0; 	cf->cf_fc.fc_pwr_mgt	= 0; 	cf->cf_fc.fc_more_data	= 0; 	cf->cf_fc.fc_wep	= 0; 	cf->cf_fc.fc_order	= 0;		if(active_channel_ != -1)		cf->cf_fc.fc_channel	= active_channel_;	else		cf->cf_fc.fc_channel	= DEFAULT_CHANNEL;	//cf->cf_duration = CTS_DURATION(rts_duration);	STORE4BYTE(&dst, (cf->cf_ra));		/* store cts tx time */	ch->txtime() = txtime(ch->size(), basicRate_);		/* calculate cts duration */	cf->cf_duration = usec(sec(rts_duration)			       - sifs_			       - txtime(ETHER_CTS_LEN, basicRate_));		pktCTRL_ = p;	}//JUNGMINvoidMac802_11::sendDRSH(int dst, double dcts_usage_time){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct dca_rsh_frame *drsh = (struct dca_rsh_frame*)p->access(hdr_mac::offset_);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_DRSH_LEN;	ch->iface() = -2;	ch->error() = 0;	bzero(drsh, MAC_HDR_LEN);	drsh->drsh_fc.fc_protocol_version = MAC_ProtocolVersion; 	drsh->drsh_fc.fc_type		= MAC_Type_Management; 	drsh->drsh_fc.fc_subtype	= MAC_Subtype_DRSH; 	drsh->drsh_fc.fc_to_ds		= 0; 	drsh->drsh_fc.fc_from_ds	= 0; 	drsh->drsh_fc.fc_more_frag	= 0; 	drsh->drsh_fc.fc_retry		= 0; 	drsh->drsh_fc.fc_pwr_mgt	= 0; 	drsh->drsh_fc.fc_more_data	= 0; 	drsh->drsh_fc.fc_wep		= 0;	drsh->drsh_fc.fc_order		= 0;	drsh->drsh_fc.fc_channel	= DEFAULT_CHANNEL;	STORE4BYTE(&dst, (drsh->drsh_ra));	ch->txtime() = txtime(ch->size(), basicRate_);	drsh->drsh_duration = 0;	drsh->drsh_confirmed_channel = active_channel_;	drsh->drsh_usage_time = dcts_usage_time				- sifs_				- txtime(ETHER_DRSH_LEN, basicRate_);	//update dca_cul_	DCAUpdateCUL(active_channel_, dcts_usage_time);	pktRSH_ = p;}voidMac802_11::sendDCTS(int dst, double data_transfer_time){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct dca_cts_frame *dcts = (struct dca_cts_frame*)p->access(hdr_mac::offset_);	assert(pktCTRL_ == 0);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_DCTS_LEN;	ch->iface() = -2;	ch->error() = 0;	//ch->direction() = hdr_cmn::DOWN;	bzero(dcts, MAC_HDR_LEN);	dcts->dcts_fc.fc_protocol_version = MAC_ProtocolVersion;	dcts->dcts_fc.fc_type	= MAC_Type_Control;	dcts->dcts_fc.fc_subtype	= MAC_Subtype_DCTS; 	dcts->dcts_fc.fc_to_ds	= 0; 	dcts->dcts_fc.fc_from_ds	= 0; 	dcts->dcts_fc.fc_more_frag	= 0; 	dcts->dcts_fc.fc_retry	= 0; 	dcts->dcts_fc.fc_pwr_mgt	= 0; 	dcts->dcts_fc.fc_more_data	= 0; 	dcts->dcts_fc.fc_wep	= 0; 	dcts->dcts_fc.fc_order	= 0;		dcts->dcts_fc.fc_channel	= DEFAULT_CHANNEL;	//cf->cf_duration = CTS_DURATION(rts_duration);	STORE4BYTE(&dst, (dcts->dcts_ra));		/* store cts tx time */	ch->txtime() = txtime(ch->size(), basicRate_);		/* calculate cts duration */	dcts->dcts_duration = usec(sifs_ 			      	+ txtime(ETHER_DRSH_LEN, basicRate_));	dcts->dcts_usage_time = sifs_				+ data_transfer_time				+ sifs_				+ txtime(ETHER_ACK_LEN, basicRate_);		active_channel_ = DCASelectChannel(dca_recved_fcl_);	dcts->dcts_selected_channel = active_channel_;	if(dcts->dcts_selected_channel != -1) {		dcts->dcts_wait_time = 0.0;		//update dca_cul_		DCAUpdateCUL(active_channel_, dcts->dcts_usage_time + txtime(ETHER_DCTS_LEN, basicRate_) + sifs_);	} else {		dcts->dcts_wait_time = FindNearestChannelFreeTime();	}	pktCTRL_ = p;}//end of JUNGMINvoidMac802_11::sendACK(int dst){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct ack_frame *af = (struct ack_frame*)p->access(hdr_mac::offset_);	assert(pktCTRL_ == 0);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_ACK_LEN;	ch->iface() = -2;	ch->error() = 0;		bzero(af, MAC_HDR_LEN);	af->af_fc.fc_protocol_version = MAC_ProtocolVersion; 	af->af_fc.fc_type	= MAC_Type_Control; 	af->af_fc.fc_subtype	= MAC_Subtype_ACK; 	af->af_fc.fc_to_ds	= 0; 	af->af_fc.fc_from_ds	= 0; 	af->af_fc.fc_more_frag	= 0; 	af->af_fc.fc_retry	= 0; 	af->af_fc.fc_pwr_mgt	= 0; 	af->af_fc.fc_more_data	= 0; 	af->af_fc.fc_wep	= 0; 	af->af_fc.fc_order	= 0;	if(dca_ == 0) {		if(active_channel_ != -1)			af->af_fc.fc_channel	= active_channel_;		else			af->af_fc.fc_channel	= DEFAULT_CHANNEL;	} else {		if(active_channel_ == -1 || active_channel_ == DEFAULT_CHANNEL) {			printf("ERROR: cannot send packet on this channel!\n");			exit(1);		}		else {			af->af_fc.fc_channel	= active_channel_;		}	}	//af->af_duration = ACK_DURATION();	STORE4BYTE(&dst, (af->af_ra));	/* store ack tx time */ 	ch->txtime() = txtime(ch->size(), basicRate_);		/* calculate ack duration */ 	af->af_duration = 0;			pktCTRL_ = p;}//JUNGMINvoidMac802_11::sendATIMACK(int dst){	Packet *p = Packet::alloc();	hdr_cmn* ch = HDR_CMN(p);	struct atimack_frame *aaf = (struct atimack_frame*)p->access(hdr_mac::offset_);	assert(pktATIMACK_ == 0);	ch->uid() = 0;	ch->ptype() = PT_MAC;	ch->size() = ETHER_ATIMACK_LEN;	ch->iface() = -2;	ch->error() = 0;	bzero(aaf, MAC_HDR_LEN);	aaf->aaf_fc.fc_protocol_version = MAC_ProtocolVersion; 	aaf->aaf_fc.fc_type		= MAC_Type_Management; 	aaf->aaf_fc.fc_subtype		= MAC_Subtype_ATIMACK; 	aaf->aaf_fc.fc_to_ds		= 0; 	aaf->aaf_fc.fc_from_ds		= 0; 	aaf->aaf_fc.fc_more_frag	= 0; 	aaf->aaf_fc.fc_retry		= 0; 	aaf->aaf_fc.fc_pwr_mgt		= 0; 	aaf->aaf_fc.fc_more_data	= 0; 	aaf->aaf_fc.fc_wep		= 0;	aaf->aaf_fc.fc_order		= 0;	aaf->aaf_fc.fc_channel	= DEFAULT_CHANNEL;	STORE4BYTE(&dst, (aaf->aaf_ra));	STORE4BYTE(&index_, (aaf->aaf_sa));	ch->txtime() = txtime(ch->size(), basicRate_);

⌨️ 快捷键说明

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