zigbee实现网络加入过滤(相当于白名单)


1,在app.h文件中

#if ZG_BUILD_COORDINATOR_TYPE


#define WHITE_LIST_MAX_SIZE 2 //最大缓存数


typedef struct {  
  uint8  macAddress[8];//macAddress  
  uint8  isAllow;//1 allow,0 reject  
}WhiteNode;  


typedef struct {
  WhiteNode *node[WHITE_LIST_MAX_SIZE];
  int8  currentIndex;
}WhiteList;	


extern WhiteList *whiteList;


#endif


2,在app.c文件中声明变量


 

#if ZG_BUILD_COORDINATOR_TYPE
WhiteList *whiteList;
#endif

 然后在对应的init方法中对数据结构初始化

#if ZG_BUILD_COORDINATOR_TYPE
  whiteList->currentIndex = -1;
  uint8 *myMac = NLME_GetExtAddr();//先用自己的mac地址随便初始一下
  for(int i=0;i<WHITE_LIST_MAX_SIZE;i++)
  {  
    WhiteNode *node = (WhiteNode *)osal_mem_alloc(sizeof( WhiteNode));
    osal_memcpy(node->macAddress,myMac,8); 
    node->isAllow = 0;
    whiteList->node[i] = node;
  }
#endif
  


在串口的回调函数中添加如下代码:

#if ZG_BUILD_COORDINATOR_TYPE
      
      if(SerialApp_TxBuf[0] == 0x0A)
      {
        unsigned char macAddress[8];
      //  printf(" sp: \r\n");
        for(int index = 1;index<9;index++)
        {
         // printf(" %02X ",SerialApp_TxBuf[index]);
          macAddress[index - 1] = SerialApp_TxBuf[index];
        }
       // printf("  \r\n");
        
        char isAllow = SerialApp_TxBuf[9];
        int8 currentIndex = (whiteList->currentIndex + 1)%WHITE_LIST_MAX_SIZE;
        WhiteNode *node = whiteList->node[currentIndex];
        osal_memcpy(node->macAddress,macAddress,8); 
        node->isAllow = isAllow;
        
        whiteList->currentIndex = currentIndex ;
      }
#endif  
      


3,在ZDAPP.c文件的ZDO_JoinIndicationCB添加如下代码:

ZStatus_t ZDO_JoinIndicationCB( uint16 ShortAddress, uint8 *ExtendedAddress,
                                uint8 CapabilityInformation, uint8 type )
{
  (void)ShortAddress;
  (void)ExtendedAddress;
  (void)CapabilityInformation;
  
    // Notify to save info into NV
    ZDApp_NVUpdate();
  
    // send notification to TC of new device..
    if ( ZG_SECURE_ENABLED )
    {
      if ( type == NWK_ASSOC_JOIN || type == NWK_ASSOC_REJOIN_UNSECURE )
      {
        osal_start_timerEx( ZDAppTaskID, ZDO_NEW_DEVICE, 600 );
      }
    }
  
#if ZG_BUILD_COORDINATOR_TYPE
    
    unsigned char SerialApp_TxBuf[10];
    SerialApp_TxBuf[0] = 0x0A;
    unsigned char mac_buf[8];
    for(int i=0;i<8;i++){
      mac_buf[i] = ExtendedAddress[i]; 
      SerialApp_TxBuf[1+i] = ExtendedAddress[i];
    }
    
    uint8 flag=0;
    WhiteNode *node; 
    for(uint8 i=0;i<WHITE_LIST_MAX_SIZE;i++)
    {
      node = whiteList->node[i];
      if(osal_memcmp(mac_buf,node->macAddress,8)==true)  
      {
        flag= node->isAllow; 
        break;
      }
    }
    
    
    if(flag==1){
      return ( ZSuccess );
    }else{
      SerialApp_TxBuf[9] = 0x0D;
      HalUARTWrite(0x00, SerialApp_TxBuf, 10);
      return ( ZFailure ); 
    }
    
#endif
    
    return ( ZFailure ); 
}


注意:

1,设备的mac地址唯一

2,isAllow表示是否允许设备加入网络,由用户决定是否允许其加入(通过串口发送结果到ZigBee模块)

3,whitelist是一个循环使用的数组,在芯片上面不宜保存过多的临时数据,可以将数据用串口发送到上位机或操作系统保存。




  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值