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

📄 nwk_general.c

📁 ucos在NEC平台下的移植
💻 C
字号:
/*
 *******************************************************************************************************
 * CONFIDENTIAL                                                                                        *
 * The use of this file is restricted by SkywiseSystems                    *
 *                                                                                                     *
 * Copyright liquanling 08/09/2006                                                                          *
 *******************************************************************************************************
 * This module contains general types, constants, macros and functions used by many other NWK modules.  
 *******************************************************************************************************
 * Compiler: NEC-CC78K0                                                                                  *
 * Target platform:                                                   *
 *******************************************************************************************************
 * The revision history is located at the bottom of this file                                          *
 *******************************************************************************************************/
#include"includes.h"
extern UINT16 deviceShortAddress;
UINT16 devicePanId;
NWK_INFO nwkInfo;
//extern NWK_NIB nnib;
UINT8 macDsn;
extern UINT16 deviceCoordShortAddress;
extern volatile UINT16 nwkDstAddrInHeaderOfRecivedPacket;
extern volatile UINT16 nwkSrcAddrInHeaderOfRecivedPacket;
extern UINT16  nwkBroadcastAddress;
extern MCPS_DATA_INDICATION  mcpsDataIndicationInfo;
//-------------------------------------------------------------------------------------------------------
//  ZBOOL macSetState(NWK_STATE_TYPE newState)
//
//  DESCRIPTION:
//      Changes the internal NWK state, which is used to control polling and scanning. Only specific
//      sequences of transitions are allowed.
//
//  ARGUMENTS:
//      NWK_STATE_TYPE newState
//          The new NWK state
//
//  RETURN VALUE:
//      ZBOOL 
//          TRUE if successful (legal transition to newState)
//          FALSE otherwise (the state was not changed)
//-------------------------------------------------------------------------------------------------------
extern NWK_NIB nnib;

UINT8  nwkSequenceNumber;
//nwkSequenceNumber=nnib.nwkSequcenceNumber;

//#define nwkGetSequenceNumber()   nwkSequenceNumber++

ZBOOL nwkSetState(NWK_STATE_TYPE newState) {
    ZBOOL result;
    //DISABLE_GLOBAL_INT(); //this should be substituted by hardware interrupt from MAC 
    switch (newState) 
		{

    // NetWork Formation
   case NWK_STATE_FORM_BEACON_NETWORK_START:    
        result = (ZBOOL)(nwkInfo.state == NWK_STATE_IDLE);
        break;
   case NWK_STATE_FORM_NON_BEACON_NETWORK_START:
	    result = (ZBOOL)(nwkInfo.state == NWK_STATE_IDLE);
	    break;
   
    case NWK_STATE_JOIN_NETWORK_START:
		result=(ZBOOL)(nwkInfo.state==NWK_STATE_IDLE);
		break;
   // case NWK_STATE_IDLE:     
    //      result = TRUE;
     //    break;
    default:   
         result = FALSE;
         break;
               }
    if (result) 
        nwkInfo.state = newState;
   // ENABLE_GLOBAL_INT();
    return result;

} // nwkSetState


//------------------------------------------------------------------
//     void ByteArrayToUint16(BYTE *shortAddress,UINT16 *myShortAddress);	  
//
//     DESCRIPTION:
//            Transfer the byte array to UINT16 data type.
//     PARAMETER:
//            BYTE *shortAddress,
//            A pointer points to the byte array.
//            short *myShortAddress,
//            A 'value-result' parameter,take back the result.
//    RETURN VALUE:
//            NO;
//    NOTE:in the program,the address information stored in the NWK header and some
//             NWK payload are in the byte format,some time we need to transmit.
//--------------------------------------------------------------------

void ByteArrayToUint16(BYTE *shortAddress,UINT16*myShortAddress)
{
   *myShortAddress=((*shortAddress++)<<8);
   *myShortAddress+=(*shortAddress++);   
}

//--------------------------------------------------------------------
//     void InitNWKCommonVariable();
//
//     DESCRIPTION:
//           Some variable such as the short addres of the device and the device'PanId and 
//           so on,these variables will be used in every .c file in the NWK layer,so use this 
//           the global variable to store these common information.This function initialize 
//           these global variables.
//     PARAMETER:
//           NO.
//     RETURN-VALUE:
//           NO.
//    NOTE:This function must be called at the startup of the program.
//-------------------------------------------------------------------
void InitNWKCommonVariable()
{
 /*
 MAC_ENUM getMacPibResult;
  nwkSequenceNumber=nnib.nwkSequcenceNumber;*/
 
 nwkBroadcastAddress=0xFFFF;
 
 /*
 getMacPibResult=MAC_UNSUPPORTED_ATTRIBUTE;
  while((getMacPibResult=mlmeGetRequest(MAC_SHORT_ADDRESS,&deviceShortAddress))!=NWK_SUCCESS);
  getMacPibResult=MAC_UNSUPPORTED_ATTRIBUTE;
  while((getMacPibResult=mlmeGetRequest(MAC_PAN_ID,&devicePanId))!=NWK_SUCCESS);
  getMacPibResult=MAC_UNSUPPORTED_ATTRIBUTE;
  while((getMacPibResult=mlmeGetRequest(MAC_COORD_SHORT_ADDRESS,&deviceCoordShortAddress))!=NWK_SUCCESS);
  getMacPibResult=MAC_UNSUPPORTED_ATTRIBUTE;
   while((getMacPibResult=mlmeGetRequest(MAC_DSN,&deviceCoordShortAddress))!=NWK_SUCCESS);
  getMacPibResult=MAC_UNSUPPORTED_ATTRIBUTE;*/
 

}

//----------------------------------------------------------------------
//     void getAddressInfoInNwkHeader();
//
//      DESCRIPTION:
//            There are some global variables in this program,such as  nwkSrcAddrInHeaderOfRecivedPacket
//             and nwkDstAddrInHeaderOfRecivedPacket,these variables will be used in 
//             nwkDealWithRecivedLeaveIndicationFromMCPS()and other functions,so use global
//             variable instead of passing parameters to every relate function.
//      PARAMETER:
//             NO.
//      RETURN-VALUE:
//             NO.
//      NOTE:This function should only be called in mcpsDataIndication() function.
//-------------------------------------------------------------------------
void getAddressInfoInNwkHeader()
{
  //we read the destination address and source address from the NWK header
 ByteArrayToUint16(&mcpsDataIndicationInfo.pMsdu[NWK_HEADER_DESTINATION_ADDRESS_FIELD_INDEX],  &nwkDstAddrInHeaderOfRecivedPacket);
 ByteArrayToUint16(&mcpsDataIndicationInfo.pMsdu[NWK_HEADER_SOURCE_ADDRESS_FIELD_INDEX],  &nwkSrcAddrInHeaderOfRecivedPacket);
}


//-------------------------------------------------------------------------------------------------------
//TICK getCurrentime();
//
//DESCRIPTION:
//       Catch the current time.we can use the register to implement or we can use 
//       the API of the ucOS.
//PARAMETER:
//        NO
//RETURN_VALUE
//        The current time-maybe a snap of some register
//--------------------------------------------------------------------------------------------
TICK getCurrentime()
{
TICK now;
//帒敓楾桼婸擮媍晭瘞鈸鉀幬寕淥媍崕瓡麣E寧翚h媣夅枎邖H
// now=time(0);
 now=1234;
 return now;
}

//--------------------------------------------------------------------------------------------
//   BYTE nlmeGetNwkBCSN( void )
//
//   DESCRIPTION:
//         This function provides the caller with a valid sequence  number to use in the NWK header.
//   RETURN_VALUE:
//         a valid sequence number to use in the NWK header
//   Note:           
//         This function is used to initialize the nsduhandle in the nldeDataRequest() function.
//         INVALID_NWK_HANDLE is reserved
//--------------------------------------------------------------------------------------------
BYTE nlmeGetNwkBCSN( void )
{
    nnib.nwkBCSN=123;
    nnib.nwkBCSN++;
       
    if (nnib.nwkBCSN == INVALID_NWK_HANDLE) 
		nnib.nwkBCSN++;
    return nnib.nwkBCSN;
}

//---------------------------------------------------------------------------------------------
//    ZBOOL longAddressIsNULL(BYTE * longAddress);
//
//    DESCRIPTION:
//         This function wraps the operation about justifying is the device's long address is NULL.This function is used 
//          when the device leaves the network;
//    PARAMETER:
//          BYTE * longAddress:
//            This pointer points to the device's long address.
//    RETURN_VALUE:
//          TRUE:the device's long address is NULL.
//          FALSE:the device's long address is not NULL.
//----------------------------------------------------------------------------------------------
ZBOOL longAddressIsNULL(BYTE * longAddress)
{
 BYTE nullAddress[8];
 ZBOOL isNull;
 int i;
 for(i=0;i<8;i++)
 	{
       nullAddress[i]=0x00;
       }
 isNull=ContentIsEqual(longAddress , nullAddress, 8);
 return isNull;
}

//-----------------------------------------------------------------------------------------------
//    UINT8 randNumGenerator(UINT8 maxNum);
//
//    DESCRIPTION:
//          This function is used to generat a random number between 0 and maxNum.
//    PARAMETER:
//          UINT8 maxNum:
//              The right  range of the generated random value.
//    RETURN_VALUE:
//          The generated random value.
//    NOTE:This function depends on system function.
//-----------------------------------------------------------------------------------------------
UINT8 randNumGenerator(UINT8 maxNum)
{
  UINT8 maxNumber=0;
  UINT8 sequenceNum=0;
  maxNumber=maxNum;
  //srand(time(NULL));   
  sequenceNum=rand()%maxNumber;         //夬暁0-(maxNumber-1)媍枂帤曽
  printf("夬暁媍枂帤曽棊

⌨️ 快捷键说明

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