Skip to content

Commit 97f95be

Browse files
committed
fix: Addressed the last warnings building plc4c (Still errors in the language test)
1 parent 39f2b73 commit 97f95be

11 files changed

Lines changed: 217 additions & 208 deletions

File tree

code-generation/language-base-freemarker/src/main/java/org/apache/plc4x/plugins/codegenerator/protocol/freemarker/BaseFreemarkerLanguageTemplateHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,15 @@ public Collection<EnumValue> getUniqueEnumValues(List<EnumValue> enumValues) {
300300
return filteredEnumValues.values();
301301
}
302302

303-
public Collection<EnumValue> getEnumValuesForUniqueConstantValues(List<EnumValue> enumValues, String constantName) {
303+
public Map<String, EnumValue> getEnumValuesForUniqueConstantValues(List<EnumValue> enumValues, String constantName) {
304304
Map<String, EnumValue> filteredEnumValues = new TreeMap<>();
305305
for (EnumValue enumValue : enumValues) {
306306
String key = enumValue.getConstant(constantName).orElseThrow(() -> new FreemarkerException("No constant name " + constantName + " found in enum value" + enumValue));
307-
if (!filteredEnumValues.containsKey(key)) {
307+
if(!"null".equalsIgnoreCase(key) && !filteredEnumValues.containsKey(key)) {
308308
filteredEnumValues.put(key, enumValue);
309309
}
310310
}
311-
return filteredEnumValues.values();
311+
return filteredEnumValues;
312312
}
313313

314314
public SimpleTypeReference getEnumFieldSimpleTypeReference(NonSimpleTypeReference type, String fieldName) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,16 @@ public String getWriteBufferWriteMethodCall(SimpleTypeReference simpleTypeRefere
572572
}
573573
String encoding = ((StringLiteral) encodingTerm).getValue();
574574
String length = Integer.toString(simpleTypeReference.getSizeInBits());
575+
int numChars;
576+
if("UTF-8".equalsIgnoreCase(encoding)) {
577+
numChars = simpleTypeReference.getSizeInBits() / 8;
578+
} else if("UTF-16".equalsIgnoreCase(encoding)) {
579+
numChars = simpleTypeReference.getSizeInBits() / 16;
580+
} else {
581+
throw new FreemarkerException("Unsupported encoding " + encoding);
582+
}
575583
return "plc4c_spi_write_string(writeBuffer, " + length + ", \"" +
576-
encoding + "\", (const uint8_t*) " + fieldName + ")";
584+
encoding + "\", (const uint8_t*) " + (numChars == 1 ? "&" : "") + fieldName + ")";
577585
}
578586
case VSTRING: {
579587
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));

code-generation/language/c/src/main/resources/templates/c/enum-template.c.ftlh

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,38 +157,33 @@ ${helper.getLanguageTypeNameForTypeReference(constantType)} ${helper.getCTypeNam
157157
${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_get_first_enum_for_field_${helper.camelCaseToSnakeCase(constantName)}(${helper.getLanguageTypeNameForTypeReference(constantType)} value) {
158158
<#assign uniqueEnumValues=helper.getEnumValuesForUniqueConstantValues(type.enumValues, constantName)>
159159
<#if constantType.isStringTypeReference() || constantType.isVstringTypeReference()>
160-
<#list helper.getEnumValuesForUniqueConstantValues(type.enumValues, constantName) as enumValue>
160+
<#list uniqueEnumValues?values as enumValue>
161161
if (strcmp(value, ${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}) == 0) {
162162
return ${helper.getCTypeName(type.name)}_${enumValue.name};
163163
}
164164
</#list>
165165
return (${helper.getCTypeName(type.name)}) 0;
166166
<#elseif constantType.isBooleanTypeReference()>
167-
if (!value) {
168-
<#assign enumValue=uniqueEnumValues[0]>
169-
return ${helper.getCTypeName(type.name)}_${enumValue.name};
170-
} else {
171-
<#if uniqueEnumValues?size &gt; 1>
172-
<#assign enumValue=uniqueEnumValues[1]>
173-
return ${helper.getCTypeName(type.name)}_${enumValue.name};
174-
<#else>
175-
return -1;
176-
</#if>
177-
}
167+
<#list uniqueEnumValues as enumKey, enumValue>
168+
if (value == ${enumKey}) {
169+
return ${helper.getCTypeName(type.name)}_${enumValue.name};
170+
}
171+
</#list>
172+
return -1;
178173
<#else>
179174
switch(value) {
180-
<#list uniqueEnumValues as enumValue>
175+
<#list uniqueEnumValues as enumKey, enumValue>
181176
<#if constantType.isSimpleTypeReference()>
182-
case ${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}: {
177+
case ${helper.escapeEnumValue(constantType, enumValue.getConstant(constantName).orElseThrow())?no_esc}: {
183178
<#elseif constantType.isEnumTypeReference()>
184-
case ${helper.getCTypeName(type.name)}_${enumValue.name}: /* '${enumValue.value}' */{
179+
case ${helper.getLanguageTypeNameForTypeReference(constantType)}_${enumKey}: {
185180
</#if>
186-
return ${helper.getCTypeName(type.name)}_${enumValue.name};
187-
}
181+
return ${helper.getCTypeName(type.name)}_${enumValue.name};
182+
}
188183
</#list>
189-
default: {
190-
return -1;
191-
}
184+
default: {
185+
return -1;
186+
}
192187
}
193188
</#if>
194189
}

plc4c/generated-sources/modbus/src/data_item.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ plc4c_return_code plc4c_modbus_read_write_data_item_serialize(plc4x_spi_context
828828
} else if((dataType == plc4c_modbus_read_write_modbus_data_type_CHAR) && (numberOfValues == 1)) { /* CHAR */
829829

830830
// Simple field (value)
831-
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) (*data_item)->data.char_value);
831+
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) &(*data_item)->data.char_value);
832832
if(_res != OK) {
833833
return _res;
834834
}
@@ -838,7 +838,7 @@ plc4c_return_code plc4c_modbus_read_write_data_item_serialize(plc4x_spi_context
838838
} else if((dataType == plc4c_modbus_read_write_modbus_data_type_WCHAR) && (numberOfValues == 1)) { /* WCHAR */
839839

840840
// Simple field (value)
841-
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) (*data_item)->data.wchar_value);
841+
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) &(*data_item)->data.wchar_value);
842842
if(_res != OK) {
843843
return _res;
844844
}

plc4c/generated-sources/modbus/src/modbus_data_type.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,21 +329,21 @@ uint8_t plc4c_modbus_read_write_modbus_data_type_get_data_type_size(plc4c_modbus
329329

330330
plc4c_modbus_read_write_modbus_data_type plc4c_modbus_read_write_modbus_data_type_get_first_enum_for_field_data_type_size(uint8_t value) {
331331
switch(value) {
332-
case 1: {
333-
return plc4c_modbus_read_write_modbus_data_type_CHAR;
334-
}
335-
case 2: {
336-
return plc4c_modbus_read_write_modbus_data_type_BOOL;
337-
}
338-
case 4: {
339-
return plc4c_modbus_read_write_modbus_data_type_DWORD;
340-
}
341-
case 8: {
342-
return plc4c_modbus_read_write_modbus_data_type_LWORD;
343-
}
344-
default: {
345-
return -1;
346-
}
332+
case 1: {
333+
return plc4c_modbus_read_write_modbus_data_type_CHAR;
334+
}
335+
case 2: {
336+
return plc4c_modbus_read_write_modbus_data_type_BOOL;
337+
}
338+
case 4: {
339+
return plc4c_modbus_read_write_modbus_data_type_DWORD;
340+
}
341+
case 8: {
342+
return plc4c_modbus_read_write_modbus_data_type_LWORD;
343+
}
344+
default: {
345+
return -1;
346+
}
347347
}
348348
}
349349

plc4c/generated-sources/plc4x/src/plc4x_value.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,14 @@ plc4c_return_code plc4c_plc4x_read_write_plc4x_value_serialize(plc4x_spi_context
440440
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_CHAR) { /* STRING */
441441

442442
// Simple field (value)
443-
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) (*data_item)->data.string_value);
443+
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) &(*data_item)->data.string_value);
444444
if(_res != OK) {
445445
return _res;
446446
}
447447
} else if(valueType == plc4c_plc4x_read_write_plc4x_value_type_WCHAR) { /* STRING */
448448

449449
// Simple field (value)
450-
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) (*data_item)->data.string_value);
450+
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) &(*data_item)->data.string_value);
451451
if(_res != OK) {
452452
return _res;
453453
}

plc4c/generated-sources/s7/src/cotp_tpdu_size.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,30 @@ uint16_t plc4c_s7_read_write_cotp_tpdu_size_get_size_in_bytes(plc4c_s7_read_writ
149149

150150
plc4c_s7_read_write_cotp_tpdu_size plc4c_s7_read_write_cotp_tpdu_size_get_first_enum_for_field_size_in_bytes(uint16_t value) {
151151
switch(value) {
152-
case 1024: {
153-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_1024;
154-
}
155-
case 128: {
156-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_128;
157-
}
158-
case 2048: {
159-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_2048;
160-
}
161-
case 256: {
162-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_256;
163-
}
164-
case 4096: {
165-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_4096;
166-
}
167-
case 512: {
168-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_512;
169-
}
170-
case 8192: {
171-
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_8192;
172-
}
173-
default: {
174-
return -1;
175-
}
152+
case 1024: {
153+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_1024;
154+
}
155+
case 128: {
156+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_128;
157+
}
158+
case 2048: {
159+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_2048;
160+
}
161+
case 256: {
162+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_256;
163+
}
164+
case 4096: {
165+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_4096;
166+
}
167+
case 512: {
168+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_512;
169+
}
170+
case 8192: {
171+
return plc4c_s7_read_write_cotp_tpdu_size_SIZE_8192;
172+
}
173+
default: {
174+
return -1;
175+
}
176176
}
177177
}
178178

plc4c/generated-sources/s7/src/data_item.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,14 @@ plc4c_return_code plc4c_s7_read_write_data_item_serialize(plc4x_spi_context ctx,
593593
} else if(strcmp(dataProtocolId, "IEC61131_CHAR") == 0) { /* CHAR */
594594

595595
// Simple field (value)
596-
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) (*data_item)->data.char_value);
596+
_res = plc4c_spi_write_string(writeBuffer, 8, "UTF-8", (const uint8_t*) &(*data_item)->data.char_value);
597597
if(_res != OK) {
598598
return _res;
599599
}
600600
} else if(strcmp(dataProtocolId, "IEC61131_WCHAR") == 0) { /* CHAR */
601601

602602
// Simple field (value)
603-
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) (*data_item)->data.char_value);
603+
_res = plc4c_spi_write_string(writeBuffer, 16, "UTF-16", (const uint8_t*) &(*data_item)->data.char_value);
604604
if(_res != OK) {
605605
return _res;
606606
}

plc4c/generated-sources/s7/src/data_transport_size.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ bool plc4c_s7_read_write_data_transport_size_get_size_in_bits(plc4c_s7_read_writ
148148
}
149149

150150
plc4c_s7_read_write_data_transport_size plc4c_s7_read_write_data_transport_size_get_first_enum_for_field_size_in_bits(bool value) {
151-
if (!value) {
152-
return plc4c_s7_read_write_data_transport_size_NULL;
153-
} else {
154-
return plc4c_s7_read_write_data_transport_size_BIT;
155-
}
151+
if (value == false) {
152+
return plc4c_s7_read_write_data_transport_size_NULL;
153+
}
154+
if (value == true) {
155+
return plc4c_s7_read_write_data_transport_size_BIT;
156+
}
157+
return -1;
156158
}
157159

158160
uint16_t plc4c_s7_read_write_data_transport_size_length_in_bytes(plc4x_spi_context ctx, plc4c_s7_read_write_data_transport_size* _message) {

0 commit comments

Comments
 (0)