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

📄 mac.c

📁 zigbee location examples
💻 C
📖 第 1 页 / 共 4 页
字号:
        DISABLE_GLOBAL_INT();
        mpib.macCoordShortAddress = pibAttributeValueW;
        ENABLE_GLOBAL_INT();
        break;
    case MAC_PAN_ID:
        DISABLE_GLOBAL_INT();
        mpib.macPANId = pibAttributeValueW;
        WRITE_RFR16(PANID, mpib.macPANId);
        ENABLE_GLOBAL_INT();
        break;
    case MAC_SHORT_ADDRESS:
        DISABLE_GLOBAL_INT();
        mpib.macShortAddress = pibAttributeValueW;
        WRITE_RFR16(SHORTADDR, mpib.macShortAddress);
        ENABLE_GLOBAL_INT();
        break;
    case MAC_TRANSACTION_PERSISTENCE_TIME:
        DISABLE_GLOBAL_INT();
        mpib.macTransactionPersistenceTime = pibAttributeValueW;
        ENABLE_GLOBAL_INT();
        break;
    case PHY_CURRENT_CHANNEL:
        DISABLE_GLOBAL_INT();
        msupSetChannel(*(UINT8  *)pPibAttributeValue, TRUE);
        ENABLE_GLOBAL_INT();
        break;

    // Category 4 attributes (buffered until beacon transmission / handled carefully)
    case MAC_BATT_LIFE_EXT:
        DISABLE_GLOBAL_INT();
        if (isBeaconEnabledPan && (GET_MF(MF_TRANSMIT_BEACON) || mbcnInfo.trackBeacon)) {
            macInfo.pibTempBuffer.macBattLifeExt = (BOOL)pibAttributeValueW;
            macInfo.pibTempBuffer.updateMask |= MPIB_UPD_BATT_LIFE_EXT_BM;
        } else {
            mpib.macBattLifeExt = (BOOL)pibAttributeValueW;
        }
        ENABLE_GLOBAL_INT();
        break;

    case MAC_BEACON_ORDER:
        DISABLE_GLOBAL_INT();
        if ((UINT8)pibAttributeValueW > 15) {
            return INVALID_PARAMETER;
        } else if (isBeaconEnabledPan && (GET_MF(MF_TRANSMIT_BEACON) || mbcnInfo.trackBeacon)) {

#if MAC_OPT_FFD
            // Handle beacon -> non-beacon network change
            if ((UINT8)pibAttributeValueW == 15) {
                if (mtimCancelCallback(mbcnTxPeriodicalBeacon))
                    mbcnUpdateBufferedPibAttributes();
            }
#endif

            macInfo.pibTempBuffer.macBeaconOrder = (UINT8)pibAttributeValueW;
            macInfo.pibTempBuffer.updateMask |= MPIB_UPD_BEACON_ORDER_BM;
        } else {
            mpib.macBeaconOrder = (UINT8)pibAttributeValueW;
        }

        // Handle non-beacon <-> beacon network change
        mbcnHandleBeaconModeChange(!isBeaconEnabledPan);

        ENABLE_GLOBAL_INT();
        break;
    case MAC_RX_ON_WHEN_IDLE:
        DISABLE_GLOBAL_INT();
        if (isBeaconEnabledPan && (GET_MF(MF_TRANSMIT_BEACON) || mbcnInfo.trackBeacon)) {
            macInfo.pibTempBuffer.macRxOnWhenIdle = (BOOL) pibAttributeValueW;
            macInfo.pibTempBuffer.updateMask |= MPIB_UPD_RX_ON_WHEN_IDLE_BM;
        } else {
            if (!mpib.macRxOnWhenIdle && (BOOL) pibAttributeValueW) {
                mrxIncrOnCounter();
            } else if (mpib.macRxOnWhenIdle && !((BOOL) pibAttributeValueW)) {
                mrxDecrOnCounter();
            }
            mpib.macRxOnWhenIdle = (BOOL)pibAttributeValueW;
        }
        ENABLE_GLOBAL_INT();
        break;
    case MAC_SUPERFRAME_ORDER:
        DISABLE_GLOBAL_INT();
        if ((UINT8) pibAttributeValueW > 15) {
            return INVALID_PARAMETER;
        } else if (isBeaconEnabledPan && (GET_MF(MF_TRANSMIT_BEACON) || mbcnInfo.trackBeacon)) {
            macInfo.pibTempBuffer.macSuperframeOrder = (UINT8) pibAttributeValueW;
            macInfo.pibTempBuffer.updateMask |= MPIB_UPD_SUPERFRAME_ORDER_BM;
        } else {
            mpib.macSuperframeOrder = (UINT8) pibAttributeValueW;
        }
        ENABLE_GLOBAL_INT();
        break;



#if MAC_OPT_ACL_SIZE > 0
    case MAC_ACL_ENTRY_DESCRIPTOR_SET:
        mpib.ppMacACLEntryDescriptorSet = (ACL_ENTRY_SET   *) pPibAttributeValue;
        break;
    case MAC_ACL_ENTRY_DESCRIPTOR_SETSIZE:
        mpib.macACLEntryDescriptorSetSize = (BYTE) pibAttributeValueW;
        break;
#endif

#if MAC_OPT_SECURITY
    case MAC_DEFAULT_SECURITY:
        mpib.macDefaultSecurity = (BYTE)pibAttributeValueW;
        break;
    case MAC_DEFAULT_SECURITY_MATERIAL_LENGTH:
        mpib.macDefaultSecurityMaterialLength = (BYTE) pibAttributeValueW;
        break;
    case MAC_DEFAULT_SECURITY_MATERIAL:
        mpib.pMacDefaultSecurityMaterial = (SECURITY_MATERIAL   *) pPibAttributeValue;
        break;
    case MAC_DEFAULT_SECURITY_SUITE:
        mpib.macDefaultSecuritySuite = (BYTE) pibAttributeValueW;
        break;
#endif

#if ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
    case MAC_SECURITY_MODE:
        mpib.macSecurityMode = (BYTE)pibAttributeValueW;
        break;
#endif

    default:
        return UNSUPPORTED_ATTRIBUTE;
    }
    return SUCCESS;
} // mlmeSetRequest




//-------------------------------------------------------------------------------------------------------
//  MAC_ENUM mlmeGetRequest(MAC_PIB_ATTR pibAttribute, void *pPibAttributeValue)
//
//  DESCRIPTION:
//      Get MAC PIB attributes. The value is copied to the location pointed to by the void*. Note that
//      some values are returned as pointers:
//          - pMacBeaconPayload
//          - pMacACLEntryDescriptorSet
//          - pMacDefaultSecurityMaterial
//
//  PARAMETERS:
//      MAC_PIB_ATTR pibAttribute
//          The attribute to be changed
//      void *pPibAttributeValue
//          A pointer to the PIB attribute. Note that this data is _copied_ into the PIB.
//
//  RETURN VALUE:
//      MAC_ENUM
//          SUCCESS or UNSUPPORTED_ATTRIBUTE
//-------------------------------------------------------------------------------------------------------
ROOT MAC_ENUM mlmeGetRequest(MAC_PIB_ATTR pibAttribute, void *pPibAttributeValue) {
    switch (pibAttribute) {

    case PHY_TRANSMIT_POWER:        *((BYTE  *) pPibAttributeValue) = ppib.phyTransmitPower; break;
    // Category 1 attributes (read-only)
    case MAC_ACK_WAIT_DURATION:     *((BYTE  *) pPibAttributeValue) = mpib.macAckWaitDuration; break;
    case MAC_BATT_LIFE_EXT_PERIODS: *((BYTE  *) pPibAttributeValue) = mpib.macBattLifeExtPeriods; break;
    case MAC_GTS_PERMIT:            *((BOOL  *) pPibAttributeValue) = mpib.macGTSPermit; break;
    case MAC_PROMISCUOUS_MODE:      *((BOOL  *) pPibAttributeValue) = mpib.macPromiscuousMode; break;

    // Category 2 attributes (direct write)
    case MAC_ASSOCIATION_PERMIT:    *((BOOL  *) pPibAttributeValue) = mpib.macAssociationPermit; break;
    case MAC_AUTO_REQUEST:          *((BOOL  *) pPibAttributeValue) = mpib.macAutoRequest; break;
    case MAC_BEACON_PAYLOAD_LENGTH: *((BYTE  *) pPibAttributeValue) = mpib.macBeaconPayloadLength; break;
    case MAC_BSN:                   *((BYTE  *) pPibAttributeValue) = mpib.macBSN; break;
    case MAC_DSN:                   *((BYTE  *) pPibAttributeValue) = mpib.macDSN; break;
    case MAC_MAX_CSMA_BACKOFFS:     *((BYTE  *) pPibAttributeValue) = mpib.macMaxCsmaBackoffs; break;
    case MAC_MIN_BE:                *((BYTE  *) pPibAttributeValue) = mpib.macMinBE; break;

    // Category 3 attributes (disable interrupts)
    case MAC_BEACON_PAYLOAD:
        DISABLE_GLOBAL_INT();
        *((BYTE   **) pPibAttributeValue) = mpib.pMacBeaconPayload;
        ENABLE_GLOBAL_INT();
        break;
    case MAC_BEACON_TX_TIME:
        DISABLE_GLOBAL_INT();
        memcpy(pPibAttributeValue, &mpib.macBeaconTxTime, sizeof(UINT32));
        ENABLE_GLOBAL_INT();
        break;
    case MAC_COORD_EXTENDED_ADDRESS:
        DISABLE_GLOBAL_INT();
        memcpy(pPibAttributeValue, mpib.pMacCoordExtendedAddress, sizeof(ADDRESS));
        ENABLE_GLOBAL_INT();
        break;
    case MAC_COORD_SHORT_ADDRESS:
        DISABLE_GLOBAL_INT();
        *((WORD   *) pPibAttributeValue) = mpib.macCoordShortAddress;
        ENABLE_GLOBAL_INT();
        break;
    case MAC_PAN_ID:
        DISABLE_GLOBAL_INT();
        *((WORD   *) pPibAttributeValue) = mpib.macPANId;
        ENABLE_GLOBAL_INT();
        break;
    case MAC_SHORT_ADDRESS:
        DISABLE_GLOBAL_INT();
        *((WORD   *) pPibAttributeValue) = mpib.macShortAddress;
        ENABLE_GLOBAL_INT();
        break;
    case MAC_TRANSACTION_PERSISTENCE_TIME:
        DISABLE_GLOBAL_INT();
        *((WORD   *) pPibAttributeValue) = mpib.macTransactionPersistenceTime;
        ENABLE_GLOBAL_INT();
        break;
    case PHY_CURRENT_CHANNEL:
        DISABLE_GLOBAL_INT();
        *((BYTE*) pPibAttributeValue) = ppib.phyCurrentChannel;
        ENABLE_GLOBAL_INT();
        break;

    // Category 4 attributes (buffered until beacon transmission / handled carefully)
    case MAC_BATT_LIFE_EXT:
        DISABLE_GLOBAL_INT();
        if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_BATT_LIFE_EXT_BM) {
            *((BOOL   *) pPibAttributeValue) = macInfo.pibTempBuffer.macBattLifeExt;
        } else {
            *((BOOL   *) pPibAttributeValue) = mpib.macBattLifeExt;
        }
        ENABLE_GLOBAL_INT();
        break;
    case MAC_BEACON_ORDER:
        DISABLE_GLOBAL_INT();
        if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_BEACON_ORDER_BM) {
            *((BOOL   *) pPibAttributeValue) = macInfo.pibTempBuffer.macBeaconOrder;
        } else {
            *((BOOL   *) pPibAttributeValue) = mpib.macBeaconOrder;
        }
        ENABLE_GLOBAL_INT();
        break;
    case MAC_RX_ON_WHEN_IDLE:
        DISABLE_GLOBAL_INT();
        if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_RX_ON_WHEN_IDLE_BM) {
            *((BOOL   *) pPibAttributeValue) = macInfo.pibTempBuffer.macRxOnWhenIdle;
        } else {
            *((BOOL   *) pPibAttributeValue) = mpib.macRxOnWhenIdle;
        }
        ENABLE_GLOBAL_INT();
        break;
    case MAC_SUPERFRAME_ORDER:
        DISABLE_GLOBAL_INT();
        if (macInfo.pibTempBuffer.updateMask & MPIB_UPD_SUPERFRAME_ORDER_BM) {
            *((BOOL   *) pPibAttributeValue) = macInfo.pibTempBuffer.macSuperframeOrder;
        } else {
            *((BOOL   *) pPibAttributeValue) = mpib.macSuperframeOrder;
        }
        ENABLE_GLOBAL_INT();
        break;

    // TBD:
#if MAC_OPT_ACL_SIZE>0
    case MAC_ACL_ENTRY_DESCRIPTOR_SET:              *((ACL_ENTRY_SET   **) pPibAttributeValue) = mpib.ppMacACLEntryDescriptorSet; break;
    case MAC_ACL_ENTRY_DESCRIPTOR_SETSIZE:          *((BYTE   *) pPibAttributeValue)   = mpib.macACLEntryDescriptorSetSize; break;
#endif

#if MAC_OPT_SECURITY
    case MAC_DEFAULT_SECURITY:                      *((BOOL   *) pPibAttributeValue)   = mpib.macDefaultSecurity; break;
    case MAC_DEFAULT_SECURITY_MATERIAL_LENGTH:      *((BYTE   *) pPibAttributeValue)   = mpib.macDefaultSecurityMaterialLength; break;
    case MAC_DEFAULT_SECURITY_MATERIAL:             *((SECURITY_MATERIAL   **) pPibAttributeValue) = mpib.pMacDefaultSecurityMaterial; break;
    case MAC_DEFAULT_SECURITY_SUITE:                *((BYTE   *) pPibAttributeValue)   = mpib.macDefaultSecuritySuite; break;
#endif

#if ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
    case MAC_SECURITY_MODE:                         *((BYTE   *) pPibAttributeValue)   = mpib.macSecurityMode; break;
#endif
    default:                                        return UNSUPPORTED_ATTRIBUTE;
    }
    return SUCCESS;
} // mlmeGetRequest





//-------------------------------------------------------------------------------------------------------
//  void mlmeAssociateRequest(UINT8 logicalChannel, BYTE coordAddrMode, WORD coordPANId, ...)
//
//  DESCRIPTION:
//      Generates an association request command frame, transmitted to the coordinator using direct
//      transmission. The response is polled automatically from the coordinator.
//      NOTE: Please note that the PAN ID and the coordinator address (short or extended) are set by the
//      the MAC layer, according to the IEEE 802.15.4 spec.
//
//  PARAMETERS:
//      UINT8 logicalChannel
//          Channel number (0x0B - 0x1A)
//      BYTE coordAddrMode
//          AM_SHORT_16 or AM_EXTENDED_64
//      WORD coordPANId
//          The coordinator PAN identifier
//      ADDRESS *pCoordAddress
//          Pointer to the short or extended address of the coordinator
//      BYTE capabilityInformation
//          (CI_ALTERNATE_PAN_COORD_BM | CI_DEVICE_TYPE_IS_FFD_BM | CI_POWER_SOURCE_BM |
//           CI_RX_ON_WHEN_IDLE_BM | CI_SECURITY_CAPABILITY_BM | CI_ALLOCATE_ADDRESS_BM)
//      BOOL securityEnable
//          Security is enabled?
//-------------------------------------------------------------------------------------------------------
ROOT void mlmeAssociateRequest(UINT8 logicalChannel, BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, BYTE capabilityInformation, BOOL securityEnable) {
    BYTE txOptions;
    MAC_TX_PACKET   *pPacket;
    UINT8 temp;
    BYTE   *pPayload;
    BYTE taskNumber;

    // Try to change the MAC state...
    if (!macSetState(MAC_STATE_TX_ASSOC_REQUEST)) return;

    // Check for invalid parameters (channel and coordinator address mode)
    if ((!msupChannelValid(logicalChannel)) || ((coordAddrMode != AM_SHORT_16) && (coordAddrMode != AM_EXTENDED_64))) {
        mlmeAssociateConfirm(0xFFFF, INVALID_PARAMETER);
        return;
    }

    // Update phyCurrentChannel in CC2430 and the PIB (could possibly mess up a transmission, but we don't care...)
    msupSetChannel(logicalChannel, TRUE);

    // Update the PAN ID and the coordinator address
    mlmeSetRequest(MAC_PAN_ID, &coordPANId);
    if (coordAddrMode == AM_SHORT_16) {
        mpib.macCoordShortAddress = pCoordAddress->Short;
    } else {
        mpib.macCoordShortAddress = 0xFFFE; // indicates that the short address is invalid, and that the extended address should be used
        memcpy(mpib.pMacCoordExtendedAddress, pCoordAddress, sizeof(ADDRESS));
    }

    // Reserve a packet to use with the TX engine
    do {
        pPacket = mtxpReservePacket();
    } while (!pPacket);

    // TX mode
    pPacket->txMode = MTX_MODE_USE_CSMACA_BM;
    pPacket->retriesLeft = aMaxFrameRetries;

    // Set TX options
    txOptions = TX_OPT_ACK_REQ;
#if MAC_OPT_SECURITY
    if (securityEnable) {
        SET_MF(MF_SECURE_ASSOC);
        txOptions |= TX_OPT_SECURITY_ENABLE;
    } else {
        CLEAR_MF(MF_SECURE_ASSOC);
    }
#endif

    // Generate the packet header and optionally find security material
    msupPrepareHeader(pPacket, FT_MAC_COMMAND, (BYTE)((coordAddrMode << 2) | SRC_ADDR_EXT), 0xFFFF, (ADDRESS  *) aExtendedAddress, coordPANId, pCoordAddress, txOptions);

    // Store the command type
    pPacket->commandType = CMD_ASSOCIATION_REQUEST;

    // Command frame identifier
    pPayload = pPacket->pPayload;
    *(pPayload++) = CMD_ASSOCIATION_REQUEST;

#if MAC_OPT_SECURITY
    // #bytes in frame counter + key sequence counter (0 or 5)
    temp = msecProcessSecurityCounters(pPacket, pPayload);

    // In case of frame counter overflow or missing key
    // Generate error with FAILED_SECURITY_CHECK or UNAVAILABLE_KEY
    if (pPacket->securitySuite >= 8) {
        mtxpReleasePacket(pPacket);
        mlmeAssociateConfirm(0xFFFF, pPacket->securitySuite);
        return;
    }

⌨️ 快捷键说明

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