mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-22 01:40:35 +08:00
StaticTransferBuffer extracted for standalone usage
This commit is contained in:
@@ -142,22 +142,14 @@ public:
|
|||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Statically allocated storage
|
* Standalone static buffer
|
||||||
*/
|
*/
|
||||||
template <unsigned int SIZE>
|
template <unsigned int SIZE>
|
||||||
class StaticTransferBuffer : public TransferBufferManagerEntry
|
class StaticTransferBuffer : public ITransferBuffer
|
||||||
{
|
{
|
||||||
uint8_t data_[SIZE];
|
uint8_t data_[SIZE];
|
||||||
unsigned int max_write_pos_;
|
unsigned int max_write_pos_;
|
||||||
|
|
||||||
void resetImpl()
|
|
||||||
{
|
|
||||||
max_write_pos_ = 0;
|
|
||||||
#if UAVCAN_DEBUG
|
|
||||||
std::fill(data_, data_ + SIZE, 0);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StaticTransferBuffer()
|
StaticTransferBuffer()
|
||||||
: max_write_pos_(0)
|
: max_write_pos_(0)
|
||||||
@@ -199,6 +191,42 @@ public:
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
max_write_pos_ = 0;
|
||||||
|
#if UAVCAN_DEBUG
|
||||||
|
std::fill(data_, data_ + SIZE, 0);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* getRawPtr() { return data_; }
|
||||||
|
void setMaxWritePos(unsigned int value) { max_write_pos_ = value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Statically allocated storage for buffer manager
|
||||||
|
*/
|
||||||
|
template <unsigned int SIZE>
|
||||||
|
class StaticTransferBufferManagerEntry : public TransferBufferManagerEntry
|
||||||
|
{
|
||||||
|
StaticTransferBuffer<SIZE> buf_;
|
||||||
|
|
||||||
|
void resetImpl()
|
||||||
|
{
|
||||||
|
buf_.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
int read(unsigned int offset, uint8_t* data, unsigned int len) const
|
||||||
|
{
|
||||||
|
return buf_.read(offset, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int write(unsigned int offset, const uint8_t* data, unsigned int len)
|
||||||
|
{
|
||||||
|
return buf_.write(offset, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
bool migrateFrom(const TransferBufferManagerEntry* tbme)
|
bool migrateFrom(const TransferBufferManagerEntry* tbme)
|
||||||
{
|
{
|
||||||
if (tbme == NULL || tbme->isEmpty())
|
if (tbme == NULL || tbme->isEmpty())
|
||||||
@@ -208,14 +236,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Resetting self and moving all data from the source
|
// Resetting self and moving all data from the source
|
||||||
reset(tbme->getKey());
|
TransferBufferManagerEntry::reset(tbme->getKey());
|
||||||
const int res = tbme->read(0, data_, SIZE);
|
const int res = tbme->read(0, buf_.getRawPtr(), SIZE);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
{
|
{
|
||||||
reset();
|
TransferBufferManagerEntry::reset();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
max_write_pos_ = res;
|
buf_.setMaxWritePos(res);
|
||||||
if (res < int(SIZE))
|
if (res < int(SIZE))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -223,7 +251,7 @@ public:
|
|||||||
uint8_t dummy = 0;
|
uint8_t dummy = 0;
|
||||||
if (tbme->read(SIZE, &dummy, 1) > 0)
|
if (tbme->read(SIZE, &dummy, 1) > 0)
|
||||||
{
|
{
|
||||||
reset(); // Damn, the buffer was too large
|
TransferBufferManagerEntry::reset(); // Damn, the buffer was too large
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -268,7 +296,7 @@ public:
|
|||||||
template <unsigned int MAX_BUF_SIZE, unsigned int NUM_STATIC_BUFS>
|
template <unsigned int MAX_BUF_SIZE, unsigned int NUM_STATIC_BUFS>
|
||||||
class TransferBufferManager : public ITransferBufferManager, Noncopyable
|
class TransferBufferManager : public ITransferBufferManager, Noncopyable
|
||||||
{
|
{
|
||||||
typedef StaticTransferBuffer<MAX_BUF_SIZE> StaticBufferType;
|
typedef StaticTransferBufferManagerEntry<MAX_BUF_SIZE> StaticBufferType;
|
||||||
|
|
||||||
StaticBufferType static_buffers_[NUM_STATIC_BUFS];
|
StaticBufferType static_buffers_[NUM_STATIC_BUFS];
|
||||||
LinkedListRoot<DynamicTransferBuffer> dynamic_buffers_;
|
LinkedListRoot<DynamicTransferBuffer> dynamic_buffers_;
|
||||||
|
|||||||
@@ -85,8 +85,8 @@ static const int TEST_BUFFER_SIZE = 200;
|
|||||||
|
|
||||||
TEST(StaticTransferBuffer, Basic)
|
TEST(StaticTransferBuffer, Basic)
|
||||||
{
|
{
|
||||||
using uavcan::StaticTransferBuffer;
|
using uavcan::StaticTransferBufferManagerEntry;
|
||||||
StaticTransferBuffer<TEST_BUFFER_SIZE> buf;
|
StaticTransferBufferManagerEntry<TEST_BUFFER_SIZE> buf;
|
||||||
|
|
||||||
uint8_t local_buffer[TEST_BUFFER_SIZE * 2];
|
uint8_t local_buffer[TEST_BUFFER_SIZE * 2];
|
||||||
const uint8_t* const test_data_ptr = reinterpret_cast<const uint8_t*>(TEST_DATA.c_str());
|
const uint8_t* const test_data_ptr = reinterpret_cast<const uint8_t*>(TEST_DATA.c_str());
|
||||||
|
|||||||
Reference in New Issue
Block a user