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

来源:互联网 发布:mac 隐藏磁盘 编辑:程序博客网 时间:2024/04/28 19:25


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_TYPEWhiteList *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是一个循环使用的数组,在芯片上面不宜保存过多的临时数据,可以将数据用串口发送到上位机或操作系统保存。




0 0
原创粉丝点击