📄 myapp_ex08a.c
字号:
/* Send the Associate Response to the MLME. */
if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
{
Uart_Print("Done\n");
return errorNoError;
}
else
{
/* One or more parameters in the message were invalid. */
Uart_Print("Invalid parameter!\n");
return errorInvalidParameter;
}
}
else
{
/* Allocation of a message buffer failed. */
Uart_Print("Message allocation failed!\n");
return errorAllocFailed;
}
}
/******************************************************************************
* The App_HandleMlmeInput(nwkMessage_t *pMsg) function will handle various
* messages from the MLME, e.g. (Dis)Associate Indication.
*
* The function may return either of the following values:
* errorNoError: The message was processed.
* errorNoMessage: The message pointer is NULL.
******************************************************************************/
uint8_t App_HandleMlmeInput(nwkMessage_t *pMsg)
{
if(pMsg == NULL)
return errorNoMessage;
/* Handle the incoming message. The type determines the sort of processing.*/
switch(pMsg->msgType) {
case gNwkAssociateInd_c:
Uart_Print("Received an MLME-Associate Indication from the MAC\n");
/* A device sent us an Associate Request. We must send back a response. */
return App_SendAssociateResponse(pMsg);
break;
case gNwkCommStatusInd_c:
/* Sent by the MLME after the Association Response has been transmitted. */
Uart_Print("Received an MLME-Comm-Status Indication from the MAC\n");
break;
}
return errorNoError;
}
/******************************************************************************
* The App_HandleMcpsInput(mcpsToNwkMessage_t *pMsgIn) function will handle
* messages from the MCPS, e.g. Data Confirm, and Data Indication.
*
******************************************************************************/
void App_HandleMcpsInput(mcpsToNwkMessage_t *pMsgIn)
{
switch(pMsgIn->msgType)
{
/* The MCPS-Data confirm is sent by the MAC to the network
or application layer when data has been sent. */
case gMcpsDataCnf_c:
if(numPendingPackets)
numPendingPackets--;
break;
case gMcpsDataInd_c:
/* The MCPS-Data indication is sent by the MAC to the network
or application layer when data has been received. We simply
copy the received data to the UART. */
Uart_Tx(pMsgIn->msgData.dataInd.msdu, pMsgIn->msgData.dataInd.msduLength);
break;
}
}
/******************************************************************************
* The App_InitSecurity(void) function will initialize
* security in the coordinator
*
******************************************************************************/
void App_InitSecurity(void)
{
mlmeMessage_t Msg;
uint8_t aclEntry = 0; /* We have one device so we'll only fill out the first ACL entry */
uint8_t ret;
uint8_t securityMaterial[26] = { /* The key that must be common to both sides */
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
/* Security material counters - must be initialized like this
They are used for checking repeated packets and assigning
packet counters */
1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t securityMaterialLength = sizeof(securityMaterial);
/* We will only apply security to one device and fill out the security
information for this device. The first device will always have ACL
entry #0. The next will have #1 etc. up to #7 */
/* Tell the MAC what ACL entry we are working with */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntryCurrent_c;
Msg.msgData.setReq.pibAttributeValue = &aclEntry;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set the long address of the device */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntryExtAddress_c;
Msg.msgData.setReq.pibAttributeValue = deviceLongAddress;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set the short address of the device */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntryShortAddress_c;
Msg.msgData.setReq.pibAttributeValue = deviceShortAddress;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set the PAN id that we use with this device */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntryPanId_c;
Msg.msgData.setReq.pibAttributeValue = (uint8_t *)panId;
ret = MSG_Send(NWK_MLME, &Msg);
/* Tell how much security material that we have got */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntrySecurityMaterialLength_c;
Msg.msgData.setReq.pibAttributeValue = &securityMaterialLength;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set security material */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntrySecurityMaterial_c;
Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&securityMaterial;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set the security suite/level */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibAclEntrySecuritySuite_c;
Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&securityLevel;
ret = MSG_Send(NWK_MLME, &Msg);
/* Set the security mode */
Msg.msgType = gMlmeSetReq_c;
Msg.msgData.setReq.pibAttribute = gMacPibSecurityMode_c;
Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&securityMode;
ret = MSG_Send(NWK_MLME, &Msg);
Uart_Print("Security was successfully applied.\n\n");
return;
}
/******************************************************************************
* The App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType) function does not, as
* the name implies, wait for a message, thus blocking the execution of the
* state machine. Instead the function analyzes the supplied message to determine
* whether or not the message is of the expected type.
* The function may return either of the following values:
* errorNoError: The message was of the expected type.
* errorNoMessage: The message pointer is NULL.
* errorWrongConfirm: The message is not of the expected type.
*
******************************************************************************/
uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType)
{
/* Do we have a message? If not, the exit with error code */
if(pMsg == NULL)
return errorNoMessage;
/* Is it the expected message type? If not then exit with error code */
if(pMsg->msgType != msgType)
return errorWrongConfirm;
/* Found the expected message. Return with success code */
return errorNoError;
}
/******************************************************************************
* The App_TransmitUartData() function will perform (single/multi buffered)
* data transmissions of data received by the UART. Data could also come from
* other sources such as sensors etc. This is completely determined by the
* application. The constant MAX_PENDING_DATA_PACKETS determine the maximum
* number of packets pending for transmission in the MAC. A global variable
* is incremented each time a data packet is sent to the MCPS, and decremented
* when the corresponding MCPS-Data Confirm message is received. If the counter
* reaches the defined maximum no more data buffers are allocated until the
* counter is decreased below the maximum number of pending packets.
*
* The function uses the device information, that was stored when the device,
* associated to us, for building an MCPS-Data Request message. The message
* is sent to the MCPS service access point in the MAC.
******************************************************************************/
void App_TransmitUartData(void)
{
/* Use multi buffering for increased TX performance. It does not really
have any effect at a UART baud rate of 19200bps but serves as an
example of how the throughput may be improved in a real-world
application where the data rate is of concern. */
if( (numPendingPackets < MAX_PENDING_DATA_PACKETS) && (pPacket == NULL) )
{
/* If the maximum number of pending data buffes is below maximum limit
and we do not have a data buffer already then allocate one. */
pPacket = MSG_Alloc(sizeof(nwkToMcpsMessage_t) - 1 + DEFAULT_DATA_LENGTH);
}
if(pPacket != NULL)
{
/* If we have a buffer, then get data from the UART. */
uint8_t msduLength = Uart_Poll(pPacket->msgData.dataReq.msdu);
if(msduLength)
{
/* Data was available in the UART receive buffer. Now create an
MCPS-Data Request message containing the UART data. */
pPacket->msgType = gMcpsDataReq_c;
/* Create the header using device information stored when creating
the association response. In this simple example the use of short
addresses is hardcoded. In a real world application we must be
flexible, and use the address mode required by the given situation. */
memcpy(pPacket->msgData.dataReq.dstAddr, deviceShortAddress, 2);
memcpy(pPacket->msgData.dataReq.srcAddr, (void *)shortAddress, 2);
memcpy(pPacket->msgData.dataReq.dstPanId, (void *)panId, 2);
memcpy(pPacket->msgData.dataReq.srcPanId, (void *)panId, 2);
pPacket->msgData.dataReq.dstAddrMode = gAddrModeShort_c;
pPacket->msgData.dataReq.srcAddrMode = gAddrModeShort_c;
pPacket->msgData.dataReq.msduLength = msduLength;
/* Request MAC level acknowledgement, and
indirect transmission of the data packet */
pPacket->msgData.dataReq.txOptions = gTxOptsAck_c | gTxOptsIndirect_c | gTxOptsSecurity_c;
/* Give the data packet a handle. The handle is
returned in the MCPS-Data Confirm message. */
pPacket->msgData.dataReq.msduHandle = msduHandle++;
/* Send the Data Request to the MCPS */
NR MSG_Send(NWK_MCPS, pPacket);
/* Prepare for another data buffer */
pPacket = NULL;
numPendingPackets++;
}
}
}
/******************************************************************************
* The following functions are called by the MAC to put messages into the
* Application's queue. They need to be defined even if they are not used
* in order to avoid linker errors.
******************************************************************************/
uint8_t MLME_NWK_SapHandler(nwkMessage_t * pMsg)
{
/* Put the incoming MLME message in the applications input queue. */
MSG_Queue(&mMlmeNwkInputQueue, pMsg);
return gSuccess_c;
}
uint8_t MCPS_NWK_SapHandler(mcpsToNwkMessage_t *pMsg)
{
/* Put the incoming MCPS message in the applications input queue. */
MSG_Queue(&mMcpsNwkInputQueue, pMsg);
return gSuccess_c;
}
uint8_t ASP_APP_SapHandler(aspToAppMsg_t *pMsg)
{
/* If the message is not handled anywhere it must be freed. */
MSG_Free(pMsg);
return gSuccess_c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -