From e20c6fdac4bd779e2fe0cee336a2e52b8056ca5a Mon Sep 17 00:00:00 2001 From: Rob Bocchino Date: Tue, 3 Sep 2024 17:28:27 -0700 Subject: [PATCH] Revise state machine code gen --- .../ComponentStateMachines.scala | 124 ++++++++++-------- .../ActiveStateMachinesComponentAc.ref.cpp | 5 + 2 files changed, 71 insertions(+), 58 deletions(-) diff --git a/compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentStateMachines.scala b/compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentStateMachines.scala index 568503a14..afbb5190e 100644 --- a/compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentStateMachines.scala +++ b/compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentStateMachines.scala @@ -30,27 +30,6 @@ case class ComponentStateMachines( ) } - def getInternalInterfaceHandler: List[Line] = - Line.blank :: - line("// Call the state machine update function") :: - wrapInSwitch( - "stateMachineId", - stateMachineInstances.flatMap((smi) => { - val smName = s.writeSymbol(smi.symbol) - val enumName = s.getName(smi.symbol) - - Line.blank :: - lines( - s"""|case STATE_MACHINE_${smi.getName.toUpperCase}: { - | ${smName}_Interface::${enumName}_Signals signal = - | static_cast<${smName}_Interface::${enumName}_Signals>(enumStoreSmSignal); - | this->m_stateMachine_${smi.getName}.update(stateMachineId, signal, data); - | break; - |}""" - ) - }) - ) - /** Gets the state machine interfaces */ def getSmInterfaces: String = smSymbols.map(symbol => s", public ${s.writeSymbol(symbol)}_Interface"). @@ -80,43 +59,45 @@ case class ComponentStateMachines( /** Writes the dispatch case, if any, for state machine instances */ def writeDispatch: List[Line] = { - lazy val caseBody = List.concat( - lines( - s"""|// Deserialize the state machine ID - |FwEnumStoreType enumStoreSmId = 0; - |Fw::SerializeStatus deserStatus = msg.deserialize(enumStoreSmId); - |FW_ASSERT( - | deserStatus == Fw::FW_SERIALIZE_OK, - | static_cast(deserStatus) - |); - |SmId stateMachineId = static_cast(enumStoreSmId); - | - |// Deserialize the state machine signal - |FwEnumStoreType enumStoreSmSignal = 0; - |deserStatus = msg.deserialize(enumStoreSmSignal); - |FW_ASSERT( - | deserStatus == Fw::FW_SERIALIZE_OK, - | static_cast(deserStatus) - |); - | - |// Deserialize the state machine data - |Fw::SmSignalBuffer data; - |deserStatus = msg.deserialize(data); - |FW_ASSERT( - | Fw::FW_SERIALIZE_OK == deserStatus, - | static_cast(deserStatus) - |); - | - |// Make sure there was no data left over. - |// That means the buffer size was incorrect. - |FW_ASSERT( - | msg.getBuffLeft() == 0, - | static_cast(msg.getBuffLeft()) - |);""" - ), - getInternalInterfaceHandler, - lines("\nbreak;") - ) + lazy val caseBody = + Line.blank :: + List.concat( + lines( + s"""|// Deserialize the state machine ID + |FwEnumStoreType enumStoreSmId = 0; + |Fw::SerializeStatus deserStatus = msg.deserialize(enumStoreSmId); + |FW_ASSERT( + | deserStatus == Fw::FW_SERIALIZE_OK, + | static_cast(deserStatus) + |); + |SmId stateMachineId = static_cast(enumStoreSmId); + | + |// Deserialize the state machine signal + |FwEnumStoreType enumStoreSmSignal = 0; + |deserStatus = msg.deserialize(enumStoreSmSignal); + |FW_ASSERT( + | deserStatus == Fw::FW_SERIALIZE_OK, + | static_cast(deserStatus) + |); + | + |// Deserialize the state machine data + |Fw::SmSignalBuffer data; + |deserStatus = msg.deserialize(data); + |FW_ASSERT( + | Fw::FW_SERIALIZE_OK == deserStatus, + | static_cast(deserStatus) + |); + | + |// Make sure there was no data left over. + |// That means the buffer size was incorrect. + |FW_ASSERT( + | msg.getBuffLeft() == 0, + | static_cast(msg.getBuffLeft()) + |);""" + ), + writeStateMachineUpdate, + lines("\nbreak;") + ) lazy val caseStmt = Line.blank :: line(s"// Handle state machine signals") :: @@ -215,6 +196,33 @@ case class ComponentStateMachines( ) } + private def writeStateMachineUpdate: List[Line] = + Line.blank :: + line("// Call the state machine update function") :: + wrapInSwitch( + "stateMachineId", + List.concat( + stateMachineInstances.flatMap((smi) => { + val smName = s.writeSymbol(smi.symbol) + val enumName = s.getName(smi.symbol) + Line.blank :: + lines( + s"""|case STATE_MACHINE_${smi.getName.toUpperCase}: { + | ${smName}_Interface::${enumName}_Signals signal = + | static_cast<${smName}_Interface::${enumName}_Signals>(enumStoreSmSignal); + | this->m_stateMachine_${smi.getName}.update(stateMachineId, signal, data); + | break; + |}""" + ) + }), + Line.blank :: lines( + s"""|default: + | FW_ASSERT(0, static_cast(stateMachineId)); + | break;""" + ) + ) + ) + } object ComponentStateMachines { diff --git a/compiler/tools/fpp-to-cpp/test/component/base/ActiveStateMachinesComponentAc.ref.cpp b/compiler/tools/fpp-to-cpp/test/component/base/ActiveStateMachinesComponentAc.ref.cpp index 0c996b076..fe599cac0 100644 --- a/compiler/tools/fpp-to-cpp/test/component/base/ActiveStateMachinesComponentAc.ref.cpp +++ b/compiler/tools/fpp-to-cpp/test/component/base/ActiveStateMachinesComponentAc.ref.cpp @@ -489,6 +489,7 @@ namespace M { // Handle state machine signals case STATEMACHINE_SENDSIGNALS: { + // Deserialize the state machine ID FwEnumStoreType enumStoreSmId = 0; Fw::SerializeStatus deserStatus = msg.deserialize(enumStoreSmId); @@ -565,6 +566,10 @@ namespace M { this->m_stateMachine_sm6.update(stateMachineId, signal, data); break; } + + default: + FW_ASSERT(0, static_cast(stateMachineId)); + break; } break;