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 =
39  dataOutAllocate_out(0, static_cast<U32>(size + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType)));
40  auto serialize = outgoing.getSerializer();
41  // Write data to our buffer
42  status = serialize.serializeFrom(static_cast<U32>(type));
43  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
44  status = serialize.serializeFrom(static_cast<U32>(port));
45  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
46  status = serialize.serializeFrom(data, size);
47  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
48  outgoing.setSize(static_cast<U32>(serialize.getBuffLength()));
49  dataOut_out(0, outgoing);
50 }
51 
52 // ----------------------------------------------------------------------
53 // Handler implementations for user-defined typed input ports
54 // ----------------------------------------------------------------------
55 
56 void GenericHubComponentImpl ::buffersIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
57  send_data(HUB_TYPE_BUFFER, portNum, fwBuffer.getData(), fwBuffer.getSize());
58  bufferDeallocate_out(0, fwBuffer);
59 }
60 
61 void GenericHubComponentImpl ::dataIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
62  HubType type = HUB_TYPE_MAX;
63  U32 type_in = 0;
64  U32 port = 0;
65  FwBuffSizeType size = 0;
67 
68  // Representation of incoming data prepped for serialization
69  auto incoming = fwBuffer.getDeserializer();
70  status = incoming.deserializeTo(type_in);
71  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
72  type = static_cast<HubType>(type_in);
73  FW_ASSERT(type < HUB_TYPE_MAX, type);
74  status = incoming.deserializeTo(port);
75  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
76  status = incoming.deserializeTo(size);
77  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
78 
79  // invokeSerial deserializes arguments before calling a normal invoke, this will return ownership immediately
80  U8* rawData = fwBuffer.getData() + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType);
81  U32 rawSize = static_cast<U32>(fwBuffer.getSize() - sizeof(U32) - sizeof(U32) - sizeof(FwBuffSizeType));
82  FW_ASSERT(rawSize == static_cast<U32>(size));
83  if (type == HUB_TYPE_PORT) {
84  // Com buffer representations should be copied before the call returns, so we need not "allocate" new data
85  Fw::ExternalSerializeBuffer wrapper(rawData, rawSize);
86  status = wrapper.setBuffLen(rawSize);
87  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
88  portOut_out(static_cast<FwIndexType>(port), wrapper);
89  // Deallocate the existing buffer
90  dataInDeallocate_out(0, fwBuffer);
91  } else if (type == HUB_TYPE_BUFFER) {
92  // Fw::Buffers can reuse the existing data buffer as the storage type! No deallocation done.
93  fwBuffer.set(rawData, rawSize, fwBuffer.getContext());
94  buffersOut_out(static_cast<FwIndexType>(port), fwBuffer);
95  } else if (type == HUB_TYPE_EVENT) {
96  FwEventIdType id;
97  Fw::Time timeTag;
98  Fw::LogSeverity severity;
99  Fw::LogBuffer args;
100 
101  // Deserialize tokens for events
102  status = incoming.deserializeTo(id);
103  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
104  status = incoming.deserializeTo(timeTag);
105  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
106  status = incoming.deserializeTo(severity);
107  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
108  status = incoming.deserializeTo(args);
109  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
110 
111  // Send it!
112  this->LogSend_out(static_cast<FwIndexType>(port), id, timeTag, severity, args);
113 
114  // Deallocate the existing buffer
115  dataInDeallocate_out(0, fwBuffer);
116  } else if (type == HUB_TYPE_CHANNEL) {
117  FwChanIdType id;
118  Fw::Time timeTag;
119  Fw::TlmBuffer val;
120 
121  // Deserialize tokens for channels
122  status = incoming.deserializeTo(id);
123  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
124  status = incoming.deserializeTo(timeTag);
125  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
126  status = incoming.deserializeTo(val);
127  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
128 
129  // Send it!
130  this->TlmSend_out(static_cast<FwIndexType>(port), id, timeTag, val);
131 
132  // Deallocate the existing buffer
133  dataInDeallocate_out(0, fwBuffer);
134  }
135 }
136 
137 void GenericHubComponentImpl ::LogRecv_handler(const FwIndexType portNum,
138  FwEventIdType id,
139  Fw::Time& timeTag,
140  const Fw::LogSeverity& severity,
141  Fw::LogBuffer& args) {
145  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
146  serializer.resetSer();
147  status = serializer.serializeFrom(id);
149  status = serializer.serializeFrom(timeTag);
151  status = serializer.serializeFrom(severity);
153  status = serializer.serializeFrom(args);
155  FwSizeType size = serializer.getBuffLength();
156  this->send_data(HubType::HUB_TYPE_EVENT, portNum, buffer, size);
157 }
158 
159 void GenericHubComponentImpl ::TlmRecv_handler(const FwIndexType portNum,
160  FwChanIdType id,
161  Fw::Time& timeTag,
162  Fw::TlmBuffer& val) {
165  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
166  serializer.resetSer();
167  status = serializer.serializeFrom(id);
169  status = serializer.serializeFrom(timeTag);
171  status = serializer.serializeFrom(val);
173  FwSizeType size = serializer.getBuffLength();
174  this->send_data(HubType::HUB_TYPE_CHANNEL, portNum, buffer, size);
175 }
176 
177 // ----------------------------------------------------------------------
178 // Handler implementations for user-defined serial input ports
179 // ----------------------------------------------------------------------
180 
181 void GenericHubComponentImpl ::portIn_handler(FwIndexType portNum,
182  Fw::SerializeBufferBase& Buffer
183 ) {
184  send_data(HUB_TYPE_PORT, portNum, Buffer.getBuffAddr(), Buffer.getBuffLength());
185 }
186 
187 } // end namespace Svc
Serialization/Deserialization operation was successful.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
void set(U8 *data, FwSizeType size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:86
PlatformSizeType FwSizeType
void setSize(FwSizeType size)
Definition: Buffer.cpp:75
void LogSend_out(FwIndexType portNum, FwEventIdType id, Fw::Time &timeTag, const Fw::LogSeverity &severity, Fw::LogBuffer &args)
Invoke output port LogSend.
U32 getContext() const
Definition: Buffer.cpp:64
U8 * getData() const
Definition: Buffer.cpp:56
FwSizeStoreType FwBuffSizeType
void dataOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port dataOut.
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:105
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:201
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:212
FwIdType FwChanIdType
The type of a telemetry channel identifier.
Fw::Buffer dataOutAllocate_out(FwIndexType portNum, FwSizeType size)
Invoke output port dataOutAllocate.
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:53
FwSizeType getSize() const
Definition: Buffer.cpp:60
PlatformIndexType FwIndexType
The size of the serial representation.
RateGroupDivider component implementation.
void dataInDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port dataInDeallocate.
SerializeStatus serializeFrom(U8 val)
serialize 8-bit unsigned int
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:95
#define FW_ASSERT(...)
Definition: Assert.hpp:14
#define FW_PORT_SERIALIZATION
for multi-note systems)
Definition: FpConfig.h:92
SerializeStatus deserializeTo(U8 &val)
deserialize 8-bit unsigned int