zigbee 6:绑定

来源:互联网 发布:系统助手 for mac 编辑:程序博客网 时间:2024/06/05 12:07

2015-03-03 星期二 21:52:42

1、绑定表

绑定服务只能在“互补”设备之间建立。那就是,只有分别在两个节点的简单描述结构体(simple descriptor structure)中,同时注册了相同的命令标识符(command_id)并且方向相反(一个属于输出指令“output”,另一个属于输入指令“input”),才能成功建立绑定。 
APS邦定表是在静态RAM中定义的一张表,定义在nwk_globals.c中。表的大小可以通过f8wConfig.cfg中的【NWK_MAX_BINDING_ENTRIES和MAX_BINDING_CLUSTER_IDS】莱配置。只有定义了REFLECTOR或者COORDINATOR_BINDING才能包含此表,用REFLECTOR编译选项来支持APS层的源邦定。 
也可以将绑定表保存在Flash里(NV_RESTORE编译选项激活)。

2、绑定节点

绑定指的是两个节点在应用层上建立起来的一条逻辑链路。同一个节点上可以建立多个绑定服务,分别对应不同种类的数据包。此外,绑定也允许有多个目标节点(一对多绑定)。

配置设备绑定服务,有两种机制可供选择

  • 如果目标设备的扩展地址(64位地址)已知,可通过调用zb_BindDeviceRequest()建立绑定条目。采用的目的地址是64位扩展地址。

  • 如果目标设备的扩展地址未知,可实施一个按键策略实现绑定。这时,目标设备将首先进入一个允许绑定的状态,并通过zb_AllowBindResponse()对配对请求作出响应。然后,在源节点中执行zb_BindDeviceRequest()目标地址设为无效)可实现绑定。采用的目的地址是16位网络地址。

3、方式1:自动绑定 ZDP_MatchDescReq()

本地节点以广播的方式发送请求。无需协调器。

  • 流程

  • Init

    1
    2
    3
    4
    5
    //需要注册,才能收到ZDP_MatchDesc.reponse(ZDO_CB_MSG中的Match_Desc_rsp)
      ZDO_RegisterForZDOMsg( GenericApp_TaskID, Match_Desc_rsp );
     
    //此外还需要设置f8wConfig.cfg 中的
    //-DRFD_RCVC_ALWAYS_ON = TRUE
  • 本地节点发送请求匹配
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    static void GenericApp_HandleKeys( uint8 shift, uint8 keys )
    {
      ......
        if ( keys & HAL_KEY_SW_4 )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
          // Initiate a Match Description Request (Service Discovery)
          dstAddr.addrMode = AddrBroadcast;
          dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
          ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
                            GENERICAPP_PROFID,
                            GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                            GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                            FALSE );
        }
      
      ......
    }

  • 匹配方响应后发送数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
    {
      ......
            switch ( MSGpkt->hdr.event )
          {
            case ZDO_CB_MSG:
              GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
              ......
            }
      ......
    }
         
    //zlz,GenericApp_DstAddr保存后作为目的通信地址。
    static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
    {
      ......
    case Match_Desc_rsp:
          {
            ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
            if ( pRsp )
            {
              if ( pRsp->status == ZSuccess && pRsp->cnt )
              {
                GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
                GenericApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
                // Take the first endpoint, Can be changed to search through endpoints
                GenericApp_DstAddr.endPoint = pRsp->epList[0];
       
                // Light LED
                HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
              }
              osal_mem_free( pRsp );
            }
          }
          break;
      ......
    }
     
    static void GenericApp_SendTheMessage( void )
    {
      char theMessageData[] = "Hello World";
     
      if ( AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                           GENERICAPP_CLUSTERID,
                           (byte)osal_strlen( theMessageData ) + 1,
                           (byte *)&theMessageData,
                           &GenericApp_TransID,
                           AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
      {
        // Successfully requested to be sent.
      }
      else
      {
        // Error occurred in request to send.
      }
    }

4、方式2:手动绑定ZDP_EndDeviceBindReq ()

需要协调器。

  • 流程


  • Init

    1
    2
    //需要注册,才能收到ZDP_MatchDesc.reponse(ZDO_CB_MSG中的End_Device_Bind_rsp)
        ZDO_RegisterForZDOMsg( GenericApp_TaskID, End_Device_Bind_rsp );

  • 本地节点ZDP_EndDeviceBindReq()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    static void GenericApp_HandleKeys( uint8 shift, uint8 keys )
    {
      ......
       if ( keys & HAL_KEY_SW_2 )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
     
          // Initiate an End Device Bind Request for the mandatory endpoint
          dstAddr.addrMode = Addr16Bit;
          dstAddr.addr.shortAddr = 0x0000; // Coordinator
          ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                GenericApp_epDesc.endPoint,
                                GENERICAPP_PROFID,
                                GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                                GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
                                FALSE );
        }
        
      ......
    }

0 0
原创粉丝点击