F´ Flight Software - C/C++ Documentation NASA-v1.6.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 <cstring>
18
19namespace Os {
20
22 // Class functions:
24
26 // Set member variables:
27 this->queue = nullptr;
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 (nullptr != this->queue) {
41 this->finalize();
42 }
43 FW_ASSERT(nullptr == this->queue, reinterpret_cast<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 message:
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}
#define FW_ASSERT(...)
Definition Assert.hpp:7
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
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.
Definition File.cpp:6