icspacket
  • Examples
    • MMS Utilities Manufacturing Message Specification tools
    • DNP3 Tools DNP3 utilities and tools
    • IED Enumeration Tool Logical Device enumeration and more
    • CIP / EtherNet-IP Tools EtherNet/IP discovery, explicit messaging and I/O tools
    • IEC 60870-5-104 Tools IEC 60870-5-104 interrogation and read tools
    • BACnet/IP Tools BACnet/IP discovery, read/write and COV tools
  • Changelog
/
  • Discussion

Getting Started

  • Installation
  • Protocols Overview
  • Core API Reference

DNP3 / IEEE 1815

  • Reading Objects
  • Dumping Objects
  • Resolve Link Addresses
  • API Reference
    • Link Layer
    • Transport Layer
    • Application Layer
    • Master
    • Object Library
      • Primitive Types
      • Encoding / Decoding
      • Variations
      • Object Utilities

IEC 61850

  • Enumerating IEDs
  • Operating on IEDs
  • API Reference
    • Data Classes
    • Object / Data References
    • Control / Operation
    • IED Client
    • GOOSE Client
    • SampledValues Client

MMS / ISO 9506

  • Variable Target Format
  • VariableAccess Service (rwq)
  • FileManagement Service
  • API Reference
    • MMS Connection
    • Data Conversion
    • Protocol Utilities

IEC 60870-5-104

  • Reading Data via Interrogation
  • API Reference
    • APCI - Framing Layer
    • ASDU - Application Layer
    • Master (Client)
    • Information Object Library

CIP / EtherNet-IP

  • Operating via CIP / EtherNet-IP
  • API Reference
    • Connection
    • EtherNet/IP Encapsulation
    • Messaging
    • Object Library
      • Core Objects
      • Network Objects
      • I/O and Parameter Objects
      • Motion, Drive, and Semiconductor Objects
      • Switch Management Objects
      • Diagnostics Objects

Modbus

  • Modbus Interaction (rwq)
  • API Reference

OPC-UA

  • Basic Client
  • API Reference

BACnet

  • Discovering, Reading and Writing Properties
  • API Reference

ACSE / X.227

  • API Reference

COPP / X.226 / ISO 8823

  • API Reference
    • ISO Presentation
    • Protocol Utilities

COSP / X.225 / ISO 8237-1

  • API Reference
    • TSDU
    • SPDU Types
    • Parameter Values (PV)
    • ISO Session
    • Protocol Utilities

COTP / X.224

  • API Reference
    • TPDU Types
    • Connection

TPKT Protocol (RFC1006)

  • Using a TPKT Wrapper
  • API Reference

Development

  • Changelog

On this page

  • LPDU_HEADER_MIN_LENGTH
  • LPDU_HEADER_MAX_LENGTH
  • LPDU_USER_DATA_MAX_LENGTH
  • LinkDirection
    • LinkDirection.MASTER
    • LinkDirection.OUTSTATION
  • LinkPrimaryFunctionCode
    • LinkPrimaryFunctionCode.RESET_LINK_STATES
    • LinkPrimaryFunctionCode.TEST_LINK_STATES
    • LinkPrimaryFunctionCode.CONFIRMED_USER_DATA
    • LinkPrimaryFunctionCode.UNCONFIRMED_USER_DATA
    • LinkPrimaryFunctionCode.REQUEST_LINK_STATUS
  • LinkSecondaryFunctionCode
    • LinkSecondaryFunctionCode.ACK
    • LinkSecondaryFunctionCode.NACK
    • LinkSecondaryFunctionCode.LINK_STATUS
    • LinkSecondaryFunctionCode.NOT_SUPPORTED
  • LinkControl
    • LinkControl.direction
    • LinkControl.primary_message
    • LinkControl.frame_count_bit
    • LinkControl.frame_count_valid
    • LinkControl.function_code
    • LinkControl.data_flow_control
    • LinkControl.pri2sec_code
    • LinkControl.sec2pri_code
  • LPDU
    • LPDU.start
    • LPDU.length
    • LPDU.control
    • LPDU.destination
    • LPDU.source
    • LPDU.crc16
    • LPDU.user_data
    • LPDU.build()
    • LPDU.from_octets()
    • LPDU.apdu
    • LPDU.tpdu
MatrixEditor/icspacket 0 0
Edit this page
  1. icspacket /
  2. API Reference /
  3. Link Layer
View Source Open in ChatGPT Open in Claude

Link Layer#

icspacket.proto.dnp3.link.LPDU_HEADER_MIN_LENGTH = 5#

Minimum number of bytes required to represent an LPDU header.

icspacket.proto.dnp3.link.LPDU_HEADER_MAX_LENGTH = 255#

Maximum possible size of an LPDU header in bytes.

icspacket.proto.dnp3.link.LPDU_USER_DATA_MAX_LENGTH = 250#

Maximum user data size (250 bytes) that a single LPDU can contain.

class icspacket.proto.dnp3.link.LinkDirection(*values)[source]#

Values for the DNP3 link layer’s direction bit (DIR field), used by this library to tag which kind of station produced a frame.

(See DNP3 Specification, Section 9.2.4.1.3.1)

MASTER = 1#

Marks a frame as originating from a Master device.

OUTSTATION = 0#

Marks a frame as originating from an Outstation device.

class icspacket.proto.dnp3.link.LinkPrimaryFunctionCode(*values)[source]#

Function codes this library uses for frames traveling from a primary station to a secondary station.

These codes are valid when the PRM bit is set (PRM = 1). (See DNP3 Specification, Table 9-1)

RESET_LINK_STATES = 0#

Resets the secondary station’s link-layer state.

TEST_LINK_STATES = 2#

Tests whether the secondary station’s link layer is still responsive.

CONFIRMED_USER_DATA = 3#

Carries user data that the secondary station must acknowledge.

UNCONFIRMED_USER_DATA = 4#

Carries user data that does not require an acknowledgement.

REQUEST_LINK_STATUS = 9#

Asks the secondary station to report its current link status.

class icspacket.proto.dnp3.link.LinkSecondaryFunctionCode(*values)[source]#

Function codes this library uses for frames traveling from a secondary station back to a primary station.

These codes are valid when the PRM bit is clear (PRM = 0). (See DNP3 Specification, Table 9-2)

ACK = 0#

Confirms that the previous frame from the primary station was accepted.

NACK = 1#

Reports that the previous frame from the primary station was rejected.

LINK_STATUS = 11#

Carries the secondary station’s answer to a REQUEST_LINK_STATUS query.

NOT_SUPPORTED = 15#

Tells the primary station that the requested link function code is not implemented.

class icspacket.proto.dnp3.link.LinkControl(direction: LinkDirection = LinkDirection.MASTER, primary_message: bool = False, frame_count_bit: bool = False, frame_count_valid: bool = False, function_code: int = 0)[source]#

This bitfield models the LPDU’s control octet, exposing a frame’s direction, transaction role, retry-detection flags, and function code as individual attributes instead of raw bits. (See DNP3 Specification, Section 9.2.4.1.3)

direction: LinkDirection = 1#

DIR bit. Records which kind of station - Master or Outstation - originated the frame.

primary_message: bool = False#

PRM bit.

  • True This frame opens a new transaction.

  • False This frame completes an existing transaction.

frame_count_bit: bool = False#

FCB bit. Toggled on primary-to-secondary frames so the secondary station can detect lost or duplicated frames.

frame_count_valid: bool = False#

FCV bit. Tells the secondary station whether it should pay attention to the FCB carried in this frame.

function_code: int = 0#

Raw 4-bit function code; decode it with pri2sec_code or sec2pri_code depending on whether this is a primary or secondary frame.

property data_flow_control: bool#

Expose the DFC bit as a boolean.

A True value signals that the sending station’s Data Link Layer receive buffer has too little free space to accept more data.

Returns:

True if buffer space is insufficient, False otherwise.

Return type:

bool

property pri2sec_code: LinkPrimaryFunctionCode#

Interpret the function code for Primary-to-Secondary frames.

Returns:

Link layer function code for PRM = 1.

Return type:

LinkPrimaryFunctionCode

property sec2pri_code: LinkSecondaryFunctionCode#

Interpret the function code for Secondary-to-Primary frames.

Returns:

Link layer function code for PRM = 0.

Return type:

LinkSecondaryFunctionCode

class icspacket.proto.dnp3.link.LPDU(*, start: bytes = b'\x05d', length: int = 0, control: LinkControl = None, destination: int = 0, source: int = 0, crc16: int = 0, user_data: TPDU | None = None)[source]#

Link-layer Protocol Data Unit (LPDU).

This struct models a complete LPDU as a fixed-size header followed by zero or more variable-length data blocks, each one closed off with its own 16-bit CRC. (See DNP3 Specification, Section 9.2.4)

start: bytes = b'\x05d'#

Fixed two-byte sync sequence (0x05, 0x64) at the start of every LPDU.

length: int = 0#

Length field: count of the non-CRC bytes that follow it, spanning the CONTROL, DESTINATION, SOURCE, and USER DATA fields.

control: LinkControl = None#

Control octet for the frame, modeled by LinkControl, holding the direction, function code, and status flags.

destination: int = 0#

Address of the data-link frame’s destination station.

source: int = 0#

Address of the data-link frame’s source station.

crc16: int = 0#

Checksum protecting the LPDU header block.

user_data: TPDU | None = None#

The frame’s payload, held as one or more user-data chunks via LinkUserData.

build() → bytes[source]#

Construct a serialized LPDU with correct length.

Returns:

Encoded LPDU bytes.

Return type:

bytes

static from_octets(data: bytes) → LPDU[source]#

Parse an LPDU from a raw byte sequence.

Changed in version 0.2.0: Renamed from from_bytes to from_octets

Parameters:

data (bytes) – Encoded LPDU bytes.

Returns:

Decoded LPDU instance.

Return type:

LPDU

property apdu: APDU#

Parse the APDU contained in the TPDU.

property tpdu: TPDU#

The TPDU contained in the LPDU.

Previous
API Reference
Next
Transport Layer

2025, MatrixEditor

Made with Sphinx and Shibuya theme.