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/02 15:15:51 UTC

[plc4x] branch develop updated (f8935f069 -> 9b7cee0b7)

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

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


    from f8935f069 fix(plc-simulator/cbus): fixed NPE while stopping monitor
     new 324868edc fix(cbus): fixed setting of c-bus options
     new 9b7cee0b7 fix(plc4go/cbus): fixed reading of mmi

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 plc4go/internal/cbus/Connection.go                 |  5 +-
 plc4go/internal/cbus/MessageCodec.go               | 26 ++++--
 .../apache/plc4x/java/cbus/RandomPackagesTest.java | 11 +++
 .../server/cbus/protocol/CBusServerAdapter.java    | 93 +++++++++++++++++-----
 4 files changed, 109 insertions(+), 26 deletions(-)


[plc4x] 02/02: fix(plc4go/cbus): fixed reading of mmi

Posted by sr...@apache.org.
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 9b7cee0b7f6aaba30b1135065683591002fc7572
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Aug 2 17:15:44 2022 +0200

    fix(plc4go/cbus): fixed reading of mmi
---
 plc4go/internal/cbus/MessageCodec.go               | 26 ++++--
 .../apache/plc4x/java/cbus/RandomPackagesTest.java | 11 +++
 .../server/cbus/protocol/CBusServerAdapter.java    | 93 +++++++++++++++++-----
 3 files changed, 106 insertions(+), 24 deletions(-)

diff --git a/plc4go/internal/cbus/MessageCodec.go b/plc4go/internal/cbus/MessageCodec.go
index 8db9ff1bb..0d706b57a 100644
--- a/plc4go/internal/cbus/MessageCodec.go
+++ b/plc4go/internal/cbus/MessageCodec.go
@@ -179,13 +179,27 @@ lookingForTheEnd:
 	cBusMessage, err := readwriteModel.CBusMessageParse(rb, pciResponse, m.requestContext, m.cbusOptions)
 	if err != nil {
 		// TODO: bit bad we need to do this but cal detection is not reliable enough
-		rb := utils.NewReadBufferByteBased(read)
-		cBusMessage, secondErr := readwriteModel.CBusMessageParse(rb, pciResponse, readwriteModel.NewRequestContext(false, false, false), m.cbusOptions)
-		if secondErr == nil {
-			return cBusMessage, nil
-		} else {
-			log.Debug().Err(secondErr).Msg("Second parse failed too")
+		{ // Try SAL
+			rb := utils.NewReadBufferByteBased(read)
+			cBusMessage, secondErr := readwriteModel.CBusMessageParse(rb, pciResponse, readwriteModel.NewRequestContext(false, false, false), m.cbusOptions)
+			if secondErr == nil {
+				return cBusMessage, nil
+			} else {
+				log.Debug().Err(secondErr).Msg("SAL parse failed too")
+			}
 		}
+		{ // Try MMI
+			requestContext := readwriteModel.NewRequestContext(false, false, false)
+			cbusOptions := readwriteModel.NewCBusOptions(false, false, false, false, false, false, false, false, false)
+			rb := utils.NewReadBufferByteBased(read)
+			cBusMessage, secondErr := readwriteModel.CBusMessageParse(rb, true, requestContext, cbusOptions)
+			if secondErr == nil {
+				return cBusMessage, nil
+			} else {
+				log.Debug().Err(secondErr).Msg("CAL parse failed too")
+			}
+		}
+
 		log.Warn().Err(err).Msg("error parsing")
 		// TODO: Possibly clean up ...
 		return nil, nil
diff --git a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
index e095df882..789afc012 100644
--- a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
+++ b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
@@ -431,5 +431,16 @@ public class RandomPackagesTest {
         assertMessageMatches(bytes, msg);
     }
 
+    @Test
+    void incmoingMMI() throws Exception {
+        byte[] bytes = ("86040200F940380001000000000000000008000000000000000000000000FA\r\n").getBytes(StandardCharsets.UTF_8);
+        ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+        CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+        assertThat(msg).isNotNull();
+        System.out.println(msg);
+        System.out.println(((ReplyEncodedReply) ((ReplyOrConfirmationReply) ((CBusMessageToClient) msg).getReply()).getReply()).getEncodedReply());
+
+        assertMessageMatches(bytes, msg);
+    }
 
 }
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 a20f4f4bd..74bc2198a 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
@@ -20,7 +20,6 @@ package org.apache.plc4x.simulator.server.cbus.protocol;
 
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
-import io.vavr.CheckedRunnable;
 import org.apache.plc4x.java.cbus.readwrite.*;
 import org.apache.plc4x.simulator.model.Context;
 import org.slf4j.Logger;
@@ -53,7 +52,9 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
 
     private final Lock writeLock = new ReentrantLock();
 
-    private ScheduledFuture<?> sf;
+    private ScheduledFuture<?> salMonitorFuture;
+
+    private ScheduledFuture<?> mmiMonitorFuture;
 
     public CBusServerAdapter(Context context) {
         this.context = context;
@@ -68,8 +69,10 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
 
     @Override
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
-        if (sf != null)
-            sf.cancel(false);
+        if (salMonitorFuture != null)
+            salMonitorFuture.cancel(false);
+        if (mmiMonitorFuture != null)
+            mmiMonitorFuture.cancel(false);
         super.channelInactive(ctx);
     }
 
@@ -127,13 +130,15 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                             InterfaceOptions1 interfaceOptions1 = ((ParameterValueInterfaceOptions1) calDataWrite.getParameterValue()).getValue();
                             idmon = interfaceOptions1.getIdmon();
                             monitor = interfaceOptions1.getMonitor();
-                            if (monitor) startMonitor(ctx);
-                            else stopMonitor();
+                            if (monitor) startMMIMonitor(ctx);
+                            else stopMMIMonitor();
                             smart = interfaceOptions1.getSmart();
                             srchk = interfaceOptions1.getSrchk();
                             // TODO: add support for xonxoff
                             // xonxoff = interfaceOptions1.getXonXoff();
                             connect = interfaceOptions1.getConnect();
+                            if (connect) startSALMonitor(ctx);
+                            else stopSALMonitor();
                             buildCBusOptions();
                             acknowledger.run();
                             return;
@@ -167,13 +172,15 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                             InterfaceOptions1 interfaceOptions1PowerUpSettings = ((ParameterValueInterfaceOptions1PowerUpSettings) calDataWrite.getParameterValue()).getValue().getInterfaceOptions1();
                             idmon = interfaceOptions1PowerUpSettings.getIdmon();
                             monitor = interfaceOptions1PowerUpSettings.getMonitor();
-                            if (monitor) startMonitor(ctx);
-                            else stopMonitor();
+                            if (monitor) startMMIMonitor(ctx);
+                            else stopMMIMonitor();
                             smart = interfaceOptions1PowerUpSettings.getSmart();
                             srchk = interfaceOptions1PowerUpSettings.getSrchk();
                             // TODO: add support for xonxoff
                             // xonxoff = interfaceOptions1PowerUpSettings.getXonXoff();
                             connect = interfaceOptions1PowerUpSettings.getConnect();
+                            if (connect) startSALMonitor(ctx);
+                            else stopSALMonitor();
                             buildCBusOptions();
                             acknowledger.run();
                             return;
@@ -321,7 +328,7 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 pun = false;
                 pcn = false;
                 srchk = false;
-                stopMonitor();
+                stopSALMonitor();
                 return;
             }
             if (request instanceof RequestSmartConnectShortcut) {
@@ -335,13 +342,13 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         }
     }
 
-    private void startMonitor(ChannelHandlerContext ctx) {
-        if (sf != null) {
-            LOGGER.debug("Monitor already running");
+    private void startSALMonitor(ChannelHandlerContext ctx) {
+        if (salMonitorFuture != null) {
+            LOGGER.debug("SAL Monitor already running");
             return;
         }
         LOGGER.info("Starting monitor");
-        sf = ctx.executor().scheduleAtFixedRate(() -> {
+        salMonitorFuture = ctx.executor().scheduleAtFixedRate(() -> {
             try {
                 writeLock.lock();
                 MonitoredSAL monitoredSAL;
@@ -378,7 +385,57 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 Reply reply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
                 ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x00, reply, new ResponseTermination(), cBusOptions, requestContext);
                 CBusMessage message = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
-                LOGGER.info("[Monitor] Sending out\n{}\n{}", message, encodedReply);
+                LOGGER.info("[SAL Monitor] Sending out\n{}\n{}", message, encodedReply);
+                ctx.writeAndFlush(message);
+            } finally {
+                writeLock.unlock();
+            }
+        }, 5, 5, TimeUnit.SECONDS);
+    }
+
+    private void stopMMIMonitor() {
+        if (salMonitorFuture == null) {
+            return;
+        }
+        LOGGER.info("Stopping SAL monitor");
+        salMonitorFuture.cancel(false);
+        salMonitorFuture = null;
+    }
+
+    private void startMMIMonitor(ChannelHandlerContext ctx) {
+        if (mmiMonitorFuture != null) {
+            LOGGER.debug("MMI Monitor already running");
+            return;
+        }
+        LOGGER.info("Starting MMI monitor");
+        mmiMonitorFuture = ctx.executor().scheduleAtFixedRate(() -> {
+            // TODO: for whatever reason those are not send with a crc
+            cBusOptions = new CBusOptions(connect, smart, idmon, exstat, monitor, monall, pun, pcn, false);
+            try {
+                writeLock.lock();
+                CALReply calReply;
+                if (cBusOptions.getExstat()) {
+                    byte[] data = {
+                        /*00|*/0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, //'..........'
+                        /*10|*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //'..........'
+                        /*20|*/0x00, 0x00, (byte) 0xFA,                                    //'...       '
+                    };
+                    CALData calData = new CALDataStatusExtended(CALCommandTypeContainer.CALCommandStatusExtended_25Bytes, null, (short) 0x40, ApplicationIdContainer.LIGHTING_38, (byte) 0x00, data, requestContext);
+                    calReply = new CALReplyLong((byte) 0x86, calData, 0x00, new UnitAddress((byte) 0x04), null, new SerialInterfaceAddress((byte) 0x02), (byte) 0x00, null, cBusOptions, requestContext);
+                } else {
+                    byte[] data = {
+                        /*00|*/0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, //'..........'
+                        /*10|*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //'..........'
+                        /*20|*/0x00, 0x00, (byte) 0xFA,                                    //'...       '
+                    };
+                    CALData calData = new CALDataStatus(CALCommandTypeContainer.CALCommandStatus_25Bytes, null, ApplicationIdContainer.LIGHTING_38, (byte) 0x00, data, requestContext);
+                    calReply = new CALReplyShort((byte) 0x0, calData, cBusOptions, requestContext);
+                }
+                EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+                Reply reply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
+                ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x00, reply, new ResponseTermination(), cBusOptions, requestContext);
+                CBusMessage message = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+                LOGGER.info("[MMI Monitor] Sending out\n{}\n{}", message, encodedReply);
                 ctx.writeAndFlush(message);
             } finally {
                 writeLock.unlock();
@@ -386,13 +443,13 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         }, 5, 5, TimeUnit.SECONDS);
     }
 
-    private void stopMonitor() {
-        if (sf == null) {
+    private void stopSALMonitor() {
+        if (mmiMonitorFuture == null) {
             return;
         }
         LOGGER.info("Stopping monitor");
-        sf.cancel(false);
-        sf = null;
+        mmiMonitorFuture.cancel(false);
+        mmiMonitorFuture = null;
     }
 
 }


[plc4x] 01/02: fix(cbus): fixed setting of c-bus options

Posted by sr...@apache.org.
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 324868edc250b5aa288782dde1a9e61773b339f6
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Aug 2 15:07:55 2022 +0200

    fix(cbus): fixed setting of c-bus options
---
 plc4go/internal/cbus/Connection.go | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/plc4go/internal/cbus/Connection.go b/plc4go/internal/cbus/Connection.go
index f6a991692..e9375d18c 100644
--- a/plc4go/internal/cbus/Connection.go
+++ b/plc4go/internal/cbus/Connection.go
@@ -243,6 +243,7 @@ func (c *Connection) setupConnection(ch chan plc4go.PlcConnectionConnectResult)
 		if !c.sendCalDataWrite(ch, readWriteModel.Parameter_INTERFACE_OPTIONS_3, interfaceOptions3, requestContext, cbusOptions) {
 			return
 		}
+		// TODO: add localsal to the options
 		*cbusOptions = readWriteModel.NewCBusOptions(false, false, false, true, false, false, false, false, false)
 		log.Debug().Msg("Interface options 3 set")
 	}
@@ -252,7 +253,7 @@ func (c *Connection) setupConnection(ch chan plc4go.PlcConnectionConnectResult)
 		if !c.sendCalDataWrite(ch, readWriteModel.Parameter_INTERFACE_OPTIONS_1_POWER_UP_SETTINGS, interfaceOptions1PowerUpSettings, requestContext, cbusOptions) {
 			return
 		}
-		*cbusOptions = readWriteModel.NewCBusOptions(false, true, true, true, true, false, false, false, true)
+		*cbusOptions = readWriteModel.NewCBusOptions(true, true, true, true, true, false, false, false, true)
 		log.Debug().Msg("Interface options 1 power up settings set")
 	}
 	{
@@ -261,7 +262,7 @@ func (c *Connection) setupConnection(ch chan plc4go.PlcConnectionConnectResult)
 		if !c.sendCalDataWrite(ch, readWriteModel.Parameter_INTERFACE_OPTIONS_1, interfaceOptions1, requestContext, cbusOptions) {
 			return
 		}
-		*cbusOptions = readWriteModel.NewCBusOptions(false, true, true, true, true, false, false, false, true)
+		*cbusOptions = readWriteModel.NewCBusOptions(true, true, true, true, true, false, false, false, true)
 		log.Debug().Msg("Interface options 1 set")
 	}
 	c.fireConnected(ch)