基于CC2430使终端设备依据特定的PAN ID入网

来源:互联网 发布:什么是控制算法 编辑:程序博客网 时间:2024/05/02 07:23

关于zigbee终端入网过程的过程和相关API参考我的其他博文

zigbee终端:当panid设置为非0xffff时,终端启动时首先会寻找符合条件的panid,当找不到时就会加入其他的网络,所以如果只是修改设置文件中的panid,终端如果找不到预先设置的网络也会加入其他网络,所以得修改源代码,下面是修改的过程。

终端试图加入一个网络时,首先调用NLME_NetworkDiscoveryRequest()函数。该函数将进行网络扫描,其结果由函数ZDO_NetworkDiscoveryCONfirmCB()返回

我们看下ZDO_NetworkDiscoveryCONfirmCB源码

ZStatus_t ZDO_NetworkDiscoveryConfirmCB( uint8 ResultCount,
                                         networkDesc_t *NetworkList )
{
  networkDesc_t *pNwkDesc = NetworkList;
  ZDO_NetworkDiscoveryCfm_t msg;
  uint8 i = ResultCount;
  uint8 stackProfile;
  uint8 stackProfilePro;
  uint8 selected;

/*#if defined ( ZDO_MGMT_NWKDISC_RESPONSE )
  if ( zdappMgmtNwkDiscReqInProgress )
  {
    zdappMgmtNwkDiscReqInProgress = false;
    ZDO_FinishProcessingMgmtNwkDiscReq( ResultCount, NetworkList );
    return ( ZSuccess );
  }
#endif*/


  // process discovery results
  stackProfilePro = FALSE;
  selected = FALSE;

  for ( stackProfile = 0; stackProfile < STACK_PROFILE_MAX; stackProfile++ )
  {
    pNwkDesc = NetworkList;
    for ( i = 0; i < ResultCount; i++, pNwkDesc = pNwkDesc->nextDesc )
    {
      
      if ( zgConfigPANID != 0xFFFF )
      {
        // PAN Id is preconfigured. check if it matches
        if ( pNwkDesc->panId != zgConfigPANID )
          continue;
      }
      


      if ( nwk_ExtPANIDValid( ZDO_UseExtendedPANID) == true )
      {
        // If the extended Pan ID is commissioned to a non zero value
        // Only join the Pan that has match EPID
        if ( osal_ExtAddrEqual( ZDO_UseExtendedPANID, pNwkDesc->extendedPANID) == false )
          continue;
        
      }


        // check that network is allowing joining
        if ( ZSTACK_ROUTER_BUILD )
        {
          if ( stackProfilePro == FALSE )
          {
            if ( !pNwkDesc->routerCapacity )
            {
              continue;
            }
          }
          else
          {
            if ( !pNwkDesc->deviceCapacity )
            {
              continue;
            }
          }
        }
        else if ( ZSTACK_END_DEVICE_BUILD )
        {
          if ( !pNwkDesc->deviceCapacity )
          {
            continue;
          }
        }

        // check version of zigbee protocol
        if ( pNwkDesc->version != _NIB.nwkProtocolVersion )
          continue;

        // check version of stack profile
        if ( pNwkDesc->stackProfile != zgStackProfile  )
        {
          if ( ((zgStackProfile == HOME_CONTROLS) && (pNwkDesc->stackProfile == ZIGBEEPRO_PROFILE))
              || ((zgStackProfile == ZIGBEEPRO_PROFILE) && (pNwkDesc->stackProfile == HOME_CONTROLS))  )
            stackProfilePro = TRUE;
          
          if ( stackProfile == 0 )
          {
            continue;
          }
        }

      // check if beacon order is the right value..
      //  if ( pNwkDesc->beaconOrder < ZDO_CONFIG_MAX_BO )
      //    continue;

      // choose this pan for joining
      break;
    }
    if (i < ResultCount)
    {
     selected = TRUE;
      break;
    }
   
    // break if selected or stack profile pro wasn't found
    if ( (selected == TRUE) || (stackProfilePro == FALSE) )
    {
      break;
    }
  }

  if ( i == ResultCount )
  {
     
    msg.hdr.status = ZDO_FAIL;   // couldn't find appropriate PAN to join !
     
  }

  else
  {
    msg.hdr.status = ZDO_SUCCESS;
    msg.panIdLSB = LO_UINT16( pNwkDesc->panId );
    msg.panIdMSB = HI_UINT16( pNwkDesc->panId );
    msg.logicalChannel = pNwkDesc->logicalChannel;
    msg.version = pNwkDesc->version;
    osal_cpyExtAddr( msg.extendedPANID, pNwkDesc->extendedPANID );
  }

  ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_DISC_CNF, sizeof(ZDO_NetworkDiscoveryCfm_t), (uint8 *)&msg );

  return (ZSuccess);
}  // ZDO_NetworkDiscoveryConfirmCB


蓝色是需要注释掉的,红色匹配panid关键代码

所以只需要注释掉蓝色代码,就可以只加入预先设置的panid了

0 0
原创粉丝点击