ISO Presentation

class icspacket.proto.iso_pres.presentation.ISO_PresentationContext[source]

Manages Presentation Context Items.

Each Presentation Context defines how user data is interpreted by binding an abstract syntax (ASN.1 type) to one or more transfer syntaxes.

This class is used to create, register, and remove contexts, and is passed to ISO_Presentation to negotiate which contexts are valid during association.

property items: dict[int, Member_TYPE]

Dictionary of Presentation Context Items keyed by their ID.

property asn1_types: dict[int, type]

Dictionary mapping context IDs to ASN.1 decoding classes.

add(item: Member_TYPE, asn1_cls: type) None[source]

Register an existing Presentation Context Item and bind its ASN.1 class.

new(name: str, ctx_id: int, asn1_cls: type, transfer_syntax: str | None = None) Member_TYPE[source]

Create and register a new Presentation Context Item.

Parameters:
  • name – Abstract syntax name (object identifier or string).

  • ctx_id – Unique Presentation Context Identifier.

  • asn1_cls – ASN.1 decoding class for user data.

  • transfer_syntax – Transfer syntax to bind. Defaults to Basic (2.1.1).

Returns:

The created context item.

Return type:

PresentaionContextItem

remove(item: Member_TYPE) None[source]

Remove a Presentation Context Item and its ASN.1 binding.

class icspacket.proto.iso_pres.presentation.ISO_PresentationSettings(calling_selector: bytes | None = b'\x00\x00\x00\x01', called_selector: bytes | None = b'\x00\x00\x00\x01', use_version1: bool = False, custom_requirements: Presentation_requirements | None = None)[source]

Configuration settings for the Presentation layer.

These settings influence how the Presentation connection (COPP) is established, specifically how selectors and protocol versions are negotiated.

Parameters:
  • calling_selector (bytes | None) – Local presentation selector, used to identify the calling application entity. If None, no selector is included.

  • called_selector (bytes | None) – Remote presentation selector, used to identify the destination application entity. If None, no selector is included.

  • use_version1 (bool) – If True, forces usage of COPP Version 1 semantics. If False, negotiates a higher version (default).

  • custom_requirements (Presentation_requirements | None) – Optional presentation requirements to override defaults. If None, a default requirements set is used.

class icspacket.proto.iso_pres.presentation.ISO_Presentation(session: ISO_Session, settings: ISO_PresentationSettings | None = None, context: ISO_PresentationContext | None = None)[source]

Implements the ISO Presentation protocol (X.226 / ISO 8823).

The Presentation layer sits above the Session layer and provides context negotiation and user data encapsulation. It uses PPDUs (Presentation Protocol Data Units) for connection establishment and termination.

>>> session = ISO_Session(...) # may require COTP_Connection
>>> presentation = ISO_Presentation(session)

To make sure your application layer user data is decoded and encoded correctly, register a new context id:

>>> presentation.presentation_context.new("1.2.3", 1, MyASN1Class)
<Context_list.Member_TYPE>
>>> presentation.init_session(("127.0.0.1", 1234))
<MyASN1Class> # depending on server result and ctx_id
Parameters:
  • session (ISO_Session) – Underlying ISO Session instance to use for transport.

  • settings (ISO_PresentationSettings | None) – Optional Presentation settings (selectors, version, requirements).

  • context (ISO_PresentationContext | None) – Presentation context registry, managing context IDs and ASN.1 decoding classes.

settings: ISO_PresentationSettings

Configuration settings for the Presentation layer.

property presentation_context: ISO_PresentationContext

Registered Presentation Contexts.

Provides both the raw context items and their ASN.1 decoding bindings.

Returns:

The managed Presentation Context registry.

Return type:

ISO_PresentationContext

property session: ISO_Session

Underlying Session object providing transport services.

property transport: COTP_Connection

Underlying COTP transport connection (OSI transport layer).

connect(address: tuple[str, int]) None[source]

Establish a Presentation connection.

If already connected, the call is ignored. Otherwise, it delegates connection establishment to the Session layer.

Parameters:

address (tuple[str, int]) – Network address tuple (host, port).

close() None[source]

Close the Presentation connection.

Delegates closure to the Session layer and marks the Presentation context as invalid.

init_session(app_octets: bytes, address: tuple[str, int] | None)[source]

Initialize a Presentation session (A-ASSOCIATE equivalent).

Builds and transmits a CP PPDU (Connect Presentation PDU) carrying application data, registered presentation contexts, and optional selectors. Waits for a CPA PPDU (Connect Presentation Accept) in response.

  • Includes all registered Presentation Context Items in negotiation.

  • If calling_selector or called_selector are set, they are included in the PPDU for AE identification.

  • If use_version1 is True, forces negotiation of COPP v1.

  • If custom_requirements is provided, overrides default presentation requirements.

Parameters:
  • app_octets (bytes) – Encoded application-layer data to include in the CP PPDU.

  • address (tuple[str, int] | None) – Optional address for connection establishment if the session is not already connected.

Raises:

ConnectionError – If session initiation fails, invalid CPA received, or unsupported mode is negotiated.

Returns:

Decoded user data if present in the CPA response, otherwise None.

Return type:

Any | None

close_session(octets: bytes, pres_ctx_id: int, graceful: bool = False) None[source]

Close the Presentation session.

Sends a CN/CPA termination sequence (Finish) via the Session layer, optionally embedding user data.

  • If pres_ctx_id is provided, the user data is bound to the given Presentation Context Identifier.

  • If graceful is True, waits for a Disconnect confirmation and returns raw response data.

  • If graceful is False, closes immediately.

Parameters:
  • octets (bytes) – Encoded application user data to include.

  • pres_ctx_id (int | None) – Optional presentation context ID for user data binding.

  • graceful (bool) – Whether to perform graceful closure with peer acknowledgment.

Raises:

ConnectionStateError – If session has not been initialized.

Returns:

Raw data from peer if graceful is True.

Return type:

bytes | None

send_data(octets: bytes, /) None[source]

Send raw user data.

Delegates to send_encoded_data().

Parameters:

octets (bytes) – Encoded user data.

send_encoded_data(octets: bytes, pres_ctx_id: int) None[source]

Send BER-encoded user data bound to a Presentation Context Identifier.

Parameters:
  • octets (bytes) – User data to encode.

  • pres_ctx_id (int | None) – Optional Presentation Context ID. If omitted, default context is used.

Raises:

ConnectionStateError – If not connected.

recv_data() bytes[source]

Receive raw user data from the session.

Returns:

Raw user data octets.

Return type:

bytes

recv_encoded_data(context: dict[int, type] | None = None) Any | None[source]

Receive and decode Presentation-encoded user data.

Attempts to decode User-data PPDU from the session. If decoding fails, raises a type error.

Parameters:

context (dict[int, type] | None) – Optional decoding context mapping PCI IDs to classes. If omitted, the instance’s default presentation_context.asn1_types is used.

Raises:
  • ConnectionClosedError – If no data is received (connection closed).

  • TypeError – If decoding fails due to invalid BER.

Returns:

Decoded ASN.1 object or None.

Return type:

Any | None