Skip to content

Commit

Permalink
fix(plc4j/profinet): Update for OctetString and F_MESSAGETRAILER4BYTE…
Browse files Browse the repository at this point in the history
… datatypes
  • Loading branch information
hutcheb committed Mar 31, 2023
1 parent 443b8e2 commit 29e6fc1
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 3 deletions.
Expand Up @@ -72,6 +72,36 @@ public static PlcValue staticParse(
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // USINT

// Simple Field (value)
Short value = /*TODO: migrate me*/ /*TODO: migrate me*/ readBuffer.readUnsignedShort("", 8);

return new PlcUSINT(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)) { // List
// Array field (value)
// Count array
if (numberOfValues > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ (numberOfValues)
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) numberOfValues;
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcUINT(
(Short) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readUnsignedShort("", 8)));
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // BYTE
Expand Down Expand Up @@ -479,6 +509,146 @@ public static PlcValue staticParse(
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR

// Simple Field (value)
String value = /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 8, WithOption.WithEncoding("UTF-8"));

return new PlcCHAR(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)) { // List
// Array field (value)
// Count array
if (numberOfValues > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ (numberOfValues)
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) numberOfValues;
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcSTRING(
(String) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 8, WithOption.WithEncoding("UTF-8"))));
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR

// Simple Field (value)
String value = /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 16, WithOption.WithEncoding("UTF-16"));

return new PlcCHAR(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)) { // List
// Array field (value)
// Count array
if (numberOfValues > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ (numberOfValues)
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) numberOfValues;
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcSTRING(
(String) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 16, WithOption.WithEncoding("UTF-16"))));
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR

// Simple Field (value)
String value = /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 8, WithOption.WithEncoding("UTF-8"));

return new PlcCHAR(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)) { // List
// Array field (value)
// Count array
if (numberOfValues > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ (numberOfValues)
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) numberOfValues;
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcSTRING(
(String) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readString("", 8, WithOption.WithEncoding("UTF-8"))));
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // List
// Array field (value)
// Count array
if ((4) * (8) > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ ((4) * (8))
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) (4) * (8);
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcUINT(
(Short) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readUnsignedShort("", 8)));
}
}

return new PlcList(value);
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)) { // List
// Array field (value)
// Count array
if ((numberOfValues) * (32) > Integer.MAX_VALUE) {
throw new ParseException(
"Array count of "
+ ((numberOfValues) * (32))
+ " exceeds the maximum allowed count of "
+ Integer.MAX_VALUE);
}
List<PlcValue> value;
{
int itemCount = (int) (numberOfValues) * (32);
value = new LinkedList<>();
for (int curItem = 0; curItem < itemCount; curItem++) {
value.add(
new PlcUINT(
(Short) /*TODO: migrate me*/ /*TODO: migrate me*/
readBuffer.readUnsignedShort("", 8)));
}
}

return new PlcList(value);
}
return null;
Expand Down Expand Up @@ -512,6 +682,21 @@ public static void staticSerialize(
/*TODO: migrate me*/ writeBuffer.writeBit("", (boolean) (value));
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // USINT
// Simple Field (value)
short value = (short) _value.getShort();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeUnsignedShort("", 8, ((Number) (value)).shortValue());
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
Short value = (Short) val.getShort();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeUnsignedShort("", 8, ((Number) (value)).shortValue());
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // BYTE
// Simple Field (value)
Expand Down Expand Up @@ -725,6 +910,76 @@ public static void staticSerialize(
/*TODO: migrate me*/ writeBuffer.writeString(
"", 16, (String) (value), WithOption.WithEncoding("UTF-16"));
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
String value = (String) _value.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 8, (String) (value), WithOption.WithEncoding("UTF-8"));
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
String value = (String) val.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 8, (String) (value), WithOption.WithEncoding("UTF-8"));
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
String value = (String) _value.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 16, (String) (value), WithOption.WithEncoding("UTF-16"));
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
String value = (String) val.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 16, (String) (value), WithOption.WithEncoding("UTF-16"));
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
String value = (String) _value.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 8, (String) (value), WithOption.WithEncoding("UTF-8"));
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
String value = (String) val.getString();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeString(
"", 8, (String) (value), WithOption.WithEncoding("UTF-8"));
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
Short value = (Short) val.getShort();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeUnsignedShort("", 8, ((Number) (value)).shortValue());
}

} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)) { // List
PlcList values = (PlcList) _value;

for (PlcValue val : ((List<PlcValue>) values.getList())) {
Short value = (Short) val.getShort();
/*TODO: migrate me*/
/*TODO: migrate me*/ writeBuffer.writeUnsignedShort("", 8, ((Number) (value)).shortValue());
}
}
}

Expand All @@ -743,6 +998,13 @@ public static int getLengthInBits(
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.BOOL)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 1;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // USINT
// Simple Field (value)
sizeInBits += 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.OCTETSTRING)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // BYTE
// Simple Field (value)
Expand Down Expand Up @@ -843,6 +1105,34 @@ public static int getLengthInBits(
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WCHAR)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 16;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
sizeInBits += 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.UNICODESTRING8)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
sizeInBits += 16;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.WSTRING)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 16;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)
&& EvaluationHelper.equals(numberOfValues, 1)) { // CHAR
// Simple Field (value)
sizeInBits += 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.VISIBLESTRING)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)
&& EvaluationHelper.equals(numberOfValues, 1)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 8;
} else if (EvaluationHelper.equals(dataType, ProfinetDataType.F_MESSAGETRAILER4BYTE)) { // List
PlcList values = (PlcList) _value;
sizeInBits += values.getList().size() * 8;
}
return sizeInBits;
}
Expand Down
Expand Up @@ -51,8 +51,14 @@ public enum ProfinetDataType {
LDATE_AND_TIME((short) 23, (short) 8, (String) "LDATE_AND_TIME"),
CHAR((short) 24, (short) 1, (String) "CHAR"),
WCHAR((short) 25, (short) 2, (String) "WCHAR"),
STRING((short) 26, (short) 1, (String) "STRING"),
WSTRING((short) 27, (short) 2, (String) "WSTRING");
UNICODESTRING8((short) 26, (short) 1, (String) "UNICODESTRING8"),
WSTRING((short) 27, (short) 2, (String) "WSTRING"),
VISIBLESTRING((short) 28, (short) 1, (String) "VISIBLESTRING"),
F_MESSAGETRAILER4BYTE((short) 29, (short) 4, (String) "F_MESSAGETRAILER4BYTE"),
TIMESTAMP((short) 30, (short) 12, (String) "TIMESTAMP"),
TIMESTAMPDIFFERENCE((short) 31, (short) 12, (String) "TIMESTAMPDIFFERENCE"),
TIMESTAMPDIFFERENCESHORT((short) 32, (short) 8, (String) "TIMESTAMPDIFFERENCESHORT"),
OCTETSTRING((short) 33, (short) 1, (String) "OCTETSTRING");
private static final Map<Short, ProfinetDataType> map;

static {
Expand Down
4 changes: 4 additions & 0 deletions plc4j/drivers/profinet/src/test/resources/gsdml.xml
Expand Up @@ -212,6 +212,10 @@
</DataItem>
<DataItem DataType="Float32" TextId="PLC4X_INPUT_MODULE_INFO_FLOAT"/>
</Input>
<Input Consistency="None">
<DataItem Length="15" TextId="Inputs" DataType="OctetString" UseAsBits="true" />
<DataItem DataType="Float32" TextId="PLC4X_INPUT_MODULE_INFO_FLOAT"/>
</Input>
</IOData>
<ModuleInfo>
<Name TextId="PLC4X_VIRTUAL_INPUT_MODULE_NAME"/>
Expand Down

0 comments on commit 29e6fc1

Please sign in to comment.