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 2022/01/29 16:29:39 UTC
[plc4x] branch develop updated (6652fa4 -> ae9bb4a)
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git.
from 6652fa4 feat(plc4j/plc4x): Added a skeleton for a plc4x protocol driver, which will be used to relay PLC4X API requests from one system to another
new 68a9627 refactor(code-generation): Made the code generation work with DataIo types referenced from normal model types.
new d871063 refactor(plc4x-protocol): Added a length field to the plc4x protocol messages
new c3b1c0c refactor(profinet): Continued refactoring the PNIO Messages
new ae9bb4a refactor(plc4j/profinet): Refactored the profinet discovery to the latest changes in the protocol spec and added some more features.
The 4 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:
.../java/modbus/protocol/ModbusProtocolLogic.java | 44 +++----
.../profinet/discovery/ProfinetPlcDiscoverer.java | 71 +++++++++--
.../profinet/readwrite/utils/StaticHelper.java | 74 +++++++++++
.../profinet/ManualProfinetIoDiscoveryTest.java} | 16 +--
.../plc4x/java/profinet/ManualProfinetIoTest.java} | 24 ++--
.../apache/plc4x/java/profinet/ProfinetIoTest.java | 53 --------
.../main/resources/protocols/plc4x/v0/plc4x.mspec | 1 +
.../resources/protocols/profinet/profinet.mspec | 137 +++++++++++++++------
8 files changed, 277 insertions(+), 143 deletions(-)
copy plc4j/drivers/{ads/src/test/java/org/apache/plc4x/protocol/ads/HexBase64Helper.java => profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoDiscoveryTest.java} (63%)
copy plc4j/{spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcReader.java => drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java} (61%)
delete mode 100644 plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ProfinetIoTest.java
[plc4x] 02/04: refactor(plc4x-protocol): Added a length field to the plc4x protocol messages
Posted by cd...@apache.org.
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
commit d8710630231e32f09f583ab7f8b449bbaf6fe8b8
Author: cdutz <ch...@c-ware.de>
AuthorDate: Sat Jan 29 17:27:59 2022 +0100
refactor(plc4x-protocol): Added a length field to the plc4x protocol messages
---
protocols/plc4x/src/main/resources/protocols/plc4x/v0/plc4x.mspec | 1 +
1 file changed, 1 insertion(+)
diff --git a/protocols/plc4x/src/main/resources/protocols/plc4x/v0/plc4x.mspec b/protocols/plc4x/src/main/resources/protocols/plc4x/v0/plc4x.mspec
index 6620298..01bd059 100644
--- a/protocols/plc4x/src/main/resources/protocols/plc4x/v0/plc4x.mspec
+++ b/protocols/plc4x/src/main/resources/protocols/plc4x/v0/plc4x.mspec
@@ -19,6 +19,7 @@
[discriminatedType Plc4xRequest byteOrder='BIG_ENDIAN'
[const uint 8 version 0x01 ]
+ [implicit uint 16 packetLength 'lengthInBytes' ]
[discriminator Plc4xRequestType requestType ]
[typeSwitch requestType
['READ_REQUEST' Plc4xReadRequest
[plc4x] 01/04: refactor(code-generation): Made the code generation work with DataIo types referenced from normal model types.
Posted by cd...@apache.org.
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
commit 68a96272d7d6082b801f8e3d6d20e86cdb4e06a8
Author: cdutz <ch...@c-ware.de>
AuthorDate: Sat Jan 29 17:27:06 2022 +0100
refactor(code-generation): Made the code generation work with DataIo types referenced from normal model types.
---
.../java/modbus/protocol/ModbusProtocolLogic.java | 44 ++++++++++------------
1 file changed, 19 insertions(+), 25 deletions(-)
diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
index 4925473..bee9ec7 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
@@ -191,7 +191,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
if(request.getFieldNames().size() == 1) {
String fieldName = request.getFieldNames().iterator().next();
PlcField field = request.getField(fieldName);
- final ModbusPDU requestPdu = getWriteRequestPdu(field, ((DefaultPlcWriteRequest) writeRequest).getPlcValue(fieldName));
+ final ModbusPDU requestPdu = getWriteRequestPdu(field, writeRequest.getPlcValue(fieldName));
int transactionIdentifier = transactionIdentifierGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if(transactionIdentifierGenerator.get() == 0xFFFF) {
@@ -207,7 +207,6 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
.unwrap(ModbusTcpADU::getPdu)
.handle(responsePdu -> {
// Try to decode the response data based on the corresponding request.
- PlcValue plcValue = null;
PlcResponseCode responseCode;
// Check if the response was an error response.
@@ -215,6 +214,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
responseCode = getErrorCode(errorResponse);
} else {
+ responseCode = PlcResponseCode.OK;
// TODO: Check the correct number of elements were written.
if (responsePdu instanceof ModbusPDUWriteSingleCoilResponse) {
ModbusPDUWriteSingleCoilResponse response = (ModbusPDUWriteSingleCoilResponse) responsePdu;
@@ -223,7 +223,6 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
responseCode = PlcResponseCode.REMOTE_ERROR;
}
}
- responseCode = PlcResponseCode.OK;
}
// Prepare the response.
@@ -262,7 +261,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
int group2Address = 0;
int group1Quantity;
int group2Quantity;
- short group1FileNumber = (short) (Math.floor(extendedRegister.getAddress() / 10000) + 1);
+ short group1FileNumber = (short) (Math.floor((float) extendedRegister.getAddress() / 10000) + 1);
short group2FileNumber;
List<ModbusPDUReadFileRecordRequestItem> itemArray;
@@ -319,7 +318,7 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
int group2Quantity;
byte[] plcValue1, plcValue2;
short group1FileNumber = (short)
- (Math.floor(extendedRegister.getAddress() / FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH) + 1);
+ (Math.floor((float) extendedRegister.getAddress() / FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH) + 1);
short group2FileNumber;
List<ModbusPDUWriteFileRecordRequestItem> itemArray;
if ((group1Address + extendedRegister.getLengthWords()) <= FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH) {
@@ -424,29 +423,24 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
private byte[] fromPlcValue(PlcField field, PlcValue plcValue) {
ModbusDataType fieldDataType = ((ModbusField) field).getDataType();
try {
- WriteBufferByteBased buffer;
if(plcValue instanceof PlcList) {
- buffer = DataItem.staticSerialize(plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
- byte[] data = buffer.getData();
- switch (((ModbusField) field).getDataType()) {
- case BOOL:
- //Reverse Bits in each byte as
- //they should ordered like this: 8 7 6 5 4 3 2 1 | 0 0 0 0 0 0 0 9
- byte[] bytes = new byte[data.length];
- for (int i = 0; i < data.length; i++) {
- bytes[i] = reverseBitsOfByte(data[i]);
- }
- return bytes;
- default:
- return data;
+ WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBits(plcValue, fieldDataType, plcValue.getLength()));
+ DataItem.staticSerialize(writeBuffer, plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
+ byte[] data = writeBuffer.getData();
+ if (((ModbusField) field).getDataType() == ModbusDataType.BOOL) {
+ //Reverse Bits in each byte as
+ //they should be ordered like this: 8 7 6 5 4 3 2 1 | 0 0 0 0 0 0 0 9
+ byte[] bytes = new byte[data.length];
+ for (int i = 0; i < data.length; i++) {
+ bytes[i] = reverseBitsOfByte(data[i]);
+ }
+ return bytes;
}
+ return data;
} else {
- buffer = DataItem.staticSerialize(plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
- if (buffer != null) {
- return buffer.getData();
- } else {
- throw new PlcRuntimeException("Unable to parse PlcValue :- " + ((ModbusField) field).getPlcDataType());
- }
+ WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBits(plcValue, fieldDataType, plcValue.getLength()));
+ DataItem.staticSerialize(writeBuffer, plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
+ return writeBuffer.getData();
}
} catch (SerializationException e) {
throw new PlcRuntimeException("Unable to parse PlcValue :- " + e);
[plc4x] 03/04: refactor(profinet): Continued refactoring the PNIO Messages
Posted by cd...@apache.org.
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
commit c3b1c0c05477a1ab58b61f37528280dbcb396087
Author: cdutz <ch...@c-ware.de>
AuthorDate: Sat Jan 29 17:28:45 2022 +0100
refactor(profinet): Continued refactoring the PNIO Messages
---
.../resources/protocols/profinet/profinet.mspec | 137 +++++++++++++++------
1 file changed, 100 insertions(+), 37 deletions(-)
diff --git a/protocols/profinet/src/main/resources/protocols/profinet/profinet.mspec b/protocols/profinet/src/main/resources/protocols/profinet/profinet.mspec
index 2b3916b..762e2a2 100644
--- a/protocols/profinet/src/main/resources/protocols/profinet/profinet.mspec
+++ b/protocols/profinet/src/main/resources/protocols/profinet/profinet.mspec
@@ -171,7 +171,7 @@
// dynamically endianed and the last 8 bytes are set to Big Endian, we
// had to do this trick.
[discriminatedType DceRpc_InterfaceUuid
- [discriminator uint 32 interfaceType ]
+ [discriminator uint 32 interfaceType ]
[const uint 16 data1 0x6C97 ]
[const uint 16 data2 0x11D1 ]
// This part is described as a byte array, so the byte order is always big-endian
@@ -268,16 +268,45 @@
// Page 90
[discriminatedType PnDcp_Pdu
- [discriminator PnDcp_FrameId frameId ]
- [discriminator PnDcp_ServiceId serviceId ]
- [simple PnDcp_ServiceType serviceType ]
- // 4.3.1.3.4 (Page 95)
- [simple uint 32 xid ]
- // 4.3.1.3.5 (Page 95ff)
- [simple uint 16 responseDelayFactorOrPadding ]
- // 4.3.1.3.4 (Page 95)
- [implicit uint 16 dcpDataLength 'lengthInBytes - 12']
- [typeSwitch frameId,serviceId,serviceType.response
+ [simple uint 16 frameIdValue ]
+ [virtual PnDcp_FrameId frameId 'STATIC_CALL("getFrameId", frameIdValue)']
+ [typeSwitch frameId
+ ['RT_CLASS_1' PnDcp_Pdu_RealTimeCyclic
+// [simple PnIo_CyclicServiceDataUnit dataUnit ]
+ [simple uint 16 cycleCounter ]
+ // Data Status Start (4.7.2.1.3)
+ [simple bit ignore ]
+ [reserved bit 'false' ]
+ [simple bit stationProblemIndicatorOk]
+ [simple bit providerStateRun ]
+ [reserved bit 'false' ]
+ [simple bit dataValid ]
+ [simple bit redundancy ]
+ [simple bit statePrimary ]
+ // Data Status End
+ // "Transfer-Status" (Set to 0x00 for all RT-Classes except RT-CLASS-3,
+ // which PLC4X will never be able to support
+ [reserved uint 8 '0x00' ] // transferStatus
+ ]
+ ['PTCP_DelayReqPDU' PcDcp_Pdu_DelayReq
+
+ ]
+ ['DCP_Identify_ReqPDU' PnDcp_Pdu_IdentifyReq
+ [const uint 8 serviceId 0x05 ]
+ // ServiceType Start
+ [reserved uint 5 '0x00' ]
+ [const bit notSupported false ]
+ [reserved uint 1 '0x00' ]
+ [const bit response false ]
+ // ServiceType End
+ // 4.3.1.3.3 (Page 95)
+ [simple uint 32 xid ]
+ // 4.3.1.3.5 (Page 95ff)
+ [simple uint 16 responseDelay ]
+ // 4.3.1.3.4 (Page 95)
+ [implicit uint 16 dcpDataLength 'lengthInBytes - 12' ]
+ [array PnDcp_Block blocks length 'dcpDataLength']
+ ]
////////////////////////////////////////////////////////////////////////////
// Multicast (Well theoretically)
////////////////////////////////////////////////////////////////////////////
@@ -285,17 +314,38 @@
// 1) One containing only an AllSelectorBlock
// 2) One containing optionally either NameOfStationBlock or AliasNameBlock and another optional IdentifyReqBlock
// (I assume, that if in case 2 both optionally aren't used, this might not be valid and option 1 should be sent instead)
- ['DCP_Identify_ReqPDU','IDENTIFY','false' PnDcp_Pdu_IdentifyReq(uint 16 dcpDataLength)
- [array PnDcp_Block blocks length 'dcpDataLength' ]
+ ['DCP_Identify_ResPDU' PnDcp_Pdu_IdentifyRes
+ [const uint 8 serviceId 0x05 ]
+ // ServiceType Start
+ [reserved uint 5 '0x00' ]
+ [simple bit notSupported ]
+ [reserved uint 1 '0x00' ]
+ [const bit response true ]
+ // ServiceType End
+ // 4.3.1.3.3 (Page 95)
+ [simple uint 32 xid ]
+ // 4.3.1.3.5 (Page 95ff)
+ [reserved uint 16 '0x0000' ]
+ // 4.3.1.3.4 (Page 95)
+ [implicit uint 16 dcpDataLength 'lengthInBytes - 12' ]
+ [array PnDcp_Block blocks length 'dcpDataLength']
]
+ ]
+]
- // Response to a Identify request
- ['DCP_Identify_ResPDU','IDENTIFY','true' PnDcp_Pdu_IdentifyRes(uint 16 dcpDataLength)
- [array PnDcp_Block blocks length 'dcpDataLength' ]
- ]
+[discriminatedType PnDcp_Pdu_IdentifyRes_Payload
+ [discriminator PnDcp_ServiceId serviceId ]
+ [simple PnDcp_ServiceType serviceType ]
+ // 4.3.1.3.3 (Page 95)
+ [simple uint 32 xid ]
+ // 4.3.1.3.5 (Page 95ff)
+ [simple uint 16 responseDelayFactorOrPadding ]
+ // 4.3.1.3.4 (Page 95)
+ [implicit uint 16 dcpDataLength 'lengthInBytes - 12']
+ [typeSwitch serviceId,serviceType.response
// Packet a Profinet station might emit once it is turned on
- ['DCP_Hello_ReqPDU','HELLO','false' PnDcp_Pdu_HelloReq
+// ['DCP_Hello_ReqPDU','HELLO','false' PnDcp_Pdu_HelloReq
// [simple NameOfStationBlockRes nameOfStationBlockRes ]
// [simple IPParameterBlockRes iPParameterBlockRes ]
// [simple DeviceIdBlockRes deviceIdBlockRes ]
@@ -303,35 +353,38 @@
// [simple DeviceOptionsBlockRes deviceOptionsBlockRes ]
// [simple DeviceRoleBlockRes deviceRoleBlockRes ]
// [simple DeviceInitiativeBlockRes deviceInitiativeBlockRes]
- ]
+// ]
////////////////////////////////////////////////////////////////////////////
// Unicast
////////////////////////////////////////////////////////////////////////////
- ['DCP_GetSet_PDU','GET','false' PnDcp_Pdu_GetReq
+// ['DCP_GetSet_PDU','GET','false' PnDcp_Pdu_GetReq
// [simple GetReqBlock getReqBlock ]
- ]
- ['DCP_GetSet_PDU','GET','true' PnDcp_Pdu_GetRes
+// ]
+// ['DCP_GetSet_PDU','GET','true' PnDcp_Pdu_GetRes
// [simple GetResBlock getResBlock ]
// [simple GetNegResBlock getNegResBlock ]
- ]
+// ]
- ['DCP_GetSet_PDU','SET','false' PnDcp_Pdu_SetReq
+// ['DCP_GetSet_PDU','SET','false' PnDcp_Pdu_SetReq
// [simple StartTransactionBlock startTransactionBlock ]
// [simple BlockQualifier blockQualifier ]
// [simple SetResetReqBlock setResetReqBlock ]
// [simple SetReqBlock setReqBlock ]
// [simple StopTransactionBlock stopTransactionBlock ]
// [simple BlockQualifier blockQualifier ]
- ]
- ['DCP_GetSet_PDU','SET','true' PnDcp_Pdu_SetRes
+// ]
+// ['DCP_GetSet_PDU','SET','true' PnDcp_Pdu_SetRes
// [simple SetResBlock setResBlock ]
// [simple SetNegResBlock setNegResBlock ]
- ]
+// ]
]
]
+//[discriminatedType PnIo_CyclicServiceDataUnit
+//]
+
[discriminatedType PnDcp_Block
[discriminator PnDcp_BlockOptions option ]
[discriminator uint 8 suboption ]
@@ -354,9 +407,9 @@
[reserved uint 5 '0x00' ]
[simple bit setViaDhcp ]
[simple bit setManually ]
- [array uint 8 ipAddress count '4' ]
- [array uint 8 subnetMask count '4' ]
- [array uint 8 standardGateway count '4' ]
+ [array byte ipAddress count '4' ]
+ [array byte subnetMask count '4' ]
+ [array byte standardGateway count '4' ]
]
['IP_OPTION','3' PnDcp_Block_FullIpSuite
// TODO: Implement this ...
@@ -368,13 +421,11 @@
['DEVICE_PROPERTIES_OPTION','1' PnDcp_Block_DevicePropertiesDeviceVendor(uint 16 blockLength)
[reserved uint 16 '0x0000' ]
- // TODO: Figure out how to do this correctly.
[array byte deviceVendorValue count 'blockLength-2' ]
[padding uint 8 pad '0x00' 'STATIC_CALL("arrayLength", deviceVendorValue) % 2']
]
['DEVICE_PROPERTIES_OPTION','2' PnDcp_Block_DevicePropertiesNameOfStation(uint 16 blockLength)
[reserved uint 16 '0x0000' ]
- // TODO: Figure out how to do this correctly.
[array byte nameOfStation count 'blockLength-2' ]
[padding uint 8 pad '0x00' 'STATIC_CALL("arrayLength", nameOfStation) % 2']
]
@@ -498,25 +549,36 @@
]
// 4.3.1.3.2 (Page 94ff)
+// 4.3.1.3.2 (Page 94ff)
+// The spec lists meanings for request and response separately, but
+// they are actually mergeable, which we did in this construct.
[type PnDcp_ServiceType
- [reserved uint 5 '0x00' ]
- [simple bit notSupported]
- [reserved uint 1 '0x00' ]
- [simple bit response ]
+ [reserved uint 5 '0x00' ]
+ [simple bit notSupported]
+ [reserved uint 1 '0x00' ]
+ [simple bit response ]
]
// Page 86ff: Coding of the field FrameID
[enum uint 16 PnDcp_FrameId
+ ['0x0000' RESERVED ]
// Range 1
['0x0020' PTCP_RTSyncPDUWithFollowUp ]
// Range 2
['0x0080' PTCP_RTSyncPDU ]
// Range 3
- // 0x100-0x0FFF RT_CLASS_3
+ // 0x0100-0x0FFF RT_CLASS_3
+ ['0x0100' RT_CLASS_3 ]
+ // Range 4
+ // (Not used)
+ // Range 5
+ // (Not used)
// Range 6
// 0x8000-BFFF RT_CLASS_1
+ ['0x8000' RT_CLASS_1 ]
// Range 7
// 0XC000-FBFF RT_CLASS_UDP
+ ['0xC000' RT_CLASS_UDP ]
// Range 8
['0xFC01' Alarm_High ]
['0xFE01' Alarm_Low ]
@@ -533,6 +595,7 @@
['0xFF43' PTCP_DelayResPDUWithoutFollowUp]
// Range 12
// 0xFF80 - 0xFF8F FragmentationFrameId
+ ['0xFF80' FragmentationFrameId ]
]
// Page 94
[plc4x] 04/04: refactor(plc4j/profinet): Refactored the profinet discovery to the latest changes in the protocol spec and added some more features.
Posted by cd...@apache.org.
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
commit ae9bb4abbceceba49db81022ea2c5e01948e2169
Author: cdutz <ch...@c-ware.de>
AuthorDate: Sat Jan 29 17:29:30 2022 +0100
refactor(plc4j/profinet): Refactored the profinet discovery to the latest changes in the protocol spec and added some more features.
---
.../profinet/discovery/ProfinetPlcDiscoverer.java | 71 +++++++++++++++++++--
.../profinet/readwrite/utils/StaticHelper.java | 74 ++++++++++++++++++++++
.../profinet/ManualProfinetIoDiscoveryTest.java | 33 ++++++++++
...ofinetIoTest.java => ManualProfinetIoTest.java} | 22 +------
4 files changed, 173 insertions(+), 27 deletions(-)
diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetPlcDiscoverer.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetPlcDiscoverer.java
index 6c9626f..35c7870 100644
--- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetPlcDiscoverer.java
+++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetPlcDiscoverer.java
@@ -41,6 +41,8 @@ import org.pcap4j.util.LinkLayerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
@@ -72,7 +74,7 @@ public class ProfinetPlcDiscoverer implements PlcDiscoverer {
List<PlcDiscoveryItem> values = new ArrayList<>();
try {
for (PcapNetworkInterface dev : Pcaps.findAllDevs()) {
- if (!dev.isLoopBack() && dev.isRunning()) {
+ if (!dev.isLoopBack()) {
for (LinkLayerAddress linkLayerAddress : dev.getLinkLayerAddresses()) {
org.pcap4j.util.MacAddress macAddress = (org.pcap4j.util.MacAddress) linkLayerAddress;
PcapHandle handle = dev.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);
@@ -138,19 +140,75 @@ public class ProfinetPlcDiscoverer implements PlcDiscoverer {
PnDcp_Block_DevicePropertiesDeviceVendor block = (PnDcp_Block_DevicePropertiesDeviceVendor) blocks.get(DEVICE_TYPE_NAME);
deviceTypeName = new String(block.getDeviceVendorValue());
}
+
String deviceName = "unknown";
if (blocks.containsKey(DEVICE_NAME_OF_STATION)) {
PnDcp_Block_DevicePropertiesNameOfStation block = (PnDcp_Block_DevicePropertiesNameOfStation) blocks.get(DEVICE_NAME_OF_STATION);
deviceName = new String(block.getNameOfStation());
}
- String transportUrl = srcAddr.toString();
- Map<String, String> options =
- Collections.singletonMap("localMacAddress", dstAddr.toString());
+ String role = "unknown";
+ if (blocks.containsKey(DEVICE_ROLE)) {
+ role = "";
+ PnDcp_Block_DevicePropertiesDeviceRole block = (PnDcp_Block_DevicePropertiesDeviceRole) blocks.get(DEVICE_ROLE);
+ if(block.getPnioSupervisor()) {
+ role += ",SUPERVISOR";
+ }
+ if(block.getPnioMultidevive()) {
+ role += ",MULTIDEVICE";
+ }
+ if(block.getPnioController()) {
+ role += ",CONTROLLER";
+ }
+ if(block.getPnioDevice()) {
+ role += ",DEVICE";
+ }
+ // Cut off the first comma
+ if(role.length() > 0) {
+ role = role.substring(1);
+ } else {
+ role = "unknown";
+ }
+ }
+
+ String remoteIpAddress = "unknown";
+ String remoteSubnetMask = "unknown";
+ if (blocks.containsKey(IP_OPTION_IP)) {
+ PnDcp_Block_IpParameter block = (PnDcp_Block_IpParameter) blocks.get(IP_OPTION_IP);
+ try {
+ InetAddress addr = InetAddress.getByAddress(block.getIpAddress());
+ remoteIpAddress = addr.getHostAddress();
+ InetAddress netMask = InetAddress.getByAddress(block.getSubnetMask());
+ remoteSubnetMask = netMask.getHostAddress();
+ } catch (UnknownHostException e) {
+ remoteIpAddress = "invalid";
+ }
+ }
+
+ // Get the Vendor Id and the Device Id
+ String vendorId = "unknown";
+ String deviceId = "unknown";
+ if (blocks.containsKey(DEVICE_ID)) {
+ PnDcp_Block_DevicePropertiesDeviceId block = (PnDcp_Block_DevicePropertiesDeviceId) blocks.get(DEVICE_ID);
+ vendorId = String.format("%04X", block.getVendorId());
+ deviceId = String.format("%04X", block.getDeviceId());
+ }
+
+ Map<String, String> options = Map.of(
+ "remoteIpAddress", remoteIpAddress,
+ "remoteSubnetMask", remoteSubnetMask,
+ "remoteMacAddress", srcAddr.toString(),
+ "localMacAddress", dstAddr.toString(),
+ "deviceTypeName", deviceTypeName,
+ "deviceName", deviceName,
+ "vendorId", vendorId,
+ "deviceId", deviceId,
+ "role", role
+ );
String name = deviceTypeName + " - " + deviceName;
PlcDiscoveryItem value = new DefaultPlcDiscoveryItem(
ProfinetDriver.DRIVER_CODE, RawSocketTransport.TRANSPORT_CODE,
- transportUrl, options, name);
+ remoteIpAddress, options, name);
values.add(value);
// If we have a discovery handler, pass it to the handler callback
@@ -177,8 +235,7 @@ public class ProfinetPlcDiscoverer implements PlcDiscoverer {
toPlc4xMacAddress(macAddress),
new Ethernet_FramePayload_VirtualLan(VirtualLanPriority.BEST_EFFORT, false, 0,
new Ethernet_FramePayload_PnDcp(
- new PnDcp_Pdu_IdentifyReq(
- new PnDcp_ServiceType(false, false),
+ new PnDcp_Pdu_IdentifyReq(PnDcp_FrameId.DCP_Identify_ReqPDU.getValue(),
1,
256,
Collections.singletonList(
diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/readwrite/utils/StaticHelper.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/readwrite/utils/StaticHelper.java
index 86cd3a5..8f16f85 100644
--- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/readwrite/utils/StaticHelper.java
+++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/readwrite/utils/StaticHelper.java
@@ -19,6 +19,7 @@
package org.apache.plc4x.java.profinet.readwrite.utils;
import org.apache.plc4x.java.profinet.readwrite.IpAddress;
+import org.apache.plc4x.java.profinet.readwrite.PnDcp_FrameId;
public class StaticHelper {
@@ -79,4 +80,77 @@ public class StaticHelper {
new IpAddress(new byte[]{(byte) 0xC0, (byte) 0xA8, (byte) 0x18, (byte) 0xC8})));
}
+ public static PnDcp_FrameId getFrameId(int frameIdValue) {
+ // Range 1
+ if(frameIdValue == 0x0020) {
+ return PnDcp_FrameId.PTCP_RTSyncPDUWithFollowUp;
+ }
+ // Range 2
+ if(frameIdValue == 0x0080) {
+ return PnDcp_FrameId.PTCP_RTSyncPDU;
+ }
+ // Range 3
+ if(frameIdValue >= 0x0100 && frameIdValue <= 0x0FFF) {
+ return PnDcp_FrameId.RT_CLASS_3;
+ }
+ // (We do not support RT Class 3 (No need to implement these))
+ // Range 4
+ // (Not used)
+ // Range 5
+ // (Not used)
+ // Range 6
+ if(frameIdValue >= 8000 && frameIdValue <= 0xBFFF) {
+ return PnDcp_FrameId.RT_CLASS_1;
+ }
+ // Range 7
+ if(frameIdValue >= 0xC000 && frameIdValue <= 0xFBFF) {
+ return PnDcp_FrameId.RT_CLASS_UDP;
+ }
+ // Range 8
+ if(frameIdValue == 0xFC01) {
+ return PnDcp_FrameId.Alarm_High;
+ }
+ if(frameIdValue == 0xFE01) {
+ return PnDcp_FrameId.Alarm_Low;
+ }
+ if(frameIdValue == 0xFEFC) {
+ return PnDcp_FrameId.DCP_Hello_ReqPDU;
+ }
+ if(frameIdValue == 0xFEFD) {
+ return PnDcp_FrameId.DCP_GetSet_PDU;
+ }
+ if(frameIdValue == 0xFEFE) {
+ return PnDcp_FrameId.DCP_Identify_ReqPDU;
+ }
+ if(frameIdValue == 0xFEFF) {
+ return PnDcp_FrameId.DCP_Identify_ResPDU;
+ }
+ // Range 9
+ if(frameIdValue == 0xFF00) {
+ return PnDcp_FrameId.PTCP_AnnouncePDU;
+ }
+ if(frameIdValue == 0xFF20) {
+ return PnDcp_FrameId.PTCP_FollowUpPDU;
+ }
+ if(frameIdValue == 0xFF40) {
+ return PnDcp_FrameId.PTCP_DelayReqPDU;
+ }
+ if(frameIdValue == 0xFF41) {
+ return PnDcp_FrameId.PTCP_DelayResPDUWithFollowUp;
+ }
+ if(frameIdValue == 0xFF42) {
+ return PnDcp_FrameId.PTCP_DelayFuResPDUWithFollowUp;
+ }
+ if(frameIdValue == 0xFF43) {
+ return PnDcp_FrameId.PTCP_DelayResPDUWithoutFollowUp;
+ }
+ // Range 12
+ // 0xFF80 - 0xFF8F FragmentationFrameId
+ if(frameIdValue >= 0xFF80 && frameIdValue <= 0xFF8F) {
+ return PnDcp_FrameId.FragmentationFrameId;
+ }
+
+ return PnDcp_FrameId.RESERVED;
+ }
+
}
diff --git a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoDiscoveryTest.java b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoDiscoveryTest.java
new file mode 100644
index 0000000..df48882
--- /dev/null
+++ b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoDiscoveryTest.java
@@ -0,0 +1,33 @@
+/*
+ * 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.java.profinet;
+
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcDriver;
+import org.apache.plc4x.java.api.messages.PlcDiscoveryResponse;
+
+public class ManualProfinetIoDiscoveryTest {
+
+ public static void main(String[] args) throws Exception {
+ final PlcDriver profinetDriver = new PlcDriverManager().getDriver("profinet");
+ final PlcDiscoveryResponse plcDiscoveryResponse = profinetDriver.discoveryRequestBuilder().build().execute().get();
+ System.out.println(plcDiscoveryResponse);
+ }
+
+}
diff --git a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ProfinetIoTest.java b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
similarity index 55%
rename from plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ProfinetIoTest.java
rename to plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
index 0a0e293..f27db26 100644
--- a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ProfinetIoTest.java
+++ b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
@@ -18,36 +18,18 @@
*/
package org.apache.plc4x.java.profinet;
-import org.apache.commons.codec.binary.Hex;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
-import org.apache.plc4x.java.profinet.readwrite.DceRpc_Packet;
-import org.apache.plc4x.java.spi.generation.ReadBuffer;
-import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
-import java.util.concurrent.CompletableFuture;
-
-public class ProfinetIoTest {
+public class ManualProfinetIoTest {
public static void main(String[] args) throws Exception {
final PlcConnection connection = new PlcDriverManager().getConnection("profinet://192.168.24.31");
final PlcReadRequest readRequest = connection.readRequestBuilder().addItem("test", "").build();
final PlcReadResponse plcReadResponse = readRequest.execute().get();
-
- /*long val1 = -559939584;
- long val2 = 3735027712L;
- System.out.println(Long.toHexString(val1));
- System.out.println(Long.toHexString(val2));
- // -559939584 == ffffffffdea00000
- // 3735027712l == 00000000dea00000
-
- final byte[] rawResponse = Hex.decodeHex("04020a00100000000000a0de976cd111827100010904002a0100a0de976cd111827100a02442df7d29f2443745f2e246b8035e0ab26b8293a400000001000000000000000000ffffffff5a000000000000000000460000009401000000000000460000008101001e01000001654519352df3b6428f874371217c2b510002883f990006ef889281020008010000010001bbf08102000801000002000280008103000801000001000000c8");
- ReadBuffer readBuffer = new ReadBufferByteBased(rawResponse);
- final DceRpc_Packet dceRpc_packet = DceRpc_PacketIO.staticParse(readBuffer);
- System.out.println(dceRpc_packet);*/
+ System.out.println(plcReadResponse);
}
-
}