📄 mac_802_15_4.cpp
字号:
M802_15_4PendAddrField* PendAddrFields, UInt8 CmdType){ MacData802_15_4* mac802_15_4; M802_15_4BeaconFrame* bcFrm; M802_15_4CommandFrame* cmdFrm; int payloadsize; mac802_15_4 = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; *msg = MESSAGE_Alloc(node, MAC_LAYER, MAC_PROTOCOL_802_15_4, MSG_MAC_802_15_4_Frame); switch (frmType) { case M802_15_4DEFFRMCTRL_TYPE_BEACON: //Beacon { payloadsize = sizeof (M802_15_4BeaconFrame); break; } case M802_15_4DEFFRMCTRL_TYPE_MACCMD: //Mac CMD { payloadsize = sizeof (M802_15_4CommandFrame); if (CmdType == 0x01) //association request { payloadsize += 1; } else if (CmdType == 0x02) //association response { payloadsize += sizeof(UInt16) + sizeof(M802_15_4_enum); } else if (CmdType == 0x03) //disassociation notification { payloadsize += 1; } else if (CmdType == 0x08) //realignment { payloadsize += 7; //for extra payload. } break; } default: { payloadsize = 1; //hack for ack } } MESSAGE_PacketAlloc(node, *msg, payloadsize, TRACE_MAC_802_15_4); if (frmType == M802_15_4DEFFRMCTRL_TYPE_BEACON) //beacon { bcFrm = (M802_15_4BeaconFrame*)MESSAGE_ReturnPacket((*msg)); bcFrm->MSDU_SuperSpec = SuperSpec; bcFrm->MSDU_GTSFields = *GTSFields; bcFrm->MSDU_PendAddrFields = *PendAddrFields; } else if (frmType == M802_15_4DEFFRMCTRL_TYPE_MACCMD) //MAC cmd { cmdFrm = (M802_15_4CommandFrame*)MESSAGE_ReturnPacket((*msg)); cmdFrm->MSDU_CmdType = CmdType; }}// /**// FUNCTION :: Mac802_15_4AddCommandHeader// LAYER :: Mac// PURPOSE :: Add MHR to a message// PARAMETERS ::// + node : Node* : Node receiving call// + interfaceIndex : int : Interface index// + msg : Message* : Message pointer// + FrmType : UInt8 : Frame type (beacon/data/ack/MAC cmd)// + dstAddrMode : UInt8 : Addressing mode (short/long)// + dstPANId : UInt16 : Destination PAN Id// + dstAddr : MACADDR: Destination address// + srcAddrMode : UInt8 : Source Address mode// + srcPANId : UInt16 : Source PAN Id// + srcAddr : MACADDR: Source address// + secuEnable : BOOL : Whether security is enabled// + pending : BOOL : Whether data pending// + ackreq : BOOL : Whether Ack requested// + FCS : UInt16 : checksum - usually not calculated// RETURN :: None// **/staticvoid Mac802_15_4AddCommandHeader( Node* node, int interfaceIndex, Message* msg, UInt8 FrmType, UInt8 dstAddrMode, UInt16 dstPANId, MACADDR dstAddr, UInt8 srcAddrMode, UInt16 srcPANId, MACADDR srcAddr, BOOL secuEnable, BOOL pending, BOOL ackreq, UInt8 CmdType, UInt16 FCS){ MacData802_15_4* mac; M802_15_4Header* hdr; M802_15_4FrameCtrl frmCtrl = {0, 0}; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; MESSAGE_AddHeader(node, msg, sizeof(M802_15_4Header), TRACE_MAC_802_15_4); hdr = (M802_15_4Header*) msg->packet; Mac802_15_4FrameCtrlSetFrmType(&frmCtrl, FrmType); Mac802_15_4FrameCtrlSetDstAddrMode(&frmCtrl, dstAddrMode); Mac802_15_4FrameCtrlSetSecu(&frmCtrl, secuEnable); Mac802_15_4FrameCtrlSetFrmPending(&frmCtrl, pending); Mac802_15_4FrameCtrlSetAckReq(&frmCtrl, ackreq); Mac802_15_4FrameCtrlSetSrcAddrMode(&frmCtrl, srcAddrMode); hdr->MHR_DstAddrInfo.panID = dstPANId; hdr->MHR_DstAddrInfo.addr_64 = dstAddr; hdr->MHR_SrcAddrInfo.panID = srcPANId; hdr->MHR_SrcAddrInfo.addr_64 = srcAddr; hdr->MHR_FrmCtrl = frmCtrl.FrmCtrl; hdr->MHR_FCS = FCS; hdr->msduHandle = 0; hdr->indirect = 0; if (FrmType == M802_15_4DEFFRMCTRL_TYPE_BEACON) //Beacon { hdr->MHR_BDSN = mac->mpib.macBSN++; } else if (FrmType == M802_15_4DEFFRMCTRL_TYPE_DATA || FrmType == M802_15_4DEFFRMCTRL_TYPE_MACCMD) //Data or MAC cmd { hdr->MHR_BDSN = mac->mpib.macDSN++; } if (FrmType == M802_15_4DEFFRMCTRL_TYPE_MACCMD) { hdr->MSDU_CmdType = CmdType; }}// /**// FUNCTION :: Mac802_15_4ConstructACK// LAYER :: Mac// PURPOSE :: Construct acknowledgement for a message// PARAMETERS ::// + node : Node* : Node receiving call// + interfaceIndex : int : Interface index// + msg : Message* : Message pointer// RETURN :: None// **/staticvoid Mac802_15_4ConstructACK( Node* node, int interfaceIndex, Message* msg){ MacData802_15_4* mac; M802_15_4Header* wph1; M802_15_4Header* wph2; M802_15_4FrameCtrl frmCtrl; BOOL intraPan; UInt8 origFrmType; Message* ack; int i; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; Mac802_15_4ConstructPayload( node, interfaceIndex, &ack, 0x02, 0, NULL, NULL, 0x02); wph1 = (M802_15_4Header*) msg->packet; frmCtrl.FrmCtrl = wph1->MHR_FrmCtrl; Mac802_15_4FrameCtrlParse(&frmCtrl); intraPan = frmCtrl.intraPan; origFrmType = frmCtrl.frmType; frmCtrl.FrmCtrl = 0; Mac802_15_4FrameCtrlSetFrmType(&frmCtrl, M802_15_4DEFFRMCTRL_TYPE_ACK); Mac802_15_4FrameCtrlSetSecu(&frmCtrl, FALSE); if ((origFrmType == M802_15_4DEFFRMCTRL_TYPE_MACCMD) //command packet && (wph1->MSDU_CmdType == 0x04)) //data request command { i = Mac802_15_4UpdateTransacLink( node, 2, &mac->transacLink1, &mac->transacLink2, frmCtrl.srcAddrMode, wph1->MHR_SrcAddrInfo.addr_64); Mac802_15_4FrameCtrlSetFrmPending(&frmCtrl, i == 0); } else { Mac802_15_4FrameCtrlSetFrmPending(&frmCtrl, FALSE); } Mac802_15_4FrameCtrlSetAckReq(&frmCtrl, FALSE); Mac802_15_4FrameCtrlSetIntraPan(&frmCtrl, intraPan); Mac802_15_4FrameCtrlSetDstAddrMode( &frmCtrl, M802_15_4DEFFRMCTRL_ADDRMODENONE); Mac802_15_4FrameCtrlSetSrcAddrMode( &frmCtrl, M802_15_4DEFFRMCTRL_ADDRMODENONE); Mac802_15_4AddCommandHeader( node, interfaceIndex, ack, frmCtrl.frmType, //ack frmCtrl.srcAddrMode, wph1->MHR_SrcAddrInfo.panID, wph1->MHR_SrcAddrInfo.addr_64, frmCtrl.srcAddrMode, mac->mpib.macPANId, (UInt16)node->nodeId, frmCtrl.secu, frmCtrl.frmPending, frmCtrl.ackReq, 0, 0); wph2 = (M802_15_4Header*) ack->packet; wph2->MHR_BDSN = wph1->MHR_BDSN; //copy the SN from Data/MacCmd packet if(mac->txAck) { MESSAGE_Free(node, mac->txAck); mac->txAck = NULL; } mac->txAck = ack;}// /**// FUNCTION :: Mac802_15_4GetBattLifeExtSlotNum// LAYER :: Mac// PURPOSE ::// PARAMETERS ::// + node : Node* : Node receiving call// + interfaceIndex : int : Interface index// RETURN :: int// **/int Mac802_15_4GetBattLifeExtSlotNum(Node* node, int interfaceIndex){ MacData802_15_4* mac; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; if(Phy802_15_4getChannelNumber(node, interfaceIndex) <= 10) { return 8; } else { return 6; }}// /**// FUNCTION :: Mac802_15_4GetCAP// LAYER :: Mac// PURPOSE :: Returns start of contention access period// PARAMETERS ::// + node : Node* : Node receiving call// + interfaceIndex : int : Interface index// + small : BOOL :// RETURN :: clocktype// **/clocktype Mac802_15_4GetCAP(Node* node, int interfaceIndex, BOOL small){ MacData802_15_4* mac; clocktype bcnTxTime; clocktype bcnRxTime; clocktype bcnOtherRxTime; clocktype bPeriod; clocktype sSlotDuration; clocktype sSlotDuration2; clocktype sSlotDuration3; clocktype BI2,BI3; clocktype t_CAP = 0; clocktype t_CAP2 = 0; clocktype t_CAP3 = 0; clocktype now; clocktype oneDay; clocktype tmpf; clocktype len12s; int rate; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; now = getSimTime(node); oneDay = now + 24 * 3600 * SECOND; if ((mac->mpib.macBeaconOrder == 15) && (mac->macBeaconOrder2 == 15) && (mac->macBeaconOrder3 == 15)) { return oneDay; } rate = PHY_GetRxDataRate(node, interfaceIndex); bcnTxTime = mac->macBcnTxTime; bcnRxTime = mac->macBcnRxTime; bcnOtherRxTime = mac->macBcnOtherRxTime; len12s = 12 * SECOND / rate; bPeriod = aUnitBackoffPeriod * SECOND / rate; sSlotDuration = mac->sfSpec.sd * SECOND / rate; sSlotDuration2 = mac->sfSpec2.sd * SECOND / rate; sSlotDuration3 = mac->sfSpec3.sd * SECOND / rate; BI2 = mac->sfSpec2.BI * SECOND / rate; BI3 = mac->sfSpec3.BI * SECOND / rate; if (mac->mpib.macBeaconOrder != 15) { if (mac->sfSpec.BLE) { tmpf = (mac->beaconPeriods + Mac802_15_4GetBattLifeExtSlotNum(node, interfaceIndex)) * aUnitBackoffPeriod * SECOND; t_CAP = bcnTxTime + tmpf; } else { tmpf = (mac->sfSpec.FinCAP + 1) * sSlotDuration - len12s; t_CAP = bcnTxTime + tmpf; } } if (mac->macBeaconOrder2 != 15) { if (mac->sfSpec2.BLE) { tmpf = (mac->beaconPeriods2 + Mac802_15_4GetBattLifeExtSlotNum(node, interfaceIndex)) * aUnitBackoffPeriod * SECOND; t_CAP2 = bcnRxTime + tmpf; } else { tmpf = (mac->sfSpec2.FinCAP + 1) * sSlotDuration2; t_CAP2 = bcnRxTime + tmpf; } tmpf = aMaxLostBeacons * BI2; if ((t_CAP2 < now) && (t_CAP2 + tmpf >= now)) { while (t_CAP2 < now) { t_CAP2 += BI2; } } } if (mac->macBeaconOrder3 != 15) { { tmpf = (mac->sfSpec3.FinCAP + 1) * sSlotDuration3; t_CAP3 = bcnOtherRxTime + tmpf; } tmpf = aMaxLostBeacons * BI3; if ((t_CAP3 < now) && (t_CAP3 + tmpf >= now)) { while (t_CAP3 < now) { t_CAP3 += BI3; } } } if ((mac->mpib.macBeaconOrder == 15) && (mac->macBeaconOrder2 == 15)) { if (t_CAP3 >= now) { return t_CAP3; } else { return oneDay; } } else if (mac->mpib.macBeaconOrder == 15) { if (t_CAP2 >= now) { return t_CAP2; } else { return oneDay; } } else if (mac->macBeaconOrder2 == 15) { if (t_CAP >= now) { return t_CAP; } else { return oneDay; } } else { if (t_CAP2 < now)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -