基于EFR32的Zigbee开发-自定义Cluster

在进行 Zigbee 开发时必然会用到除了标准 Cluster 之外的自定义内容,在 Simplicity studio 开发平台是通过勾选配置的形式来启用对应 Cluster的,平台中并没有添加自定义的接口,这篇文章就主要分享一下如何在 Simplicity studio 开发平台添加自定义的 Cluster。

添加新 Cluster

D:\v4\developer\sdks\gecko_sdk_suite\v2.7\app\zcl 路径中,找到在相关配置生成 Cluster 位置,可以参考原有的示例文件 sample-extensions.xml 来了解一下相关格式。

<domain name="Ember" />
  <!-- Use manufacturerCode to indicate that this is a manufacturer specific cluster. -->
<cluster manufacturerCode="0x1002">
    <name>Sample Mfg Specific Cluster</name>
    <domain>Ember</domain>
    <description>This cluster provides an example of how the Application 
      Framework can be extended to include manufacturer specific clusters.
      </description>
    <!-- Cluster Id must be within the mfg spec range 0xfc00 - 0xffff -->
    <code>0xFC00</code> 
    <define>SAMPLE_MFG_SPECIFIC_CLUSTER</define>
    <client init="false" tick="false">true</client>
    <server init="false" tick="false">true</server>
    <attribute side="server" code="0x0000" define="ATTRIBUTE_ONE" 
      type="INT8U"  min="0x00"   max="0xFF"   writable="true"  
      default="0x00" optional="true">ember sample attribute</attribute>
    <attribute side="server" code="0x0001" define="ATTRIBUTE_TWO" 
      type="INT8U"  min="0x00"   max="0xFF"   writable="true"  
      default="0x00" optional="true">ember sample attribute 2</attribute>
    <command source="client" code="0x00" 
      name="CommandOne" 
      optional="true">
      <description>
        A sample manufacturer specific command within the sample manufacturer specific
        cluster.
      </description>
      <arg name="argOne" type="INT8U" />
    </command>
</cluster>

其中 domain 是来设置分类的,codeCluster IDattribute 是来设置右侧的不同事件,根据在 client 还是 server 字段下方以及其中的 side 字段来区分属于客户端还是服务端的事件,attribute 字段中 code 用来设置 attribute ID ,其他字段可以设置数据类型、最大值、最小值、默认值、是否可写等,command 字段用来设置自定义命令,可以对照着在开发平台生成的页面来理解。

在这里插入图片描述

需要注意的是,根据 ZCL 规范,自定义的 Cluster 字段要在 0xFC00 – 0xFFFF 区间。

在既有的 Cluster 中添加新的 attribute

如果是在已有的 cluster 中添加新的 attribute 和 command ,可以参考如下,相对来说简单一些,code 字段为要添加的 Cluster。

  <clusterExtension code="0x0006"> 
    <attribute side="server" code="0x0000" 
      define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME" 
      type="INT16U" min="0x0000" max="0xFFFF" writable="true" 
      default="0x0000" optional="true" 
      manufacturerCode="0x1002">Sample Mfg Specific Attribute: 0x0000 0x1002</attribute>
      
    <command source="client" code="0x00" 
      name="SampleMfgSpecificOffWithTransition" optional="true" manufacturerCode="0x1002">
      <description>Client command that turns the device off with a transition given
        by the transition time in the Ember Sample transition time attribute.</description>
    </command>
  </clusterExtension>

值得一说的是 manufacturerCode 字段用来指定厂商的编号,这一项可以用来设置同一个 Cluster 中使用 attribute ID,业务逻辑可以通过这一字段进行分离。 在这里插入图片描述

官方相关介绍可参考:https://github.com/Jim-tech/IoT-Developer-Boot-Camp/wiki/Zigbee-Custom-Clusters-CN