Skip to content

Commit 86f2a8d

Browse files
committed
fix(plc4c): generate constants.c
1 parent 9d8d737 commit 86f2a8d

4 files changed

Lines changed: 161 additions & 3 deletions

File tree

code-generation/language/c/src/main/java/org/apache/plc4x/language/c/CLanguageOutput.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@ protected List<Template> getSpecTemplates(Configuration freemarkerConfiguration)
5555

5656
@Override
5757
protected List<Template> getConstantsTemplates(Configuration freemarkerConfiguration) throws IOException {
58-
return Collections.singletonList(
59-
freemarkerConfiguration.getTemplate("templates/c/constants-template.h.ftlh"));
58+
return Arrays.asList(
59+
freemarkerConfiguration.getTemplate("templates/c/constants-template.h.ftlh"),
60+
freemarkerConfiguration.getTemplate("templates/c/constants-template.c.ftlh")
61+
);
6062
}
6163

6264
@Override
6365
protected List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException {
6466
return Arrays.asList(
6567
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.h.ftlh"),
66-
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.c.ftlh"));
68+
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.c.ftlh")
69+
);
6770
}
6871

6972
@Override
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<#--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<#-- Prevent freemarker from escaping stuff -->
20+
<#outputformat "undefined">
21+
<#-- Declare the name and type of variables passed in to the template -->
22+
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
23+
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
24+
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
25+
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.c.CLanguageTemplateHelper" -->
26+
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
27+
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
28+
<#-- Declare the name and type of variables declared locally inside the template -->
29+
<#-- @ftlvariable name="field" type="org.apache.plc4x.plugins.codegenerator.types.fields.Field" -->
30+
${helper.getSourceDirectory()?replace(".", "/")}/${helper.camelCaseToSnakeCase(type.name)}.c
31+
/*
32+
* Licensed to the Apache Software Foundation (ASF) under one
33+
* or more contributor license agreements. See the NOTICE file
34+
* distributed with this work for additional information
35+
* regarding copyright ownership. The ASF licenses this file
36+
* to you under the Apache License, Version 2.0 (the
37+
* "License"); you may not use this file except in compliance
38+
* with the License. You may obtain a copy of the License at
39+
*
40+
* https://www.apache.org/licenses/LICENSE-2.0
41+
*
42+
* Unless required by applicable law or agreed to in writing,
43+
* software distributed under the License is distributed on an
44+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
45+
* KIND, either express or implied. See the License for the
46+
* specific language governing permissions and limitations
47+
* under the License.
48+
*/
49+
50+
#ifndef ${helper.getCTypeName(type.name)?upper_case}_H_
51+
#define ${helper.getCTypeName(type.name)?upper_case}_H_
52+
53+
#include <stdbool.h>
54+
#include <stdint.h>
55+
#include <plc4c/spi/context.h>
56+
#include <plc4c/spi/read_buffer.h>
57+
#include <plc4c/spi/write_buffer.h>
58+
#include <plc4c/utils/list.h>
59+
60+
// Code generated by code-generation. DO NOT EDIT.
61+
62+
<#--
63+
When using const fields, output the constant reference values
64+
as global const values so we can use them elsewhere.
65+
-->
66+
<#if helper.getAllConstFields()?has_content>
67+
68+
// Constant values.
69+
<#list helper.getAllConstFields() as entry>
70+
<#assign constField=entry.key>
71+
<#assign parentTypeName=entry.value>
72+
static const ${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const = ${helper.toParseExpression(type, constField, constField.referenceValue, parserArguments)};
73+
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}() {
74+
return ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const;
75+
}
76+
</#list>
77+
</#if>
78+
79+
</#outputformat>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef PLC4C_MODBUS_READ_WRITE_CONSTANTS_H_
21+
#define PLC4C_MODBUS_READ_WRITE_CONSTANTS_H_
22+
23+
#include <stdbool.h>
24+
#include <stdint.h>
25+
#include <plc4c/spi/context.h>
26+
#include <plc4c/spi/read_buffer.h>
27+
#include <plc4c/spi/write_buffer.h>
28+
#include <plc4c/utils/list.h>
29+
30+
// Code generated by code-generation. DO NOT EDIT.
31+
32+
33+
// Constant values.
34+
static const uint16_t PLC4C_MODBUS_READ_WRITE_CONSTANTS_MODBUS_TCP_DEFAULT_PORT_const = 502;
35+
uint16_t PLC4C_MODBUS_READ_WRITE_CONSTANTS_MODBUS_TCP_DEFAULT_PORT() {
36+
return PLC4C_MODBUS_READ_WRITE_CONSTANTS_MODBUS_TCP_DEFAULT_PORT_const;
37+
}
38+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef PLC4C_PLC4X_READ_WRITE_CONSTANTS_H_
21+
#define PLC4C_PLC4X_READ_WRITE_CONSTANTS_H_
22+
23+
#include <stdbool.h>
24+
#include <stdint.h>
25+
#include <plc4c/spi/context.h>
26+
#include <plc4c/spi/read_buffer.h>
27+
#include <plc4c/spi/write_buffer.h>
28+
#include <plc4c/utils/list.h>
29+
30+
// Code generated by code-generation. DO NOT EDIT.
31+
32+
33+
// Constant values.
34+
static const uint16_t PLC4C_PLC4X_READ_WRITE_CONSTANTS_PLC4X_TCP_DEFAULT_PORT_const = 59837;
35+
uint16_t PLC4C_PLC4X_READ_WRITE_CONSTANTS_PLC4X_TCP_DEFAULT_PORT() {
36+
return PLC4C_PLC4X_READ_WRITE_CONSTANTS_PLC4X_TCP_DEFAULT_PORT_const;
37+
}
38+

0 commit comments

Comments
 (0)