Skip to content

Commit

Permalink
fix(code-gen/java): Updated the codegen for java to generally use sma…
Browse files Browse the repository at this point in the history
…ller types for unsigned integers (a byte is able to keep an unsigned integer value to 7 bits and not up to 4 bits)

feat(java): Added PlcBINT, PlcUBINT and PlcBREAL plc-value types
  • Loading branch information
chrisdutz committed Jul 21, 2023
1 parent 04eb5da commit 8772f90
Show file tree
Hide file tree
Showing 373 changed files with 5,539 additions and 5,135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,50 +157,49 @@ public String getPlcValueTypeForTypeReference(TypeReference typeReference) {
return "PlcStruct";
}
SimpleTypeReference simpleTypeReference = (SimpleTypeReference) typeReference;
int sizeInBits = simpleTypeReference.getSizeInBits();
switch (simpleTypeReference.getBaseType()) {
case BIT:
return "PlcBOOL";
case BYTE:
return "PlcBYTE";
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
if (sizeInBits <= 8) {
return "PlcUSINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
if (sizeInBits <= 16) {
return "PlcUINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
if (sizeInBits <= 32) {
return "PlcUDINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 64) {
if (sizeInBits <= 64) {
return "PlcULINT";
}
throw new RuntimeException("Unsupported UINT with bit length " + sizeInBits);
case INT:
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (integerTypeReference.getSizeInBits() <= 8) {
if (sizeInBits <= 8) {
return "PlcSINT";
}
if (integerTypeReference.getSizeInBits() <= 16) {
if (sizeInBits <= 16) {
return "PlcINT";
}
if (integerTypeReference.getSizeInBits() <= 32) {
if (sizeInBits <= 32) {
return "PlcDINT";
}
if (integerTypeReference.getSizeInBits() <= 64) {
if (sizeInBits <= 64) {
return "PlcLINT";
}

throw new RuntimeException("Unsupported INT with bit length " + sizeInBits);
case FLOAT:
case UFLOAT:
FloatTypeReference floatTypeReference = (FloatTypeReference) simpleTypeReference;
int sizeInBits = floatTypeReference.getSizeInBits();
if (sizeInBits <= 32) {
return "PlcREAL";
}
if (sizeInBits <= 64) {
return "PlcLREAL";
}
throw new RuntimeException("Unsupported REAL with bit length " + sizeInBits);
case STRING:
case VSTRING:
return "PlcSTRING";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public String getLanguageTypeNameForTypeReference(TypeReference typeReference, b
return allowPrimitive ? byte.class.getSimpleName() : Byte.class.getSimpleName();
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 4) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 7) {
return allowPrimitive ? byte.class.getSimpleName() : Byte.class.getSimpleName();
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 15) {
return allowPrimitive ? short.class.getSimpleName() : Short.class.getSimpleName();
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 31) {
return allowPrimitive ? int.class.getSimpleName() : Integer.class.getSimpleName();
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 63) {
return allowPrimitive ? long.class.getSimpleName() : Long.class.getSimpleName();
}
return BigInteger.class.getSimpleName();
Expand Down Expand Up @@ -172,50 +172,49 @@ public String getPlcValueTypeForTypeReference(TypeReference typeReference) {
return "PlcStruct";
}
SimpleTypeReference simpleTypeReference = (SimpleTypeReference) typeReference;
int sizeInBits = simpleTypeReference.getSizeInBits();
switch (simpleTypeReference.getBaseType()) {
case BIT:
return "PlcBOOL";
case BYTE:
return "PlcSINT";
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 4) {
if (sizeInBits <= 8) {
return "PlcUSINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
if (sizeInBits <= 16) {
return "PlcUINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
if (sizeInBits <= 32) {
return "PlcUDINT";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
if (sizeInBits <= 64) {
return "PlcULINT";
}
throw new RuntimeException("Unsupported UINT with bit length " + sizeInBits);
case INT:
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (integerTypeReference.getSizeInBits() <= 8) {
if (sizeInBits <= 8) {
return "PlcSINT";
}
if (integerTypeReference.getSizeInBits() <= 16) {
if (sizeInBits <= 16) {
return "PlcINT";
}
if (integerTypeReference.getSizeInBits() <= 32) {
if (sizeInBits <= 32) {
return "PlcDINT";
}
if (integerTypeReference.getSizeInBits() <= 64) {
if (sizeInBits <= 64) {
return "PlcLINT";
}

throw new RuntimeException("Unsupported INT with bit length " + sizeInBits);
case FLOAT:
case UFLOAT:
FloatTypeReference floatTypeReference = (FloatTypeReference) simpleTypeReference;
int sizeInBits = floatTypeReference.getSizeInBits();
if (sizeInBits <= 32) {
return "PlcREAL";
}
if (sizeInBits <= 64) {
return "PlcLREAL";
}
throw new RuntimeException("Unsupported REAL with bit length " + sizeInBits);
case STRING:
case VSTRING:
return "PlcSTRING";
Expand All @@ -238,10 +237,10 @@ public String getNullValueForTypeReference(TypeReference typeReference) {
return "0";
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 31) {
return "0";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 63) {
return "0l";
}
return "null";
Expand Down Expand Up @@ -315,13 +314,13 @@ public String getReadBufferReadMethodCall(String logicalName, SimpleTypeReferenc
case UINT:
String unsignedIntegerType;
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 4) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 7) {
unsignedIntegerType = "UnsignedByte";
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 15) {
unsignedIntegerType = "UnsignedShort";
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 31) {
unsignedIntegerType = "UnsignedInt";
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
} else if (unsignedIntegerTypeReference.getSizeInBits() <= 63) {
unsignedIntegerType = "UnsignedLong";
} else {
unsignedIntegerType = "UnsignedBigInteger";
Expand Down Expand Up @@ -416,10 +415,10 @@ public String getDataReaderCall(SimpleTypeReference simpleTypeReference) {
case BYTE:
return "readByte(readBuffer, " + sizeInBits + ")";
case UINT:
if (sizeInBits <= 4) return "readUnsignedByte(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 8) return "readUnsignedShort(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 16) return "readUnsignedInt(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 32) return "readUnsignedLong(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 7) return "readUnsignedByte(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 15) return "readUnsignedShort(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 31) return "readUnsignedInt(readBuffer, " + sizeInBits + ")";
if (sizeInBits <= 63) return "readUnsignedLong(readBuffer, " + sizeInBits + ")";
return "readUnsignedBigInteger(readBuffer, " + sizeInBits + ")";
case INT:
if (sizeInBits <= 8) return "readSignedByte(readBuffer, " + sizeInBits + ")";
Expand Down Expand Up @@ -483,10 +482,10 @@ public String getDataWriterCall(SimpleTypeReference simpleTypeReference) {
case BYTE:
return "writeByte(writeBuffer, " + sizeInBits + ")";
case UINT:
if (sizeInBits <= 4) return "writeUnsignedByte(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 8) return "writeUnsignedShort(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 16) return "writeUnsignedInt(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 32) return "writeUnsignedLong(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 7) return "writeUnsignedByte(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 15) return "writeUnsignedShort(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 31) return "writeUnsignedInt(writeBuffer, " + sizeInBits + ")";
if (sizeInBits <= 63) return "writeUnsignedLong(writeBuffer, " + sizeInBits + ")";
return "writeUnsignedBigInteger(writeBuffer, " + sizeInBits + ")";
case INT:
if (sizeInBits <= 8) return "writeSignedByte(writeBuffer, " + sizeInBits + ")";
Expand Down Expand Up @@ -534,16 +533,16 @@ public String getWriteBufferWriteMethodCall(String logicalName, SimpleTypeRefere
return "/*TODO: migrate me*/" + "writeBuffer.writeByte(\"" + logicalName + "\", ((Number) " + fieldName + ").byteValue()" + writerArgsString + ")";
case UINT:
IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
if (unsignedIntegerTypeReference.getSizeInBits() <= 4) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 7) {
return "/*TODO: migrate me*/" + "writeBuffer.writeUnsignedByte(\"" + logicalName + "\", " + unsignedIntegerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").byteValue()" + writerArgsString + ")";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 15) {
return "/*TODO: migrate me*/" + "writeBuffer.writeUnsignedShort(\"" + logicalName + "\", " + unsignedIntegerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").shortValue()" + writerArgsString + ")";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 31) {
return "/*TODO: migrate me*/" + "writeBuffer.writeUnsignedInt(\"" + logicalName + "\", " + unsignedIntegerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").intValue()" + writerArgsString + ")";
}
if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
if (unsignedIntegerTypeReference.getSizeInBits() <= 63) {
return "/*TODO: migrate me*/" + "writeBuffer.writeUnsignedLong(\"" + logicalName + "\", " + unsignedIntegerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").longValue()" + writerArgsString + ")";
}
return "/*TODO: migrate me*/" + "writeBuffer.writeUnsignedBigInteger(\"" + logicalName + "\", " + unsignedIntegerTypeReference.getSizeInBits() + ", (BigInteger) " + fieldName + "" + writerArgsString + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public class UIntTypeTest implements Message {
protected final byte ThreeField;
protected final short ByteField;
protected final int WordField;
protected final long WordPlusByteField;
protected final int WordPlusByteField;
protected final long DoubleIntField;
protected final BigInteger QuadIntField;

public UIntTypeTest(
byte ThreeField,
short ByteField,
int WordField,
long WordPlusByteField,
int WordPlusByteField,
long DoubleIntField,
BigInteger QuadIntField) {
super();
Expand All @@ -74,7 +74,7 @@ public int getWordField() {
return WordField;
}

public long getWordPlusByteField() {
public int getWordPlusByteField() {
return WordPlusByteField;
}

Expand All @@ -101,7 +101,7 @@ public void serialize(WriteBuffer writeBuffer) throws SerializationException {
writeSimpleField("WordField", WordField, writeUnsignedInt(writeBuffer, 16));

// Simple Field (WordPlusByteField)
writeSimpleField("WordPlusByteField", WordPlusByteField, writeUnsignedLong(writeBuffer, 24));
writeSimpleField("WordPlusByteField", WordPlusByteField, writeUnsignedInt(writeBuffer, 24));

// Simple Field (DoubleIntField)
writeSimpleField("DoubleIntField", DoubleIntField, writeUnsignedLong(writeBuffer, 32));
Expand Down Expand Up @@ -161,7 +161,7 @@ public static UIntTypeTest staticParse(ReadBuffer readBuffer) throws ParseExcept

int WordField = readSimpleField("WordField", readUnsignedInt(readBuffer, 16));

long WordPlusByteField = readSimpleField("WordPlusByteField", readUnsignedLong(readBuffer, 24));
int WordPlusByteField = readSimpleField("WordPlusByteField", readUnsignedInt(readBuffer, 24));

long DoubleIntField = readSimpleField("DoubleIntField", readUnsignedLong(readBuffer, 32));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static PlcValue staticParse(

// Reserved Field (Compartmentalized so the "reserved" variable can't leak)
{
short reserved = /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readUnsignedShort("", 7);
if (reserved != (short) 0x00) {
byte reserved = /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readUnsignedByte("", 7);
if (reserved != (byte) 0x00) {
LOGGER.info(
"Expected constant value " + 0x00 + " but got " + reserved + " for reserved field.");
}
Expand Down Expand Up @@ -275,8 +275,7 @@ public static void staticSerialize(
if (EvaluationHelper.equals(plcValueType, PlcValueType.BOOL)) { // BOOL
// Reserved Field
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeUnsignedShort(
"", 7, ((Number) (short) 0x00).shortValue());
/*TODO: migrate me*/ writeBuffer.writeUnsignedByte("", 7, ((Number) (byte) 0x00).byteValue());
// Simple Field (value)
boolean value = (boolean) _value.getBoolean();
/*TODO: migrate me*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public BACnetObjectType getObjectType() {
return (BACnetObjectType) (getPayload().getObjectType());
}

public long getInstanceNumber() {
return (long) (getPayload().getInstanceNumber());
public int getInstanceNumber() {
return (int) (getPayload().getInstanceNumber());
}

@Override
Expand All @@ -75,7 +75,7 @@ protected void serializeBACnetApplicationTagChild(WriteBuffer writeBuffer)
writeBuffer.writeVirtual("objectType", objectType);

// Virtual field (doesn't actually serialize anything, just makes the value available)
long instanceNumber = getInstanceNumber();
int instanceNumber = getInstanceNumber();
writeBuffer.writeVirtual("instanceNumber", instanceNumber);

writeBuffer.popContext("BACnetApplicationTagObjectIdentifier");
Expand Down Expand Up @@ -115,8 +115,7 @@ public static BACnetApplicationTagBuilder staticParseBACnetApplicationTagBuilder
() -> BACnetTagPayloadObjectIdentifier.staticParse(readBuffer), readBuffer));
BACnetObjectType objectType =
readVirtualField("objectType", BACnetObjectType.class, payload.getObjectType());
long instanceNumber =
readVirtualField("instanceNumber", long.class, payload.getInstanceNumber());
int instanceNumber = readVirtualField("instanceNumber", int.class, payload.getInstanceNumber());

readBuffer.closeContext("BACnetApplicationTagObjectIdentifier");
// Create the instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public BACnetObjectType getObjectType() {
return (BACnetObjectType) (getPayload().getObjectType());
}

public long getInstanceNumber() {
return (long) (getPayload().getInstanceNumber());
public int getInstanceNumber() {
return (int) (getPayload().getInstanceNumber());
}

@Override
Expand All @@ -82,7 +82,7 @@ protected void serializeBACnetContextTagChild(WriteBuffer writeBuffer)
writeBuffer.writeVirtual("objectType", objectType);

// Virtual field (doesn't actually serialize anything, just makes the value available)
long instanceNumber = getInstanceNumber();
int instanceNumber = getInstanceNumber();
writeBuffer.writeVirtual("instanceNumber", instanceNumber);

writeBuffer.popContext("BACnetContextTagObjectIdentifier");
Expand Down Expand Up @@ -123,8 +123,7 @@ public static BACnetContextTagBuilder staticParseBACnetContextTagBuilder(
() -> BACnetTagPayloadObjectIdentifier.staticParse(readBuffer), readBuffer));
BACnetObjectType objectType =
readVirtualField("objectType", BACnetObjectType.class, payload.getObjectType());
long instanceNumber =
readVirtualField("instanceNumber", long.class, payload.getInstanceNumber());
int instanceNumber = readVirtualField("instanceNumber", int.class, payload.getInstanceNumber());

readBuffer.closeContext("BACnetContextTagObjectIdentifier");
// Create the instance
Expand Down
Loading

0 comments on commit 8772f90

Please sign in to comment.