You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cg...@apache.org on 2024/02/14 16:15:53 UTC

(plc4x) branch develop updated: Corrects the call to generate events that were commented. Tests again… (#1400)

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

cgarcia pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new bc51fe49d9 Corrects the call to generate events that were commented. Tests again… (#1400)
bc51fe49d9 is described below

commit bc51fe49d9fdb0bb9371e7e314ded2d3e148d4ef
Author: César José García León <ce...@gmail.com>
AuthorDate: Wed Feb 14 12:15:41 2024 -0400

    Corrects the call to generate events that were commented. Tests again… (#1400)
    
    * Corrects the call to generate events that were commented. Tests against S7-400.
    
    * Code cleaning is done.
    
    ---------
    
    Co-authored-by: Cesar Garcia <ce...@ceos.com.ve>
---
 .../java/s7/readwrite/protocol/S7HMuxImpl.java     |  3 +-
 .../readwrite/protocol/S7ProtocolEventLogic.java   | 26 +++---
 .../s7/readwrite/protocol/S7ProtocolLogic.java     | 94 +++++++++++++++-------
 3 files changed, 74 insertions(+), 49 deletions(-)

diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java
index f815dc470b..5742647ea9 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java
@@ -127,7 +127,6 @@ public class S7HMuxImpl extends MessageToMessageCodec<ByteBuf, ByteBuf> implemen
      */
     @Override
     protected void encode(ChannelHandlerContext ctx, ByteBuf outbb, List<Object> list) {
-        logger.debug("ENCODE: " + outbb.toString());
         if ((embed_ctx == null) && (ctx.channel() instanceof EmbeddedChannel)) embed_ctx = ctx;
         if ((tcp_channel != null) && (embed_ctx == ctx)) {
             tcp_channel.writeAndFlush(outbb.copy());
@@ -142,7 +141,7 @@ public class S7HMuxImpl extends MessageToMessageCodec<ByteBuf, ByteBuf> implemen
      * the pipeline of the channel "embeded_channel"
      */
     @Override
-    protected void decode(ChannelHandlerContext ctx, ByteBuf inbb, List<Object> list) throws Exception {
+    protected void decode(ChannelHandlerContext ctx, ByteBuf inbb, List<Object> list) throws Exception {     
         embed_ctx.fireChannelRead(inbb.copy());
     }
 
diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolEventLogic.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolEventLogic.java
index 2da531d1b0..0191660ceb 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolEventLogic.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolEventLogic.java
@@ -129,25 +129,17 @@ public class S7ProtocolEventLogic implements PlcSubscriber {
             while (!shutdown) {
                 try {
                     S7Event s7Event = eventQueue.poll(DEFAULT_DELAY, TimeUnit.MILLISECONDS);
-                    if (s7Event != null) {
-                        if (s7Event instanceof S7ParameterModeTransition) {
-                            S7ModeEvent modeEvent = new S7ModeEvent((S7ParameterModeTransition) s7Event);
-                            dispatchQueue.add(modeEvent);
-                        } else if (s7Event instanceof S7PayloadDiagnosticMessage) {
-                            S7PayloadDiagnosticMessage msg = (S7PayloadDiagnosticMessage) s7Event;
-                            if ((msg.getEventId() >= 0x0A000) & (msg.getEventId() <= 0x0BFFF)) {
-                                S7UserEvent userEvent = new S7UserEvent(msg);
-                                dispatchQueue.add(userEvent);
-                            } else {
-                                S7SysEvent sysEvent = new S7SysEvent(msg);
-                                dispatchQueue.add(sysEvent);
-                            }
+                    if ((s7Event != null) && (dispatchQueue.remainingCapacity() > 1)) {
+                        if (s7Event instanceof S7ModeEvent) {
+                            dispatchQueue.add(s7Event);
+                        } else if (s7Event instanceof S7UserEvent) {
+                            dispatchQueue.add(s7Event);                            
+                        } else if (s7Event instanceof S7SysEvent) {                            
+                            dispatchQueue.add(s7Event);                            
                         } else if (s7Event instanceof S7CyclicEvent) {
-                            S7CyclicEvent cyclicEvent = (S7CyclicEvent) s7Event;
-                            dispatchQueue.add(cyclicEvent);
+                            dispatchQueue.add(s7Event);
                         } else {
-                            S7AlarmEvent alarmEvent = new S7AlarmEvent(s7Event);
-                            dispatchQueue.add(alarmEvent);
+                            dispatchQueue.add(s7Event);
                         }
                     }
                 } catch (InterruptedException ex) {
diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
index d02be979b7..ee64e9b0ec 100644
--- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
+++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java
@@ -63,6 +63,10 @@ import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+import org.apache.plc4x.java.s7.events.S7AlarmEvent;
+import org.apache.plc4x.java.s7.events.S7ModeEvent;
+import org.apache.plc4x.java.s7.events.S7SysEvent;
+import org.apache.plc4x.java.s7.events.S7UserEvent;
 
 /**
  * The S7 Protocol states that there can not be more then {min(maxAmqCaller, maxAmqCallee} "ongoing" requests.
@@ -1448,32 +1452,62 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
     }
 
     /**
-     * This method is only called when there is no Response Handler.
+     * DECODE:
+     * This method is called when there is no handler for the message. 
+     * By default it must correspond to asynchronous events, which if so, 
+     * must be transferred to the event queue.
+     * 
+     * The event's own information is encapsulated in the parameters and payload
+     * field. From this it is abstracted to the corresponding event model.
+     * 
+     * 01. S7ModeEvent:
+     * 02. S7UserEvent:
+     * 03. S7SysEvent:
+     * 04. S7AlarmEvent
+     * 05. S7CyclicEvent:
+     * 06. S7CyclicEvent:
+     * 
+     * TODO: Use mspec to generate types that allow better interpretation of 
+     * the code using "instanceof".
      */
     @Override
     protected void decode(ConversationContext<TPKTPacket> context, TPKTPacket msg) throws Exception {
-
-        S7Message s7msg = msg.getPayload().getPayload();
-        S7Parameter parameter = s7msg.getParameter();
-        if (parameter instanceof S7ParameterModeTransition) {
-            // TODO: The eventQueue is only drained in the S7ProtocolEventLogic.ObjectProcessor and here only messages of type S7Event are processed, so S7PayloadUserDataItem elements will just be ignored.
-            //eventQueue.add(parameter);
+        
+        final S7Message s7msg = msg.getPayload().getPayload();
+        final S7Parameter parameter = s7msg.getParameter();
+        final S7PayloadUserData payload = (S7PayloadUserData) s7msg.getPayload();       
+        
+        if (parameter instanceof S7ParameterModeTransition) {  //(01)  
+            
+            S7ModeEvent modeEvent = new S7ModeEvent((S7ParameterModeTransition) parameter);
+            eventQueue.add(modeEvent);
+            
         } else if (parameter instanceof S7ParameterUserData) {
+            
             S7ParameterUserData parameterUD = (S7ParameterUserData) parameter;
             List<S7ParameterUserDataItem> parameterUDItems = parameterUD.getItems();
+            
             for (S7ParameterUserDataItem parameterUDItem : parameterUDItems) {
+                
                 if (parameterUDItem instanceof S7ParameterUserDataItemCPUFunctions) {
+                    
                     S7ParameterUserDataItemCPUFunctions myParameter = (S7ParameterUserDataItemCPUFunctions) parameterUDItem;
-                    //TODO: Check from mspec. We can try using "instanceof"
-                    if ((myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x03)) {
-                        S7PayloadUserData payload = (S7PayloadUserData) s7msg.getPayload();
-                        List<S7PayloadUserDataItem> items = payload.getItems();
-                        for (S7PayloadUserDataItem item : items) {
+                    
+                    if ((myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x03)) { //(02)                      
+                        
+                        payload.getItems().forEach(item ->{
                             if (item instanceof S7PayloadDiagnosticMessage) {
-                                // TODO: The eventQueue is only drained in the S7ProtocolEventLogic.ObjectProcessor and here only messages of type S7Event are processed, so S7PayloadUserDataItem elements will just be ignored.
-                                //eventQueue.add(item);
-                            }
-                        }
+                                final S7PayloadDiagnosticMessage pload = (S7PayloadDiagnosticMessage) item; 
+                                if ((pload.getEventId() >= 0x0A000) & (pload.getEventId() <= 0x0BFFF)) {
+                                    S7UserEvent userEvent = new S7UserEvent(pload);
+                                    eventQueue.add(userEvent);                                
+                                } else {
+                                    S7SysEvent sysEvent = new S7SysEvent(pload);
+                                    eventQueue.add(sysEvent);                                        
+                                }
+                            } 
+                        });
+
                     } else if ((myParameter.getCpuFunctionType() == 0x00) &&
                         ((myParameter.getCpuSubfunction() == 0x05) ||
                             (myParameter.getCpuSubfunction() == 0x06) ||
@@ -1481,21 +1515,22 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
                             (myParameter.getCpuSubfunction() == 0x11) ||
                             (myParameter.getCpuSubfunction() == 0x12) ||
                             (myParameter.getCpuSubfunction() == 0x13) ||
-                            (myParameter.getCpuSubfunction() == 0x16))) {
-                        S7PayloadUserData payload = (S7PayloadUserData) s7msg.getPayload();
-                        List<S7PayloadUserDataItem> items = payload.getItems();
-                        // TODO: The eventQueue is only drained in the S7ProtocolEventLogic.ObjectProcessor and here only messages of type S7Event are processed, so S7PayloadUserDataItem elements will just be ignored.
-                        //eventQueue.addAll(items);
+                            (myParameter.getCpuSubfunction() == 0x16))) { //(04)
+                        
+                        payload.getItems().forEach(item ->{
+                            S7AlarmEvent alrmEvent = new S7AlarmEvent(item);
+                            eventQueue.add(alrmEvent);                                
+                        });
+                        
+                                                
                     } else if ((myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x13)) {
-
-                    } else if ((myParameter.getCpuFunctionGroup() == 0x02) && (myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x01)) {
+                        //TODO: Requires reverse engineering.
+                    } else if ((myParameter.getCpuFunctionGroup() == 0x02) && (myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x01)) { //(05)
 
                         S7ParameterUserDataItemCPUFunctions parameterItem =
                             (S7ParameterUserDataItemCPUFunctions)
                                 ((S7ParameterUserData) parameter).getItems().get(0);
 
-                        S7PayloadUserData payload = (S7PayloadUserData) s7msg.getPayload();
-
                         S7PayloadUserDataItemCyclicServicesPush payloadItem =
                             (S7PayloadUserDataItemCyclicServicesPush)
                                 payload.getItems().get(0);
@@ -1505,13 +1540,12 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
                             payloadItem);
                         eventQueue.add(cycEvent);
 
-                    } else if ((myParameter.getCpuFunctionGroup() == 0x02) && (myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x05)) {
+                    } else if ((myParameter.getCpuFunctionGroup() == 0x02) && (myParameter.getCpuFunctionType() == 0x00) && (myParameter.getCpuSubfunction() == 0x05)) { //(06)
+                        
                         S7ParameterUserDataItemCPUFunctions parameterItem =
                             (S7ParameterUserDataItemCPUFunctions)
                                 ((S7ParameterUserData) parameter).getItems().get(0);
 
-                        S7PayloadUserData payload = (S7PayloadUserData) s7msg.getPayload();
-
                         S7PayloadUserDataItemCyclicServicesChangeDrivenPush payloadItem =
                             (S7PayloadUserDataItemCyclicServicesChangeDrivenPush)
                                 payload.getItems().get(0);
@@ -1522,9 +1556,9 @@ public class S7ProtocolLogic extends Plc4xProtocolBase<TPKTPacket> {
                         eventQueue.add(cycEvent);
 
                     } else if ((myParameter.getCpuFunctionType() == 0x08) && (myParameter.getCpuSubfunction() == 0x01)) {
-
+                        //TODO: Requires reverse engineering.
                     } else if ((myParameter.getCpuFunctionType() == 0x08) && (myParameter.getCpuSubfunction() == 0x04)) {
-
+                        //TODO: Requires reverse engineering.
                     }
                 }
             }