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
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 <FpConfig.hpp>
15
16namespace Svc {
17
18 // ----------------------------------------------------------------------
19 // Construction, initialization, and destruction
20 // ----------------------------------------------------------------------
21
22 BufferAccumulator ::
23 BufferAccumulator(const char *const compName) :
24 BufferAccumulatorComponentBase(compName),
25 mode(DRAIN),
26 bufferMemory(nullptr),
27 bufferQueue(),
28 send(true),
29 numWarnings(0),
30 allocatorId(0)
31 {
32
33 }
34
35 void BufferAccumulator ::
36 init(
37 const NATIVE_INT_TYPE queueDepth,
38 const NATIVE_INT_TYPE instance
39 )
40 {
41 BufferAccumulatorComponentBase::init(queueDepth, instance);
42 }
43
44 BufferAccumulator ::
45 ~BufferAccumulator()
46 {
47 }
48
49 // ----------------------------------------------------------------------
50 // Public methods
51 // ----------------------------------------------------------------------
52
53 void BufferAccumulator ::
54 allocateQueue(
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
70 void BufferAccumulator ::
71 deallocateQueue(Fw::MemAllocator& allocator)
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::CmdResponse::OK);
153 }
154
155 // ----------------------------------------------------------------------
156 // Private helper methods
157 // ----------------------------------------------------------------------
158
159 void BufferAccumulator ::
160 sendStoredBuffer()
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}
#define FW_ASSERT(...)
Definition Assert.hpp:7
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
PlatformUIntType NATIVE_UINT_TYPE
Definition BasicTypes.h:52
U32 FwOpcodeType
Definition FpConfig.h:56
C++-compatible configuration header for fprime configuration.
virtual void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)=0
Allocate memory.
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.