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/08 09:59:56 UTC

[plc4x] branch develop updated: fix(cbus): implementd InterfaceRequirementsTest and fixed bugs

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


The following commit(s) were added to refs/heads/develop by this push:
     new 4e4395133 fix(cbus): implementd InterfaceRequirementsTest and fixed bugs
4e4395133 is described below

commit 4e439513366a9a75139dd6d6593e176bce8038d5
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon Aug 8 11:59:49 2022 +0200

    fix(cbus): implementd InterfaceRequirementsTest and fixed bugs
---
 .../cbus/readwrite/model/ConfirmationType.go       |   8 +
 .../protocols/cbus/readwrite/model/StaticHelper.go |   2 +-
 .../java/cbus/readwrite/utils/StaticHelper.java    |   2 +-
 .../org/apache/plc4x/java/cbus/ReferenceTest.java  | 318 ++++++++++++++++++---
 .../src/main/resources/protocols/cbus/c-bus.mspec  |   1 +
 5 files changed, 293 insertions(+), 38 deletions(-)

diff --git a/plc4go/protocols/cbus/readwrite/model/ConfirmationType.go b/plc4go/protocols/cbus/readwrite/model/ConfirmationType.go
index 6b2aee38a..e0ec111ba 100644
--- a/plc4go/protocols/cbus/readwrite/model/ConfirmationType.go
+++ b/plc4go/protocols/cbus/readwrite/model/ConfirmationType.go
@@ -40,6 +40,7 @@ const (
 	ConfirmationType_NOT_TRANSMITTED_CORRUPTION               ConfirmationType = 0x24
 	ConfirmationType_NOT_TRANSMITTED_SYNC_LOSS                ConfirmationType = 0x25
 	ConfirmationType_NOT_TRANSMITTED_TOO_LONG                 ConfirmationType = 0x27
+	ConfirmationType_CHECKSUM_FAILURE                         ConfirmationType = 0x21
 )
 
 var ConfirmationTypeValues []ConfirmationType
@@ -52,11 +53,14 @@ func init() {
 		ConfirmationType_NOT_TRANSMITTED_CORRUPTION,
 		ConfirmationType_NOT_TRANSMITTED_SYNC_LOSS,
 		ConfirmationType_NOT_TRANSMITTED_TOO_LONG,
+		ConfirmationType_CHECKSUM_FAILURE,
 	}
 }
 
 func ConfirmationTypeByValue(value byte) (enum ConfirmationType, ok bool) {
 	switch value {
+	case 0x21:
+		return ConfirmationType_CHECKSUM_FAILURE, true
 	case 0x23:
 		return ConfirmationType_NOT_TRANSMITTED_TO_MANY_RE_TRANSMISSIONS, true
 	case 0x24:
@@ -73,6 +77,8 @@ func ConfirmationTypeByValue(value byte) (enum ConfirmationType, ok bool) {
 
 func ConfirmationTypeByName(value string) (enum ConfirmationType, ok bool) {
 	switch value {
+	case "CHECKSUM_FAILURE":
+		return ConfirmationType_CHECKSUM_FAILURE, true
 	case "NOT_TRANSMITTED_TO_MANY_RE_TRANSMISSIONS":
 		return ConfirmationType_NOT_TRANSMITTED_TO_MANY_RE_TRANSMISSIONS, true
 	case "NOT_TRANSMITTED_CORRUPTION":
@@ -134,6 +140,8 @@ func (e ConfirmationType) Serialize(writeBuffer utils.WriteBuffer) error {
 // PLC4XEnumName returns the name that is used in code to identify this enum
 func (e ConfirmationType) PLC4XEnumName() string {
 	switch e {
+	case ConfirmationType_CHECKSUM_FAILURE:
+		return "CHECKSUM_FAILURE"
 	case ConfirmationType_NOT_TRANSMITTED_TO_MANY_RE_TRANSMISSIONS:
 		return "NOT_TRANSMITTED_TO_MANY_RE_TRANSMISSIONS"
 	case ConfirmationType_NOT_TRANSMITTED_CORRUPTION:
diff --git a/plc4go/protocols/cbus/readwrite/model/StaticHelper.go b/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
index cf8e75c8c..e0ffdb10d 100644
--- a/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
+++ b/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
@@ -112,7 +112,7 @@ func ReadCALData(readBuffer utils.ReadBuffer) (CALData, error) {
 func readBytesFromHex(logicalName string, readBuffer utils.ReadBuffer, srchk bool) ([]byte, error) {
 	payloadLength := findHexEnd(readBuffer)
 	if payloadLength == 0 {
-		return nil, errors.New("Length is 0")
+		return nil, utils.ParseAssertError{Message: "Length is 0"}
 	}
 	hexBytes, err := readBuffer.ReadByteArray(logicalName, payloadLength)
 	if err != nil {
diff --git a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
index 15c22fd77..d77126466 100644
--- a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
+++ b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
@@ -94,7 +94,7 @@ public class StaticHelper {
     private static byte[] readBytesFromHex(String logicalName, ReadBuffer readBuffer, boolean srchk) throws ParseException {
         int payloadLength = findHexEnd(readBuffer);
         if (payloadLength == 0) {
-            throw new ParseException("Length is 0");
+            throw new ParseAssertException("Length is 0");
         }
 
         byte[] hexBytes = readBuffer.readByteArray(logicalName, payloadLength);
diff --git a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/ReferenceTest.java b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/ReferenceTest.java
index d433316f4..3e626678b 100644
--- a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/ReferenceTest.java
+++ b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/ReferenceTest.java
@@ -47,58 +47,304 @@ public class ReferenceTest {
     // from: https://updates.clipsal.com/ClipsalSoftwareDownload/DL/downloads/OpenCBus/C-Bus%20Interface%20Requirements.pdf
     @Nested
     class InterfaceRequirementsTest {
-        // TODO: implement those
 
-
-        // 8.5
+        // 8.2
         @Nested
-        class Level4InterfaceImplementationRequirements {
+        class Level1InterfaceImplementationRequirements {
 
-            @Test
-            void Reset() throws Exception {
-                byte[] bytes = "~~~\r".getBytes(StandardCharsets.UTF_8);
-                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
-                assertThat(msg).isNotNull();
-                System.out.println(msg);
+            // 8.2.4
+            @Nested
+            class SerialInterfaceInitialisation {
 
-                assertMessageMatches(bytes, msg);
+                @Test
+                void Step_1_Reset() throws Exception {
+                    byte[] bytes = "~~~\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_2_SetInterfaceOptions3() throws Exception {
+                    byte[] bytes = "@A3420002\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_3_SetInterfaceOptions1_PUN() throws Exception {
+                    byte[] bytes = "@A3410058\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_4_SetInterfaceOptions1() throws Exception {
+                    byte[] bytes = "@A3300058\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
             }
 
-            @Test
-            void SetInterfaceOptions3() throws Exception {
-                byte[] bytes = "@A342000A\r".getBytes(StandardCharsets.UTF_8);
-                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
-                assertThat(msg).isNotNull();
-                System.out.println(msg);
+            // 8.2.5
+            @Nested
+            class ConfirmationOfTransmission {
+                @Test
+                void SomeCommand() throws Exception {
+                    byte[] bytes = "\\0538000121A1g\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
 
-                assertMessageMatches(bytes, msg);
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void success() throws Exception {
+                    byte[] bytes = "g.".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void checksumFailure() throws Exception {
+                    byte[] bytes = "g!".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void tooManyRetransmissions() throws Exception {
+                    byte[] bytes = "g#".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void corruptionInTransmission() throws Exception {
+                    byte[] bytes = "g$".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void noSystemClock() throws Exception {
+                    byte[] bytes = "g%".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
             }
+        }
 
-            @Test
-            void SetInterfaceOptions1_PUN() throws Exception {
-                byte[] bytes = "@A3410079\r".getBytes(StandardCharsets.UTF_8);
-                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
-                assertThat(msg).isNotNull();
-                System.out.println(msg);
+        // 8.3
+        @Nested
+        class Level2InterfaceImplementationRequirements {
 
-                assertMessageMatches(bytes, msg);
+            // 8.3.4
+            @Nested
+            class SerialInterfaceInitialisation {
+                @Test
+                void Step_1_Reset() throws Exception {
+                    byte[] bytes = "~~~\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_2_AnyApplicationFilter() throws Exception {
+                    byte[] bytes = "@A3210038\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_3_SetInterfaceOptions3() throws Exception {
+                    byte[] bytes = "@A3420002\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_4_SetInterfaceOptions1_PUN() throws Exception {
+                    byte[] bytes = "@A3410059\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_5_SetInterfaceOptions1() throws Exception {
+                    byte[] bytes = "@A3300059\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
             }
 
-            @Test
-            void SetInterfaceOptions1() throws Exception {
-                byte[] bytes = "@A3300079\r".getBytes(StandardCharsets.UTF_8);
-                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
-                assertThat(msg).isNotNull();
-                System.out.println(msg);
+            // 8.3.5
+            @Nested
+            class ProgrammingTheSerialInterfaceToFilterSALMessageTraffic {
 
-                assertMessageMatches(bytes, msg);
+                @Test
+                void Step_1_SelectOnlyLighting() throws Exception {
+                    byte[] bytes = "@A3210038\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_2_SelectHeatingAsSecondApplication() throws Exception {
+                    byte[] bytes = "@A3220088\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
             }
         }
 
+        // 8.4
+        @Nested
+        class Level3InterfaceImplementationRequirements {
+            // No specific tests
+        }
+
+        // 8.5
+        @Nested
+        class Level4InterfaceImplementationRequirements {
+
+            // 8.5.4
+            @Nested
+            class SerialInterfaceInitialisation {
+                @Test
+                void Step_1_Reset() throws Exception {
+                    byte[] bytes = "~~~\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_2_AnyApplicationFilter() throws Exception {
+                    byte[] bytes = "@A3210038\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_3_SetInterfaceOptions3() throws Exception {
+                    byte[] bytes = "@A342000A\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_4_SetInterfaceOptions1_PUN() throws Exception {
+                    byte[] bytes = "@A3410079\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+
+                @Test
+                void Step_5_SetInterfaceOptions1() throws Exception {
+                    byte[] bytes = "@A3300079\r".getBytes(StandardCharsets.UTF_8);
+                    ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                    CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+                    assertThat(msg).isNotNull();
+                    System.out.println(msg);
+
+                    assertMessageMatches(bytes, msg);
+                }
+            }
+        }
+
+        // 8.6
+        @Nested
+        class Level5InterfaceImplementationRequirements {
+            // No specific tests
+        }
+
+        // 8.7
+        @Nested
+        class Level6InterfaceImplementationRequirements {
+            // No specific tests
+        }
     }
 
     // from: https://updates.clipsal.com/ClipsalSoftwareDownload/DL/downloads/OpenCBus/Serial%20Interface%20User%20Guide.pdf
diff --git a/protocols/c-bus/src/main/resources/protocols/cbus/c-bus.mspec b/protocols/c-bus/src/main/resources/protocols/cbus/c-bus.mspec
index c50b41542..9fbd237a1 100644
--- a/protocols/c-bus/src/main/resources/protocols/cbus/c-bus.mspec
+++ b/protocols/c-bus/src/main/resources/protocols/cbus/c-bus.mspec
@@ -1557,6 +1557,7 @@
     ['0x24'    NOT_TRANSMITTED_CORRUPTION               ] // "$"
     ['0x25'    NOT_TRANSMITTED_SYNC_LOSS                ] // "%"
     ['0x27'    NOT_TRANSMITTED_TOO_LONG                 ] // "'"
+    ['0x21'    CHECKSUM_FAILURE                         ] // "!"
 ]
 
 [type PowerUp