Zigbee四种绑定方式一、两个节点分别通过按键机制调用政法P_EndDeviceBindReq函数这种绑定方式必须要有协调器设备存在,两个设备通过按键在一定时间间隔内(默认为15秒)调用政法P_EndDeviceBindReq函数完成绑定。首先要在初始化程序中注册End_Device_Bind_rsp进程,然后按键绑定请求程序如下:1if(Up_down_mark==1)2{3Up_down_mark=0;4HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);5//InitiateanEndDeviceBindRequestforthemandatoryendpoint6dstAddr.addrMode=Addr16Bit;7dstAddr.addr.shortAddr=0x0000;//Coordinator8政法P_EndDeviceBindReq(dstAddr,NLME_GetShortAddr(),ZG_Serial_Control_epDesc.endPoint,ZG_Serial_Control_PROFID,ZG_Serial_Control_MAX_CLUSTERS,(cId_t*)ZG_Serial_Control_ClusterList,ZG_Serial_Control_MAX_CLUSTERS,(cId_t*)ZG_Serial_Control_ClusterList,FALSE);}回调处理函数如下:9caseEnd_Device_Bind_rsp:10if(政法O_ParseBindRsp(inMsg)==ZSuccess)11{12HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);13}14break;二、Match方式这种绑定方式无需协调器设备存在,可采用按键机制来实现。首先在初始化函数中注册Match_Desc_rsp进程,然后按键请求函数如下:15if(Right_left_mark==1)16{17Right_left_mark=0;18HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);19//InitiateaMatchDescriptionRequest(ServiceDiscovery)20dstAddr.addrMode=AddrBroadcast;21dstAddr.addr.shortAddr=NWK_BROADCAST_SHORTADDR;22政法P_MatchDescReq(dstAddr,NWK_BROADCAST_SHORTADDR,ZG_Serial_Control_PROFID,ZG_Serial_Control_MAX_CLUSTERS,(cId_t*)ZG_Serial_Control_ClusterList,ZG_Serial_Control_MAX_CLUSTERS,(cId_t*)ZG_Serial_Control_ClusterList,FALSE);}回调处理函数如下:23caseMatch_Desc_rsp:24{25政法O_ActiveEndpointRsp_t*pRsp=政法O_ParseEPListRsp(inMsg);26if(pRsp)27{28if(pRsp->status==ZSuccesspRsp->cnt)29{30ZG_Serial_Control_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;31ZG_Serial_Control_DstAddr.addr.shortAddr=pRsp->nwkAddr;32//Takethefirstendpoint,Canbechangedtosearchthroughendpoints33ZG_Serial_Control_DstAddr.endPoint=pRsp->epList[0];3435//LightLED36HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);37}38osal_mem_free(pRsp);39}40}41break;三、政法P_BindReq和政法P_UnbindReq方式这种绑定方式需要第三方设备辅助完成,即假如A设备和B设备想进行绑定,A为源设备,B为目标设备,这时需要一个C设备来发起绑定,C设备需要知道A设备和B设备端点号、簇ID号和64位IEEE地址。首先需要注册Bind_rsp/Unbind_rsp,然后绑定请求函数如下:42destAddr.addrMode=Addr16Bit;43destAddr.addr.shortAddr=BUILD_UINT16(Uart_buf[1],Uart_buf[2]);//目标设备网络地址44osal_cpyExtAddr(Bind_SourceAddr,Uart_buf[3]);4546devAddr.addrMode=Addr64Bit;47osal_cpyExtAddr(devAddr.addr.extAddr,Uart_buf[11]);4849政法P_BindReq(destAddr,Bind_SourceAddr,ZG_Serial_Control_ENDPOINT,ZG_Serial_Control_CLUSTERID,devAddr,ZG_Serial_Control_ENDPOINT,0);回调处理函数如下:50if(政法O_ParseBindRsp(inMsg)==ZSuccess)//绑定成功51HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);四、手工管理绑定表这种方式是最灵活的方式,不过目前我只实现了子节点对其父节点的绑定,而无法实现对非父节点的绑定,原因尚不清楚,若有朋友清楚还请告诉我,谢谢!绑定表添加绑定程序:52BindAddr.addrMode=Addr64Bit;53osal_cpyExtAddr(BindAddr.addr.extAddr,Uart_buf[1]);54bindAddEntry(ZG_Serial_Control_ENDPOINT,55BindAddr,56ZG_Serial_Control_ENDPOINT,57ZG_Serial_Control_MAX_CLUSTERS,58ZG_Serial_Control_ClusterList);绑定表删除绑定条目:59BindAddr.addrMode=Addr64Bit;60osal_cpyExtAddr(BindAddr.addr.extAddr,Uart_buf[1]);61pBindingTable=bindFindExisting(ZG_Serial_Control_ENDPOINT,62BindAddr,63ZG_Serial_Control_ENDPOINT);6465bindRemoveEntry(pBindingTable);