# rtlabs-com/p-net architecture

> Architecture diagram of the GitHub repository rtlabs-com/p-net, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rtlabs-com/p-net
- Repository: https://github.com/rtlabs-com/p-net

## Overview

#### Purpose and build boundary
P-Net is a C PROFINET Device protocol stack for resource-constrained embedded targets and Linux/RTOS deployments. This evaluation source is intentionally incomplete: it requires target-specific port sources before it can build. Public integration contracts are in `include/pnet_api.h`, `include/pnal.h`, and `src/pf_driver.h`.

#### Application-facing API and stack bootstrap
The application configures and drives a device through the public P-Net API, implemented by `src/device/pnet_api.c`. API calls enter device configuration, identification, IO, diagnosis, and alarm subsystems rather than exposing protocol internals directly. Platform abstraction (`include/pnal.h`) separates OS/network/clock primitives from stack logic.

#### Device configuration and connection state machines
The `src/device` layer implements PROFINET device behavior and its coordinated state machines:
- Device lifecycle and controller connection handling: `src/device/pf_cmdev.c`, `src/device/pf_cmsm.c`, `src/device/pf_cmrpc.c`.
- Configuration/parameter services and startup: `src/device/pf_cmina.c`, `src/device/pf_cmpbe.c`, `src/device/pf_fspm.c`.
- IO and record access handling: `src/device/pf_cmio.c`, `src/device/pf_cmrdr.c`, `src/device/pf_cmwrr.c`.
This is the boundary between protocol requests and application-visible device model/state.

#### RPC, discovery, and management protocols
Connection establishment and management traffic are handled through CM-RPC and endpoint mapping in `src/device/pf_cmrpc.c` and `src/device/pf_cmrpc_epm.c`. Common Ethernet-facing protocols provide discovery/configuration, topology advertisement, and management: `src/common/pf_dcp.c`, `src/common/pf_lldp.c`, and `src/common/pf_snmp.c`. Protocol block serialization is centralized in `src/device/pf_block_reader.c` and `src/device/pf_block_writer.c`.

#### Real-time cyclic IO and timing
Cyclic PROFINET traffic is split into consumer and provider paths: `src/common/pf_cpm.c` receives controller output data, while `src/common/pf_ppm.c` transmits device input data. Their software fallback drivers are `src/common/pf_cpm_driver_sw.c` and `src/common/pf_ppm_driver_sw.c`. Scheduling and synchronization are supplied by `src/common/pf_scheduler.c` and `src/common/pf_ptcp.c`; timing and deterministic packet handling are key platform/driver boundaries.

#### Network, alarms, diagnostics, and ports
Raw Ethernet and UDP transport adapters live in `src/common/pf_eth.c` and `src/common/pf_udp.c`. Alarm delivery and diagnostic state are maintained in `src/common/pf_alarm.c` and `src/device/pf_diag.c`. Physical-port and port-data behavior is modeled by `src/device/pf_port.c`, `src/device/pf_pdport.c`, and `src/device/pf_plugsm.c`, connecting network-interface events to PROFINET state.

#### Hardware/runtime integrations
The portable core expects OS/network/file implementations through PNAL and driver hooks. A specialized LAN9662 integration replaces software cyclic IO with hardware-oriented drivers in `src/drivers/lan9662/src/pf_cpm_driver_lan9662.c` and `src/drivers/lan9662/src/pf_ppm_driver_lan9662.c`, using MESA and UIO-backed resources (`src/drivers/lan9662/src/pf_rte_uio.c`). Supported deployment intent includes Linux and RTOSs such as rt-kernel and FreeRTOS; the missing port is the required infrastructure layer.

Main entry points: [`src/device/pnet_api.c`](https://github.com/rtlabs-com/p-net/blob/public/src/device/pnet_api.c) (API implementation), [`src/common/pf_cpm.c`](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_cpm.c) (Cyclic IO consumer), [`src/common/pf_ppm.c`](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_ppm.c) (Cyclic IO provider), [`src/drivers/lan9662/src/pf_cpm_driver_lan9662.c`](https://github.com/rtlabs-com/p-net/blob/public/src/drivers/lan9662/src/pf_cpm_driver_lan9662.c) (LAN9662 cyclic drivers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Integration boundary"]
  node_app["Device application<br/>integrator"]
  node_api["P-Net public API<br/>[pnet_api.h]"]
  node_pnal["PNAL platform abstraction<br/>OS/network contract<br/>[pnal.h]"]
  node_driver_hooks["Driver hooks<br/>driver contract<br/>[pf_driver.h]"]
end

subgraph group_device["Device behavior"]
  node_api_impl["API implementation<br/>bootstrap<br/>[pnet_api.c]"]
  node_connection["Connection state machines<br/>CMDEV/CMSM/CMRPC<br/>[pf_cmdev.c]"]
  node_configuration["Configuration and startup<br/>CMINA/CMPBE/FSPM<br/>[pf_cmina.c]"]
  node_io_records["IO and record services<br/>CMIO/CMRDR/CMWRR<br/>[pf_cmio.c]"]
  node_diagnostics["Diagnostics and alarms<br/>device state<br/>[pf_diag.c]"]
  node_ports["Physical port behavior<br/>port state<br/>[pf_port.c]"]
end

subgraph group_protocols["Protocols and cyclic IO"]
  node_rpc_epm["RPC endpoint mapping<br/>CMRPC/EPM<br/>[pf_cmrpc_epm.c]"]
  node_block_codec["Protocol block codec<br/>serialization<br/>[pf_block_reader.c]"]
  node_discovery["Discovery and management<br/>DCP/LLDP/SNMP<br/>[pf_dcp.c]"]
  node_cyclic_consumer["Cyclic IO consumer<br/>CPM<br/>[pf_cpm.c]"]
  node_cyclic_provider["Cyclic IO provider<br/>PPM<br/>[pf_ppm.c]"]
  node_timing["Scheduling and sync<br/>scheduler/PTCP<br/>[pf_scheduler.c]"]
  node_transport["Ethernet and UDP transport<br/>network adapter<br/>[pf_eth.c]"]
end

subgraph group_hardware["Runtime and hardware"]
  node_software_drivers["Software cyclic drivers<br/>CPM/PPM fallback<br/>[pf_cpm_driver_sw.c]"]
  node_lan9662_drivers["LAN9662 cyclic drivers<br/>hardware driver"]
  node_lan9662_uio["LAN9662 UIO resources<br/>MESA/UIO runtime<br/>[pf_rte_uio.c]"]
end

node_app -->|"configures and drives"| node_api
node_api -->|"implemented by"| node_api_impl
node_api_impl -->|"device lifecycle"| node_connection
node_api_impl -->|"configuration"| node_configuration
node_api_impl -->|"IO and records"| node_io_records
node_api_impl -->|"diagnosis and alarms"| node_diagnostics
node_connection -->|"establishes RPC"| node_rpc_epm
node_connection -->|"coordinates startup"| node_configuration
node_configuration -->|"serializes services"| node_block_codec
node_io_records -->|"controller outputs"| node_cyclic_consumer
node_io_records -->|"device inputs"| node_cyclic_provider
node_diagnostics -->|"delivers alarms"| node_transport
node_ports -->|"topology and port data"| node_discovery
node_discovery -->|"Ethernet management traffic"| node_transport
node_rpc_epm -->|"management transport"| node_transport
node_cyclic_consumer -->|"scheduled receive"| node_timing
node_cyclic_provider -->|"scheduled transmit"| node_timing
node_cyclic_consumer -->|"fallback driver"| node_software_drivers
node_cyclic_provider -->|"fallback driver"| node_software_drivers
node_cyclic_consumer -.->|"hardware option"| node_lan9662_drivers
node_cyclic_provider -.->|"hardware option"| node_lan9662_drivers
node_lan9662_drivers -->|"uses"| node_lan9662_uio
node_transport -->|"requires platform services"| node_pnal
node_timing -->|"requires clock and OS services"| node_pnal
node_software_drivers -->|"implements"| node_driver_hooks
node_lan9662_drivers -->|"implements"| node_driver_hooks

click node_api "https://github.com/rtlabs-com/p-net/blob/public/include/pnet_api.h"
click node_api_impl "https://github.com/rtlabs-com/p-net/blob/public/src/device/pnet_api.c"
click node_pnal "https://github.com/rtlabs-com/p-net/blob/public/include/pnal.h"
click node_driver_hooks "https://github.com/rtlabs-com/p-net/blob/public/src/pf_driver.h"
click node_connection "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmdev.c"
click node_configuration "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmina.c"
click node_io_records "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmio.c"
click node_diagnostics "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_diag.c"
click node_ports "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_port.c"
click node_rpc_epm "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmrpc_epm.c"
click node_block_codec "https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_block_reader.c"
click node_discovery "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_dcp.c"
click node_cyclic_consumer "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_cpm.c"
click node_cyclic_provider "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_ppm.c"
click node_timing "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_scheduler.c"
click node_transport "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_eth.c"
click node_software_drivers "https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_cpm_driver_sw.c"
click node_lan9662_drivers "https://github.com/rtlabs-com/p-net/blob/public/src/drivers/lan9662/src/pf_cpm_driver_lan9662.c"
click node_lan9662_uio "https://github.com/rtlabs-com/p-net/blob/public/src/drivers/lan9662/src/pf_rte_uio.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_api,node_pnal,node_driver_hooks toneBlue
class node_api_impl,node_connection,node_configuration,node_io_records,node_diagnostics,node_ports toneAmber
class node_rpc_epm,node_block_codec,node_discovery,node_cyclic_consumer,node_cyclic_provider,node_timing,node_transport toneMint
class node_software_drivers,node_lan9662_drivers,node_lan9662_uio toneRose
```

## Components

### Integration boundary

- **Device application**: integrator · Configures and drives the P-Net device.
- [P-Net public API](https://github.com/rtlabs-com/p-net/blob/public/include/pnet_api.h): C API · `include/pnet_api.h`
- [PNAL platform abstraction](https://github.com/rtlabs-com/p-net/blob/public/include/pnal.h): OS/network contract · Required target-specific runtime and network implementation. · `include/pnal.h`
- [Driver hooks](https://github.com/rtlabs-com/p-net/blob/public/src/pf_driver.h): driver contract · `src/pf_driver.h`

### Device behavior

- [API implementation](https://github.com/rtlabs-com/p-net/blob/public/src/device/pnet_api.c): bootstrap · `src/device/pnet_api.c`
- [Connection state machines](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmdev.c): CMDEV/CMSM/CMRPC · Coordinates lifecycle and controller connections. · `src/device/pf_cmdev.c`
- [Configuration and startup](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmina.c): CMINA/CMPBE/FSPM · `src/device/pf_cmina.c`
- [IO and record services](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmio.c): CMIO/CMRDR/CMWRR · `src/device/pf_cmio.c`
- [Diagnostics and alarms](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_diag.c): device state · `src/device/pf_diag.c`
- [Physical port behavior](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_port.c): port state · Maps interface events into PROFINET port state. · `src/device/pf_port.c`

### Protocols and cyclic IO

- [RPC endpoint mapping](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_cmrpc_epm.c): CMRPC/EPM · `src/device/pf_cmrpc_epm.c`
- [Protocol block codec](https://github.com/rtlabs-com/p-net/blob/public/src/device/pf_block_reader.c): serialization · `src/device/pf_block_reader.c`
- [Discovery and management](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_dcp.c): DCP/LLDP/SNMP · `src/common/pf_dcp.c`
- [Cyclic IO consumer](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_cpm.c): CPM · Receives controller output data. · `src/common/pf_cpm.c`
- [Cyclic IO provider](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_ppm.c): PPM · Transmits device input data. · `src/common/pf_ppm.c`
- [Scheduling and sync](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_scheduler.c): scheduler/PTCP · `src/common/pf_scheduler.c`
- [Ethernet and UDP transport](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_eth.c): network adapter · `src/common/pf_eth.c`

### Runtime and hardware

- [Software cyclic drivers](https://github.com/rtlabs-com/p-net/blob/public/src/common/pf_cpm_driver_sw.c): CPM/PPM fallback · `src/common/pf_cpm_driver_sw.c`
- [LAN9662 cyclic drivers](https://github.com/rtlabs-com/p-net/blob/public/src/drivers/lan9662/src/pf_cpm_driver_lan9662.c): hardware driver · Hardware-oriented replacement for software cyclic IO. · `src/drivers/lan9662/src/pf_cpm_driver_lan9662.c`
- [LAN9662 UIO resources](https://github.com/rtlabs-com/p-net/blob/public/src/drivers/lan9662/src/pf_rte_uio.c): MESA/UIO runtime · `src/drivers/lan9662/src/pf_rte_uio.c`

## Connections

- Device application → P-Net public API: configures and drives
- P-Net public API → API implementation: implemented by
- API implementation → Connection state machines: device lifecycle
- API implementation → Configuration and startup: configuration
- API implementation → IO and record services: IO and records
- API implementation → Diagnostics and alarms: diagnosis and alarms
- Connection state machines → RPC endpoint mapping: establishes RPC
- Connection state machines → Configuration and startup: coordinates startup
- Configuration and startup → Protocol block codec: serializes services
- IO and record services → Cyclic IO consumer: controller outputs
- IO and record services → Cyclic IO provider: device inputs
- Diagnostics and alarms → Ethernet and UDP transport: delivers alarms
- Physical port behavior → Discovery and management: topology and port data
- Discovery and management → Ethernet and UDP transport: Ethernet management traffic
- RPC endpoint mapping → Ethernet and UDP transport: management transport
- Cyclic IO consumer → Scheduling and sync: scheduled receive
- Cyclic IO provider → Scheduling and sync: scheduled transmit
- Cyclic IO consumer → Software cyclic drivers: fallback driver
- Cyclic IO provider → Software cyclic drivers: fallback driver
- Cyclic IO consumer → LAN9662 cyclic drivers: hardware option
- Cyclic IO provider → LAN9662 cyclic drivers: hardware option
- LAN9662 cyclic drivers → LAN9662 UIO resources: uses
- Ethernet and UDP transport → PNAL platform abstraction: requires platform services
- Scheduling and sync → PNAL platform abstraction: requires clock and OS services
- Software cyclic drivers → Driver hooks: implements
- LAN9662 cyclic drivers → Driver hooks: implements
