# `HL7v2.MLLP`
[🔗](https://github.com/Balneario-de-Cofrentes/hl7v2/blob/v3.10.1/lib/hl7v2/mllp.ex#L1)

MLLP (Minimal Lower Layer Protocol) framing for HL7v2 messages.

MLLP wraps each HL7v2 message in a simple frame:

    <SB>message<EB><CR>

Where:
- SB (Start Block) = `0x0B`
- EB (End Block)   = `0x1C`
- CR (Carriage Return) = `0x0D`

See HL7 Implementation Technology Specification — Minimal Lower Layer Protocol.

# `extract_messages`

```elixir
@spec extract_messages(binary()) :: {[binary()], binary()}
```

Extracts complete MLLP messages from a buffer.

Returns `{messages, remaining_buffer}` where `messages` is a list of
extracted message binaries (without MLLP framing) and `remaining_buffer`
contains any incomplete data that needs more bytes.

## Examples

    iex> HL7v2.MLLP.extract_messages(<<0x0B, "MSG1", 0x1C, 0x0D, 0x0B, "MSG2", 0x1C, 0x0D>>)
    {["MSG1", "MSG2"], <<>>}

    iex> HL7v2.MLLP.extract_messages(<<0x0B, "MSG1", 0x1C, 0x0D, 0x0B, "partial">>)
    {["MSG1"], <<0x0B, "partial">>}

# `frame`

```elixir
@spec frame(binary()) :: binary()
```

Wraps a message binary in an MLLP frame.

## Examples

    iex> HL7v2.MLLP.frame("MSH|...")
    <<0x0B, "MSH|...", 0x1C, 0x0D>>

# `unframe`

```elixir
@spec unframe(binary()) :: {:ok, binary()} | {:error, :invalid_frame}
```

Extracts the message from an MLLP frame.

Returns `{:ok, message}` when the binary starts with SB and ends with EB+CR.
Returns `{:error, :invalid_frame}` otherwise.

## Examples

    iex> HL7v2.MLLP.unframe(<<0x0B, "MSH|...", 0x1C, 0x0D>>)
    {:ok, "MSH|..."}

    iex> HL7v2.MLLP.unframe("bad")
    {:error, :invalid_frame}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
