Skip to content

Commit

Permalink
fix(codegen): Worked on implementing checksum fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz committed Mar 7, 2022
1 parent 18eb18f commit 845953f
Show file tree
Hide file tree
Showing 42 changed files with 367 additions and 404 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct ${helper.getCTypeName(type.name)} {
</#if>
</#list>
};
<#elseif (field.isTypedField() && field.type.isSimpleTypeReference() || helper.isEnumField(field)) && !field.isOptionalField() && !field.isArrayField() && !field.isManualArrayField()>
<#elseif (field.isTypedField() && field.asTypedField().orElseThrow().type.isSimpleTypeReference() || helper.isEnumField(field)) && !field.isOptionalField() && !field.isArrayField() && !field.isManualArrayField()>
<#assign namedField = field.asNamedField().orElseThrow()>
${helper.getLanguageTypeNameForField(field)} ${helper.camelCaseToSnakeCase(namedField.name)}${helper.getTypeSizeForField(field.asTypedField().orElseThrow())}<#if field.loopType??>${helper.getLoopExpressionSuffix(field.asTypedField().orElseThrow())}</#if>;
<#elseif field.isArrayField() || field.isManualArrayField()>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,12 +888,9 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if hasParserArguments>, ${pa
if _checksumRefErr != nil {
return nil, errors.Wrap(_checksumRefErr, "Error parsing 'checksum' field")<@emitImport import="github.com/pkg/errors" />
}
checksum, _checksumErr := ${helper.toParseExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}
if _checksumErr != nil {
return nil, errors.Wrap(_checksumErr, "Error parsing 'checksum' field")<@emitImport import="github.com/pkg/errors" />
}
checksum := ${helper.toParseExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}
if checksum != checksumRef {
return nil, errors.Errorf("Checksum verification failed. Expected %d but got %d", (checksumRef & 0xFFFF), (checksum & 0xFFFF))<@emitImport import="github.com/pkg/errors" />
return nil, errors.Errorf("Checksum verification failed. Expected %x but got %x", checksumRef, checksum)<@emitImport import="github.com/pkg/errors" />
}
}
<#break>
Expand Down Expand Up @@ -1515,11 +1512,8 @@ func (m *${type.name}) Serialize(writeBuffer utils.WriteBuffer) error {

// Checksum Field (checksum) (Calculated)
{
_checksum, _checksumErr := ${helper.toSerializationExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}
if _checksumErr != nil {
return errors.Wrap(_checksumErr, "Error serializing 'checksum' field")<@emitImport import="github.com/pkg/errors" />
}
_checksumErr = ${helper.getWriteBufferWriteMethodCall("checksum", simpleTypeReference, "(_checksum)", checksumField)}
_checksum := ${helper.toSerializationExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}
_checksumErr := ${helper.getWriteBufferWriteMethodCall("checksum", simpleTypeReference, "(_checksum)", checksumField)}
if _checksumErr != nil {
return errors.Wrap(_checksumErr, "Error serializing 'checksum' field")<@emitImport import="github.com/pkg/errors" />
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public<#if type.isDiscriminatedParentTypeDefinition()> abstract</#if> class ${ty
<#assign typedField = field.asTypedField().orElseThrow()>
<#assign namedField = field.asNamedField().orElseThrow()>

${helper.getLanguageTypeNameForField(field)} ${namedField.name} = read${field.typeName?cap_first}Field("${namedField.name}", ${helper.getDataReaderCall(typedField.type)}, ${helper.toParseExpression(checksumField, checksumField.type, checksumField.checksumExpression, parserArguments)}${helper.getFieldOptions(typedField, parserArguments)});
${helper.getLanguageTypeNameForField(field)} ${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)});
<#break>
<#case "const">
<#assign constField = field.asConstField().orElseThrow()>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
//Abstract fields can only be used within discriminated base types.
//[abstract unit 8 abstractField]
[array uint 8 arrayField count '5']
//TODO: Checksums fields are not supported in C
//[checksum uint 8 'checksumField' '100']
[checksum uint 8 checksumField '100']
[const uint 8 constField 5]
// Discriminated Field can't be used in simple type
//[discriminator uint 8 discriminatorField]
Expand All @@ -55,8 +54,7 @@
//Abstract fields can only be used within discriminated base types.
//[abstract unit 8 abstractField]
[array uint 8 arrayField count '5']
//TODO: Checksums fields are not supported in C
//[checksum uint 8 'checksumField' '100']
[checksum uint 8 checksumField '100']
[const uint 8 constField 5]
// Discriminated Field can't be used in simple type
//[discriminator uint 8 discriminatorField]
Expand All @@ -70,14 +68,6 @@
//[typeSwitch simpleField ]
]

/*
* TODO: doesn't compile in java
[type UFloatTypeTest
[simple ufloat 32 ufloatField]
[simple ufloat 64 udoubleField]
]
*/

/*
* TODO: doesn't compile in java
[type TimeTypeTest
Expand Down Expand Up @@ -161,18 +151,14 @@
[manualArray string 8 stringField count 'true' 'STATIC_CALL("parseString", readBuffer)' 'STATIC_CALL("serializeString", writeBuffer, _value)' '5' ]
]*/
//TODO: Checksums fields are not supported in C
//[type CheckSumTypeTest
//Bit field cannot be used for a checksum
//[checksum bit 'bitField' true]
//[checksum int 8 'intField' '100']
//[checksum uint 8 'uintField' '100']
//Float fields cannot be used as checksums
//[checksum float 32 'floatField' '100.0f']
//[checksum float 64 'doubleField' '100.0']
//String field cannot be used as a checksum
//[checksum vstring '11 * 8' 'stringField' '"HELLO TODDY"']
//]
[type CheckSumTypeTest
//Bit field cannot be used for a checksum: [checksum bit bitField 'true']
[checksum int 8 intField '100']
[checksum uint 8 uintField '100']
// Float fields cannot be used as checksums: [checksum float 32 floatField '100.0']
// Float fields cannot be used as checksums: [checksum float 64 doubleField '100.0']
// String field cannot be used as a checksum: [checksum vstring '11 * 8' stringField '"HELLO TODDY"']
]
[type ConstTypeTest
[const bit bitField true]
Expand Down
2 changes: 0 additions & 2 deletions plc4c/generated-sources/modbus/include/modbus_serial_adu.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ extern "C" {

struct plc4c_modbus_read_write_modbus_serial_adu {
/* Properties */
uint16_t transaction_id;
uint16_t length;
uint8_t address;
plc4c_modbus_read_write_modbus_pdu* pdu;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ plc4c_return_code plc4c_modbus_read_write_modbus_device_information_object_parse
}
(*_message)->object_id = objectId;

// Implicit Field (objectLength) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (objectLength) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t objectLength = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &objectLength);
if(_res != OK) {
Expand Down
36 changes: 18 additions & 18 deletions plc4c/generated-sources/modbus/src/modbus_pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x02 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x02 ) && ( response == true ) ) { /* ModbusPDUReadDiscreteInputsResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_discrete_inputs_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -245,7 +245,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x01 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x01 ) && ( response == true ) ) { /* ModbusPDUReadCoilsResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_coils_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -335,7 +335,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x0F ) && ( response == false )
(*_message)->modbus_pdu_write_multiple_coils_request_quantity = quantity;


// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -407,7 +407,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x04 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x04 ) && ( response == true ) ) { /* ModbusPDUReadInputRegistersResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_input_registers_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -459,7 +459,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x03 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x03 ) && ( response == true ) ) { /* ModbusPDUReadHoldingRegistersResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_holding_registers_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -549,7 +549,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x10 ) && ( response == false )
(*_message)->modbus_pdu_write_multiple_holding_registers_request_quantity = quantity;


// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -637,7 +637,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x17 ) && ( response == false )
(*_message)->modbus_pdu_read_write_multiple_holding_registers_request_write_quantity = writeQuantity;


// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -669,7 +669,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x17 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x17 ) && ( response == true ) ) { /* ModbusPDUReadWriteMultipleHoldingRegistersResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_write_multiple_holding_registers_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -770,15 +770,15 @@ if( ( errorFlag == false ) && ( functionFlag == 0x18 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x18 ) && ( response == true ) ) { /* ModbusPDUReadFifoQueueResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_fifo_queue_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint16_t byteCount = 0;
_res = plc4c_spi_read_unsigned_short(readBuffer, 16, (uint16_t*) &byteCount);
if(_res != OK) {
return _res;
}


// Implicit Field (fifoCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (fifoCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint16_t fifoCount = 0;
_res = plc4c_spi_read_unsigned_short(readBuffer, 16, (uint16_t*) &fifoCount);
if(_res != OK) {
Expand Down Expand Up @@ -810,7 +810,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x18 ) && ( response == true ) )
if( ( errorFlag == false ) && ( functionFlag == 0x14 ) && ( response == false ) ) { /* ModbusPDUReadFileRecordRequest */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_file_record_request;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -842,7 +842,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x14 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x14 ) && ( response == true ) ) { /* ModbusPDUReadFileRecordResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_read_file_record_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -874,7 +874,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x14 ) && ( response == true ) )
if( ( errorFlag == false ) && ( functionFlag == 0x15 ) && ( response == false ) ) { /* ModbusPDUWriteFileRecordRequest */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_write_file_record_request;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -906,7 +906,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x15 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x15 ) && ( response == true ) ) { /* ModbusPDUWriteFileRecordResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_write_file_record_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -1018,7 +1018,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x0C ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x0C ) && ( response == true ) ) { /* ModbusPDUGetComEventLogResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_get_com_event_log_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x11 ) && ( response == false )
if( ( errorFlag == false ) && ( functionFlag == 0x11 ) && ( response == true ) ) { /* ModbusPDUReportServerIdResponse */
(*_message)->_type = plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_report_server_id_response;

// Implicit Field (byteCount) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (byteCount) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t byteCount = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &byteCount);
if(_res != OK) {
Expand Down Expand Up @@ -1201,7 +1201,7 @@ if( ( errorFlag == false ) && ( functionFlag == 0x2B ) && ( response == true ) )
(*_message)->modbus_pdu_read_device_identification_response_next_object_id = nextObjectId;


// Implicit Field (numberOfObjects) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (numberOfObjects) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t numberOfObjects = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &numberOfObjects);
if(_res != OK) {
Expand Down Expand Up @@ -1243,7 +1243,7 @@ plc4c_return_code plc4c_modbus_read_write_modbus_pdu_serialize(plc4c_spi_write_b
// Discriminator Field (functionFlag)
plc4c_spi_write_unsigned_byte(writeBuffer, 7, plc4c_modbus_read_write_modbus_pdu_get_discriminator(_message->_type).functionFlag);

// Switch Field (Depending of the current type, serialize the sub-type elements)
// Switch Field (Depending on the current type, serialize the subtype elements)
switch(_message->_type) {
case plc4c_modbus_read_write_modbus_pdu_type_plc4c_modbus_read_write_modbus_pdu_error: {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ plc4c_return_code plc4c_modbus_read_write_modbus_pdu_read_file_record_response_i
return NO_MEMORY;
}

// Implicit Field (dataLength) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
// Implicit Field (dataLength) (Used for parsing, but its value is not stored as it's implicitly given by the objects content)
uint8_t dataLength = 0;
_res = plc4c_spi_read_unsigned_byte(readBuffer, 8, (uint8_t*) &dataLength);
if(_res != OK) {
Expand Down
Loading

0 comments on commit 845953f

Please sign in to comment.