Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,33 +242,35 @@ public String getReadBufferReadMethodCall(SimpleTypeReference simpleTypeReferenc
}
case UINT: {
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
String endianness = integerTypeReference.isLittleEndian() ? ", true" : "";
if (integerTypeReference.getSizeInBits() <= 4) {
return "io.readUnsignedByte(" + integerTypeReference.getSizeInBits() + ")";
}
if (integerTypeReference.getSizeInBits() <= 8) {
return "io.readUnsignedShort(" + integerTypeReference.getSizeInBits() + ")";
return "io.readUnsignedShort(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 16) {
return "io.readUnsignedInt(" + integerTypeReference.getSizeInBits() + ")";
return "io.readUnsignedInt(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 32) {
return "io.readUnsignedLong(" + integerTypeReference.getSizeInBits() + ")";
return "io.readUnsignedLong(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
return "io.readUnsignedBigInteger(" + integerTypeReference.getSizeInBits() + ")";
}
case INT: {
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
String endianness = integerTypeReference.isLittleEndian() ? ", true" : "";
if (integerTypeReference.getSizeInBits() <= 8) {
return "io.readByte(" + integerTypeReference.getSizeInBits() + ")";
}
if (integerTypeReference.getSizeInBits() <= 16) {
return "io.readShort(" + integerTypeReference.getSizeInBits() + ")";
return "io.readShort(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 32) {
return "io.readInt(" + integerTypeReference.getSizeInBits() + ")";
return "io.readInt(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 64) {
return "io.readLong(" + integerTypeReference.getSizeInBits() + ")";
return "io.readLong(" + integerTypeReference.getSizeInBits() + endianness + ")";
}
return "io.readBigInteger(" + integerTypeReference.getSizeInBits() + ")";
}
Expand Down Expand Up @@ -301,33 +303,35 @@ public String getWriteBufferReadMethodCall(SimpleTypeReference simpleTypeReferen
}
case UINT: {
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
String endianness = integerTypeReference.isLittleEndian() ? ", true" : "";
if (integerTypeReference.getSizeInBits() <= 4) {
return "io.writeUnsignedByte(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").byteValue())";
}
if (integerTypeReference.getSizeInBits() <= 8) {
return "io.writeUnsignedShort(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").shortValue())";
return "io.writeUnsignedShort(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").shortValue()" + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 16) {
return "io.writeUnsignedInt(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").intValue())";
return "io.writeUnsignedInt(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").intValue()" + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 32) {
return "io.writeUnsignedLong(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").longValue())";
return "io.writeUnsignedLong(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").longValue()" + endianness + ")";
}
return "io.writeUnsignedBigInteger(" + integerTypeReference.getSizeInBits() + ", (BigInteger) " + fieldName + ")";
}
case INT: {
IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
String endianness = integerTypeReference.isLittleEndian() ? ", true" : "";
if (integerTypeReference.getSizeInBits() <= 8) {
return "io.writeByte(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").byteValue())";
}
if (integerTypeReference.getSizeInBits() <= 16) {
return "io.writeShort(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").shortValue())";
return "io.writeShort(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").shortValue()" + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 32) {
return "io.writeInt(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").intValue())";
return "io.writeInt(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").intValue()" + endianness + ")";
}
if (integerTypeReference.getSizeInBits() <= 64) {
return "io.writeLong(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").longValue())";
return "io.writeLong(" + integerTypeReference.getSizeInBits() + ", ((Number) " + fieldName + ").longValue() " + endianness + ")";
}
return "io.writeBigInteger(" + integerTypeReference.getSizeInBits() + ", BigInteger.valueOf( " + fieldName + "))";
}
Expand Down Expand Up @@ -359,6 +363,8 @@ public String getReadMethodName(SimpleTypeReference simpleTypeReference) {
languageTypeName = languageTypeName.substring(0, 1).toUpperCase() + languageTypeName.substring(1);
if(simpleTypeReference.getBaseType().equals(SimpleTypeReference.SimpleBaseType.UINT)) {
return "readUnsigned" + languageTypeName;
} else if(simpleTypeReference.getBaseType().equals(SimpleTypeReference.SimpleBaseType.UINT)) {
return "readUnsigned" + languageTypeName + "LE";
} else {
return "read" + languageTypeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import java.util.function.Supplier;

public class ${typeName}IO {

// data-io

private static final Logger LOGGER = LoggerFactory.getLogger(${typeName}IO.class);

public static PlcValue staticParse(ReadBuffer io<#if type.parserArguments?has_content>, <#list type.parserArguments as parserArgument>${helper.getLanguageTypeName(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public class ${typeName}IO implements <#if outputFlavor != "passive">MessageIO<$
public static ${typeName}<#if helper.isDiscriminatedType(type)>Builder</#if> staticParse(ReadBuffer io<#if type.parserArguments?has_content>, <#list type.parserArguments as parserArgument>${helper.getLanguageTypeName(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) throws ParseException {
int startPos = io.getPos();
int curPos;
<#list type.fields as field>

<#if type.littleEndian>// this type is declared as little endian, field order have been changed to match full bytes in big endian order.</#if>

<#list type.fieldsInOrder as field>
<#switch field.typeName>
<#case "array">

Expand Down Expand Up @@ -350,7 +353,9 @@ public class ${typeName}IO implements <#if outputFlavor != "passive">MessageIO<$
public static void staticSerialize(WriteBuffer io, ${typeName} _value<#if helper.getSerializerArguments(type.parserArguments)?has_content>, <#list helper.getSerializerArguments(type.parserArguments) as parserArgument>${helper.getLanguageTypeName(parserArgument.type, false)} ${parserArgument.name}<#sep>, </#sep></#list></#if>) throws ParseException {
int startPos = io.getPos();

<#list type.fields as field>
<#if type.littleEndian>// this type is declared as little endian, field order have been changed to match full bytes in big endian order.</#if>

<#list type.fieldsInOrder as field>
<#switch field.typeName>
<#case "array">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ complexTypeDefinition
;

complexType
: 'type' name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition*
| 'discriminatedType' name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition+
| 'enum' type=typeReference name=idExpression (LBRACKET params=argumentList RBRACKET)? enumValues=enumValueDefinition+
| 'dataIo' name=idExpression (LBRACKET params=argumentList RBRACKET)? dataIoTypeSwitch=dataIoDefinition
: 'type' (endianness=ENDIANNESS_LITERAL)? name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition*
| 'discriminatedType' (endianness=ENDIANNESS_LITERAL)? name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition+
| 'enum' (endianness=ENDIANNESS_LITERAL)? type=typeReference name=idExpression (LBRACKET params=argumentList RBRACKET)? enumValues=enumValueDefinition+
| 'dataIo' (endianness=ENDIANNESS_LITERAL)? name=idExpression (LBRACKET params=argumentList RBRACKET)? dataIoTypeSwitch=dataIoDefinition
;

fieldDefinition
Expand Down Expand Up @@ -138,8 +138,8 @@ caseStatement

dataType
: base='bit'
| base='int' size=INTEGER_LITERAL
| base='uint' size=INTEGER_LITERAL
| base='int' size=INTEGER_LITERAL (endianness=ENDIANNESS_LITERAL)?
| base='uint' size=INTEGER_LITERAL (endianness=ENDIANNESS_LITERAL)?
| base='float' exponent=INTEGER_LITERAL '.' mantissa=INTEGER_LITERAL
| base='ufloat' exponent=INTEGER_LITERAL '.' mantissa=INTEGER_LITERAL
/* For the following types the parsing/serialization has to be handled manually */
Expand All @@ -152,6 +152,10 @@ dataType
| base='dateTime'
;

ENDIANNESS_LITERAL
: 'big endian' | 'little endian'
;

Comment on lines +155 to +158

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this "LE" or "BE"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you want LE/BE instead of little endian and big endian, or next to i? I took longer notation as it is self descriptive. Making BE/LE option next to these isn't a problem as I extracted comparison to one place which works with antlr token regardless of its use context (type, field etc).

argument
: type=typeReference name=idExpression
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.plugins.codegenerator.types.definitions.Argument;
import org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.fields.*;
import org.apache.plc4x.plugins.codegenerator.types.references.IntegerTypeReference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class DefaultComplexTypeDefinition extends DefaultTypeDefinition implements ComplexTypeDefinition {

private final boolean isAbstract;
private final List<Field> fields;

public DefaultComplexTypeDefinition(String name, Argument[] parserArguments, String[] tags, boolean isAbstract, List<Field> fields) {
super(name, parserArguments, tags);
public DefaultComplexTypeDefinition(String name, Argument[] parserArguments, String[] tags, boolean littleEndian, boolean isAbstract, List<Field> fields) {
super(name, parserArguments, tags, littleEndian);
this.isAbstract = isAbstract;
this.fields = fields;
}
Expand Down Expand Up @@ -89,4 +92,40 @@ public List<PropertyField> getParentPropertyFields() {
return Collections.emptyList();
}

public List<Field> getFieldsInOrder() {
if (!isLittleEndian()) {
return getFields();
}

List<Field> ordered = new ArrayList<>();
List<Field> group = new ArrayList<>();
int groupSize = 0;
int length = this.fields.size();
for (int index = length; index > 0; index--) {
Field field = fields.get(index - 1);

if (field instanceof TypedField) {
TypedField typed = (TypedField) field;
if (typed.getType() instanceof IntegerTypeReference) {
IntegerTypeReference size = (IntegerTypeReference) typed.getType();
group.add(field);
groupSize += size.getSizeInBits();

// we can have odd sizes then its best to
if ((groupSize % 8) == 0) {
Collections.reverse(group);
ordered.addAll(group);
group.clear();
groupSize = 0;
}
} else {
ordered.add(field);
}
} else {
ordered.add(field);
}
}

return ordered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class DefaultDataIoTypeDefinition extends DefaultTypeDefinition implement

private final SwitchField switchField;

public DefaultDataIoTypeDefinition(String name, Argument[] parserArguments, String[] tags, SwitchField switchField) {
super(name, parserArguments, tags);
public DefaultDataIoTypeDefinition(String name, Argument[] parserArguments, String[] tags, boolean littleEndian, SwitchField switchField) {
super(name, parserArguments, tags, littleEndian);
this.switchField = switchField;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class DefaultDiscriminatedComplexTypeDefinition extends DefaultComplexTyp

private final String[] discriminatorValues;

public DefaultDiscriminatedComplexTypeDefinition(String name, Argument[] parserArguments, String[] tags, String[] discriminatorValues, List<Field> fields) {
super(name, parserArguments, tags, false, fields);
public DefaultDiscriminatedComplexTypeDefinition(String name, Argument[] parserArguments, String[] tags, boolean littleEndian, String[] discriminatorValues, List<Field> fields) {
super(name, parserArguments, tags, littleEndian, false, fields);
this.discriminatorValues = discriminatorValues;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class DefaultEnumTypeDefinition extends DefaultTypeDefinition implements
private final Map<String, TypeReference> constants;

public DefaultEnumTypeDefinition(String name, TypeReference type, EnumValue[] enumValues,
Argument[] constants, String[] tags) {
super(name, constants, tags);
Argument[] constants, String[] tags, boolean littleEndian) {
super(name, constants, tags, littleEndian);
this.type = type;
this.enumValues = enumValues;
this.constants = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public abstract class DefaultTypeDefinition {
private final String name;
private final Argument[] parserArguments;
private final String[] tags;
private final boolean littleEndian;
private TypeDefinition parentType;

public DefaultTypeDefinition(String name, Argument[] parserArguments, String[] tags) {
public DefaultTypeDefinition(String name, Argument[] parserArguments, String[] tags, boolean littleEndian) {
this.name = name;
this.parserArguments = parserArguments;
this.tags = tags;
this.littleEndian = littleEndian;
this.parentType = null;
}

Expand All @@ -53,6 +55,10 @@ public TypeDefinition getParentType() {
return parentType;
}

public boolean isLittleEndian() {
return littleEndian;
}

public void setParentType(TypeDefinition parentType) {
this.parentType = parentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ Licensed to the Apache Software Foundation (ASF) under one

public class DefaultIntegerTypeReference extends DefaultSimpleTypeReference implements IntegerTypeReference {

public DefaultIntegerTypeReference(SimpleBaseType baseType, int sizeInBits) {
private final boolean littleEndian;

public DefaultIntegerTypeReference(SimpleBaseType baseType, int sizeInBits, boolean littleEndian) {
super(baseType, sizeInBits);
this.littleEndian = littleEndian;
}

@Override
public boolean isLittleEndian() {
return littleEndian;
}
Comment on lines +33 to 36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider removing the "@OverRide" and leaving the build-tools repo unchanged ...


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one
public class DefaultSimpleVarLengthTypeReference extends DefaultIntegerTypeReference implements SimpleVarLengthTypeReference {

public DefaultSimpleVarLengthTypeReference(SimpleBaseType baseType) {
super(baseType, -1);
super(baseType, -1, false);
}

}
Loading