# `ExMqtt.Protocol`
[🔗](https://github.com/Ultraluv/ex_mqtt_uv/blob/v0.1.0/lib/ex_mqtt/protocol.ex#L1)

Unified Sans-I/O protocol entry point dispatching between MQTT 3.1.1 (`V311`)
and MQTT 5.0 (`V5`) codecs.

## Version Selection

Supported version identifiers (all normalized to `:"3.1.1"` or `:"5.0"`):
- MQTT 3.1.1: `:"3.1.1"`, `:v311`, `4`, `"3.1.1"`
- MQTT 5.0:   `:"5.0"`, `:v5`, `5`, `"5.0"`

# `version`

```elixir
@type version() :: :&quot;3.1.1&quot; | :&quot;5.0&quot;
```

# `version_input`

```elixir
@type version_input() :: version() | :v311 | :v5 | 4 | 5 | String.t()
```

# `codec_for`

```elixir
@spec codec_for(version()) :: module()
```

Returns the codec module for the given normalized protocol version.

# `decode`

```elixir
@spec decode(binary(), version_input()) ::
  {:ok, ExMqtt.Protocol.Packet.t(), binary()} | :incomplete | {:error, term()}
```

Decodes a single MQTT packet from the front of `buffer`.

Returns:
- `{:ok, packet, rest}` when a complete packet was decoded
- `:incomplete` when more bytes are needed from the transport
- `{:error, reason}` when the packet violates the selected protocol version

# `decode_all`

```elixir
@spec decode_all(binary(), version_input()) ::
  {:ok, [ExMqtt.Protocol.Packet.t()], binary()} | {:error, term()}
```

Decodes all complete MQTT packets from `buffer`, returning `{:ok, packets, remaining_buffer}`
or `{:error, reason}` if a malformed packet is encountered.

# `encode`

```elixir
@spec encode(ExMqtt.Protocol.Packet.t(), version_input()) ::
  {:ok, iodata()} | {:error, term()}
```

Encodes an `ExMqtt.Protocol.Packet` struct into iodata for `version`.

# `encode_binary`

```elixir
@spec encode_binary(ExMqtt.Protocol.Packet.t(), version_input()) ::
  {:ok, binary()} | {:error, term()}
```

Encodes a packet directly into a flat binary.

# `normalize_version`

```elixir
@spec normalize_version(version_input()) ::
  {:ok, version()} | {:error, {:unsupported_version, term()}}
```

Normalizes user-provided protocol version into `:"3.1.1"` or `:"5.0"`.

# `normalize_version!`

```elixir
@spec normalize_version!(version_input()) :: version()
```

Same as `normalize_version/1` but raises on unsupported version.

---

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