Skip to content

Commit

Permalink
fix: Fixed some issues in the old PN driver (Adjusted the Transport c…
Browse files Browse the repository at this point in the history
…onfig and made sure it doesn't consume own outgoing messages)
  • Loading branch information
chrisdutz committed Dec 9, 2023
1 parent 40c01b7 commit 48f8e65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -64,30 +63,36 @@ public void send(Ethernet_Frame ethFrame) {
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.
Expand Down Expand Up @@ -138,9 +143,9 @@ public PacketListener createListener() {
}
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);
}
Expand Down

0 comments on commit 48f8e65

Please sign in to comment.