You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jm...@apache.org on 2018/01/06 00:21:00 UTC

[incubator-plc4x] branch master updated (0a22610 -> 4383c38)

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

jmclean pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git.


    from 0a22610  PLC4X-22: temporary disable camel BOM due to long build-time on site:site
     new ff882df  fix up switch and endless loops
     new 04245aa  refactor to reduce complexity and fix endless loops
     new 4383c38  log exception

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../plc4x/java/s7/netty/Plc4XS7Protocol.java       |  26 ++---
 .../org/apache/plc4x/java/s7/netty/S7Protocol.java | 110 +++++++++++----------
 .../apache/plc4x/java/s7/S7PlcReaderSample.java    |   2 +-
 3 files changed, 76 insertions(+), 62 deletions(-)

-- 
To stop receiving notification emails like this one, please contact
['"commits@plc4x.apache.org" <co...@plc4x.apache.org>'].

[incubator-plc4x] 02/03: refactor to reduce complexity and fix endless loops

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jmclean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 04245aa62de2545be239c3945fede4abf60d16d5
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Sat Jan 6 11:20:33 2018 +1100

    refactor to reduce complexity and fix endless loops
---
 .../org/apache/plc4x/java/s7/netty/S7Protocol.java | 110 +++++++++++----------
 1 file changed, 60 insertions(+), 50 deletions(-)

diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
index 5534d74..585173f 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/S7Protocol.java
@@ -104,44 +104,10 @@ public class S7Protocol extends MessageToMessageCodec<IsoTPMessage, S7Message> {
             switch (s7Parameter.getType()) {
                 case READ_VAR:
                 case WRITE_VAR:
-                    VarParameter varParameter = (VarParameter) s7Parameter;
-                    List<VarParameterItem> items = varParameter.getItems();
-                    // ReadRequestItem count (Read one variable at a time)
-                    buf.writeByte((byte) items.size());
-                    for (VarParameterItem item : items) {
-                        VariableAddressingMode addressMode = item.getAddressingMode();
-                        if (addressMode == VariableAddressingMode.S7ANY) {
-                            S7AnyVarParameterItem s7AnyRequestItem = (S7AnyVarParameterItem) item;
-                            buf.writeByte(s7AnyRequestItem.getSpecificationType().getCode());
-                            // Length of this item (excluding spec type and length)
-                            buf.writeByte((byte) 0x0a);
-                            buf.writeByte(s7AnyRequestItem.getAddressingMode().getCode());
-                            buf.writeByte(s7AnyRequestItem.getTransportSize().getCode());
-                            buf.writeShort(s7AnyRequestItem.getNumElements());
-                            buf.writeShort(s7AnyRequestItem.getDataBlockNumber());
-                            buf.writeByte(s7AnyRequestItem.getMemoryArea().getCode());
-                            // A S7 address is 3 bytes long. Unfortunately the byte-offset is NOT located in
-                            // byte 1 and byte 2 and the bit offset in byte 3. Siemens used the last 3 bits of
-                            // byte 3 for the bit-offset and the remaining 5 bits of byte 3 to contain the lowest
-                            // 5 bits of the byte-offset. The highest 5 bits of byte 1 are probably left unused
-                            // for future extensions.
-                            buf.writeShort((short) (s7AnyRequestItem.getByteOffset() >> 5));
-                            buf.writeByte((byte) ((
-                                    (s7AnyRequestItem.getByteOffset() & 0x1F) << 3)
-                                    | (s7AnyRequestItem.getBitOffset() & 0x07)));
-                        } else {
-                            logger.error("writing this item type not implemented");
-                            return;
-                        }
-                    }
+                    encodeReadWriteVar(buf, (VarParameter) s7Parameter);
                     break;
                 case SETUP_COMMUNICATION:
-                    SetupCommunicationParameter setupCommunication = (SetupCommunicationParameter) s7Parameter;
-                    // Reserved (is always constant 0x00)
-                    buf.writeByte((byte) 0x00);
-                    buf.writeShort(setupCommunication.getMaxAmqCaller());
-                    buf.writeShort(setupCommunication.getMaxAmqCallee());
-                    buf.writeShort(setupCommunication.getPduLength());
+                    encodeSetupCommunication(buf, (SetupCommunicationParameter) s7Parameter);
                     break;
                 default:
                     logger.error("writing this parameter type not implemented");
@@ -149,19 +115,17 @@ public class S7Protocol extends MessageToMessageCodec<IsoTPMessage, S7Message> {
             }
         }
 
-        if(!in.getPayloads().isEmpty()) {
+        if (!in.getPayloads().isEmpty()) {
             for (S7Payload payload : in.getPayloads()) {
-                switch (payload.getType()) {
-                    case READ_VAR:
-                    case WRITE_VAR:
-                        VarPayload varPayload = (VarPayload) payload;
-                        for (VarPayloadItem payloadItem : varPayload.getPayloadItems()) {
-                            buf.writeByte(payloadItem.getReturnCode().getCode());
-                            buf.writeByte(payloadItem.getDataTransportSize().getCode());
-                            buf.writeShort(payloadItem.getData().length);
-                            buf.writeBytes(payloadItem.getData());
-                        }
-                        break;
+                ParameterType parameterType = payload.getType();
+                if (parameterType == ParameterType.READ_VAR || parameterType == ParameterType.WRITE_VAR) {
+                    VarPayload varPayload = (VarPayload) payload;
+                    for (VarPayloadItem payloadItem : varPayload.getPayloadItems()) {
+                        buf.writeByte(payloadItem.getReturnCode().getCode());
+                        buf.writeByte(payloadItem.getDataTransportSize().getCode());
+                        buf.writeShort(payloadItem.getData().length);
+                        buf.writeBytes(payloadItem.getData());
+                    }
                 }
             }
         }
@@ -169,6 +133,48 @@ public class S7Protocol extends MessageToMessageCodec<IsoTPMessage, S7Message> {
         out.add(new DataTpdu(true, (byte) 1, Collections.emptyList(), buf));
     }
 
+    private void encodeSetupCommunication(ByteBuf buf, SetupCommunicationParameter s7Parameter) {
+        // Reserved (is always constant 0x00)
+        buf.writeByte((byte) 0x00);
+        buf.writeShort(s7Parameter.getMaxAmqCaller());
+        buf.writeShort(s7Parameter.getMaxAmqCallee());
+        buf.writeShort(s7Parameter.getPduLength());
+    }
+
+    private void encodeReadWriteVar(ByteBuf buf, VarParameter s7Parameter) {
+        List<VarParameterItem> items = s7Parameter.getItems();
+        // ReadRequestItem count (Read one variable at a time)
+        buf.writeByte((byte) items.size());
+        for (VarParameterItem item : items) {
+            VariableAddressingMode addressMode = item.getAddressingMode();
+            if (addressMode == VariableAddressingMode.S7ANY) {
+                encodeS7AnyParameterItem(buf, (S7AnyVarParameterItem) item);
+            } else {
+                logger.error("writing this item type not implemented");
+            }
+        }
+    }
+
+    private void encodeS7AnyParameterItem(ByteBuf buf, S7AnyVarParameterItem s7AnyRequestItem) {
+        buf.writeByte(s7AnyRequestItem.getSpecificationType().getCode());
+        // Length of this item (excluding spec type and length)
+        buf.writeByte((byte) 0x0a);
+        buf.writeByte(s7AnyRequestItem.getAddressingMode().getCode());
+        buf.writeByte(s7AnyRequestItem.getTransportSize().getCode());
+        buf.writeShort(s7AnyRequestItem.getNumElements());
+        buf.writeShort(s7AnyRequestItem.getDataBlockNumber());
+        buf.writeByte(s7AnyRequestItem.getMemoryArea().getCode());
+        // A S7 address is 3 bytes long. Unfortunately the byte-offset is NOT located in
+        // byte 1 and byte 2 and the bit offset in byte 3. Siemens used the last 3 bits of
+        // byte 3 for the bit-offset and the remaining 5 bits of byte 3 to contain the lowest
+        // 5 bits of the byte-offset. The highest 5 bits of byte 1 are probably left unused
+        // for future extensions.
+        buf.writeShort((short) (s7AnyRequestItem.getByteOffset() >> 5));
+        buf.writeByte((byte) ((
+                (s7AnyRequestItem.getByteOffset() & 0x1F) << 3)
+                | (s7AnyRequestItem.getBitOffset() & 0x07)));
+    }
+
     @Override
     protected void decode(ChannelHandlerContext ctx, IsoTPMessage in, List<Object> out) throws Exception {
         if (logger.isTraceEnabled()) {
@@ -200,7 +206,9 @@ public class S7Protocol extends MessageToMessageCodec<IsoTPMessage, S7Message> {
             List<S7Parameter> s7Parameters = new LinkedList<>();
             SetupCommunicationParameter setupCommunicationParameter = null;
             VarParameter readWriteVarParameter = null;
-            for (int i = 0; i < headerParametersLength; ) {
+            int i = 0;
+
+            while (i < headerParametersLength) {
                 S7Parameter parameter = parseParameter(userData, isResponse, headerParametersLength - i);
                 s7Parameters.add(parameter);
                 if (parameter instanceof SetupCommunicationParameter) {
@@ -217,7 +225,9 @@ public class S7Protocol extends MessageToMessageCodec<IsoTPMessage, S7Message> {
             List<S7Payload> s7Payloads = new LinkedList<>();
             if(readWriteVarParameter != null) {
                 List<VarPayloadItem> payloadItems = new LinkedList<>();
-                for (int i = 0; i < userDataLength; ) {
+                i = 0;
+
+                while (i < userDataLength) {
                     DataTransportErrorCode dataTransportErrorCode = DataTransportErrorCode.valueOf(userData.readByte());
                     // This is a response to a WRITE_VAR request (It only contains the return code for every sent item.
                     if ((readWriteVarParameter.getType() == ParameterType.WRITE_VAR) &&

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.

[incubator-plc4x] 03/03: log exception

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jmclean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 4383c3883340e84c446d8810c8ab957d2fe93cdf
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Sat Jan 6 11:20:52 2018 +1100

    log exception
---
 .../s7/src/test/java/org/apache/plc4x/java/s7/S7PlcReaderSample.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/S7PlcReaderSample.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/S7PlcReaderSample.java
index 9415179..0780172 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/S7PlcReaderSample.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/S7PlcReaderSample.java
@@ -90,7 +90,7 @@ public class S7PlcReaderSample {
         }
         // Catch any exception or the application won't be able to finish if something goes wrong.
         catch (Exception e) {
-            e.printStackTrace();
+            logger.error("S7 PLC reader sample", e);
         }
         // The application would cleanly terminate after several seconds ... this just speeds things up.
         System.exit(0);

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.

[incubator-plc4x] 01/03: fix up switch and endless loops

Posted by jm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jmclean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit ff882df35e2487502d7865d5f546ed235e225547
Author: Justin Mclean <jm...@apache.org>
AuthorDate: Sat Jan 6 11:20:00 2018 +1100

    fix up switch and endless loops
---
 .../plc4x/java/s7/netty/Plc4XS7Protocol.java       | 26 +++++++++++++---------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
index 6a75419..ed69c1f 100644
--- a/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
+++ b/plc4j/protocols/s7/src/main/java/org/apache/plc4x/java/s7/netty/Plc4XS7Protocol.java
@@ -341,22 +341,26 @@ public class Plc4XS7Protocol extends MessageToMessageCodec<S7Message, PlcRequest
     ////////////////////////////////////////////////////////////////////////////////
 
     private ResponseCode decodeResponseCode(DataTransportErrorCode dataTransportErrorCode) {
-        if(dataTransportErrorCode != null) {
-            switch (dataTransportErrorCode) {
-                case OK:
-                    return ResponseCode.OK;
-                case NOT_FOUND:
-                    return ResponseCode.NOT_FOUND;
-                case INVALID_ADDRESS:
-                    return ResponseCode.INVALID_ADDRESS;
-            }
+        if (dataTransportErrorCode == null) {
+            return ResponseCode.INTERNAL_ERROR;
+        }
+        switch (dataTransportErrorCode) {
+            case OK:
+                return ResponseCode.OK;
+            case NOT_FOUND:
+                return ResponseCode.NOT_FOUND;
+            case INVALID_ADDRESS:
+                return ResponseCode.INVALID_ADDRESS;
+            default:
+                return ResponseCode.INTERNAL_ERROR;
         }
-        return ResponseCode.INTERNAL_ERROR;
     }
 
     private List<Object> decodeData(Class<?> datatype, byte[] s7Data) throws PlcProtocolException {
         List<Object> result = new LinkedList<>();
-        for(int i = 0; i < s7Data.length;) {
+        int i = 0;
+        
+        while (i < s7Data.length) {
             if (datatype == Boolean.class) {
                 result.add((s7Data[i] & 0x01) == 0x01);
                 i+=1;

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.