📄 mac.c
字号:
// Set the coordinator flag (used in addition to the PAN coordinator flag)
SET_MF(MF_COORDINATOR);
// Set the MDMCTRL0.PAN_COORDINATOR bit (affects address regocnition)
DISABLE_GLOBAL_INT();
/// FASTSPI_SETREG(CC2420_MDMCTRL0, mdmctrl0);
ENABLE_GLOBAL_INT();
// Other parameters
if (securityEnable) {
SET_MF(MF_BEACON_SECURITY);
} else {
CLEAR_MF(MF_BEACON_SECURITY);
}
// Set battery life extension
mlmeSetRequest(MAC_BATT_LIFE_EXT, &batteryLifeExtension);
// Beacon now?
if (beaconOrder == 15) {
mtimCancelCallback(mbcnExpirePacketsNonBeacon);
mtimSetCallback(mbcnExpirePacketsNonBeacon, aBaseSuperframeDuration / aUnitBackoffPeriod);
} else if (firstBeaconNow) {
SET_MF(MF_TRANSMIT_BEACON);
// Reserve a task number and a TX packet
if (mbcnInfo.txTaskNumber == NO_TASK) mbcnInfo.txTaskNumber = mschReserveTask();
if (mbcnInfo.pTxPacket == NULL) mbcnInfo.pTxPacket = mtxpReservePacket();
// Start transmitting periodical beacons
mbcnTxPeriodicalBeacon();
}
return SUCCESS;
}
} // mlmeStartRequest
#endif //MAC_OPT_FFD
//-------------------------------------------------------------------------------------------------------
// void mlmeSyncRequest(UINT8 logicalChannel, ZBOOL trackBeacon)
//
// DESCRIPTION:
// Switch to the selected channel, locate a single beacon, and start or stop tracking beacons
// (optional).
//
// PARAMETERS:
// UINT8 logicalChannel
// The channel to switch to.
// ZBOOL trackBeacon
// Track beacons if >0.
//-------------------------------------------------------------------------------------------------------
void mlmeSyncRequest(UINT8 logicalChannel, ZBOOL trackBeacon) {
UINT8 taskNumber;
WORD taskData;
// Reserve a task if we are using beacon network.
if (mpib.macBeaconOrder != 15)
{
do {
taskNumber = mschReserveTask();
} while (taskNumber == NO_TASK);
// Create the task that will start the synchronization process
taskData = (((WORD) logicalChannel) << 8) | ((BYTE) trackBeacon);
mschAddTask(taskNumber, MAC_TASK_PRI_HIGH, mbcnSyncToBeacons, taskData);
}
} // mlmeSyncRequest
//-------------------------------------------------------------------------------------------------------
// void mlmePollRequest(BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, ZBOOL ...)
//
// DESCRIPTION:
// Poll indirect data from the coordinator
//
// PARAMETERS:
// BYTE coordAddrMode
// The coordinator address mode (AM_SHORT_16 or AM_EXTENDED_64)
// WORD coordPANId
// The PAN identifier of the coordinator
// ADDRESS *pCoordAddress
// A pointer to the coordinator address (short or extended)
// ZBOOL securityEnable
// Enable security for data-request command frame?
//-------------------------------------------------------------------------------------------------------
void mlmePollRequest(BYTE coordAddrMode, WORD coordPANId, ADDRESS *pCoordAddress, ZBOOL securityEnable) {
MAC_ENUM result;
// Force switch to manual polling mode
while (!macSetState(MAC_STATE_TX_MANUAL_DATA_REQUEST));
// Transmit the data request
do {
result = mipTransmitDataRequest(coordAddrMode, coordPANId, pCoordAddress, securityEnable);
} while (result == RESOURCE_SHORTAGE);
// Clean up if it failed
if (result != SUCCESS) macSetState(MAC_STATE_DEFAULT);
} // mlmePollRequest
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM mlmeScanRequest(BYTE scanType, DWORD scanChannels, UINT8 scanDuration)
//
// DESCRIPTION:
// Scan through the selected channels (energy, active, passive and orphan scanning supported).
// Important:
// - The maximum number of results returned for active and passive scans is
// defined by the MAC_OPT_MAX_PAN_DESCRIPTORS (>= 1) mac option
// - This function will not exit before the scan is completed.
//
// PARAMETERS:
// BYTE scanType
// ENERGY_SCAN, ACTIVE_SCAN, PASSIVE_SCAN or ORPHAN_SCAN
// DWORD scanChannels
// The channel index mask (0x07FFF800 are the legal values for 2.4 GHz channels)
// UINT8 scanDuration
// The scan duration defines the time spent scanning each channel, defined as:
// (aBaseSuperframeDuration * (2 ^^ scanDuration + 1)) symbol periods
// = (60 * 16 * (2^^scanDuration+1)) symbol periods
// E.g., scanning all 16 channels with Scanduration 5 takes 8.11 seconds
// MAC_SCAN_RESULT *pScanResult
// The pointer to the MAC_SCAN_RESULT struct (defined by the higher layer) where
// the MAC sublayer shall store the scan result.
//
// RETURN VALUE:
// MAC_ENUM
// INVALID_PARAMETER, SUCCESS or NO_BEACON
//-------------------------------------------------------------------------------------------------------
MAC_ENUM mlmeScanRequest(BYTE scanType, DWORD scanChannels, UINT8 scanDuration, MAC_SCAN_RESULT *pScanResult) {
//pScanResult->scanType = scanType;
//// Invalid parameters?
//if ((scanType & 0xFC) || ((scanType != ORPHAN_SCAN) && (scanDuration > 14)))
//{
// return INVALID_PARAMETER;
//}
//// Copy the scan parameters
//mscInfo.scanType = scanType; // TBD: Remove this variable
//mscInfo.scanChannels = scanChannels;
//mscInfo.scanDuration = scanDuration;
//mscInfo.pScanResult = pScanResult;
//// Clear the complete flag
//mscInfo.scanStatus = MSC_STATUS_ACTIVE;
//printf("mlmeScanRequest starts!\n");
//OSTaskCreateExt(mscScanProcedure,
// (void *)&mscScanState, //this is the parameter
// (OS_STK *)&mscScanProcedureStk[TASK_STK_SIZE-1],
// TASK_START_PRIO+1,
// TASK_START_PRIO+1,
// (OS_STK *)&mscScanProcedureStk[0],
// TASK_STK_SIZE,
// (void *)0,
// OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
////should wait some semaphore send by 'mscScanProcedure';
//for(;;)
//{
// if(mscInfo.scanStatus == MSC_STATUS_ACTIVE)
// {
// Sleep(1000);
// continue;
// }
// else
// {
// printf("mscInfo.scanStatus changed!\n");
// break;
// }
//}
//printf("mscScanProcedure 'returns'!\n");
//OSTaskDel(TASK_START_PRIO+1);
//printf("mscInfo.pScanResult->resultListSize is :%d!\n",mscInfo.pScanResult->resultListSize);
//if ((mscInfo.pScanResult->resultListSize != 0) || (scanType == ENERGY_SCAN) || ((scanType == ORPHAN_SCAN) && (mscInfo.scanStatus == MSC_STATUS_ORPHAN_REALIGNED)))
//{
// printf("mlmeScanRequest SUCCESS!\n");
// return MAC_SUCCESS;
//}
//else
//{
// printf("mlmeScanRequest NO_BEACON!\n");
// return NO_BEACON;
//}
return MAC_SUCCESS;
} // mlmeScanRequest
//MAC_ENUM mlmeScanRequest(BYTE scanType, DWORD scanChannels, UINT8 scanDuration, MAC_SCAN_RESULT *pScanResult)
//{
//
// if(scanType==ENERGY_SCAN)
// {
//
// pScanResult->resultListSize=4;
// pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[0]=13;
// pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[1]=12;
// pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[2]=16;
// pScanResult->ENERGY_OR_ACTIVE.pEnergyDetectList[3]=15;
//
// pScanResult->unscannedChannels=0x8fff000;
// }
// else if(scanType==ACTIVE_SCAN)
// {
// if(scanChannels==33554432)
// {
// pScanResult->resultListSize=2;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddrMode=AM_SHORT_16;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddress.Short=0x1011;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordPanId=0x0000;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].linkQuality=5;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].logicalChannel=0x12;
//
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddrMode=AM_SHORT_16;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddress.Short=0x1012;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordPanId=0x0001;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].linkQuality=5;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].logicalChannel=0x13;
//
//
// pScanResult->unscannedChannels=0x8fff000;
// }
// else if(scanChannels==67108864)
// {
// pScanResult->resultListSize=3;
// }
// else if(scanChannels==2048)
// {
// pScanResult->resultListSize=4;
// }
// else if(scanChannels==16777216)
// {
// pScanResult->resultListSize=5;
// }
// else if(scanChannels==0x7000800)
// {
// pScanResult->resultListSize=2;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddrMode=AM_SHORT_16;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordAddress.Short=0x1011;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].coordPanId=0x0000;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].linkQuality=5;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[0].logicalChannel=0x12;
//
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddrMode=AM_SHORT_16;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordAddress.Short=0x1012;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].coordPanId=0x0001;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].linkQuality=5;
// pScanResult->ENERGY_OR_ACTIVE.pPANDescriptorList[1].logicalChannel=0x13;
//
// }
// else
// {
// return MAC_NO_DATA ;
// }
//
// }
// else if(pScanResult->scanType==ORPHAN_SCAN)
// {
// printf("\nThe MAC layer is performing ORPHAN_SCAN!\n");
// }
// return MAC_SUCCESS;
//}
//-------------------------------------------------------------------------------------------------------
// void mlmeOrphanResponse(QWORD orphanAddress, WORD shortAddress, ZBOOL associatedMember, ZBOOL ...)
//
// DESCRIPTION:
// Respond to an orphan notification by transmitting a coordinator realignment frame.
//
// PARAMETERS:
// QWORD orphanAddress
// Extended address of the orphaned device
// WORD shortAddress
// The short address of the coordinator
// ZBOOL associatedMember
// This node is associated on this PAN
// Note: mlmeOrphanResponse is ignored if set to FALSE
// ZBOOL securityEnable
// Security is enabled for the coordinator realignment command frame?
//-------------------------------------------------------------------------------------------------------
#if MAC_OPT_FFD
void mlmeOrphanResponse(QWORD orphanAddress, WORD shortAddress, ZBOOL associatedMember, ZBOOL securityEnable) {
MAC_TX_PACKET *pPacket;
UINT8 taskNumber;
// Ignore this response if the associatedMember field is FALSE
if (associatedMember) {
// Reserve a packet to use with the TX engine
do {
pPacket = mtxpReservePacket();
} while (!pPacket);
// Prepare the packet data
mbcnPrepareCoordinatorRealignment(pPacket, &orphanAddress, shortAddress, securityEnable, mpib.macPANId, ppib.phyCurrentChannel);
// Initiate the transmission
do {
taskNumber = mschReserveTask();
} while (taskNumber == NO_TASK);
mschAddTask(taskNumber, MAC_TASK_PRI_LOW, mtxScheduleTransmission, (WORD) pPacket);
}
} // mlmeOrphanResponse
#endif // MAC_OPT_FFD
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM mlmeSetRequest(MAC_PIB_ATTR pibAttribute, void *pPibAttributeValue)
//
// DESCRIPTION:
// Set MAC PIB attribute.
//
// 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
// INVALID_PARAMETER, SUCCESS or UNSUPPORTED_ATTRIBUTE
//-------------------------------------------------------------------------------------------------------
MAC_ENUM mlmeSetRequest(MAC_PIB_ATTR pibAttribute, void *pPibAttributeValue) {
ZBOOL isBeaconEnabledPan = (mpib.macBeaconOrder < 15);
WORD pibAttributeValue;
// Store the word, not always used but saves memory space
pibAttributeValue = *((WORD*) pPibAttributeValue);
switch (pibAttribute) {
#if MAC_OPT_TRANSMIT_POWER
case PHY_TRANSMIT_POWER:
ppib.phyTransmitPower = *((UINT8*)pPibAttributeValue);
break;
#endif
// Category 1 attributes (read-only)
case MAC_ACK_WAIT_DURATION:
if ((UINT8) pibAttributeValue == 54) break;
return INVALID_PARAMETER;
case MAC_BATT_LIFE_EXT_PERIODS:
if ((UINT8) pibAttributeValue == 6) break;
return INVALID_PARAMETER;
case MAC_GTS_PERMIT:
case MAC_PROMISCUOUS_MODE:
if ((ZBOOL) pibAttributeValue == FALSE) break;
return INVALID_PARAMETER;
// Category 2 attributes (direct write)
case MAC_ASSOCIATION_PERMIT:
mpib.macAssociationPermit = (ZBOOL) pibAttributeValue;
break;
case MAC_AUTO_REQUEST:
mpib.macAutoRequest = (ZBOOL) pibAttributeValue;
break;
case MAC_BEACON_PAYLOAD_LENGTH:
if (((UINT8) pibAttributeValue) > aMaxBeaconPayloadLength) {
return INVALID_PARAMETER;
} else {
mpib.macBeaconPayloadLength = (UINT8) pibAttributeValue;
}
break;
case MAC_BSN:
mpib.macBSN = (UINT8) pibAttributeValue;
break;
case MAC_DSN:
mpib.macDSN = (UINT8) pibAttributeValue;
break;
case MAC_MAX_CSMA_BACKOFFS:
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 = (ZBOOL) pibAttributeValue;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -