F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
DpDemo.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title DpDemo.cpp
3 // \author jawest
4 // \brief cpp file for DpDemo component implementation class
5 // ======================================================================
6 
7 #include "Ref/DpDemo/DpDemo.hpp"
8 
9 namespace Ref {
10 
11 // ----------------------------------------------------------------------
12 // Component construction and destruction
13 // ----------------------------------------------------------------------
14 
15 DpDemo ::DpDemo(const char* const compName) : DpDemoComponentBase(compName) {
16  this->selectedColor = DpDemo_ColorEnum::RED;
17  this->numRecords = 0;
18  this->dpPriority = 0;
19  this->dpInProgress = false;
20 }
21 
23 
24 // ----------------------------------------------------------------------
25 // Handler implementations for typed input ports
26 // ----------------------------------------------------------------------
27 
28 void DpDemo ::run_handler(FwIndexType portNum, U32 context) {
29  // If a Data product is being generated, store records
30  if (this->dpInProgress) {
31  this->dpContainer.serializeRecord_StringRecord(Fw::String("Test string"));
32  this->dpContainer.serializeRecord_BooleanRecord(true);
33  this->dpContainer.serializeRecord_I32Record(-100);
34  this->dpContainer.serializeRecord_F64Record(1.25);
35  this->dpContainer.serializeRecord_U32ArrayRecord(DpDemo_U32Array({1, 2, 3, 4, 5}));
36  this->dpContainer.serializeRecord_F32ArrayRecord(DpDemo_F32Array({1.1f, 2.2f, 3.3f}));
37  this->dpContainer.serializeRecord_BooleanArrayRecord(DpDemo_BooleanArray({true, false}));
38  // Array Records
39  // Array record of strings
40  Fw::String str0("String array element 0");
41  Fw::String str1("String array element 1");
42  Fw::String str2("String array element 2");
43  const Fw::StringBase* strings[3] = {&str0, &str1, &str2};
44  this->dpContainer.serializeRecord_StringArrayRecord(strings, 3);
45  // Array record of arrays
46  const DpDemo_StringArray arrayArray[1] = {DpDemo_StringArray(
47  {Fw::String("0 - String array record element 0"), Fw::String("0 - String array record element 1")})};
48  this->dpContainer.serializeRecord_ArrayArrayRecord(arrayArray, 1);
49  // Array record of structs
50  const DpDemo_StructWithStringMembers structArray[2] = {
51  DpDemo_StructWithStringMembers(Fw::String("0 - String member"),
52  DpDemo_StringArray({Fw::String("0 - String array element 0"),
53  Fw::String("0 - String array element 1")})),
54  DpDemo_StructWithStringMembers(Fw::String("1 - String member"),
55  DpDemo_StringArray({Fw::String("1 - String array element 0"),
56  Fw::String("1 - String array element 1")}))};
57  this->dpContainer.serializeRecord_StructArrayRecord(structArray, 2);
58  this->dpContainer.serializeRecord_ArrayOfStringArrayRecord(DpDemo_ArrayOfStringArray(
59  {DpDemo_StringArray({Fw::String("0 - String array element 0"), Fw::String("0 - String array element 1")}),
60  DpDemo_StringArray({Fw::String("1 - String array element 0"), Fw::String("1 - String array element 1")}),
61  DpDemo_StringArray(
62  {Fw::String("2 - String array element 0"), Fw::String("2 - String array element 1")})}));
63  this->dpContainer.serializeRecord_ArrayOfStructsRecord(DpDemo_ArrayOfStructs(
64  {DpDemo_StructWithStringMembers(Fw::String("0 - String member"),
65  DpDemo_StringArray({Fw::String("0 - String array element 0"),
66  Fw::String("0 - String array element 1")})),
67  DpDemo_StructWithStringMembers(Fw::String("1 - String member"),
68  DpDemo_StringArray({Fw::String("1 - String array element 0"),
69  Fw::String("1 - String array element 1")})),
70  DpDemo_StructWithStringMembers(Fw::String("2 - String member"),
71  DpDemo_StringArray({Fw::String("2 - String array element 0"),
72  Fw::String("2 - String array element 1")}))}));
73  this->dpContainer.serializeRecord_EnumArrayRecord(
74  DpDemo_EnumArray({DpDemo_ColorEnum::RED, DpDemo_ColorEnum::GREEN, DpDemo_ColorEnum::BLUE}));
75  this->dpContainer.serializeRecord_StructWithEverythingRecord(DpDemo_StructWithEverything(
76  -1, 2.5, Fw::String("String Member"), false, this->selectedColor,
77  {DpDemo_U32Array({1, 2, 3, 4, 5}), DpDemo_U32Array({6, 7, 8, 9, 10})}, DpDemo_F32Array({4.4f, 5.5f, 6.6f}),
78  DpDemo_U32Array({6, 7, 8, 9, 10}),
79  DpDemo_EnumArray({DpDemo_ColorEnum::RED, DpDemo_ColorEnum::GREEN, DpDemo_ColorEnum::BLUE}),
80  DpDemo_StringArray({Fw::String("String array element 0"), Fw::String("String array element 1")}),
81  DpDemo_BooleanArray({true, false}),
82  DpDemo_StructWithStringMembers(
83  Fw::String("String member"),
84  DpDemo_StringArray({Fw::String("String array element 0"), Fw::String("String array element 1")})),
85  DpDemo_ArrayOfStringArray({DpDemo_StringArray({Fw::String("0 - String array element 0"),
86  Fw::String("0 - String array element 1")}),
87  DpDemo_StringArray({Fw::String("1 - String array element 0"),
88  Fw::String("1 - String array element 1")}),
89  DpDemo_StringArray({Fw::String("2 - String array element 0"),
90  Fw::String("2 - String array element 1")})})));
91  this->log_ACTIVITY_LO_DpComplete(this->numRecords);
92  this->cleanupAndSendDp();
93  }
94 }
95 
96 // ----------------------------------------------------------------------
97 // Handler implementations for commands
98 // ----------------------------------------------------------------------
99 
100 void DpDemo ::SelectColor_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, const Ref::DpDemo_ColorEnum& color) {
101  this->selectedColor = color;
102  log_ACTIVITY_HI_ColorSelected(color);
103  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
104 }
105 
106 void DpDemo ::Dp_cmdHandler(FwOpcodeType opCode,
107  U32 cmdSeq,
108  const DpDemo_DpReqType& reqType,
109  U32 priority,
110  const Fw::DpCfg::ProcType& proc) {
111  // make sure DPs are available
112  if (!this->isConnected_productGetOut_OutputPort(0) || !this->isConnected_productRequestOut_OutputPort(0)) {
113  this->log_WARNING_HI_DpsNotConnected();
114  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
115  return;
116  }
117 
118  this->numRecords = 14; // 14 records in current demo
119 
120  // Element counts for the variable length array records serialized below
121  const FwSizeType stringArrayElements = 3;
122  const FwSizeType arrayArrayElements = 1;
123  const FwSizeType structArrayElements = 2;
124 
125  // The generated SIZE_OF_*_RECORD constants already account for the record
126  // id, and for array records the serialized length prefix, so the demo no
127  // longer has to reproduce the layout by hand
128  FwSizeType dpSize = SIZE_OF_StringRecord_RECORD + SIZE_OF_BooleanRecord_RECORD + SIZE_OF_I32Record_RECORD +
129  SIZE_OF_F64Record_RECORD + SIZE_OF_U32ArrayRecord_RECORD + SIZE_OF_F32ArrayRecord_RECORD +
130  SIZE_OF_BooleanArrayRecord_RECORD + SIZE_OF_EnumArrayRecord_RECORD +
131  SIZE_OF_StructWithEverythingRecord_RECORD + SIZE_OF_ArrayOfStringArrayRecord_RECORD +
132  SIZE_OF_ArrayOfStructsRecord_RECORD + SIZE_OF_StringArrayRecord_RECORD(stringArrayElements) +
133  SIZE_OF_ArrayArrayRecord_RECORD(arrayArrayElements) +
134  SIZE_OF_StructArrayRecord_RECORD(structArrayElements);
135 
136  this->dpPriority = static_cast<FwDpPriorityType>(priority);
137  this->dpProc = proc;
138  this->log_ACTIVITY_LO_DpMemRequested(dpSize);
139  if (reqType == DpDemo_DpReqType::IMMEDIATE) {
140  Fw::Success stat = this->dpGet_DpDemoContainer(dpSize, this->dpContainer);
141  // make sure we got the memory we wanted
142  if (Fw::Success::FAILURE == stat) {
143  this->log_WARNING_HI_DpMemoryFail();
144  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
145  } else {
146  this->dpInProgress = true;
147  this->log_ACTIVITY_LO_DpStarted(numRecords);
148  this->log_ACTIVITY_LO_DpMemReceived(this->dpContainer.getBuffer().getSize());
149  // override priority with requested priority
150  this->dpContainer.setPriority(priority);
151  this->dpContainer.setProcTypes(proc);
152  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
153  }
154  } else if (reqType == DpDemo_DpReqType::ASYNC) {
155  // Response is deferred until the requested container arrives
156  this->pendingOpCode = opCode;
157  this->pendingCmdSeq = cmdSeq;
158  this->dpRequest_DpDemoContainer(dpSize);
159  } else {
160  // should never get here
161  FW_ASSERT(0, reqType.e);
162  }
163 }
164 
165 // ----------------------------------------------------------------------
166 // Handler implementations for data products
167 // ----------------------------------------------------------------------
168 
169 void DpDemo ::dpRecv_DpDemoContainer_handler(DpContainer& container, Fw::Success::T status) {
170  // Make sure we got the buffer we wanted or quit
171  if (Fw::Success::SUCCESS == status) {
172  this->dpContainer = container;
173  this->dpInProgress = true;
174  // set previously requested priority
175  this->dpContainer.setPriority(this->dpPriority);
176  this->dpContainer.setProcTypes(this->dpProc);
177  this->log_ACTIVITY_LO_DpStarted(this->numRecords);
178  this->cmdResponse_out(this->pendingOpCode, this->pendingCmdSeq, Fw::CmdResponse::OK);
179  } else {
180  this->log_WARNING_HI_DpMemoryFail();
181  // cleanup
182  this->dpInProgress = false;
183  this->numRecords = 0;
184  this->cmdResponse_out(this->pendingOpCode, this->pendingCmdSeq, Fw::CmdResponse::EXECUTION_ERROR);
185  }
186 }
187 
188 void DpDemo ::cleanupAndSendDp() {
189  this->dpSend(this->dpContainer);
190  this->dpInProgress = false;
191  this->numRecords = 0;
192 }
193 
194 } // namespace Ref
FwIdType FwOpcodeType
The type of a command opcode.
Representing success.
PlatformSizeType FwSizeType
U32 FwDpPriorityType
The type of a data product priority.
StringTemplate< FW_FIXED_LENGTH_STRING_SIZE > String
Definition: String.hpp:14
~DpDemo()
Destroy DpDemo object.
Definition: DpDemo.cpp:22
T
The raw enum type.
Representing failure.
Command successfully executed.
Command had execution error.
PlatformIndexType FwIndexType
DpDemo(const char *const compName)
Construct DpDemo object.
Definition: DpDemo.cpp:15
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.