📄 maca.pc
字号:
/* * GloMoSim is COPYRIGHTED software. Release 2.02 of GloMoSim is available * at no cost to educational users only. * * Commercial use of this software requires a separate license. No cost, * evaluation licenses are available for such purposes; please contact * info@scalable-networks.com * * By obtaining copies of this and any other files that comprise GloMoSim2.02, * you, the Licensee, agree to abide by the following conditions and * understandings with respect to the copyrighted software: * * 1.Permission to use, copy, and modify this software and its documentation * for education and non-commercial research purposes only is hereby granted * to Licensee, provided that the copyright notice, the original author's * names and unit identification, and this permission notice appear on all * such copies, and that no charge be made for such copies. Any entity * desiring permission to use this software for any commercial or * non-educational research purposes should contact: * * Professor Rajive Bagrodia * University of California, Los Angeles * Department of Computer Science * Box 951596 * 3532 Boelter Hall * Los Angeles, CA 90095-1596 * rajive@cs.ucla.edu * * 2.NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY * PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. * * 3.Neither the software developers, the Parallel Computing Lab, UCLA, or any * affiliate of the UC system shall be liable for any damages suffered by * Licensee from the use of this software. */// Use the latest version of Parsec if this line causes a compiler error./* * $Id: maca.pc,v 1.36 2001/08/31 18:25:51 jmartin Exp $ * * MACA (multiple access with collision avoidance) * * reference: C. L Fuller and J. J. Garcia paper MACA specification * using RTS /CTS control frames to reserve medium */#include <stdio.h>#include <string.h>#include <assert.h>#include <math.h>#include "api.h"#include "structmsg.h"#include "fileio.h"#include "message.h"#include "mac.h"#include "maca.h"#define noQDEBUGstatic /*inline*/RadioStatusType RadioStatus(GlomoNode* node, GlomoMacMaca* maca){ return GLOMO_RadioGetStatus(node, maca->myGlomoMac->interfaceIndex);}/* * NAME: MacMacaPrintStats * * PURPOSE: Print MAC layer statistics. * * PARAMETERS: node. * * RETURN: None. * * ASSUMPTION: node != NULL. */staticvoid MacMacaPrintStats(GlomoNode *node, GlomoMacMaca* maca){ char buf[GLOMO_MAX_STRING_LENGTH]; sprintf(buf, "Number of packets from network: %d", maca->pktsToSend); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of packets lost due to buffer overflow: %d", maca->pktsLostOverflow); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of UNICAST packets output to the channel: %d", maca->pktsSentUnicast); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of BROADCAST packets output to the channel: %d", maca->pktsSentBroadcast); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of UNICAST packets received clearly: %d", maca->pktsGotUnicast); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of BROADCAST packets received clearly: %d", maca->pktsGotBroadcast); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of RTS Packets sent: %d", maca->RtsPacketSent); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of CTS Packets sent: %d", maca->CtsPacketSent); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of RTS Packets got: %d", maca->RtsPacketGot); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of CTS Packets got: %d", maca->CtsPacketGot); GLOMO_PrintStat(node, "MacMACA", buf); sprintf(buf, "Number of Noisy Packets got: %d", maca->NoisyPacketGot); GLOMO_PrintStat(node, "MacMACA", buf);}/* * NAME: MacMacaSetTimer. * * PURPOSE: Set a timer for node to expire at time timerValue. * * PARAMETERS: node, node setting the timer. * timerType, what type of timer is being set. * timerValue, when timer is to expire. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaSetTimer(GlomoNode *node, GlomoMacMaca *maca, int timerType, clocktype timerValue){ Message *newMsg; int *timerSeq; maca->timer.flag = MACA_TIMER_ON | timerType; maca->timer.seq++; newMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_TimerExpired); GLOMO_MsgSetInstanceId(newMsg, maca->myGlomoMac->interfaceIndex); GLOMO_MsgInfoAlloc(node, newMsg, sizeof(maca->timer.seq)); timerSeq = (int *) newMsg->info; *timerSeq = maca->timer.seq; GLOMO_MsgSend(node, newMsg, timerValue);}/* * NAME: MacMacaMacMacaCancelTimer. * * PURPOSE: Cancel a timer that was already set. * * PARAMETERS: node, node cancelling the timer. * timerType, what type of timer is being cancelled. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaCancelTimer(GlomoNode *node, GlomoMacMaca *maca, int timerType){ if (timerType == MACA_T_UNDEFINED) { maca->timer.flag = MACA_TIMER_OFF | MACA_T_UNDEFINED; } else if ((maca->timer.flag & MACA_TIMER_TYPE) == timerType) { maca->timer.flag = MACA_TIMER_OFF | MACA_T_UNDEFINED; } else { assert(FALSE); }}/* * NAME: MacMacaMacMacaResetTimer. * * PURPOSE: Resets backoff timers to default values. * * PARAMETERS: node, node resetting backoff timers. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaResetTimer(GlomoNode *node, GlomoMacMaca *maca){ char clockStr[GLOMO_MAX_STRING_LENGTH]; maca->BOmin = MACA_BO_MIN; maca->BOmax = MACA_BO_MAX; maca->BOtimes = 0;}/* * NAME: MacMacaSetState. * * PURPOSE: Set the state of a node. * * PARAMETERS: node, node setting the state. * state, state to set to. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaSetState(GlomoNode *node, GlomoMacMaca *maca, int state){ maca->state = state;}/* * NAME: MacMacaYield. * * PURPOSE: Yield so neighboring nodes can transmit or receive. * * PARAMETERS: node, node that is yielding. * vacation, how long to yield for. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaYield(GlomoNode *node, GlomoMacMaca *maca, clocktype vacation){ assert(maca->state == MACA_S_YIELD); MacMacaSetTimer(node, maca, MACA_T_YIELD, vacation + pc_nrand(node->seed) % 20); }/* * NAME: MacMacaMacMacaSendCts. * * PURPOSE: Send CTS to neighboring nodes. * * PARAMETERS: node, node sending CTS frame. * fromNodeAddr, node that CTS frame is intended for. * payloadSize, size of the data to be sent. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaSendCts(GlomoNode *node, GlomoMacMaca *maca, NODE_ADDR fromNodeAddr, int payloadSize){ int macFrameSize; MacaHeader* hdr; Message* msg = GLOMO_MsgAlloc(node, 0, 0, 0); GLOMO_MsgPacketAlloc(node, msg, 0); GLOMO_MsgAddHeader(node, msg, sizeof(MacaHeader)); hdr = (MacaHeader *)msg->packet; hdr->sourceAddr = node->nodeAddr; hdr->destAddr = fromNodeAddr; hdr->frameType = MACA_CTS; hdr->payloadSize = payloadSize; hdr->priority = maca->currentPriority; maca->payloadSizeExpected = payloadSize; /* Size of CTS frame is simply the size of frame header. */ macFrameSize = sizeof(MacaHeader); msg->packetSize = macFrameSize; MacMacaSetState(node, maca, MACA_S_IN_XMITING_CTS); GLOMO_RadioStartTransmittingPacket( node, maca->myGlomoMac->interfaceIndex, msg, hdr->destAddr, FALSE, 0); maca->CtsPacketSent++; }/* * NAME: MacMacaMacMacaGetData. * * PURPOSE: Sends packet to upper layer. * * PARAMETERS: node, node handling the data packet. * msg, packet to send to upper layers. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaGetData(GlomoNode *node, GlomoMacMaca *maca, Message *msg){ MacaHeader *hdr = (MacaHeader *) msg->packet; NODE_ADDR lastHopAddress = hdr->sourceAddr; NODE_ADDR destinationAddress = hdr->destAddr; GLOMO_MsgRemoveHeader(node, msg, sizeof(MacaHeader)); NetworkIpReceivePacketFromMacLayer(node, msg, lastHopAddress); if (destinationAddress == ANY_DEST) { maca->pktsGotBroadcast++; } else { maca->pktsGotUnicast++; }}/* * NAME: MacMacaHandlePromiscuousMode. * * PURPOSE: Supports promiscuous mode sending remote packets to * upper layers. * * PARAMETERS: node, node using promiscuous mode. * msg, packet to send to upper layers. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaHandlePromiscuousMode( GlomoNode *node, GlomoMacMaca *maca, Message *msg){ GLOMO_MsgRemoveHeader(node, msg, sizeof(MacaHeader)); NetworkIpSneakPeekAtMacPacket(node, msg); GLOMO_MsgAddHeader(node, msg, sizeof(MacaHeader));}/* * NAME: MacMacaRts. * * PURPOSE: Send RTS frame to intended destination. * * PARAMETERS: node, node sending the RTS frame. * * RETURN: None. * * ASSUMPTION: node != NULL. */static void MacMacaRts(GlomoNode *node, GlomoMacMaca *maca){ NODE_ADDR nextHopAddress; MacaHeader *hdr; Message *msg; Message *tmpPktPtr; assert(maca->state == MACA_S_RTS); NetworkIpOutputQueueTopPacketForAPriority( node, maca->myGlomoMac->interfaceIndex, maca->currentPriority, &tmpPktPtr, &nextHopAddress); if (tmpPktPtr == NULL) { #ifdef QDEBUG printf("MACA: Queue should not be empty...\n"); #endif return; } /* Send RTS. */ msg = GLOMO_MsgAlloc(node, 0, 0, 0); GLOMO_MsgPacketAlloc(node, msg, 0); GLOMO_MsgAddHeader(node, msg, sizeof(MacaHeader)); hdr = (MacaHeader *) msg->packet; hdr->sourceAddr = node->nodeAddr; hdr->destAddr = nextHopAddress; hdr->frameType = MACA_RTS; hdr->payloadSize = tmpPktPtr->packetSize; hdr->priority = maca->currentPriority; maca->payloadSizeExpected = tmpPktPtr->packetSize; MacMacaSetState(node, maca, MACA_S_IN_XMITING_RTS); msg->packetSize = sizeof(MacaHeader); GLOMO_RadioStartTransmittingPacket( node, maca->myGlomoMac->interfaceIndex, msg, hdr->destAddr, FALSE, 0); maca->RtsPacketSent++;}/* * NAME: MacMacaDataXmit. * * PURPOSE: Sending data frames to destination. * * PARAMETERS: node, node sending the data frame. * tag, type of data frame to send. * * RETURN: None.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -