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

📄 mac-tdma.cc

📁 在网络仿真模拟工具下实现支持时隙的MAC层TDMA协议
💻 CC
📖 第 1 页 / 共 2 页
字号:
// -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-//// Copyright (c) 1999 by the University of Southern California// All rights reserved.//// Permission to use, copy, modify, and distribute this software and its// documentation in source and binary forms for non-commercial purposes// and without fee is hereby granted, provided that the above copyright// notice appear in all copies and that both the copyright notice and// this permission notice appear in supporting documentation. and that// any documentation, advertising materials, and other materials related// to such distribution and use acknowledge that the software was// developed by the University of Southern California, Information// Sciences Institute.  The name of the University may not be used to// endorse or promote products derived from this software without// specific prior written permission.//// THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about// the suitability of this software for any purpose.  THIS SOFTWARE IS// PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,// INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.//// Other copyrights might apply to parts of this software and are so// noted when applicable.//// $Header: /nfs/jade/vint/CVSROOT/ns-2/mac/mac-tdma.cc,v 1.12 2004/02/06 16:33:52 xuanc Exp $//// mac-tdma.cc// by Xuan Chen (xuanc@isi.edu), ISI/USC//// Preamble TDMA MAC layer for single hop.// Centralized slot assignment computing.#include "delay.h"#include "connector.h"#include "packet.h"#include "random.h"// #define DEBUG//#include <debug.h>#include "arp.h"#include "ll.h"#include "mac.h"#include "mac-tdma.h"#include "cmu-trace.h"#define SET_RX_STATE(x)			\{					\	rx_state_ = (x);			\}#define SET_TX_STATE(x)				\{						\	tx_state_ = (x);				\}/* Phy specs from 802.11 */static PHY_MIB PMIB = {	DSSS_CWMin, DSSS_CWMax, DSSS_SlotTime, DSSS_CCATime,	DSSS_RxTxTurnaroundTime, DSSS_SIFSTime, DSSS_PreambleLength,	DSSS_PLCPHeaderLength};	/* Timers */void MacTdmaTimer::start(Packet *p, double time){	Scheduler &s = Scheduler::instance();	assert(busy_ == 0);  	busy_ = 1;	paused_ = 0;	stime = s.clock();	rtime = time;	assert(rtime >= 0.0);  	s.schedule(this, p, rtime);}void MacTdmaTimer::stop(Packet *p) {	Scheduler &s = Scheduler::instance();	assert(busy_);  	if(paused_ == 0)		s.cancel((Event *)p);	// Should free the packet p.	Packet::free(p);  	busy_ = 0;	paused_ = 0;	stime = 0.0;	rtime = 0.0;}/* Slot timer for TDMA scheduling. */void SlotTdmaTimer::handle(Event *e){       	busy_ = 0;	paused_ = 0;	stime = 0.0;	rtime = 0.0;  	mac->slotHandler(e);}/* Receive Timer */void RxPktTdmaTimer::handle(Event *e) {       	busy_ = 0;	paused_ = 0;	stime = 0.0;	rtime = 0.0;  	mac->recvHandler(e);}/* Send Timer */void TxPktTdmaTimer::handle(Event *e) {       	busy_ = 0;	paused_ = 0;	stime = 0.0;	rtime = 0.0;  	mac->sendHandler(e);}/* ======================================================================   TCL Hooks for the simulator   ====================================================================== */static class MacTdmaClass : public TclClass {public:	MacTdmaClass() : TclClass("Mac/Tdma") {}	TclObject* create(int, const char*const*) {		return (new MacTdma(&PMIB));	}} class_mac_tdma;// Mac Tdma definitions// Frame format:// Pamble Slot1 Slot2 Slot3...MacTdma::MacTdma(PHY_MIB* p) : 	Mac(), mhSlot_(this), mhTxPkt_(this), mhRxPkt_(this){	/* Global variables setting. */	// Setup the phy specs.	phymib_ = p;		/* Get the parameters of the link (which in bound in mac.cc, 2M by default),	   the packet length within one TDMA slot (1500 byte by default), 	   and the max number of nodes (64) in the simulations.*/	bind("slot_packet_len_", &slot_packet_len_);	bind("max_node_num_", &max_node_num_);		//  slot_packet_len_ = 1500;	//  max_node_num_ = 64;	// Calculate the slot time based on the MAX allowed data length.	slot_time_ = DATA_Time(slot_packet_len_);		/* Calsulate the max slot num within on frame from max node num.	   In the simple case now, they are just equal. 	*/	max_slot_num_ = max_node_num_;		/* Much simplified centralized scheduling algorithm for single hop	   topology, like WLAN etc. 	*/	// Initualize the tdma schedule and preamble data structure.	tdma_schedule_ = new int[max_slot_num_];	tdma_preamble_ = new int[max_slot_num_];	/* Do each node's initialization. */	// Record the initial active node number.	active_node_++;	if (active_node_ > max_node_num_) {		printf("Too many nodes taking part in the simulations, aborting...\n");		exit(-1);	}    	// Initial channel / transceiver states. 	tx_state_ = rx_state_ = MAC_IDLE;	tx_active_ = 0;	// Initialy, the radio is off. NOTE: can't use radioSwitch(OFF) here.	radio_active_ = 0;	// Do slot scheduling.	re_schedule();	/* Deal with preamble. */	// Can't send anything in the first frame.	slot_count_ = FIRST_ROUND;	tdma_preamble_[slot_num_] = NOTHING_TO_SEND;	//Start the Slot timer..	mhSlot_.start((Packet *) (& intr_), 0);  }/* similar to 802.11, no cached node lookup. */int MacTdma::command(int argc, const char*const* argv){	if (argc == 3) {		if (strcmp(argv[1], "log-target") == 0) {			logtarget_ = (NsObject*) TclObject::lookup(argv[2]);			if(logtarget_ == 0)				return TCL_ERROR;			return TCL_OK;		}	}	return Mac::command(argc, argv);}/* ======================================================================   Debugging Routines   ====================================================================== */void MacTdma::trace_pkt(Packet *p) {	struct hdr_cmn *ch = HDR_CMN(p);	struct hdr_mac_tdma* dh = HDR_MAC_TDMA(p);	u_int16_t *t = (u_int16_t*) &dh->dh_fc;	fprintf(stderr, "\t[ %2x %2x %2x %2x ] %x %s %d\n",		*t, dh->dh_duration,		ETHER_ADDR(dh->dh_da), ETHER_ADDR(dh->dh_sa),		index_, packet_info.name(ch->ptype()), ch->size());}void MacTdma::dump(char *fname){	fprintf(stderr, "\n%s --- (INDEX: %d, time: %2.9f)\n", fname, 		index_, Scheduler::instance().clock());		fprintf(stderr, "\ttx_state_: %x, rx_state_: %x, idle: %d\n", 		tx_state_, rx_state_, is_idle());	fprintf(stderr, "\tpktTx_: %x, pktRx_: %x, callback: %x\n", 		(int) pktTx_, (int) pktRx_, (int) callback_);}/* ======================================================================   Packet Headers Routines   ====================================================================== */int MacTdma::hdr_dst(char* hdr, int dst ){	struct hdr_mac_tdma *dh = (struct hdr_mac_tdma*) hdr;	if(dst > -2)		STORE4BYTE(&dst, (dh->dh_da));	return ETHER_ADDR(dh->dh_da);}int MacTdma::hdr_src(char* hdr, int src ){	struct hdr_mac_tdma *dh = (struct hdr_mac_tdma*) hdr;	if(src > -2)		STORE4BYTE(&src, (dh->dh_sa));  	return ETHER_ADDR(dh->dh_sa);}int MacTdma::hdr_type(char* hdr, u_int16_t type) {	struct hdr_mac_tdma *dh = (struct hdr_mac_tdma*) hdr;	if(type)		STORE2BYTE(&type,(dh->dh_body));	return GET2BYTE(dh->dh_body);}/* Test if the channel is idle. */int MacTdma::is_idle() {	if(rx_state_ != MAC_IDLE)		return 0;	if(tx_state_ != MAC_IDLE)		return 0;	return 1;}/* Do the slot re-scheduling:   The idea of postphone the slot scheduling for one slot time may be useful.*/void MacTdma::re_schedule() {	static int slot_pointer = 0;	// Record the start time of the new schedule.	start_time_ = NOW;	/* Seperate slot_num_ and the node id: 	   we may have flexibility as node number changes.	*/	slot_num_ = slot_pointer++;	tdma_schedule_[slot_num_] = (char) index_;}/* To handle incoming packet. */

⌨️ 快捷键说明

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