-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
GenericHubComponentImpl.cpp
186 lines (160 loc) · 8.68 KB
/
GenericHubComponentImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// ======================================================================
// \title GenericHubComponentImpl.cpp
// \author mstarch
// \brief cpp file for GenericHub component implementation class
//
// \copyright
// Copyright 2009-2015, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
#include <FpConfig.hpp>
#include <Svc/GenericHub/GenericHubComponentImpl.hpp>
#include "Fw/Logger/Logger.hpp"
#include "Fw/Types/Assert.hpp"
// Required port serialization or the hub cannot work
static_assert(FW_PORT_SERIALIZATION, "FW_PORT_SERIALIZATION must be enabled to use GenericHub");
namespace Svc {
// ----------------------------------------------------------------------
// Construction, initialization, and destruction
// ----------------------------------------------------------------------
GenericHubComponentImpl ::GenericHubComponentImpl(const char* const compName) : GenericHubComponentBase(compName) {}
void GenericHubComponentImpl ::init(const NATIVE_INT_TYPE instance) {
GenericHubComponentBase::init(instance);
}
GenericHubComponentImpl ::~GenericHubComponentImpl() {}
void GenericHubComponentImpl ::send_data(const HubType type,
const NATIVE_INT_TYPE port,
const U8* data,
const U32 size) {
FW_ASSERT(data != nullptr);
Fw::SerializeStatus status;
// Buffer to send and a buffer used to write to it
Fw::Buffer outgoing = dataOutAllocate_out(0, size + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType));
Fw::SerializeBufferBase& serialize = outgoing.getSerializeRepr();
// Write data to our buffer
status = serialize.serialize(static_cast<U32>(type));
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = serialize.serialize(static_cast<U32>(port));
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = serialize.serialize(data, size);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
outgoing.setSize(serialize.getBuffLength());
dataOut_out(0, outgoing);
}
// ----------------------------------------------------------------------
// Handler implementations for user-defined typed input ports
// ----------------------------------------------------------------------
void GenericHubComponentImpl ::buffersIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
send_data(HUB_TYPE_BUFFER, portNum, fwBuffer.getData(), fwBuffer.getSize());
bufferDeallocate_out(0, fwBuffer);
}
void GenericHubComponentImpl ::dataIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
HubType type = HUB_TYPE_MAX;
U32 type_in = 0;
U32 port = 0;
FwBuffSizeType size = 0;
Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
// Representation of incoming data prepped for serialization
Fw::SerializeBufferBase& incoming = fwBuffer.getSerializeRepr();
FW_ASSERT(incoming.setBuffLen(fwBuffer.getSize()) == Fw::FW_SERIALIZE_OK);
// Must inform buffer that there is *real* data in the buffer
status = incoming.setBuffLen(fwBuffer.getSize());
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(type_in);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
type = static_cast<HubType>(type_in);
FW_ASSERT(type < HUB_TYPE_MAX, type);
status = incoming.deserialize(port);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(size);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
// invokeSerial deserializes arguments before calling a normal invoke, this will return ownership immediately
U8* rawData = fwBuffer.getData() + sizeof(U32) + sizeof(U32) + sizeof(FwBuffSizeType);
U32 rawSize = fwBuffer.getSize() - sizeof(U32) - sizeof(U32) - sizeof(FwBuffSizeType);
FW_ASSERT(rawSize == static_cast<U32>(size));
if (type == HUB_TYPE_PORT) {
// Com buffer representations should be copied before the call returns, so we need not "allocate" new data
Fw::ExternalSerializeBuffer wrapper(rawData, rawSize);
status = wrapper.setBuffLen(rawSize);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
portOut_out(port, wrapper);
// Deallocate the existing buffer
dataInDeallocate_out(0, fwBuffer);
} else if (type == HUB_TYPE_BUFFER) {
// Fw::Buffers can reuse the existing data buffer as the storage type! No deallocation done.
fwBuffer.set(rawData, rawSize, fwBuffer.getContext());
buffersOut_out(port, fwBuffer);
} else if (type == HUB_TYPE_EVENT) {
FwEventIdType id;
Fw::Time timeTag;
Fw::LogSeverity severity;
Fw::LogBuffer args;
// Deserialize tokens for events
status = incoming.deserialize(id);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(timeTag);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(severity);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(args);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
// Send it!
this->LogSend_out(port, id, timeTag, severity, args);
// Deallocate the existing buffer
dataInDeallocate_out(0, fwBuffer);
} else if (type == HUB_TYPE_CHANNEL) {
FwChanIdType id;
Fw::Time timeTag;
Fw::TlmBuffer val;
// Deserialize tokens for channels
status = incoming.deserialize(id);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(timeTag);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
status = incoming.deserialize(val);
FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<NATIVE_INT_TYPE>(status));
// Send it!
this->TlmSend_out(port, id, timeTag, val);
// Deallocate the existing buffer
dataInDeallocate_out(0, fwBuffer);
}
}
void GenericHubComponentImpl ::LogRecv_handler(const NATIVE_INT_TYPE portNum,
FwEventIdType id,
Fw::Time& timeTag,
const Fw::LogSeverity& severity,
Fw::LogBuffer& args) {
U8 buffer[sizeof(FwEventIdType) + Fw::Time::SERIALIZED_SIZE + Fw::LogSeverity::SERIALIZED_SIZE + FW_LOG_BUFFER_MAX_SIZE];
Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
serializer.resetSer();
FW_ASSERT(serializer.serialize(id) == Fw::SerializeStatus::FW_SERIALIZE_OK);;
FW_ASSERT(serializer.serialize(timeTag) == Fw::SerializeStatus::FW_SERIALIZE_OK);
FW_ASSERT(serializer.serialize(severity) == Fw::SerializeStatus::FW_SERIALIZE_OK);
FW_ASSERT(serializer.serialize(args) == Fw::SerializeStatus::FW_SERIALIZE_OK);
U32 size = serializer.getBuffLength();
this->send_data(HubType::HUB_TYPE_EVENT, portNum, buffer, size);
}
void GenericHubComponentImpl ::TlmRecv_handler(const NATIVE_INT_TYPE portNum,
FwChanIdType id,
Fw::Time& timeTag,
Fw::TlmBuffer& val) {
U8 buffer[sizeof(FwChanIdType) + Fw::Time::SERIALIZED_SIZE + FW_TLM_BUFFER_MAX_SIZE];
Fw::ExternalSerializeBuffer serializer(buffer, sizeof(buffer));
serializer.resetSer();
FW_ASSERT(serializer.serialize(id) == Fw::SerializeStatus::FW_SERIALIZE_OK);
FW_ASSERT(serializer.serialize(timeTag) == Fw::SerializeStatus::FW_SERIALIZE_OK);
FW_ASSERT(serializer.serialize(val) == Fw::SerializeStatus::FW_SERIALIZE_OK);
U32 size = serializer.getBuffLength();
this->send_data(HubType::HUB_TYPE_CHANNEL, portNum, buffer, size);
}
// ----------------------------------------------------------------------
// Handler implementations for user-defined serial input ports
// ----------------------------------------------------------------------
void GenericHubComponentImpl ::portIn_handler(NATIVE_INT_TYPE portNum, /*!< The port number*/
Fw::SerializeBufferBase& Buffer /*!< The serialization buffer*/
) {
send_data(HUB_TYPE_PORT, portNum, Buffer.getBuffAddr(), Buffer.getBuffLength());
}
} // end namespace Svc