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 2022/08/05 15:13:13 UTC

[plc4x] 02/02: feat(plc-simulator/cbus): implemented identify responses

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

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

commit 3c7065cb47349ff041b749ddc6dfac73419f0fee
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Aug 5 17:13:01 2022 +0200

    feat(plc-simulator/cbus): implemented identify responses
---
 .../server/cbus/protocol/CBusServerAdapter.java    | 134 ++++++++++++++++++++-
 1 file changed, 132 insertions(+), 2 deletions(-)

diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
index e58490f06..9a890561d 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
@@ -210,8 +210,129 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 LOGGER.info("Handling CBusCommand\n{}", cbusCommand);
                 if (cbusCommand instanceof CBusCommandPointToPoint) {
                     CBusCommandPointToPoint cBusCommandPointToPoint = (CBusCommandPointToPoint) cbusCommand;
-                    LOGGER.info("Handling CBusCommandPointToPoint\n{}", cBusCommandPointToPoint);
-                    // TODO: handle this
+                    CBusPointToPointCommand command = cBusCommandPointToPoint.getCommand();
+                    UnitAddress unitAddress;
+                    if (command instanceof CBusPointToPointCommandIndirect) {
+                        CBusPointToPointCommandIndirect cBusPointToPointCommandIndirect = (CBusPointToPointCommandIndirect) command;
+                        // TODO: handle bridgeAddress
+                        // TODO: handle networkRoute
+                        unitAddress = cBusPointToPointCommandIndirect.getUnitAddress();
+                    }
+                    if (command instanceof CBusPointToPointCommandDirect) {
+                        CBusPointToPointCommandDirect cBusPointToPointCommandDirect = (CBusPointToPointCommandDirect) command;
+                        unitAddress = cBusPointToPointCommandDirect.getUnitAddress();
+                    }
+                    CALData calData = command.getCalData();
+                    // TODO: handle other Datatypes
+                    if (calData instanceof CALDataIdentify) {
+                        short numBytes = 0;
+                        IdentifyReplyCommand identifyReplyCommand;
+                        CALDataIdentify calDataIdentify = (CALDataIdentify) calData;
+                        switch (calDataIdentify.getAttribute()) {
+                            case Manufacturer:
+                                numBytes = 0x08;
+                                identifyReplyCommand = new IdentifyReplyCommandManufacturer("Apache", numBytes);
+                                break;
+                            case Type:
+                                numBytes = 0x08;
+                                identifyReplyCommand = new IdentifyReplyCommandType("plc4x-si", numBytes);
+                                break;
+                            case FirmwareVersion:
+                                numBytes = 0x08;
+                                identifyReplyCommand = new IdentifyReplyCommandFirmwareVersion("0.9", numBytes);
+                                break;
+                            case Summary:
+                                numBytes = 0x09;
+                                identifyReplyCommand = new IdentifyReplyCommandFirmwareSummary("0.9", (byte) 0xAF, "0.0", numBytes);
+                                break;
+                            case ExtendedDiagnosticSummary:
+                                numBytes = 0x0C;
+                                identifyReplyCommand = new IdentifyReplyCommandExtendedDiagnosticSummary(ApplicationIdContainer.FREE_USAGE_01, ApplicationIdContainer.FREE_USAGE_0F, (byte) 0x0, 0x0, 4711l, (byte) 0x13, false, false, false, true, false, false, false, false, false, false, false, false, false, numBytes);
+                                break;
+                            case NetworkTerminalLevels:
+                                numBytes = 0x0D;
+                                identifyReplyCommand = new IdentifyReplyCommandNetworkTerminalLevels(new byte[]{0x13}, numBytes);
+                                break;
+                            case TerminalLevel:
+                                numBytes = 0x0D;
+                                identifyReplyCommand = new IdentifyReplyCommandTerminalLevels(new byte[]{0x13}, numBytes);
+                                break;
+                            case NetworkVoltage:
+                                numBytes = 0x05;
+                                identifyReplyCommand = new IdentifyReplyCommandNetworkVoltage("48", "7", numBytes);
+                                break;
+                            case GAVValuesCurrent:
+                                numBytes = 0x10;
+                                identifyReplyCommand = new IdentifyReplyCommandGAVValuesCurrent(new byte[]{
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                }, numBytes);
+                                break;
+                            case GAVValuesStored:
+                                numBytes = 0x10;
+                                identifyReplyCommand = new IdentifyReplyCommandGAVValuesStored(new byte[]{
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                }, numBytes);
+                                break;
+                            case GAVPhysicalAddresses:
+                                numBytes = 0x10;
+                                identifyReplyCommand = new IdentifyReplyCommandGAVPhysicalAddresses(new byte[]{
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                    0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
+                                }, numBytes);
+                                break;
+                            case LogicalAssignment:
+                                numBytes = 0x0E;
+                                identifyReplyCommand = new IdentifyReplyCommandLogicalAssignment(List.of(new LogicAssignment(false, true, true, true, true, true)), numBytes);
+                                break;
+                            case Delays:
+                                numBytes = 0x0F;
+                                identifyReplyCommand = new IdentifyReplyCommandDelays(new byte[]{0x3}, (byte) 0x13, numBytes);
+                                break;
+                            case MinimumLevels:
+                                numBytes = 0x0E;
+                                identifyReplyCommand = new IdentifyReplyCommandMinimumLevels(new byte[]{0x3}, numBytes);
+                                break;
+                            case MaximumLevels:
+                                numBytes = 0x0F;
+                                identifyReplyCommand = new IdentifyReplyCommandMaximumLevels(new byte[]{0xF}, numBytes);
+                                break;
+                            case CurrentSenseLevels:
+                                numBytes = 0x10;
+                                identifyReplyCommand = new IdentifyReplyCommandCurrentSenseLevels(new byte[]{0xF}, numBytes);
+                                break;
+                            case OutputUnitSummary:
+                                numBytes = 0x12;
+                                identifyReplyCommand = new IdentifyReplyCommandOutputUnitSummary(new IdentifyReplyCommandUnitSummary(false, false, false, false, false, false, false, false), (byte) 0x4, (byte) 0x4, (short) 45, numBytes);
+                                break;
+                            case DSIStatus:
+                                numBytes = 0x12;
+                                identifyReplyCommand = new IdentifyReplyCommandDSIStatus(ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, ChannelStatus.OK, UnitStatus.OK, (byte) 0x34, numBytes);
+                                break;
+                            default:
+                                throw new IllegalStateException("unmapped type " + calDataIdentify.getAttribute());
+                        }
+
+                        calData = new CALDataIdentifyReply(getReplyCommandType(numBytes + 1), null, ((CALDataIdentify) calData).getAttribute(), identifyReplyCommand, requestContext);
+                        CALReply calReply;
+                        if (exstat) {
+                            calReply = new CALReplyLong((byte) 0x0, calData, (byte) 0x0, new UnitAddress((byte) 0x0), null, new SerialInterfaceAddress((byte) 0x02), (byte) 0x0, null, cBusOptions, requestContext);
+                        } else {
+                            calReply = new CALReplyShort((byte) 0x0, calData, cBusOptions, requestContext);
+                        }
+                        EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+                        ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, cBusOptions, requestContext);
+                        ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0xFF, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
+                        Alpha alpha = requestCommand.getAlpha();
+                        if (alpha != null) {
+                            Confirmation confirmation = new Confirmation(alpha, null, ConfirmationType.CONFIRMATION_SUCCESSFUL);
+                            replyOrConfirmation = new ReplyOrConfirmationConfirmation(alpha.getCharacter(), confirmation, replyOrConfirmation, cBusOptions, requestContext);
+                        }
+                        CBusMessage response = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+                        LOGGER.info("Send identify response\n{}", response);
+                        ctx.writeAndFlush(response);
+                    }
                     return;
                 }
                 if (cbusCommand instanceof CBusCommandPointToMultiPoint) {
@@ -451,4 +572,13 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         mmiMonitorFuture = null;
     }
 
+    private CALCommandTypeContainer getReplyCommandType(int numBytes) {
+        for (CALCommandTypeContainer value : CALCommandTypeContainer.values()) {
+            if (value.getCommandType() == CALCommandType.REPLY && value.getNumBytes() == numBytes) {
+                return value;
+            }
+        }
+        throw new IllegalArgumentException("No reply type for " + numBytes);
+    }
+
 }