PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
virtual_phoenix_plc.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <cstdint>
12#include <memory>
13#include <mutex>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20#include "profinet/IOSignal.h"
21#include "profinet/cyclic.h"
22#include "profinet/gsdml.h"
23#include "profinet/rt.h"
25#include "profinet/util.h"
26
27namespace profinet::sim
28{
29
30class VirtualAumaValve;
31
41
51{
52 public:
57 VirtualPhoenixPlc(std::string name,
60
63
66
73 void Connect(asio::io_context& ioContext,
75 VirtualAumaValve& valve,
76 rt::IOCRConfig inputIocr,
77 rt::IOCRConfig outputIocr,
78 int maxConsecutiveTimeouts = cyclic::DEFAULT_MAX_CONSECUTIVE_TIMEOUTS);
79
82 void Disconnect(const std::string& valveName);
83
85 void Stop();
86
91 void SetOutput(const std::string& valveName,
92 std::string_view signalTextId,
93 double value);
94
99 [[nodiscard]] std::optional<double> ReadInput(const std::string& valveName,
100 std::string_view signalTextId) const;
101
105 void SetLocalInputBytes(std::string_view signalTextId, const Bytes& data);
106
110 void SetLocalInputValue(std::string_view signalTextId, double value);
111
115 [[nodiscard]] std::optional<double> GetLocalInputValue(std::string_view signalTextId) const;
116
120 void SetLocalOutputBytes(std::string_view signalTextId, const Bytes& data);
121
125 [[nodiscard]] std::optional<Bytes> GetLocalOutputBytes(std::string_view signalTextId) const;
126
130 void SetLocalOutputValue(std::string_view signalTextId, double value);
131
135 [[nodiscard]] std::optional<double> GetLocalOutputValue(std::string_view signalTextId) const;
136
140 [[nodiscard]] std::optional<Bytes> GetLocalInputBytes(std::string_view signalTextId) const;
141
143 [[nodiscard]] const io::IoSignalCatalog& SignalCatalog() const noexcept;
144
146 [[nodiscard]] const MacAddress& ControllerMac() const noexcept;
147
149 [[nodiscard]] const std::string& Name() const noexcept;
150
152 [[nodiscard]] std::size_t ConnectionCount() const;
153
155 [[nodiscard]] std::optional<cyclic::CyclicState>
156 ConnectionState(const std::string& valveName) const;
157
159 [[nodiscard]] std::optional<cyclic::CyclicStatsSnapshot>
160 ConnectionStats(const std::string& valveName) const;
161
163 [[nodiscard]] std::string StatusReport() const;
164
165 private:
167 {
168 std::string name;
169 VirtualAumaValve* valve = nullptr;
170 std::unique_ptr<cyclic::CyclicController> controller;
172 };
173
175 [[nodiscard]] Connection* FindConnection(const std::string& valveName);
176
178 [[nodiscard]] const Connection* FindConnection(const std::string& valveName) const;
179
181 [[nodiscard]] static const io::IoSignal*
183 std::string_view textId,
184 io::IoSignalDirection direction);
185
187 [[nodiscard]] double DecodeSignal(const io::IoSignal& signal,
188 const Bytes& data) const;
189
191 [[nodiscard]] Bytes EncodeSignalValue(const io::IoSignal& signal,
192 const Bytes& data,
193 double value) const;
194
196 [[nodiscard]] static std::size_t RequiredImageSize(const io::IoSignalCatalog& catalog);
197
199 [[nodiscard]] const io::IoSignal* FindLocalInputSignal(std::string_view textId) const;
200
202 [[nodiscard]] const io::IoSignal* FindLocalOutputSignal(std::string_view textId) const;
203
205 [[nodiscard]] static std::pair<std::uint16_t, std::uint16_t> ResolveDeviceLocation(
206 const io::IoSignal& signal,
207 std::uint16_t defaultSlot,
208 std::uint16_t defaultSubslot);
209
210 std::string name;
214 mutable std::mutex mutex;
215 std::vector<std::unique_ptr<Connection>> connections;
217};
218
219} // namespace profinet::sim
Defines the GSDML-derived logical process-data signal catalogue.
Builds and owns the logical sensor and actuator signals described by a GSDML device.
Definition IOSignal.h:186
Small virtual Ethernet switch suitable for deterministic tests and demos.
AUMA SGx application model that behaves as a real RTC1 peer in memory.
Virtual Phoenix Contact AXC F 1252 controller hosting real CyclicController instances.
void SetLocalOutputValue(std::string_view signalTextId, double value)
Set one typed Phoenix output signal.
std::optional< double > ReadInput(const std::string &valveName, std::string_view signalTextId) const
Read a typed AUMA input signal using the production controller API.
void SetLocalOutputBytes(std::string_view signalTextId, const Bytes &data)
Set raw bytes in the local Phoenix output process image.
const std::string & Name() const noexcept
Return the simulator instance name.
void SetOutput(const std::string &valveName, std::string_view signalTextId, double value)
Set a typed AUMA actuator signal.
const io::IoSignalCatalog & SignalCatalog() const noexcept
Return the Phoenix PLC's logical signal catalogue.
void Stop()
Stop all active connections.
double DecodeSignal(const io::IoSignal &signal, const Bytes &data) const
Decode one GSDML signal into the connector's double representation.
VirtualPhoenixPlc & operator=(const VirtualPhoenixPlc &)=delete
void Disconnect(const std::string &valveName)
Disconnect a named AUMA connection.
void Connect(asio::io_context &ioContext, InMemoryEthernetBus &bus, VirtualAumaValve &valve, rt::IOCRConfig inputIocr, rt::IOCRConfig outputIocr, int maxConsecutiveTimeouts=cyclic::DEFAULT_MAX_CONSECUTIVE_TIMEOUTS)
Connect the PLC to one virtual AUMA device.
const MacAddress & ControllerMac() const noexcept
Return the PLC MAC address.
std::optional< Bytes > GetLocalInputBytes(std::string_view signalTextId) const
Read raw bytes from the local Phoenix input process image.
std::size_t ConnectionCount() const
Return the number of active AUMA connections.
std::optional< cyclic::CyclicState > ConnectionState(const std::string &valveName) const
Return one connection state.
const io::IoSignal * FindLocalInputSignal(std::string_view textId) const
Return an input signal from the local Phoenix catalogue.
static std::size_t RequiredImageSize(const io::IoSignalCatalog &catalog)
Return the image size required by a GSDML catalogue.
std::string StatusReport() const
Return a human-readable PLC and connection dashboard.
std::vector< std::unique_ptr< Connection > > connections
static const io::IoSignal * FindDeviceSignal(const VirtualAumaValve &valve, std::string_view textId, io::IoSignalDirection direction)
Find a unique signal in a connected device catalogue.
const io::IoSignal * FindLocalOutputSignal(std::string_view textId) const
Return an output signal from the local Phoenix catalogue.
static std::pair< std::uint16_t, std::uint16_t > ResolveDeviceLocation(const io::IoSignal &signal, std::uint16_t defaultSlot, std::uint16_t defaultSubslot)
Find a unique configured slot/subslot for an AUMA signal.
std::optional< Bytes > GetLocalOutputBytes(std::string_view signalTextId) const
Read raw bytes from the local Phoenix output process image.
std::optional< double > GetLocalInputValue(std::string_view signalTextId) const
Read one typed Phoenix input signal.
Bytes EncodeSignalValue(const io::IoSignal &signal, const Bytes &data, double value) const
Encode one connector double into a device output image.
VirtualPhoenixPlc(const VirtualPhoenixPlc &)=delete
void SetLocalInputBytes(std::string_view signalTextId, const Bytes &data)
Set raw bytes in the local Phoenix input process image.
std::optional< cyclic::CyclicStatsSnapshot > ConnectionStats(const std::string &valveName) const
Return one connection statistics snapshot.
Connection * FindConnection(const std::string &valveName)
Find a mutable connection by valve name.
const gsdml::GSDMLDevice & gsdml
~VirtualPhoenixPlc()
Stop all device connections.
std::optional< double > GetLocalOutputValue(std::string_view signalTextId) const
Read one typed Phoenix output signal.
void SetLocalInputValue(std::string_view signalTextId, double value)
Set one typed Phoenix input signal.
Declares the PROFINET RTC1 cyclic IO controller and process-data exchange API.
Declares the GSDML parser and PROFINET device-description data model.
Thread-safe in-memory Layer-2 Ethernet switch for PROFINET simulation.
constexpr int DEFAULT_MAX_CONSECUTIVE_TIMEOUTS
Default number of consecutive watchdog timeouts before transitioning to Fault.
Definition cyclic.h:157
IoSignalDirection
Logical direction of a process-data signal from the controller's perspective.
Definition IOSignal.h:30
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
Definition util.h:48
std::vector< std::uint8_t > Bytes
Generic byte buffer alias used throughout the library for raw wire data.
Definition protocol.h:55
Declares PROFINET RT_CLASS_1 cyclic frame and IOCR data structures.
Complete parsed GSDML device profile.
Definition gsdml.h:166
Describes one logical process-data signal available from a GSDML device.
Definition IOSignal.h:83
IOCR configuration derived from AR setup: timing parameters and IO object mappings needed for cyclic ...
Definition rt.h:137
Local Phoenix PLC process image derived from its GSDML signal layout.
Bytes inputs
PLC-side input process image.
Bytes outputs
PLC-side output process image.
std::unique_ptr< cyclic::CyclicController > controller
std::unique_ptr< asio::io_context > ioContext
Shared ASIO context.
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.