📄 mac.c
字号:
pPacket->duration = msupCalcPacketDuration(pPacket->length, TRUE);
printf("the pPacket->duration is %d!\n",pPacket->duration );
//OSTaskCreateExt(mtxScheduleTransmission,
// (void *)&pPacket, //this is the parameter
// (OS_STK *)&mtxScheduleTransmissionStk[TASK_STK_SIZE-1],
// TASK_START_PRIO+20,
// TASK_START_PRIO+20,
//(OS_STK *)&mtxScheduleTransmissionStk[0],
//TASK_STK_SIZE,
//(void *)0,
//OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
printf("mlmeAssociateRequest is called!\n");
} // mlmeAssociateRequest
//-------------------------------------------------------------------------------------------------------
// void mlmeAssociateResponse(ADDRESS *pDeviceAddress, WORD assocShortAddress, MAC_ENUM status, ...
//
// DESCRIPTION:
// Used by a ccordinator to respond to an association indication. The response is placed in the
// indirect transmission queue.
// Generates a mlmeCommStatusIndication callback upon completion
//
// PARAMETERS:
// ADDRESS *pDeviceAddress
// Pointer to the extended address of the associated device
// WORD assocShortAddress
// The assigned short address
// MAC_ENUM status
// The association status
// ZBOOL securityEnable
// Security is enabled?
//-------------------------------------------------------------------------------------------------------
#if MAC_OPT_FFD
void mlmeAssociateResponse(ADDRESS *pDeviceAddress, WORD assocShortAddress, MAC_ENUM status, ZBOOL securityEnable) {
MAC_TX_PACKET *pPacket;
BYTE txOptions;
UINT8 taskNumber;
UINT8 temp;
BYTE *pPayload;
MAC_ENUM commStatusError;
// Reserve an indirect packet to use with the TX engine
if (!(pPacket = mtxpReservePacket())) {
commStatusError = TRANSACTION_OVERFLOW;
mlmeCommStatusIndication(mpib.macPANId, AM_EXTENDED_64, (ADDRESS*) &aExtendedAddress, AM_EXTENDED_64, pDeviceAddress, commStatusError);
return;
}
// TX mode
pPacket->txMode = MTX_MODE_USE_CSMACA_BM;
pPacket->retriesLeft = aMaxFrameRetries;
// Set TX options
txOptions = TX_OPT_ACK_REQ | TX_OPT_INDIRECT;
#if MAC_OPT_SECURITY
if (securityEnable) txOptions |= TX_OPT_SECURITY_ENABLE;
#endif
// Generate the packet header
msupPrepareHeader(pPacket, FT_MAC_COMMAND, DEST_ADDR_EXT | SRC_ADDR_EXT, mpib.macPANId, (ADDRESS*) &aExtendedAddress, mpib.macPANId, pDeviceAddress, txOptions);
// Store the command type
pPacket->commandType = CMD_ASSOCIATION_RESPONSE;
// Command frame identifier
pPayload = pPacket->pPayload;
*(pPayload++) = CMD_ASSOCIATION_RESPONSE;
#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) {
commStatusError = pPacket->securitySuite;
mtxpReleasePacket(pPacket);
mlmeCommStatusIndication(mpib.macPANId, AM_EXTENDED_64, (ADDRESS*) &aExtendedAddress, AM_EXTENDED_64, pDeviceAddress, commStatusError);
return;
}
// Increment payload pointer when counters inserted
pPayload += temp;
// Include command payload length and optional MIC (integrity code) length
temp += CMD_ASSOCIATION_RESPONSE_PAYLOAD_LENGTH + pPacket->securitySetup.micLength;
#else
// No security material included, set MAC payload length
temp = CMD_ASSOCIATION_RESPONSE_PAYLOAD_LENGTH;
#endif
// Set the packet length (four bytes of payload)
pPacket->length = pPacket->headerLength + temp + MAC_FCS_LENGTH;
// Generate the payload
*(pPayload++) = (BYTE) assocShortAddress;
*(pPayload++) = (BYTE) ((assocShortAddress & 0xFF00) >> 8);
*(pPayload++) = (BYTE) status;
// Calculate the packet duration
pPacket->duration = msupCalcPacketDuration(pPacket->length, TRUE);
// Initiate the transmission
do {
taskNumber = mschReserveTask();
} while (taskNumber == NO_TASK);
mschAddTask(taskNumber, MAC_TASK_PRI_MEDIUM, miqAddIndirectPacket, (WORD) pPacket);
} // mlmeAssociateResponse
#endif // MAC_OPT_FFD
//-------------------------------------------------------------------------------------------------------
// void mlmeDisassociateRequest(QWORD *pDeviceAddress, BYTE disassociateReason, ZBOOL securityEnable)
//
// DESCRIPTION:
// Used by an associated device to notify the coordinator of its intent to leave the PAN or
// used by the coordinator to instruct an associated device to leave the PAN. pDeviceAddress is a
// pointer to the extended address of the device to which to send the disassociation notification
// command.
//
// PARAMETERS:
// QWORD *pDeviceAddress
// For coordinators: A pointer to the extended address of the device to disassociate
// For devices: A pointer to the extended address of coordinator
// BYTE disassociateReason
// The disassociate reason (COORD_WISHES_DEVICE_TO_LEAVE | DEVICE_WISHES_TO_LEAVE)
// ZBOOL securityEnable
// Security is enabled?
//-------------------------------------------------------------------------------------------------------
void mlmeDisassociateRequest(QWORD *pDeviceAddress, BYTE disassociateReason, ZBOOL securityEnable) {
MAC_TX_PACKET *pPacket;
UINT8 taskNumber;
BYTE txOptions;
UINT8 temp;
BYTE *pPayload;
// Set TX options
txOptions = TX_OPT_ACK_REQ;
printf("mlmeDisassociateRequest starts!\n");
#if MAC_OPT_SECURITY
if (securityEnable) {
txOptions |= TX_OPT_SECURITY_ENABLE;
}
#endif
// Device -> Coordinator: Use direct transmission
if (msupCompareQword(&mpib.macCoordExtendedAddress, pDeviceAddress)) {
// Reserve the packet to be used
do {
pPacket = mtxpReservePacket();
} while (pPacket == NULL);
} else {
#if MAC_OPT_FFD
// Coordinator -> Device: Use indirect transmission
if (GET_MF(MF_COORDINATOR)) {
// Start by trying to reserve a TX packet
pPacket = mtxpReservePacket();
if (!pPacket) {
mlmeDisassociateConfirm(TRANSACTION_OVERFLOW);
return;
}
// Set the indirect transmission flag
txOptions |= TX_OPT_INDIRECT;
// Device -> Device: Error
} else {
mlmeDisassociateConfirm(INVALID_PARAMETER);
return;
}
#else // RFD
mlmeDisassociateConfirm(INVALID_PARAMETER);
return;
#endif
}
// Set transmission mode
pPacket->txMode = MTX_MODE_USE_CSMACA_BM;
pPacket->retriesLeft = aMaxFrameRetries;
// Generate the packet header
msupPrepareHeader(pPacket, FT_MAC_COMMAND, (DEST_ADDR_EXT | SRC_ADDR_EXT), mpib.macPANId, (ADDRESS*) &aExtendedAddress, mpib.macPANId, (ADDRESS*) pDeviceAddress, txOptions);
// Store the command type
pPacket->commandType = CMD_DISASSOCIATION_NOTIFICATION;
// Command frame identifier
pPayload = pPacket->pPayload;
*(pPayload++) = CMD_DISASSOCIATION_NOTIFICATION;
#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);
mlmeDisassociateConfirm(pPacket->securitySuite);
return;
}
// Increment payload pointer when counters inserted
pPayload += temp;
// Include command payload length and optional MIC (integrity code) length
temp += CMD_DISASSOCIATION_NOTIFICATION_PAYLOAD_LENGTH + pPacket->securitySetup.micLength;
#else
// No security material included, set MAC payload length
temp = CMD_DISASSOCIATION_NOTIFICATION_PAYLOAD_LENGTH;
#endif
// Set the packet length (two bytes of payload)
pPacket->length = pPacket->headerLength + temp + MAC_FCS_LENGTH;
// Generate the payload
*(pPayload++) = (BYTE) disassociateReason;
// Calculate the packet duration
pPacket->duration = msupCalcPacketDuration(pPacket->length, TRUE);
// Initiate the transmission
//do {
// taskNumber = mschReserveTask();
//} while (taskNumber == NO_TASK);
//if (txOptions & TX_OPT_INDIRECT) {
// mschAddTask(taskNumber, MAC_TASK_PRI_MEDIUM, miqAddIndirectPacket, (WORD) pPacket);
//} else {
// mschAddTask(taskNumber, MAC_TASK_PRI_LOW, mtxScheduleTransmission, (WORD) pPacket);
//}
if (txOptions & TX_OPT_INDIRECT)
{
// mschAddTask(taskNumber, MAC_TASK_PRI_MEDIUM, miqAddIndirectPacket, (WORD) pPacket);
OSTaskCreateExt(miqAddIndirectPacket,
(void *)&pPacket,
(OS_STK *)&miqAddIndirectPacketStk[TASK_STK_SIZE-1],
TASK_START_PRIO+11,
TASK_START_PRIO+11,
(OS_STK *)&miqAddIndirectPacketStk[0],
TASK_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
}
else
{
// mschAddTask(taskNumber, MAC_TASK_PRI_LOW, mtxScheduleTransmission, (WORD) pPacket);
OSTaskCreateExt(mtxScheduleTransmission,
(void *)&pPacket, //this is the parameter
(OS_STK *)&mtxScheduleTransmissionStk[TASK_STK_SIZE-1],
TASK_START_PRIO+1,
TASK_START_PRIO+1,
(OS_STK *)&mtxScheduleTransmissionStk[0],
TASK_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
}
} // mlmeDisassociateRequest
//-------------------------------------------------------------------------------------------------------
// void mlmeRxEnableRequest(ZBOOL deferPermit, UINT32 rxOnTime, UINT32 rxOnDuration)
//
// DESCRIPTION:
// Enable the receiver for after a given timeout (in symbols), and turn it off after the given
// duration (also in symbols). An rxOnDuration = 0 will immediately shut down the receiver.
// Note: Do NOT use on beacon networks, set RX_ON_WHEN_IDLE to TRUE in stead
//
// PARAMETERS:
// ZBOOL deferPermit
// Reception can be deferred until the next superframe
// UINT32 rxOnTime
// The number of symbols to elapse before the receiver should be turned on
// UINT32 rxOnDuration
// The number of symbols to listen before turning the receiver off
//-------------------------------------------------------------------------------------------------------
void mlmeRxEnableRequest(ZBOOL deferPermit, UINT32 rxOnTime, UINT32 rxOnDuration) {
// Store the time values for use by the task
mrxInfo.rxEnableOnDuration = (rxOnDuration + (aUnitBackoffPeriod - 1)) / aUnitBackoffPeriod;
while (!mschAddTask(mschReserveTask(), MAC_TASK_PRI_MEDIUM, mrxRxEnableRequestTask, 0));
} // mlmeRxEnableRequest
//-------------------------------------------------------------------------------------------------------
// MAC_ENUM mcpsPurgeRequest(BYTE msduHandle)
//
// DESCRIPTION:
// Purge data frames from the indirect data transmission queue
//
// PARAMETERS:
// BYTE msduHandle
// The packet handle (from mcpsDataRequest(...))
//
// RETURN VALUE:
// MAC_ENUM
// SUCCESS: OK
// INVALID_HANDLE: The packet could not be found (already transmitted?)
//-------------------------------------------------------------------------------------------------------
#if MAC_OPT_FFD
MAC_ENUM mcpsPurgeRequest(BYTE msduHandle) {
UINT8 packetIndex;
MAC_TX_PACKET *pIndirectPacket;
// For each packet in the queue ...
restart:
packetIndex = miqInfo.firstIndirectPacket;
while (packetIndex != NO_PACKET) {
pIndirectPacket = &pMtxPacketPool[packetIndex];
// If there's a match on the MSDU handle
if ((pIndirectPacket->msduHandle == msduHandle) && (pIndirectPacket->type == FT_DATA)) {
// Just wait if the packet has been requested (we shouldn't interfere with the transmission)
if (pIndirectPacket->requested) {
goto restart;
} else {
pIndirectPacket->purgeRequest = TRUE;
pIndirectPacket->timeToLive = 0;
mschAddTask(mschReserveTask(), MAC_TASK_PRI_MEDIUM, miqExpireIndirectPacketsTask, (WORD) pIndirectPacket);
return SUCCESS;
}
}
// Move to the next packet in the queue
packetIndex = pIndirectPacket->nextPacket;
}
return INVALID_HANDLE;
} // mcpsPurgeRequest
#endif
/*******************************************************************************************************
* Revision history:
*
* $Log: mac.c,v $
* Revision 1.25 2005/02/03 15:03:10 thl
* added pib attribute MAC_ASSOCIATED_PAN_CORDINATOR.
* used to disable/enable pan conflict message.
*
* Revision 1.24 2005/01/05 10:45:28 thl
* Added full suport for ppib.phyTransmitPower, will also now adjust the transmit power
* of the radio chip. to include set compile switch MAC_OPT_TRANSMIT_POWER=1 in
* make file.
*
* Revision 1.23 2004/12/10 12:41:06 thl
* On a sync request the MAC now check if we have a none beacon network,
* if this is the case a the sync request is canceld.
*
* Revision 1.22 2004/12/07 09:47:49 thl
* Fixed potential coruption of memmory when max payload is recived.
*
* Revision 1.21 2004/11/10 09:33:13 thl
* Fixed a number of bugs according to: MAC software check lists.xls
*
* Revision 1.20 2004/09/02 14:31:57 oyj
* Fixed bug in mcpsPurgeRequest, which caused the pIndirectPacket->type to be set to FT_DATA
* in stead of making comparison as intended.
*
* Revision 1.19 2004/08/31 10:36:07 oyj
* Fixed bug in mcpsPurgeRequest, which caused the pIndirectPacket->type to be set to FT_DATA
* in stead of making comparison as intended.
*
* Revision 1.18 2004/08/13 13:04:45 jol
* CC2420 MAC Release v0.7
*
*
*******************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -