transfer_buffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_TRANSPORT_TRANSFER_BUFFER_HPP_INCLUDED
6 #define UAVCAN_TRANSPORT_TRANSFER_BUFFER_HPP_INCLUDED
7 
8 #include <uavcan/std.hpp>
9 #include <uavcan/error.hpp>
14 #include <uavcan/build_config.hpp>
15 #include <uavcan/debug.hpp>
16 
17 namespace uavcan
18 {
23 {
24  uint8_t* const data_;
25  const uint16_t size_;
27 
28 public:
30  data_(buf),
31  size_(buf_size),
32  max_write_pos_(0)
33  { }
34 
35  virtual int read(unsigned offset, uint8_t* data, unsigned len) const;
36  virtual int write(unsigned offset, const uint8_t* data, unsigned len);
37 
38  void reset();
39 
40  uint16_t getSize() const { return size_; }
41 
42  uint8_t* getRawPtr() { return data_; }
43  const uint8_t* getRawPtr() const { return data_; }
44 
46  void setMaxWritePos(uint16_t value) { max_write_pos_ = value; }
47 };
48 
49 template <uint16_t Size>
51 {
52  uint8_t buffer_[Size];
53 public:
55  {
56  StaticAssert<(Size > 0)>::check();
57  }
58 };
59 
64 {
67 
68 public:
70  : transfer_type_(TransferType(0))
71  {
72  UAVCAN_ASSERT(isEmpty());
73  }
74 
76  : node_id_(node_id)
77  , transfer_type_(ttype)
78  {
79  UAVCAN_ASSERT(!isEmpty());
80  }
81 
82  bool operator==(const TransferBufferManagerKey& rhs) const
83  {
84  return node_id_ == rhs.node_id_ && transfer_type_ == rhs.transfer_type_;
85  }
86 
87  bool isEmpty() const { return !node_id_.isValid(); }
88 
89  NodeID getNodeID() const { return node_id_; }
90  TransferType getTransferType() const { return TransferType(transfer_type_); }
91 
92 #if UAVCAN_TOSTRING
93  std::string toString() const;
94 #endif
95 };
96 
103  , public LinkedListNode<TransferBufferManagerEntry>
104 {
105  struct Block : LinkedListNode<Block>
106  {
107  enum { Size = MemPoolBlockSize - sizeof(LinkedListNode<Block>) };
108  uint8_t data[static_cast<unsigned>(Size)];
109 
110  static Block* instantiate(IPoolAllocator& allocator);
111  static void destroy(Block*& obj, IPoolAllocator& allocator);
112 
113  void read(uint8_t*& outptr, unsigned target_offset,
114  unsigned& total_offset, unsigned& left_to_read);
115  void write(const uint8_t*& inptr, unsigned target_offset,
116  unsigned& total_offset, unsigned& left_to_write);
117  };
118 
120  LinkedListRoot<Block> blocks_; // Blocks are ordered from lower to higher buffer offset
124 
125 public:
127  allocator_(allocator),
128  max_write_pos_(0),
129  max_size_(max_size)
130  {
131  StaticAssert<(Block::Size > 8)>::check();
134  }
135 
137 
138  static TransferBufferManagerEntry* instantiate(IPoolAllocator& allocator, uint16_t max_size);
139  static void destroy(TransferBufferManagerEntry*& obj, IPoolAllocator& allocator);
140 
141  virtual int read(unsigned offset, uint8_t* data, unsigned len) const;
142  virtual int write(unsigned offset, const uint8_t* data, unsigned len);
143 
145 
146  const TransferBufferManagerKey& getKey() const { return key_; }
147  bool isEmpty() const { return key_.isEmpty(); }
148 };
149 
154 {
158 
160 
161 public:
162  TransferBufferManager(uint16_t max_buf_size, IPoolAllocator& allocator) :
163  allocator_(allocator),
164  max_buf_size_(max_buf_size)
165  { }
166 
168 
169  ITransferBuffer* access(const TransferBufferManagerKey& key);
170  ITransferBuffer* create(const TransferBufferManagerKey& key);
171  void remove(const TransferBufferManagerKey& key);
172  bool isEmpty() const;
173 
174  unsigned getNumBuffers() const;
175 };
176 
181 {
184 
185 public:
187  bufmgr_(bufmgr),
188  key_(key)
189  {
190  UAVCAN_ASSERT(!key.isEmpty());
191  }
192  ITransferBuffer* access() { return bufmgr_.access(key_); }
193  ITransferBuffer* create() { return bufmgr_.create(key_); }
194  void remove() { bufmgr_.remove(key_); }
195 };
196 
197 }
198 
199 #endif // UAVCAN_TRANSPORT_TRANSFER_BUFFER_HPP_INCLUDED
std::uint8_t uint8_t
Definition: std.hpp:24
void setMaxWritePos(uint16_t value)
ROSCPP_DECL bool check()
virtual int write(unsigned offset, const uint8_t *data, unsigned len)
ITransferBuffer * create(const TransferBufferManagerKey &key)
static const unsigned MemPoolBlockSize
Safe default that should be OK for any platform.
StaticTransferBufferImpl(uint8_t *buf, uint16_t buf_size)
const TransferBufferManagerKey & getKey() const
const TransferBufferManagerKey key_
virtual int read(unsigned offset, uint8_t *data, unsigned len) const
TransferBufferAccessor(TransferBufferManager &bufmgr, TransferBufferManagerKey key)
TransferBufferManagerEntry(IPoolAllocator &allocator, uint16_t max_size)
const uint8_t * getRawPtr() const
static std::string toString(long x)
Definition: multiset.cpp:16
TransferType getTransferType() const
bool operator==(const TransferBufferManagerKey &rhs) const
TransferBufferManager(uint16_t max_buf_size, IPoolAllocator &allocator)
bool isValid() const
Definition: transfer.hpp:134
TransferType
Definition: transfer.hpp:18
LinkedListRoot< TransferBufferManagerEntry > buffers_
TransferBufferManager & bufmgr_
std::uint16_t uint16_t
Definition: std.hpp:25
void remove(const TransferBufferManagerKey &key)
ITransferBuffer * access(const TransferBufferManagerKey &key)
TransferBufferManagerKey(NodeID node_id, TransferType ttype)


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