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

📄 network.c

📁 motorola jw32 usb 源码
💻 C
字号:

/*********************************************************************
 *
 *                  MFT network layer
 *
 *********************************************************************
 * FileName:        network.c
 * Dependencies:
 * Processor:       c51
 * Company:         chengdu MFT, Inc.
 *																											  
 * Software License Agreement
 *
 *
 * Author               Date    Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * jiangchao    				5/09/05 Rel 0.1
 ********************************************************************/

#define     SM_NWK_ENERGY_SCAN          0
#define     SM_NWK_ENERGY_SCAN_WAIT     1
#define     SM_NWK_ACTIVE_SCAN_WAIT     2
#define     SM_NWK_FORMED               3
#define     SM_NWK_NEXT_CHANNEL         4

BYTE smNWK;
BYTE bIsNWKDiscovered;
BYTE bScanSuccess;
static BYTE bScanOnce=1;
/*********************************************************************
 * Function:        void NWKInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Starts network discovery process
 *
 * Note:            Available for coorindator only.
 ********************************************************************/
 
 
void NWKInit(void)
{
		bScanOnce^=0x01;
		MACRandDelayWait();    
    MACSetFirstChannel(bScanOnce);
    smNWK = SM_NWK_ENERGY_SCAN;
	bScanSuccess = TRUE;
    bIsNWKDiscovered = FALSE;
    MACPANCLR() ;
       // printf("NWK: NWKStartDiscovery(): Starting ED...\r\n");
  }
/*********************************************************************
 * Function:        BYTE NWKIsDiscoveryComplete(void)
 *
 * PreCondition:    NWKStartDiscovery() is called
 *
 * Input:           None
 *
 * Output:          TRUE, if complete
 *                  FALSE otherwise
 *                 
 *
 * Side Effects:    None
 *
 * Overview:        Performs network discovery process.
 *
 * Note:            None
 ********************************************************************/
BYTE NWKIsDiscoveryComplete(void)
{
   	if(smNWK==SM_NWK_ENERGY_SCAN)
    {
       // MACStartED();
        smNWK = SM_NWK_ENERGY_SCAN_WAIT;
    } else if(smNWK==SM_NWK_ENERGY_SCAN_WAIT) {						 
      
              MACStartScan();
            	smNWK = SM_NWK_ACTIVE_SCAN_WAIT;

    } else if(smNWK==SM_NWK_NEXT_CHANNEL){
      
        if ( !MACSetNextChannel(bScanOnce) )
        {
           // printf("NWK: NWKDiscoveryComplete: No channel is free.\r\n");
           bScanSuccess = FALSE;
            return TRUE;
        }

        smNWK = SM_NWK_ENERGY_SCAN;

    } else if(smNWK==SM_NWK_ACTIVE_SCAN_WAIT){
      
        if ( MACIsScanComplete() )
        {
            // Depending on whether this is coordinator or end device,
            // this function may get called to continue scanning next available channel.

            // A coordinator would want to scan for empty channel. If no network is discovered,
            // coordinator may form network and may not call this function again.

            // An end device needs to find an occupied channel. If no network is discovered,
            // end device would continue to scan and see if it can find a busy channel. In
            // that case, this function will be called again.

            // In any case, set the next state to NEXT_CHANNEL so that if this function is called
            // agian, it will switch to next channel and continue the discovery.

            smNWK = SM_NWK_NEXT_CHANNEL;
            return TRUE;

        }
    }
    return FALSE;
		
}
/*********************************************************************
 * Function:        BYTE NWKIsCoordInitComplete(void)
 *
 * PreCondition:    None
 *
 * Input:           TRUE if NWK Coordinator is complete
 *                  FALSE, otherwise
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Performs coordinaor initialization process.
 *
 * Note:            Available for coorindator only.
 ********************************************************************/

BYTE NWKInitIsComplete()
{
    // Check to see if discovery is complete.
    static BYTE scancount=0;   
    if ( NWKIsDiscoveryComplete())
    {
        // Terminate this function if there was an error.
        scancount++;
       /* if(scancount==1)
        {
        	  	
        }*/
       // printf("NWK: Discovery: %d channel is scaned.\r\n",scancount);
        if ( MACIsPANAvailable() )//find a PAN 
        {              
          
           #if defined(I_AM_COORDINATOR) 
            MACPANCLR() ;
          #else
          	 bIsNWKDiscovered = TRUE;//device should continue scan,till scan all channel
          #endif
        }
        else                    //find a free channel
        {
        	 #if defined(I_AM_COORDINATOR)  
        	 if(!bIsNWKDiscovered&&bScanOnce)      	 
          	 {
          	    if(MACAcceptCurrentPAN())
          	    {
          	        bIsNWKDiscovered = TRUE;
          	        scancount=0;
		        				return TRUE;
          	     }
          	 }          	
            #endif
        	
        }
        if (!bScanSuccess)
				{
						scancount=0;
		        return TRUE;
				}
        else if (scancount>=MAC_SCAN_MAX_CHANNEL)
        {
            // Terminate this function.                                  
						scancount=0;						
             return TRUE;
        }
    }

    // we are still not done.
    return FALSE;
}

/*********************************************************************
 * Function:        void NWKForm(void)
 *
 * PreCondition:    NWKDiscovery is successfully completed.
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Selects PANID and establishes network
 *
 * Note:            Available to coordinators only.
 ********************************************************************/

void NWKFormStart(void)
{
    // Mark that the network is formed.    
    MACStart();
}

/*********************************************************************
 * Function:        BYTE NWKFormIsComplete()
 *
 * PreCondition:    NWKDiscovery is successfully completed.
 *
 * Output:          for  device if join network success ,return TRUE,for coordinator,
 	                  directly return TRUE.
 *
 * Side Effects:    None
 *
 * Overview:        Selects PANID and establishes network
 *
 * Note:            Available to coordinators only.
 ********************************************************************/
BYTE NWKFormIsComplete()
{
	#if defined(I_AM_COORDINATOR)
		return TRUE;		
	#else
		return MACIsAssociationComplete();
	#endif
	
}

⌨️ 快捷键说明

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