F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
BufferQueueCommon.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title BufferQueueCommon.hpp
3 // \author dinkel
4 // \brief This file implements some of the methods for the generic
5 // buffer queue data structure declared in BufferQueue.hpp that
6 // are common amongst different queue implementations.
7 //
8 // \copyright
9 // Copyright 2009-2015, by the California Institute of Technology.
10 // ALL RIGHTS RESERVED. United States Government Sponsorship
11 // acknowledged.
12 //
13 // ======================================================================
14 
16 #include <Fw/Types/Assert.hpp>
17 #include <cstring>
18 
19 namespace Os {
20 
22  // Class functions:
24 
26  // Set member variables:
27  this->m_queue = nullptr;
28  this->m_msgSize = 0;
29  this->m_depth = 0;
30  this->m_count = 0;
31  this->m_maxCount = 0;
32  }
33 
35  this->finalize();
36  }
37 
39  // Queue is already set up. destroy it and try again:
40  if (nullptr != this->m_queue) {
41  this->finalize();
42  }
43  FW_ASSERT(nullptr == this->m_queue, static_cast<FwAssertArgType>(reinterpret_cast<POINTER_CAST>(this->m_queue)));
44 
45  // Set member variables:
46  this->m_msgSize = msgSize;
47  this->m_depth = depth;
48  return this->initialize(depth, msgSize);
49  }
50 
51  bool BufferQueue::push(const U8* buffer, NATIVE_UINT_TYPE size, NATIVE_INT_TYPE priority) {
52 
53  FW_ASSERT(size <= this->m_msgSize);
54  if( this->isFull() ) {
55  return false;
56  }
57 
58  // Enqueue the data:
59  bool ret = enqueue(buffer, size, priority);
60  if( !ret ) {
61  return false;
62  }
63 
64  // Increment count:
65  ++this->m_count;
66  if( this->m_count > this->m_maxCount ) {
67  this->m_maxCount = this->m_count;
68  }
69  return true;
70  }
71 
72  bool BufferQueue::pop(U8* buffer, NATIVE_UINT_TYPE& size, NATIVE_INT_TYPE &priority) {
73 
74  if( this->isEmpty() ) {
75  size = 0;
76  return false;
77  }
78 
79  // Dequeue the data:
80  bool ret = dequeue(buffer, size, priority);
81  if( !ret ) {
82  return false;
83  }
84 
85  // Decrement count:
86  --this->m_count;
87 
88  return true;
89  }
90 
92  return (this->m_count == this->m_depth);
93  }
94 
96  return (this->m_count == 0);
97  }
98 
100  return this->m_count;
101  }
102 
104  return this->m_maxCount;
105  }
106 
107 
109  return this->m_msgSize;
110  }
111 
113  return this->m_depth;
114  }
115 
116  NATIVE_UINT_TYPE BufferQueue::getBufferIndex(NATIVE_INT_TYPE index) {
117  return static_cast<NATIVE_UINT_TYPE>((static_cast<NATIVE_UINT_TYPE>(index) % this->m_depth) * (sizeof(NATIVE_INT_TYPE) + this->m_msgSize));
118  }
119 
120  void BufferQueue::enqueueBuffer(const U8* buffer, NATIVE_UINT_TYPE size, U8* data, NATIVE_UINT_TYPE index) {
121  // Copy size of buffer onto queue:
122  void* dest = &data[index];
123  void* ptr = memcpy(dest, &size, sizeof(size));
124  FW_ASSERT(ptr == dest);
125 
126  // Copy buffer onto queue:
127  index += static_cast<NATIVE_UINT_TYPE>(sizeof(size));
128  dest = &data[index];
129  ptr = memcpy(dest, buffer, size);
130  FW_ASSERT(ptr == dest);
131  }
132 
133  bool BufferQueue::dequeueBuffer(U8* buffer, NATIVE_UINT_TYPE& size, U8* data, NATIVE_UINT_TYPE index) {
134  // Copy size of buffer from queue:
135  NATIVE_UINT_TYPE storedSize;
136  void* source = &data[index];
137  void* ptr = memcpy(&storedSize, source, sizeof(size));
138  FW_ASSERT(ptr == &storedSize);
139 
140  // If the buffer passed in is not big
141  // enough, return false, and pass out
142  // the size of the message:
143  if(storedSize > size){
144  size = storedSize;
145  return false;
146  }
147  size = storedSize;
148 
149  // Copy buffer from queue:
150  index += static_cast<NATIVE_UINT_TYPE>(sizeof(size));
151  source = &data[index];
152  ptr = memcpy(buffer, source, storedSize);
153  FW_ASSERT(ptr == buffer);
154  return true;
155  }
156 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
bool create(NATIVE_UINT_TYPE depth, NATIVE_UINT_TYPE msgSize)
BufferQueue creation.
NATIVE_UINT_TYPE getCount()
Get the current number of items on the queue.
NATIVE_UINT_TYPE getMsgSize()
Get the maximum message size.
bool pop(U8 *buffer, NATIVE_UINT_TYPE &size, NATIVE_INT_TYPE &priority)
pop an item off the queue
NATIVE_UINT_TYPE getDepth()
Get the queue depths.
bool isEmpty()
check if the queue is empty
~BufferQueue()
BufferQueue deconstructor.
bool push(const U8 *buffer, NATIVE_UINT_TYPE size, NATIVE_INT_TYPE priority)
push an item onto the queue
BufferQueue()
BufferQueue constructor.
bool isFull()
check if the queue is full
NATIVE_UINT_TYPE getMaxCount()
Get the maximum number of items seen on the queue.