You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/03/08 13:36:20 UTC

[incubator-plc4x] branch master updated: fix build

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

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new 13932e9  fix build
13932e9 is described below

commit 13932e9bed5cba1479e0ce30266f4fc261f876be
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Mar 8 14:36:19 2018 +0100

    fix build
---
 .../plc4x/java/ads/api/generic/types/AMSNetId.java |  2 --
 .../plc4x/java/ads/api/generic/types/AMSPort.java  |  2 ++
 .../plc4x/java/ads/protocol/util/DigestUtil.java   | 23 ++++++++++++++++++++--
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSNetId.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSNetId.java
index 138a441..585ba8a 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSNetId.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSNetId.java
@@ -29,8 +29,6 @@ import java.util.stream.Stream;
  * It is not only possible to exchange data between TwinCAT modules on one PC, it is even possible to do so by ADS
  * methods between multiple TwinCAT PC's on the network.
  * <p>
- * <img src="https://infosys.beckhoff.com/content/1033/tcadscommon/images/TcMultiplePC.gif"/>
- * <p>
  * Every PC on the network can be uniquely identified by a TCP/IP address, such as "172.1.2.16". The AdsAmsNetId is an
  * extension of the TCP/IP address and identifies a TwinCAT message router, e.g. "172.1.2.16.1.1". TwinCAT message
  * routers exist on every TwinCAT PC, and on every Beckhoff BCxxxx bus controller (e.g. BC3100, BC8100, BC9000, ...).
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
index 8807acb..7c9521b 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
@@ -28,6 +28,7 @@ import java.util.regex.Pattern;
  * The ADS devices in a TwinCAT message router are uniquely identified by a number referred to as the ADS-PortNr. For ADS devices this has a fixed specification, whereas pure ADS client applications (e.g. a visualisation system) are allocated a variable ADS port number when they first access the message router.
  * <p>
  * The following ADS port numbers are already assigned:
+ * <p>
  * <table border="1">
  * <th><td>ADS-PortNr</td><td>ADS device description</td></th>
  * <tr><td>100<tr><td>Logger (only NT-Log)</td></tr>
@@ -46,6 +47,7 @@ import java.util.regex.Pattern;
  * <tr><td>10000<tr><td>System Service</td></tr>
  * <tr><td>14000<tr><td>Scope</td></tr>
  * </table>
+ * </p>
  *
  * @see <a href="https://infosys.beckhoff.com/content/1033/tcadscommon/html/tcadscommon_identadsdevice.htm?id=3991659524769593444">ADS device identification</a>
  */
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
index ad74dc5..f6e7d74 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/DigestUtil.java
@@ -21,6 +21,9 @@ package org.apache.plc4x.java.ads.protocol.util;
 import io.netty.buffer.ByteBuf;
 import org.apache.plc4x.java.ads.api.util.ByteReadable;
 
+import static java.util.Arrays.stream;
+import static org.apache.commons.lang3.ArrayUtils.toObject;
+
 /**
  * TODO: temporary due to unclear licence
  * From https://stackoverflow.com/a/18333436/850036
@@ -32,6 +35,11 @@ import org.apache.plc4x.java.ads.api.util.ByteReadable;
  */
 public class DigestUtil {
 
+    private static final int PRESET_VALUE = 0xFFFF;
+
+    // CRC-16-IBM reversed
+    private static final int POLYNOMIAL = 0xA001;
+
     private DigestUtil() {
         // Utility class
     }
@@ -49,7 +57,18 @@ public class DigestUtil {
     }
 
     public static int calculateCrc16(byte[] bytes) {
-        // TODO: implement me
-        return 0;
+        return (~stream(toObject(bytes))
+            .mapToInt(Byte::intValue)
+            .reduce(PRESET_VALUE, (int crcValue, int aByte) -> {
+                crcValue ^= aByte & 0xFF;
+                for (int j = 0; j < 8; j++) {
+                    if ((crcValue & 1) != 0) {
+                        crcValue = (crcValue >>> 1) ^ POLYNOMIAL;
+                    } else {
+                        crcValue = crcValue >>> 1;
+                    }
+                }
+                return crcValue;
+            })) & 0xFFFF;
     }
 }

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.