# ExIEC61850

Pure Elixir implementation of the **IEC 61850** protocol stack (MMS, GOOSE, Sampled Values, R-GOOSE/R-SV, SNTP, SCL/CFG Parser, Data Model, 36 CDCs) with built-in `:telemetry ~> 1.4` instrumentation.

## Installation

Add `ex_iec61850` and `telemetry` to your `mix.exs` dependencies:

```elixir
def deps do
  [
    {:ex_iec61850, "~> 0.1.0"}
  ]
end
```

## Quick Start

```elixir
# Start a server directly from an SCL (.icd/.cid/.scd) or .cfg file
{:ok, server} = ExIEC61850.start_server("tools/model_generator/sample_model.icd", port: 10102, name: :my_ied)
:ok = ExIEC61850.update_value(:my_ied, "simpleIOGenericIO/GGIO1.SPCSO1.stVal", true)

# Connect a client (supports :tls, :password, :name, :closed_handler)
{:ok, conn} = ExIEC61850.connect("127.0.0.1", 10102, name: :my_client)
{:ok, lds} = ExIEC61850.logical_devices(:my_client)
{:ok, val} = ExIEC61850.read(:my_client, "simpleIOGenericIO/GGIO1.SPCSO1.stVal", :st)

:ok = ExIEC61850.disconnect(conn)
:ok = ExIEC61850.stop_server(server)
```

## Architecture & Modules

| Layer / Feature | Directory / Modules |
|---|---|
| Stack Configuration | `config/config.exs`, `ExIEC61850.Config` |
| Binary Conversions | `ExIEC61850.Common.Conversions` |
| Ethernet L2 HAL | `ExIEC61850.Hal.Ethernet` (Linux `AF_PACKET` via `:socket`) |
| ASN.1 BER | `ExIEC61850.Mms.Asn1.{BerInteger, BerEncoder, BerDecode}` |
| ISO Transport (TPKT/COTP) | `ExIEC61850.Mms.IsoCotp.{Tpkt, Cotp}` |
| ISO Session / Presentation / ACSE | `ExIEC61850.Mms.{IsoSession, IsoPresentation, IsoAcse.Acse}` |
| ISO Client / Server | `ExIEC61850.Mms.{IsoCommon.IsoConnectionParameters, IsoClient.IsoClientConnection, IsoServer.{IsoServer, IsoConnection}}` |
| MMS Common | `ExIEC61850.Mms.IsoMms.Common.{MmsValue, MmsTypeSpec, MmsCommonMsg}` |
| MMS Client Services | `ExIEC61850.Mms.IsoMms.Client.*` (11 service modules) |
| MMS Server Services | `ExIEC61850.Mms.IsoMms.Server.*` (12 service modules) |
| IEC 61850 Common Types | `ExIEC61850.IEC61850.Common.*` |
| IEC 61850 Client | `ExIEC61850.IEC61850.Client.{IedConnection, ClientControl, ClientReport, ClientReportControl, ClientGooseControl, ClientSvControl, ClientDataSet}` |
| Data Model & 36 CDCs | `ExIEC61850.IEC61850.Server.Model.{Model, DynamicModel, Cdc, ConfigFileParser, SclParser}` |
| MMS Mapping / Control / Reporting | `ExIEC61850.IEC61850.Server.MmsMapping.{MmsMapping, Control, Reporting, MmsGoose, MmsSv}` |
| IED Server | `ExIEC61850.IEC61850.Server.Impl.{IedServer, IedServerConfig}` |
| GOOSE | `ExIEC61850.Goose.{GoosePublisher, GooseSubscriber, GooseReceiver}` |
| Sampled Values (9-2) | `ExIEC61850.SampledValues.{SvPublisher, SvSubscriber}` |
| R-Session (R-GOOSE / R-SV) | `ExIEC61850.RSession.{RSession, RSessionCrypto}` (AES-128/256-GCM + HMAC-SHA256) |
| Journal / Log Storage | `ExIEC61850.Logging.{LogStorage, Drivers.MemoryLogStorage}` |
| Time Synchronization | `ExIEC61850.Sntp.SntpClient` |
| Model & Config Generators | `tools/model_generator/sample_model.icd`, `mix iec61850.gen.{model,config}` |
| Examples, Demos & Fuzzing | `examples/<name>/<file>.exs` (48 examples), `demos/substation_demo.exs`, `fuzz/fuzz_decoders.exs` |

## Telemetry (`{:telemetry, "~> 1.4"}`)

```elixir
ExIEC61850.Telemetry.attach_default_logger(level: :info)
```

## Running Tests, Fuzzing, Demos & Examples

```bash
mix deps.get
mix compile --warnings-as-errors
mix test
mix run fuzz/fuzz_decoders.exs 500
mix run demos/substation_demo.exs

# Client/Server example:
mix run examples/server_example_basic_io/server_example_basic_io.exs 10102
mix run examples/iec61850_client_example2/client_example2.exs 127.0.0.1 10102
```
