F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <string.h>
18 
19 namespace Os {
20 
22  // Class functions:
24 
26  // Set member variables:
27  this->queue = NULL;
28  this->msgSize = 0;
29  this->depth = 0;
30  this->count = 0;
31  this->maxCount = 0;
32  }
33 
35  this->finalize();
36  }
37 
39  // Queue is already set up. destroy it and try again:
40  if (NULL != this->queue) {
41  this->finalize();
42  }
43  FW_ASSERT(NULL == this->queue, (POINTER_CAST) this->queue);
44 
45  // Set member variables:
46  this->msgSize = msgSize;
47  this->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->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->count;
66  if( this->count > this->maxCount ) {
67  this->maxCount = this->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->count;
87 
88  return true;
89  }
90 
92  return (this->count == this->depth);
93  }
94 
96  return (this->count == 0);
97  }
98 
100  return this->count;
101  }
102 
104  return this->maxCount;
105  }
106 
107 
109  return this->msgSize;
110  }
111 
113  return this->depth;
114  }
115 
116  NATIVE_UINT_TYPE BufferQueue::getBufferIndex(NATIVE_INT_TYPE index) {
117  return (index % this->depth) * (sizeof(NATIVE_INT_TYPE) + this->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 += 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 messsage:
143  if(storedSize > size){
144  size = storedSize;
145  return false;
146  }
147  size = storedSize;
148 
149  // Copy buffer from queue:
150  index += sizeof(size);
151  source = &data[index];
152  ptr = memcpy(buffer, source, storedSize);
153  FW_ASSERT(ptr == buffer);
154  return true;
155  }
156 }
Os
Definition: File.cpp:7
Os::BufferQueue::getDepth
NATIVE_UINT_TYPE getDepth()
Get the queue depths.
Definition: BufferQueueCommon.cpp:112
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Os::BufferQueue::getCount
NATIVE_UINT_TYPE getCount()
Get the current number of items on the queue.
Definition: BufferQueueCommon.cpp:99
Os::BufferQueue::~BufferQueue
~BufferQueue()
BufferQueue deconstructor.
Definition: BufferQueueCommon.cpp:34
Os::BufferQueue::push
bool push(const U8 *buffer, NATIVE_UINT_TYPE size, NATIVE_INT_TYPE priority)
push an item onto the queue
Definition: BufferQueueCommon.cpp:51
Os::BufferQueue::BufferQueue
BufferQueue()
BufferQueue constructor.
Definition: BufferQueueCommon.cpp:25
Assert.hpp
Os::BufferQueue::getMaxCount
NATIVE_UINT_TYPE getMaxCount()
Get the maximum number of items seen on the queue.
Definition: BufferQueueCommon.cpp:103
BufferQueue.hpp
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Os::BufferQueue::isEmpty
bool isEmpty()
check if the queue is empty
Definition: BufferQueueCommon.cpp:95
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Os::BufferQueue::isFull
bool isFull()
check if the queue is full
Definition: BufferQueueCommon.cpp:91
Os::BufferQueue::getMsgSize
NATIVE_UINT_TYPE getMsgSize()
Get the maximum message size.
Definition: BufferQueueCommon.cpp:108
Os::BufferQueue::create
bool create(NATIVE_UINT_TYPE depth, NATIVE_UINT_TYPE msgSize)
BufferQueue creation.
Definition: BufferQueueCommon.cpp:38
Os::BufferQueue::pop
bool pop(U8 *buffer, NATIVE_UINT_TYPE &size, NATIVE_INT_TYPE &priority)
pop an item off the queue
Definition: BufferQueueCommon.cpp:72
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100