|
大家好!
大家都知道Z-stack有四种绑定方式:1、两个节点分别通过按键机制调用ZDP_EndDeviceBindReq函数; 2、Match方式; 3、ZDP_BindReq和ZDP_UnbindReq方式; 4、手工管理绑定表。很多人明白这四种绑定方式的原理,但具体的程序事项方法可能并不清楚,今天我在这里送上这四种绑定方式具体的程序时序实现方法,希望能对大家有所帮助。
一、两个节点分别通过按键机制调用ZDP_EndDeviceBindReq函数
这种绑定方式必须要有协调器设备存在,两个设备通过按键在一定时间间隔内(默认为15秒)调用ZDP_EndDeviceBindReq函数完成绑定。首先要在初始化程序中注册End_Device_Bind_rsp进程,然后按键绑定请求程序如下:- if(Up_down_mark==1)
- {
- Up_down_mark=0;
- HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
- // Initiate an End Device Bind Request for the mandatory endpoint
- dstAddr.addrMode = Addr16Bit;
- dstAddr.addr.shortAddr = 0x0000; // Coordinator
- ZDP_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 );
-
- }
复制代码 回调处理函数如下:- case End_Device_Bind_rsp:
- if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
- {
- HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
- }
- break;
复制代码 二、Match方式
这种绑定方式无需协调器设备存在,可采用按键机制来实现。首先在初始化函数中注册Match_Desc_rsp进程,然后按键请求函数如下:- if(Right_left_mark==1)
- {
- Right_left_mark=0;
- HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
- // Initiate a Match Description Request (Service Discovery)
- dstAddr.addrMode = AddrBroadcast;
- dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
- ZDP_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 );
- }
复制代码 回调处理函数如下:- case Match_Desc_rsp:
- {
- ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
- if ( pRsp )
- {
- if ( pRsp->status == ZSuccess && pRsp->cnt )
- {
- ZG_Serial_Control_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
- ZG_Serial_Control_DstAddr.addr.shortAddr = pRsp->nwkAddr;
- // Take the first endpoint, Can be changed to search through endpoints
- ZG_Serial_Control_DstAddr.endPoint = pRsp->epList[0];
- // Light LED
- HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF );
- }
- osal_mem_free( pRsp );
- }
- }
- break;
复制代码 三、ZDP_BindReq和ZDP_UnbindReq方式
这种绑定方式需要第三方设备辅助完成,即假如A设备和B设备想进行绑定,A为源设备,B为目标设备,这时需要一个C设备来发起绑定,C设备需要知道A设备和B设备端点号、簇ID号和64位IEEE 地址。首先需要注册Bind_rsp/Unbind_rsp,然后绑定请求函数如下:- destAddr.addrMode = Addr16Bit;
- destAddr.addr.shortAddr = BUILD_UINT16( Uart_buf[1], Uart_buf[2] ); //目标设备网络地址
- osal_cpyExtAddr(Bind_SourceAddr,&Uart_buf[3]);
-
- devAddr.addrMode = Addr64Bit;
- osal_cpyExtAddr( devAddr.addr.extAddr, &Uart_buf[11] );
-
- ZDP_BindReq( &destAddr,
- Bind_SourceAddr,
- ZG_Serial_Control_ENDPOINT,
- ZG_Serial_Control_CLUSTERID,
- &devAddr,
- ZG_Serial_Control_ENDPOINT,
- 0 );
复制代码 回调处理函数如下:- if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )//绑定成功
- HalLedSet( HAL_LED_2, HAL_LED_MODE_OFF );
复制代码 四、手工管理绑定表
这种方式是最灵活的方式,不过目前我只实现了子节点对其父节点的绑定,而无法实现对非父节点的绑定,原因尚不清楚,若有朋友清楚还请告诉我,谢谢!
绑定表添加绑定程序:- BindAddr.addrMode = Addr64Bit;
- osal_cpyExtAddr( BindAddr.addr.extAddr, &Uart_buf[1] );
- bindAddEntry(ZG_Serial_Control_ENDPOINT,
- &BindAddr,
- ZG_Serial_Control_ENDPOINT,
- ZG_Serial_Control_MAX_CLUSTERS,
- ZG_Serial_Control_ClusterList);
复制代码 绑定表删除绑定条目:- BindAddr.addrMode = Addr64Bit;
- osal_cpyExtAddr( BindAddr.addr.extAddr, &Uart_buf[1] );
- pBindingTable = bindFindExisting( ZG_Serial_Control_ENDPOINT,
- &BindAddr,
- ZG_Serial_Control_ENDPOINT );
-
- bindRemoveEntry( pBindingTable );
复制代码 |
|