You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/01/13 11:30:12 UTC

[plc4x] branch next-gen-core-2 updated: - Made the "endianess" configurable - Inverted the way "big-endian"/"little-endian" was treated in the ReadBuffer and WriteBuffer - Fixed things this inversion broke

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch next-gen-core-2
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/next-gen-core-2 by this push:
     new baf44d9  - Made the "endianess" configurable - Inverted the way "big-endian"/"little-endian" was treated in the ReadBuffer and WriteBuffer - Fixed things this inversion broke
baf44d9 is described below

commit baf44d9f20d85d979a17ee49522000d7c45c5f7b
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Jan 13 12:30:01 2020 +0100

    - Made the "endianess" configurable
    - Inverted the way "big-endian"/"little-endian" was treated in the ReadBuffer and WriteBuffer
    - Fixed things this inversion broke
---
 .../src/test/resources/testsuite/AbEthTestsuite.xml   |  2 +-
 .../java/spi/GeneratedDriverByteToMessageCodec.java   | 10 ++++++----
 .../spi/connection/GeneratedProtocolMessageCodec.java |  9 ++-------
 .../spi/connection/SingleProtocolStackConfigurer.java | 19 +++++++++++++++----
 .../apache/plc4x/java/spi/generation/ReadBuffer.java  | 13 +++++++------
 .../apache/plc4x/java/spi/generation/WriteBuffer.java | 10 +++++-----
 .../simulator/server/s7/protocol/S7Step7Protocol.java |  2 +-
 .../configuration/PassiveBacNetIpConfiguration.java   |  8 +++++++-
 .../java/org/apache/plc4x/java/df1/util/DF1Utils.java |  8 ++++----
 .../src/test/resources/testsuite/Df1Testsuite.xml     |  8 ++++----
 .../test/resources/testsuite/KNXNetIPTestsuite.xml    |  2 +-
 11 files changed, 53 insertions(+), 38 deletions(-)

diff --git a/plc4j/drivers/ab-eth/src/test/resources/testsuite/AbEthTestsuite.xml b/plc4j/drivers/ab-eth/src/test/resources/testsuite/AbEthTestsuite.xml
index d7ab531..8856f44 100644
--- a/plc4j/drivers/ab-eth/src/test/resources/testsuite/AbEthTestsuite.xml
+++ b/plc4j/drivers/ab-eth/src/test/resources/testsuite/AbEthTestsuite.xml
@@ -18,7 +18,7 @@
   under the License.
   -->
 <test:testsuite xmlns:test="https://plc4x.apache.org/schemas/testsuite.xsd"
-                bigEndian="false">
+                bigEndian="true">
 
   <name>Allen-Bradley ETH</name>
 
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
index 9b37e11..5bd4ff1 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/GeneratedDriverByteToMessageCodec.java
@@ -35,16 +35,18 @@ public abstract class GeneratedDriverByteToMessageCodec<T extends Message> exten
 
     private static final Logger logger = LoggerFactory.getLogger(GeneratedDriverByteToMessageCodec.class);
 
-    private MessageIO<T, T> io;
+    private final boolean bigEndian;
+    private final MessageIO<T, T> io;
 
-    public GeneratedDriverByteToMessageCodec(MessageIO<T, T> io, Class<T> clazz) {
+    public GeneratedDriverByteToMessageCodec(MessageIO<T, T> io, Class<T> clazz, boolean bigEndian) {
         super(clazz);
         this.io = io;
+        this.bigEndian = bigEndian;
     }
 
     @Override
     protected void encode(ChannelHandlerContext ctx, T packet, ByteBuf byteBuf) throws Exception {
-        WriteBuffer buffer = new WriteBuffer(packet.getLengthInBytes());
+        WriteBuffer buffer = new WriteBuffer(packet.getLengthInBytes(), !bigEndian);
         io.serialize(buffer, packet);
         byteBuf.writeBytes(buffer.getData());
         logger.debug("Sending bytes to PLC for message {} as data {}", packet, Hex.encodeHexString(buffer.getData()));
@@ -64,7 +66,7 @@ public abstract class GeneratedDriverByteToMessageCodec<T extends Message> exten
             // Read the packet data into a new ReadBuffer
             byte[] bytes = new byte[packetSize];
             byteBuf.readBytes(bytes);
-            ReadBuffer readBuffer = new ReadBuffer(bytes);
+            ReadBuffer readBuffer = new ReadBuffer(bytes, !bigEndian);
 
             try {
                 // Parse the packet.
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java
index 8afc02e..103d748 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedProtocolMessageCodec.java
@@ -31,16 +31,11 @@ class GeneratedProtocolMessageCodec<BASE_PACKET_CLASS extends Message> extends G
     private final ToIntFunction<ByteBuf> packetSizeEstimator;
     private final Consumer<ByteBuf> corruptPackageRemover;
 
-    public GeneratedProtocolMessageCodec(Class<BASE_PACKET_CLASS> basePacketClass,
-                                         Parser<BASE_PACKET_CLASS> parser,
-                                         Serializer<BASE_PACKET_CLASS> serializer) {
-        this(basePacketClass, parser, serializer, null, null);
-    }
-
     public GeneratedProtocolMessageCodec(
         Class<BASE_PACKET_CLASS> basePacketClass,
         Parser<BASE_PACKET_CLASS> parser,
         Serializer<BASE_PACKET_CLASS> serializer,
+        boolean bigEndian,
         ToIntFunction<ByteBuf> packetSizeEstimator,
         Consumer<ByteBuf> corruptPackageRemover) {
         super(new MessageIO<BASE_PACKET_CLASS, BASE_PACKET_CLASS>() {
@@ -54,7 +49,7 @@ class GeneratedProtocolMessageCodec<BASE_PACKET_CLASS extends Message> extends G
                 serializer.serialize(io, value);
             }
 
-        }, basePacketClass);
+        }, basePacketClass, bigEndian);
         this.packetSizeEstimator = packetSizeEstimator;
         this.corruptPackageRemover = corruptPackageRemover;
     }
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java
index 2eaac31..e0324c6 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurer.java
@@ -40,6 +40,7 @@ import java.util.function.ToIntFunction;
 public class SingleProtocolStackConfigurer<BASE_PACKET_CLASS extends Message> implements ProtocolStackConfigurer<BASE_PACKET_CLASS> {
 
     private final Class<BASE_PACKET_CLASS> basePacketClass;
+    private boolean bigEndian = true;
     private final Class<? extends Plc4xProtocolBase<BASE_PACKET_CLASS>> protocolClass;
     private final Class<? extends ToIntFunction<ByteBuf>> packetSizeEstimatorClass;
     private final Class<? extends Consumer<ByteBuf>> corruptPacketRemoverClass;
@@ -49,10 +50,13 @@ public class SingleProtocolStackConfigurer<BASE_PACKET_CLASS extends Message> im
     }
 
     /** Only accessible via Builder */
-    SingleProtocolStackConfigurer(Class<BASE_PACKET_CLASS> basePacketClass, Class<? extends Plc4xProtocolBase<BASE_PACKET_CLASS>> protocol,
+    SingleProtocolStackConfigurer(Class<BASE_PACKET_CLASS> basePacketClass,
+                                  boolean bigEndian,
+                                  Class<? extends Plc4xProtocolBase<BASE_PACKET_CLASS>> protocol,
                                   Class<? extends ToIntFunction<ByteBuf>> packetSizeEstimatorClass,
                                   Class<? extends Consumer<ByteBuf>> corruptPacketRemoverClass) {
         this.basePacketClass = basePacketClass;
+        this.bigEndian = bigEndian;
         this.protocolClass = protocol;
         this.packetSizeEstimatorClass = packetSizeEstimatorClass;
         this.corruptPacketRemoverClass = corruptPacketRemoverClass;
@@ -60,7 +64,7 @@ public class SingleProtocolStackConfigurer<BASE_PACKET_CLASS extends Message> im
 
     private ChannelHandler getMessageCodec(Configuration configuration) {
         ReflectionBasedIo<BASE_PACKET_CLASS> io = new ReflectionBasedIo<>(basePacketClass);
-        return new GeneratedProtocolMessageCodec<>(basePacketClass, io, io,
+        return new GeneratedProtocolMessageCodec<>(basePacketClass, io, io, bigEndian,
             packetSizeEstimatorClass != null ? configure(configuration, createInstance(packetSizeEstimatorClass)) : null,
             corruptPacketRemoverClass != null ? configure(configuration, createInstance(corruptPacketRemoverClass)) : null);
     }
@@ -92,14 +96,20 @@ public class SingleProtocolStackConfigurer<BASE_PACKET_CLASS extends Message> im
     public static final class SingleProtocolStackBuilder<BASE_PACKET_CLASS extends Message> {
 
         private Class<BASE_PACKET_CLASS> basePacketClass;
+        private boolean bigEndian = true;
         private Class<? extends Plc4xProtocolBase<BASE_PACKET_CLASS>> protocol;
         private Class<? extends ToIntFunction<ByteBuf>> packetSizeEstimator;
         private Class<? extends Consumer<ByteBuf>> corruptPacketRemover;
 
-        SingleProtocolStackBuilder(Class<BASE_PACKET_CLASS> basePacketClass) {
+        public SingleProtocolStackBuilder(Class<BASE_PACKET_CLASS> basePacketClass) {
             this.basePacketClass = basePacketClass;
         }
 
+        public SingleProtocolStackBuilder<BASE_PACKET_CLASS> littleEndian() {
+            this.bigEndian = false;
+            return this;
+        }
+
         public SingleProtocolStackBuilder<BASE_PACKET_CLASS> withProtocol(Class<? extends Plc4xProtocolBase<BASE_PACKET_CLASS>> protocol) {
             this.protocol = protocol;
             return this;
@@ -117,7 +127,8 @@ public class SingleProtocolStackConfigurer<BASE_PACKET_CLASS extends Message> im
 
         public SingleProtocolStackConfigurer<BASE_PACKET_CLASS> build() {
             assert this.protocol != null;
-            return new SingleProtocolStackConfigurer<>(basePacketClass, protocol, packetSizeEstimator, corruptPacketRemover);
+            return new SingleProtocolStackConfigurer<>(
+                basePacketClass, bigEndian, protocol, packetSizeEstimator, corruptPacketRemover);
         }
 
     }
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
index 90eff15..32c1c68 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
@@ -33,7 +33,7 @@ public class ReadBuffer {
     private final long totalBytes;
 
     public ReadBuffer(byte[] input) {
-        this(input, true);
+        this(input, false);
     }
 
     public ReadBuffer(byte[] input, boolean littleEndian) {
@@ -97,6 +97,7 @@ public class ReadBuffer {
             throw new ParseException("unsigned short can only contain max 8 bits");
         }
         try {
+            // No need to flip here as we're only reading one byte.
             return bi.readShort(true, bitLength);
         } catch (IOException e) {
             throw new ParseException("Error reading", e);
@@ -111,7 +112,7 @@ public class ReadBuffer {
             throw new ParseException("unsigned int can only contain max 16 bits");
         }
         try {
-            if (!littleEndian) {
+            if (littleEndian) {
                 return Integer.reverseBytes(bi.readInt(true, bitLength)) >> 16;
             }
             return bi.readInt(true, bitLength);
@@ -128,7 +129,7 @@ public class ReadBuffer {
             throw new ParseException("unsigned long can only contain max 32 bits");
         }
         try {
-            if (!littleEndian) {
+            if (littleEndian) {
                 return Long.reverseBytes(bi.readLong(true, bitLength)) >> 32;
             }
             return bi.readLong(true, bitLength);
@@ -163,7 +164,7 @@ public class ReadBuffer {
             throw new ParseException("short can only contain max 16 bits");
         }
         try {
-            if (!littleEndian) {
+            if (littleEndian) {
                 return Short.reverseBytes(bi.readShort(false, bitLength));
             }
             return bi.readShort(false, bitLength);
@@ -180,7 +181,7 @@ public class ReadBuffer {
             throw new ParseException("int can only contain max 32 bits");
         }
         try {
-            if (!littleEndian) {
+            if (littleEndian) {
                 return Integer.reverseBytes(bi.readInt(false, bitLength));
             }
             return bi.readInt(false, bitLength);
@@ -197,7 +198,7 @@ public class ReadBuffer {
             throw new ParseException("long can only contain max 64 bits");
         }
         try {
-            if (!littleEndian) {
+            if (littleEndian) {
                 return Long.reverseBytes(bi.readLong(false, bitLength));
             }
             return bi.readLong(false, bitLength);
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/WriteBuffer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/WriteBuffer.java
index c4ac4df..2c3158f 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/WriteBuffer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/WriteBuffer.java
@@ -94,7 +94,7 @@ public class WriteBuffer {
             throw new ParseException("unsigned int can only contain max 16 bits");
         }
         try {
-            if(!littleEndian) {
+            if(littleEndian) {
                 value = Integer.reverseBytes(value) >> 16;
             }
             bo.writeInt(true, bitLength, value);
@@ -111,7 +111,7 @@ public class WriteBuffer {
             throw new ParseException("unsigned long can only contain max 32 bits");
         }
         try {
-            if(!littleEndian) {
+            if(littleEndian) {
                 value = Long.reverseBytes(value) >> 32;
             }
             bo.writeLong(true, bitLength, value);
@@ -146,7 +146,7 @@ public class WriteBuffer {
             throw new ParseException("short can only contain max 16 bits");
         }
         try {
-            if(!littleEndian) {
+            if(littleEndian) {
                 value = Short.reverseBytes(value);
             }
             bo.writeShort(false, bitLength, value);
@@ -163,7 +163,7 @@ public class WriteBuffer {
             throw new ParseException("int can only contain max 32 bits");
         }
         try {
-            if(!littleEndian) {
+            if(littleEndian) {
                 value = Integer.reverseBytes(value);
             }
             bo.writeInt(false, bitLength, value);
@@ -180,7 +180,7 @@ public class WriteBuffer {
             throw new ParseException("long can only contain max 64 bits");
         }
         try {
-            if(!littleEndian) {
+            if(littleEndian) {
                 value = Long.reverseBytes(value);
             }
             bo.writeLong(false, bitLength, value);
diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
index 5fb548d..fcabfa3 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/protocol/S7Step7Protocol.java
@@ -50,7 +50,7 @@ public class S7Step7Protocol extends GeneratedDriverByteToMessageCodec<TPKTPacke
                     throw new ParseException("Error serializing message", e);
                 }
             }
-        }, TPKTPacket.class);
+        }, TPKTPacket.class, true);
     }
 
     @Override
diff --git a/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/configuration/PassiveBacNetIpConfiguration.java b/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/configuration/PassiveBacNetIpConfiguration.java
index ce1f43e..f42e4b6 100644
--- a/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/configuration/PassiveBacNetIpConfiguration.java
+++ b/sandbox/test-java-bacnetip-driver/src/main/java/org/apache/plc4x/java/bacnetip/configuration/PassiveBacNetIpConfiguration.java
@@ -19,13 +19,19 @@ under the License.
 package org.apache.plc4x.java.bacnetip.configuration;
 
 import org.apache.plc4x.java.spi.configuration.Configuration;
+import org.apache.plc4x.java.transport.pcap.PcapTransportConfiguration;
 import org.apache.plc4x.java.transport.rawsocket.RawSocketTransportConfiguration;
 
-public class PassiveBacNetIpConfiguration implements Configuration, RawSocketTransportConfiguration {
+public class PassiveBacNetIpConfiguration implements Configuration, RawSocketTransportConfiguration, PcapTransportConfiguration {
 
     @Override
     public Integer getProtocolId() {
         return null;
     }
 
+    @Override
+    public float getReplaySpeedFactor() {
+        return 0;
+    }
+
 }
diff --git a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/util/DF1Utils.java b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/util/DF1Utils.java
index 023d0e5..81f2e2f 100644
--- a/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/util/DF1Utils.java
+++ b/sandbox/test-java-df1-driver/src/main/java/org/apache/plc4x/java/df1/util/DF1Utils.java
@@ -27,7 +27,7 @@ import org.apache.plc4x.java.spi.generation.WriteBuffer;
 
 public class DF1Utils {
 
-    public static short crcCheck(Object... args) {
+    public static int crcCheck(Object... args) {
         short destinationAddress = (short) args[0];
         short sourceAddress = (short) args[1];
         DF1Command command = (DF1Command) args[2];
@@ -48,7 +48,7 @@ public class DF1Utils {
                 writeBuffer.writeUnsignedShort(8, (byte) 0x03);
 
                 byte[] data = writeBuffer.getData();
-                return calculateCRC(data);
+                return calculateCRC(data) & 0xFFFF;
 
             } catch (ParseException e) {
                 throw new RuntimeException("Something went wrong during the CRC check", e);
@@ -77,7 +77,7 @@ public class DF1Utils {
                 writeBuffer.writeUnsignedShort(8, (byte) 0x03);
 
                 byte[] data = writeBuffer.getData();
-                return calculateCRC(data);
+                return calculateCRC(data) & 0xFFFF;
 
             } catch (ParseException e) {
                 throw new RuntimeException("Something went wrong during the CRC check", e);
@@ -153,6 +153,6 @@ public class DF1Utils {
                     tmp = tmp >> 1;
                 }
         }
-        return (short)tmp;
+        return Short.reverseBytes((short) tmp);
     }
 }
diff --git a/sandbox/test-java-df1-driver/src/test/resources/testsuite/Df1Testsuite.xml b/sandbox/test-java-df1-driver/src/test/resources/testsuite/Df1Testsuite.xml
index 27dcbec..92ac806 100644
--- a/sandbox/test-java-df1-driver/src/test/resources/testsuite/Df1Testsuite.xml
+++ b/sandbox/test-java-df1-driver/src/test/resources/testsuite/Df1Testsuite.xml
@@ -32,8 +32,8 @@
         <sourceAddress>0</sourceAddress>
         <command className="org.apache.plc4x.java.df1.readwrite.DF1UnprotectedReadRequest">
           <status>0</status>
-          <transactionCounter>1</transactionCounter>
-          <address>17</address>
+          <transactionCounter>256</transactionCounter>
+          <address>4352</address>
           <size>2</size>
         </command>
       </DF1SymbolMessageFrame>
@@ -50,7 +50,7 @@
         <sourceAddress>9</sourceAddress>
         <command className="org.apache.plc4x.java.df1.readwrite.DF1UnprotectedReadResponse">
           <status>0</status>
-          <transactionCounter>1</transactionCounter>
+          <transactionCounter>256</transactionCounter>
           <data>
             <data>255</data>
             <data>255</data>
@@ -70,7 +70,7 @@
         <sourceAddress>9</sourceAddress>
         <command className="org.apache.plc4x.java.df1.readwrite.DF1UnprotectedReadResponse">
           <status>0</status>
-          <transactionCounter>1</transactionCounter>
+          <transactionCounter>256</transactionCounter>
           <data>
             <data>16</data>
             <data>255</data>
diff --git a/sandbox/test-java-knxnetip-driver/src/test/resources/testsuite/KNXNetIPTestsuite.xml b/sandbox/test-java-knxnetip-driver/src/test/resources/testsuite/KNXNetIPTestsuite.xml
index 10fb85c..1966a8f 100644
--- a/sandbox/test-java-knxnetip-driver/src/test/resources/testsuite/KNXNetIPTestsuite.xml
+++ b/sandbox/test-java-knxnetip-driver/src/test/resources/testsuite/KNXNetIPTestsuite.xml
@@ -17,7 +17,7 @@
   specific language governing permissions and limitations
   under the License.
   -->
-<test:testsuite xmlns:test="https://plc4x.apache.org/schemas/testsuite.xsd">
+<test:testsuite xmlns:test="https://plc4x.apache.org/schemas/testsuite.xsd" bigEndian="true">
 
   <name>KNXNet/IP</name>