Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plc4j/opcua): Fix incorrectly handled GUID tags (#1099)
1. The driver tried to `arraycopy()` a `Long`, which caused `ArrayStoreException`. 2. GUIDs are structured objects, that matters when it comes to byte ordering. The OPC-UA protocol uses little (mixed) endian GUID encoding format, while Java uses the standard big endian one (RFC4122), so conversion is needed. References: * https://reference.opcfoundation.org/Core/Part6/v104/docs/5.2.2.6 * https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding * https://devblogs.microsoft.com/oldnewthing/20220928-00/?p=107221 * https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html * https://www.ietf.org/rfc/rfc4122.txt
- Loading branch information
Showing
3 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/ManualOpcuaGuidTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.opcua; | ||
|
||
import org.apache.plc4x.java.DefaultPlcDriverManager; | ||
import org.apache.plc4x.java.api.PlcConnection; | ||
import org.apache.plc4x.java.api.messages.PlcReadRequest; | ||
import org.apache.plc4x.java.api.messages.PlcReadResponse; | ||
|
||
public class ManualOpcuaGuidTag { | ||
|
||
public static void main(String... args) throws Exception { | ||
DefaultPlcDriverManager driverManager = new DefaultPlcDriverManager(); | ||
try (PlcConnection opcuaConnection = driverManager.getConnection("opcua:tcp://opcuaserver.com:48010")) { | ||
PlcReadRequest request = opcuaConnection.readRequestBuilder() | ||
.addTagAddress( | ||
"VariableWithGuidNodeId", | ||
"ns=2;g=5CE9DBCE-5D79-434C-9AC3-1CFBA9A6E92C" | ||
) | ||
.build(); | ||
|
||
PlcReadResponse response = request.execute().get(); | ||
System.out.println(response.getObject("VariableWithGuidNodeId")); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...vers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.opcua.protocol; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.apache.plc4x.java.opcua.readwrite.NodeId; | ||
import org.apache.plc4x.java.opcua.readwrite.NodeIdGuid; | ||
import org.apache.plc4x.java.opcua.readwrite.NodeIdType; | ||
import org.apache.plc4x.java.opcua.tag.OpcuaTag; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class OpcuaProtocolLogicTest { | ||
|
||
@Test | ||
public void testGenerateNodeId() { | ||
OpcuaTag tag = OpcuaTag.of("ns=2;g=00112233-4455-6677-8899-aabbccddeeff"); | ||
NodeId nodeId = OpcuaProtocolLogic.generateNodeId(tag); | ||
assertEquals(NodeIdType.nodeIdTypeGuid, nodeId.getNodeId().getNodeType()); | ||
|
||
NodeIdGuid nodeIdGuid = (NodeIdGuid) nodeId.getNodeId(); | ||
assertEquals(2, nodeIdGuid.getNamespaceIndex()); | ||
assertArrayEquals( | ||
new byte[] { | ||
(byte) 0x33, | ||
(byte) 0x22, | ||
(byte) 0x11, | ||
(byte) 0x00, | ||
(byte) 0x55, | ||
(byte) 0x44, | ||
(byte) 0x77, | ||
(byte) 0x66, | ||
(byte) 0x88, | ||
(byte) 0x99, | ||
(byte) 0xaa, | ||
(byte) 0xbb, | ||
(byte) 0xcc, | ||
(byte) 0xdd, | ||
(byte) 0xee, | ||
(byte) 0xff | ||
}, | ||
nodeIdGuid.getId() | ||
); | ||
} | ||
} |