zigbee实现网络加入过滤(相当于白名单)
2017-03-15 17:27
393 查看
1,在app.h文件中
2,在app.c文件中声明变量
然后在对应的init方法中对数据结构初始化
在串口的回调函数中添加如下代码:
3,在ZDAPP.c文件的ZDO_JoinIndicationCB添加如下代码:
注意:
1,设备的mac地址唯一
2,isAllow表示是否允许设备加入网络,由用户决定是否允许其加入(通过串口发送结果到ZigBee模块)
3,whitelist是一个循环使用的数组,在芯片上面不宜保存过多的临时数据,可以将数据用串口发送到上位机或操作系统保存。
#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是一个循环使用的数组,在芯片上面不宜保存过多的临时数据,可以将数据用串口发送到上位机或操作系统保存。
相关文章推荐
- 【转载】在ZigBee网络中实现节电断电之后重新加入网络
- 在ZigBee网络中怎么实现节电断电之后重新加入网络
- 在ZigBee网络中怎么实现节电断电之后重新加入网络
- 在ZigBee网络中怎么实现节电断电之后重新加入网络
- 在ZigBee网络中实现节电断电之后重新加入网络
- zigbee 无线网络通讯设计与实现(二)
- zigbee中限制节点加入网络
- 用java实现简单的网络通信,相当于一个最简单的控制台qq(附代码) 推荐
- zigbee网关 利用现有的网络实现全球控制
- 净化网络环境 ASP程序实现过滤脏话
- MQTT--多个Zigbee监测网络远程监控的实现
- zigbee让设备直接加入网络(DirectJion) (附代码)
- ZIgbee设备加入网络过程(方式一 关联加入)
- MQTT–多个Zigbee监测网络远程监控的实现
- Lucene.Net 实现groupby并可以加入自定义过滤功能
- 【科研论文】ZigBee网络在智能电网需求侧的组网与实现--基于W5200
- 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中
- 如何防止Zigbee加入不该加的网络
- Zigbee网络架构+ZigBee的体系结构+理解zigbee节点的实现的案例+“51单片机” 和 “zigbee” 、 “cc2530芯片” 之间的关系+芯片cc2530
- Servlet实现网络IP过滤