MATLAB Examples

ZigBee Light Link Frame Generation and Decoding

This example shows how to generate and decode frames of the ZigBee® Light Link application profile [ 1 ] using the Communications System Toolbox™ Library for the ZigBee® protocol.

Contents

Background

The ZigBee standard [ 2 ] specifies network (NET or NWK) and application (APP or APL) layers of low-rate wireless personal area networks (LR-WPANs). These NET- and APP-layer specifications build upon the PHY and MAC specifications of IEEE 802.15.4 [ 3 ]. ZigBee devices find application in home automation and sensor networking and are highly relevant to the Internet of Things (IoT) trend.

The ZigBee application layer consists of multiple sub-layers: (i) the Application Support Sublayer (APS), and (ii) the ZigBee Cluster Library (ZCL).

The APS and ZCL headers follow a format that is common for all application profiles and ZigBee clusters (see Clauses 2.2.5 in [ 2 ] and 2.4 in [ 4 ], respectively). The ZCL payload is used only by some clusters and it follows a cluster-specific format. The generic APS and ZCL header generation and decoding is illustrated in a different example. This example illustrates the cluster-specific generation and decoding of ZigBee Light Link ZCL payloads.

Clusters and Commands

Out of the 7 clusters specified in the Light Link application profile [ 1 ], this example generates and decodes frames for the following clusters:

  1. Identify cluster: This cluster sets a device into identification mode (e.g., flashing a light). This example illustrates frame generation and decoding for the Identify command (described in Clause 3.5 of [ 4 ]).
  2. Color Control cluster: This cluster changes the color of a lighting device. This example illustrates frame generation and decoding for the Move to Color command (described in Clause 5.2 of [ 4 ]).
  3. Level Control cluster: This cluster modifies the level of a device, e.g., the intensity of a light bulb, how closed a door is, or the intensity of a heater. This example illustrates frame generation and decoding for the Move to Level command (described in Clause 3.10 of [ 4 ]).
  4. Scenes cluster: The scenes cluster sets up and recalls scenes (i.e., sets of stored attribute values for other clusters in the same device). This example illustrates frame generation and decoding for the View Scene command (described in Clause 3.7 of [ 4 ]).
  5. Group cluster: This cluster manages groups of devices, e.g., by creating or removing a group, or by discovering group membership. This example illustrates frame generation and decoding for the Add group command (described in Clause 3.6 of [ 4 ]).

In addition to the illustrated commands, this example provides an implementation for generating and decoding frames for all commands of the five mentioned clusters (see this section for a complete list).

Generating and Decoding ZCL Payload of Identify Cluster

A zigbee.IdentifyFrameConfig configuration object is used both in generating and decoding ZCL payloads of the Identify cluster. Such objects describe an Identify cluster payload and all applicable properties. The zigbee.IdentifyFrameGenerator function accepts a zigbee.IdentifyFrameConfig object describing the Identify cluster payload and outputs the generated payload in bytes. The following code creates a ZCL payload for a command asking a device to identify for 4 seconds.

% Creation of configuration object for Identify cluster
identifyConfigTx = zigbee.IdentifyFrameConfig('CommandType', 'Identify', ...
                                              'IdentifyTime', 4);

% Frame generation (ZCL payload) for Identify cluster
identifyPayload = zigbee.IdentifyFrameGenerator(identifyConfigTx);

The zigbee.IdentifyFrameDecoder function accepts the command name and a Identify cluster payload in bytes and outputs a zigbee.IdentifyFrameConfig object describing the Identify cluster payload. The command name is retrieved from the decoding of the ZCL header.

identifyConfigRx = zigbee.IdentifyFrameDecoder('Identify', identifyPayload) %#ok<*NOPTS>
identifyConfigRx = 

  IdentifyFrameConfig with properties:

     CommandType: 'Identify'
    IdentifyTime: 4

The following code visualizes a "software bulb" that illustrates the identification effect specified in the received frame.

bulb = plotBulb('white');
zigbeeIdentifyBulb(bulb, identifyConfigRx.IdentifyTime);
close(bulb);

Generating and Decoding ZCL Payload of Color Control Cluster

A zigbee.ColorControlFrameConfig configuration object is used both in generating and decoding ZCL payloads of the Color Control cluster. Such objects describe a Color Control cluster payload and all applicable properties. The zigbee.ColorControlFrameGenerator function accepts a zigbee.ColorControlFrameConfig object describing the Color Control cluster payload and outputs the generated payload in bytes. The following code generates a Color Control cluster payload that instructs a lighting device to progressively change its current color (red) to a different value (green) within 50 deciseconds (i.e., 5 seconds). Color is described in terms of x, y values according to the CIE 1931 color space established by the International Commission on Illumination (CIE) [ 5 ].

bulb = plotBulb('red');
% Creation of configuration object for Color Control cluster
colorCtrlConfigTx = zigbee.ColorControlFrameConfig('CommandType', 'Move to Color', ...
                                                   'ColorX', 16384, 'ColorY', 39322, 'Time', 50);

% Frame generation (ZCL payload) for Color Control cluster
colorControlPayload = zigbee.ColorControlFrameGenerator(colorCtrlConfigTx);

The zigbee.ColorControlFrameDecoder function accepts the command name and a Color Control cluster payload in bytes and outputs a zigbee.ColorControlFrameConfig object describing the Color Control cluster payload. The command name is retrieved from the decoding of the ZCL header.

colorCtrlConfigRx = zigbee.ColorControlFrameDecoder('Move to Color', colorControlPayload)
colorCtrlConfigRx = 

  ColorControlFrameConfig with properties:

    CommandType: 'Move to Color'
         ColorX: 16384
         ColorY: 39322
           Time: 50

The following command uses a "software bulb" to visualize the Color Control effect specified in the received frame. Specifically, the color of a bulb progressively changes from red to green within 5 seconds.

zigbeeMoveBulbColor(bulb, colorCtrlConfigRx.ColorX, colorCtrlConfigRx.ColorY, colorCtrlConfigRx.Time);

Next, the same effect occurs on a different color trajectory (from green to violet).

colorCtrlConfigTx2 = zigbee.ColorControlFrameConfig('CommandType', 'Move to Color', ...
                                                   'ColorX',  19661, 'ColorY', 6554, 'Time', 50);
colorControlPayload2 = zigbee.ColorControlFrameGenerator(colorCtrlConfigTx2);
colorCtrlConfigRx2 = zigbee.ColorControlFrameDecoder('Move to Color', colorControlPayload2);
zigbeeMoveBulbColor(bulb, colorCtrlConfigRx2.ColorX, colorCtrlConfigRx2.ColorY, colorCtrlConfigRx2.Time);

pause(1.5);

Generating and Decoding ZCL Payload of Level Control Cluster

A zigbee.LevelControlFrameConfig configuration object is used both in generating and decoding Level Control cluster ZCL payloads. Such objects describe a Level Control cluster payload and all applicable properties. The zigbee.LevelControlFrameGenerator function accepts a zigbee.LevelControlFrameConfig object describing the Level Control cluster payload and outputs the generated payload in bytes. The following code creates a Level Control cluster payload that instructs a device to change its current level to the specified value.

% Creation of Level Control cluster configuration object
levelCtrlConfigTx = zigbee.LevelControlFrameConfig('CommandType', 'Move to Level', ...
                                                   'Level', 20, 'TransitionTime', 1);

% Level Control cluster frame generation (ZCL payload)
levelControlPayload = zigbee.LevelControlFrameGenerator(levelCtrlConfigTx);

The zigbee.LevelControlFrameDecoder function accepts the command name and a Level Control cluster payload in bytes and outputs a zigbee.LevelControlFrameConfig object describing the Level Control cluster payload. The command name is retrieved from the decoding of the ZCL header.

levelCtrlConfigRx = zigbee.LevelControlFrameDecoder('Move to Level', levelControlPayload)
levelCtrlConfigRx = 

  LevelControlFrameConfig with properties:

       CommandType: 'Move to Level'
             Level: 20
    TransitionTime: 1

While the Level Control cluster can be used to regulate the intensity of a light, the Color Control cluster leaves it to the Level Control cluster to control the luminance of a lighting device's color. The following example uses the received Level Control frame to increase the luminance level of a light bulb.

zigbeeMoveBulbColor(bulb, colorCtrlConfigRx2.ColorX, colorCtrlConfigRx2.ColorY, 1, levelCtrlConfigRx.Level);

Generating and Decoding ZCL Payload of Scenes Cluster

A zigbee.SceneFrameConfig configuration object is used both in generating and decoding Scenes cluster ZCL payloads. Such objects describe a Scenes cluster payload and all applicable properties. The zigbee.ScenesFrameGenerator function accepts a zigbee.ScenesFrameConfig object describing the Scenes cluster payload and outputs the generated payload in bytes. The following code generates a Scenes cluster payload that requests a device to transmit a different frame (View Scene Response) describing a scene.

% Creation of Scenes cluster configuration object
scenesConfigTx = zigbee.ScenesFrameConfig('CommandType', 'View Scene', ...
                                          'GroupID', '1234', 'SceneID', '56');

% Scenes cluster frame generation (ZCL payload)
scenesPayload = zigbee.ScenesFrameGenerator(scenesConfigTx);

The zigbee.SceneFrameDecoder function accepts the command name and a Scenes cluster payload in bytes and outputs a zigbee.SceneFrameConfig object describing the Scenes cluster payload. The command name is retrieved from the decoding of the ZCL header.

scenesConfigRx = zigbee.ScenesFrameDecoder('View Scene', scenesPayload)
scenesConfigRx = 

  ScenesFrameConfig with properties:

    CommandType: 'View Scene'
        GroupID: '1234'
        SceneID: '56'

Generating and Decoding ZCL Payload of Groups Cluster

A zigbee.GroupFrameConfig configuration object is used both in generating and decoding Groups cluster ZCL payloads. Such objects describe a Groups cluster payload and all applicable properties. The zigbee.GroupsFrameGenerator function accepts a zigbee.GroupsFrameConfig object describing the Groups cluster payload and outputs the generated payload in bytes. The following code creates a Groups cluster payload that instructs a device to add the specified group to its Group table.

% Creation of Groups cluster configuration object
groupsConfigTx = zigbee.GroupsFrameConfig('CommandType', 'Add group', ...
                        'GroupName', 'Dining Hall', 'GroupID', '1234');

% Groups cluster frame generation (ZCL payload)
groupsPayload = zigbee.GroupsFrameGenerator(groupsConfigTx);

The zigbee.GroupFrameDecoder function accepts the command name and a Groups cluster payload in bytes and outputs a zigbee.GroupFrameConfig object describing the Groups cluster payload. The command name is retrieved from the decoding of the ZCL header.

groupsConfigRx = zigbee.GroupsFrameDecoder('Add group', groupsPayload)
groupsConfigRx = 

  GroupsFrameConfig with properties:

    CommandType: 'Add group'
        GroupID: '1234'
      GroupName: 'Dining Hall'

Wireshark Decoding

The generated frames can be converted to a PCAP format, which can be analyzed and visualized with Wireshark [ 6 ]. This process can serve as an additional verification step advocating that the Communications System Toolbox Library for the ZigBee Protocol generates and decodes frames in a standard-compliant manner.

The PCAP file needs the ZCL payloads to be enclosed with headers from all other layers and sublayers (MAC, NET, APS, ZCL). The following commands generate a PCAP file, for the ZCL payloads generated in this example, that can be loaded with Wireshark.

% ZLL profile ID
zllProfileID = zigbee.profileID('Light Link');

payloadsWithInfo(1) = struct('Payload',   identifyPayload,       'ProfileID',   zllProfileID, ...
                             'ClusterSpecific', true,            'ClusterID',   zigbee.clusterID('Identify'),       'CommandType', 'Identify',      'Direction', 'Downlink');
payloadsWithInfo(2) = struct('Payload',   colorControlPayload,   'ProfileID',   zllProfileID, ...
                             'ClusterSpecific', true,            'ClusterID',   zigbee.clusterID('Color Control'),  'CommandType', 'Move to Color', 'Direction', 'Downlink');
payloadsWithInfo(3) = struct('Payload',   levelControlPayload,   'ProfileID',   zllProfileID, ...
                             'ClusterSpecific', true,            'ClusterID',   zigbee.clusterID('Level Control'),  'CommandType', 'Move to Level', 'Direction', 'Downlink');
payloadsWithInfo(4) = struct('Payload',   scenesPayload,         'ProfileID',   zllProfileID, ...
                             'ClusterSpecific', true,            'ClusterID',   zigbee.clusterID('Scenes'),         'CommandType', 'View Scene',    'Direction', 'Downlink');
payloadsWithInfo(5) = struct('Payload',   groupsPayload,         'ProfileID',   zllProfileID, ...
                             'ClusterSpecific', true,            'ClusterID',   zigbee.clusterID('Groups'),         'CommandType', 'Add group',     'Direction', 'Downlink');

% Add headers from other layers/sublayers:
MPDUs = zigbeeAddProtocolHeaders(payloadsWithInfo);

% Export MPDUs to a PCAP format
zigbeeExportToPcap(MPDUs, 'zigbeeLightLink.pcap');

% Open PCAP file with Wireshark

Further Exploration

You can further explore the following generator and decoding functions, as well as the configuration object:

In addition to the commands illustrated in this example, the offered implementation also supports the commands listed in the following table. The commands listed in the middle column can be exported to a PCAP file that can be analyzed with Wireshark.

Selected Bibliography

  1. ZigBee Alliance, ZigBee Light Link Standard, v. 1.0, April 5th, 2012.
  2. ZigBee Alliance, ZigBee Specification Document 053474r17, 2007
  3. IEEE 802.15.4-2011 - IEEE Standard for Local and metropolitan area networks--Part 15.4: Low-Rate Wireless Personal Area Networks (LR-WPANs)
  4. ZigBee Alliance, ZigBee Cluster Library Specification, Revision 6, Jan. 2016.
  5. CIE 1931 Color Space. Commission Internationale de l'Eclairage Proceedings. Cambridge University Press, Cambridge
  6. Wireshark software: https://www.wireshark.org/