can_io.hpp
Go to the documentation of this file.
1 /*
2  * CAN bus IO logic.
3  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
4  * Copyright (C) 2019 Theodoros Ntakouris <zarkopafilis@gmail.com>
5  */
6 
7 #ifndef UAVCAN_TRANSPORT_CAN_IO_HPP_INCLUDED
8 #define UAVCAN_TRANSPORT_CAN_IO_HPP_INCLUDED
9 
10 #include <cassert>
11 #include <uavcan/error.hpp>
12 #include <uavcan/std.hpp>
14 #include <uavcan/util/avl_tree.hpp>
16 #include <uavcan/build_config.hpp>
19 #include <uavcan/driver/can.hpp>
21 #include <uavcan/time.hpp>
22 
23 namespace uavcan
24 {
26 {
30 
32  : iface_index(0)
33  { }
34 
35 #if UAVCAN_TOSTRING
36  std::string toString(StringRepresentation mode = StrTight) const;
37 #endif
38 };
39 
40 struct UAVCAN_EXPORT CanTxQueueEntry // Not required to be packed - fits the block in any case
41 {
43  const CanFrame frame;
45 
46  CanTxQueueEntry(const CanFrame& arg_frame, const MonotonicTime arg_deadline, CanIOFlags arg_flags)
47  : deadline(arg_deadline)
48  , frame(arg_frame)
49  , flags(arg_flags)
50  {
52  }
53 
54  static void destroy(CanTxQueueEntry*& obj, IPoolAllocator& allocator);
55 
56  bool isExpired(const MonotonicTime timestamp) const { return timestamp > deadline; }
57 
58  bool operator<(const CanTxQueueEntry& other) const
59  {
60  return this->frame.priorityLowerThan(other.frame);
61  }
62 
63  bool operator>(const CanTxQueueEntry& other) const
64  {
65  return this->frame.priorityHigherThan(other.frame);
66  }
67 
68  bool operator==(const CanTxQueueEntry& other) const
69  {
70  return this->frame == other.frame;
71  }
72 
73 #if UAVCAN_TOSTRING
74  std::string toString() const;
75 #endif
76 };
77 
78 class UAVCAN_EXPORT CanTxQueue : public AvlTree<CanTxQueueEntry>
79 {
80 private:
81  void postOrderTraverseEntryCleanup(Node* n);
82 
83 protected:
86 
87  bool linkedListContains(Node* head, const CanFrame& frame) const;
88  void safeIncrementRejectedFrames();
89  AvlTree::Node* searchForNonExpiredMax(Node* n);
90 
91 public:
92  CanTxQueue(IPoolAllocator& allocator, ISystemClock& sysclock, std::size_t allocator_quota) :
93  AvlTree(allocator, allocator_quota),
94  sysclock_(sysclock),
95  rejected_frames_cnt_(0)
96  {}
97 
98  ~CanTxQueue() override;
99 
100  /* Avl Tree allocates the AvlTree::Node, while this(CanTxQueue) allocates the CanTxQueueEntry
101  * Same logic for removal. */
102  void push(const CanFrame& frame, MonotonicTime tx_deadline, CanIOFlags flags);
103  void remove(CanTxQueueEntry* entry);
104 
105  uint32_t getRejectedFrameCount() const { return rejected_frames_cnt_; }
106 
107  bool contains(const CanFrame& frame) const;
108 
109  /* Tries to look up rightmost Node. If the frame is expired, removes it and continues traversing */
110  CanTxQueueEntry* peek();
111 
112  bool topPriorityHigherOrEqual(const CanFrame& rhs_frame);
113 };
114 
116 {
120 
122  : frames_tx(0)
123  , frames_rx(0)
124  , errors(0)
125  { }
126 };
127 
129 {
131  {
134 
136  : frames_tx(0)
137  , frames_rx(0)
138  { }
139  };
140 
143 
146 
148 
149  int sendToIface(uint8_t iface_index, const CanFrame& frame, MonotonicTime tx_deadline, CanIOFlags flags);
150  int sendFromTxQueue(uint8_t iface_index);
151  int callSelect(CanSelectMasks& inout_masks, const CanFrame* (& pending_tx)[MaxCanIfaces],
152  MonotonicTime blocking_deadline);
153 
154 public:
155  CanIOManager(ICanDriver& driver, IPoolAllocator& allocator, ISystemClock& sysclock,
156  std::size_t mem_blocks_per_iface = 0);
157 
158  uint8_t getNumIfaces() const { return num_ifaces_; }
159 
160  CanIfacePerfCounters getIfacePerfCounters(uint8_t iface_index) const;
161 
162  const ICanDriver& getCanDriver() const { return driver_; }
163  ICanDriver& getCanDriver() { return driver_; }
164 
165  uint8_t makePendingTxMask() const;
166 
173  int send(const CanFrame& frame, MonotonicTime tx_deadline, MonotonicTime blocking_deadline,
174  uint8_t iface_mask, CanIOFlags flags);
175  int receive(CanRxFrame& out_frame, MonotonicTime blocking_deadline, CanIOFlags& out_flags);
176 };
177 
178 }
179 
180 #endif // UAVCAN_TRANSPORT_CAN_IO_HPP_INCLUDED
uavcan::CanIfacePerfCounters
Definition: can_io.hpp:115
can.hpp
uavcan::CanTxQueueEntry::operator>
bool operator>(const CanTxQueueEntry &other) const
Definition: can_io.hpp:63
uavcan::CanIfacePerfCounters::frames_tx
uint64_t frames_tx
Definition: can_io.hpp:117
uavcan::Noncopyable
Definition: templates.hpp:46
uavcan::uint64_t
std::uint64_t uint64_t
Definition: std.hpp:27
templates.hpp
uavcan::AvlTree::Node
Definition: avl_tree.hpp:25
std::size_t
unsigned long size_t
Definition: coverity_scan_model.cpp:19
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
uavcan::CanIOManager::IfaceFrameCounters
Definition: can_io.hpp:130
uavcan::UtcTime
Implicitly convertible to/from uavcan.Timestamp.
Definition: time.hpp:191
uavcan::CanIfacePerfCounters::CanIfacePerfCounters
CanIfacePerfCounters()
Definition: can_io.hpp:121
dynamic_memory.hpp
system_clock.hpp
uavcan::CanTxQueueEntry::deadline
MonotonicTime deadline
Definition: can_io.hpp:42
uavcan::ICanDriver
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:207
uavcan::CanRxFrame::CanRxFrame
CanRxFrame()
Definition: can_io.hpp:31
uavcan::CanTxQueueEntry::frame
const CanFrame frame
Definition: can_io.hpp:43
uavcan::CanTxQueueEntry::operator<
bool operator<(const CanTxQueueEntry &other) const
Definition: can_io.hpp:58
uavcan::CanFrame
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:24
uavcan::CanTxQueue::rejected_frames_cnt_
uint32_t rejected_frames_cnt_
Definition: can_io.hpp:85
uavcan::CanRxFrame
Definition: can_io.hpp:25
std.hpp
uavcan::MaxCanIfaces
@ MaxCanIfaces
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:19
uavcan::CanTxQueue::sysclock_
ISystemClock & sysclock_
Definition: can_io.hpp:84
uavcan::CanIOManager::driver_
ICanDriver & driver_
Definition: can_io.hpp:141
uavcan::CanTxQueue::CanTxQueue
CanTxQueue(IPoolAllocator &allocator, ISystemClock &sysclock, std::size_t allocator_quota)
Definition: can_io.hpp:92
uavcan::CanRxFrame::ts_utc
UtcTime ts_utc
Definition: can_io.hpp:28
uavcan::IsDynamicallyAllocatable::check
static void check()
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:246
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::CanFrame::priorityLowerThan
bool priorityLowerThan(const CanFrame &rhs) const
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:91
uavcan::CanIOManager::getNumIfaces
uint8_t getNumIfaces() const
Definition: can_io.hpp:158
uavcan::CanIOManager
Definition: can_io.hpp:128
uavcan::CanIfacePerfCounters::frames_rx
uint64_t frames_rx
Definition: can_io.hpp:118
lazy_constructor.hpp
uavcan::IPoolAllocator
Definition: dynamic_memory.hpp:21
uavcan::CanSelectMasks
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:119
toString
static std::string toString(long x)
Definition: multiset.cpp:16
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::CanTxQueueEntry::isExpired
bool isExpired(const MonotonicTime timestamp) const
Definition: can_io.hpp:56
uavcan::CanIOManager::IfaceFrameCounters::frames_tx
uint64_t frames_tx
Definition: can_io.hpp:132
uavcan::CanRxFrame::iface_index
uint8_t iface_index
Definition: can_io.hpp:29
build_config.hpp
frame
uavcan::CanFrame frame
Definition: can.cpp:78
uavcan::CanTxQueueEntry
Definition: can_io.hpp:40
linked_list.hpp
uavcan::AvlTree
Definition: avl_tree.hpp:22
uavcan::CanTxQueueEntry::CanTxQueueEntry
CanTxQueueEntry(const CanFrame &arg_frame, const MonotonicTime arg_deadline, CanIOFlags arg_flags)
Definition: can_io.hpp:46
uavcan::CanIOManager::getCanDriver
ICanDriver & getCanDriver()
Definition: can_io.hpp:163
uavcan::CanIOManager::num_ifaces_
const uint8_t num_ifaces_
Definition: can_io.hpp:147
uavcan::CanTxQueue::getRejectedFrameCount
uint32_t getRejectedFrameCount() const
Definition: can_io.hpp:105
uavcan::CanIfacePerfCounters::errors
uint64_t errors
Definition: can_io.hpp:119
uavcan::CanIOManager::getCanDriver
const ICanDriver & getCanDriver() const
Definition: can_io.hpp:162
uavcan::CanIOManager::IfaceFrameCounters::frames_rx
uint64_t frames_rx
Definition: can_io.hpp:133
uavcan::CanIOFlags
uint16_t CanIOFlags
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:140
uavcan::CanTxQueueEntry::flags
CanIOFlags flags
Definition: can_io.hpp:44
uavcan::CanTxQueue
Definition: can_io.hpp:78
uavcan::MonotonicTime
Definition: time.hpp:184
time.hpp
uavcan::CanIOManager::sysclock_
ISystemClock & sysclock_
Definition: can_io.hpp:142
uavcan::CanTxQueueEntry::operator==
bool operator==(const CanTxQueueEntry &other) const
Definition: can_io.hpp:68
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::ISystemClock
Definition: system_clock.hpp:19
uavcan::CanIOManager::IfaceFrameCounters::IfaceFrameCounters
IfaceFrameCounters()
Definition: can_io.hpp:135
uavcan::CanRxFrame::ts_mono
MonotonicTime ts_mono
Definition: can_io.hpp:27
uavcan::LazyConstructor
Definition: lazy_constructor.hpp:27
uavcan::Node
Definition: node.hpp:38
avl_tree.hpp
error.hpp


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