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

📄 myapp_ex04b.c

📁 This network protcol stack,it is very strong and powerful!
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
*   MyApp_Ex01.c  - Initialization and main loop. 
*   MyApp_Ex02.c  - Energy Detection Scan
*   MyApp_Ex03a.c - A PAN Coordinator is started
*   MyApp_Ex03b.c - Device locates coordinator using Active Scan
*   MyApp_Ex04a.c - Coordinator responds to an Associate request 
* = MyApp_Ex04b.c - Device Associates to the PAN coordinator ===== This file ==
*   MyApp_Ex05a.c - Coordinator receives data from device
*   MyApp_Ex05b.c - Device sends direct data to the coordinator
*   MyApp_Ex06a.c - Coordinator sends indirect data to device
*   MyApp_Ex06b.c - Device polls for data from the coordinator
*   MyApp_Ex07a.c - Coordinator starts a beaconed network
*   MyApp_Ex07b.c - Device receives data using automatic polling
*   MyApp_Ex08a.c - Coordinator uses security
*   MyApp_Ex08b.c - Device uses security
*
* This demo application builds upon MyApp_Ex03b.c which demonstrated how
* to use the Active Scan feature of the MAC for locating a coordinator. Now
* that we have the capability of finding a coordinator, we will associate to
* one in this demo application.
*
* Associations are used for making a logical connection between devices and
* coordinators. One advantage of associations is that the communication can be
* performed using 2 byte addresses instead of 8 byte addresses. The benefit of
* this is longer battery life since less power is required for the radio.
*
* It is the coordinator that assigns short addresses to devices wishing to
* associate. The devices do so by sending out an Associate Request command.
* to the coordinator. The network or application layer of the coordinator
* accepts the associate request by sending an Associate Response message
* back to the device. The response will contain a short address, and a status
* code indicating whether or not association was successful.
* 
* The steps required for associating to a coordinator are:
* 1) Build and send an MLME-Associate Request message to the MAC.
* 2) Wait for the MLME-Associate Confirm message from the MAC.
*
* Step 1 is performed by the 'stateAssociate' state, and step 2 is 
* performed by the 'stateAssociateWaitConfirm' state.
*
******************************************************************************/

#include "802_15_4.h" /* Include everything related to the 802.15.4 interface*/
#include "Uart.h"     /* Defines the interface of the demo UART. */
#include "ToolBox.h"

/* Defines the channels to scan. Each bit represents one channel. Use
   0x07FFF800 to scan all 16 802.15.4 channels in the 2.4GHz band. */
#define SCAN_CHANNELS 0x07FFF800

/* Forward declarations of helper functions */
uint8_t App_StartScan(uint8_t scanType);
uint8_t App_HandleScanActiveConfirm(nwkMessage_t *pMsg);
uint8_t App_WaitMsg(nwkMessage_t *pMsg, uint8_t msgType);
uint8_t App_SendAssociateRequest(void);
void    App_HandleAssociateConfirm(nwkMessage_t *pMsg);

/* All states in the applications state machine */
enum {
  stateInit,
  stateScanActiveStart,
  stateScanActiveWaitConfirm,
  stateAssociate,
  stateAssociateWaitConfirm,
  stateListen,
  stateTerminate
};

/* Error codes */
enum {
  errorNoError,
  errorWrongConfirm,
  errorNotSuccessful,
  errorNoMessage,
  errorAllocFailed,
  errorInvalidParameter,
  errorNoScanResults
};


/* The current state of the applications state machine */
uint8_t state;

/* Information about the PAN we are part of */
panDescriptor_t coordInfo;

/* This is either the short address assigned by the PAN coordinator
   during association, or our own extended MAC address. */
uint8_t myAddress[8];
/* The devices address mode. If 2, then myAddress contains the short
   address assigned by the PAN coordinator. If 3, then myAddress is
   equal to the extended address. */
uint8_t myAddrMode;

/* Application input queues */
anchor_t mMlmeNwkInputQueue;


/* Application Main Loop */
void main(void)
{ 
  /* Pointer for storing the messages from MLME, MCPS, and ASP. */
  void *pMsgIn;
  /* Stores the status code returned by some functions. */
  uint8_t rc;
  /* return value of Mlme_Main() - not used yet */
  uint8_t macStatus;
  
  /* Initialize variables */
  state = stateInit;

  /* Prepare input queues.*/
  MSG_InitQueue(&mMlmeNwkInputQueue);

  /* Execute the application state machine */    
  while(state < stateTerminate)
  {
    /* Preset error to contain the success code */
    rc = errorNoError;
    
    /* Try to get a message from MLME */
    if(MSG_Pending(&mMlmeNwkInputQueue))
      pMsgIn = MSG_DeQueue(&mMlmeNwkInputQueue);
    else
      pMsgIn = NULL;
      
    switch(state)
    {
    case stateInit:
      /* Initialize the UART so that we can print out status messages */
      Uart_Init();
      /* Initialize the 802.15.4 stack */
      Init_802_15_4();
      /* Goto Energy Detection state. */
      state = stateScanActiveStart;

      /* Print a welcome message to the UART */
      Uart_Print("The Myapp_Ex04b demo application is initialized and ready.\n\n");
      break;
      
    case stateScanActiveStart:
      /* Start the Active scan, and goto wait for confirm state. */
      Uart_Print("Start scanning for a PAN coordinator\n");
      rc = App_StartScan(gScanModeActive_c);
      if(rc == errorNoError)
      {
        state = stateScanActiveWaitConfirm;
      }
      break;
      
    case stateScanActiveWaitConfirm:
      /* Stay in this state until the Scan confirm message
         arrives, and then goto the associate state. */
         
      /* ALWAYS free the beacon frame contained in the beacon notify indication.*/
      rc = App_WaitMsg(pMsgIn, gNwkBeaconNotifyInd_c);
      if(rc == errorNoError) {
        MSG_Free(((nwkMessage_t *)pMsgIn)->msgData.beaconNotifyInd.pBufferRoot);
        Uart_Print("Received an MLME-Beacon Notify Indication\n");
      }
      
      /* Handle the Scan Confirm message. */
      rc = App_WaitMsg(pMsgIn, gNwkScanCnf_c);
      if(rc == errorNoError)
      {
        rc = App_HandleScanActiveConfirm(pMsgIn);
        if(rc == errorNoError)
        {
          Uart_Print("Found a coordinator with the following properties:\n");
          Uart_Print("----------------------------------------------------");
          Uart_Print("\nAddress...........0x"); Uart_PrintHex(coordInfo.coordAddress, coordInfo.coordAddrMode == gAddrModeShort_c ? 2 : 8, 0);
          Uart_Print("\nPAN ID............0x"); Uart_PrintHex(coordInfo.coordPanId, 2, 0);
          Uart_Print("\nLogical Channel...0x"); Uart_PrintHex(&coordInfo.logicalChannel, 1, 0);
          Uart_Print("\nBeacon Spec.......0x"); Uart_PrintHex(coordInfo.superFrameSpec, 2, 0);
          Uart_Print("\nLink Quality......0x"); Uart_PrintHex(&coordInfo.linkQuality, 1, 0);
          Uart_Print("\n\n");

          state = stateAssociate;
        }
        else
          Uart_Print("Scan did not find a suitable coordinator\n");
      }
      break;

    case stateAssociate:
      /* Associate to the PAN coordinator */
      Uart_Print("Associating to PAN coordinator on channel 0x");
      Uart_PrintHex(&(coordInfo.logicalChannel), 1, gPrtHexNewLine_c);
      rc = App_SendAssociateRequest();
      if(rc == errorNoError)
        state = stateAssociateWaitConfirm;
      break; 

    case stateAssociateWaitConfirm:
      /* Stay in this state until the Associate confirm message
         arrives, and then goto the Listen state. */
      rc = App_WaitMsg(pMsgIn, gNwkAssociateCnf_c);
      if(rc == errorNoError)
      {
        App_HandleAssociateConfirm(pMsgIn);
        state = stateListen;
        Uart_Print("Successfully associated with the coordinator.\n");
        Uart_Print("We were assigned the short address 0x");
        Uart_PrintHex(myAddress, myAddrMode == gAddrModeShort_c ? 2 : 8, gPrtHexNewLine_c);
      }
      break; 
      
    case stateListen:
      /* Stay in this state forever. */
      break;
    }
    
    if(pMsgIn)
      /* ALWAYS free messages from MLME */
      MSG_Free(pMsgIn);
  
    /* 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...");

⌨️ 快捷键说明

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