ZigBee学习(5)————网络管理基础

目录

网络管理主要包括两方面的内容:

ZigBee学习(5)————网络管理基础_第1张图片


 

ZigBee学习(5)————网络管理基础_第2张图片

协调器上电后建立网络,路由器自动加入网络,路由器调用函数获取本身的网络地址,MAC地址,父节点网络地址和父节点MAC地址,通过串口将其输出到PC机

 

协调器编程:

//Coordinator.c
void GenericApp_Init( byte task_id )
{
  GenericApp_TaskID = task_id;
  GenericApp_TransID = 0;
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
  GenericApp_epDesc.task_id = &GenericApp_TaskID;
  GenericApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  GenericApp_epDesc.latencyReq = noLatencyReqs;
  afRegister( &GenericApp_epDesc );
}

UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )
{
  
  return 0;
}

端口的初始化和端口的注册,没有对具体事件的处理,所以空函数

 

路由器编程:

//Enddevice.c
typedef struct RFTXBUF
{
    uint8 myNWK[4];                  
    uint8 myMAC[16];               
    uint8 pNWK[4]; 
    uint8 pMAC[16];               
}RFTX;

分别存储本节点的网络地址和MAC地址,父节点的网络地址和MAC地址

 

//Enddevice.c
void GenericApp_Init( byte task_id )
{
  halUARTCfg_t  uartConfig;
  GenericApp_TaskID = task_id;
  GenericApp_NwkState = DEV_INIT;
  GenericApp_TransID = 0;
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
  GenericApp_epDesc.task_id = &GenericApp_TaskID;
  GenericApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  GenericApp_epDesc.latencyReq = noLatencyReqs;
  afRegister( &GenericApp_epDesc );
  
  uartConfig.configured             =TRUE;
  uartConfig.baudRate               =HAL_UART_BR_115200;
  uartConfig.flowControl            =FALSE;
  uartConfig.callBackFunc           =NULL;
  HalUARTOpen(0, &uartConfig);
}

UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
        case ZDO_STATE_CHANGE:
          GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          if (GenericApp_NwkState == DEV_ROUTER )
          {
             osal_set_event(GenericApp_TaskID,SHOW_INFO_EVENT);
          }
          break;

        default:
          break;
      }
      osal_msg_deallocate( (uint8 *)MSGpkt );
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    }

    return (events ^ SYS_EVENT_MSG);
  }
  if(events & SHOW_INFO_EVENT)
  {
      ShowInfo();
      osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,5000);
      return(events ^ SHOW_INFO_EVENT);
  }
  return 0;
}

串口的初始化,当路由器加入网络后,将设置事件SHOW_INFO_EVENT,在SHOW_INFO_EVENT事件处理函数中,调用ShowInfo()函数显示相关的地址信息

 

//Enddevice.c
void To_string(uint8 *dest,char *src,uint8 length)   //二进制转为十六进制
{
    uint8 *xad;
    uint8 i = 0;
    uint8 ch;
    xad = src + length - 1;
    for(i = 0;i < length;i++,xad-- )
    {
      ch = (*xad >> 4) & 0x0F;
      dest[i<<1] = ch + (( ch < 10) ? '0' : '7' );
      ch = *xad & 0x0F;
      dest[(i<<1) + 1] = ch + (( ch < 10 ) ? '0' : '7' );
      ch + (( ch < 10 ) ? '0' : '7' );
    }
}

void ShowInfo( void )
{
  RFTX  rftx;
  uint16 nwk;
  uint8 buf[8];
  uint8 changline[2]={0x0A,0x0D};               
  nwk = NLME_GetShortAddr();
  To_string(rftx.myNWK,(uint8 *)&nwk,2);
  To_string(rftx.myMAC,NLME_GetExtAddr(),8);
  nwk = NLME_GetCoordShortAddr();
  To_string(rftx.pNWK,(uint8 *)&nwk,2);
  NLME_GetCoordExtAddr(buf);
  To_string(rftx.pMAC,buf,8);
  
  HalUARTWrite(0,"NWK:",osal_strlen("NWK:"));
  HalUARTWrite(0,rftx.myNWK,4);
  HalUARTWrite(0," MAC:",osal_strlen(" MAC:"));
  HalUARTWrite(0,rftx.myMAC,16);
  HalUARTWrite(0," p-NWK:",osal_strlen(" p-NWK:"));
  HalUARTWrite(0,rftx.pNWK,4);
  HalUARTWrite(0," p-MAC:",osal_strlen(" p-MAC:"));
  HalUARTWrite(0,rftx.pMAC,16);
  HalUARTWrite(0,changline,2);
}

调用二进制转十六进制函数输出到串口,调用NLME_GetShortAddr()函数获取本节点的网络地址,NLME_GetExtAddr()函数返回的是指向节点MAC地址的指针,所以可以直接作为To_string()函数的参数传递,调用NLME_GetCoorShortAddr()函数获取父节点的网络地址,因为NLME_GetCoorShortAddr()函数的返回值就是父节点的网络地址。

 

打开协调器和路由器电源,可以看到串口已经输出了节点的网络地址,MAC地址,父节点的网络地址和父节点的MAC地址信息。

你可能感兴趣的:(ZigBee)