Cleaned up GAMS and duplicated code

This commit is contained in:
Martino Ferrari
2026-02-05 12:09:34 +01:00
parent 084b18d095
commit d18a573a5a
94 changed files with 667 additions and 12021 deletions

View File

@@ -17,7 +17,8 @@
* or implied. See the Licence permissions and limitations under the Licence.
* @details This source file contains the definition of all the methods for
* the class JAConditionalSignalUpdateGAM (public, protected, and private). Be aware that some
* the class JAConditionalSignalUpdateGAM (public, protected, and private). Be
aware that some
* methods, such as those inline could be defined on the header file, instead.
*/
@@ -28,8 +29,11 @@
/*---------------------------------------------------------------------------*/
/* Project header includes */
/*---------------------------------------------------------------------------*/
#include "JAConditionalSignalUpdateGAM.h"
#include "AdvancedErrorManagement.h"
#include "Architecture/x86_gcc/CompilerTypes.h"
#include "ErrorType.h"
#include "JAConditionalSignalUpdateGAM.h"
#include "TypeDescriptor.h"
/*---------------------------------------------------------------------------*/
/* Static definitions */
@@ -39,271 +43,269 @@
/* Method definitions */
/*---------------------------------------------------------------------------*/
bool parse_comparator(const MARTe::StreamString &str,
JAConditionalSignalUpdateGAM::ComparisonMode &op) {
if (str == "EQUALS") {
op = JAConditionalSignalUpdateGAM::Equals;
return true;
}
if (str == "NOT") {
op = JAConditionalSignalUpdateGAM::Not;
return true;
}
if (str == "GREATER") {
op = JAConditionalSignalUpdateGAM::Greater;
return true;
}
if (str == "EQUALS_OR_GREATER") {
op = JAConditionalSignalUpdateGAM::EqualsOrGreater;
return true;
}
if (str == "LESS") {
op = JAConditionalSignalUpdateGAM::Less;
return true;
}
if (str == "EQUALS_OR_LESS") {
op = JAConditionalSignalUpdateGAM::EqualsOrLess;
return true;
}
return false;
}
JAConditionalSignalUpdateGAM::JAConditionalSignalUpdateGAM() {
inputSignals = NULL_PTR(void **);
inputSignalTypes = NULL_PTR(MARTe::TypeDescriptor *);
values = NULL_PTR(MARTe::uint32 *);
valuesCount = 0u;
outputSignals = NULL_PTR(MARTe::uint32 **);
defaultValues = NULL_PTR(MARTe::uint32 **);
needsReset = false;
expectedValues = NULL_PTR(MARTe::uint32 *);
expectedValuesCount = 0u;
operation = And;
comparators = NULL_PTR(ComparisonMode *);
inputSignals = NULL_PTR(void **);
inputSignalTypes = NULL_PTR(MARTe::TypeDescriptor *);
comparators = NULL_PTR(comparator_t *);
outputSignals = NULL_PTR(MARTe::uint32 **);
outputs = NULL_PTR(output_t *);
needsReset = false;
operation = And;
}
JAConditionalSignalUpdateGAM::~JAConditionalSignalUpdateGAM() {
if (outputSignals != NULL_PTR(MARTe::uint32 **)) {
delete[] outputSignals;
}
if (inputSignals != NULL_PTR(void **)) {
delete[] inputSignals;
}
if (inputSignalTypes != NULL_PTR(MARTe::TypeDescriptor *)) {
delete[] inputSignalTypes;
}
if (values != NULL_PTR(MARTe::uint32 *)) {
delete[] values;
}
if (comparators != NULL_PTR(ComparisonMode *)) {
delete[] comparators;
}
if (defaultValues != NULL_PTR(MARTe::uint32 **)) {
delete[] defaultValues;
}
if (outputSignals != NULL_PTR(MARTe::uint32 **)) {
delete[] outputSignals;
}
if (inputSignals != NULL_PTR(void **)) {
delete[] inputSignals;
}
if (inputSignalTypes != NULL_PTR(MARTe::TypeDescriptor *)) {
delete[] inputSignalTypes;
}
if (outputs != NULL_PTR(output_t *)) {
delete[] outputs;
}
if (comparators != NULL_PTR(comparator_t *)) {
delete[] comparators;
}
}
bool JAConditionalSignalUpdateGAM::Initialise(MARTe::StructuredDataI & data) {
using namespace MARTe;
bool ok = GAM::Initialise(data);
if (ok) {
// Read expected values.
AnyType valuesArray = data.GetType("ExpectedValues");
if (valuesArray.GetDataPointer() != NULL) {
expectedValuesCount = valuesArray.GetNumberOfElements(0u);
expectedValues = new uint32[expectedValuesCount];
Vector<uint32> valuesVector(expectedValues, expectedValuesCount);
ok = (data.Read("ExpectedValues", valuesVector));
}
bool JAConditionalSignalUpdateGAM::Initialise(MARTe::StructuredDataI &data) {
using namespace MARTe;
bool ok = GAM::Initialise(data);
if (ok) {
MARTe::StreamString operationStr;
if (data.Read("Operation", operationStr)) {
if (operationStr == "AND") {
operation = And;
} else if (operationStr == "OR") {
operation = Or;
} else if (operationStr == "NOR") {
operation = Nor;
} else if (operationStr == "XOR") {
operation = Xor;
} else {
ok = false;
REPORT_ERROR(ErrorManagement::ParametersError,
"Operation %s is not defined", operationStr.Buffer());
}
}
}
if (ok) {
ok = data.MoveRelative("InputSignals");
uint32 level = 0;
if (ok) {
// Read comparators.
AnyType comparatorsArray = data.GetType("Comparators");
if (comparatorsArray.GetDataPointer() != NULL) {
uint32 count;
if (ok) {
count = comparatorsArray.GetNumberOfElements(0u);
ok = count == expectedValuesCount;
}
if (ok) {
comparators = new ComparisonMode[count];
StreamString* comp = new StreamString[count];
Vector<StreamString> compVector(comp, count);
ok = (data.Read("Comparators", compVector));
if (ok) {
for (uint32 i = 0; i < count; ++i) {
if (comp[i] == "EQUALS") {
comparators[i] = Equals;
} else if (comp[i] == "NOT") {
comparators[i] = Not;
} else if (comp[i] == "GREATER") {
comparators[i] = Greater;
} else if (comp[i] == "EQUALS_OR_GREATER") {
comparators[i] = EqualsOrGreater;
} else if (comp[i] == "LESS") {
comparators[i] = Less;
} else if (comp[i] == "EQUALS_OR_LESS") {
comparators[i] = EqualsOrLess;
} else {
ok = false;
REPORT_ERROR(ErrorManagement::ParametersError, "Comparator %s is not defined.", comp[i].Buffer());
}
}
}
delete[] comp;
} else {
REPORT_ERROR(ErrorManagement::ParametersError, "Expected values and operators shall have the same "
"number of elements.");
}
} else {
// Create default comparators (equals) when they aren't provided in the configuration.
comparators = new ComparisonMode[expectedValuesCount];
for (uint32 i = 0; i < expectedValuesCount; ++i) {
comparators[i] = Equals;
}
}
}
if (ok) {
MARTe::StreamString operationStr;
if (data.Read("Operation", operationStr)) {
if (operationStr == "AND") {
operation = And;
}
else if (operationStr == "OR") {
operation = Or;
}
else if (operationStr == "NOR") {
operation = Nor;
}
else if (operationStr == "XOR") {
operation = Xor;
}
else {
ok = false;
REPORT_ERROR(ErrorManagement::ParametersError, "Operation %s is not defined", operationStr.Buffer());
}
}
}
if (ok) {
// Read output signal values to be set.
AnyType valuesArray = data.GetType("Values");
ok = (valuesArray.GetDataPointer() != NULL);
if (ok) {
valuesCount = valuesArray.GetNumberOfElements(0u);
ok = valuesCount > 0u;
}
if (ok) {
values = new uint32[valuesCount];
Vector<uint32> valuesVector(values, valuesCount);
ok = (data.Read("Values", valuesVector));
}
level++;
uint32 n_inputs = data.GetNumberOfChildren();
comparators = new comparator_t[n_inputs];
StreamString buffer;
TypeDescriptor td;
for (uint32 i = 0; ok && i < n_inputs; i++) {
ok = data.MoveToChild(i);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError, "Values shall be defined.");
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to move to InputSignals[%lu]", i);
break;
}
level++;
ok = data.Read("Type", buffer);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Missing mandatory field Type from InputSignals[%lu]",
i);
break;
}
td = TypeDescriptor::GetTypeDescriptorFromTypeName(buffer.Buffer());
ok = (td == UnsignedInteger8Bit) || (td == UnsignedInteger16Bit) ||
(td == UnsignedInteger32Bit);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Wrong value for field Type from InputSignals[%lu]", i);
break;
}
ok = data.Read("Comparator", buffer);
if (!ok) {
REPORT_ERROR(
ErrorManagement::ParametersError,
"Missing mandatory field Comparator from InputSignals[%lu]", i);
break;
}
ok = parse_comparator(buffer, comparators[i].comparator);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Non Valid Comparator `%s` from InputSignals[%lu]",
buffer, i);
break;
}
ok = data.Read("Value", comparators[i].value);
if (!ok) {
REPORT_ERROR(
ErrorManagement::ParametersError,
"Missing field Value (expecting int) from InputSignals[%lu]", i);
break;
}
if (data.MoveToAncestor(1)) {
level--;
} else {
ok = false;
}
}
data.MoveToAncestor(level);
} else {
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to move to InputSignals");
}
return ok;
}
if (ok) {
ok = data.MoveRelative("OutputSignals");
uint32 level = 0;
if (ok) {
level++;
uint32 n_outputs = data.GetNumberOfChildren();
outputs = new output_t[n_outputs];
for (uint32 i = 0; ok && i < n_outputs; i++) {
ok = data.MoveToChild(i);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to move to InputSignals[%lu]", i);
break;
}
level++;
ok = data.Read("DefaultValue", outputs[i].defaultValue);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to read field DefaultValue for output %lu",
i);
break;
}
ok = data.Read("Value", outputs[i].value);
if (!ok) {
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to read field Value for output %lu", i);
break;
}
ok = data.MoveToAncestor(1);
if (ok) {
level--;
}
}
data.MoveToAncestor(level);
} else {
REPORT_ERROR(ErrorManagement::ParametersError,
"Impossible to move to OutputSignals");
}
}
return ok;
}
bool JAConditionalSignalUpdateGAM::Setup() {
using namespace MARTe;
bool ok = numberOfInputSignals == (expectedValuesCount + numberOfOutputSignals);
using namespace MARTe;
bool ok = numberOfInputSignals > 0;
if (ok) {
inputSignals = new void *[numberOfInputSignals];
uint32 i;
for (uint32 i = 0u; i < numberOfOutputSignals; i++) {
inputSignals[i] = GetInputSignalMemory(i);
}
} else {
REPORT_ERROR(ErrorManagement::ParametersError,
"Number of input signals shall be greater then 0 ");
}
if (ok) {
ok = numberOfOutputSignals > 0u;
if (ok) {
inputSignals = new void*[expectedValuesCount];
defaultValues = new uint32*[numberOfOutputSignals];
uint32 i;
for (i = 0u; i < expectedValuesCount; i++) {
inputSignals[i] = GetInputSignalMemory(i);
}
for (; i < numberOfInputSignals; i++) {
defaultValues[i - expectedValuesCount] = reinterpret_cast<uint32 *>(GetInputSignalMemory(i));
}
outputSignals = new uint32 *[numberOfOutputSignals];
for (uint32 i = 0u; i < numberOfOutputSignals; i++) {
outputSignals[i] = reinterpret_cast<uint32 *>(GetOutputSignalMemory(i));
}
} else {
REPORT_ERROR(ErrorManagement::ParametersError, "Number of input signals shall be equal to number "
"of expected values plus number of output signals.");
}
if (ok) {
inputSignalTypes = new TypeDescriptor[expectedValuesCount];
uint32 i;
for (i = 0u; (i < expectedValuesCount) && (ok); i++) {
inputSignalTypes[i] = GetSignalType(InputSignals, i);
ok = ((inputSignalTypes[i] == UnsignedInteger32Bit) || (inputSignalTypes[i] == UnsignedInteger16Bit));
if (!ok) {
StreamString signalName;
(void) GetSignalName(InputSignals, i, signalName);
REPORT_ERROR(ErrorManagement::ParametersError, "Signal %s shall be defined as uint32 or uint16", signalName.Buffer());
}
}
}
if (ok) {
ok = numberOfOutputSignals == valuesCount;
if (ok) {
ok = numberOfOutputSignals > 0u;
if (ok) {
outputSignals = new uint32*[numberOfOutputSignals];
uint32 i;
for (i = 0u; i < numberOfOutputSignals; i++) {
outputSignals[i] = reinterpret_cast<uint32 *>(GetOutputSignalMemory(i));
}
}
else {
REPORT_ERROR(ErrorManagement::ParametersError, "At least one output signal shall be defined");
}
}
else {
REPORT_ERROR(ErrorManagement::ParametersError, "Number of output signals shall be the same as "
"number of provided values.");
}
REPORT_ERROR(ErrorManagement::ParametersError,
"At least one output signal shall be defined");
}
}
return ok;
return ok;
}
bool JAConditionalSignalUpdateGAM::PrepareNextState(const MARTe::char8 * const currentStateName, const MARTe::char8 * const nextStateName) {
needsReset = false;
return true;
bool JAConditionalSignalUpdateGAM::PrepareNextState(
const MARTe::char8 *const currentStateName,
const MARTe::char8 *const nextStateName) {
needsReset = false;
return true;
}
bool accumulate(JAConditionalSignalUpdateGAM::OperationMode mode, bool next,
bool current) {
switch (mode) {
case JAConditionalSignalUpdateGAM::Or:
return next || current;
case JAConditionalSignalUpdateGAM::And:
return next && current;
case JAConditionalSignalUpdateGAM::Nor:
return !(next || current);
case JAConditionalSignalUpdateGAM::Xor:
return (!next != !current);
}
return false;
}
bool JAConditionalSignalUpdateGAM::Execute() {
if (!needsReset) {
bool eventDetected = expectedValuesCount == 0;
if (!eventDetected) {
if (operation == Or) {
MARTe::uint32 j;
for (j = 0; (j < expectedValuesCount) && (!eventDetected); j++) {
eventDetected = Compare(j);
}
}
else if (operation == Nor) {
MARTe::uint32 j;
for (j = 0; (j < expectedValuesCount) && (!eventDetected); j++) {
eventDetected = Compare(j);
}
eventDetected = !eventDetected;
}
else if (operation == And) {
MARTe::uint32 j;
eventDetected = Compare(0);
for (j = 1; (j < expectedValuesCount); j++) {
eventDetected &= Compare(j);
}
}
else if (operation == Xor) {
MARTe::uint32 j;
MARTe::uint32 eventDetectedUint32;
if (inputSignalTypes[0] == MARTe::UnsignedInteger32Bit) {
eventDetectedUint32 = *static_cast<MARTe::uint32 *>(inputSignals[0]);
}
else {
eventDetectedUint32 = *static_cast<MARTe::uint16 *>(inputSignals[0]);
}
for (j = 1; (j < expectedValuesCount); j++) {
eventDetectedUint32 ^= Compare(j);
}
eventDetected = (eventDetectedUint32 == 1u);
}
}
if (eventDetected) {
needsReset = true;
MARTe::uint32 i;
for (i = 0u; i < numberOfOutputSignals; ++i) {
*outputSignals[i] = values[i];
MARTe::StreamString signalName;
(void) GetSignalName(MARTe::OutputSignals, i, signalName);
}
}
else {
MARTe::uint32 i;
for (i = 0u; i < numberOfOutputSignals; ++i) {
*outputSignals[i] = *defaultValues[i];
}
}
if (!needsReset) {
bool state = Compare(0);
for (MARTe::uint32 i = 1; i < numberOfInputSignals; i++) {
state = accumulate(operation, Compare(i), state);
}
return true;
if (state) {
needsReset = true;
MARTe::uint32 i;
for (i = 0u; i < numberOfOutputSignals; ++i) {
*outputSignals[i] = outputs[i].value;
}
} else {
MARTe::uint32 i;
for (i = 0u; i < numberOfOutputSignals; ++i) {
*outputSignals[i] = outputs[i].defaultValue;
}
}
}
return true;
}
bool JAConditionalSignalUpdateGAM::Compare(MARTe::uint32 index) {
if (inputSignalTypes[index] == MARTe::UnsignedInteger32Bit) {
return Compare<MARTe::uint32>(index);
}
return Compare<MARTe::uint16>(index);
if (inputSignalTypes[index] == MARTe::UnsignedInteger32Bit) {
return Compare<MARTe::uint32>(index);
}
return Compare<MARTe::uint16>(index);
}
CLASS_REGISTER(JAConditionalSignalUpdateGAM, "1.0")