F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
GenericHubComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title GenericHubComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for GenericHub component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <Fw/FPrimeBasicTypes.hpp>
15 #include "Fw/Logger/Logger.hpp"
16 #include "Fw/Types/Assert.hpp"
17 
18 // Required port serialization or the hub cannot work
19 static_assert(FW_PORT_SERIALIZATION, "FW_PORT_SERIALIZATION must be enabled to use GenericHub");
20 
21 namespace Svc {
22 
23 // ----------------------------------------------------------------------
24 // Construction, initialization, and destruction
25 // ----------------------------------------------------------------------
26 
28 
30 
31 void GenericHubComponentImpl ::send_data(const HubType type,
32  const FwIndexType port,
33  const U8* data,
34  const FwSizeType size) {
35  FW_ASSERT(data != nullptr);
36  Fw::SerializeStatus status;
37  // Buffer to send and a buffer used to write to it
38  Fw::Buffer outgoing = dataOutAllocate_out(0, static_cast<U32>(size + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType)));
39  auto serialize = outgoing.getSerializer();
40  // Write data to our buffer
41  status = serialize.serialize(static_cast<U32>(type));
42  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
43  status = serialize.serialize(static_cast<U32>(port));
44  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
45  status = serialize.serialize(data, size);
46  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
47  outgoing.setSize(static_cast<U32>(serialize.getBuffLength()));
48  dataOut_out(0, outgoing);
49 }
50 
51 // ----------------------------------------------------------------------
52 // Handler implementations for user-defined typed input ports
53 // ----------------------------------------------------------------------
54 
55 void GenericHubComponentImpl ::buffersIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
56  send_data(HUB_TYPE_BUFFER, portNum, fwBuffer.getData(), fwBuffer.getSize());
57  bufferDeallocate_out(0, fwBuffer);
58 }
59 
60 void GenericHubComponentImpl ::dataIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
61  HubType type = HUB_TYPE_MAX;
62  U32 type_in = 0;
63  U32 port = 0;
64  FwBuffSizeType size = 0;
66 
67  // Representation of incoming data prepped for serialization
68  auto incoming = fwBuffer.getDeserializer();
69  status = incoming.deserialize(type_in);
70  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
71  type = static_cast<HubType>(type_in);
72  FW_ASSERT(type < HUB_TYPE_MAX, type);
73  status = incoming.deserialize(port);
74  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
75  status = incoming.deserialize(size);
76  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
77 
78  // invokeSerial deserializes arguments before calling a normal invoke, this will return ownership immediately
79  U8* rawData = fwBuffer.getData() + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType);
80  U32 rawSize = static_cast<U32>(fwBuffer.getSize() - sizeof(U32) - sizeof(U32) - sizeof(FwBuffSizeType));
81  FW_ASSERT(rawSize == static_cast<U32>(size));
82  if (type == HUB_TYPE_PORT) {
83  // Com buffer representations should be copied before the call returns, so we need not "allocate" new data
84  Fw::ExternalSerializeBuffer wrapper(rawData, rawSize);
85  status = wrapper.setBuffLen(rawSize);
86  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
87  portOut_out(static_cast<FwIndexType>(port), wrapper);
88  // Deallocate the existing buffer
89  dataInDeallocate_out(0, fwBuffer);
90  } else if (type == HUB_TYPE_BUFFER) {
91  // Fw::Buffers can reuse the existing data buffer as the storage type! No deallocation done.
92  fwBuffer.set(rawData, rawSize, fwBuffer.getContext());
93  buffersOut_out(static_cast<FwIndexType>(port), fwBuffer);
94  } else if (type == HUB_TYPE_EVENT) {
95  FwEventIdType id;
96  Fw::Time timeTag;
97  Fw::LogSeverity severity;
98  Fw::LogBuffer args;
99 
100  // Deserialize tokens for events
101  status = incoming.deserialize(id);
102  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
103  status = incoming.deserialize(timeTag);
104  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
105  status = incoming.deserialize(severity);
106  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
107  status = incoming.deserialize(args);
108  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
109 
110  // Send it!
111  this->LogSend_out(static_cast<FwIndexType>(port), id, timeTag, severity, args);
112 
113  // Deallocate the existing buffer
114  dataInDeallocate_out(0, fwBuffer);
115  } else if (type == HUB_TYPE_CHANNEL) {
116  FwChanIdType id;
117  Fw::Time timeTag;
118  Fw::TlmBuffer val;
119 
120  // Deserialize tokens for channels
121  status = incoming.deserialize(id);
122  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
123  status = incoming.deserialize(timeTag);
124  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
125  status = incoming.deserialize(val);
126  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
127 
128  // Send it!
129  this->TlmSend_out(static_cast<FwIndexType>(port), id, timeTag, val);
130 
131  // Deallocate the existing buffer
132  dataInDeallocate_out(0, fwBuffer);
133  }
134 }
135 
136 void GenericHubComponentImpl ::LogRecv_handler(const FwIndexType portNum,
137  FwEventIdType id,
138  Fw::Time& timeTag,
139  const Fw::LogSeverity& severity,
140  Fw::LogBuffer& args) {
143  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
144  serializer.resetSer();
145  status = serializer.serialize(id);
147  status = serializer.serialize(timeTag);
149  status = serializer.serialize(severity);
151  status = serializer.serialize(args);
153  FwSizeType size = serializer.getBuffLength();
154  this->send_data(HubType::HUB_TYPE_EVENT, portNum, buffer, size);
155 
156 }
157 
158 void GenericHubComponentImpl ::TlmRecv_handler(const FwIndexType portNum,
159  FwChanIdType id,
160  Fw::Time& timeTag,
161  Fw::TlmBuffer& val) {
164  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
165  serializer.resetSer();
166  status = serializer.serialize(id);
168  status = serializer.serialize(timeTag);
170  status = serializer.serialize(val);
172  FwSizeType size = serializer.getBuffLength();
173  this->send_data(HubType::HUB_TYPE_CHANNEL, portNum, buffer, size);
174 }
175 
176 // ----------------------------------------------------------------------
177 // Handler implementations for user-defined serial input ports
178 // ----------------------------------------------------------------------
179 
180 void GenericHubComponentImpl ::portIn_handler(FwIndexType portNum,
181  Fw::SerializeBufferBase& Buffer
182 ) {
183  send_data(HUB_TYPE_PORT, portNum, Buffer.getBuffAddr(), Buffer.getBuffLength());
184 }
185 
186 } // end namespace Svc
Serialization/Deserialization operation was successful.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
Definition: Time.hpp:9
PlatformSizeType FwSizeType
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
void LogSend_out(FwIndexType portNum, FwEventIdType id, Fw::Time &timeTag, const Fw::LogSeverity &severity, Fw::LogBuffer &args)
Invoke output port LogSend.
void setSize(SizeType size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
U8 * getData() const
Definition: Buffer.cpp:68
FwSizeStoreType FwBuffSizeType
void dataOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port dataOut.
Fw::Buffer dataOutAllocate_out(FwIndexType portNum, U32 size)
Invoke output port dataOutAllocate.
Fw::SerializeStatus portOut_out(FwIndexType portNum, Fw::SerializeBufferBase &buffer)
Invoke output port portOut.
Auto-generated base for GenericHub component.
SerializeStatus
forward declaration for string
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:117
FwIdType FwEventIdType
The type of an event identifier.
Serializable::SizeType getBuffLength() const
returns current buffer size
#define FW_LOG_BUFFER_MAX_SIZE
Definition: FpConfig.h:213
void buffersOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port buffersOut.
External serialize buffer with no copy semantics.
#define FW_TLM_BUFFER_MAX_SIZE
Definition: FpConfig.h:224
FwIdType FwChanIdType
The type of a telemetry channel identifier.
Enum representing event severity.
GenericHubComponentImpl(const char *const compName)
void TlmSend_out(FwIndexType portNum, FwChanIdType id, Fw::Time &timeTag, Fw::TlmBuffer &val)
Invoke output port TlmSend.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
The size of the serial representation.
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
PlatformIndexType FwIndexType
RateGroupDivider component implementation.
void dataInDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port dataInDeallocate.
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
SizeType getSize() const
Definition: Buffer.cpp:72
void set(U8 *data, SizeType size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:98
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:107
#define FW_ASSERT(...)
Definition: Assert.hpp:14
#define FW_PORT_SERIALIZATION
for multi-note systems)
Definition: FpConfig.h:104