Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/s7 discovery #1743

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ New Features
array requests into multiple ones and transparently merge
them back together. It is now possible to read arrays of
almost unlimited size.
- Added auto-discovery to the EIP and KNXNet/IP Drivers.
- Added auto-discovery to the EIP, KNXNet/IP and S7 Drivers.
- Added an Optimizer to the Modbus driver, that improves read
performance of multi-item read requests by more than 10 times.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public CompletableFuture<PlcDiscoveryResponse> discoverWithHandler(PlcDiscoveryR
}

try {
Thread.sleep(3000);
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -205,6 +205,8 @@ public CompletableFuture<PlcDiscoveryResponse> discoverWithHandler(PlcDiscoveryR
public void run() {
PlcDiscoveryResponse response =
new DefaultPlcDiscoveryResponse(discoveryRequest, PlcResponseCode.OK, new ArrayList<>(values));
timer.cancel();
timer.purge();
future.complete(response);
}
}, 5000L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.plc4x.protocol.ads;

import org.apache.plc4x.java.ads.AdsPlcDriver;
import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;

public class ManualAdsDiscovery {

public static void main(String[] args) throws Exception {
PlcDiscoveryResponse discoveryResponse = new AdsPlcDriver().discoveryRequestBuilder().addQuery("all", "*").build().executeWithHandler(discoveryItem -> System.out.println(discoveryItem.getConnectionUrl())).get();
System.out.println(discoveryResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public CompletableFuture<PlcDiscoveryResponse> discoverWithHandler(PlcDiscoveryR
}

try {
Thread.sleep(3000);
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -162,6 +162,8 @@ public CompletableFuture<PlcDiscoveryResponse> discoverWithHandler(PlcDiscoveryR
public void run() {
PlcDiscoveryResponse response =
new DefaultPlcDiscoveryResponse(discoveryRequest, PlcResponseCode.OK, new ArrayList<>(values));
timer.cancel();
timer.purge();
future.complete(response);
}
}, 5000L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

package org.apache.plc4x.java.eip.base;

import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;

public class ManualEipDiscovery {

public static void main(String[] args) throws Exception {
PlcDiscoveryResponse discoveryResponse = new EIPDriver().discoveryRequestBuilder().addQuery("all", "*").build().execute().get();
System.out.println(discoveryResponse);
new EIPDriver().discoveryRequestBuilder().addQuery("all", "*")
.build()
.executeWithHandler(discoveryItem -> System.out.println("Found new device: " + discoveryItem.getConnectionUrl() + " (" + discoveryItem.getName() + ")"))
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

public class KnxNetIpPlcDiscoverer implements PlcDiscoverer {
Expand Down Expand Up @@ -182,6 +180,8 @@ public CompletableFuture<PlcDiscoveryResponse> discoverWithHandler(PlcDiscoveryR
public void run() {
PlcDiscoveryResponse response =
new DefaultPlcDiscoveryResponse(discoveryRequest, PlcResponseCode.OK, new ArrayList<>(values.values()));
timer.cancel();
timer.purge();
future.complete(response);
}
}, 5000L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

package org.apache.plc4x.java.knxnetip;

import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;

public class ManualKnxNetIpDiscovery {

public static void main(String[] args) throws Exception {
PlcDiscoveryResponse discoveryResponse = new KnxNetIpDriver().discoveryRequestBuilder().addQuery("all", "*").build().execute().get();
System.out.println(discoveryResponse);
new KnxNetIpDriver().discoveryRequestBuilder().addQuery("all", "*")
.build()
.executeWithHandler(discoveryItem -> System.out.println("Found new device: " + discoveryItem.getConnectionUrl() + " (" + discoveryItem.getName() + ")"))
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public PacketListener createListener() {
// Check if it's a PROFINET packet
if (payload.getHeader().getDstPort().value() == -30572 || payload.getHeader().getDstPort().value() == -15536 || payload.getHeader().getDstPort().value() == -15535) {
isPnPacket = true;
} else {
System.out.println("UDP Packet from port: " + payload.getHeader().getSrcPort().value());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class ProfinetDiscoverer implements PlcDiscoverer {
private static final MacAddress PROFINET_BROADCAST_MAC_ADDRESS = new MacAddress(new byte[]{0x01, 0x0E, (byte) 0xCF, 0x00, 0x00, 0x00});
final private ProfinetChannel channel;
final List<PlcDiscoveryItem> values = new ArrayList<>();
final Set<Timer> periodicTimers = new HashSet<>();
private final Logger logger = LoggerFactory.getLogger(ProfinetDiscoverer.class);
private PlcDiscoveryItemHandler handler;

Expand Down Expand Up @@ -99,7 +98,12 @@ public void sendPnDcpDiscoveryRequest() {
try {
Packet packet = EthernetPacket.newPacket(buffer.getBytes(), 0, identificationRequest.getLengthInBytes());
handle.sendPacket(packet);
} catch (PcapNativeException | NotOpenException | IllegalRawDataException e) {
} catch (PcapNativeException e) {
// This occurs, if for example the Wi-Fi network is disabled.
if(!e.getMessage().contains("Network is down")) {
throw new RuntimeException(e);
}
} catch (NotOpenException | IllegalRawDataException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -123,10 +127,8 @@ public void run() {
logger.error("Error occurred while closing handle");
}
}
for (Timer timer : periodicTimers) {
timer.cancel();
timer.purge();
}
timer.cancel();
timer.purge();
future.complete(response);
}
}, delay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,15 @@

import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcDriver;
import org.apache.plc4x.java.api.messages.PlcDiscoveryItem;
import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;

import java.util.Map;
import java.util.TreeMap;

public class ManualProfinetIoDiscoveryTest {

public static void main(String[] args) throws Exception {
final PlcDriver profinetDriver = new DefaultPlcDriverManager().getDriver("profinet");
final PlcDiscoveryResponse plcDiscoveryResponse = profinetDriver.discoveryRequestBuilder().build().execute().get();
// As we can reach some devices from multiple network devices, aggregate them by connection url
Map<String, PlcDiscoveryItem> items = new TreeMap<>();
for (PlcDiscoveryItem responseValue : plcDiscoveryResponse.getValues()) {
items.put(responseValue.getConnectionUrl(), responseValue);
}
// Output the aggregated values.
for (Map.Entry<String, PlcDiscoveryItem> stringPlcDiscoveryItemEntry : items.entrySet()) {
PlcDiscoveryItem responseValue = stringPlcDiscoveryItemEntry.getValue();
System.out.println(responseValue.getName() + ": " + responseValue.getConnectionUrl());
}
profinetDriver.discoveryRequestBuilder()
.build()
.executeWithHandler(discoveryItem -> System.out.println("Found new device: " + discoveryItem.getConnectionUrl() + " (" + discoveryItem.getName() + ")"))
.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@

import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcDriver;
import org.apache.plc4x.java.api.messages.PlcDiscoveryItem;
import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;

public class ManualProfinetIoDiscoveryTest {

public static void main(String[] args) throws Exception {
final PlcDriver profinetDriver = new DefaultPlcDriverManager().getDriver("profinet");
final PlcDiscoveryResponse plcDiscoveryResponse = profinetDriver.discoveryRequestBuilder().build().execute().get();
for (PlcDiscoveryItem responseValue : plcDiscoveryResponse.getValues()) {
System.out.println(responseValue.getConnectionUrl());
}
profinetDriver.discoveryRequestBuilder()
.build()
.executeWithHandler(discoveryItem -> System.out.println("Found new device: " + discoveryItem.getConnectionUrl() + " (" + discoveryItem.getName() + ")"))
.get();
}

}
25 changes: 25 additions & 0 deletions plc4j/drivers/s7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@
<version>0.13.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.pcap4j</groupId>
<artifactId>pcap4j-core</artifactId>
<!-- Override the "provided" scope -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.pcap4j</groupId>
<artifactId>pcap4j-packetfactory-static</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -188,6 +200,19 @@
<outputDir>src/main/generated</outputDir>
</configuration>
</execution>
<execution>
<id>generate-discovery</id>
<phase>generate-sources</phase>
<goals>
<goal>generate-driver</goal>
</goals>
<configuration>
<protocolName>s7-discovery</protocolName>
<languageName>java</languageName>
<outputFlavor>read-write</outputFlavor>
<outputDir>src/main/generated</outputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Loading
Loading