Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb/Defs.hxx
Go to the documentation of this file.
1
34#ifndef _OPENLCB_DEFS_HXX_
35#define _OPENLCB_DEFS_HXX_
36
37#include <cstdint>
38
39#include "utils/macros.h"
40
41namespace openlcb
42{
43
45typedef uint64_t NodeID;
46
48typedef uint16_t NodeAlias;
49
51typedef string Payload;
52
55static const NodeAlias NOT_RESPONDING = 0xF000;
56
59{
63 explicit NodeHandle(NodeID _id) : id(_id), alias(0) {}
64 explicit NodeHandle(NodeAlias _alias) : id(0), alias(_alias) {}
65 NodeHandle(NodeID _id, NodeAlias _alias) : id(_id), alias(_alias) {}
66 NodeHandle() : id(0), alias(0) {}
67
69 void clear()
70 {
71 id = 0;
72 alias = 0;
73 }
74
79 bool operator==(const NodeHandle& o) const
80 {
81 return id == o.id && alias == o.alias;
82 }
83};
84
86enum class EventState
87{
88 VALID = 0,
89 INVALID = 1,
90 RESERVED = 2,
91 UNKNOWN = 3
92};
93
96struct Defs
97{
100 enum MTI
101 {
102 MTI_EXACT = 0xFFFF,
103 MTI_NONE = 0x0000,
126 MTI_LEARN_EVENT = 0x0594,
127 MTI_EVENT_REPORT = 0x05B4,
128 MTI_TRACTION_CONTROL_COMMAND = 0x05EB,
129 MTI_TRACTION_CONTROL_REPLY = 0x01E9,
130 MTI_TRACTION_PROXY_COMMAND = 0x05EA,
131 MTI_TRACTION_PROXY_REPLY = 0x01E8,
132 MTI_XPRESSNET = 0x09C0,
135 MTI_DATAGRAM = 0x1C48,
145 MTI_EVENT_MASK = 0x0004,
148 MTI_TYPE_MASK = 0x03e0,
164 };
165
167 {
168 ERROR_CODE_OK = 0,
169
170 ERROR_PERMANENT = 0x1000,
171 ERROR_TEMPORARY = 0x2000,
172
173 ERROR_SRC_NOT_PERMITTED = 0x1020,
174 ERROR_OPENMRN_NOT_FOUND = 0x1030,
175 ERROR_UNIMPLEMENTED = 0x1040,
176 ERROR_INVALID_ARGS = 0x1080,
177
178 ERROR_OPENLCB_TIMEOUT = 0x2030,
179 ERROR_OUT_OF_ORDER = 0x2040,
180
181 ERROR_INVALID_ARGS_MESSAGE_TOO_SHORT = ERROR_INVALID_ARGS | 1,
182
183 ERROR_UNIMPLEMENTED_MTI = ERROR_UNIMPLEMENTED | 3,
184 ERROR_UNIMPLEMENTED_CMD = ERROR_UNIMPLEMENTED | 2,
185 ERROR_UNIMPLEMENTED_SUBCMD = ERROR_UNIMPLEMENTED | 1,
186
187 ERROR_OPENMRN_ALREADY_EXISTS = ERROR_OPENMRN_NOT_FOUND | 2,
188
189 // Codes defined by the firmware upgrade standard
190
192 ERROR_FIRMWARE_INCOMPATIBLE = ERROR_INVALID_ARGS | 8,
194 ERROR_FIRMWARE_CORRUPTED = ERROR_INVALID_ARGS | 9,
197
198 // Internal error codes generated by the stack.
199 ERROR_DST_NOT_FOUND = 0x40000, //< on CAN. Permanent error code.
200 // There is a conflict with MinGW macros here.
201 OPENMRN_TIMEOUT = 0x80000, //< Timeout waiting for ack/nack.
202 ERROR_DST_REBOOT = 0x100000, //< Target node has rebooted.
203 ERROR_REJECTED = 0x200000, //< Target node has rejectedthe request.
204 };
205
209 {
210 SIMPLE_PROTOCOL_SUBSET = 0x800000000000,
211 DATAGRAM = 0x400000000000,
212 STREAM = 0x200000000000,
213 MEMORY_CONFIGURATION = 0x100000000000,
214 RESERVATION = 0x080000000000,
215 EVENT_EXCHANGE = 0x040000000000,
216 IDENTIFICATION = 0x020000000000,
217 LEARN_CONFIGURATION = 0x010000000000,
218 REMOTE_BUTTON = 0x008000000000,
219 ABBREVIATED_DEFAULT_CDI = 0x004000000000,
220 DISPLAY_PROTOCOL = 0x002000000000,
221 SIMPLE_NODE_INFORMATION = 0x001000000000,
222 CDI = 0x000800000000,
223 TRACTION_CONTROL = 0x000400000000,
224 TRACTION_FDI = 0x000200000000,
225 TRACTION_PROXY = 0x000100000000,
226 TRACTION_SIMPLE_TRAIN_INFO
227 = 0x000080000000,
228 FUNCTION_CONFIGURATION = 0x000040000000,
229 FIRMWARE_UPGRADE = 0x000020000000,
230 FIRMWARE_UPGRADE_ACTIVE = 0x000010000000,
231 RESERVED_MASK = 0x00000FFFFFFF
232 };
233
238 static constexpr uint64_t EMERGENCY_OFF_EVENT = 0x010000000000FFFFULL;
239
244 static constexpr uint64_t CLEAR_EMERGENCY_OFF_EVENT = 0x010000000000FFFEULL;
245
255 static constexpr uint64_t EMERGENCY_STOP_EVENT = 0x010000000000FFFDULL;
256
260 static constexpr uint64_t CLEAR_EMERGENCY_STOP_EVENT = 0x010000000000FFFCULL;
261
265 static constexpr uint64_t POWER_STANDARD_BROWNOUT_EVENT = 0x010000000000FFF0ULL;
266
270 static constexpr uint64_t NODE_POWER_BROWNOUT_EVENT = 0x010000000000FFF1ULL;
271
275 static constexpr uint64_t NODE_IDENT_BUTTON_EVENT = 0x010000000000FE00ULL;
276
283
288 static bool get_mti_address(MTI mti)
289 {
290 return (mti & MTI_ADDRESS_MASK);
291 }
292
297 static bool get_mti_event(MTI mti)
298 {
299 return (mti & MTI_EVENT_MASK);
300 }
301
306 static bool get_mti_datagram(MTI mti)
307 {
308 return (mti & MTI_DATAGRAM_MASK);
309 }
310
315 static unsigned int mti_priority(MTI mti)
316 {
317 return (mti & MTI_PRIORITY_MASK) >> MTI_PRIORITY_SHIFT;
318 }
319
321 static const size_t MAX_ADDRESSED_SIZE = 14;
322
323private:
326};
327
331{
332 switch (state)
333 {
334 case EventState::VALID:
335 return EventState::INVALID;
336 case EventState::INVALID:
337 return EventState::VALID;
338 default:
339 return state;
340 }
341}
342
345inline EventState to_event_state(bool state)
346{
347 return state ? EventState::VALID : EventState::INVALID;
348}
349
352inline Defs::MTI operator+(const Defs::MTI &value, EventState state)
353{
354 int code = static_cast<int>(value);
355 code += static_cast<int>(state);
356 return static_cast<Defs::MTI>(code);
357}
358
361{
362 Defs::MTI result = value;
363 value = static_cast<Defs::MTI>(static_cast<int>(value) + 1);
364 return result;
365}
366
369{
370 value = static_cast<Defs::MTI>(static_cast<int>(value) + 1);
371 return value;
372}
373
376{
377 Defs::MTI result = value;
378 value = static_cast<Defs::MTI>(static_cast<int>(value) - 1);
379 return result;
380}
381
384{
385 value = static_cast<Defs::MTI>(static_cast<int>(value) - 1);
386 return value;
387}
388
389}; /* namespace openlcb */
390
391#endif // _OPENLCB_DEFS_HXX_
Defs::MTI operator+(const Defs::MTI &value, EventState state)
Allows of setting the producer/consumer identified MTI with the event state to set the low bits.
EventState invert_event_state(EventState state)
Returns the inverted event state, switching valid and invalid, but not changing unknown and reserved.
uint64_t NodeID
48-bit NMRAnet Node ID type
Defs::MTI operator++(Defs::MTI &value, int)
Operator overload for post increment.
EventState
Allowed states of producers and consumers.
string Payload
Container that carries the data bytes in an NMRAnet message.
static const NodeAlias NOT_RESPONDING
Guard value put into the the internal node alias maps when a node ID could not be translated to a val...
EventState to_event_state(bool state)
Returns the inverted event state, switching valid and invalid, but not changing unknown and reserved.
Defs::MTI operator--(Defs::MTI &value, int)
Operator overload for post decrement.
uint16_t NodeAlias
Alias to a 48-bit NMRAnet Node ID type.
The generic interface for NMRAnet network interfaces.
static constexpr uint64_t EMERGENCY_STOP_EVENT
"Emergency stop of all operations" The Emergency Stop Event is a request for a node to command all of...
static unsigned int mti_priority(MTI mti)
Get the MTI priority (value 0 through 3).
MTI
Known Message type indicators.
@ MTI_IDENT_INFO_REPLY
node identity reply
@ MTI_RESERVED_SHIFT
reserved shift
@ MTI_VERIFY_NODE_ID_ADDRESSED
verify a Node ID
@ MTI_CONSUMER_IDENTIFIED_INVALID
consumer broadcast, invalid state
@ MTI_PRODUCER_IDENTIFY
query about producers
@ MTI_EVENT_SHIFT
event number present shift
@ MTI_VERIFY_NODE_ID_GLOBAL
verify a Node ID globally
@ MTI_IDENT_INFO_REQUEST
request node identity
@ MTI_DATAGRAM_REJECTED
datagram rejected by receiver
@ MTI_EVENT_MASK
event number present mask
@ MTI_EVENTS_IDENTIFY_GLOBAL
request identify all of every node's events
@ MTI_CONSUMER_IDENTIFIED_RESERVED
reserved for future use
@ MTI_PRODUCER_IDENTIFIED_RANGE
producer broadcast about a range of producers
@ MTI_STREAM_PROCEED
stream flow control
@ MTI_MODIFIER_MASK
modifier within Priority/Type mask
@ MTI_PRODUCER_IDENTIFIED_RESERVED
reserved for future use
@ MTI_SPECIAL_SHIFT
special shift
@ MTI_MODIFIER_SHIFT
modifier within Priority/Type shift
@ MTI_SIMPLE_MASK
simple protocol mask
@ MTI_DATAGRAM_SHIFT
stream or datagram shift
@ MTI_EXACT
match mask for a single MTI
@ MTI_RESERVED_MASK
reserved mask
@ MTI_SIMPLE_SHIFT
simple protocol shift
@ MTI_PROTOCOL_SUPPORT_INQUIRY
inquire on supported protocols
@ MTI_PRIORITY_SHIFT
priority shift
@ MTI_TYPE_MASK
type within priority mask
@ MTI_CONSUMER_IDENTIFIED_UNKNOWN
consumer broadcast, validity unknown
@ MTI_TYPE_SHIFT
type within priority shift
@ MTI_ADDRESS_SHIFT
Address present shift.
@ MTI_PRODUCER_IDENTIFIED_VALID
producer broadcast, valid state
@ MTI_STREAM_INITIATE_REPLY
Stream initiate reply.
@ MTI_NONE
invalid MTI
@ MTI_VERIFIED_NODE_ID_NUMBER
respond to a verify Node ID request
@ MTI_CONSUMER_IDENTIFY
query about consumers
@ MTI_EVENTS_IDENTIFY_ADDRESSED
request identify all of a node's events
@ MTI_PRODUCER_IDENTIFIED_INVALID
producer broadcast, invalid state
@ MTI_STREAM_INITIATE_REQUEST
Stream initiate request.
@ MTI_DATAGRAM_OK
datagram received okay
@ MTI_STREAM_COMPLETE
stream terminate connection
@ MTI_PRODUCER_IDENTIFIED_UNKNOWN
producer broadcast, validity unknown
@ MTI_PRIORITY_MASK
priority mask
@ MTI_STREAM_DATA
stream data
@ MTI_ADDRESS_MASK
Address present mask.
@ MTI_INITIALIZATION_COMPLETE
initialization complete
@ MTI_CONSUMER_IDENTIFIED_VALID
consumer broadcast, valid state
@ MTI_OPTIONAL_INTERACTION_REJECTED
rejected request
@ MTI_SPECIAL_MASK
special mask
@ MTI_CONSUMER_IDENTIFIED_RANGE
consumer broadcast about a range of consumers
@ MTI_PROTOCOL_SUPPORT_REPLY
reply with supported protocols
@ MTI_DATAGRAM_MASK
stream or datagram mask
@ MTI_TERMINATE_DUE_TO_ERROR
terminate due to some error
@ MTI_DATAGRAM
datagram
Protocols
Bitmask for all potentially supported NMRAnet protocols.
@ ERROR_FIRMWARE_CORRUPTED
The firmware data is invalid or corrupted.
@ ERROR_FIRMWARE_INCOMPATIBLE
The firmware data is incompatible with this hardware node.
@ ERROR_FIRMWARE_CSUM
The firmware written has failed checksum (temporary error).
static constexpr uint64_t EMERGENCY_OFF_EVENT
"Emergency off (de-energize)" Producing this event causes an Emergency Off (de-energize).
static constexpr uint64_t CLEAR_EMERGENCY_STOP_EVENT
"Clear Emergency stop of all operations" Producing this event clears an Emergency Stop.
static constexpr uint64_t NODE_IDENT_BUTTON_EVENT
"Ident button combination pressed" This event can be generated by a node when it is instructed to gen...
LinkStatus
Status of the pysical layer link.
@ LINK_DOWN
link is down and unable to transmit
@ LINK_UP
link is up and ready for transmit
static constexpr uint64_t NODE_POWER_BROWNOUT_EVENT
"Power supply brownout detected below minimum required by node" This event can be generated when a no...
static constexpr uint64_t POWER_STANDARD_BROWNOUT_EVENT
"Power supply brownout detected below minimum required by standard" This event can be generated when ...
Defs()
This struct should not be instantiated.
static bool get_mti_event(MTI mti)
Get the MTI event present value field.
static const size_t MAX_ADDRESSED_SIZE
Maximum size of a static addressed message payload.
static bool get_mti_datagram(MTI mti)
Get the MTI datagram or stream value field.
static bool get_mti_address(MTI mti)
Get the MTI address present value field.
static constexpr uint64_t CLEAR_EMERGENCY_OFF_EVENT
"Clear emergency off (energize)" Producing this event clears an Emergency Off (energize).
Container of both a NodeID and NodeAlias.
NodeID id
48-bit NMRAnet Node ID
NodeAlias alias
alias to NMRAnet Node ID
bool operator==(const NodeHandle &o) const
Compares two NodeHandle instances.
void clear()
Resets node handle to global (broadcast) handle.