# `ExIEC61850`
[🔗](https://github.com/Ultraluv/ex_iec61850/blob/v0.1.0/lib/ex_iec61850.ex#L1)

Pure Elixir implementation of the IEC 61850 protocol stack.

This top-level module provides ergonomic entrypoints for the most common client,
server, control, and model-parsing workflows while delegating to the full
layered protocol stack under `ExIEC61850.*`.

## Quick Start

    # Start an IED server from an SCL (.icd/.cid/.scd) or .cfg file, or an %IedModel{}
    {:ok, server} = ExIEC61850.start_server("tools/model_generator/sample_model.icd", port: 10102)

    # Connect a client (with optional :tls, :password, :name, :closed_handler)
    {:ok, conn} = ExIEC61850.connect("127.0.0.1", 10102)
    {:ok, lds} = ExIEC61850.logical_devices(conn)
    {:ok, val} = ExIEC61850.read(conn, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", :mx)
    :ok = ExIEC61850.disconnect(conn)
    :ok = ExIEC61850.stop_server(server)

# `connect`

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

Creates and connects an IEC 61850 client to `host:port`.

## Options
- `:tls` - Erlang `:ssl` client options list (enables TLS)
- `:password` - ACSE password string for authentication
- `:name` - Optional `GenServer` process registration name
- `:closed_handler` - `fn reason -> ... end` invoked on passive disconnect

# `disconnect`

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

Closes an active client connection.

# `logical_devices`

```elixir
@spec logical_devices(GenServer.server()) :: {:ok, [String.t()]} | {:error, term()}
```

Lists Logical Devices exposed by the connected IED server.

# `operate`

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

Executes a direct or SBOw `Operate` command on a controllable DataObject (e.g. `LD/XCBR1.Pos`).

# `parse_cfg`

```elixir
@spec parse_cfg(Path.t()) ::
  {:ok, ExIEC61850.IEC61850.Server.Model.IedModel.t()} | {:error, term()}
```

Parses a `.cfg` model file into an `%IedModel{}`.

# `parse_scl`

```elixir
@spec parse_scl(Path.t()) ::
  {:ok, ExIEC61850.IEC61850.Server.Model.IedModel.t()} | {:error, term()}
```

Parses an SCL (`.icd`/`.cid`/`.scd`) file into an `%IedModel{}`.

# `read`

```elixir
@spec read(GenServer.server(), String.t(), atom()) ::
  {:ok, ExIEC61850.Mms.IsoMms.Common.MmsValue.t()} | {:error, term()}
```

Reads an IEC 61850 object/attribute reference with the given Functional Constraint.

# `server_config`

```elixir
@spec server_config(keyword()) :: ExIEC61850.IEC61850.Server.Impl.IedServerConfig.t()
```

Creates an `%IedServerConfig{}` struct.

# `start_reconnect_client`

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

Starts a supervisor-friendly auto-reconnecting client worker (`ReconnectWorker`).

# `start_server`

```elixir
@spec start_server(
  ExIEC61850.IEC61850.Server.Model.IedModel.t() | Path.t(),
  keyword()
) ::
  {:ok, pid()} | {:error, term()}
```

Starts an IEC 61850 server from an `%IedModel{}` or an SCL (`.icd`/`.cid`/`.scd`) / `.cfg` file path.

## Options
- `:port` - TCP/TLS listening port (default `102`)
- `:config` - Optional `%IedServerConfig{}` struct
- `:tls` / `:tls_options` - Erlang `:ssl` server options list
- `:authenticator` - `fn password -> boolean() end` or `{mod, fun, args}`
- `:file_root` - Root directory for MMS file service
- `:name` - Optional `GenServer` process registration name

# `stop_server`

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

Stops a running IEC 61850 server.

# `update_value`

```elixir
@spec update_value(GenServer.server(), String.t(), term()) :: :ok
```

Updates a server attribute value using a native Elixir value or `%MmsValue{}`.

# `version`

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

Returns the current `ExIEC61850` package version string.

# `write`

```elixir
@spec write(GenServer.server(), String.t(), atom(), term()) :: :ok | {:error, term()}
```

Writes a native Elixir value or `%MmsValue{}` to an IEC 61850 attribute reference.

---

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