Skip to content

Commit

Permalink
Merge pull request #335 from fprime-community/issue-334-buffer-union-…
Browse files Browse the repository at this point in the history
…type

Revise component cpp code gen for async buffers
  • Loading branch information
bocchino authored Oct 4, 2023
2 parents dc93730 + 8c71407 commit 7e27b0d
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ case class ComponentCppWriter (
}

private def getClassMembers: List[CppDoc.Class.Member] = {
List(
List.concat(
// Friend classes
getFriendClassMembers,

Expand Down Expand Up @@ -205,7 +205,7 @@ case class ComponentCppWriter (
paramWriter.getVariableMembers,
getMsgSizeVariableMember,
getMutexVariableMembers,
).flatten
)
}

private def getConstantMembers: List[CppDoc.Class.Member] = {
Expand Down Expand Up @@ -251,79 +251,85 @@ case class ComponentCppWriter (
)
}

private def getAnonymousNamespaceMembers: List[CppDoc.Class.Member] = {
List(
linesClassMember(
Line.blank :: wrapInAnonymousNamespace(
intersperseBlankLines(
List(
getMsgTypeEnum,
getBuffUnion,
getComponentIpcSerializableBufferClass
private def getAnonymousNamespaceMembers: List[CppDoc.Class.Member] =
data.kind match {
case Ast.ComponentKind.Passive => Nil
case _ => List(
linesClassMember(
Line.blank :: wrapInAnonymousNamespace(
intersperseBlankLines(
List(
getMsgTypeEnum,
getBuffUnion,
getComponentIpcSerializableBufferClass
)
)
)
),
CppDoc.Lines.Cpp
),
CppDoc.Lines.Cpp
)
)
)
}
}

private def getMsgTypeEnum: List[Line] = {
wrapInScope(
"enum MsgTypeEnum {",
List(
if data.kind != Ast.ComponentKind.Passive then lines(
s"$exitConstantName = Fw::ActiveComponentBase::ACTIVE_COMPONENT_EXIT,"
)
else Nil,
List(
typedAsyncInputPorts.map(portCppConstantName),
serialAsyncInputPorts.map(portCppConstantName),
asyncCmds.map((_, cmd) => commandCppConstantName(cmd)),
internalPorts.map(internalPortCppConstantName),
).flatten.map(s => line(s"$s,"))
).flatten,
List.concat(
lines(s"$exitConstantName = Fw::ActiveComponentBase::ACTIVE_COMPONENT_EXIT"),
typedAsyncInputPorts.map(portCppConstantName),
serialAsyncInputPorts.map(portCppConstantName),
asyncCmds.map((_, cmd) => commandCppConstantName(cmd)),
internalPorts.map(internalPortCppConstantName),
).map(s => line(s"$s,")),
"};"
)
}

/** Generates a union type that lets the compiler calculate
* the max serialized size of any list of arguments that goes
* on the queue */
private def getBuffUnion: List[Line] = {
line("// Get the max size by doing a union of the input and internal port serialization sizes") ::
// Collect the serialized sizes of all the async port arguments
// For each one, add a byte array of that size as a member
val members = List.concat(
// Typed async input ports
typedAsyncInputPorts.flatMap(p => {
val portName = p.getUnqualifiedName
val portTypeName = getQualifiedPortTypeName(p, p.getDirection.get)
lines(s"BYTE ${portName}PortSize[${portTypeName}::SERIALIZED_SIZE];")
}),
// Command input port
guardedList (cmdRecvPort.isDefined)
(lines(s"BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];")),
// Internal ports
// Sum the sizes of the port arguments
internalPorts.flatMap(p =>
line(s"// Size of ${p.getUnqualifiedName} argument list") ::
(p.aNode._2.data.params match {
case Nil => lines("// [ no port arguments ]")
case _ => wrapInScope(
s"BYTE ${p.getUnqualifiedName}IntIfSize[",
lines(
p.aNode._2.data.params.map(param =>
s.getSerializedSizeExpr(
s.a.typeMap(param._2.data.typeName.id),
writeInternalPortParamType(param._2.data)
)
).mkString(" +\n")
),
"];"
)
})
)
)
List.concat(
lines("""|// Get the max size by constructing a union of the async input, command, and
|// internal port serialization sizes"""),
wrapInScope(
"union BuffUnion {",
List(
lines(
typedInputPorts.map(p =>
s"BYTE ${p.getUnqualifiedName}PortSize[${getQualifiedPortTypeName(p, p.getDirection.get)}::SERIALIZED_SIZE];"
).mkString("\n"),
),
cmdRespPort match {
case Some(p) => lines(
s"BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];"
)
case None => Nil
},
internalPorts.flatMap(p =>
line(s"// Size of ${p.getUnqualifiedName} argument list") ::
(p.aNode._2.data.params match {
case Nil => lines(s"BYTE ${p.getUnqualifiedName}IntIfSize[0];")
case _ => wrapInScope(
s"BYTE ${p.getUnqualifiedName}IntIfSize[",
lines(
p.aNode._2.data.params.map(param =>
s.getSerializedSizeExpr(
s.a.typeMap(param._2.data.typeName.id),
writeInternalPortParamType(param._2.data)
)
).mkString(" +\n")
),
"];"
)
})
)
).flatten,
members,
"};"
)
)
}

private def getComponentIpcSerializableBufferClass: List[Line] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ namespace {
CMD_CMD_PARAMS_PRIORITY_DROP,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ namespace {
TYPEDASYNCDROPPRIORITY_TYPED,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ namespace {
TYPEDASYNCDROPPRIORITY_TYPED,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,14 @@ namespace {
INT_IF_INTERNALSTRUCT,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
// Size of internalArray argument list
BYTE internalArrayIntIfSize[
Expand All @@ -68,7 +61,7 @@ namespace {
sizeof(U8)
];
// Size of internalPriorityDrop argument list
BYTE internalPriorityDropIntIfSize[0];
// [ no port arguments ]
// Size of internalString argument list
BYTE internalStringIntIfSize[
Fw::InternalInterfaceString::SERIALIZED_SIZE +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ namespace {
TYPEDASYNCDROPPRIORITY_TYPED,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,14 @@ namespace M {
INT_IF_INTERNALSTRUCT,
};

// Get the max size by doing a union of the input and internal port serialization sizes
// Get the max size by constructing a union of the async input, command, and
// internal port serialization sizes
union BuffUnion {
BYTE noArgsAsyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsGuardedPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE noArgsReturnGuardedPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsReturnSyncPortSize[Ports::InputNoArgsReturnPort::SERIALIZED_SIZE];
BYTE noArgsSyncPortSize[Ports::InputNoArgsPort::SERIALIZED_SIZE];
BYTE typedAsyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncAssertPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncBlockPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedAsyncDropPriorityPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedGuardedPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE typedReturnGuardedPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedReturnSyncPortSize[Ports::InputTypedReturnPort::SERIALIZED_SIZE];
BYTE typedSyncPortSize[Ports::InputTypedPort::SERIALIZED_SIZE];
BYTE cmdPortSize[Fw::InputCmdPort::SERIALIZED_SIZE];
// Size of internalArray argument list
BYTE internalArrayIntIfSize[
Expand All @@ -66,7 +59,7 @@ namespace M {
sizeof(U8)
];
// Size of internalPriorityDrop argument list
BYTE internalPriorityDropIntIfSize[0];
// [ no port arguments ]
// Size of internalString argument list
BYTE internalStringIntIfSize[
Fw::InternalInterfaceString::SERIALIZED_SIZE +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,6 @@
#endif
#include "base/EmptyComponentAc.hpp"

namespace {
// Get the max size by doing a union of the input and internal port serialization sizes
union BuffUnion {

};

// Define a message buffer class large enough to handle all the
// asynchronous inputs to the component
class ComponentIpcSerializableBuffer :
public Fw::SerializeBufferBase
{

public:

enum {
// Max. message size = size of data + message id + port
SERIALIZATION_SIZE =
sizeof(BuffUnion) +
sizeof(NATIVE_INT_TYPE) +
sizeof(NATIVE_INT_TYPE)
};

NATIVE_UINT_TYPE getBuffCapacity() const {
return sizeof(m_buff);
}

U8* getBuffAddr() {
return m_buff;
}

const U8* getBuffAddr() const {
return m_buff;
}

private:
// Should be the max of all the input ports serialized sizes...
U8 m_buff[SERIALIZATION_SIZE];

};
}

// ----------------------------------------------------------------------
// Component initialization
// ----------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 7e27b0d

Please sign in to comment.