F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BufferAccumulator.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title BufferAccumulator.cpp
3 // \author bocchino
4 // \brief BufferAccumulator implementation
5 //
6 // \copyright
7 // Copyright (C) 2017 California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include "Fw/Types/BasicTypes.hpp"
15 
16 namespace Svc {
17 
18  // ----------------------------------------------------------------------
19  // Construction, initialization, and destruction
20  // ----------------------------------------------------------------------
21 
23  BufferAccumulator(const char *const compName) :
24  BufferAccumulatorComponentBase(compName),
25  mode(DRAIN),
26  bufferMemory(NULL),
27  bufferQueue(),
28  send(true),
29  numWarnings(0),
30  allocatorId(0)
31  {
32 
33  }
34 
37  const NATIVE_INT_TYPE queueDepth,
38  const NATIVE_INT_TYPE instance
39  )
40  {
41  BufferAccumulatorComponentBase::init(queueDepth, instance);
42  }
43 
46  {
47  }
48 
49  // ----------------------------------------------------------------------
50  // Public methods
51  // ----------------------------------------------------------------------
52 
55  NATIVE_INT_TYPE identifier,
56  Fw::MemAllocator& allocator,
57  NATIVE_UINT_TYPE maxNumBuffers
58  )
59  {
60  this->allocatorId = identifier;
61  bool recoverable; // don't need to recover
62  NATIVE_UINT_TYPE actualSize = sizeof(Fw::Buffer) * maxNumBuffers;
63  this->bufferMemory = static_cast<Fw::Buffer*>(
64  allocator.allocate(identifier, actualSize, recoverable));
65  NATIVE_UINT_TYPE actualBuffers = actualSize/sizeof(Fw::Buffer);
66 
67  bufferQueue.init(this->bufferMemory, actualBuffers);
68  }
69 
72  {
73  allocator.deallocate(this->allocatorId, (void*)this->bufferMemory);
74  }
75 
76  // ----------------------------------------------------------------------
77  // Handler implementations for user-defined typed input ports
78  // ----------------------------------------------------------------------
79 
80  void BufferAccumulator ::
81  bufferSendInFill_handler(
82  const NATIVE_INT_TYPE portNum,
83  Fw::Buffer& buffer
84  )
85  {
86  const bool status = this->bufferQueue.enqueue(buffer);
87  if (status) {
88  if (this->numWarnings > 0) {
89  this->log_ACTIVITY_HI_BA_BufferAccepted();
90  }
91  this->numWarnings = 0;
92  }
93  else {
94  if (this->numWarnings == 0) {
95  this->log_WARNING_HI_BA_QueueFull();
96  }
97  ++numWarnings;
98  }
99  if (this->send) {
100  this->sendStoredBuffer();
101  }
102  }
103 
104  void BufferAccumulator ::
105  bufferSendInReturn_handler(
106  const NATIVE_INT_TYPE portNum,
107  Fw::Buffer& buffer
108  )
109  {
110  this->bufferSendOutReturn_out(0, buffer);
111  this->send = true;
112  this->sendStoredBuffer();
113  }
114 
115  void BufferAccumulator ::
116  pingIn_handler(
117  const NATIVE_INT_TYPE portNum,
118  U32 key
119  )
120  {
121  this->pingOut_out(0, key);
122  }
123 
124  void BufferAccumulator ::
125  schedIn_handler(
126  const NATIVE_INT_TYPE portNum,
127  NATIVE_UINT_TYPE context
128  )
129  {
130  // TODO
131  }
132 
133  // ----------------------------------------------------------------------
134  // Command handler implementations
135  // ----------------------------------------------------------------------
136 
137  void BufferAccumulator ::
138  BA_SetMode_cmdHandler(
139  const FwOpcodeType opCode,
140  const U32 cmdSeq,
141  OpState mode
142  )
143  {
144  this->mode = mode;
145  if (mode == DRAIN) {
146  this->send = true;
147  this->sendStoredBuffer();
148  }
149  else {
150  this->send = false;
151  }
152  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_OK);
153  }
154 
155  // ----------------------------------------------------------------------
156  // Private helper methods
157  // ----------------------------------------------------------------------
158 
159  void BufferAccumulator ::
160  sendStoredBuffer(void)
161  {
162  FW_ASSERT(this->send);
163  Fw::Buffer buffer;
164  const bool status = this->bufferQueue.dequeue(buffer);
165  if (status) {
166  this->bufferSendOutDrain_out(0, buffer);
167  this->send = false;
168  }
169  }
170 
171 }
Fw::MemAllocator::deallocate
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.
Svc::BufferAccumulator::BufferAccumulator
BufferAccumulator(const char *const compName)
Definition: BufferAccumulator.cpp:23
Fw::MemAllocator::allocate
virtual void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)=0
Allocate memory.
Fw::Buffer
Definition: Buffer.hpp:43
Svc::BufferAccumulator::~BufferAccumulator
~BufferAccumulator(void)
Definition: BufferAccumulator.cpp:45
Svc::BufferAccumulator::init
void init(const NATIVE_INT_TYPE queueDepth, const NATIVE_INT_TYPE instance=0)
Definition: BufferAccumulator.cpp:36
BufferAccumulator.hpp
Svc::BufferAccumulator::deallocateQueue
void deallocateQueue(Fw::MemAllocator &allocator)
Return allocated queue. Should be done during shutdown.
Definition: BufferAccumulator.cpp:71
FwOpcodeType
#define FwOpcodeType
Type representation for a command opcode.
Definition: FpConfig.hpp:62
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Svc::BufferAccumulator::allocateQueue
void allocateQueue(NATIVE_INT_TYPE identifier, Fw::MemAllocator &allocator, NATIVE_UINT_TYPE maxNumBuffers)
Definition: BufferAccumulator.cpp:54
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::MemAllocator
Definition: MemAllocator.hpp:44
Svc
Definition: ActiveRateGroupImplCfg.hpp:18
BasicTypes.hpp
Declares ISF basic types.
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100