📄 mac-802_16.cc
字号:
/**************************************************************************************
* *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 "cmu-trace.h"
#include "rtp.h"
#include "agent.h"
#include "basetrace.h"
#include "geographic.h"
#define BsID 0
#define MaxProfileNum 6
#define PDUSIZE 100
#define DlSymbolNum 36
#define UlSymbolNum 12
#define DlSubchanNum 60
#define UlSubchanNum 92
extern TOMAC *CalMod;
int BasicCID_Counter = 0x0001; //0x0001 ~ 0x1000
int PrimaryCID_Counter = 0x001; //0x1001 ~ 0x2000
int TransportCID_Counter = 0x2001; //0x2001 ~ 0xFFFE
int FrameNumber_Counter = 0;
int DCD_Counter = 0;
int UCD_Counter = 0;
int ServiceFlowID_Counter0 = 0;
int ServiceFlowID_Counter1 = 0;
int ServiceFlowID_Counter2 = 0;
int ServiceFlowID_Counter3 = 0;
int ServiceFlowID_Counter4 = 0;
int ServiceFlowID_Counter5 = 0;
int ServiceFlowID_Counter6 = 0;
int ServiceFlowID_Counter7 = 0;
int ServiceFlowID_Counter8 = 0;
int ServiceFlowID_Counter9 = 0;
int UlMapIeNum, DlMapIeNum;
int DlSymbolMap[DlSymbolNum][DlSubchanNum];
int UlSymbolMap[UlSymbolNum][UlSubchanNum];
double FrameLength = 0.005, SymbolLength = 0.00010084, TtgLength = 0.00002941, RtgLength = 0.00002941;
double AllocationStartTime;
int DlAvailSymbol, UlAvailSymbol;
BsSsInfo *BsSsInfo_head;
BsSsInfo *BsSsInfo_tail;
BsServiceFlow *BsSF_head;
BsServiceFlow *BsSF_tail;
UplinkBurstProfile *UlBurstProfile_head;
UplinkBurstProfile *UlBurstProfile_tail;
DownlinkBurstProfile *DlBurstProfile_head;
DownlinkBurstProfile *DlBurstProfile_tail;
UlMapIe *UlMapIe_head;
UlMapIe *UlMapIe_tail;
DlMapIe *DlMapIe_head;
DlMapIe *DlMapIe_tail;
Mac802_16::Mac802_16() :Mac(),frameTimer(this),preambleTimer(this),downlinkTimer(this),uplinkTimer(this),ulmapTimer(this),dlmapTimer(this)
{
NodeID = index_;
if (NodeID == BsID) {
TransactionID_Counter = 0x8000; //0x8000 ~ 0xFFFFF
BsSsInfo_head = NULL;
BsSsInfo_tail = NULL;
BsSF_head = NULL;
BsSF_tail = NULL;
UlBurstProfile_head = NULL;
UlBurstProfile_tail = NULL;
DlBurstProfile_head = NULL;
DlBurstProfile_tail = NULL;
UlMapIe_head = NULL;
UlMapIe_tail = NULL;
DlMapIe_head = NULL;
DlMapIe_tail = NULL;
DlAvailSymbol = DlSymbolNum*DlSubchanNum;
UlAvailSymbol = UlSymbolNum*UlSubchanNum;
frameTimer.start(0);
DlMapIeNum = 0;
UlMapIeNum = 0; RangingFlag=false;//BS刚初始化的时候没有任何SS接入,设置为false
} else {
TransactionID_Counter = 0x0000; //0x0000 ~ 0x7FFFF
SsSF_head = NULL;
SsSF_tail = NULL;
RangingStatus = false;
}
}
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);
UcdMsg *ucd = (UcdMsg*) p->accessdata();
DcdMsg *dcd = (DcdMsg*) p->accessdata();
RngReqMsg *rngreq = (RngReqMsg*) p->accessdata();
RngRspMsg *rngrsp = (RngRspMsg*) p->accessdata();
DsaReqMsg *dsareq = (DsaReqMsg*) p->accessdata();
DsaRspMsg *dsarsp = (DsaRspMsg*) p->accessdata();
DsaAckMsg *dsaack = (DsaAckMsg*) p->accessdata();
if (hdr_cmn::access(p)->direction() == hdr_cmn::UP) {
if (NodeID == BsID) {
if (hdr->ptype() == PT_UGS) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)) {
/*if (hdr->next_hop() != BsID) {
if (!BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_UGS, DL, -1, -1))
sendDSAREQ(NULL, BsCreateServiceFlow(p, DL));
BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_UGS, DL, -1, -1)->SduQueue.enque(p);
} else*/
uptarget_->recv(p, this);
//printf("bs recv UGS pkt from ss%d\n",BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr);
}
} else if (hdr->ptype() == PT_ertPS) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)) {
/*if (hdr->next_hop() != BsID) {
if (!BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_ertPS, DL, -1, -1))
sendDSAREQ(NULL, BsCreateServiceFlow(p, DL));
BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_ertPS, DL, -1, -1)->SduQueue.enque(p);
} else*/
uptarget_->recv(p, this);
//printf("bs recv ertPS pkt from ss%d\n",BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr);
}
} else if (hdr->ptype() == PT_rtPS) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)) {
/*if (hdr->next_hop() != BsID) {
if (!BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_rtPS, DL, -1, -1))
sendDSAREQ(NULL, BsCreateServiceFlow(p, DL));
BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_rtPS, DL, -1, -1)->SduQueue.enque(p);
} else*/
uptarget_->recv(p, this);
//printf("bs recv rtPS pkt from ss%d\n",BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr);
}
} else if (hdr->ptype() == PT_nrtPS) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)) {
/*if (hdr->next_hop() != BsID) {
if (!BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_nrtPS, DL, -1, -1))
sendDSAREQ(NULL, BsCreateServiceFlow(p, DL));
BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_nrtPS, DL, -1, -1)->SduQueue.enque(p);
} else*/
uptarget_->recv(p, this);
//printf("bs recv nrtPS pkt from ss%d\n",BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr);
}
} else if (hdr->ptype() == PT_BE) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)) {
/*if (hdr->next_hop() != BsID) {
if (!BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_BE, DL, -1, -1))
sendDSAREQ(NULL, BsCreateServiceFlow(p, DL));
BsSearchServiceFlow(BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr, hdr->next_hop(), PT_BE, DL, -1, -1)->SduQueue.enque(p);
} else*/
uptarget_->recv(p, this);
//printf("bs recv BE pkt from ss%d\n",BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, -1, mac16->generic_hdr.CID)->MacAddr);
}
} else if (hdr->ptype() == PT_ACK) {
if (mac->macDA() == NodeID)
uptarget_->recv(p, this);
} else if (hdr->ptype() == PT_BWREQ) {
//printf("bs recv BW-REQ from ss%d\n",mac->macSA());
BsSearchSsInfo(-1, mac16->bwreq_hdr.CID)->UplinkBandwidth += mac16->bwreq_hdr.BR;
mac_log(p);
} else if (hdr->ptype() == PT_RNGREQ) {
//printf("bs recv RNG-REQ from ss%d \n", mac->macSA());//////////16协议中SS是竞争接入的,BS一帧内发送的测距ie只供一个SS接入,而长庚的补丁中是一个测距ie可以让多个SS同时接入,我在在BS中增加一个变量RangingFlag,可以使BS一帧内只接收到一个测距请求 if(RangingFlag) return;//如果处于测距请求阻止状态,拒绝接受该消息
mac_log(p->copy());
if (!BsSearchSsInfo(rngreq->SS_MAC_Address, -1)) {
sendRNGRSP(BsCreateSsInfo(p)); RangingFlag=true;//如果BS发送了测距回复,将RangingFlag置true,阻止一帧内的其他测距请求,这个标志位在BS发送完本RNGRSP消息后将会恢复 }
} else if (hdr->ptype() == PT_DSAREQ) {
if (dsareq->Transaction_ID <= 0x7FFF) {
packet_t ptype;
switch (dsareq->Service_Flow_Parameters.Service_Flow_Scheduling_Type.Value) {
case 0x06: ptype = PT_UGS; break;
case 0x05: ptype = PT_ertPS; break;
case 0x04: ptype = PT_rtPS; break;
case 0x03: ptype = PT_nrtPS; break;
case 0x02: ptype = PT_BE; break;
}
if (BsSearchSsInfo(-1, mac16->generic_hdr.CID) && !BsSearchServiceFlow(dsareq->Service_Flow_Parameters.Source_MAC_Address.Value, dsareq->Service_Flow_Parameters.Destination_MAC_Address.Value, ptype, UL, -1, -1)) {
//printf("bs recv DSA-REQ from ss%d\n", BsSearchSsInfo(-1, mac16->generic_hdr.CID)->MacAddr);
mac_log(p->copy());
sendDSXRVD(p->copy());
sendDSARSP(NULL, BsCreateServiceFlow(p->copy(), UL));
}
}
} else if (hdr->ptype() == PT_DSARSP) {
if (dsarsp->Transaction_ID >= 0x8000)
if (BsSearchSsInfo(-1, mac16->generic_hdr.CID)) {
mac_log(p->copy());
if (dsarsp->Confirmation_Code == 0x00)
BsSearchServiceFlow(-1, -1, PT_NTYPE, DL, dsarsp->Transaction_ID, -1)->Status = Active;
sendDSAACK(p);
}
} else if (hdr->ptype() == PT_DSAACK) {
if (dsaack->Transaction_ID <= 0x7FFF) {
if (BsSearchServiceFlow(-1, -1, PT_NTYPE, UL, dsaack->Transaction_ID, -1)) {
//printf("bs recv DSA-ACK from ss%d\n",mac->macSA());
mac_log(p);
}
}
} else {
// if(hdr->ptype()==PT_AODV)
//printf("Node%d 接收从下层来的AODV信息\n",NodeID);
sendUp(p);
return;
}
return;
} else {
if (hdr->ptype() == PT_UGS) {
if (SsSearchServiceFlow(-1, PT_NTYPE, DL, -1, mac16->generic_hdr.CID)) {
uptarget_->recv(p, this);
//printf("ss%d recv UGS pkt\n",NodeID);
}
} else if (hdr->ptype() == PT_ertPS) {
if (SsSearchServiceFlow(-1, PT_NTYPE, DL, -1, mac16->generic_hdr.CID)) {
uptarget_->recv(p, this);
//printf("ss%d recv ertPS pkt\n",NodeID);
}
} else if (hdr->ptype() == PT_rtPS) {
if (SsSearchServiceFlow(-1, PT_NTYPE, DL, -1, mac16->generic_hdr.CID)) {
uptarget_->recv(p, this);
//printf("ss%d recv rtPS pkt\n",NodeID);
}
} else if (hdr->ptype() == PT_nrtPS) {
if (SsSearchServiceFlow(-1, PT_NTYPE, DL, -1, mac16->generic_hdr.CID)) {
uptarget_->recv(p, this);
//printf("ss%d recv nrtPS pkt\n",NodeID);
}
} else if (hdr->ptype() == PT_BE) {
if (SsSearchServiceFlow(-1, PT_NTYPE, DL, -1, mac16->generic_hdr.CID)) {
uptarget_->recv(p, this);
//printf("ss%d recv BE pkt\n",NodeID);
}
} else if (hdr->ptype() == PT_ACK) {
if (mac->macDA() == NodeID)
uptarget_->recv(p, this);
} else if (hdr->ptype() == PT_UCD) {
//printf("ss%d recv UCD\n",NodeID);
if (UcdConfigChangeCount != ucd->Config_Change_Count) {
UcdConfigChangeCount = ucd->Config_Change_Count;
UplinkBurstProfile *tmp = UlBurstProfile_head;
while (tmp) {
int FEC_Code;
switch (ModulCoding) {
case QPSK_12: FEC_Code = 0; break;
case QPSK_34: FEC_Code = 1; break;
case QAM16_12: FEC_Code = 2; break;
case QAM16_34: FEC_Code = 3; break;
case QAM64_23: FEC_Code = 4; break;
case QAM64_34: FEC_Code = 5; break;
}
if (tmp->FEC_Code_and_modulation_type.Value == FEC_Code) {
UplinkBurstProfileTable.UIUC = tmp->UIUC;
UplinkBurstProfileTable.FEC_Code_and_modulation_type.Value = tmp->FEC_Code_and_modulation_type.Value;
}
tmp = tmp->next;
}
}
mac_log(p);
} else if (hdr->ptype() == PT_DCD) {
//printf("ss%d recv DCD\n",NodeID);
if (DcdConfigChangeCount != dcd->Config_Change_Count) {
DcdConfigChangeCount = dcd->Config_Change_Count;
DownlinkBurstProfile *tmp = DlBurstProfile_head;
while (tmp) {
int FEC_Code;
ModulCoding = CalMod->search(NodeID)->ToMod(CalMod->search(0)->x,CalMod->search(0)->y);
switch (ModulCoding) {
case QPSK_12: FEC_Code = 0; break;
case QPSK_34: FEC_Code = 1; break;
case QAM16_12: FEC_Code = 2; break;
case QAM16_34: FEC_Code = 3; break;
case QAM64_23: FEC_Code = 4; break;
case QAM64_34: FEC_Code = 5; break;
}
if (tmp->FEC_Code_type.Value == FEC_Code) {
DownlinkBurstProfileTable.DIUC = tmp->DIUC;
DownlinkBurstProfileTable.FEC_Code_type.Value = tmp->FEC_Code_type.Value;
}
tmp = tmp->next;
}
}
mac_log(p);
} else if (hdr->ptype() == PT_BWREQ) {
Packet::free(p);
} else if (hdr->ptype() == PT_RNGREQ) {
Packet::free(p);
} else if (hdr->ptype() == PT_DLMAP) {
mac_log(p);
} else if (hdr->ptype() == PT_ULMAP) {
ulmapTimer.start(AllocationStartTime - Scheduler::instance().clock());
mac_log(p);
} else if (hdr->ptype() == PT_RNGRSP) {
if ((int)rngrsp->SS_MAC_Address == NodeID) {
//printf("ss%d recv RNG-RSP basic=%d primary=%d\n",NodeID,rngrsp->Basic_CID,rngrsp->Primary_Management_CID);
if (rngrsp->Ranging_Status == 0x03) {
RangingStatus = true;
BasicCID = rngrsp->Basic_CID;
PrimaryCID = rngrsp->Primary_Management_CID;
}
mac_log(p);
}
} else if (hdr->ptype() == PT_DSAREQ) {
if (dsareq->Transaction_ID >= 0x8000) {
packet_t ptype;
switch (dsareq->Service_Flow_Parameters.Service_Flow_Scheduling_Type.Value) {
case 0x06: ptype = PT_UGS; break;
case 0x05: ptype = PT_ertPS; break;
case 0x04: ptype = PT_rtPS; break;
case 0x03: ptype = PT_nrtPS; break;
case 0x02: ptype = PT_BE; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -