BACnet.Protocol (bacstack v0.1.0-dev.1)

Copy Markdown View Source

This module is mostly used for basic decoding of BACnet frames (Protocol Data Units - PDU).

This module handles decoding of BVLL (and delegates specifics), NPCI and NSDU. APDU is completely covered by BACnet.Protocol.APDU.

For BACnet Virtual Link Layer (BVLL), it will handle it and delegate, once it determines it is a BVLC function. BVLC function codes such as distribute broadcast, original broad- and unicast and forwarded NPDU are handled by this module directly. Currently only BVLL type 0x81 (BACnet/IPv4) is implemented.

The five simple BVLC functions that only carry an NPDU (or nothing) are represented in this module as atoms inside the bvlc/0 union: :original_unicast, :original_broadcast, :distribute_broadcast_to_network. All seven management functions (Register-Foreign-Device, the BDT/FDT read/write operations, Delete-FDT-Entry, etc.) are represented by the richer structs in BACnet.Protocol.BvlcFunction, BACnet.Protocol.BvlcResult and BACnet.Protocol.BvlcForwardedNPDU.

For Network Protocol Control Information (NPCI), it will handle all decoding associated with it and handle field handling.

For Network Service Data Unit (NSDU), it will handle all decoding associated with the regular BACnet types, excluding reserved and vendor proprietary.

For Application Data Unit (APDU), see the BACnet.Protocol.APDU module.

See Also

Summary

Types

BACnet Application Data Units (APDU).

BACnet Virtual Link Control (BVLC), used in BACnet/IP (Annex J).

Functions

Decodes the BVLL header of a BACnet/IP packet (Annex J.2).

Decodes the NPCI header of a BACnet packet.

Decodes the NPDU of a BACnet packet.

Decodes the NSDU of a BACnet packet.

Types

apdu()

BACnet Application Data Units (APDU).

bvlc()

@type bvlc() ::
  BACnet.Protocol.BvlcForwardedNPDU.t()
  | BACnet.Protocol.BvlcFunction.t()
  | BACnet.Protocol.BvlcResult.t()
  | :distribute_broadcast_to_network
  | :original_broadcast
  | :original_unicast

BACnet Virtual Link Control (BVLC), used in BACnet/IP (Annex J).

This is the union of everything that can appear after a BVLL header (Type 0x81) on the wire. The five simple carriers are represented as atoms so that higher layers (especially BACnet.Stack.Transport.IPv4Transport) do not have to allocate structs for the common case of ordinary unicast/broadcast traffic.

VariantProduced by BVLC FunctionDescription
BvlcForwardedNPDU.t()0x04Broadcast or foreign-device traffic relayed by a BBMD
BvlcFunction.t()0x01, 0x02, 0x03, 0x05-0x08The seven management operations (BDT/FDT, Register, Delete)
BvlcResult.t()0x00Success/NAK reply for a management request
:original_unicast0x0ANormal directed NPDU (most common)
:original_broadcast0x0BLocal broadcast on a B/IP subnet
:distribute_broadcast_to_network0x09Foreign device asking its BBMD to broadcast on its behalf

Functions

decode_bvll(bvll_type, bvlc_function, data)

@spec decode_bvll(non_neg_integer(), non_neg_integer(), binary()) ::
  {:ok, {bvlc_size :: non_neg_integer(), bvlc :: bvlc(), rest :: binary()}}
  | {:error, term()}

Decodes the BVLL header of a BACnet/IP packet (Annex J.2).

The first two arguments are the BVLL Type (normally 0x81) and the BVLC Function code. The third argument is the remaining bytes after the 4-octet BVLL header. On success the function returns the number of bytes consumed by the BVLC payload (so the caller knows where the NPDU begins), the decoded bvlc/0 value, and any trailing bytes that belong to the NPDU.

Management functions (0x01-0x03, 0x05-0x08) are delegated to BACnet.Protocol.BvlcFunction.decode/2. The five simple NPDU carriers and the BVLC-Result (0x00) and Forwarded-NPDU (0x04) cases are handled directly and return the corresponding atom or struct.

decode_npci(data)

@spec decode_npci(binary()) ::
  {:ok, {BACnet.Protocol.NPCI.t(), rest :: binary()}} | {:error, term()}

Decodes the NPCI header of a BACnet packet.

decode_npdu(npci, data)

@spec decode_npdu(BACnet.Protocol.NPCI.t(), binary()) ::
  {:ok,
   {type :: :network | :apdu,
    BACnet.Protocol.NetworkLayerProtocolMessage.t() | binary()}}
  | {:error, term()}

Decodes the NPDU of a BACnet packet.

For network messages, it decodes the NSDU. For application messages, it simply returns the APDU for further processing.

decode_nsdu(data)

@spec decode_nsdu(binary()) ::
  {:ok, BACnet.Protocol.NetworkLayerProtocolMessage.t()} | {:error, term()}

Decodes the NSDU of a BACnet packet.