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

📄 mac_802_15_4.cpp

📁 csma协议
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        {            return t_CAP;        }        if ((small)             &&  (t_CAP > t_CAP2))        {            t_CAP = t_CAP2;        }        if ((!small)              &&  (t_CAP < t_CAP2))        {            t_CAP = t_CAP2;        }        return t_CAP;    }}// /**// FUNCTION   :: Mac802_15_4GetCAPbyType// LAYER      :: Mac// PURPOSE    :: Returns the start of contention access period// PARAMETERS ::// + node           : Node*     : Node receiving call// + interfaceIndex : int       : Interface index// + type           : int       :// RETURN  :: clocktype// **/clocktype Mac802_15_4GetCAPbyType(Node* node, int interfaceIndex, int type){    MacData802_15_4* mac;    clocktype bcnTxTime;    clocktype bcnRxTime;    clocktype bcnOtherRxTime;    clocktype bPeriod;    clocktype sSlotDuration;    clocktype sSlotDuration2;    clocktype sSlotDuration3;    clocktype BI2;    clocktype BI3;    clocktype t_CAP;    clocktype t_CAP2;    clocktype t_CAP3;    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 (type == 1)    {        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(t_CAP >= now)            {                return t_CAP;            }            else            {                return oneDay;            }        }        else        {            return oneDay;        }    }    if (type == 2)    {        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)) //no more than                //<aMaxLostBeacons> beacons missed            {                while (t_CAP2 < now)                {                    t_CAP2 += BI2;                }            }            if(t_CAP2 >= now)            {                return t_CAP2;            }            else            {                return oneDay;            }        }        else        {            return oneDay;        }    }    if (type == 3)    {        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(t_CAP3 >= now)            {                return t_CAP3;            }            else            {                return oneDay;            }        }        else        {            return oneDay;        }    }    return oneDay;}// /**// FUNCTION   :: Mac802_15_4CheckForOutgoingPacket// LAYER      :: Mac// PURPOSE    :: Check if there is any pending packet in Network queue// PARAMETERS ::// + node           : Node*     : Node receiving call// + interfaceIndex : int       : Interface index// RETURN  :: None// **/staticvoid Mac802_15_4CheckForOutgoingPacket(Node* node, int interfaceIndex){    MacData802_15_4* mac;    mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar;    Mac802_15_4NetworkLayerHasPacketToSend(node, mac);}// /**// FUNCTION   :: Mac802_15_4TaskFailed// LAYER      :: Mac// PURPOSE    :: Task Failed// PARAMETERS ::// + node           : Node*     : Node receiving call// + interfaceIndex : int       : Interface index// + task           : char      : Task// + status         : M802_15_4_enum : MAC status// + csmacaRes      : BOOL      : Whether to resume CSMA-CA// RETURN  :: None// **/staticvoid Mac802_15_4TaskFailed(        Node* node,        int interfaceIndex,        char task,        M802_15_4_enum status,        BOOL csmacaRes){    MacData802_15_4* mac;    M802_15_4Header* wph;    mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar;    if ((task == 'b')   //beacon         || (task == 'a')    //ack.         || (task == 'c'))   //command    {        ERROR_Assert(0, "");  //we don't handle the above failures here    }    else if (task == 'C')   //command    {        MESSAGE_Free(node, mac->txBcnCmd2);        mac->txBcnCmd2 = NULL;    }    else if (task == 'd')   //data    {        wph = (M802_15_4Header*)mac->txData;        Message* p = mac->txData;        int rt;        mac->txData = NULL;        if (wph->msduHandle)    //from SSCS        {            Sscs802_15_4MCPS_DATA_confirm(                    node,                    interfaceIndex,                    wph->msduHandle,                    status);        }        else        {        }        //if it is a pending packet, delete it from the pending list        rt = Mac802_15_4UpdateTransacLinkByPktOrHandle(                node,                1,  //del                &mac->transacLink1,                &mac->transacLink2,                p,                0);        if(rt != 0)        {            MESSAGE_Free(node, p);            p = NULL;        }        Mac802_15_4CheckForOutgoingPacket(node, interfaceIndex);    }    else    {        ERROR_Assert(0, "");    }    if (csmacaRes)    {        Mac802_15_4CsmacaResume(node, interfaceIndex);    }}// /**// FUNCTION   :: Mac802_15_4TaskSuccess// LAYER      :: Mac// PURPOSE    :: Task finalization// PARAMETERS ::// + node           : Node*     : Node receiving call// + interfaceIndex : int       : Interface index// + task           : char      : Task// + csmacaRes      : BOOL      : Whether to resume CSMA-CA// RETURN  :: None// **/staticvoid Mac802_15_4TaskSuccess(        Node* node,        int interfaceIndex,        char task,        BOOL csmacaRes){    MacData802_15_4* mac;    UInt16 t_CAP;    UInt8 ifs;    clocktype tmpf;    mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar;    if (task == 'b')    //beacon    {        if (!mac->txBeacon)        {            ERROR_Assert(mac->txBcnCmd2, "");            mac->txBeacon = mac->txBcnCmd2;            mac->txBcnCmd2 = NULL;        }        //--- calculate CAP ---        Mac802_15_4SuperFrameParse(&mac->sfSpec);        if (MESSAGE_ReturnPacketSize(mac->txBeacon) <= aMaxSIFSFrameSize)        {            ifs = aMinSIFSPeriod;        }        else        {            ifs = aMinLIFSPeriod;        }        {            tmpf = Mac802_15_4TrxTime(node, interfaceIndex, mac->txBeacon) *                    PHY_GetRxDataRate(node, interfaceIndex);            tmpf += ifs * SECOND;            mac->beaconPeriods = (UInt8)(tmpf /                                    (aUnitBackoffPeriod * SECOND));        }        tmpf = Mac802_15_4TrxTime(node, interfaceIndex, mac->txBeacon) *                PHY_GetRxDataRate(node, interfaceIndex);        tmpf += ifs * SECOND;        if (tmpf % (aUnitBackoffPeriod * SECOND) > 0)        {            mac->beaconPeriods++;        }        t_CAP = (mac->sfSpec.FinCAP + 1) *                ((UInt16)mac->sfSpec.sd /aUnitBackoffPeriod)                - mac->beaconPeriods;        if (t_CAP == 0)        {            csmacaRes = FALSE;            mac->trx_state_req = TRX_OFF;            Phy802_15_4PlmeSetTRX_StateRequest(                    node,                    interfaceIndex,                    TRX_OFF);        }        else        {            mac->trx_state_req = RX_ON;            Phy802_15_4PlmeSetTRX_StateRequest(                    node,                    interfaceIndex,                    RX_ON);        }        //CSMA-CA may be waiting for the new beacon        if (mac->backoffStatus == 99)        {            Csma802_15_4NewBeacon(node, interfaceIndex, 't');        }        mac->beaconWaiting = FALSE;        if(mac->txBeacon)        {            MESSAGE_Free(node, mac->txBeacon);            mac->txBeacon = NULL;        }    }    else if (task == 'a')   //ack.    {        ERROR_Assert(mac->txAck, "");        if(mac->txAck)        {            MESSAGE_Free(node, mac->txAck);            mac->txAck = NULL;        }    }    else if (task == 'c')   //command    {        int rt;        ERROR_Assert(mac->txBcnCmd, "");        //if it is a pending packet, delete it from the pending list        rt = Mac802_15_4UpdateTransacLinkByPktOrHandle(                node,                1,  //del                &mac->transacLink1,                &mac->transacLink2,                mac->txBcnCmd,                0);        if(rt != 0)        {            MESSAGE_Free(node, mac->txBcnCmd);        }        mac->txBcnCmd = NULL;    }    else if (task == 'C')   //command    {        ERROR_Assert(mac->txBcnCmd2, "");        if(mac->txBcnCmd2)

⌨️ 快捷键说明

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