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
std::uint8_t uint8_t
Definition: std.hpp:24
uint8_t getNumIfaces() const
Definition: can_io.hpp:158
uint32_t rejected_frames_cnt_
Definition: can_io.hpp:85
UtcTime ts_utc
Definition: can_io.hpp:28
MonotonicTime ts_mono
Definition: can_io.hpp:27
MonotonicTime deadline
Definition: can_io.hpp:42
const CanFrame frame
Definition: can_io.hpp:43
CanTxQueue(IPoolAllocator &allocator, ISystemClock &sysclock, std::size_t allocator_quota)
Definition: can_io.hpp:92
bool isExpired(const MonotonicTime timestamp) const
Definition: can_io.hpp:56
const uint8_t num_ifaces_
Definition: can_io.hpp:147
uint8_t iface_index
Definition: can_io.hpp:29
const ICanDriver & getCanDriver() const
Definition: can_io.hpp:162
ICanDriver & getCanDriver()
Definition: can_io.hpp:163
unsigned long size_t
ISystemClock & sysclock_
Definition: can_io.hpp:84
static std::string toString(long x)
Definition: multiset.cpp:16
bool operator<(const CanTxQueueEntry &other) const
Definition: can_io.hpp:58
Implicitly convertible to/from uavcan.Timestamp.
Definition: time.hpp:191
ISystemClock & sysclock_
Definition: can_io.hpp:142
bool operator==(const CanTxQueueEntry &other) const
Definition: can_io.hpp:68
std::uint32_t uint32_t
Definition: std.hpp:26
std::uint64_t uint64_t
Definition: std.hpp:27
bool priorityHigherThan(const CanFrame &rhs) const
Definition: uc_can.cpp:19
bool priorityLowerThan(const CanFrame &rhs) const
uavcan::CanFrame frame
Definition: can.cpp:78
CanTxQueueEntry(const CanFrame &arg_frame, const MonotonicTime arg_deadline, CanIOFlags arg_flags)
Definition: can_io.hpp:46
uint32_t getRejectedFrameCount() const
Definition: can_io.hpp:105
bool operator>(const CanTxQueueEntry &other) const
Definition: can_io.hpp:63
ICanDriver & driver_
Definition: can_io.hpp:141


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39