F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.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 NATIVE_INT_TYPE port,
33  const U8* data,
34  const U32 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  Fw::SerializeBufferBase& serialize = outgoing.getSerializeRepr();
40  // Write data to our buffer
41  status = serialize.serialize(static_cast<U32>(type));
42  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
43  status = serialize.serialize(static_cast<U32>(port));
44  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
45  status = serialize.serialize(data, size);
46  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
47  outgoing.setSize(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 NATIVE_INT_TYPE 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 NATIVE_INT_TYPE 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  Fw::SerializeBufferBase& incoming = fwBuffer.getSerializeRepr();
69  FW_ASSERT(incoming.setBuffLen(fwBuffer.getSize()) == Fw::FW_SERIALIZE_OK);
70 
71  // Must inform buffer that there is *real* data in the buffer
72  status = incoming.setBuffLen(fwBuffer.getSize());
73  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
74  status = incoming.deserialize(type_in);
75  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
76  type = static_cast<HubType>(type_in);
77  FW_ASSERT(type < HUB_TYPE_MAX, type);
78  status = incoming.deserialize(port);
79  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
80  status = incoming.deserialize(size);
81  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
82 
83  // invokeSerial deserializes arguments before calling a normal invoke, this will return ownership immediately
84  U8* rawData = fwBuffer.getData() + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType);
85  U32 rawSize = static_cast<U32>(fwBuffer.getSize() - sizeof(U32) - sizeof(U32) - sizeof(FwBuffSizeType));
86  FW_ASSERT(rawSize == static_cast<U32>(size));
87  if (type == HUB_TYPE_PORT) {
88  // Com buffer representations should be copied before the call returns, so we need not "allocate" new data
89  Fw::ExternalSerializeBuffer wrapper(rawData, rawSize);
90  status = wrapper.setBuffLen(rawSize);
91  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
92  portOut_out(static_cast<FwIndexType>(port), wrapper);
93  // Deallocate the existing buffer
94  dataInDeallocate_out(0, fwBuffer);
95  } else if (type == HUB_TYPE_BUFFER) {
96  // Fw::Buffers can reuse the existing data buffer as the storage type! No deallocation done.
97  fwBuffer.set(rawData, rawSize, fwBuffer.getContext());
98  buffersOut_out(static_cast<FwIndexType>(port), fwBuffer);
99  } else if (type == HUB_TYPE_EVENT) {
100  FwEventIdType id;
101  Fw::Time timeTag;
102  Fw::LogSeverity severity;
103  Fw::LogBuffer args;
104 
105  // Deserialize tokens for events
106  status = incoming.deserialize(id);
107  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
108  status = incoming.deserialize(timeTag);
109  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
110  status = incoming.deserialize(severity);
111  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
112  status = incoming.deserialize(args);
113  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
114 
115  // Send it!
116  this->LogSend_out(static_cast<FwIndexType>(port), id, timeTag, severity, args);
117 
118  // Deallocate the existing buffer
119  dataInDeallocate_out(0, fwBuffer);
120  } else if (type == HUB_TYPE_CHANNEL) {
121  FwChanIdType id;
122  Fw::Time timeTag;
123  Fw::TlmBuffer val;
124 
125  // Deserialize tokens for channels
126  status = incoming.deserialize(id);
127  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
128  status = incoming.deserialize(timeTag);
129  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
130  status = incoming.deserialize(val);
131  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
132 
133  // Send it!
134  this->TlmSend_out(static_cast<FwIndexType>(port), id, timeTag, val);
135 
136  // Deallocate the existing buffer
137  dataInDeallocate_out(0, fwBuffer);
138  }
139 }
140 
141 void GenericHubComponentImpl ::LogRecv_handler(const NATIVE_INT_TYPE portNum,
142  FwEventIdType id,
143  Fw::Time& timeTag,
144  const Fw::LogSeverity& severity,
145  Fw::LogBuffer& args) {
148  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
149  serializer.resetSer();
150  status = serializer.serialize(id);
152  status = serializer.serialize(timeTag);
154  status = serializer.serialize(severity);
156  status = serializer.serialize(args);
158  U32 size = serializer.getBuffLength();
159  this->send_data(HubType::HUB_TYPE_EVENT, portNum, buffer, size);
160 
161 }
162 
163 void GenericHubComponentImpl ::TlmRecv_handler(const NATIVE_INT_TYPE portNum,
164  FwChanIdType id,
165  Fw::Time& timeTag,
166  Fw::TlmBuffer& val) {
169  Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
170  serializer.resetSer();
171  status = serializer.serialize(id);
173  status = serializer.serialize(timeTag);
175  status = serializer.serialize(val);
177  U32 size = serializer.getBuffLength();
178  this->send_data(HubType::HUB_TYPE_CHANNEL, portNum, buffer, size);
179 }
180 
181 // ----------------------------------------------------------------------
182 // Handler implementations for user-defined serial input ports
183 // ----------------------------------------------------------------------
184 
185 void GenericHubComponentImpl ::portIn_handler(NATIVE_INT_TYPE portNum,
186  Fw::SerializeBufferBase& Buffer
187 ) {
188  send_data(HUB_TYPE_PORT, portNum, Buffer.getBuffAddr(), Buffer.getBuffLength());
189 }
190 
191 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
U32 FwChanIdType
Definition: FpConfig.h:95
#define FW_LOG_BUFFER_MAX_SIZE
Definition: FpConfig.h:313
U32 FwEventIdType
Definition: FpConfig.h:103
FwSizeStoreType FwBuffSizeType
Definition: FpConfig.h:452
#define FW_PORT_SERIALIZATION
for multi-note systems)
Definition: FpConfig.h:204
#define FW_TLM_BUFFER_MAX_SIZE
Definition: FpConfig.h:324
PlatformIndexType FwIndexType
Definition: FpConfig.h:25
C++-compatible configuration header for fprime configuration.
U8 * getData() const
Definition: Buffer.cpp:68
void set(U8 *data, U32 size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:98
U32 getSize() const
Definition: Buffer.cpp:72
void setSize(U32 size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
SerializeBufferBase & getSerializeRepr()
Definition: Buffer.cpp:107
External serialize buffer with no copy semantics.
Enum representing event severity.
@ SERIALIZED_SIZE
The size of the serial representation.
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
Serializable::SizeType getBuffLength() const
returns current buffer size
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Time.hpp:9
@ SERIALIZED_SIZE
Definition: Time.hpp:13
Auto-generated base for GenericHub component.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
Fw::Buffer dataOutAllocate_out(FwIndexType portNum, U32 size)
Invoke output port dataOutAllocate.
void buffersOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port buffersOut.
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.
void dataInDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port dataInDeallocate.
void LogSend_out(FwIndexType portNum, FwEventIdType id, Fw::Time &timeTag, const Fw::LogSeverity &severity, Fw::LogBuffer &args)
Invoke output port LogSend.
void TlmSend_out(FwIndexType portNum, FwChanIdType id, Fw::Time &timeTag, Fw::TlmBuffer &val)
Invoke output port TlmSend.
@ HUB_TYPE_BUFFER
Buffer type transmission.
@ HUB_TYPE_CHANNEL
Telemetry channel type.
@ HUB_TYPE_PORT
Port type transmission.
GenericHubComponentImpl(const char *const compName)
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.