⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myapp_ex08b.c

📁 This network protcol stack,it is very strong and powerful!
💻 C
📖 第 1 页 / 共 3 页
字号:
     that means we must use our own extended address in all
     communications with the coordinator. Otherwise, we use
     the short address assigned to us. */
  if( (pMsg->msgData.associateCnf.assocShortAddress[0] >= 0xFE) && 
      (pMsg->msgData.associateCnf.assocShortAddress[1] == 0xFF) )
  {
    myAddrMode = gAddrModeLong_c;
    memcpy(myAddress, (void *)aExtendedAddress, 8);
  }
  else
  {
    myAddrMode = gAddrModeShort_c;
    memcpy(myAddress, pMsg->msgData.associateCnf.assocShortAddress, 2);
  }
  
  /* Initialize security settings */
  App_InitSecurity();
}


/******************************************************************************
* 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:
    /* 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);
  uint8_t shortAddr[2] = {0xfe, 0xff};
  uint8_t longAddr[8];
   
  /* 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);
 
  if (coordInfo.coordAddrMode == gAddrModeLong_c) {
    /* Set the long address of the coordinator */
    Msg.msgType = gMlmeSetReq_c;
    Msg.msgData.setReq.pibAttribute = gMacPibAclEntryExtAddress_c;
    Msg.msgData.setReq.pibAttributeValue = coordInfo.coordAddress;
    ret = MSG_Send(NWK_MLME, &Msg);  
    
    /* Set the short address of the coordinator */
    Msg.msgType = gMlmeSetReq_c;
    Msg.msgData.setReq.pibAttribute = gMacPibAclEntryShortAddress_c;
    Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&shortAddr;
    ret = MSG_Send(NWK_MLME, &Msg);      
  }
  else {
    /* Get the long address of the coordinator. This was set by the MAC
       when we did the association procedure */
    Msg.msgType = gMlmeGetReq_c;
    Msg.msgData.setReq.pibAttribute = gMacPibCoordExtendedAddress_c;
    Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&longAddr;
    ret = MSG_Send(NWK_MLME, &Msg);  
       
    /* Set the long address of the coordinator */
    Msg.msgType = gMlmeSetReq_c;
    Msg.msgData.setReq.pibAttribute = gMacPibAclEntryExtAddress_c;
    Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&longAddr;
    ret = MSG_Send(NWK_MLME, &Msg);  
    
    /* Set the short address of the coordinator */
    Msg.msgType = gMlmeSetReq_c;
    Msg.msgData.setReq.pibAttribute = gMacPibAclEntryShortAddress_c;
    Msg.msgData.setReq.pibAttributeValue = (uint8_t *)&(coordInfo.coordAddress);
    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 *)coordInfo.coordPanId;
  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 coordinator information gained during the Passive Scan,
* and the short address assigned to us by coordinator, 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 coordinator information gained during 
         the scan procedure. Also use the short address we were assigned
         by the coordinator during association. */
      memcpy(pPacket->msgData.dataReq.dstAddr, coordInfo.coordAddress, 8);
      memcpy(pPacket->msgData.dataReq.srcAddr, myAddress, 8);
      memcpy(pPacket->msgData.dataReq.dstPanId, coordInfo.coordPanId, 2);
      memcpy(pPacket->msgData.dataReq.srcPanId, coordInfo.coordPanId, 2);
      pPacket->msgData.dataReq.dstAddrMode = coordInfo.coordAddrMode;
      pPacket->msgData.dataReq.srcAddrMode = myAddrMode;
      pPacket->msgData.dataReq.msduLength = msduLength;
      /* Request MAC level acknowledgement of the data packet */
      pPacket->msgData.dataReq.txOptions = gTxOptsAck_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 + -