# `ExCEP.Gateway.Client`

Single-channel Edge Gateway TCP/TLS client (`:management` on 9901 or `:business` on 9902).

## Lifecycle
1. Opens TCP/TLS connection to the Cluster server.
2. If `require_auth: true`, executes the Section 5 three-way SM2 authentication handshake:
   - Sends `AuthAsdu.Request` (`0x01`) with random `r1`
   - Receives & verifies `AuthAsdu.Response` (`0x02`) checking `r1` and SM2 signature
   - Sends `AuthAsdu.Confirm` (`0x03`) echoing `r2`
3. Enters active framing mode (`{:active, :once}`), sending `Health` heartbeat whenever
   idle for `heartbeat_interval_ms` (default 60s).
4. Correlates outgoing requests by `Head/Mid` and dispatches incoming Cluster requests to
   the configured `ExCEP.Gateway.Handler` (default `ExCEP.Gateway.DefaultHandler`).

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `close`

```elixir
@spec close(GenServer.server()) :: :ok
```

Closes the connection.

# `connect`

```elixir
@spec connect(GenServer.server()) :: :ok | {:error, term()}
```

Connects and authenticates to the Cluster server.

# `connected?`

```elixir
@spec connected?(GenServer.server()) :: boolean()
```

Returns `true` if connected (and authenticated when `require_auth` is enabled).

# `pull_file`

```elixir
@spec pull_file(GenServer.server(), String.t(), keyword()) ::
  {:ok, binary()} | {:error, term()}
```

Pulls a binary file from the Cluster using Section 6.2.1 `FileGet` (e.g. firmware/app package).

# `push_file`

Pushes a binary file to the Cluster using Section 6.2.2 chunked transfer.

# `report_app_alarm`

Reports an app alarm (`AppAlarm`) on the management channel.

# `report_app_install_result`

Reports `AppInstallResult` (Section 7.6.2) on the management channel.

# `report_con_alarm`

Reports a container alarm (`ConAlarm`) on the management channel.

# `report_data_spont`

Reports `DataSpont` (spontaneous or periodic) and awaits `DataSpontResp`.

# `report_dev_add`

Reports `DevAdd` and awaits `DevAddResp`.

# `report_dev_del`

Reports `DevDel` and awaits `DevDelResp`.

# `report_dev_update`

Reports `DevUpdate` and awaits `DevUpdateResp`.

# `report_device_update_result`

Reports `DeviceUpdateResult` (Section 7.4.2) on the management channel.

# `report_model_change`

Reports `ModelChange` and awaits `ModelChangeResp`.

# `report_sys_alarm`

Reports a system alarm (`SysAlarm`) on the management channel.

# `request`

```elixir
@spec request(GenServer.server(), ExCEP.Codec.DataAsdu.t(), keyword()) ::
  {:ok, ExCEP.Codec.DataAsdu.t()} | {:error, term()}
```

Sends a `%DataAsdu{}` and awaits the correlated response matching `Head/Mid`.

# `send_asdu`

```elixir
@spec send_asdu(GenServer.server(), ExCEP.Codec.DataAsdu.t(), keyword()) ::
  :ok | {:error, term()}
```

Sends a `%DataAsdu{}` without waiting for a response (one-way upload / alarm).

# `send_heartbeat`

```elixir
@spec send_heartbeat(GenServer.server()) :: :ok | {:error, term()}
```

Sends a `Health` heartbeat immediately.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

---

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