Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CanDefs.hxx
Go to the documentation of this file.
1
34#ifndef _OPENLCB_CANDEFS_HXX_
35#define _OPENLCB_CANDEFS_HXX_
36
37#include <cstdint>
38
39#include "openlcb/Defs.hxx"
40#include "can_frame.h"
41#include "nmranet_config.h"
42
43namespace openlcb
44{
45
47struct CanDefs {
57
58
93
94 // @TODO(balazs.racz) do we need this?
95 typedef uint16_t CanMTI;
96
107
110 {
112 NMRANET_MSG = 1
113 };
114
123
125 {
126 RID_FRAME = 0x0700,
127 AMD_FRAME = 0x0701,
128 AME_FRAME = 0x0702,
129 AMR_FRAME = 0x0703
130 };
131
132 enum AddressedPayloadFlags
133 {
134 NOT_FIRST_FRAME = 0x20,
135 NOT_LAST_FRAME = 0x10,
136 };
137
149
154 static NodeAlias get_src(uint32_t can_id)
155 {
156 return (can_id & SRC_MASK) >> SRC_SHIFT;
157 }
158
163 static CanMTI get_mti(uint32_t can_id)
164 {
165 return ((can_id & MTI_MASK) >> MTI_SHIFT);
166 }
167
172 static NodeAlias get_dst(uint32_t can_id)
173 {
174 return (can_id & DST_MASK) >> DST_SHIFT;
175 }
176
181 static CanFrameType get_can_frame_type(uint32_t can_id)
182 {
184 }
185
190 static FrameType get_frame_type(uint32_t can_id)
191 {
192 return (FrameType)((can_id & FRAME_TYPE_MASK) >> FRAME_TYPE_SHIFT);
193 }
194
199 static Priority get_priority(uint32_t can_id)
200 {
201 return (Priority)((can_id & PRIORITY_MASK) >> PRIORITY_SHIFT);
202 }
203
208 static bool is_cid_frame(uint32_t can_id)
209 {
210 return ((can_id >> CAN_FRAME_TYPE_SHIFT) & 0x1C) == 0x14;
211 }
212
217 static bool is_stream_frame(uint32_t can_id)
218 {
219 return ((can_id >> CAN_FRAME_TYPE_SHIFT) & 0xF) == 0xF;
220 }
221
226 static void set_mti(uint32_t *can_id, CanMTI mti)
227 {
228 *can_id &= ~MTI_MASK;
229 *can_id |= mti << MTI_SHIFT;
230 }
231
236 static void set_src(uint32_t *can_id, NodeAlias src)
237 {
238 *can_id &= ~SRC_MASK;
239 *can_id |= src << SRC_SHIFT;
240 }
241
246 static void set_dst(uint32_t *can_id, NodeAlias dst)
247 {
248 *can_id &= ~DST_MASK;
249 *can_id |= dst << DST_SHIFT;
250 }
251
256 static void set_can_frame_type(uint32_t *can_id, CanFrameType type)
257 {
258 *can_id &= ~CAN_FRAME_TYPE_MASK;
259 *can_id |= type << CAN_FRAME_TYPE_SHIFT;
260 }
261
266 static void set_frame_type(uint32_t *can_id, FrameType type)
267 {
268 *can_id &= ~FRAME_TYPE_MASK;
269 *can_id |= type << FRAME_TYPE_SHIFT;
270 }
271
276 static void set_priority(uint32_t *can_id, Priority priority)
277 {
278 *can_id &= ~PRIORITY_MASK;
279 *can_id |= priority << PRIORITY_SHIFT;
280 }
281
290 static void set_fields(uint32_t *can_id, NodeAlias src, Defs::MTI mti, CanFrameType can_type, FrameType type, Priority priority)
291 {
292 *can_id = (src << SRC_SHIFT ) +
293 (mti << MTI_SHIFT ) +
294 (can_type << CAN_FRAME_TYPE_SHIFT) +
295 (type << FRAME_TYPE_SHIFT ) +
296 (priority << PRIORITY_SHIFT );
297 }
298
305 static void set_datagram_fields(uint32_t *can_id, NodeAlias src,
306 NodeAlias dst, CanFrameType can_type)
307 {
308 *can_id = (src << SRC_SHIFT ) +
309 (dst << DST_SHIFT ) +
310 (can_type << CAN_FRAME_TYPE_SHIFT) +
313 }
314
319 static Defs::MTI nmranet_mti(uint32_t can_id);
320
326 static uint32_t can_identifier(Defs::MTI mti, NodeAlias src);
327
334 static ControlField get_control_field(uint32_t can_id)
335 {
337 }
338
339#if 0
344 static NodeAlias get_control_src(uint32_t can_id)
345 {
346 return (can_id & CONTROL_SRC_MASK) >> CONTROL_SRC_SHIFT;
347 }
348
353 static unsigned int get_control_sequence(uint32_t can_id)
354 {
356 }
357
358#endif
359
365 static uint32_t set_control_fields(
366 NodeAlias src, uint16_t field, int sequence)
367 {
368 return (src << CONTROL_SRC_SHIFT) | (field << CONTROL_FIELD_SHIFT) |
369 (sequence << CONTROL_SEQUENCE_SHIFT) | ((0) << CONTROL_TYPE_SHIFT) |
370 ((1) << CONTROL_PRIORITY_SHIFT);
371 }
372
379 static void control_init(struct can_frame &frame, NodeAlias src, uint16_t field, int sequence)
380 {
381 SET_CAN_FRAME_ID_EFF(frame, set_control_fields(src, field, sequence));
382 frame.can_dlc = 0;
383 }
384
390 {
391 return RESERVED_ALIAS_NODE_BITS | alias;
392 }
393
401
402private:
405};
406
407} // namespace openlcb
408
409#endif // _OPENLCB_CANDEFS_HXX_
uint64_t NodeID
48-bit NMRAnet Node ID type
uint16_t NodeAlias
Alias to a 48-bit NMRAnet Node ID type.
Static values and helper functions for CAN-bus interfaces.
Definition CanDefs.hxx:47
static bool is_cid_frame(uint32_t can_id)
Tests if the incoming frame is a CID frame.
Definition CanDefs.hxx:208
static CanFrameType get_can_frame_type(uint32_t can_id)
Get the CAN frame type field value of the CAN ID.
Definition CanDefs.hxx:181
static void set_mti(uint32_t *can_id, CanMTI mti)
Set the MTI field value of the CAN ID.
Definition CanDefs.hxx:226
static uint32_t can_identifier(Defs::MTI mti, NodeAlias src)
Get the CAN identifier from an NMRAnet mti and source alias.
Definition CanDefs.cxx:69
static NodeID get_reserved_alias_node_id(NodeAlias alias)
Computes a reserved alias node ID for the local alias cache map.
Definition CanDefs.hxx:389
FrameType
Frame Types, Control or normal NMRAnet message.
Definition CanDefs.hxx:110
@ NMRANET_MSG
normal NMRAnet message
Definition CanDefs.hxx:112
@ CONTROL_MSG
CAN control frame message.
Definition CanDefs.hxx:111
CanDefs()
This class should not be instantiated.
static void set_dst(uint32_t *can_id, NodeAlias dst)
Set the destination field value of the CAN ID.
Definition CanDefs.hxx:246
static ControlField get_control_field(uint32_t can_id)
Get the control field of a can control frame.
Definition CanDefs.hxx:334
static void set_frame_type(uint32_t *can_id, FrameType type)
Set the frame type field value of the CAN ID.
Definition CanDefs.hxx:266
static void control_init(struct can_frame &frame, NodeAlias src, uint16_t field, int sequence)
Initialize a control frame CAN ID and set DLC to 0.
Definition CanDefs.hxx:379
static NodeAlias get_dst(uint32_t can_id)
Get the destination field value of the CAN ID.
Definition CanDefs.hxx:172
static FrameType get_frame_type(uint32_t can_id)
Get the frame type field value of the CAN ID.
Definition CanDefs.hxx:190
static uint32_t set_control_fields(NodeAlias src, uint16_t field, int sequence)
Initialize a control frame CAN ID and set DLC to 0.
Definition CanDefs.hxx:365
static CanMTI get_mti(uint32_t can_id)
Get the MTI field value of the CAN ID.
Definition CanDefs.hxx:163
static void set_datagram_fields(uint32_t *can_id, NodeAlias src, NodeAlias dst, CanFrameType can_type)
Set all the CAN ID fields for datagram or stream message.
Definition CanDefs.hxx:305
AliasStatus
Status value for an alias pool item.
Definition CanDefs.hxx:51
@ RESERVED
the alias has been reserved for use
Definition CanDefs.hxx:53
@ CONFLICT
we discovered someone else already is using this alias
Definition CanDefs.hxx:54
@ FREE
the alias is free for another request
Definition CanDefs.hxx:55
@ UNDER_TEST
this is an alias we are trying to claim
Definition CanDefs.hxx:52
@ AMD_FRAME
Alias Map Definition frame.
Definition CanDefs.hxx:127
@ AME_FRAME
Alias Mapping Enquiry.
Definition CanDefs.hxx:128
@ RID_FRAME
Reserve ID Frame.
Definition CanDefs.hxx:126
@ AMR_FRAME
Alias Map Reset.
Definition CanDefs.hxx:129
ReservedAliasNodeId
Constants used in the LocalAliasCache for reserved but not used aliases.
Definition CanDefs.hxx:141
@ RESERVED_ALIAS_NODE_MASK
Mask for the reserved aliases.
Definition CanDefs.hxx:147
@ RESERVED_ALIAS_NODE_BITS
To mark a reserved alias in the local alias cache, we use this as a node ID and add the alias to the ...
Definition CanDefs.hxx:145
static void set_src(uint32_t *can_id, NodeAlias src)
Set the source field value of the CAN ID.
Definition CanDefs.hxx:236
static Priority get_priority(uint32_t can_id)
Get the priority field value of the CAN ID.
Definition CanDefs.hxx:199
static void set_can_frame_type(uint32_t *can_id, CanFrameType type)
Set the CAN frame type field value of the CAN ID.
Definition CanDefs.hxx:256
static bool is_reserved_alias_node_id(NodeID id)
Tests if a node ID is a reserved alias Node ID.
Definition CanDefs.hxx:397
CanFrameType
CAN Frame Types.
Definition CanDefs.hxx:99
@ DATAGRAM_MIDDLE_FRAME
middle frame of multi-frame datagram
Definition CanDefs.hxx:103
@ DATAGRAM_FINAL_FRAME
last frame of multi-frame datagram
Definition CanDefs.hxx:104
@ DATAGRAM_FIRST_FRAME
first frame of multi-frame datagram
Definition CanDefs.hxx:102
@ GLOBAL_ADDRESSED
most CAN frame types fall in this category
Definition CanDefs.hxx:100
@ STREAM_DATA
stream data frame
Definition CanDefs.hxx:105
@ DATAGRAM_ONE_FRAME
a single frame datagram
Definition CanDefs.hxx:101
Priority
Highest order priority of a CAN message.
Definition CanDefs.hxx:119
@ HIGH_PRIORITY
high priority CAN message
Definition CanDefs.hxx:120
@ NORMAL_PRIORITY
normal priority CAN message
Definition CanDefs.hxx:121
static NodeAlias get_src(uint32_t can_id)
Get the source field value of the CAN ID.
Definition CanDefs.hxx:154
static Defs::MTI nmranet_mti(uint32_t can_id)
Get the NMRAnet MTI from a can identifier.
Definition CanDefs.cxx:43
static bool is_stream_frame(uint32_t can_id)
Tests if the incoming frame is a stream data send frame.
Definition CanDefs.hxx:217
static void set_priority(uint32_t *can_id, Priority priority)
Set the priority field value of the CAN ID.
Definition CanDefs.hxx:276
ID
CAN ID bit fields for most CAN frames.
Definition CanDefs.hxx:61
@ FRAME_TYPE_MASK
mask for frame type field of CAN ID
Definition CanDefs.hxx:66
@ SRC_MASK
mask for source field of CAN ID
Definition CanDefs.hxx:62
@ FRAME_TYPE_SHIFT
shift for frame type field of CAN ID
Definition CanDefs.hxx:75
@ PADDING_MASK
mask for padding field of CAN ID
Definition CanDefs.hxx:68
@ CONTROL_PRIORITY_MASK
priority mask
Definition CanDefs.hxx:83
@ CONTROL_FIELD_MASK
control field data mask
Definition CanDefs.hxx:80
@ CONTROL_SRC_MASK
source alias mask
Definition CanDefs.hxx:79
@ CONTROL_SEQUENCE_SHIFT
frame sequence number shift
Definition CanDefs.hxx:88
@ MTI_MASK
mask for MTI field of CAN ID
Definition CanDefs.hxx:63
@ CONTROL_FIELD_SHIFT
control field data shift
Definition CanDefs.hxx:87
@ CONTROL_SEQUENCE_MASK
frame sequence number mask
Definition CanDefs.hxx:81
@ CONTROL_SRC_SHIFT
source alias shift
Definition CanDefs.hxx:86
@ CONTROL_PRIORITY_SHIFT
priority shift
Definition CanDefs.hxx:90
@ STREAM_DG_RECV_MASK
mask for receiving datagram and stream frames.
Definition CanDefs.hxx:69
@ CAN_FRAME_TYPE_SHIFT
shift for can frame type field of CAN ID
Definition CanDefs.hxx:74
@ CAN_FRAME_TYPE_MASK
mask for can frame type field of CAN ID
Definition CanDefs.hxx:65
@ CONTROL_TYPE_SHIFT
value of '0' means control frame shift
Definition CanDefs.hxx:89
@ CONTROL_PADDING_MASK
pad out to a full 32-bit word
Definition CanDefs.hxx:84
@ CONTROL_PADDING_SHIFT
pad out to a full 32-bit word
Definition CanDefs.hxx:91
@ PADDING_SHIFT
shift for padding field of CAN ID
Definition CanDefs.hxx:77
@ PRIORITY_SHIFT
shift for priority field of CAN ID
Definition CanDefs.hxx:76
@ DST_MASK
mask for MTI field of CAN ID
Definition CanDefs.hxx:64
@ PRIORITY_MASK
mask for priority field of CAN ID
Definition CanDefs.hxx:67
@ MTI_SHIFT
shift for MTI field of CAN ID
Definition CanDefs.hxx:72
@ DST_SHIFT
shift for MTI field of CAN ID
Definition CanDefs.hxx:73
@ CONTROL_TYPE_MASK
value of '0' means control frame mask
Definition CanDefs.hxx:82
@ SRC_SHIFT
shift for source field of CAN ID
Definition CanDefs.hxx:71
static void set_fields(uint32_t *can_id, NodeAlias src, Defs::MTI mti, CanFrameType can_type, FrameType type, Priority priority)
Set all the CAN ID fields.
Definition CanDefs.hxx:290
MTI
Known Message type indicators.