ExCEP.Codec.Frame (ex_cep v0.1.1)

Copy Markdown

APDU framing codec for CSG CEP TS2.2 Section 4.6–4.8.

Frame Layout

+-----------------------------------+------------------+------------------------+
| Fixed 32-byte Header              | ASDU (0..65535B) | Optional SM2 Sig (64B) |
+-----------------------------------+------------------+------------------------+

32-Byte Header (Table 1)

  • Bytes 0..3: Sync bytes <<0xEB, 0x90, 0xEB, 0x90>>
  • Bytes 4..5: ASDU wire length (uint16, big-endian, max 65535)
  • Byte 6: Control byte (Figure 5):
    • bit0 (0x01): Signature flag (1 = 64-byte SM2 signature appended)
    • bit1 (0x02): Auth flag (1 = binary authentication ASDU)
    • bit2 (0x04): Compression flag (1 = deflate/zlib compressed ASDU)
    • bit3..bit7: Reserved (0)
  • Byte 7: Reserved (0x00)
  • Bytes 8..31: GatewayId (24 bytes, 1..23 byte UTF-8 string, NUL-terminated & NUL-padded)

Summary

Functions

Decodes a single complete APDU frame binary.

Decodes the next complete frame from buffer.

Extracts all complete APDU frames from a TCP byte stream buffer.

Encodes a %Frame{} into wire binary.

Reconstructs the exact raw wire bytes of a decoded %Frame{} (header + wire ASDU + optional 64B signature).

Returns the 4-byte sync header <<0xEB, 0x90, 0xEB, 0x90>>.

Types

t()

@type t() :: %ExCEP.Codec.Frame{
  asdu: binary(),
  auth?: boolean(),
  compressed?: boolean(),
  gateway_id: String.t(),
  raw_signed_data: binary() | nil,
  reserved: non_neg_integer(),
  signature: binary() | nil,
  signed?: boolean()
}

Functions

decode(binary, opts \\ [])

@spec decode(binary(), keyword()) :: {:ok, t()} | {:error, term()}

Decodes a single complete APDU frame binary.

Options

  • :signer - {module, opts} for SM2 signature verification when signed?: true
  • :verify_signature - boolean (default true when :signer is provided, false otherwise)

decode_ctrl(ctrl)

@spec decode_ctrl(non_neg_integer()) :: {boolean(), boolean(), boolean()}

decode_next(buffer, opts \\ [])

@spec decode_next(binary(), keyword()) ::
  {:ok, t(), binary()} | {:more, binary()} | {:error, term()}

Decodes the next complete frame from buffer.

Returns {:ok, frame, rest}, {:more, remaining_buffer}, or {:error, reason}.

decode_stream(buffer, opts \\ [])

@spec decode_stream(binary(), keyword()) ::
  {:ok, [t()], binary()} | {:error, term(), binary()}

Extracts all complete APDU frames from a TCP byte stream buffer.

Returns {:ok, frames, rest_buffer} or {:error, reason, rest_buffer}. Automatically skips leading garbage bytes until the 0xEB 0x90 0xEB 0x90 sync header.

encode(frame, opts \\ [])

@spec encode(t(), keyword()) :: {:ok, binary()} | {:error, term()}

Encodes a %Frame{} into wire binary.

Options

  • :signer - {module, opts} implementing ExCEP.Crypto.Signer (required when signed?: true and frame.signature == nil)
  • :deflate_mode - :zlib (default, RFC 1950) or :raw (RFC 1951 raw deflate)
  • :cipher - optional {module, opts} implementing ExCEP.Transport.Cipher (e.g. SM4)

encode_ctrl(compressed?, auth?, signed?)

@spec encode_ctrl(boolean(), boolean(), boolean()) :: non_neg_integer()

raw_wire(frame)

@spec raw_wire(t()) :: binary() | nil

Reconstructs the exact raw wire bytes of a decoded %Frame{} (header + wire ASDU + optional 64B signature).

sync_bytes()

@spec sync_bytes() :: <<_::32>>

Returns the 4-byte sync header <<0xEB, 0x90, 0xEB, 0x90>>.