# dnp3/opendnp3 architecture

> Architecture diagram of the GitHub repository dnp3/opendnp3, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dnp3/opendnp3
- Repository: https://github.com/dnp3/opendnp3

## Overview

##### Main Components
- Core DNP3 Library (C++ modules):
– The “cpp/lib” folder contains the primary source for the protocol stack implementation.
– This module includes many submodules such as:
▪ Channel layer (handling physical/serial/TCP/UDP communication, TLS support under “cpp/lib/src/channel” and “cpp/lib/src/channel/tls”)
▪ Transport layer (“cpp/lib/src/transport”)
▪ Application layer with classes for APDU building/parsing and measurement/event management (“cpp/lib/src/app”)
▪ Protocol-specific parts like link layer and master/outstation modules (“cpp/lib/src/master”, “cpp/lib/src/outstation”, “cpp/lib/src/link”)
- Examples:
– The “cpp/examples” directory contains demo applications for masters, outstations, UDP, TLS etc.
- Testing Infrastructure:
– “cpp/tests” is organized into multiple groups (unit tests, integration tests, ASIO tests, fuzz tests) indicating both component verification and protocol stress testing.
- Code Generation Tools:
– The “generation” folder (with subdirectories in Scala and pom.xml files) is used to automatically generate portions of the DNP3 protocol objects and enums.
- Language Bindings:
– The “dotnet” directory provides the CLR adapter and interface, along with example projects.
– The “java” directory contains the Java bindings (source, tests, JNI wrappers, code generation in Scala, and even adapter code in “java/cpp” for JNI support).

##### Relationships & Interactions
- The core C++ library forms the heart of the system. It is structured in a layered approach, where the physical or serial channels interact with the channel layer which, in turn, feeds the transport layer. This passes data to the application layer and then further bifurcates into master or outstation logic.
- The examples use the core library via APIs provided by different submodules (master, outstation), demonstrating typical use cases like handling multiple TCP sessions or simulating huge numbers of devices.
- The language bindings (for dotnet and java) wrap the core C++ library so that external clients can interact with it using managed languages. They essentially invoke or translate calls to the underlying protocol stack.
- The code generation components support the development process by generating enums, objects, and even source code structures used by both the core and the bindings.
- Testing components interact with all layers of the stack by simulating or emulating communication to validate protocol correctness and performance.

##### Architectural Patterns & Design Principles
- Layered Architecture – The stack is clearly divided into physical/channel, transport, and application layers.
- Separation of Concerns – C++ modules are factored into distinct subdirectories dedicated to roles like “master” and “outstation,” and even further into “link,” “channel,” “transport” etc.
- Extensibility – The presence of language bindings and code generation indicates that the design supports extensibility to other environments and customization via adapters.
- Testability – The file structure shows comprehensive tests covering unit, integration, and fuzz testing to verify each component’s robustness.

##### Final Elements
- Clear labels for each component (e.g., “C++ Core – Channel Layer”, “Transport Layer”, “Application Layer – Master/Outstation”, “.NET Binder”, “Java Binder”, “Code Generator”).
– For example, data flows from physical connectors to channel layer modules, then to a transport unit, then onward to master/outstation application logic.
- Additional notes for areas that might be more critical (like performance considerations in the channel and transport layers).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Physical/Network Layer
    PN["Physical/Network"]:::external

    %% C++ Core Library Subgraph
    subgraph "C++ Core Library"
        CL["Channel Layer"]:::core
        TS["TLS Channel"]:::core
        LL["Link Layer"]:::core
        TL["Transport Layer"]:::core
        AL["Application Layer"]:::core
        MM["Master Module"]:::core
        OM["Outstation Module"]:::core
    end

    %% Side Modules Subgraph
    subgraph "Side Modules"
        EX["Examples"]:::tools
        TE["Testing"]:::tools
        CG["Code Generation Tools"]:::tools
        subgraph "Language Bindings"
            DN[".NET Bindings"]:::binding
            JV["Java Bindings"]:::binding
        end
    end

    %% Data Flow Arrows - Core Chain
    PN -->|"sends"| CL
    CL -->|"TLS_support"| TS
    CL -->|"feeds"| TL
    TL -->|"processes"| AL
    AL -->|"invokes"| MM
    AL -->|"invokes"| OM

    %% Additional Core Interactions via Link Layer
    CL -->|"protocol"| LL
    LL -->|"routes"| MM
    LL -->|"routes"| OM

    %% Side Modules interacting with Core
    EX -->|"uses"| AL
    TE -->|"tests"| AL
    CG -->|"generates"| AL
    DN -->|"bridges"| AL
    JV -->|"bridges"| AL

    %% Click Events for Core Components
    click CL "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/channel"
    click TS "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/channel/tls"
    click TL "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/transport"
    click AL "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/app"
    click MM "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/master"
    click OM "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/outstation"
    click LL "https://github.com/dnp3/opendnp3/tree/release/cpp/lib/src/link"

    %% Click Events for Side Modules
    click EX "https://github.com/dnp3/opendnp3/tree/release/cpp/examples"
    click TE "https://github.com/dnp3/opendnp3/tree/release/cpp/tests"
    click CG "https://github.com/dnp3/opendnp3/tree/release/generation"
    click DN "https://github.com/dnp3/opendnp3/tree/release/dotnet"
    click JV "https://github.com/dnp3/opendnp3/tree/release/java"

    %% Styles
    classDef external fill:#E2E3E5,stroke:#333,stroke-width:2px
    classDef core fill:#CCE5FF,stroke:#333,stroke-width:2px
    classDef tools fill:#F8D7DA,stroke:#333,stroke-width:2px
    classDef binding fill:#D4EDDA,stroke:#333,stroke-width:2px
```
