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 2024/08/31 21:24:21 UTC
(plc4x) 08/35: feat(code-gen/go): migrate checksum field
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 454c96431c4379d94cc81aa4aaf5d5837a384db6
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Aug 30 23:30:56 2024 +0200
feat(code-gen/go): migrate checksum field
---
.../templates/go/complex-type-template.go.ftlh | 21 +++++----------------
.../df1/readwrite/model/DF1SymbolMessageFrame.go | 19 +++++--------------
.../modbus/readwrite/model/ModbusAsciiADU.go | 19 +++++--------------
.../modbus/readwrite/model/ModbusRtuADU.go | 19 +++++--------------
4 files changed, 20 insertions(+), 58 deletions(-)
diff --git a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
index 78e41ec232..d5cbc86636 100644
--- a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
+++ b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
@@ -838,23 +838,12 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
<#break>
<#case "checksum">
<#assign checksumField = field.asChecksumField().orElseThrow()>
- <#assign simpleTypeReference = checksumField.type.asSimpleTypeReference().orElseThrow()>
+ <#assign typedField = field.asTypedField().orElseThrow()>
+ <#assign namedField = field.asNamedField().orElseThrow()>
- // Checksum Field (checksum)
- {
- checksumRef, _checksumRefErr := ${helper.getReadBufferReadMethodCall("checksum", simpleTypeReference, checksumField)}
- if _checksumRefErr != nil {
- return nil, errors.Wrap(_checksumRefErr, "Error parsing 'checksum' field of ${type.name}")<@emitImport import="github.com/pkg/errors" />
- }
- checksum, _checksumErr:= ${helper.toParseExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}
- if _checksumErr != nil {
- return nil, errors.Wrap(_checksumErr, "Checksum verification failed")
- }
- if checksum != checksumRef {
- return nil, errors.Errorf("Checksum verification failed. Expected %x but got %x", checksumRef, checksum)<@emitImport import="github.com/pkg/errors" />
- }
- }
- <#break>
+ ${namedField.name} := Read${field.typeName?cap_first}Field("${namedField.name}", ${helper.getDataReaderCall(typedField.type)}, (${helper.getLanguageTypeNameForField(field)}) (${helper.toParseExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)})${helper.getFieldOptions(typedField, parserArguments)});
+ _ = ${namedField.name}
+ <#break>
<#case "const">
<#assign constField = field.asConstField().orElseThrow()>
diff --git a/plc4go/protocols/df1/readwrite/model/DF1SymbolMessageFrame.go b/plc4go/protocols/df1/readwrite/model/DF1SymbolMessageFrame.go
index 98068d1423..0096449c16 100644
--- a/plc4go/protocols/df1/readwrite/model/DF1SymbolMessageFrame.go
+++ b/plc4go/protocols/df1/readwrite/model/DF1SymbolMessageFrame.go
@@ -27,6 +27,9 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
+ "github.com/apache/plc4x/plc4go/spi/codegen"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/fields"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/io"
"github.com/apache/plc4x/plc4go/spi/utils"
)
@@ -239,20 +242,8 @@ func DF1SymbolMessageFrameParseWithBuffer(ctx context.Context, readBuffer utils.
return nil, errors.New("Expected constant value " + fmt.Sprintf("%d", DF1SymbolMessageFrame_ENDTRANSACTION) + " but got " + fmt.Sprintf("%d", endTransaction))
}
- // Checksum Field (checksum)
- {
- checksumRef, _checksumRefErr := /*TODO: migrate me*/ /*TODO: migrate me*/ readBuffer.ReadUint16("checksum", 16)
- if _checksumRefErr != nil {
- return nil, errors.Wrap(_checksumRefErr, "Error parsing 'checksum' field of DF1SymbolMessageFrame")
- }
- checksum, _checksumErr := CrcCheck(ctx, destinationAddress, sourceAddress, command)
- if _checksumErr != nil {
- return nil, errors.Wrap(_checksumErr, "Checksum verification failed")
- }
- if checksum != checksumRef {
- return nil, errors.Errorf("Checksum verification failed. Expected %x but got %x", checksumRef, checksum)
- }
- }
+ crc := ReadChecksumField("crc", ReadUnsignedShort(readBuffer, 16), (uint16)(CrcCheck(ctx, destinationAddress, sourceAddress, command)), codegen.WithByteOrder(binary.BigEndian))
+ _ = crc
if closeErr := readBuffer.CloseContext("DF1SymbolMessageFrame"); closeErr != nil {
return nil, errors.Wrap(closeErr, "Error closing for DF1SymbolMessageFrame")
diff --git a/plc4go/protocols/modbus/readwrite/model/ModbusAsciiADU.go b/plc4go/protocols/modbus/readwrite/model/ModbusAsciiADU.go
index 20b3853912..5ac48f404c 100644
--- a/plc4go/protocols/modbus/readwrite/model/ModbusAsciiADU.go
+++ b/plc4go/protocols/modbus/readwrite/model/ModbusAsciiADU.go
@@ -27,6 +27,9 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
+ "github.com/apache/plc4x/plc4go/spi/codegen"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/fields"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/io"
"github.com/apache/plc4x/plc4go/spi/utils"
)
@@ -176,20 +179,8 @@ func ModbusAsciiADUParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuf
return nil, errors.Wrap(closeErr, "Error closing for pdu")
}
- // Checksum Field (checksum)
- {
- checksumRef, _checksumRefErr := /*TODO: migrate me*/ /*TODO: migrate me*/ readBuffer.ReadUint8("checksum", 8)
- if _checksumRefErr != nil {
- return nil, errors.Wrap(_checksumRefErr, "Error parsing 'checksum' field of ModbusAsciiADU")
- }
- checksum, _checksumErr := AsciiLrcCheck(ctx, address, pdu)
- if _checksumErr != nil {
- return nil, errors.Wrap(_checksumErr, "Checksum verification failed")
- }
- if checksum != checksumRef {
- return nil, errors.Errorf("Checksum verification failed. Expected %x but got %x", checksumRef, checksum)
- }
- }
+ crc := ReadChecksumField("crc", ReadUnsignedByte(readBuffer, 8), (uint8)(AsciiLrcCheck(ctx, address, pdu)), codegen.WithByteOrder(binary.BigEndian))
+ _ = crc
if closeErr := readBuffer.CloseContext("ModbusAsciiADU"); closeErr != nil {
return nil, errors.Wrap(closeErr, "Error closing for ModbusAsciiADU")
diff --git a/plc4go/protocols/modbus/readwrite/model/ModbusRtuADU.go b/plc4go/protocols/modbus/readwrite/model/ModbusRtuADU.go
index 2d20d8bd0f..e8a4f334b6 100644
--- a/plc4go/protocols/modbus/readwrite/model/ModbusRtuADU.go
+++ b/plc4go/protocols/modbus/readwrite/model/ModbusRtuADU.go
@@ -27,6 +27,9 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
+ "github.com/apache/plc4x/plc4go/spi/codegen"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/fields"
+ . "github.com/apache/plc4x/plc4go/spi/codegen/io"
"github.com/apache/plc4x/plc4go/spi/utils"
)
@@ -176,20 +179,8 @@ func ModbusRtuADUParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
return nil, errors.Wrap(closeErr, "Error closing for pdu")
}
- // Checksum Field (checksum)
- {
- checksumRef, _checksumRefErr := /*TODO: migrate me*/ /*TODO: migrate me*/ readBuffer.ReadUint16("checksum", 16)
- if _checksumRefErr != nil {
- return nil, errors.Wrap(_checksumRefErr, "Error parsing 'checksum' field of ModbusRtuADU")
- }
- checksum, _checksumErr := RtuCrcCheck(ctx, address, pdu)
- if _checksumErr != nil {
- return nil, errors.Wrap(_checksumErr, "Checksum verification failed")
- }
- if checksum != checksumRef {
- return nil, errors.Errorf("Checksum verification failed. Expected %x but got %x", checksumRef, checksum)
- }
- }
+ crc := ReadChecksumField("crc", ReadUnsignedShort(readBuffer, 16), (uint16)(RtuCrcCheck(ctx, address, pdu)), codegen.WithByteOrder(binary.BigEndian))
+ _ = crc
if closeErr := readBuffer.CloseContext("ModbusRtuADU"); closeErr != nil {
return nil, errors.Wrap(closeErr, "Error closing for ModbusRtuADU")