# `ExCEP`

Pure Elixir implementation of the **China Southern Power Grid (CSG) CEP**
Edge Gateway Plug-and-Play Protocol (TS2.2).

This top-level module provides unified entrypoints for starting Cluster (Master)
servers, connecting Edge Gateways (single-channel, dual-channel, or auto-reconnecting),
and executing **every** Management (TCP 9901), Business (TCP 9902), File Transfer,
Proactive Reporting, TS2.1 Model/Catalog, and SM2 Certificate API.

## Quick Start

    # Generate SM2 keypairs for Cluster and Gateway
    {cluster_pub, cluster_priv} = ExCEP.Crypto.SM2.generate_keypair()
    {gw_pub, gw_priv} = ExCEP.Crypto.SM2.generate_keypair()

    # Start Cluster Business Server
    {:ok, server} =
      ExCEP.start_server(
        channel: :business,
        port: 9902,
        signer: {ExCEP.Crypto.SM2, private_key: cluster_priv, peer_public_key: gw_pub}
      )

    # Connect Edge Gateway Client
    {:ok, gw} =
      ExCEP.connect("127.0.0.1", 9902,
        channel: :business,
        gateway_id: "GW-001",
        signer: {ExCEP.Crypto.SM2, private_key: gw_priv, peer_public_key: cluster_pub}
      )

    {:ok, resp} = ExCEP.get_model(server, "GW-001")
    :ok = ExCEP.disconnect(gw)
    :ok = ExCEP.stop_server(server)

# `activate_parameters`

# `app_disable`

# `app_enable`

# `app_get_config`

# `app_get_log`

# `app_install`

# `app_install_status`

# `app_remove`

# `app_set_config`

# `app_start`

# `app_status`

# `app_stop`

# `build_sm2_signer`

Builds an `{ExCEP.Crypto.SM2, opts}` signer tuple from PEM/DER/file certificates and keys.

# `build_standard_model`

Builds a TS2.1 standard `%ExCEP.Model{}` from the built-in 5-domain/26-device catalog.

# `cancel_parameters`

# `cluster_request`

Sends a `%DataAsdu{}` request from Cluster `server` to `gateway_id` and awaits response.

# `cluster_send_asdu`

Sends a one-way `%DataAsdu{}` from Cluster `server` to `gateway_id`.

# `connect`

```elixir
@spec connect(String.t() | :inet.ip_address(), :inet.port_number(), keyword()) ::
  {:ok, pid()} | {:error, term()}
```

Creates and connects a single-channel Edge Gateway client to `host:port`.

# `connect_dual`

```elixir
@spec connect_dual(keyword()) :: {:ok, pid()} | {:error, term()}
```

Starts and connects a DualChannel Gateway client (both 9901 Management + 9902 Business).

# `connected?`

Returns `true` if Gateway `client` is connected and authenticated.

# `container_get_config`

# `container_get_log`

# `container_install`

# `container_remove`

# `container_restart`

# `container_set_config`

# `container_start`

# `container_status`

# `container_stop`

# `device_config`

# `device_control`

# `device_get_log`

# `device_info`

# `device_status`

# `device_update`

# `device_update_status`

# `direct_control`

# `disconnect`

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

Closes a connected Gateway client.

# `disconnect_dual`

Closes both Management and Business channels of a DualChannel Gateway client.

# `dual_business`

Returns the Business channel client pid from a DualChannel Gateway client.

# `dual_channels`

Returns `{mgmt_client_pid, biz_client_pid}` for a DualChannel Gateway client.

# `dual_management`

Returns the Management channel client pid from a DualChannel Gateway client.

# `gateway_pull_file`

Pulls a binary file from the Cluster (`FileGet` initiated by Gateway).

# `gateway_push_file`

Pushes a binary file from the Gateway to the Cluster (`FileSend` initiated by Gateway).

# `gateway_request`

Sends a `%DataAsdu{}` request from Gateway `client` to Cluster and awaits response.

# `gateway_send_asdu`

Sends a one-way `%DataAsdu{}` from Gateway `client` to Cluster.

# `get_history`

# `get_message_files`

# `get_model`

# `get_session`

Looks up the active `ExCEP.Cluster.Session` pid for `gateway_id`.

# `get_subscriptions`

# `get_topo_files`

# `list_domains`

Returns the 5 standard TS2.1 domains.

# `list_gateways`

```elixir
@spec list_gateways(GenServer.server()) :: [String.t()]
```

Lists connected and authenticated `gateway_id`s on `server`.

# `list_logical_nodes`

Returns all 168 TS2.1 logical node names.

# `list_standard_devices`

Lists all 26 standard TS2.1 device model definitions (optionally filtered by domain).

# `lookup_logical_node`

Looks up a TS2.1 standard Logical Node (e.g. `"SlrBrkMMXU"`) and its Appendix A DOs.

# `parse_cert`

Parses an SM2 X.509 certificate from a PEM or DER binary.

# `parse_cert_file`

Loads and parses an SM2 X.509 certificate file (`.pem`, `.crt`, `.cer`, `.der`).

# `parse_model`

```elixir
@spec parse_model(Path.t(), keyword()) :: {:ok, ExCEP.Model.t()} | {:error, term()}
```

Parses a CSG CEP TS2.1 device model file (`.json` or `.icd`) from disk into `%ExCEP.Model{}`.

# `parse_model_icd`

Parses a CSG CEP TS2.1 DL/T 860 `.icd` XML binary into `%ExCEP.Model{}`.

# `parse_model_json`

Parses a CSG CEP TS2.1 simplified JSON model binary into `%ExCEP.Model{}`.

# `preset_parameters`

# `pull_file`

Pulls a file from a connected Gateway using Section 6.2.1 (`FileGet` + `FileDataTrans`).

# `push_file`

Pushes a binary file to a connected Gateway using Section 6.2.2 (`FileSend` + `FileDataTrans`).

# `read_data`

# `read_parameters`

# `read_time`

# `report_app_alarm`

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

# `report_app_install_result`

Reports asynchronous application install completion (`AppInstallResult`).

# `report_con_alarm`

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

# `report_data_spont`

Reports spontaneous/periodic measurements (`DataSpont`) to the Cluster.

# `report_dev_add`

Reports sub-device additions (`DevAdd`) to the Cluster.

# `report_dev_del`

Reports sub-device deletions (`DevDel`) to the Cluster.

# `report_dev_update`

Reports sub-device updates (`DevUpdate`) to the Cluster.

# `report_device_update_result`

Reports asynchronous firmware/device upgrade completion (`DeviceUpdateResult`).

# `report_model_change`

Reports device model changes (`ModelChange`) to the Cluster.

# `report_sys_alarm`

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

# `sbo_control`

# `search_dos`

Searches all 1,395 TS2.1 Data Objects by keyword.

# `send_control`

# `send_heartbeat`

Sends an immediate `Health` heartbeat from Gateway `client`.

# `server_port`

Returns the TCP listen port bound by `server`.

# `start_reconnect_client`

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

Starts a supervisor-friendly auto-reconnecting Gateway client worker.

# `start_server`

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

Starts an Edge Cluster server (`:management` on 9901 or `:business` on 9902).

# `stop_server`

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

Stops a running Cluster server.

# `subscribe`

# `sync_time`

# `update_parameters`

# `verify_model_consistency`

Verifies TS2.1 Section 9.3 consistency between an ICD model and a simplified JSON model.

# `version`

```elixir
@spec version() :: String.t()
```

Returns the current `ExCEP` package version string.

---

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