Main Content

IEEE 802.15.4 - MAC Frame Generation and Decoding

This example shows how to generate and decode MAC frames of the IEEE® 802.15.4™ standard [1] using the Communications Toolbox™.

Background

The IEEE 802.15.4 standard specifies the MAC and PHY layers of Low-Rate Wireless Personal Area Networks (LR-WPANs) [1]. The IEEE 802.15.4 MAC and PHY layers provide the basis of other higher-layer standards, such as ZigBee, WirelessHart®, 6LoWPAN and MiWi. Such standards find application in home automation and sensor networking and are highly relevant to the Internet of Things (IoT) trend.

Architecture

The IEEE 802.15.4 MAC layer inserts a MAC header and a MAC footer before and after a network-layer frame, respectively. The MAC footer contains a CRC check.

A lrwpan.MACFrameConfig configuration object is used both in generating and decoding IEEE 802.15.4 MAC frames. Such objects describe a MAC frame and specify its frame type and all applicable properties.

The lrwpan.MACFrameGenerator function accepts an lrwpan.MACFrameConfig object describing the frame, and optionally a MAC-layer payload (NET-layer frame) in bytes (two-characters), and outputs the MAC frame in bits.

The lrwpan.MACFrameDecoder function accepts a MAC protocol data unit (MPDU) in bits and outputs a lrwpan.MACFrameConfig object describing the frame and possibly a NET-layer frame in bytes. Clause 5 in [1] describes the MAC frame formats.

Decode MAC Frames of Home Automation ZigBee Radios

This section decodes MAC frames transmitted from commercial ZigBee radios enabling home automation, and captured using a USRP® B200-mini radio and the Communications Toolbox Support Package for USRP® radio. The PHY layer of the captured waveforms has been decoded according to the methodology described in the Recovery of IEEE 802.15.4 OQPSK Signals example. The resulting MPDUs are stored into a MAT file.

load lrwpanMACCaptures

First, a data frame is decoded.

[dataFrameMACConfig, netFrame] = lrwpan.MACFrameDecoder(MPDU_data);
if ~isempty(dataFrameMACConfig)
    fprintf('CRC check passed for the MAC frame.\n');
    dataFrameMACConfig
end
CRC check passed for the MAC frame.
dataFrameMACConfig = 
  MACFrameConfig with properties:

                       FrameType: 'Data'

   General MAC properties:
                  SequenceNumber: 244
           AcknowledgmentRequest: 1
           DestinationAddressing: 'Short address'
        DestinationPANIdentifier: '1E16'
              DestinationAddress: '35EA'
                SourceAddressing: 'Short address'
                   SourceAddress: '0000'
    PANIdentificationCompression: 1
                    FramePending: 0
                    FrameVersion: '2003'
                        Security: 0

   Security properties:
    No properties.

   Beacon properties:
    No properties.

   "MAC Command" properties:
    No properties.

Next, an acknowledgment frame is decoded.

ackFrameMACConfig = lrwpan.MACFrameDecoder(MPDU_ack)
ackFrameMACConfig = 
  MACFrameConfig with properties:

                FrameType: 'Acknowledgment'

   General MAC properties:
           SequenceNumber: 165
    DestinationAddressing: 'Not present'
         SourceAddressing: 'Not present'
             FramePending: 0
             FrameVersion: '2003'
                 Security: 0

   Security properties:
    No properties.

   Beacon properties:
    No properties.

   "MAC Command" properties:
    No properties.

Generate MAC Frames

The lrwpan.MACFrameGenerator function can generate all MAC frame types from the IEEE 802.15.4 standard [1], such as 'Beacon', 'Data', 'Acknowledgment', and 'MAC Command' frame types. The MAC Command frame types can be further specified as: 'Association request', 'Association response', 'Disassociation notification', 'Data request', 'PAN ID conflict notification', 'Orphan notification', 'Beacon request', and 'GTS request'.

This code illustrates how to generate frames for all frame types.

% Beacon
beaconConfig = lrwpan.MACFrameConfig('FrameType','Beacon');
beaconMACFrame = lrwpan.MACFrameGenerator(beaconConfig);

% Data
dataConfig = lrwpan.MACFrameConfig('FrameType','Data');
numOctets = 50;
payload = dec2hex(randi([0 2^8-1], numOctets, 1), 2);
dataMACFrame = lrwpan.MACFrameGenerator(dataConfig, payload);

% Acknowledgment
ackConfig = lrwpan.MACFrameConfig('FrameType','Acknowledgment');
ackFrame = lrwpan.MACFrameGenerator(ackConfig);

% MAC Command
commandConfig = lrwpan.MACFrameConfig('FrameType','MAC Command');
commandConfig.MACCommand = 'Association request';
% Valid settings for MACCommand also include: 'Association response',
% 'Disassociation notification', 'Data request', 'PAN ID conflict
% notification', 'Orphan notification', 'Beacon request', and 'GTS request'.
commandFrame = lrwpan.MACFrameGenerator(commandConfig);

Further Exploration

The example uses these undocumented utilities. The API and functionality of undocumented utilities may change in the future. To view the source code of the utilities, use the edit function.

  • lrwpan.MACFrameGenerator and lrwpan.MACFrameDecoder: Create and decode an IEEE 802.15.4 MAC frame.

  • lrwpan.MACFrameConfig: Create an IEEE 802.15.4 frame configuration.

References

1 - "IEEE Standard for Local and Metropolitan Area Networks--Part 15.4: Low-Rate Wireless Personal Area Networks (LR-WPANs)," in IEEE Std 802.15.4-2011 (Revision of IEEE Std 802.15.4-2006) , vol., no., pp.1-314, 5 Sept. 2011, doi: 10.1109/IEEESTD.2011.6012487.

Related Topics