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

📄 myapp_ex08b.c

📁 This network protcol stack,it is very strong and powerful!
💻 C
📖 第 1 页 / 共 3 页
字号:
      {
        pMsgIn = MSG_DeQueue(&mMcpsNwkInputQueue);
        App_HandleMcpsInput(pMsgIn);
        /* ALWAYS free messages from MCPS */
        MSG_Free(pMsgIn);
      }

      /* Check if the UART buffer has data to be sent. */
      App_TransmitUartData();
    }
  
    /* Call the MAC main function. */
    macStatus = Mlme_Main();
  }
}


/******************************************************************************
* The App_StartScan(scanType) function will start the scan process of the
* specified type in the MAC. This is accomplished by allocating a MAC message,
* which is then assigned the desired scan parameters and sent to the MLME
* service access point.
* The function may return either of the following values:
*   errorNoError:          The Scan message was sent successfully.
*   errorInvalidParameter: The MLME service access point rejected the
*                          message due to an invalid parameter.
*   errorAllocFailed:      A message buffer could not be allocated.
*
******************************************************************************/
uint8_t App_StartScan(uint8_t scanType)
{
  mlmeMessage_t *pMsg;
  mlmeScanReq_t *pScanReq;

  Uart_Print("Sending the MLME-Scan Request message to the MAC...");

  /* Allocate a message for the MLME (We should check for NULL). */
  pMsg = MSG_AllocType(mlmeMessage_t);
  if(pMsg != NULL)
  {
    /* This is a MLME-START.req command */
    pMsg->msgType = gMlmeScanReq_c;
    /* Create the Start request message data. */
    pScanReq = &pMsg->msgData.scanReq;
    /* gScanModeED_c, gScanModeActive_c, gScanModePassive_c, or gScanModeOrphan_c */
    pScanReq->scanType = scanType;
    /* ChannelsToScan & 0xFF - LSB, always 0x00 */
    pScanReq->scanChannels[0] = (uint8_t)((SCAN_CHANNELS)     & 0xFF);
    /* ChannelsToScan>>8 & 0xFF  */
    pScanReq->scanChannels[1] = (uint8_t)((SCAN_CHANNELS>>8)  & 0xFF);
    /* ChannelsToScan>>16 & 0xFF  */
    pScanReq->scanChannels[2] = (uint8_t)((SCAN_CHANNELS>>16) & 0xFF);
    /* ChannelsToScan>>24 & 0xFF - MSB */
    pScanReq->scanChannels[3] = (uint8_t)((SCAN_CHANNELS>>24) & 0xFF);
    /* Duration per channel 0-14 (dc). T[sec] = (16*960*((2^dc)+1))/1000000.
       A scan duration of 2 on 16 channels approximately takes 1.2 secs. */
    /* We know beforehand that we will talk to a coordinator with a Beacon
       order of 1. Thus, choosing a Scan Duration of 2 per channel gives us
       a very good chance (100% assuming no interference) of finding the
       coordinator. 1 should be fine too though in an RF quiet environment. */
    pScanReq->scanDuration = 2;
    
    /* Send the Scan request to the MLME. */
    if(MSG_Send(NWK_MLME, pMsg) == gSuccess_c)
    {
      Uart_Print("Done\n");
      return errorNoError;
    }
    else
    {
      Uart_Print("Invalid parameter!\n");
      return errorInvalidParameter;
    }
  }
  else
  {
    /* Allocation of a message buffer failed. */
    Uart_Print("Message allocation failed!\n");
    return errorAllocFailed;
  }
}

/******************************************************************************
* The App_HandleScanPassiveConfirm(nwkMessage_t *pMsg) function will handle the
* Passive Scan confirm message received from the MLME when the Passive scan has
* completed. The message contains a list of PAN descriptors. Based on link
* quality information in the pan descriptors the nearest coordinator is chosen.
* The corresponding pan descriptor is stored in the global variable coordInfo. 
*
* If a suitable coordinator was found, we synchronize to it immediately.
*
* The function may return either of the following values:
*   errorNoError:       A suitable pan descriptor was found.
*   errorNoScanResults: No scan results were present in the confirm message.
*
******************************************************************************/
uint8_t App_HandleScanPassiveConfirm(nwkMessage_t *pMsg)
{
  uint8_t panDescListSize   = pMsg->msgData.scanCnf.resultListSize;
  panDescriptor_t *pPanDesc = pMsg->msgData.scanCnf.resList.pPanDescriptorList;
  uint8_t rc = errorNoScanResults;
 
  /* Check if the scan resulted in any coordinator responses. */
  if(panDescListSize != 0)
  {
    /* Initialize link quality to very poor. */
    uint8_t i, bestLinkQuality = 0;
    
    /* Check all PAN descriptors. */
    for(i=0; i<panDescListSize; i++, pPanDesc++)
    {
      /* Only attempt to associate if the coordinator accepts associations. */
      if(pPanDesc->superFrameSpec[1] & gSuperFrameSpecMsbAssocPermit_c)
      {
        /* Find the nearest coordinator using the link quality measure. */
        if(pPanDesc->linkQuality > bestLinkQuality)
        {
          /* Save the information of the coordinator candidate. If we
             find a better candiate, the information will be replaced. */
          memcpy(&coordInfo, pPanDesc, sizeof(panDescriptor_t));
          bestLinkQuality = pPanDesc->linkQuality;
          rc = errorNoError;
        }
      }
    }
  }

  if(rc == errorNoError)
  {
    /* If we have found a beaconing coodinator we must setup the MAC to
       synchronize to the beacon frames. This requires us to set the
       PAN ID attribute of the MAC PIB to the PAN ID of the coordinator.
       Furthermore, if we want to take advantage of the automatic
       polling feature we must set the Auto Request MAC PIB attribute. */
    if((coordInfo.superFrameSpec[0] & gSuperFrameSpecLsbBO_c) < 0xF) 
    {
      mlmeMessage_t *pMsgOut = MSG_AllocType(mlmeMessage_t);
      if(pMsgOut != NULL)
      {
        uint8_t value = TRUE;
        /* Set MAC PIB auto request to TRUE. In this way the device will
           automatically poll for data if the pending address list of the 
           beacon frame contains our address. */
        pMsgOut->msgType = gMlmeSetReq_c;
        pMsgOut->msgData.setReq.pibAttribute = gMacPibAutoRequest_c;
        pMsgOut->msgData.setReq.pibAttributeValue = &value;
        /* Get/Set/Reset Request messages are NOT freed by the MLME. */
        NR MSG_Send(NWK_MLME, pMsgOut);
        
        /* Since we are going to receive data from the coordinator 
           using automatic polling we must synchronize to the beacon
           and keep tracking it. Before synchronizing it is required
           that the MAC PIB PAN ID, and the MAC PIB coordinator
           address is set. */
        pMsgOut->msgData.setReq.pibAttribute = gMacPibPanId_c;
        pMsgOut->msgData.setReq.pibAttributeValue = coordInfo.coordPanId;
        /* Get/Set/Reset Request messages are NOT freed by the MLME. */
        NR MSG_Send(NWK_MLME, pMsgOut);

        /* Set coordinator address PIB attribute according the the 
           address mode of the coordinator (short or long address). */
        pMsgOut->msgData.setReq.pibAttribute = 
          coordInfo.coordAddrMode == gAddrModeShort_c ? gMacPibCoordShortAddress_c :
                                                        gMacPibCoordExtendedAddress_c;
        pMsgOut->msgData.setReq.pibAttributeValue = coordInfo.coordAddress;
        /* Get/Set/Reset Request messages are NOT freed by the MLME. */
        NR MSG_Send(NWK_MLME, pMsgOut);
                
        /* Now send the MLME-Sync Request. We choose to let the MAC track
           the beacons on the logical channel obtained by the passive scan.*/
        pMsgOut->msgType = gMlmeSyncReq_c;
        pMsgOut->msgData.syncReq.trackBeacon = TRUE;
        pMsgOut->msgData.syncReq.logicalChannel = coordInfo.logicalChannel;
        NR MSG_Send(NWK_MLME, pMsgOut);
      }
    }
  }
  
  /* ALWAYS free the PAN descriptor list */
  MSG_Free(pMsg->msgData.scanCnf.resList.pPanDescriptorList);
  
  return rc;
}


/******************************************************************************
* The App_SendAssociateRequest(void) will create an Associate Request message
* and send it to the coordinator it wishes to associate to. The function uses
* information gained about the coordinator during the scan procedure.
*
* The function may return either of the following values:
*   errorNoError:          The Associate Request message was sent successfully.
*   errorInvalidParameter: The MLME service access point rejected the
*                          message due to an invalid parameter.
*   errorAllocFailed:      A message buffer could not be allocated.
*
******************************************************************************/
uint8_t App_SendAssociateRequest(void)
{
  mlmeMessage_t *pMsg;
  mlmeAssociateReq_t *pAssocReq;

  Uart_Print("Sending the MLME-Associate Request message to the MAC...");
  
  /* Allocate a message for the MLME message. */
  pMsg = MSG_AllocType(mlmeMessage_t);
  if(pMsg != NULL)
  {
    /* This is a MLME-ASSOCIATE.req command. */
    pMsg->msgType = gMlmeAssociateReq_c;
    
    /* Create the Associate request message data. */
    pAssocReq = &pMsg->msgData.associateReq;
 
    /* Use the coordinator info we got from the Passive Scan. */
    memcpy(pAssocReq->coordAddress, coordInfo.coordAddress, 8);
    memcpy(pAssocReq->coordPanId,   coordInfo.coordPanId, 2);
    pAssocReq->coordAddrMode      = coordInfo.coordAddrMode;
    pAssocReq->logicalChannel     = coordInfo.logicalChannel;
    pAssocReq->securityEnable     = FALSE;
    /* We want the coordinator to assign a short address to us. */
    pAssocReq->capabilityInfo     = gCapInfoAllocAddr_c;
      
    /* Send the Associate Request 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 - 
       the state machine will call us again. */
    Uart_Print("Message allocation failed - retrying...\n");
    return errorAllocFailed;
  }
}

/******************************************************************************
* The App_HandleAssociateConfirm(nwkMessage_t *pMsg) function will handle the
* Associate confirm message received from the MLME when the Association
* procedure has completed. The message contains the short address that the
* coordinator has assigned to us. This address is 0xfffe if we did not specify
* the gCapInfoAllocAddr_c flag in the capability info field of the Associate
* request. The address and address mode are saved in global variables. They
* will be used in the next demo application when sending data.
*
******************************************************************************/
void App_HandleAssociateConfirm(nwkMessage_t *pMsg)
{
  /* This is our own extended address (MAC address). It cannot be modified. */
  extern const uint8_t aExtendedAddress[8];
  
  /* If the coordinator assigns a short address of 0xfffe then,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -