Copilot commented on code in PR #2640: URL: https://github.com/apache/plc4x/pull/2640#discussion_r3569604440
########## plc4j/drivers/slmp/src/main/java/org/apache/plc4x/java/slmp/tag/SlmpTag.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.tag; + +import org.apache.plc4x.java.api.exceptions.PlcInvalidTagException; +import org.apache.plc4x.java.api.model.ArrayInfo; +import org.apache.plc4x.java.api.model.PlcTag; +import org.apache.plc4x.java.api.types.PlcValueType; +import org.apache.plc4x.java.slmp.SlmpDataType; +import org.apache.plc4x.java.slmp.readwrite.SlmpDeviceCode; +import org.apache.plc4x.java.spi.drivers.model.DefaultArrayInfo; + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A single SLMP word-device tag. v0 supports the word devices D (decimal addr), + * W (hex addr) and R (decimal addr); bit devices are rejected. Address grammar: + * {@code <device><address>[:<datatype>][\[<quantity>\]]}, e.g. {@code D350}, + * {@code R200:REAL[4]}, {@code W1A:WORD[10]} (W also accepts {@code W0x1A}). + */ +public class SlmpTag implements PlcTag, Serializable { + + public static final Pattern ADDRESS_PATTERN = Pattern.compile( + "^(?<device>[A-Za-z]+)(?<hexPrefix>0[xX])?(?<address>[0-9A-Fa-f]+)" + + "(:(?<datatype>[A-Za-z_]+))?(\\[(?<quantity>\\d+)])?$"); + + /** Conservative single-frame word ceiling for 3E binary Batch Read (not the exact device max). */ + static final int MAX_POINTS = 960; + + private final SlmpDeviceCode deviceCode; + private final int deviceNumber; + private final SlmpDataType dataType; + private final int quantity; + + public SlmpTag(SlmpDeviceCode deviceCode, int deviceNumber, SlmpDataType dataType, int quantity) { + this.deviceCode = deviceCode; + this.deviceNumber = deviceNumber; + this.dataType = dataType; + this.quantity = quantity; + } + + public static SlmpTag of(String addressString) { + Matcher matcher = ADDRESS_PATTERN.matcher(addressString); + if (!matcher.matches()) { + throw new PlcInvalidTagException("Unable to parse SLMP address: " + addressString); + } + String deviceToken = matcher.group("device").toUpperCase(); + SlmpDeviceCode device; + int radix; + switch (deviceToken) { + case "D": + device = SlmpDeviceCode.D; + radix = 10; + break; + case "R": + device = SlmpDeviceCode.R; + radix = 10; + break; + case "W": + device = SlmpDeviceCode.W; + radix = 16; + break; + default: + throw new PlcInvalidTagException( + "device '" + deviceToken + "' not supported in this version (word devices D/W/R only)"); + } + + boolean hasHexPrefix = matcher.group("hexPrefix") != null; + if (hasHexPrefix && radix != 16) { + throw new PlcInvalidTagException("0x prefix is only valid for hex devices (W): " + addressString); + } + int deviceNumber; + try { + deviceNumber = Integer.parseInt(matcher.group("address"), radix); + } catch (NumberFormatException e) { + throw new PlcInvalidTagException("Invalid " + (radix == 16 ? "hex" : "decimal") + + " device number in: " + addressString); + } + + String datatypeToken = matcher.group("datatype"); + SlmpDataType dataType; + if (datatypeToken == null) { + dataType = SlmpDataType.WORD; + } else { + try { + dataType = SlmpDataType.valueOf(datatypeToken.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new PlcInvalidTagException("Unsupported SLMP data type '" + datatypeToken + + "' (supported: WORD, INT, UINT, DINT, UDINT, REAL)"); + } + } + + String quantityToken = matcher.group("quantity"); + int quantity = quantityToken == null ? 1 : Integer.parseInt(quantityToken); + if (quantity < 1) { + throw new PlcInvalidTagException("quantity must be >= 1 in: " + addressString); + } Review Comment: `quantity` is parsed with `Integer.parseInt` without handling overflow. For very large quantities (e.g., `D0:WORD[999999999999]`) this will throw a raw `NumberFormatException` instead of a `PlcInvalidTagException`, which is inconsistent with other parse failures in this method. ########## plc4j/drivers/slmp/src/main/java/org/apache/plc4x/java/slmp/config/SlmpConfiguration.java: ########## @@ -0,0 +1,59 @@ +/* + * 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.config; + +import org.apache.plc4x.java.spi.config.Configuration; +import org.apache.plc4x.java.spi.config.annotations.ConfigurationParameter; +import org.apache.plc4x.java.spi.config.annotations.Description; +import org.apache.plc4x.java.spi.config.annotations.defaults.IntDefaultValue; + +public class SlmpConfiguration implements Configuration { + + @ConfigurationParameter("monitoring-timer") + @IntDefaultValue(0x0000) + @Description("SLMP monitoring timer written into each 3E request frame (0 = wait infinitely).") + private int monitoringTimer; + + @ConfigurationParameter("request-timeout") + @IntDefaultValue(5_000) + @Description("Client-side timeout in milliseconds awaiting a response.") + private int requestTimeout; + + public int getMonitoringTimer() { + return monitoringTimer; + } + + public void setMonitoringTimer(int monitoringTimer) { + this.monitoringTimer = monitoringTimer; + } Review Comment: `monitoring-timer` is serialized as an unsigned 16-bit field in the 3E frame. Without validating the configured value, out-of-range inputs can cause unexpected truncation or serialization errors at runtime. ########## plc4j/drivers/slmp/src/main/java/org/apache/plc4x/java/slmp/config/SlmpConfiguration.java: ########## @@ -0,0 +1,59 @@ +/* + * 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.config; + +import org.apache.plc4x.java.spi.config.Configuration; +import org.apache.plc4x.java.spi.config.annotations.ConfigurationParameter; +import org.apache.plc4x.java.spi.config.annotations.Description; +import org.apache.plc4x.java.spi.config.annotations.defaults.IntDefaultValue; + +public class SlmpConfiguration implements Configuration { + + @ConfigurationParameter("monitoring-timer") + @IntDefaultValue(0x0000) + @Description("SLMP monitoring timer written into each 3E request frame (0 = wait infinitely).") + private int monitoringTimer; + + @ConfigurationParameter("request-timeout") + @IntDefaultValue(5_000) + @Description("Client-side timeout in milliseconds awaiting a response.") + private int requestTimeout; + + public int getMonitoringTimer() { + return monitoringTimer; + } + + public void setMonitoringTimer(int monitoringTimer) { + this.monitoringTimer = monitoringTimer; + } + + public int getRequestTimeout() { + return requestTimeout; + } + + public void setRequestTimeout(int requestTimeout) { + this.requestTimeout = requestTimeout; + } Review Comment: `request-timeout` is later passed to `CompletableFuture.orTimeout(...)`. Negative values can trigger immediate/undefined timeout behavior or exceptions; it's safer to reject them at configuration parse/set time. -- 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]
