📄 mac.c
字号:
if (((UINT8) pibAttributeValue) > 5) {
return INVALID_PARAMETER;
} else {
mpib.macMaxCsmaBackoffs = (UINT8) pibAttributeValue;
}
break;
case MAC_MIN_BE:
if (((UINT8) pibAttributeValue) > 3) {
return INVALID_PARAMETER;
} else {
mpib.macMinBE = (UINT8) pibAttributeValue;
}
break;
case MAC_ASSOCIATED_PAN_CORDINATOR:
mpib.macAssocatedPanCordinator = (BOOL) pibAttributeValue;
break;
// Category 3 attributes (disable interrupts)
case MAC_BEACON_PAYLOAD:
DISABLE_GLOBAL_INT();
mpib.pMacBeaconPayload = (BYTE*) pPibAttributeValue;
ENABLE_GLOBAL_INT();
break;
case MAC_BEACON_TX_TIME:
DISABLE_GLOBAL_INT();
memcpy(&mpib.macBeaconTxTime, (UINT32*) pPibAttributeValue, sizeof(UINT32));
ENABLE_GLOBAL_INT();
break;
case MAC_COORD_EXTENDED_ADDRESS:
DISABLE_GLOBAL_INT();
memcpy(&mpib.macCoordExtendedAddress, (QWORD*) pPibAttributeValue, sizeof(QWORD));
ENABLE_GLOBAL_INT();
break;
case MAC_COORD_SHORT_ADDRESS:
DISABLE_GLOBAL_INT();
mpib.macCoordShortAddress = pibAttributeValue;
ENABLE_GLOBAL_INT();
break;
case MAC_PAN_ID:
DISABLE_GLOBAL_INT();
mpib.macPANId = pibAttributeValue;
msupWriteRam((BYTE*) &mpib.macPANId, CC2420RAM_PANID, 2, FALSE);
ENABLE_GLOBAL_INT();
break;
case MAC_SHORT_ADDRESS:
DISABLE_GLOBAL_INT();
mpib.macShortAddress = pibAttributeValue;
msupWriteRam((BYTE*) &mpib.macShortAddress, CC2420RAM_SHORTADDR, 2, FALSE);
ENABLE_GLOBAL_INT();
break;
case MAC_TRANSACTION_PERSISTENCE_TIME:
DISABLE_GLOBAL_INT();
mpib.macTransactionPersistenceTime = pibAttributeValue;
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) pibAttributeValue;
macInfo.pibTempBuffer.updateMask |= MPIB_UPD_BATT_LIFE_EXT_BM;
} else {
mpib.macBattLifeExt = (BOOL) pibAttributeValue;
}
ENABLE_GLOBAL_INT();
break;
case MAC_BEACON_ORDER:
DISABLE_GLOBAL_INT();
if (((UINT8) pibAttributeValue) > 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) pibAttributeValue == 15) {
if (mtimCancelCallback(mbcnTxPeriodicalBeacon)) mbcnUpdateBufferedPibAttributes();
}
#endif
macInfo.pibTempBuffer.macBeaconOrder = (UINT8) pibAttributeValue;
macInfo.pibTempBuffer.updateMask |= MPIB_UPD_BEACON_ORDER_BM;
} else {
mpib.macBeaconOrder = (UINT8) pibAttributeValue;
}
// 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) pibAttributeValue;
macInfo.pibTempBuffer.updateMask |= MPIB_UPD_RX_ON_WHEN_IDLE_BM;
} else {
if (!mpib.macRxOnWhenIdle && (BOOL) pibAttributeValue) {
mrxIncrOnCounter();
} else if (mpib.macRxOnWhenIdle && !((BOOL) pibAttributeValue)) {
mrxDecrOnCounter();
}
mpib.macRxOnWhenIdle = (BOOL) pibAttributeValue;
}
ENABLE_GLOBAL_INT();
break;
case MAC_SUPERFRAME_ORDER:
DISABLE_GLOBAL_INT();
if (((UINT8) pibAttributeValue) > 15) {
return INVALID_PARAMETER;
} else if (isBeaconEnabledPan && (GET_MF(MF_TRANSMIT_BEACON) || mbcnInfo.trackBeacon)) {
macInfo.pibTempBuffer.macSuperframeOrder = (UINT8) pibAttributeValue;
macInfo.pibTempBuffer.updateMask |= MPIB_UPD_SUPERFRAME_ORDER_BM;
} else {
mpib.macSuperframeOrder = (UINT8) pibAttributeValue;
}
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) pibAttributeValue;
break;
#endif
#if MAC_OPT_SECURITY
case MAC_DEFAULT_SECURITY:
mpib.macDefaultSecurity = (BYTE) pibAttributeValue;
break;
case MAC_DEFAULT_SECURITY_MATERIAL_LENGTH:
mpib.macDefaultSecurityMaterialLength = (BYTE) pibAttributeValue;
break;
case MAC_DEFAULT_SECURITY_MATERIAL:
mpib.pMacDefaultSecurityMaterial = (SECURITY_MATERIAL*) pPibAttributeValue;
break;
case MAC_DEFAULT_SECURITY_SUITE:
mpib.macDefaultSecuritySuite = (BYTE) pibAttributeValue;
break;
#endif
#if ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
case MAC_SECURITY_MODE:
mpib.macSecurityMode = (BYTE) pibAttributeValue;
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
//-------------------------------------------------------------------------------------------------------
MAC_ENUM mlmeGetRequest(MAC_PIB_ATTR pibAttribute, void *pPibAttributeValue) {
switch (pibAttribute) {
#if MAC_OPT_TRANSMIT_POWER
case PHY_TRANSMIT_POWER: *((BYTE*) pPibAttributeValue) = ppib.phyTransmitPower; break;
#endif
// 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;
case MAC_ASSOCIATED_PAN_CORDINATOR: *((BOOL*) pPibAttributeValue) = mpib.macAssocatedPanCordinator; 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.macCoordExtendedAddress, sizeof(QWORD));
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;
// 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?
//-------------------------------------------------------------------------------------------------------
void mlmeAssociateRequest(UINT8 logicalChannel, BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, BYTE capabilityInformation, BOOL securityEnable) {
BYTE txOptions;
UINT8 taskNumber;
MAC_TX_PACKET *pPacket;
UINT8 temp;
BYTE *pPayload;
// 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 CC2420 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
mpib.macCoordExtendedAddress = pCoordAddress->Extended;
}
// 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, (coordAddrMode << 2) | SRC_ADDR_EXT, 0xFFFF, (ADDRESS*) &aExtendedAddress, coordPANId, pCoordAddress, txOptions);
// Store the command type
pPacket->mc.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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -