LivingLikeKrillin commented on code in PR #2651:
URL: https://github.com/apache/plc4x/pull/2651#discussion_r3621098204


##########
plc4j/drivers/slmp/src/main/java/org/apache/plc4x/java/slmp/SlmpDataType.java:
##########
@@ -90,6 +93,64 @@ public PlcValue decode(byte[] responseData, int quantity) {
         }
     }
 
+    /**
+     * Encode {@code quantity} elements of this type to little-endian SLMP 
request bytes
+     * ({@code 2 * wordsPerElement} per element). Returns {@code null} on a 
type/arity mismatch
+     * (caller maps to INVALID_DATA), symmetric with {@link #decode}.
+     */
+    public byte[] encode(PlcValue value, int quantity) {
+        int totalBytes = quantity * wordsPerElement * 2;
+        WriteBufferByteBased buffer = new WriteBufferByteBased(new 
byte[totalBytes],
+            WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"),
+            WithOption.WithUnsignedIntegerEncoding("unsigned-binary"),
+            WithOption.WithSignedIntegerEncoding("twos-complement"),
+            WithOption.WithFloatEncoding("IEEE754"));
+        try {
+            if (quantity == 1) {
+                if (value == null || value.isList()) {
+                    return null;
+                }
+                writeOne(buffer, value);
+            } else {
+                if (value == null || !value.isList() || value.getList().size() 
!= quantity) {
+                    return null;
+                }
+                for (PlcValue element : value.getList()) {
+                    writeOne(buffer, element);
+                }
+            }
+            return buffer.getBytes();
+        } catch (BufferException | PlcRuntimeException e) {
+            LOGGER.warn("Failed to encode SLMP {} value", this, e);
+            return null;
+        }

Review Comment:
   Fixed in 006f3441e5: `PlcIncompatibleDatatypeException` (an un-coercible 
value, which maps to `INVALID_DATA`) is now caught separately and logged at 
DEBUG without a stack trace; genuine buffer/encoding failures keep the WARN 
with the throwable.



##########
plc4j/drivers/slmp/src/test/java/org/apache/plc4x/java/slmp/SlmpDriverTest.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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
+ *
+ *   https://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.slmp;
+
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class SlmpDriverTest {
+
+    private final SlmpDriver driver = new SlmpDriver();
+
+    @Test
+    void capabilities() throws Exception {
+        // Use reflection to access the protected can* methods.
+        for (String name : new String[]{"canRead", "canWrite"}) {
+            Method m = SlmpDriver.class.getDeclaredMethod(name);
+            m.setAccessible(true);
+            assertEquals(Boolean.TRUE, m.invoke(driver), name + " should 
return true");
+        }
+    }
+}

Review Comment:
   Fixed in 006f3441e5 — the reflection is gone; the test calls the protected 
`canRead()`/`canWrite()` directly (same package).



##########
plc4j/drivers/slmp/src/main/java/org/apache/plc4x/java/slmp/tag/SlmpTag.java:
##########
@@ -135,7 +135,7 @@ public static SlmpTag of(String addressString) {
         long numberOfPoints = (long) quantity * dataType.getWordsPerElement();
         if (numberOfPoints > MAX_POINTS) {
             throw new PlcInvalidTagException("requested " + numberOfPoints + " 
words exceeds the v0 single-frame "
-                + "Batch Read ceiling of " + MAX_POINTS + " (no optimizer to 
split): " + addressString);
+                + "batch Read/Write ceiling of " + MAX_POINTS + " (no 
optimizer to split): " + addressString);
         }

Review Comment:
   Fixed in 006f3441e5 — normalized to "Batch Read/Write" in the javadoc, the 
exception message, and the getNumberOfPoints javadoc.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to