Main Content

Generate and Decode Bluetooth Protocol Data Units

The Bluetooth® Toolbox enables you to model the Bluetooth low energy (LE) link layer (LL), the logical link control and adaptation protocol (L2CAP), the generic access profile (GAP), the attribute protocol (ATT) layer and the Bluetooth basic rate/enhanced data rate (BR/EDR) baseband and link control layer. Use the configuration objects and functions to parameterize, generate, and decode the Bluetooth protocol data units (PDUs) based on Bluetooth Core Specification 5.3 [2].

Generate and Decode Bluetooth LE ATT PDU

This example enables you to generate and decode a Bluetooth LE ATT PDU using the bleATTPDU and bleATTPDUDecode functions, respectively. Use the bleATTPDUConfig configuration object to set the configuration parameters for Bluetooth LE ATT PDU generation.

Create a Bluetooth LE ATT PDU configuration object, specifying the opcode as "Information request".

cfgATT = bleATTPDUConfig(Opcode="Information request")
cfgATT = 
  bleATTPDUConfig with properties:

         Opcode: 'Information request'
    StartHandle: '0001'
      EndHandle: 'FFFF'

Generate a Bluetooth LE ATT PDU.

attPDU = bleATTPDU(cfgATT)
attPDU = 5x2 char array
    '04'
    '01'
    '00'
    'FF'
    'FF'

Decode the generated Bluetooth LE ATT PDU. The returned status indicates decoding is successful.

[status,cfgATTDecode] = bleATTPDUDecode(attPDU)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgATTDecode = 
  bleATTPDUConfig with properties:

         Opcode: 'Information request'
    StartHandle: '0001'
      EndHandle: 'FFFF'

Generate and Decode Bluetooth LE LL Data Channel PDU

This example enables you to generate and decode a Bluetooth LE LL data channel PDU using the bleLLDataChannelPDU and bleLLDataChannelPDUDecode functions, respectively. Use the bleLLDataChannelPDUConfig configuration object to set the configuration parameters for Bluetooth LE LL data channel PDU generation.

Create a default Bluetooth LE LL data channel PDU configuration object for a data PDU. Set the link layer identifier to "Data (start fragment/complete)".

cfgLLData = bleLLDataChannelPDUConfig
cfgLLData = 
  bleLLDataChannelPDUConfig with properties:

                 LLID: 'Data (continuation fragment/empty)'
                 NESN: 0
       SequenceNumber: 0
             MoreData: 0
    CRCInitialization: '012345'

cfgLLData.LLID = "Data (start fragment/complete)"
cfgLLData = 
  bleLLDataChannelPDUConfig with properties:

                 LLID: 'Data (start fragment/complete)'
                 NESN: 0
       SequenceNumber: 0
             MoreData: 0
    CRCInitialization: '012345'

Set the CRC value.

cfgLLData.CRCInitialization = '123456';

Generate a Bluetooth LE LL data channel PDU by using the created configuration object and the upper-layer payload, "030004000A0100".

dataPDU = bleLLDataChannelPDU(cfgLLData,"030004000A0100")
dataPDU = 96×1

     0
     1
     0
     0
     0
     0
     0
     0
     1
     1
      ⋮

Decode the generated Bluetooth LE LL data channel PDU by initializing the cyclic redundancy check (CRC) value. The returned status indicates decoding is successful.

crcInit = '123456';
[status,cfgRx,llPayload] = bleLLDataChannelPDUDecode(dataPDU,crcInit)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgRx = 
  bleLLDataChannelPDUConfig with properties:

                 LLID: 'Data (start fragment/complete)'
                 NESN: 0
       SequenceNumber: 0
             MoreData: 0
    CRCInitialization: '123456'

llPayload = 7x2 char array
    '03'
    '00'
    '04'
    '00'
    '0A'
    '01'
    '00'

Generate and Decode Bluetooth LE LL Control PDU

This example enables you to generate and decode a Bluetooth LE LL control PDU using the bleLLDataChannelPDU and bleLLDataChannelPDUDecode functions, respectively. Use the bleLLControlPDUConfig configuration object to set the configuration parameters for Bluetooth LE LL control PDU generation.

Create a default Bluetooth LE LL control PDU configuration object.

cfgControl = bleLLControlPDUConfig
cfgControl = 
  bleLLControlPDUConfig with properties:

                Opcode: 'Connection update indication'
            WindowSize: 1
          WindowOffset: 0
    ConnectionInterval: 6
     PeripheralLatency: 0
     ConnectionTimeout: 10
               Instant: 0

Create a Bluetooth LE LL data channel PDU configuration object, specifying the control PDU configuration object.

cfgLLData = bleLLDataChannelPDUConfig(ControlConfig=cfgControl);

Specify the link layer identifier and CRC initialization values.

cfgLLData.LLID = "Control";
cfgLLData.CRCInitialization = "E23456"
cfgLLData = 
  bleLLDataChannelPDUConfig with properties:

                 LLID: 'Control'
                 NESN: 0
       SequenceNumber: 0
             MoreData: 0
    CRCInitialization: 'E23456'
        ControlConfig: [1x1 bleLLControlPDUConfig]

Generate a Bluetooth LE LL control PDU.

controlPDU = bleLLDataChannelPDU(cfgLLData)
controlPDU = 136×1

     1
     1
     0
     0
     0
     0
     0
     0
     0
     0
      ⋮

Decode the generated Bluetooth LE LL control PDU by initializing the CRC value. The returned status indicates decoding is successful.

crcInit = 'E23456';                                                              % Received during associaton
[status,cfgLLControl,llPayload] = bleLLDataChannelPDUDecode(controlPDU,crcInit)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgLLControl = 
  bleLLDataChannelPDUConfig with properties:

                 LLID: 'Control'
                 NESN: 0
       SequenceNumber: 0
             MoreData: 0
    CRCInitialization: 'E23456'
        ControlConfig: [1x1 bleLLControlPDUConfig]

llPayload =

  1x0 empty char array

Generate and Decode Bluetooth LE LL Advertising Channel PDU

This example enables you to generate and decode a Bluetooth LE LL Advertising Channel PDU using the bleLLAdvertisingChannelPDU and bleLLAdvertisingChannelPDUDecode functions, respectively. Use the bleLLAdvertisingChannelPDUConfig configuration object to set the configuration parameters for Bluetooth LE LL Advertising Channel PDU generation.

Create a default Bluetooth LE LL Advertising Channel PDU configuration object. Specify the PDU type as "Scan response".

cfgLLAdv = bleLLAdvertisingChannelPDUConfig;
cfgLLAdv.PDUType = "Scan response";

Set the channel selection algorithm to "Algorithm2" and hop increment count to 10.

cfgLLAdv.ChannelSelection = "Algorithm2";
cfgLLAdv.HopIncrement = 10
cfgLLAdv = 
  bleLLAdvertisingChannelPDUConfig with properties:

                  PDUType: 'Scan response'
    AdvertiserAddressType: 'Random'
        AdvertiserAddress: '0123456789AB'
         ScanResponseData: [3x2 char]

Generate a Bluetooth LE LL Advertising Channel PDU.

advLLPDU = bleLLAdvertisingChannelPDU(cfgLLAdv)
advLLPDU = 112×1

     0
     0
     1
     0
     0
     1
     1
     0
     1
     0
      ⋮

Decode the generated Bluetooth LE LL Advertising Channel PDU. The returned status indicates decoding is successful.

[status,cfgLLAdvDecode] = bleLLAdvertisingChannelPDUDecode(advLLPDU)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgLLAdvDecode = 
  bleLLAdvertisingChannelPDUConfig with properties:

                  PDUType: 'Scan response'
    AdvertiserAddressType: 'Random'
        AdvertiserAddress: '0123456789AB'
         ScanResponseData: [3x2 char]

Generate and Decode Bluetooth LE GAP Data Block

This example enables you to generate and decode a Bluetooth LE GAP data block using the bleGAPDataBlock and bleGAPDataBlockDecode functions, respectively. Use the bleGAPDataBlockConfig configuration object to set the configuration parameters for Bluetooth LE GAP data block generation.

Create a Bluetooth LE GAP data block configuration object, specifying the advertising data type as "Advertising interval" and enabling simultaneous support for LE and BR/EDR modes at the Host.

cfgGAP = bleGAPDataBlockConfig(AdvertisingDataTypes="Advertising interval",LE="Host")
cfgGAP = 
  bleGAPDataBlockConfig with properties:

    AdvertisingDataTypes: {'Advertising interval'}
     AdvertisingInterval: 32

Generate a Bluetooth LE GAP data block.

dataBlock = bleGAPDataBlock(cfgGAP)
dataBlock = 4x2 char array
    '03'
    '1A'
    '20'
    '00'

Decode the generated Bluetooth LE GAP AD block. The returned status indicates decoding is successful.

[status,cfgGAP] = bleGAPDataBlockDecode(dataBlock)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgGAP = 
  bleGAPDataBlockConfig with properties:

    AdvertisingDataTypes: {'Advertising interval'}
     AdvertisingInterval: 32

Generate and Decode Bluetooth LE L2CAP Frame

This example enables you to generate and decode a Bluetooth LE L2CAP frame using the bleL2CAPFrame and bleL2CAPFrameDecode functions, respectively. Use the bleL2CAPFrameConfig configuration object to set the configuration parameters for Bluetooth LE L2CAP frame generation.

Create a default Bluetooth LE L2CAP frame configuration object, specifying the signaling command type as "Credit Based Connection request".

cfgL2CAP = bleL2CAPFrameConfig(CommandType="Credit based connection request")
cfgL2CAP = 
  bleL2CAPFrameConfig with properties:

          ChannelIdentifier: '0005'
                CommandType: 'Credit based connection request'
           SignalIdentifier: '01'
    SourceChannelIdentifier: '0040'
                      LEPSM: '001F'
        MaxTransmissionUnit: 23
          MaxPDUPayloadSize: 23
                    Credits: 1

Generate a Bluetooth LE L2CAP frame.

L2CAPFrame = bleL2CAPFrame(cfgL2CAP)
L2CAPFrame = 18x2 char array
    '0E'
    '00'
    '05'
    '00'
    '14'
    '01'
    '0A'
    '00'
    '1F'
    '00'
    '40'
    '00'
    '17'
    '00'
    '17'
    '00'
    '01'
    '00'

Decode the generated Bluetooth LE L2CAP frame. The returned status indicates decoding is successful.

[status,cfgL2CAP,sdu] = bleL2CAPFrameDecode(L2CAPFrame)
status = 
  blePacketDecodeStatus enumeration

    Success

cfgL2CAP = 
  bleL2CAPFrameConfig with properties:

          ChannelIdentifier: '0005'
                CommandType: 'Credit based connection request'
           SignalIdentifier: '01'
    SourceChannelIdentifier: '0040'
                      LEPSM: '001F'
        MaxTransmissionUnit: 23
          MaxPDUPayloadSize: 23
                    Credits: 1

sdu =

  1x0 empty char array

References

[1] Bluetooth Technology Website. “Bluetooth Technology Website | The Official Website of Bluetooth Technology.” Accessed September 1, 2020. https://www.bluetooth.com/.

[2] Bluetooth Special Interest Group (SIG). "Bluetooth Core Specification." Version 5.3. https://www.bluetooth.com/.

Related Topics