?? mac-tdma.c
字號:
// -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-
/*
* mac-tdma.cc
* Copyright (C) 1999 by the University of Southern California
* $Id: mac-tdma.cc,v 1.16 2006/02/22 13:25:43 mahrenho Exp $
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
*
* The copyright of this module includes the following
* linking-with-specific-other-licenses addition:
*
* In addition, as a special exception, the copyright holders of
* this module give you permission to combine (via static or
* dynamic linking) this module with free software programs or
* libraries that are released under the GNU LGPL and with code
* included in the standard release of ns-2 under the Apache 2.0
* license or under otherwise-compatible licenses with advertising
* requirements (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for this module and the licenses of the
* other code concerned, provided that you include the source code of
* that other code when and as the GNU GPL requires distribution of
* source code.
*
* Note that people who make modified versions of this module
* are not obligated to grant this special exception for their
* modified versions; it is their choice whether to do so. The GNU
* General Public License gives permission to release a modified
* version without this exception; this exception also makes it
* possible to release a modified version which carries forward this
* exception.
*
*/
//
// $Header: /cvsroot/nsnam/ns-2/mac/mac-tdma.cc,v 1.16 2006/02/22 13:25:43 mahrenho 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
#include "arp.h"
#include "ll.h"
#include "mac.h"
#include "mac-tdma.h"
#include "wireless-phy.h"
#include "cmu-trace.h"
#include
#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_: %lx, pktRx_: %lx, callback: %lx\n",
(long) pktTx_, (long) pktRx_, (long) 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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -