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/07/19 13:29:16 UTC

[plc4x] 02/03: fix(cbus): fixed vstrings

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 d607519675397a97e1d68747ce74eeb2514946e9
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Jul 19 13:25:28 2022 +0200

    fix(cbus): fixed vstrings
---
 .../MediaTransportControlDataSelectionName.go      |  6 +--
 .../model/MediaTransportControlDataTrackName.go    |  6 +--
 .../model/SALDataTelephonyStatusAndControl.go      | 54 +++++++++++++++++++---
 .../readwrite/model/SecurityDataDisplayMessage.go  |  6 +--
 .../java/cbus/readwrite/utils/StaticHelper.java    | 11 +++++
 .../org/apache/plc4x/java/cbus/ReferenceTest.java  | 48 +++++++++++++++++++
 .../src/main/resources/protocols/cbus/c-bus.mspec  |  6 +--
 7 files changed, 119 insertions(+), 18 deletions(-)

diff --git a/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataSelectionName.go b/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataSelectionName.go
index acd89ec13..2d0487e36 100644
--- a/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataSelectionName.go
+++ b/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataSelectionName.go
@@ -114,7 +114,7 @@ func (m *_MediaTransportControlDataSelectionName) GetLengthInBitsConditional(las
 	lengthInBits := uint16(m.GetParentLengthInBits())
 
 	// Simple field (selectionName)
-	lengthInBits += uint16(int32(m.GetCommandTypeContainer().NumBytes()) - int32(int32(1)))
+	lengthInBits += uint16(int32(int32(int32(m.GetCommandTypeContainer().NumBytes())-int32(int32(1)))) * int32(int32(8)))
 
 	return lengthInBits
 }
@@ -133,7 +133,7 @@ func MediaTransportControlDataSelectionNameParse(readBuffer utils.ReadBuffer, co
 	_ = currentPos
 
 	// Simple Field (selectionName)
-	_selectionName, _selectionNameErr := readBuffer.ReadString("selectionName", uint32((commandTypeContainer.NumBytes())-(1)))
+	_selectionName, _selectionNameErr := readBuffer.ReadString("selectionName", uint32(((commandTypeContainer.NumBytes())-(1))*(8)))
 	if _selectionNameErr != nil {
 		return nil, errors.Wrap(_selectionNameErr, "Error parsing 'selectionName' field of MediaTransportControlDataSelectionName")
 	}
@@ -162,7 +162,7 @@ func (m *_MediaTransportControlDataSelectionName) Serialize(writeBuffer utils.Wr
 
 		// Simple Field (selectionName)
 		selectionName := string(m.GetSelectionName())
-		_selectionNameErr := writeBuffer.WriteString("selectionName", uint32((m.GetCommandTypeContainer().NumBytes())-(1)), "UTF-8", (selectionName))
+		_selectionNameErr := writeBuffer.WriteString("selectionName", uint32(((m.GetCommandTypeContainer().NumBytes())-(1))*(8)), "UTF-8", (selectionName))
 		if _selectionNameErr != nil {
 			return errors.Wrap(_selectionNameErr, "Error serializing 'selectionName' field")
 		}
diff --git a/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataTrackName.go b/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataTrackName.go
index b9b204d23..c3213186c 100644
--- a/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataTrackName.go
+++ b/plc4go/protocols/cbus/readwrite/model/MediaTransportControlDataTrackName.go
@@ -114,7 +114,7 @@ func (m *_MediaTransportControlDataTrackName) GetLengthInBitsConditional(lastIte
 	lengthInBits := uint16(m.GetParentLengthInBits())
 
 	// Simple field (trackName)
-	lengthInBits += uint16(int32(m.GetCommandTypeContainer().NumBytes()) - int32(int32(1)))
+	lengthInBits += uint16(int32(int32(int32(m.GetCommandTypeContainer().NumBytes())-int32(int32(1)))) * int32(int32(8)))
 
 	return lengthInBits
 }
@@ -133,7 +133,7 @@ func MediaTransportControlDataTrackNameParse(readBuffer utils.ReadBuffer, comman
 	_ = currentPos
 
 	// Simple Field (trackName)
-	_trackName, _trackNameErr := readBuffer.ReadString("trackName", uint32((commandTypeContainer.NumBytes())-(1)))
+	_trackName, _trackNameErr := readBuffer.ReadString("trackName", uint32(((commandTypeContainer.NumBytes())-(1))*(8)))
 	if _trackNameErr != nil {
 		return nil, errors.Wrap(_trackNameErr, "Error parsing 'trackName' field of MediaTransportControlDataTrackName")
 	}
@@ -162,7 +162,7 @@ func (m *_MediaTransportControlDataTrackName) Serialize(writeBuffer utils.WriteB
 
 		// Simple Field (trackName)
 		trackName := string(m.GetTrackName())
-		_trackNameErr := writeBuffer.WriteString("trackName", uint32((m.GetCommandTypeContainer().NumBytes())-(1)), "UTF-8", (trackName))
+		_trackNameErr := writeBuffer.WriteString("trackName", uint32(((m.GetCommandTypeContainer().NumBytes())-(1))*(8)), "UTF-8", (trackName))
 		if _trackNameErr != nil {
 			return errors.Wrap(_trackNameErr, "Error serializing 'trackName' field")
 		}
diff --git a/plc4go/protocols/cbus/readwrite/model/SALDataTelephonyStatusAndControl.go b/plc4go/protocols/cbus/readwrite/model/SALDataTelephonyStatusAndControl.go
index 12c84e581..42acad0b5 100644
--- a/plc4go/protocols/cbus/readwrite/model/SALDataTelephonyStatusAndControl.go
+++ b/plc4go/protocols/cbus/readwrite/model/SALDataTelephonyStatusAndControl.go
@@ -31,6 +31,8 @@ type SALDataTelephonyStatusAndControl interface {
 	utils.LengthAware
 	utils.Serializable
 	SALData
+	// GetTelephonyData returns TelephonyData (property field)
+	GetTelephonyData() TelephonyData
 }
 
 // SALDataTelephonyStatusAndControlExactly can be used when we want exactly this type and not a type which fulfills SALDataTelephonyStatusAndControl.
@@ -43,6 +45,7 @@ type SALDataTelephonyStatusAndControlExactly interface {
 // _SALDataTelephonyStatusAndControl is the data-structure of this message
 type _SALDataTelephonyStatusAndControl struct {
 	*_SALData
+	TelephonyData TelephonyData
 }
 
 ///////////////////////////////////////////////////////////
@@ -67,10 +70,25 @@ func (m *_SALDataTelephonyStatusAndControl) GetParent() SALData {
 	return m._SALData
 }
 
+///////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////
+/////////////////////// Accessors for property fields.
+///////////////////////
+
+func (m *_SALDataTelephonyStatusAndControl) GetTelephonyData() TelephonyData {
+	return m.TelephonyData
+}
+
+///////////////////////
+///////////////////////
+///////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////
+
 // NewSALDataTelephonyStatusAndControl factory function for _SALDataTelephonyStatusAndControl
-func NewSALDataTelephonyStatusAndControl(salData SALData) *_SALDataTelephonyStatusAndControl {
+func NewSALDataTelephonyStatusAndControl(telephonyData TelephonyData, salData SALData) *_SALDataTelephonyStatusAndControl {
 	_result := &_SALDataTelephonyStatusAndControl{
-		_SALData: NewSALData(salData),
+		TelephonyData: telephonyData,
+		_SALData:      NewSALData(salData),
 	}
 	_result._SALData._SALDataChildRequirements = _result
 	return _result
@@ -98,6 +116,9 @@ func (m *_SALDataTelephonyStatusAndControl) GetLengthInBits() uint16 {
 func (m *_SALDataTelephonyStatusAndControl) GetLengthInBitsConditional(lastItem bool) uint16 {
 	lengthInBits := uint16(m.GetParentLengthInBits())
 
+	// Simple field (telephonyData)
+	lengthInBits += m.TelephonyData.GetLengthInBits()
+
 	return lengthInBits
 }
 
@@ -114,9 +135,17 @@ func SALDataTelephonyStatusAndControlParse(readBuffer utils.ReadBuffer, applicat
 	currentPos := positionAware.GetPos()
 	_ = currentPos
 
-	// Validation
-	if !(bool((1) == (2))) {
-		return nil, errors.WithStack(utils.ParseValidationError{"TELEPHONY_STATUS_AND_CONTROL Not yet implemented"})
+	// Simple Field (telephonyData)
+	if pullErr := readBuffer.PullContext("telephonyData"); pullErr != nil {
+		return nil, errors.Wrap(pullErr, "Error pulling for telephonyData")
+	}
+	_telephonyData, _telephonyDataErr := TelephonyDataParse(readBuffer)
+	if _telephonyDataErr != nil {
+		return nil, errors.Wrap(_telephonyDataErr, "Error parsing 'telephonyData' field of SALDataTelephonyStatusAndControl")
+	}
+	telephonyData := _telephonyData.(TelephonyData)
+	if closeErr := readBuffer.CloseContext("telephonyData"); closeErr != nil {
+		return nil, errors.Wrap(closeErr, "Error closing for telephonyData")
 	}
 
 	if closeErr := readBuffer.CloseContext("SALDataTelephonyStatusAndControl"); closeErr != nil {
@@ -125,7 +154,8 @@ func SALDataTelephonyStatusAndControlParse(readBuffer utils.ReadBuffer, applicat
 
 	// Create a partially initialized instance
 	_child := &_SALDataTelephonyStatusAndControl{
-		_SALData: &_SALData{},
+		TelephonyData: telephonyData,
+		_SALData:      &_SALData{},
 	}
 	_child._SALData._SALDataChildRequirements = _child
 	return _child, nil
@@ -139,6 +169,18 @@ func (m *_SALDataTelephonyStatusAndControl) Serialize(writeBuffer utils.WriteBuf
 			return errors.Wrap(pushErr, "Error pushing for SALDataTelephonyStatusAndControl")
 		}
 
+		// Simple Field (telephonyData)
+		if pushErr := writeBuffer.PushContext("telephonyData"); pushErr != nil {
+			return errors.Wrap(pushErr, "Error pushing for telephonyData")
+		}
+		_telephonyDataErr := writeBuffer.WriteSerializable(m.GetTelephonyData())
+		if popErr := writeBuffer.PopContext("telephonyData"); popErr != nil {
+			return errors.Wrap(popErr, "Error popping for telephonyData")
+		}
+		if _telephonyDataErr != nil {
+			return errors.Wrap(_telephonyDataErr, "Error serializing 'telephonyData' field")
+		}
+
 		if popErr := writeBuffer.PopContext("SALDataTelephonyStatusAndControl"); popErr != nil {
 			return errors.Wrap(popErr, "Error popping for SALDataTelephonyStatusAndControl")
 		}
diff --git a/plc4go/protocols/cbus/readwrite/model/SecurityDataDisplayMessage.go b/plc4go/protocols/cbus/readwrite/model/SecurityDataDisplayMessage.go
index ae0766f7b..6790084d9 100644
--- a/plc4go/protocols/cbus/readwrite/model/SecurityDataDisplayMessage.go
+++ b/plc4go/protocols/cbus/readwrite/model/SecurityDataDisplayMessage.go
@@ -114,7 +114,7 @@ func (m *_SecurityDataDisplayMessage) GetLengthInBitsConditional(lastItem bool)
 	lengthInBits := uint16(m.GetParentLengthInBits())
 
 	// Simple field (message)
-	lengthInBits += uint16(int32(m.GetCommandTypeContainer().NumBytes()) - int32(int32(1)))
+	lengthInBits += uint16(int32(int32(int32(m.GetCommandTypeContainer().NumBytes())-int32(int32(1)))) * int32(int32(8)))
 
 	return lengthInBits
 }
@@ -133,7 +133,7 @@ func SecurityDataDisplayMessageParse(readBuffer utils.ReadBuffer, commandTypeCon
 	_ = currentPos
 
 	// Simple Field (message)
-	_message, _messageErr := readBuffer.ReadString("message", uint32((commandTypeContainer.NumBytes())-(1)))
+	_message, _messageErr := readBuffer.ReadString("message", uint32(((commandTypeContainer.NumBytes())-(1))*(8)))
 	if _messageErr != nil {
 		return nil, errors.Wrap(_messageErr, "Error parsing 'message' field of SecurityDataDisplayMessage")
 	}
@@ -162,7 +162,7 @@ func (m *_SecurityDataDisplayMessage) Serialize(writeBuffer utils.WriteBuffer) e
 
 		// Simple Field (message)
 		message := string(m.GetMessage())
-		_messageErr := writeBuffer.WriteString("message", uint32((m.GetCommandTypeContainer().NumBytes())-(1)), "UTF-8", (message))
+		_messageErr := writeBuffer.WriteString("message", uint32(((m.GetCommandTypeContainer().NumBytes())-(1))*(8)), "UTF-8", (message))
 		if _messageErr != nil {
 			return errors.Wrap(_messageErr, "Error serializing 'message' field")
 		}
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 3148f675b..a8dd4b447 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
@@ -194,4 +194,15 @@ public class StaticHelper {
         }
     }
 
+    public static boolean knowsTelephonyCommandTypeContainer(ReadBuffer readBuffer) {
+        int oldPos = readBuffer.getPos();
+        try {
+            return TelephonyCommandTypeContainer.isDefined(readBuffer.readUnsignedShort(8));
+        } catch (ParseException ignore) {
+            return false;
+        } finally {
+            readBuffer.reset(oldPos);
+        }
+    }
+
 }
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 ca3504545..7df8fa4ff 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
@@ -1153,4 +1153,52 @@ public class ReferenceTest {
             }
         }
     }
+
+    // from: https://updates.clipsal.com/ClipsalSoftwareDownload/DL/downloads/OpenCBus/Chapter%2024%20-%20C-Bus%20Telephony%20Application.pdf
+    @Nested
+    class Telephony{
+
+        //24.11
+        @Nested
+        class Examples{
+
+            @Test
+            void LineOnHook() throws Exception {
+                byte[] bytes = "\\05E000090111\r".getBytes(StandardCharsets.UTF_8);
+                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                cBusOptions = new CBusOptions(false, false, false, false, false, false, false, false, true);
+                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions, bytes.length);
+                assertThat(msg).isNotNull();
+                System.out.println(msg);
+                System.out.println(((RequestCommand) ((CBusMessageToServer) msg).getRequest()).getCbusCommand());
+                assertMessageMatches(bytes, msg);
+            }
+
+            @Disabled("Again it seems as this command is just wrong... there is no command definition for 2C")
+            @Test
+            void LineOffHook() throws Exception {
+                byte[] bytes = "\\05E0002C020230333935323734333231FD\r".getBytes(StandardCharsets.UTF_8);
+                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                cBusOptions = new CBusOptions(false, false, false, false, false, false, false, false, true);
+                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions, bytes.length);
+                assertThat(msg).isNotNull();
+                System.out.println(msg);
+                System.out.println(((RequestCommand) ((CBusMessageToServer) msg).getRequest()).getCbusCommand());
+                assertMessageMatches(bytes, msg);
+            }
+
+            @Test
+            void LineOffHookFixedQuestionMark() throws Exception {
+                byte[] bytes = "\\05E000AC020130333935323734333231FD\r".getBytes(StandardCharsets.UTF_8);
+                ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+                cBusOptions = new CBusOptions(false, false, false, false, false, false, false, false, true);
+                CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions, bytes.length);
+                assertThat(msg).isNotNull();
+                System.out.println(msg);
+                System.out.println(((RequestCommand) ((CBusMessageToServer) msg).getRequest()).getCbusCommand());
+                assertMessageMatches(bytes, msg);
+            }
+
+        }
+    }
 }
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 fa1533fac..ddd616e87 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
@@ -1689,7 +1689,7 @@
             [virtual bit  isVacation    'key == 0x87'                ]
         ]
         ['ON', '0xA6'       *DisplayMessage(SecurityCommandTypeContainer commandTypeContainer)
-            [simple vstring 'commandTypeContainer.numBytes-1' message                           ]
+            [simple vstring '(commandTypeContainer.numBytes-1)*8' message                           ]
         ]
         ['EVENT', '0xA7'    *RequestZoneName
             [simple uint 8      zoneNumber  ]
@@ -2332,10 +2332,10 @@
             [simple  uint 8 size                                        ]
         ]
         ['TRACK_NAME'       *TrackName(MediaTransportControlCommandTypeContainer commandTypeContainer)
-             [simple vstring 'commandTypeContainer.numBytes-1' trackName                                    ]
+             [simple vstring '(commandTypeContainer.numBytes-1)*8' trackName                                    ]
         ]
         ['SELECTION_NAME'   *SelectionName(MediaTransportControlCommandTypeContainer commandTypeContainer)
-             [simple vstring 'commandTypeContainer.numBytes-1' selectionName                                ]
+             [simple vstring '(commandTypeContainer.numBytes-1)*8' selectionName                                ]
         ]
     ]
 ]