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/08/23 08:44:13 UTC

[incubator-plc4x] branch master updated: [ADS] partially fixed length calculation where it is partially possible

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 0b0e202  [ADS] partially fixed length calculation where it is partially possible
0b0e202 is described below

commit 0b0e2023f4b7afd615bfd80cea62f1246cbf5697
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Aug 23 10:44:08 2018 +0200

    [ADS] partially fixed length calculation where it is partially possible
---
 .../PlcUnsupportedDataTypeException.java           | 46 ++++++++++++++++++++++
 .../plc4x/java/ads/protocol/Plc4x2AdsProtocol.java | 46 +++++++++++++++++++++-
 2 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcUnsupportedDataTypeException.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcUnsupportedDataTypeException.java
new file mode 100644
index 0000000..54ca86b
--- /dev/null
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcUnsupportedDataTypeException.java
@@ -0,0 +1,46 @@
+/*
+ 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.api.exceptions;
+
+/**
+ * Indicate that a data type ({@link Class}) is not supported by Plc4x.
+ */
+public class PlcUnsupportedDataTypeException extends PlcRuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    public PlcUnsupportedDataTypeException(Class<?> dataType) {
+        super(dataType + " not supported by plc4x");
+    }
+
+    public PlcUnsupportedDataTypeException(String message) {
+        super(message);
+    }
+
+    public PlcUnsupportedDataTypeException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public PlcUnsupportedDataTypeException(Throwable cause) {
+        super(cause);
+    }
+
+    public PlcUnsupportedDataTypeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
index 6e86460..b72d471 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/Plc4x2AdsProtocol.java
@@ -32,6 +32,7 @@ import org.apache.plc4x.java.ads.protocol.exception.AdsException;
 import org.apache.plc4x.java.api.exceptions.PlcException;
 import org.apache.plc4x.java.api.exceptions.PlcIoException;
 import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
+import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
 import org.apache.plc4x.java.api.messages.*;
 import org.apache.plc4x.java.api.messages.items.ReadRequestItem;
 import org.apache.plc4x.java.api.messages.items.ReadResponseItem;
@@ -48,6 +49,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Calendar;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -183,14 +186,53 @@ public class Plc4x2AdsProtocol extends MessageToMessageCodec<AmsPacket, PlcReque
         Invoke invokeId = Invoke.of(correlationBuilder.incrementAndGet());
         IndexGroup indexGroup = IndexGroup.of(adsAddress.getIndexGroup());
         IndexOffset indexOffset = IndexOffset.of(adsAddress.getIndexOffset());
-        // TODO: multiplicator is missing is missing. So an integer 4Bytes and 10 of them should be 40 Bytes not one.
-        Length length = Length.of(readRequestItem.getSize());
+        Length length = Length.of(calculateLength(readRequestItem.getDatatype(), readRequestItem.getSize()));
         AmsPacket amsPacket = AdsReadRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, length);
         LOGGER.debug("encoded read request {}", amsPacket);
         out.add(amsPacket);
         requests.put(invokeId.getAsLong(), msg);
     }
 
+    private long calculateLength(Class<?> dataType, int size) {
+        if (dataType == Boolean.class) {
+            // Boolean is one byte
+            return size;
+        } else if (dataType == Byte.class) {
+            // Byte is one byte
+            return size;
+        } else if (dataType == Short.class) {
+            return (Short.SIZE / Byte.SIZE) * size;
+        } else if (dataType == Float.class) {
+            return (Float.SIZE / Byte.SIZE) * size;
+        } else if (dataType == Integer.class) {
+            return (Integer.SIZE / Byte.SIZE) * size;
+        } else if (dataType == Double.class) {
+            return (Double.SIZE / Byte.SIZE) * size;
+        } else if (dataType == BigInteger.class) {
+            // TODO: how to calculate size for this?
+            //return (BigInteger.SIZE / Byte.SIZE) * size;
+            return 4 + size;
+        } else if (dataType == Calendar.class || Calendar.class.isAssignableFrom(dataType)) {
+            // TODO: how to calculate size for this?
+            //return (Calendar.SIZE / Byte.SIZE) * size;
+            return 4 + size;
+        } else if (dataType == String.class) {
+            // TODO: how to calculate size for this?
+            //return (String.SIZE / Byte.SIZE) * size;
+            return 4 + size;
+        } else if (dataType == byte[].class) {
+            // TODO: how to calculate size for this?
+            //return (byte[].SIZE / Byte.SIZE) * size;
+            return 4 + size;
+        } else if (dataType == Byte[].class) {
+            // TODO: how to calculate size for this?
+            //return (Byte[].SIZE / Byte.SIZE) * size;
+            return 4 + size;
+        } else {
+            throw new PlcUnsupportedDataTypeException(dataType);
+        }
+    }
+
     private void encodeProprietaryRequest(PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws PlcProtocolException {
         PlcProprietaryRequest plcProprietaryRequest = (PlcProprietaryRequest) msg.getRequest();
         if (!(plcProprietaryRequest.getRequest() instanceof AmsPacket)) {