F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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
14#include "Fw/Logger/Logger.hpp"
15#include "Fw/Types/Assert.hpp"
16#include <FpConfig.hpp>
17
18// Required port serialization or the hub cannot work
19static_assert(FW_PORT_SERIALIZATION, "FW_PORT_SERIALIZATION must be enabled to use GenericHub");
20
21namespace Svc {
22
23// ----------------------------------------------------------------------
24// Construction, initialization, and destruction
25// ----------------------------------------------------------------------
26
27GenericHubComponentImpl ::GenericHubComponentImpl(const char* const compName) : GenericHubComponentBase(compName) {}
28
29void GenericHubComponentImpl ::init(const NATIVE_INT_TYPE instance) {
30 GenericHubComponentBase::init(instance);
31}
32
33GenericHubComponentImpl ::~GenericHubComponentImpl() {}
34
35void GenericHubComponentImpl ::send_data(const HubType type,
36 const NATIVE_INT_TYPE port,
37 const U8* data,
38 const U32 size) {
39 FW_ASSERT(data != nullptr);
41 // Buffer to send and a buffer used to write to it
42 Fw::Buffer outgoing = dataOutAllocate_out(0, size + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType));
43 Fw::SerializeBufferBase& serialize = outgoing.getSerializeRepr();
44 // Write data to our buffer
45 status = serialize.serialize(static_cast<U32>(type));
46 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
47 status = serialize.serialize(static_cast<U32>(port));
48 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
49 status = serialize.serialize(data, size);
50 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
51 outgoing.setSize(serialize.getBuffLength());
52 dataOut_out(0, outgoing);
53
54}
55
56// ----------------------------------------------------------------------
57// Handler implementations for user-defined typed input ports
58// ----------------------------------------------------------------------
59
60void GenericHubComponentImpl ::buffersIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
61 send_data(HUB_TYPE_BUFFER, portNum, fwBuffer.getData(), fwBuffer.getSize());
62 bufferDeallocate_out(0, fwBuffer);
63}
64
65void GenericHubComponentImpl ::dataIn_handler(const NATIVE_INT_TYPE portNum,
66 Fw::Buffer& fwBuffer) {
67 HubType type = HUB_TYPE_MAX;
68 U32 type_in = 0;
69 U32 port = 0;
70 FwBuffSizeType size = 0;
72
73 // Representation of incoming data prepped for serialization
74 Fw::SerializeBufferBase& incoming = fwBuffer.getSerializeRepr();
75 FW_ASSERT(incoming.setBuffLen(fwBuffer.getSize()) == Fw::FW_SERIALIZE_OK);
76
77 // Must inform buffer that there is *real* data in the buffer
78 status = incoming.setBuffLen(fwBuffer.getSize());
79 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
80 status = incoming.deserialize(type_in);
81 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
82 type = static_cast<HubType>(type_in);
83 FW_ASSERT(type < HUB_TYPE_MAX, type);
84 status = incoming.deserialize(port);
85 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
86 status = incoming.deserialize(size);
87 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
88
89 // invokeSerial deserializes arguments before calling a normal invoke, this will return ownership immediately
90 U8* rawData = fwBuffer.getData() + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType);
91 U32 rawSize = fwBuffer.getSize() - sizeof(U32) - sizeof(U32) - sizeof(FwBuffSizeType);
92 FW_ASSERT(rawSize == static_cast<U32>(size));
93 if (type == HUB_TYPE_PORT) {
94 // Com buffer representations should be copied before the call returns, so we need not "allocate" new data
95 Fw::ExternalSerializeBuffer wrapper(rawData, rawSize);
96 status = wrapper.setBuffLen(rawSize);
97 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
98 portOut_out(port, wrapper);
99 dataInDeallocate_out(0, fwBuffer);
100 } else if (type == HUB_TYPE_BUFFER) {
101 fwBuffer.set(rawData, rawSize, fwBuffer.getContext());
102 buffersOut_out(port, fwBuffer);
103 }
104}
105
106// ----------------------------------------------------------------------
107// Handler implementations for user-defined serial input ports
108// ----------------------------------------------------------------------
109
110void GenericHubComponentImpl ::portIn_handler(NATIVE_INT_TYPE portNum,
112) {
113 send_data(HUB_TYPE_PORT, portNum, Buffer.getBuffAddr(), Buffer.getBuffLength());
114}
115
116} // end namespace Svc
#define FW_ASSERT(...)
Definition Assert.hpp:7
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
#define FW_PORT_SERIALIZATION
Definition FpConfig.h:130
U16 FwBuffSizeType
Definition FpConfig.h:30
C++-compatible configuration header for fprime configuration.
U8 * getData() const
Definition Buffer.cpp:60
void set(U8 *data, U32 size, U32 context=NO_CONTEXT)
Definition Buffer.cpp:90
U32 getSize() const
Definition Buffer.cpp:64
void setSize(U32 size)
Definition Buffer.cpp:79
U32 getContext() const
Definition Buffer.cpp:68
SerializeBufferBase & getSerializeRepr()
Definition Buffer.cpp:99
SerializeStatus setBuffLen(NATIVE_UINT_TYPE length)
sets buffer length manually after filling with data
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
NATIVE_UINT_TYPE getBuffLength() const
returns current buffer size
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.