Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
esi_data_type.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <nlohmann/json_fwd.hpp>
6#include <optional>
7#include <string_view>
8
9// <cmath>/<math.h> — pulled in transitively by the STL headers above — defines the obsolete SVID
10// matherr macro DOMAIN on macOS/Clang and Windows/MSVC. Left in place it would expand inside the
11// kPrimitiveTypes table below and break the build. glibc gates the macro behind a feature test
12// that -std=c++2b disables, so only the non-Linux toolchains hit this. See the identical guard in
13// libs/comm/object_data_types.h.
14#ifdef DOMAIN
15#undef DOMAIN
16#endif
17
18namespace mm::etg {
19
29enum class ObjectCode : uint16_t {
30 Var = 0x07,
31 Array = 0x08,
32 Record = 0x09,
33};
34
36constexpr std::string_view objectCodeName(ObjectCode code) {
37 switch (code) {
38 case ObjectCode::Var:
39 return "VAR";
41 return "ARRAY";
43 return "RECORD";
44 }
45 return "VAR";
46}
47
58 std::string_view name;
59 uint16_t code = 0;
60 uint16_t bitSize = 0;
61 bool isSigned = false;
62};
63
70inline constexpr auto kPrimitiveTypes = std::to_array<PrimitiveType>({
71 {"BOOL", 0x0001, 1, false},
72 {"BOOLEAN", 0x0001, 1, false},
73
74 {"SINT", 0x0002, 8, true},
75 {"INT", 0x0003, 16, true},
76 {"INT24", 0x0010, 24, true},
77 {"DINT", 0x0004, 32, true},
78 {"INT40", 0x0012, 40, true},
79 {"INT48", 0x0013, 48, true},
80 {"INT56", 0x0014, 56, true},
81 {"LINT", 0x0015, 64, true},
82
83 {"USINT", 0x0005, 8, false},
84 {"UINT", 0x0006, 16, false},
85 {"UINT24", 0x0016, 24, false},
86 {"UDINT", 0x0007, 32, false},
87 {"UINT40", 0x0018, 40, false},
88 {"UINT48", 0x0019, 48, false},
89 {"UINT56", 0x001A, 56, false},
90 {"ULINT", 0x001B, 64, false},
91
92 {"REAL", 0x0008, 32, false},
93 {"LREAL", 0x0011, 64, false},
94
95 // Bit-string types. BYTE/WORD/DWORD are distinct CoE codes from the same-width unsigned
96 // integers: a device reporting WORD (0x001F) is describing a bitfield, and rendering it as
97 // UNSIGNED16 (0x0006) would lose that. v5.6.6.xml uses WORD, so this distinction is live.
98 {"BYTE", 0x001E, 8, false},
99 {"WORD", 0x001F, 16, false},
100 {"DWORD", 0x0020, 32, false},
101 {"BITARR8", 0x002D, 8, false},
102 {"BITARR16", 0x002E, 16, false},
103 {"BITARR32", 0x002F, 32, false},
104 {"BIT1", 0x0030, 1, false},
105 {"BIT2", 0x0031, 2, false},
106 {"BIT3", 0x0032, 3, false},
107 {"BIT4", 0x0033, 4, false},
108 {"BIT5", 0x0034, 5, false},
109 {"BIT6", 0x0035, 6, false},
110 {"BIT7", 0x0036, 7, false},
111 {"BIT8", 0x0037, 8, false},
112 {"BIT9", 0x0038, 9, false},
113 {"BIT10", 0x0039, 10, false},
114 {"BIT11", 0x003A, 11, false},
115 {"BIT12", 0x003B, 12, false},
116 {"BIT13", 0x003C, 13, false},
117 {"BIT14", 0x003D, 14, false},
118 {"BIT15", 0x003E, 15, false},
119 {"BIT16", 0x003F, 16, false},
120
121 {"GUID", 0x001D, 128, false},
122 {"TIME_OF_DAY", 0x000C, 48, false},
123 {"TIME_DIFFERENCE", 0x000D, 48, false},
124 {"DOMAIN", 0x000F, 0, false},
125
126 // Parameterised: width comes from the element count in the name.
127 {"STRING", 0x0009, 0, false}, // VISIBLE_STRING, 8 bits per element.
128 {"OCTET_STRING", 0x000A, 0, false}, // 8 bits per element.
129 {"UNICODE_STRING", 0x000B, 0, false}, // 16 bits per element.
130});
131
148std::optional<PrimitiveType> resolvePrimitiveType(std::string_view esiName);
149
154std::string_view arrayElementTypeName(std::string_view esiName);
155
158bool isStringTypeName(std::string_view esiName);
159
167enum class ValueKind : uint8_t {
168 Bool,
169 Int8,
170 Int16,
171 Int32,
172 Int64,
173 Uint8,
174 Uint16,
175 Uint32,
176 Uint64,
177 Real32,
178 Real64,
179 String,
180 Bytes,
181};
182
189constexpr std::string_view cxxTypeName(ValueKind kind) {
190 switch (kind) {
191 case ValueKind::Bool:
192 case ValueKind::Uint8:
193 return "uint8_t";
194 case ValueKind::Int8:
195 return "int8_t";
196 case ValueKind::Int16:
197 return "int16_t";
198 case ValueKind::Int32:
199 return "int32_t";
200 case ValueKind::Int64:
201 return "int64_t";
203 return "uint16_t";
205 return "uint32_t";
207 return "uint64_t";
209 return "float";
211 return "double";
213 return "std::string";
214 case ValueKind::Bytes:
215 return "std::vector<uint8_t>";
216 }
217 return "std::vector<uint8_t>";
218}
219
233ValueKind resolveValueKind(uint16_t dataType, uint16_t bitSize);
234
236void to_json(nlohmann::json& j, const PrimitiveType& type);
237
238} // namespace mm::etg
uint16_t bitSize
Definition esi_entry.cc:286
Definition eni.cc:18
void to_json(nlohmann::json &j, const EniNetwork &network)
Serialises a network as JSON, for a client that renders rather than replays it.
Definition eni.cc:907
ObjectCode
CoE object code (ETG.1000.6 §5 Table 41 "Object Code").
Definition esi_data_type.h:29
@ Array
Subindex 0 is the entry count; 1..N share one element type.
@ Var
A single value: no subindices beyond 0.
@ Record
Subindex 0 is the entry count; 1..N each have their own type.
std::optional< PrimitiveType > resolvePrimitiveType(std::string_view esiName)
Resolves an ESI type name to its CoE code, width and signedness.
Definition esi_data_type.cc:65
constexpr auto kPrimitiveTypes
Catalogue of the ESI primitive types, keyed by their IEC 61131-3 name.
Definition esi_data_type.h:70
bool isStringTypeName(std::string_view esiName)
True when esiName is a parameterised string type — STRING(n) / OCTET_STRING(n) / UNICODE_STRING(n).
Definition esi_data_type.cc:56
constexpr std::string_view objectCodeName(ObjectCode code)
Returns the ESI/CiA spelling of code — "VAR", "ARRAY" or "RECORD".
Definition esi_data_type.h:36
std::string_view arrayElementTypeName(std::string_view esiName)
Returns the element type name of an "ARRAY [a..b] OF T" composition, else empty.
Definition esi_data_type.cc:43
constexpr std::string_view cxxTypeName(ValueKind kind)
The C++ spelling of kind — "int32_t", "std::string", "std::vector<uint8_t>".
Definition esi_data_type.h:189
ValueKind resolveValueKind(uint16_t dataType, uint16_t bitSize)
Resolves the C++ type an entry holds, from its ETG.1020 code and declared width.
Definition esi_data_type.cc:98
ValueKind
The C++ type an entry's value maps to.
Definition esi_data_type.h:167
@ String
STRING(n) / UNICODE_STRING(n).
@ Uint32
UDINT, DWORD, BITARR32.
@ Uint16
UINT, WORD, BITARR16, BIT9..BIT16.
@ Uint8
USINT, BYTE, BITARR8, BIT1..BIT8.
@ Bytes
Everything else, including any type/width contradiction.
@ Bool
BOOL — carried in a uint8_t, as CoE does.
One primitive type: its ESI spelling and the CoE numbering it maps onto.
Definition esi_data_type.h:57
uint16_t bitSize
Bit width; 0 for types whose width comes from a parameter.
Definition esi_data_type.h:60
std::string_view name
Canonical ESI name (the base name for parameterised types).
Definition esi_data_type.h:58
uint16_t code
ETG.1020 data type code.
Definition esi_data_type.h:59
bool isSigned
True for two's-complement signed integers.
Definition esi_data_type.h:61