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 2023/12/09 14:40:48 UTC

(plc4x) branch develop updated: fix: Fixed some issues in the old PN driver (Adjusted the Transport config and made sure it doesn't consume own outgoing messages)

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

cdutz 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 48f8e65437 fix: Fixed some issues in the old PN driver (Adjusted the Transport config and made sure it doesn't consume own outgoing messages)
48f8e65437 is described below

commit 48f8e65437d79ba04dc034db0e51f0164b7a9c27
Author: Christofer Dutz <cd...@apache.org>
AuthorDate: Sat Dec 9 15:40:40 2023 +0100

    fix: Fixed some issues in the old PN driver (Adjusted the Transport config and made sure it doesn't consume own outgoing messages)
---
 .../ProfinetRawSocketTransportConfiguration.java   | 15 --------
 .../java/profinet/device/ProfinetChannel.java      | 41 ++++++++++++----------
 2 files changed, 23 insertions(+), 33 deletions(-)

diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/config/ProfinetRawSocketTransportConfiguration.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/config/ProfinetRawSocketTransportConfiguration.java
index 99f08f1820..4570f83050 100644
--- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/config/ProfinetRawSocketTransportConfiguration.java
+++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/config/ProfinetRawSocketTransportConfiguration.java
@@ -19,26 +19,11 @@
 
 package org.apache.plc4x.java.profinet.config;
 
-import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
-import org.apache.plc4x.java.spi.configuration.annotations.defaults.BooleanDefaultValue;
 import org.apache.plc4x.java.transport.rawsocket.DefaultRawSocketTransportConfiguration;
 import org.apache.plc4x.java.utils.pcap.netty.handlers.PacketHandler;
 
 public class ProfinetRawSocketTransportConfiguration extends DefaultRawSocketTransportConfiguration {
 
-    @ConfigurationParameter("resolve-mac-address")
-    @BooleanDefaultValue(true)
-    private boolean resolveMacAddress;
-
-    @Override
-    public boolean isResolveMacAccess() {
-        return resolveMacAddress;
-    }
-
-    public void setResolveMacAddress(boolean resolveMacAddress) {
-        this.resolveMacAddress = resolveMacAddress;
-    }
-
     @Override
     public int getDefaultPort() {
         return 34964;
diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/device/ProfinetChannel.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/device/ProfinetChannel.java
index 01a87ff3d1..7d939d4b8c 100644
--- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/device/ProfinetChannel.java
+++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/device/ProfinetChannel.java
@@ -19,7 +19,6 @@
 
 package org.apache.plc4x.java.profinet.device;
 
-import org.apache.plc4x.java.profinet.config.ProfinetDevices;
 import org.apache.plc4x.java.profinet.discovery.ProfinetPlcDiscoverer;
 import org.apache.plc4x.java.profinet.readwrite.*;
 import org.apache.plc4x.java.spi.generation.*;
@@ -64,30 +63,36 @@ public class ProfinetChannel {
     public void startListener() {
         for (Map.Entry<MacAddress, PcapHandle> entry : openHandles.entrySet()) {
             PcapHandle handle = entry.getValue();
-
-            Thread thread = new Thread(
-                new ProfinetRunnable(handle,
-                    message -> {
-                        PacketListener listener = createListener();
-                        try {
-                            handle.loop(-1, listener);
-                        } catch (InterruptedException e) {
-                            Thread.currentThread().interrupt();
-                        } catch (PcapNativeException | NotOpenException e) {
-                            logger.error("Got error handling raw socket", e);
-                        }
-                        return null;
-                    }));
+            MacAddress macAddress = entry.getKey();
+            ProfinetRunnable packetHandler = new ProfinetRunnable(handle,
+                message -> {
+                    PacketListener listener = createListener(macAddress);
+                    try {
+                        handle.loop(-1, listener);
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                    } catch (PcapNativeException | NotOpenException e) {
+                        logger.error("Got error handling raw socket", e);
+                    }
+                    return null;
+                });
+            Thread thread = new Thread(packetHandler);
             thread.start();
         }
     }
-    public PacketListener createListener() {
+    public PacketListener createListener(MacAddress localMacAddress) {
         PacketListener listener =
             packet -> {
                 // EthernetPacket is the highest level of abstraction we can be expecting.
                 // Everything inside this we will have to decode ourselves.
                 if (packet instanceof EthernetPacket) {
                     EthernetPacket ethernetPacket = (EthernetPacket) packet;
+
+                    // Check if the packet is an outgoing packet (src != our owm MAC address)
+                    if(Arrays.equals(((EthernetPacket.EthernetHeader) packet.getHeader()).getSrcAddr().getAddress(), localMacAddress.getAddress())) {
+                        return;
+                    }
+
                     boolean isPnPacket = false;
                     // I have observed sometimes the ethernet packets being wrapped inside a VLAN
                     // Packet, in this case we simply unpack the content.
@@ -138,9 +143,9 @@ public class ProfinetChannel {
                                     }
                                     else if (pdu.getFrameId() == PnDcp_FrameId.RT_CLASS_1) {
                                         for (Map.Entry<String, ProfinetDevice> device : devices.entrySet()) {
-                                            if (device.getValue().getDeviceContext().getMacAddress() == null) {
+                                            /*if (device.getValue().getDeviceContext().getMacAddress() == null) {
                                                 logger.info("Hurz");
-                                            } else if (Arrays.equals(device.getValue().getDeviceContext().getMacAddress().getAddress(), ethernetFrame.getSource().getAddress())) {
+                                            } else*/ if (Arrays.equals(device.getValue().getDeviceContext().getMacAddress().getAddress(), ethernetFrame.getSource().getAddress())) {
                                                 PnDcp_Pdu_RealTimeCyclic cyclicPdu = (PnDcp_Pdu_RealTimeCyclic) pdu;
                                                 device.getValue().handleRealTimeResponse(cyclicPdu);
                                             }