uc_can.cpp
Go to the documentation of this file.
1 /*
2  * CAN bus driver interface.
3  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
4  */
5 
6 #include <uavcan/driver/can.hpp>
7 #include <cassert>
8 
9 namespace uavcan
10 {
11 
18 
19 bool CanFrame::priorityHigherThan(const CanFrame& rhs) const
20 {
21  const uint32_t clean_id = id & MaskExtID;
22  const uint32_t rhs_clean_id = rhs.id & MaskExtID;
23 
24  /*
25  * STD vs EXT - if 11 most significant bits are the same, EXT loses.
26  */
27  const bool ext = id & FlagEFF;
28  const bool rhs_ext = rhs.id & FlagEFF;
29  if (ext != rhs_ext)
30  {
31  const uint32_t arb11 = ext ? (clean_id >> 18) : clean_id;
32  const uint32_t rhs_arb11 = rhs_ext ? (rhs_clean_id >> 18) : rhs_clean_id;
33  if (arb11 != rhs_arb11)
34  {
35  return arb11 < rhs_arb11;
36  }
37  else
38  {
39  return rhs_ext;
40  }
41  }
42 
43  /*
44  * RTR vs Data frame - if frame identifiers and frame types are the same, RTR loses.
45  */
46  const bool rtr = id & FlagRTR;
47  const bool rhs_rtr = rhs.id & FlagRTR;
48  if (clean_id == rhs_clean_id && rtr != rhs_rtr)
49  {
50  return rhs_rtr;
51  }
52 
53  /*
54  * Plain ID arbitration - greater value loses.
55  */
56  return clean_id < rhs_clean_id;
57 }
58 
59 #if UAVCAN_TOSTRING
60 std::string CanFrame::toString(StringRepresentation mode) const
61 {
62  UAVCAN_ASSERT(mode == StrTight || mode == StrAligned);
63 
64  static const unsigned AsciiColumnOffset = 36U;
65 
66  char buf[50];
67  char* wpos = buf;
68  char* const epos = buf + sizeof(buf);
69  fill(buf, buf + sizeof(buf), '\0');
70 
71  if (id & FlagEFF)
72  {
73  wpos += snprintf(wpos, unsigned(epos - wpos), "0x%08x ", unsigned(id & MaskExtID));
74  }
75  else
76  {
77  const char* const fmt = (mode == StrAligned) ? " 0x%03x " : "0x%03x ";
78  wpos += snprintf(wpos, unsigned(epos - wpos), fmt, unsigned(id & MaskStdID));
79  }
80 
81  if (id & FlagRTR)
82  {
83  wpos += snprintf(wpos, unsigned(epos - wpos), " RTR");
84  }
85  else if (id & FlagERR)
86  {
87  wpos += snprintf(wpos, unsigned(epos - wpos), " ERR");
88  }
89  else
90  {
91  for (int dlen = 0; dlen < dlc; dlen++) // hex bytes
92  {
93  wpos += snprintf(wpos, unsigned(epos - wpos), " %02x", unsigned(data[dlen]));
94  }
95 
96  while ((mode == StrAligned) && (wpos < buf + AsciiColumnOffset)) // alignment
97  {
98  *wpos++ = ' ';
99  }
100 
101  wpos += snprintf(wpos, unsigned(epos - wpos), " \'"); // ascii
102  for (int dlen = 0; dlen < dlc; dlen++)
103  {
104  uint8_t ch = data[dlen];
105  if (ch < 0x20 || ch > 0x7E)
106  {
107  ch = '.';
108  }
109  wpos += snprintf(wpos, unsigned(epos - wpos), "%c", ch);
110  }
111  wpos += snprintf(wpos, unsigned(epos - wpos), "\'");
112  }
113  (void)wpos;
114  return std::string(buf);
115 }
116 #endif
117 
118 }
can.hpp
uavcan::CanFrame::MaskStdID
static const uint32_t MaskStdID
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:26
uavcan::CanFrame::FlagEFF
static const uint32_t FlagEFF
Extended frame format.
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:28
uavcan::CanFrame::FlagERR
static const uint32_t FlagERR
Error frame.
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:30
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
uavcan::CanFrame
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:24
uavcan::CanFrame::dlc
uint8_t dlc
Data Length Code.
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:36
uavcan::CanFrame::MaxDataLen
static const uint8_t MaxDataLen
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:32
uavcan::CanFrame::id
uint32_t id
CAN ID with flags (above)
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:34
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::snprintf
int snprintf(char *out, std::size_t maxlen, const char *format,...)
Definition: std.hpp:73
uavcan::CanFrame::MaskExtID
static const uint32_t MaskExtID
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:27
toString
static std::string toString(long x)
Definition: multiset.cpp:16
uavcan::CanFrame::data
uint8_t data[MaxDataLen]
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:35
uavcan::fill
UAVCAN_EXPORT void fill(ForwardIt first, ForwardIt last, const T &value)
Definition: templates.hpp:254
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::CanFrame::priorityHigherThan
bool priorityHigherThan(const CanFrame &rhs) const
Definition: uc_can.cpp:19
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan::CanFrame::FlagRTR
static const uint32_t FlagRTR
Remote transmission request.
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:29


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03