void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt ){ uint8 stat; uint8 seqnb; uint8 delay; switch ( pkt->clusterId ) { // A message with a serial data block to be transmitted on the serial port. case SERIALAPP_CLUSTERID1: // Store the address for sending and retrying. osal_memcpy(&SerialApp_RxAddr, &(pkt->srcAddr), sizeof( afAddrType_t )); seqnb = pkt->cmd.Data[0]; // Keep message if not a repeat packet if ( (seqnb > SerialApp_RxSeq) || // Normal ((seqnb < 0x80 ) && ( SerialApp_RxSeq > 0x80)) ) // Wrap-around { // Transmit the data on the serial port. if ( HalUARTWrite( SERIAL_APP_PORT, pkt->cmd.Data+1, (pkt->cmd.DataLength-1) ) ) { // Save for next incoming message SerialApp_RxSeq = seqnb; stat = OTA_SUCCESS; } else { stat = OTA_SER_BUSY; } } else { stat = OTA_DUP_MSG; } // Select approproiate OTA flow-control delay. delay = (stat == OTA_SER_BUSY) ? SERIALAPP_NAK_DELAY : SERIALAPP_ACK_DELAY; // Build & send OTA response message. SerialApp_RspBuf[0] = stat; SerialApp_RspBuf[1] = seqnb; SerialApp_RspBuf[2] = LO_UINT16( delay ); SerialApp_RspBuf[3] = HI_UINT16( delay ); osal_set_event( SerialApp_TaskID, SERIALAPP_RESP_EVT ); osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_RESP_EVT); break; // A response to a received serial data block. case SERIALAPP_CLUSTERID2: if ((pkt->cmd.Data[1] == SerialApp_TxSeq) && ((pkt->cmd.Data[0] == OTA_SUCCESS) || (pkt->cmd.Data[0] == OTA_DUP_MSG))) { SerialApp_TxLen = 0; osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_EVT); } else { // Re-start timeout according to delay sent from other device. delay = BUILD_UINT16( pkt->cmd.Data[2], pkt->cmd.Data[3] ); osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_EVT, delay ); } break; case SERIALAPP_CONNECTREQ_CLUSTER: SerialApp_ConnectReqProcess((uint8*)pkt->cmd.Data); case SERIALAPP_CONNECTRSP_CLUSTER: SerialApp_DeviceConnectRsp((uint8*)pkt->cmd.Data); default: break; }}每行代码注释
时间: 2024-03-07 09:47:56 浏览: 166
这段代码是一个处理ZigBee串口应用的主函数,主要功能是接收并处理来自ZigBee网络中其他节点发送的消息和命令。以下是每行代码的注释解释:
```C
void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt )
{
uint8 stat;
uint8 seqnb;
uint8 delay;
switch ( pkt->clusterId )
{
// A message with a serial data block to be transmitted on the serial port.
case SERIALAPP_CLUSTERID1:
// Store the address for sending and retrying.
osal_memcpy(&SerialApp_RxAddr, &(pkt->srcAddr), sizeof( afAddrType_t ));
seqnb = pkt->cmd.Data[0];
// Keep message if not a repeat packet
if ( (seqnb > SerialApp_RxSeq) || // Normal
((seqnb < 0x80 ) && ( SerialApp_RxSeq > 0x80)) ) // Wrap-around
{
// Transmit the data on the serial port.
if ( HalUARTWrite( SERIAL_APP_PORT, pkt->cmd.Data+1, (pkt->cmd.DataLength-1) ) )
{
// Save for next incoming message
SerialApp_RxSeq = seqnb;
stat = OTA_SUCCESS;
}
else
{
stat = OTA_SER_BUSY;
}
}
else
{
stat = OTA_DUP_MSG;
}
// Select approproiate OTA flow-control delay.
delay = (stat == OTA_SER_BUSY) ? SERIALAPP_NAK_DELAY : SERIALAPP_ACK_DELAY;
// Build & send OTA response message.
SerialApp_RspBuf[0] = stat;
SerialApp_RspBuf[1] = seqnb;
SerialApp_RspBuf[2] = LO_UINT16( delay );
SerialApp_RspBuf[3] = HI_UINT16( delay );
osal_set_event( SerialApp_TaskID, SERIALAPP_RESP_EVT );
osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_RESP_EVT);
break;
// A response to a received serial data block.
case SERIALAPP_CLUSTERID2:
if ((pkt->cmd.Data[1] == SerialApp_TxSeq) &&
((pkt->cmd.Data[0] == OTA_SUCCESS) || (pkt->cmd.Data[0] == OTA_DUP_MSG)))
{
SerialApp_TxLen = 0;
osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_EVT);
}
else
{
// Re-start timeout according to delay sent from other device.
delay = BUILD_UINT16( pkt->cmd.Data[2], pkt->cmd.Data[3] );
osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_EVT, delay );
}
break;
case SERIALAPP_CONNECTREQ_CLUSTER:
SerialApp_ConnectReqProcess((uint8*)pkt->cmd.Data);
case SERIALAPP_CONNECTRSP_CLUSTER:
SerialApp_DeviceConnectRsp((uint8*)pkt->cmd.Data);
break;
default:
break;
}
}
```
阅读全文