Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cdutz/go ads ng (Streamlining of PLC4X API in PLC4Go and PLC4J) #576

Merged
merged 34 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
03c9fdb
chore(protocols): Added a pointer to a possibly interesting protocol …
chrisdutz Nov 1, 2022
c3d00f5
feat(ads): ADS Auto-Discovery
chrisdutz Nov 2, 2022
41a19c1
feat(ads): ADS Auto-Discovery
chrisdutz Nov 2, 2022
9a014f4
feat(ads): ADS Auto-Discovery
chrisdutz Nov 3, 2022
921d4cb
Merge remote-tracking branch 'origin/develop' into feature/cdutz/go-a…
chrisdutz Nov 3, 2022
b138c73
feat(protocols): Added a pointer to a possibly interesting protocol s…
chrisdutz Nov 4, 2022
87d81b8
refactor(api) PLC4J API refactoring
chrisdutz Nov 6, 2022
23f6814
Merge remote-tracking branch 'origin/develop' into feature/cdutz/go-a…
chrisdutz Nov 6, 2022
e4527f5
refactor(api) PLC4Go API refactoring
chrisdutz Nov 6, 2022
7368b14
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
ba88ccb
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
c10d47b
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
4664e11
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
93e5b64
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
ee321aa
refactor(api) PLC4Go API refactoring
chrisdutz Nov 7, 2022
8e9b4af
fix(test) Not all tests are executed when enabling the "jenkins-build…
chrisdutz Nov 7, 2022
c7aace7
fix(test) Not all tests are executed when enabling the "jenkins-build…
chrisdutz Nov 7, 2022
3a00255
fix(test) Fix RandomPackagesTest
chrisdutz Nov 8, 2022
96726d5
fix(test) Fix RandomPackagesTest
chrisdutz Nov 8, 2022
803eca2
fix(test) Fix error in WriteBufferByteBased, which output the padding…
chrisdutz Nov 8, 2022
dd08203
fix(test) Fix error in WriteBufferByteBased, which output the padding…
chrisdutz Nov 8, 2022
e1a2ab3
Merge remote-tracking branch 'origin/develop' into feature/cdutz/go-a…
chrisdutz Nov 8, 2022
558a82a
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
dcbfa53
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
cc2216c
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
a3b5a86
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
93c84d5
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
3dd976f
refactor(api) PLC4J API refactoring
chrisdutz Nov 9, 2022
08933ce
refactor(api) PLC4J API refactoring
chrisdutz Nov 10, 2022
a415886
refactor(api) PLC4Go API refactoring
chrisdutz Nov 10, 2022
39ba084
refactor(api) PLC4J API refactoring
chrisdutz Nov 10, 2022
22d38c5
refactor(api) PLC4Go API refactoring
chrisdutz Nov 10, 2022
6be695d
refactor(api) PLC4Go API refactoring
chrisdutz Nov 10, 2022
11dc603
Merge remote-tracking branch 'origin/develop' into feature/cdutz/go-a…
chrisdutz Nov 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pipeline {
// Test failures will be handled by the jenkins junit steps and mark the build as unstable.
MVN_TEST_FAIL_IGNORE = '-Dmaven.test.failure.ignore=true'

ENABLE_ALL_TESTS = true

SONARCLOUD_PARAMS = "-Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=apache -Dsonar.projectKey=apache_plc4x -Dsonar.branch.name=develop"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.text.CaseUtils;
import org.apache.plc4x.plugins.codegenerator.language.mspec.model.definitions.DefaultArgument;
import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultBooleanTypeReference;
import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultFloatTypeReference;
Expand Down Expand Up @@ -59,9 +60,18 @@ public String fileName(String protocolName, String languageName, String language
String.join("", languageFlavorName.split("\\-"));
}

public String getSanitizedPackageName() {
String sanitizedName = getProtocolName().replaceAll("-", "");
sanitizedName = sanitizedName.replaceAll("\\.", "/");
sanitizedName = sanitizedName.toLowerCase();
return sanitizedName;
}

// TODO: check if protocol name can be enforced to only contain valid chars
public String getSanitizedProtocolName() {
return getProtocolName().replaceAll("-", "");
String sanitizedName = getProtocolName().replaceAll("-", "");
sanitizedName = CaseUtils.toCamelCase(sanitizedName, false, '.');
return sanitizedName;
}

public String packageName(String languageFlavorName) {
Expand Down Expand Up @@ -712,6 +722,10 @@ private String toLiteralTermExpression(Field field, TypeReference fieldType, Ter
return tracer + "\"" + ((StringLiteral) term).getValue() + "\"";
} else if (term instanceof VariableLiteral) {
tracer = tracer.dive("variable literal instanceOf");
VariableLiteral variableLiteral = (VariableLiteral) term;
if ("curPos".equals(((VariableLiteral) term).getName())) {
return "(positionAware.GetPos() - startPos)";
}
return tracer + toVariableExpression(field, fieldType, (VariableLiteral) term, parserArguments, serializerArguments, serialize, suppressPointerAccess);
} else {
throw new RuntimeException("Unsupported Literal type " + term.getClass().getName());
Expand Down Expand Up @@ -1540,4 +1554,21 @@ public String capitalize(String str) {
String cleanedString = dummyTracer.removeTraces(str);
return extractedTrace + StringUtils.capitalize(cleanedString);
}

public String getEndiannessOptions(boolean read, boolean separatorPrefix) {
Optional<Term> byteOrder = thisType.getAttribute("byteOrder");
if (byteOrder.isPresent()) {
emitRequiredImport("encoding/binary");

String functionName = read ? "WithByteOrderForReadBufferByteBased" : "WithByteOrderForByteBasedBuffer";
String byteOrderValue = ((VariableLiteral) byteOrder.get()).getName();
if("BIG_ENDIAN".equals(byteOrderValue)) {
return (separatorPrefix ? ", " : "") + "utils." + functionName + "(binary.BigEndian)";
} else if ("LITTLE_ENDIAN".equals(byteOrderValue)) {
return (separatorPrefix ? ", " : "") + "utils." + functionName + "(binary.LittleEndian)";
}
}
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func (m *_${type.name}) GetLengthInBytes() uint16 {
<#assign parserArgumentList><#if hasParserArguments><#list parserArguments as parserArgument>${parserArgument.name} ${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if></#assign>
<#assign parserArgumentNameList><#if hasParserArguments><#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if></#assign>
func ${type.name}Parse(theBytes []byte<#if hasParserArguments>, ${parserArgumentList}</#if>) (${type.name}, error) {
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes, utils.WithByteOrderForReadBufferByteBased(binary.BigEndian))<#if hasParserArguments>, ${parserArgumentNameList}</#if>) <@emitImport import="encoding/binary" /> // TODO: get endianness from mspec
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes${helper.getEndiannessOptions(true, true)})<#if hasParserArguments>, ${parserArgumentNameList}</#if>)
}

func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArguments>, ${parserArgumentList}</#if>) (${type.name}, error) {
Expand All @@ -762,9 +762,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
var startPos = positionAware.GetPos()
_ = startPos
</#if>
<#if helper.requiresCurPos()>
var curPos uint16
</#if>
<#assign reservedFieldIndex=0>
<#list type.fields as field>
<#switch field.typeName>
Expand Down Expand Up @@ -799,10 +796,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
if pullErr := readBuffer.PullContext("${arrayField.name}", utils.WithRenderAsList(true)); pullErr != nil {
return nil, errors.Wrap(pullErr, "Error pulling for ${arrayField.name}")<@emitImport import="github.com/pkg/errors" />
}
<#-- Only update curPos if the length expression uses it -->
<#if arrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
<#-- If this is a count array, we can directly initialize an array with the given size -->
<#if field.isCountArrayField()>
// Count array
Expand Down Expand Up @@ -897,10 +890,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
<#else>
${arrayField.name} = append(${arrayField.name}, _item.(${arrayElementType.asComplexTypeReference().orElseThrow().name}))
</#if>
<#-- After parsing, update the current position, but only if it's needed -->
<#if arrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
}
}
<#-- A terminated array keeps on reading data as long as the termination expression evaluates to false -->
Expand Down Expand Up @@ -943,11 +932,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
<#else>
${arrayField.name} = append(${arrayField.name}, _item.(${arrayElementType.asComplexTypeReference().orElseThrow().name}))
</#if>

<#-- After parsing, update the current position, but only if it's needed -->
<#if arrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
}
}
</#if>
Expand Down Expand Up @@ -1066,10 +1050,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
return nil, errors.Wrap(pullErr, "Error pulling for ${manualArrayField.name}")<@emitImport import="github.com/pkg/errors" />
}
// Manual Array Field (${manualArrayField.name})
<#-- Only update curPos if the length expression uses it -->
<#if manualArrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
<#-- If this is a count array, we can directly initialize an array with the given size -->
<#if field.isCountArrayField()>
// Count array
Expand All @@ -1092,10 +1072,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
${manualArrayField.name}EndPos := positionAware.GetPos() + _${manualArrayField.name}Length
for ;positionAware.GetPos() < ${manualArrayField.name}EndPos; {
_${manualArrayField.name}List = append(_${manualArrayField.name}List, ((${helper.getLanguageTypeNameForField(field)}) (${helper.toParseExpression(manualArrayField, arrayElementType, manualArrayField.parseExpression, parserArguments)})))
<#-- After parsing, update the current position, but only if it's needed -->
<#if manualArrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
}
<#-- A terminated array keeps on reading data as long as the termination expression evaluates to false -->
<#elseif field.isTerminatedArrayField()>
Expand All @@ -1107,10 +1083,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
for ;!((bool) (${helper.toParseExpression(manualArrayField, helper.boolTypeReference, manualArrayField.loopExpression, parserArguments, true)})); {
_${manualArrayField.name}List = append(_${manualArrayField.name}List, ((${helper.getLanguageTypeNameForTypeReference(arrayElementType)}) (${helper.toParseExpression(manualArrayField, arrayElementType, manualArrayField.parseExpression, parserArguments)})))

<#-- After parsing, update the current position, but only if it's needed -->
<#if manualArrayField.loopExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
}
}
</#if>
Expand Down Expand Up @@ -1153,9 +1125,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
<#assign optionalField = field.asOptionalField().orElseThrow()>

// Optional Field (${optionalField.name}) (Can be skipped, if a given expression evaluates to false)
<#if optionalField.conditionExpression.present && optionalField.conditionExpression.get().contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
var ${optionalField.name} <#if !optionalField.type.isComplexTypeReference()>*</#if>${helper.getLanguageTypeNameForField(field)} = nil
<#if optionalField.conditionExpression.present>
if ${helper.toBooleanParseExpression(optionalField, optionalField.conditionExpression.get(), parserArguments)} </#if>{
Expand Down Expand Up @@ -1223,9 +1192,6 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if hasParserArgume
<#assign assertField = field.asAssertField().orElseThrow()>

// Assert Field (${assertField.name}) (Can be skipped, if a given expression evaluates to false)
<#if assertField.conditionExpression.contains("curPos")>
curPos = positionAware.GetPos() - startPos
</#if>
<#if assertField.type.isSimpleTypeReference()>
${assertField.name}, _err := ${helper.getReadBufferReadMethodCall(assertField.name, assertField.type.asSimpleTypeReference().orElseThrow(), assertField)}
if _err != nil {
Expand Down Expand Up @@ -1571,7 +1537,7 @@ func (pm *_${type.name}) SerializeParent(writeBuffer utils.WriteBuffer, child ${
_ = m
<#else>
func (m *_${type.name}) Serialize() ([]byte, error) {
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian), utils.WithInitialSizeForByteBasedBuffer(int(m.GetLengthInBytes())))<@emitImport import="encoding/binary" /> // TODO: get endianness from mspec
wb := utils.NewWriteBufferByteBased(utils.WithInitialSizeForByteBasedBuffer(int(m.GetLengthInBytes()))${helper.getEndiannessOptions(false, true)})
if err := m.SerializeWithWriteBuffer(wb); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import (
<#-- TODO: the code below implies that parserArguments will be null if not present... not pretty -->
<#if type.parserArguments.isPresent()><#assign parserArguments=type.parserArguments.orElseThrow()></#if>
func ${type.name}Parse(theBytes []byte<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if parserArgument.type.isNonSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) (api.PlcValue, error) {
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes, utils.WithByteOrderForReadBufferByteBased(binary.BigEndian))<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>) // TODO: get endianness from mspec
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes${helper.getEndiannessOptions(true, true)})<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>)
}

func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if parserArgument.type.isNonSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) (api.PlcValue, error) {
Expand Down Expand Up @@ -209,7 +209,7 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer<#if parserArguments
}

func ${type.name}Serialize(value api.PlcValue<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name} <#if parserArgument.type.isNonSimpleTypeReference() && !parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>, </#sep></#list></#if>) ([]byte, error) {
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))<@emitImport import="encoding/binary" /> // TODO: get endianness from mspec
wb := utils.NewWriteBufferByteBased(${helper.getEndiannessOptions(false, false)})
if err := ${type.name}SerializeWithWriteBuffer(wb, value<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ ${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/m
package model

import (
"encoding/binary"

"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -180,7 +178,7 @@ func (m ${type.name}) GetLengthInBytes() uint16 {
<#assign simpleTypeReference = type.type.orElseThrow().asSimpleTypeReference().orElseThrow()>
<#if simpleTypeReference.getSizeInBits() != -1>
func ${type.name}Parse(theBytes []byte) (${type.name}, error) {
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes, utils.WithByteOrderForReadBufferByteBased(binary.BigEndian))) // TODO: get endianness from mspec
return ${type.name}ParseWithBuffer(utils.NewReadBufferByteBased(theBytes${helper.getEndiannessOptions(true, true)}))
}

func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer) (${type.name}, error) {
Expand All @@ -197,7 +195,7 @@ func ${type.name}ParseWithBuffer(readBuffer utils.ReadBuffer) (${type.name}, err
}

func (e ${type.name}) Serialize() ([]byte, error) {
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian)) // TODO: get endianness from mspec
wb := utils.NewWriteBufferByteBased(${helper.getEndiannessOptions(false, false)})
if err := e.SerializeWithWriteBuffer(wb); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/P
package ${outputFlavor?replace("-","")}

import (
"github.com/apache/plc4x/plc4go/protocols/${helper.getSanitizedProtocolName()}/${outputFlavor?replace("-","")}/model"
"github.com/apache/plc4x/plc4go/protocols/${helper.getSanitizedPackageName()}/${outputFlavor?replace("-","")}/model"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/X
package ${outputFlavor?replace("-","")}

import (
"github.com/apache/plc4x/plc4go/protocols/${helper.getSanitizedProtocolName()}/${outputFlavor?replace("-","")}/model"
"github.com/apache/plc4x/plc4go/protocols/${helper.getSanitizedPackageName()}/${outputFlavor?replace("-","")}/model"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"strings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package ${helper.packageName(protocolName, languageName, outputFlavor)};

import static org.apache.plc4x.java.spi.generation.StaticHelper.*;

import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.value.*;
import org.apache.plc4x.java.spi.generation.EvaluationHelper;
import org.apache.plc4x.java.spi.generation.ParseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ STRING_CHARACTER
// Stuff we just want to ignore

LINE_COMMENT
: '//' ~[\r\n]* -> skip
: '//' ~[\r\n]* -> channel(HIDDEN)
;

BLOCK_COMMENT
: '/*' .*? '*/' -> skip
: '/*' .*? '*/' -> channel(HIDDEN)
;

WS
: [ \t\r\n\u000C]+ -> skip
: [ \t\r\n\u000C]+ -> channel(HIDDEN)
;
6 changes: 3 additions & 3 deletions plc4c/api/include/plc4c/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ extern "C" {
void plc4c_read_request_destroy(plc4c_read_request *read_request);

/**
* Function for adding a new item to a given request.
* Function for adding a new tag, based on a given address to a given request.
* @param read_request the read-request.
* @param address address string
* @return return code
*/
plc4c_return_code plc4c_read_request_add_item(plc4c_read_request *read_request,
char *field_name,
plc4c_return_code plc4c_read_request_add_tag_address(plc4c_read_request *read_request,
char *tag_name,
char *address);

/**
Expand Down
Loading